From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTP id 9CF523983063 for ; Fri, 12 Jun 2020 21:53:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9CF523983063 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 79B05117F49; Fri, 12 Jun 2020 17:53:58 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id hY7nYAQEMSc0; Fri, 12 Jun 2020 17:53:58 -0400 (EDT) Received: from murgatroyd.Home (174-16-104-48.hlrn.qwest.net [174.16.104.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 34B23117F39; Fri, 12 Jun 2020 17:53:58 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 2/4] Rewrite target_read_string Date: Fri, 12 Jun 2020 15:53:54 -0600 Message-Id: <20200612215356.22145-3-tromey@adacore.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200612215356.22145-1-tromey@adacore.com> References: <20200612215356.22145-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Jun 2020 21:54:00 -0000 This rewrites target_read_string in terms of read_string. gdb/ChangeLog 2020-06-02 Tom Tromey * valprint.c (read_string): Update comment. * target.c (MIN): Remove. (target_read_string): Rewrite. --- gdb/ChangeLog | 6 +++++ gdb/target.c | 71 +++++++------------------------------------------- gdb/valprint.c | 8 +----- 3 files changed, 16 insertions(+), 69 deletions(-) diff --git a/gdb/target.c b/gdb/target.c index 82c405a8491..14c494688e0 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -803,9 +803,6 @@ target_xfer_status_to_string (enum target_xfer_status status) }; -#undef MIN -#define MIN(A, B) (((A) <= (B)) ? (A) : (B)) - /* target_read_string -- read a null terminated string, up to LEN bytes, from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful. Set *STRING to a pointer to malloc'd memory containing the data; the caller @@ -816,68 +813,18 @@ int target_read_string (CORE_ADDR memaddr, gdb::unique_xmalloc_ptr *string, int len, int *errnop) { - int tlen, offset, i; - gdb_byte buf[4]; - int errcode = 0; - char *buffer; - int buffer_allocated; - char *bufptr; - unsigned int nbytes_read = 0; - - gdb_assert (string); - - /* Small for testing. */ - buffer_allocated = 4; - buffer = (char *) xmalloc (buffer_allocated); - bufptr = buffer; - - while (len > 0) - { - tlen = MIN (len, 4 - (memaddr & 3)); - offset = memaddr & 3; + int bytes_read; + gdb::unique_xmalloc_ptr buffer; - errcode = target_read_memory (memaddr & ~3, buf, sizeof buf); - if (errcode != 0) - { - /* The transfer request might have crossed the boundary to an - unallocated region of memory. Retry the transfer, requesting - a single byte. */ - tlen = 1; - offset = 0; - errcode = target_read_memory (memaddr, buf, 1); - if (errcode != 0) - goto done; - } - - if (bufptr - buffer + tlen > buffer_allocated) - { - unsigned int bytes; + /* Note that the endian-ness does not matter here. */ + int errcode = read_string (memaddr, -1, 1, len, BFD_ENDIAN_LITTLE, + &buffer, &bytes_read); - bytes = bufptr - buffer; - buffer_allocated *= 2; - buffer = (char *) xrealloc (buffer, buffer_allocated); - bufptr = buffer + bytes; - } - - for (i = 0; i < tlen; i++) - { - *bufptr++ = buf[i + offset]; - if (buf[i + offset] == '\000') - { - nbytes_read += i + 1; - goto done; - } - } - - memaddr += tlen; - len -= tlen; - nbytes_read += tlen; - } -done: - string->reset (buffer); - if (errnop != NULL) + if (errnop != nullptr) *errnop = errcode; - return nbytes_read; + + string->reset ((char *) buffer.release ()); + return bytes_read; } struct target_section_table * diff --git a/gdb/valprint.c b/gdb/valprint.c index d5490898b9b..f2549805268 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -2027,13 +2027,7 @@ partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, Unless an exception is thrown, BUFFER will always be allocated, even on failure. In this case, some characters might have been read before the - failure happened. Check BYTES_READ to recognize this situation. - - Note: There was a FIXME asking to make this code use target_read_string, - but this function is more general (can read past null characters, up to - given LEN). Besides, it is used much more often than target_read_string - so it is more tested. Perhaps callers of target_read_string should use - this function instead? */ + failure happened. Check BYTES_READ to recognize this situation. */ int read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit, -- 2.21.3