From b17c7ab3808f5b781537514b28695dafaa29cb28 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 13 Apr 2022 06:42:19 -0600 Subject: [PATCH] Move target_read_string to target/target.c This moves the two overloads of target_read_string to a new file, target/target.c, and updates both gdb and gdbserver to build this. --- gdb/Makefile.in | 2 +- gdb/target.c | 19 ----- gdb/target.h | 8 -- gdb/target/target.c | 190 ++++++++++++++++++++++++++++++++++++++++++ gdb/target/target.h | 31 +++++++ gdb/valprint.c | 172 -------------------------------------- gdb/valprint.h | 5 -- gdbserver/Makefile.in | 2 + 8 files changed, 224 insertions(+), 205 deletions(-) create mode 100644 gdb/target/target.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 872fbe1f6df..c8e140b5544 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -487,7 +487,7 @@ SELFTESTS_SRCS = \ SELFTESTS_OBS = $(patsubst %.c,%.o,$(SELFTESTS_SRCS)) -SUBDIR_TARGET_SRCS = target/waitstatus.c +SUBDIR_TARGET_SRCS = target/target.c target/waitstatus.c SUBDIR_TARGET_OBS = $(patsubst %.c,%.o,$(SUBDIR_TARGET_SRCS)) diff --git a/gdb/target.c b/gdb/target.c index 5a416d5ee3a..7d291fd4d0d 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -1394,25 +1394,6 @@ target_xfer_status_to_string (enum target_xfer_status status) }; -/* See target.h. */ - -gdb::unique_xmalloc_ptr -target_read_string (CORE_ADDR memaddr, int len, int *bytes_read) -{ - gdb::unique_xmalloc_ptr buffer; - - int ignore; - if (bytes_read == nullptr) - bytes_read = &ignore; - - /* Note that the endian-ness does not matter here. */ - int errcode = target_read_string (memaddr, -1, 1, len, &buffer, bytes_read); - if (errcode != 0) - return {}; - - return gdb::unique_xmalloc_ptr ((char *) buffer.release ()); -} - const target_section_table * target_get_section_table (struct target_ops *target) { diff --git a/gdb/target.h b/gdb/target.h index c9791e850ac..093178af0bc 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -1567,14 +1567,6 @@ extern void target_dumpcore (const char *filename); extern bool target_can_run_breakpoint_commands (); -/* Read a string from target memory at address MEMADDR. The string - will be at most LEN bytes long (note that excess bytes may be read - in some cases -- but these will not be returned). Returns nullptr - on error. */ - -extern gdb::unique_xmalloc_ptr target_read_string - (CORE_ADDR memaddr, int len, int *bytes_read = nullptr); - /* For target_read_memory see target/target.h. */ extern int target_read_raw_memory (CORE_ADDR memaddr, gdb_byte *myaddr, diff --git a/gdb/target/target.c b/gdb/target/target.c new file mode 100644 index 00000000000..0b165bc05fe --- /dev/null +++ b/gdb/target/target.c @@ -0,0 +1,190 @@ +/* String reading + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "gdbsupport/common-defs.h" +#include "target/target.h" + +/* Read LEN bytes of target memory at address MEMADDR, placing the + results in GDB's memory at MYADDR. Returns a count of the bytes + actually read, and optionally a target_xfer_status value in the + location pointed to by ERRPTR if ERRPTR is non-null. */ + +static int +partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, + int len, int *errptr) +{ + int nread; /* Number of bytes actually read. */ + int errcode; /* Error from last read. */ + + /* First try a complete read. */ + errcode = target_read_memory (memaddr, myaddr, len); + if (errcode == 0) + { + /* Got it all. */ + nread = len; + } + else + { + /* Loop, reading one byte at a time until we get as much as we can. */ + for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--) + { + errcode = target_read_memory (memaddr++, myaddr++, 1); + } + /* If an error, the last read was unsuccessful, so adjust count. */ + if (errcode != 0) + { + nread--; + } + } + if (errptr != NULL) + { + *errptr = errcode; + } + return (nread); +} + +/* See target/target.h. */ + +int +target_read_string (CORE_ADDR addr, int len, int width, + unsigned int fetchlimit, + gdb::unique_xmalloc_ptr *buffer, + int *bytes_read) +{ + int errcode; /* Errno returned from bad reads. */ + unsigned int nfetch; /* Chars to fetch / chars fetched. */ + gdb_byte *bufptr; /* Pointer to next available byte in + buffer. */ + + /* Loop until we either have all the characters, or we encounter + some error, such as bumping into the end of the address space. */ + + buffer->reset (nullptr); + + if (len > 0) + { + /* We want fetchlimit chars, so we might as well read them all in + one operation. */ + unsigned int fetchlen = std::min ((unsigned) len, fetchlimit); + + buffer->reset ((gdb_byte *) xmalloc (fetchlen * width)); + bufptr = buffer->get (); + + nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode) + / width; + addr += nfetch * width; + bufptr += nfetch * width; + } + else if (len == -1) + { + unsigned long bufsize = 0; + unsigned int chunksize; /* Size of each fetch, in chars. */ + int found_nul; /* Non-zero if we found the nul char. */ + gdb_byte *limit; /* First location past end of fetch buffer. */ + + found_nul = 0; + /* We are looking for a NUL terminator to end the fetching, so we + might as well read in blocks that are large enough to be efficient, + but not so large as to be slow if fetchlimit happens to be large. + So we choose the minimum of 8 and fetchlimit. We used to use 200 + instead of 8 but 200 is way too big for remote debugging over a + serial line. */ + chunksize = std::min (8u, fetchlimit); + + do + { + nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize); + + if (*buffer == NULL) + buffer->reset ((gdb_byte *) xmalloc (nfetch * width)); + else + buffer->reset ((gdb_byte *) xrealloc (buffer->release (), + (nfetch + bufsize) * width)); + + bufptr = buffer->get () + bufsize * width; + bufsize += nfetch; + + /* Read as much as we can. */ + nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode) + / width; + + /* Scan this chunk for the null character that terminates the string + to print. If found, we don't need to fetch any more. Note + that bufptr is explicitly left pointing at the next character + after the null character, or at the next character after the end + of the buffer. */ + + limit = bufptr + nfetch * width; + while (bufptr < limit) + { + bool found_nonzero = false; + + for (int i = 0; !found_nonzero && i < width; ++i) + if (bufptr[i] != 0) + found_nonzero = true; + + addr += width; + bufptr += width; + if (!found_nonzero) + { + /* We don't care about any error which happened after + the NUL terminator. */ + errcode = 0; + found_nul = 1; + break; + } + } + } + while (errcode == 0 /* no error */ + && bufptr - buffer->get () < fetchlimit * width /* no overrun */ + && !found_nul); /* haven't found NUL yet */ + } + else + { /* Length of string is really 0! */ + /* We always allocate *buffer. */ + buffer->reset ((gdb_byte *) xmalloc (1)); + bufptr = buffer->get (); + errcode = 0; + } + + /* bufptr and addr now point immediately beyond the last byte which we + consider part of the string (including a '\0' which ends the string). */ + *bytes_read = bufptr - buffer->get (); + + return errcode; +} + +/* See target/target.h. */ + +gdb::unique_xmalloc_ptr +target_read_string (CORE_ADDR memaddr, int len, int *bytes_read) +{ + gdb::unique_xmalloc_ptr buffer; + + int ignore; + if (bytes_read == nullptr) + bytes_read = &ignore; + + /* Note that the endian-ness does not matter here. */ + int errcode = target_read_string (memaddr, -1, 1, len, &buffer, bytes_read); + if (errcode != 0) + return {}; + + return gdb::unique_xmalloc_ptr ((char *) buffer.release ()); +} diff --git a/gdb/target/target.h b/gdb/target/target.h index 818c8c6232f..a5b0dd3ed1a 100644 --- a/gdb/target/target.h +++ b/gdb/target/target.h @@ -48,6 +48,37 @@ extern int target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, extern int target_read_uint32 (CORE_ADDR memaddr, uint32_t *result); +/* Read a string from target memory at address MEMADDR. The string + will be at most LEN bytes long (note that excess bytes may be read + in some cases -- but these will not be returned). Returns nullptr + on error. */ + +extern gdb::unique_xmalloc_ptr target_read_string + (CORE_ADDR memaddr, int len, int *bytes_read = nullptr); + +/* Read a string from the inferior, at ADDR, with LEN characters of + WIDTH bytes each. Fetch at most FETCHLIMIT characters. BUFFER + will be set to a newly allocated buffer containing the string, and + BYTES_READ will be set to the number of bytes read. Returns 0 on + success, or a target_xfer_status on failure. + + If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters + (including eventual NULs in the middle or end of the string). + + If LEN is -1, stops at the first null character (not necessarily + the first null byte) up to a maximum of FETCHLIMIT characters. Set + FETCHLIMIT to UINT_MAX to read as many characters as possible from + the string. + + 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. */ + +extern int target_read_string (CORE_ADDR addr, int len, int width, + unsigned int fetchlimit, + gdb::unique_xmalloc_ptr *buffer, + int *bytes_read); + /* Write LEN bytes from MYADDR to target memory at address MEMADDR. Return zero for success, nonzero if any error occurs. This function must be provided by the client. Implementations of this diff --git a/gdb/valprint.c b/gdb/valprint.c index 8f9954778c5..47114676934 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -85,9 +85,6 @@ struct cmd_list_element *showprintrawlist; /* Prototypes for local functions */ -static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, - int len, int *errptr); - static void set_input_radix_1 (int, unsigned); static void set_output_radix_1 (int, unsigned); @@ -1989,175 +1986,6 @@ value_print_array_elements (struct value *val, struct ui_file *stream, } } -/* Read LEN bytes of target memory at address MEMADDR, placing the - results in GDB's memory at MYADDR. Returns a count of the bytes - actually read, and optionally a target_xfer_status value in the - location pointed to by ERRPTR if ERRPTR is non-null. */ - -/* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this - function be eliminated. */ - -static int -partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, - int len, int *errptr) -{ - int nread; /* Number of bytes actually read. */ - int errcode; /* Error from last read. */ - - /* First try a complete read. */ - errcode = target_read_memory (memaddr, myaddr, len); - if (errcode == 0) - { - /* Got it all. */ - nread = len; - } - else - { - /* Loop, reading one byte at a time until we get as much as we can. */ - for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--) - { - errcode = target_read_memory (memaddr++, myaddr++, 1); - } - /* If an error, the last read was unsuccessful, so adjust count. */ - if (errcode != 0) - { - nread--; - } - } - if (errptr != NULL) - { - *errptr = errcode; - } - return (nread); -} - -/* Read a string from the inferior, at ADDR, with LEN characters of - WIDTH bytes each. Fetch at most FETCHLIMIT characters. BUFFER - will be set to a newly allocated buffer containing the string, and - BYTES_READ will be set to the number of bytes read. Returns 0 on - success, or a target_xfer_status on failure. - - If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters - (including eventual NULs in the middle or end of the string). - - If LEN is -1, stops at the first null character (not necessarily - the first null byte) up to a maximum of FETCHLIMIT characters. Set - FETCHLIMIT to UINT_MAX to read as many characters as possible from - the string. - - 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. */ - -int -target_read_string (CORE_ADDR addr, int len, int width, - unsigned int fetchlimit, - gdb::unique_xmalloc_ptr *buffer, - int *bytes_read) -{ - int errcode; /* Errno returned from bad reads. */ - unsigned int nfetch; /* Chars to fetch / chars fetched. */ - gdb_byte *bufptr; /* Pointer to next available byte in - buffer. */ - - /* Loop until we either have all the characters, or we encounter - some error, such as bumping into the end of the address space. */ - - buffer->reset (nullptr); - - if (len > 0) - { - /* We want fetchlimit chars, so we might as well read them all in - one operation. */ - unsigned int fetchlen = std::min ((unsigned) len, fetchlimit); - - buffer->reset ((gdb_byte *) xmalloc (fetchlen * width)); - bufptr = buffer->get (); - - nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode) - / width; - addr += nfetch * width; - bufptr += nfetch * width; - } - else if (len == -1) - { - unsigned long bufsize = 0; - unsigned int chunksize; /* Size of each fetch, in chars. */ - int found_nul; /* Non-zero if we found the nul char. */ - gdb_byte *limit; /* First location past end of fetch buffer. */ - - found_nul = 0; - /* We are looking for a NUL terminator to end the fetching, so we - might as well read in blocks that are large enough to be efficient, - but not so large as to be slow if fetchlimit happens to be large. - So we choose the minimum of 8 and fetchlimit. We used to use 200 - instead of 8 but 200 is way too big for remote debugging over a - serial line. */ - chunksize = std::min (8u, fetchlimit); - - do - { - nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize); - - if (*buffer == NULL) - buffer->reset ((gdb_byte *) xmalloc (nfetch * width)); - else - buffer->reset ((gdb_byte *) xrealloc (buffer->release (), - (nfetch + bufsize) * width)); - - bufptr = buffer->get () + bufsize * width; - bufsize += nfetch; - - /* Read as much as we can. */ - nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode) - / width; - - /* Scan this chunk for the null character that terminates the string - to print. If found, we don't need to fetch any more. Note - that bufptr is explicitly left pointing at the next character - after the null character, or at the next character after the end - of the buffer. */ - - limit = bufptr + nfetch * width; - while (bufptr < limit) - { - bool found_nonzero = false; - - for (int i = 0; !found_nonzero && i < width; ++i) - if (bufptr[i] != 0) - found_nonzero = true; - - addr += width; - bufptr += width; - if (!found_nonzero) - { - /* We don't care about any error which happened after - the NUL terminator. */ - errcode = 0; - found_nul = 1; - break; - } - } - } - while (errcode == 0 /* no error */ - && bufptr - buffer->get () < fetchlimit * width /* no overrun */ - && !found_nul); /* haven't found NUL yet */ - } - else - { /* Length of string is really 0! */ - /* We always allocate *buffer. */ - buffer->reset ((gdb_byte *) xmalloc (1)); - bufptr = buffer->get (); - errcode = 0; - } - - /* bufptr and addr now point immediately beyond the last byte which we - consider part of the string (including a '\0' which ends the string). */ - *bytes_read = bufptr - buffer->get (); - - return errcode; -} - /* Return true if print_wchar can display W without resorting to a numeric escape, false otherwise. */ diff --git a/gdb/valprint.h b/gdb/valprint.h index b12495b10c1..ff536fbe4f0 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -165,11 +165,6 @@ extern void print_function_pointer_address (const struct value_print_options *op CORE_ADDR address, struct ui_file *stream); -extern int target_read_string (CORE_ADDR addr, int len, int width, - unsigned int fetchlimit, - gdb::unique_xmalloc_ptr *buffer, - int *bytes_read); - /* Helper function to check the validity of some bits of a value. If TYPE represents some aggregate type (e.g., a structure), return 1. diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in index 47648b8d962..b9a687c9231 100644 --- a/gdbserver/Makefile.in +++ b/gdbserver/Makefile.in @@ -223,6 +223,7 @@ SFILES = \ $(srcdir)/../gdb/nat/ppc-linux.c \ $(srcdir)/../gdb/nat/riscv-linux-tdesc.c \ $(srcdir)/../gdb/nat/fork-inferior.c \ + $(srcdir)/../gdb/target/target.c \ $(srcdir)/../gdb/target/waitstatus.c DEPFILES = @GDBSERVER_DEPFILES@ @@ -250,6 +251,7 @@ OBS = \ tracepoint.o \ utils.o \ version.o \ + target/target.o \ target/waitstatus.o \ $(DEPFILES) \ $(LIBOBJS) \ -- 2.30.2