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))
};
-/* See target.h. */
-
-gdb::unique_xmalloc_ptr<char>
-target_read_string (CORE_ADDR memaddr, int len, int *bytes_read)
-{
- gdb::unique_xmalloc_ptr<gdb_byte> 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> ((char *) buffer.release ());
-}
-
const target_section_table *
target_get_section_table (struct target_ops *target)
{
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<char> 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,
--- /dev/null
+/* 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 <http://www.gnu.org/licenses/>. */
+
+#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<gdb_byte> *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<char>
+target_read_string (CORE_ADDR memaddr, int len, int *bytes_read)
+{
+ gdb::unique_xmalloc_ptr<gdb_byte> 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> ((char *) buffer.release ());
+}
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<char> 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<gdb_byte> *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
/* 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);
}
}
-/* 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<gdb_byte> *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. */
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<gdb_byte> *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.
$(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@
tracepoint.o \
utils.o \
version.o \
+ target/target.o \
target/waitstatus.o \
$(DEPFILES) \
$(LIBOBJS) \