From: Tom Tromey Date: Mon, 15 Jun 2020 12:28:09 +0000 (-0600) Subject: Rewrite target_read_string X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=670e35fad9c17e8e166c5a6260201eebcc2ba9e6;p=binutils-gdb.git Rewrite target_read_string This rewrites target_read_string in terms of read_string. gdb/ChangeLog 2020-06-15 Tom Tromey * valprint.c (read_string): Update comment. * target.c (MIN): Remove. (target_read_string): Rewrite. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 1ebe8f3f894..4f12edc7264 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2020-06-15 Tom Tromey + + * valprint.c (read_string): Update comment. + * target.c (MIN): Remove. + (target_read_string): Rewrite. + 2020-06-15 Tom Tromey * corefile.c (read_memory_string): Remove. diff --git a/gdb/target.c b/gdb/target.c index 82c405a8491..897b8fdd32b 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -50,6 +50,7 @@ #include "terminal.h" #include #include "target-connection.h" +#include "valprint.h" static void generic_tls_error (void) ATTRIBUTE_NORETURN; @@ -803,9 +804,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 +814,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,