3 Copyright (C) 2022-2024 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 "target/target.h"
22 /* Read LEN bytes of target memory at address MEMADDR, placing the
23 results in GDB's memory at MYADDR. Returns a count of the bytes
24 actually read, and optionally a target_xfer_status value in the
25 location pointed to by ERRPTR if ERRPTR is non-null. */
28 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
31 int nread
; /* Number of bytes actually read. */
32 int errcode
; /* Error from last read. */
34 /* First try a complete read. */
35 errcode
= target_read_memory (memaddr
, myaddr
, len
);
43 /* Loop, reading one byte at a time until we get as much as we can. */
44 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
46 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
48 /* If an error, the last read was unsuccessful, so adjust count. */
61 /* See target/target.h. */
64 target_read_string (CORE_ADDR addr
, int len
, int width
,
65 unsigned int fetchlimit
,
66 gdb::unique_xmalloc_ptr
<gdb_byte
> *buffer
,
69 int errcode
; /* Errno returned from bad reads. */
70 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
71 gdb_byte
*bufptr
; /* Pointer to next available byte in
74 /* Loop until we either have all the characters, or we encounter
75 some error, such as bumping into the end of the address space. */
77 buffer
->reset (nullptr);
81 /* We want fetchlimit chars, so we might as well read them all in
83 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
85 buffer
->reset ((gdb_byte
*) xmalloc (fetchlen
* width
));
86 bufptr
= buffer
->get ();
88 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
90 addr
+= nfetch
* width
;
91 bufptr
+= nfetch
* width
;
95 unsigned long bufsize
= 0;
96 unsigned int chunksize
; /* Size of each fetch, in chars. */
97 int found_nul
; /* Non-zero if we found the nul char. */
98 gdb_byte
*limit
; /* First location past end of fetch buffer. */
101 /* We are looking for a NUL terminator to end the fetching, so we
102 might as well read in blocks that are large enough to be efficient,
103 but not so large as to be slow if fetchlimit happens to be large.
104 So we choose the minimum of 8 and fetchlimit. We used to use 200
105 instead of 8 but 200 is way too big for remote debugging over a
107 chunksize
= std::min (8u, fetchlimit
);
111 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
114 buffer
->reset ((gdb_byte
*) xmalloc (nfetch
* width
));
116 buffer
->reset ((gdb_byte
*) xrealloc (buffer
->release (),
117 (nfetch
+ bufsize
) * width
));
119 bufptr
= buffer
->get () + bufsize
* width
;
122 /* Read as much as we can. */
123 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
126 /* Scan this chunk for the null character that terminates the string
127 to print. If found, we don't need to fetch any more. Note
128 that bufptr is explicitly left pointing at the next character
129 after the null character, or at the next character after the end
132 limit
= bufptr
+ nfetch
* width
;
133 while (bufptr
< limit
)
135 bool found_nonzero
= false;
137 for (int i
= 0; !found_nonzero
&& i
< width
; ++i
)
139 found_nonzero
= true;
145 /* We don't care about any error which happened after
146 the NUL terminator. */
153 while (errcode
== 0 /* no error */
154 && bufptr
- buffer
->get () < fetchlimit
* width
/* no overrun */
155 && !found_nul
); /* haven't found NUL yet */
158 { /* Length of string is really 0! */
159 /* We always allocate *buffer. */
160 buffer
->reset ((gdb_byte
*) xmalloc (1));
161 bufptr
= buffer
->get ();
165 /* bufptr and addr now point immediately beyond the last byte which we
166 consider part of the string (including a '\0' which ends the string). */
167 *bytes_read
= bufptr
- buffer
->get ();
172 /* See target/target.h. */
174 gdb::unique_xmalloc_ptr
<char>
175 target_read_string (CORE_ADDR memaddr
, int len
, int *bytes_read
)
177 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
180 if (bytes_read
== nullptr)
181 bytes_read
= &ignore
;
183 /* Note that the endian-ness does not matter here. */
184 int errcode
= target_read_string (memaddr
, -1, 1, len
, &buffer
, bytes_read
);
188 return gdb::unique_xmalloc_ptr
<char> ((char *) buffer
.release ());
191 /* See target/target.h. */
194 to_string (gdb_thread_options options
)
196 static constexpr gdb_thread_options::string_mapping mapping
[] = {
197 MAP_ENUM_FLAG (GDB_THREAD_OPTION_CLONE
),
198 MAP_ENUM_FLAG (GDB_THREAD_OPTION_EXIT
),
200 return options
.to_string (mapping
);