3 Copyright (C) 2022-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "target/target.h"
23 /* Read LEN bytes of target memory at address MEMADDR, placing the
24 results in GDB's memory at MYADDR. Returns a count of the bytes
25 actually read, and optionally a target_xfer_status value in the
26 location pointed to by ERRPTR if ERRPTR is non-null. */
29 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
32 int nread
; /* Number of bytes actually read. */
33 int errcode
; /* Error from last read. */
35 /* First try a complete read. */
36 errcode
= target_read_memory (memaddr
, myaddr
, len
);
44 /* Loop, reading one byte at a time until we get as much as we can. */
45 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
47 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
49 /* If an error, the last read was unsuccessful, so adjust count. */
62 /* See target/target.h. */
65 target_read_string (CORE_ADDR addr
, int len
, int width
,
66 unsigned int fetchlimit
,
67 gdb::unique_xmalloc_ptr
<gdb_byte
> *buffer
,
70 int errcode
; /* Errno returned from bad reads. */
71 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
72 gdb_byte
*bufptr
; /* Pointer to next available byte in
75 /* Loop until we either have all the characters, or we encounter
76 some error, such as bumping into the end of the address space. */
78 buffer
->reset (nullptr);
82 /* We want fetchlimit chars, so we might as well read them all in
84 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
86 buffer
->reset ((gdb_byte
*) xmalloc (fetchlen
* width
));
87 bufptr
= buffer
->get ();
89 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
91 addr
+= nfetch
* width
;
92 bufptr
+= nfetch
* width
;
96 unsigned long bufsize
= 0;
97 unsigned int chunksize
; /* Size of each fetch, in chars. */
98 int found_nul
; /* Non-zero if we found the nul char. */
99 gdb_byte
*limit
; /* First location past end of fetch buffer. */
102 /* We are looking for a NUL terminator to end the fetching, so we
103 might as well read in blocks that are large enough to be efficient,
104 but not so large as to be slow if fetchlimit happens to be large.
105 So we choose the minimum of 8 and fetchlimit. We used to use 200
106 instead of 8 but 200 is way too big for remote debugging over a
108 chunksize
= std::min (8u, fetchlimit
);
112 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
115 buffer
->reset ((gdb_byte
*) xmalloc (nfetch
* width
));
117 buffer
->reset ((gdb_byte
*) xrealloc (buffer
->release (),
118 (nfetch
+ bufsize
) * width
));
120 bufptr
= buffer
->get () + bufsize
* width
;
123 /* Read as much as we can. */
124 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
127 /* Scan this chunk for the null character that terminates the string
128 to print. If found, we don't need to fetch any more. Note
129 that bufptr is explicitly left pointing at the next character
130 after the null character, or at the next character after the end
133 limit
= bufptr
+ nfetch
* width
;
134 while (bufptr
< limit
)
136 bool found_nonzero
= false;
138 for (int i
= 0; !found_nonzero
&& i
< width
; ++i
)
140 found_nonzero
= true;
146 /* We don't care about any error which happened after
147 the NUL terminator. */
154 while (errcode
== 0 /* no error */
155 && bufptr
- buffer
->get () < fetchlimit
* width
/* no overrun */
156 && !found_nul
); /* haven't found NUL yet */
159 { /* Length of string is really 0! */
160 /* We always allocate *buffer. */
161 buffer
->reset ((gdb_byte
*) xmalloc (1));
162 bufptr
= buffer
->get ();
166 /* bufptr and addr now point immediately beyond the last byte which we
167 consider part of the string (including a '\0' which ends the string). */
168 *bytes_read
= bufptr
- buffer
->get ();
173 /* See target/target.h. */
175 gdb::unique_xmalloc_ptr
<char>
176 target_read_string (CORE_ADDR memaddr
, int len
, int *bytes_read
)
178 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
181 if (bytes_read
== nullptr)
182 bytes_read
= &ignore
;
184 /* Note that the endian-ness does not matter here. */
185 int errcode
= target_read_string (memaddr
, -1, 1, len
, &buffer
, bytes_read
);
189 return gdb::unique_xmalloc_ptr
<char> ((char *) buffer
.release ());