]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Core dump and executable file functions above target vector, for GDB. |
1bac305b | 2 | |
d01e8234 | 3 | Copyright (C) 1986-2025 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 19 | |
d55e5aa6 | 20 | #include <signal.h> |
4de283e4 | 21 | #include <fcntl.h> |
e5dc0d5d | 22 | #include "event-top.h" |
ec452525 | 23 | #include "extract-store-integer.h" |
4de283e4 TT |
24 | #include "inferior.h" |
25 | #include "symtab.h" | |
c906108c | 26 | #include "command.h" |
5b9707eb | 27 | #include "cli/cli-cmds.h" |
4de283e4 TT |
28 | #include "bfd.h" |
29 | #include "target.h" | |
c906108c | 30 | #include "gdbcore.h" |
4de283e4 TT |
31 | #include "dis-asm.h" |
32 | #include <sys/stat.h> | |
33 | #include "completer.h" | |
76727919 | 34 | #include "observable.h" |
4de283e4 | 35 | #include "cli/cli-utils.h" |
0d12e84c | 36 | #include "gdbarch.h" |
ec517d10 | 37 | #include "interps.h" |
d3d13bf8 | 38 | #include "arch-utils.h" |
c906108c | 39 | |
c906108c | 40 | void |
fba45db2 | 41 | reopen_exec_file (void) |
c906108c | 42 | { |
70fd94b2 | 43 | bfd *exec_bfd = current_program_space->exec_bfd (); |
c906108c | 44 | |
aff410f1 | 45 | /* Don't do anything if there isn't an exec file. */ |
70fd94b2 | 46 | if (exec_bfd == nullptr) |
c906108c | 47 | return; |
c5aa993b | 48 | |
70fd94b2 AB |
49 | /* The main executable can't be an in-memory BFD object. If it was then |
50 | the use of bfd_stat below would not work as expected. */ | |
51 | gdb_assert ((exec_bfd->flags & BFD_IN_MEMORY) == 0); | |
52 | ||
aff410f1 | 53 | /* If the timestamp of the exec file has changed, reopen it. */ |
70fd94b2 | 54 | struct stat st; |
a357defd | 55 | int res = gdb_bfd_stat (exec_bfd, &st); |
c906108c | 56 | |
5a36e715 | 57 | if (res == 0 |
70fd94b2 | 58 | && current_program_space->ebfd_mtime != 0 |
5a36e715 | 59 | && current_program_space->ebfd_mtime != st.st_mtime) |
70fd94b2 | 60 | exec_file_attach (bfd_get_filename (exec_bfd), 0); |
c906108c SS |
61 | } |
62 | \f | |
63 | /* If we have both a core file and an exec file, | |
64 | print a warning if they don't go together. */ | |
65 | ||
66 | void | |
fba45db2 | 67 | validate_files (void) |
c906108c | 68 | { |
6fdf95ae | 69 | if (current_program_space->exec_bfd () && current_program_space->core_bfd ()) |
c906108c | 70 | { |
6fdf95ae | 71 | if (!core_file_matches_executable_p (current_program_space->core_bfd (), |
7e10abd1 | 72 | current_program_space->exec_bfd ())) |
8a3fe4f8 | 73 | warning (_("core file may not match specified executable file.")); |
a357defd TT |
74 | else if (gdb_bfd_get_mtime (current_program_space->exec_bfd ()) |
75 | > gdb_bfd_get_mtime (current_program_space->core_bfd ())) | |
8a3fe4f8 | 76 | warning (_("exec file is newer than core file.")); |
c906108c SS |
77 | } |
78 | } | |
79 | ||
d3d13bf8 AB |
80 | /* See arch-utils.h. */ |
81 | ||
82 | core_file_exec_context | |
83 | default_core_parse_exec_context (struct gdbarch *gdbarch, bfd *cbfd) | |
84 | { | |
85 | return {}; | |
86 | } | |
87 | \f | |
88 | ||
1ccbe998 | 89 | std::string |
9b409511 | 90 | memory_error_message (enum target_xfer_status err, |
578d3588 | 91 | struct gdbarch *gdbarch, CORE_ADDR memaddr) |
6be7b56e PA |
92 | { |
93 | switch (err) | |
94 | { | |
95 | case TARGET_XFER_E_IO: | |
96 | /* Actually, address between memaddr and memaddr + len was out of | |
97 | bounds. */ | |
1ccbe998 TT |
98 | return string_printf (_("Cannot access memory at address %s"), |
99 | paddress (gdbarch, memaddr)); | |
bc113b4e | 100 | case TARGET_XFER_UNAVAILABLE: |
1ccbe998 TT |
101 | return string_printf (_("Memory at address %s unavailable."), |
102 | paddress (gdbarch, memaddr)); | |
6be7b56e | 103 | default: |
f34652de | 104 | internal_error ("unhandled target_xfer_status: %s (%s)", |
9b409511 | 105 | target_xfer_status_to_string (err), |
6be7b56e PA |
106 | plongest (err)); |
107 | } | |
108 | } | |
109 | ||
578d3588 | 110 | /* Report a memory error by throwing a suitable exception. */ |
c906108c SS |
111 | |
112 | void | |
9b409511 | 113 | memory_error (enum target_xfer_status err, CORE_ADDR memaddr) |
c906108c | 114 | { |
8635b3bf | 115 | enum errors exception = GDB_NO_ERROR; |
578d3588 PA |
116 | |
117 | /* Build error string. */ | |
99d9c3b9 SM |
118 | std::string str |
119 | = memory_error_message (err, current_inferior ()->arch (), memaddr); | |
578d3588 PA |
120 | |
121 | /* Choose the right error to throw. */ | |
122 | switch (err) | |
123 | { | |
124 | case TARGET_XFER_E_IO: | |
8635b3bf | 125 | exception = MEMORY_ERROR; |
578d3588 | 126 | break; |
bc113b4e | 127 | case TARGET_XFER_UNAVAILABLE: |
8635b3bf | 128 | exception = NOT_AVAILABLE_ERROR; |
578d3588 PA |
129 | break; |
130 | } | |
131 | ||
132 | /* Throw it. */ | |
1ccbe998 | 133 | throw_error (exception, ("%s"), str.c_str ()); |
c906108c SS |
134 | } |
135 | ||
edf689f0 | 136 | /* Helper function. */ |
4e5d721f | 137 | |
edf689f0 YQ |
138 | static void |
139 | read_memory_object (enum target_object object, CORE_ADDR memaddr, | |
140 | gdb_byte *myaddr, ssize_t len) | |
c906108c | 141 | { |
9b409511 | 142 | ULONGEST xfered = 0; |
c5504eaf | 143 | |
6be7b56e PA |
144 | while (xfered < len) |
145 | { | |
9b409511 YQ |
146 | enum target_xfer_status status; |
147 | ULONGEST xfered_len; | |
6be7b56e | 148 | |
328d42d8 SM |
149 | status = target_xfer_partial (current_inferior ()->top_target (), object, |
150 | NULL, myaddr + xfered, NULL, | |
9b409511 YQ |
151 | memaddr + xfered, len - xfered, |
152 | &xfered_len); | |
153 | ||
5c328c05 YQ |
154 | if (status != TARGET_XFER_OK) |
155 | memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status, | |
156 | memaddr + xfered); | |
9b409511 | 157 | |
9b409511 | 158 | xfered += xfered_len; |
6be7b56e PA |
159 | QUIT; |
160 | } | |
c906108c SS |
161 | } |
162 | ||
edf689f0 YQ |
163 | /* Same as target_read_memory, but report an error if can't read. */ |
164 | ||
165 | void | |
166 | read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) | |
167 | { | |
168 | read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len); | |
169 | } | |
170 | ||
4e5d721f DE |
171 | /* Same as target_read_stack, but report an error if can't read. */ |
172 | ||
173 | void | |
45aa4659 | 174 | read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) |
4e5d721f | 175 | { |
edf689f0 | 176 | read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len); |
4e5d721f DE |
177 | } |
178 | ||
0865b04a YQ |
179 | /* Same as target_read_code, but report an error if can't read. */ |
180 | ||
181 | void | |
182 | read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) | |
183 | { | |
edf689f0 | 184 | read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len); |
0865b04a YQ |
185 | } |
186 | ||
ee8ff470 KB |
187 | /* Read memory at MEMADDR of length LEN and put the contents in |
188 | RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero | |
189 | if successful. */ | |
190 | ||
16a0f3e7 | 191 | int |
c5504eaf MS |
192 | safe_read_memory_integer (CORE_ADDR memaddr, int len, |
193 | enum bfd_endian byte_order, | |
e17a4113 | 194 | LONGEST *return_value) |
16a0f3e7 | 195 | { |
5e43d467 | 196 | gdb_byte buf[sizeof (LONGEST)]; |
16a0f3e7 | 197 | |
5e43d467 UW |
198 | if (target_read_memory (memaddr, buf, len)) |
199 | return 0; | |
16a0f3e7 | 200 | |
5e43d467 UW |
201 | *return_value = extract_signed_integer (buf, len, byte_order); |
202 | return 1; | |
16a0f3e7 EZ |
203 | } |
204 | ||
cc2c4da8 MK |
205 | /* Read memory at MEMADDR of length LEN and put the contents in |
206 | RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero | |
207 | if successful. */ | |
208 | ||
209 | int | |
210 | safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len, | |
211 | enum bfd_endian byte_order, | |
212 | ULONGEST *return_value) | |
213 | { | |
214 | gdb_byte buf[sizeof (ULONGEST)]; | |
215 | ||
216 | if (target_read_memory (memaddr, buf, len)) | |
217 | return 0; | |
218 | ||
219 | *return_value = extract_unsigned_integer (buf, len, byte_order); | |
220 | return 1; | |
221 | } | |
222 | ||
c906108c | 223 | LONGEST |
aff410f1 MS |
224 | read_memory_integer (CORE_ADDR memaddr, int len, |
225 | enum bfd_endian byte_order) | |
c906108c | 226 | { |
dfb65433 | 227 | gdb_byte buf[sizeof (LONGEST)]; |
c906108c SS |
228 | |
229 | read_memory (memaddr, buf, len); | |
e17a4113 | 230 | return extract_signed_integer (buf, len, byte_order); |
c906108c SS |
231 | } |
232 | ||
233 | ULONGEST | |
aff410f1 MS |
234 | read_memory_unsigned_integer (CORE_ADDR memaddr, int len, |
235 | enum bfd_endian byte_order) | |
c906108c | 236 | { |
dfb65433 | 237 | gdb_byte buf[sizeof (ULONGEST)]; |
c906108c SS |
238 | |
239 | read_memory (memaddr, buf, len); | |
e17a4113 | 240 | return extract_unsigned_integer (buf, len, byte_order); |
c906108c SS |
241 | } |
242 | ||
0865b04a YQ |
243 | LONGEST |
244 | read_code_integer (CORE_ADDR memaddr, int len, | |
245 | enum bfd_endian byte_order) | |
246 | { | |
247 | gdb_byte buf[sizeof (LONGEST)]; | |
248 | ||
249 | read_code (memaddr, buf, len); | |
250 | return extract_signed_integer (buf, len, byte_order); | |
251 | } | |
252 | ||
253 | ULONGEST | |
254 | read_code_unsigned_integer (CORE_ADDR memaddr, int len, | |
255 | enum bfd_endian byte_order) | |
256 | { | |
257 | gdb_byte buf[sizeof (ULONGEST)]; | |
258 | ||
259 | read_code (memaddr, buf, len); | |
260 | return extract_unsigned_integer (buf, len, byte_order); | |
261 | } | |
262 | ||
0d540cdf KD |
263 | CORE_ADDR |
264 | read_memory_typed_address (CORE_ADDR addr, struct type *type) | |
265 | { | |
df86565b | 266 | gdb_byte *buf = (gdb_byte *) alloca (type->length ()); |
c5504eaf | 267 | |
df86565b | 268 | read_memory (addr, buf, type->length ()); |
0d540cdf KD |
269 | return extract_typed_address (buf, type); |
270 | } | |
271 | ||
cb6f16cf SM |
272 | /* See gdbcore.h. */ |
273 | ||
c26e4683 | 274 | void |
aff410f1 | 275 | write_memory (CORE_ADDR memaddr, |
45aa4659 | 276 | const bfd_byte *myaddr, ssize_t len) |
c26e4683 JB |
277 | { |
278 | int status; | |
c5504eaf | 279 | |
00630ca8 | 280 | status = target_write_memory (memaddr, myaddr, len); |
c26e4683 | 281 | if (status != 0) |
d09f2c3f | 282 | memory_error (TARGET_XFER_E_IO, memaddr); |
c26e4683 JB |
283 | } |
284 | ||
ec517d10 SM |
285 | /* Notify interpreters and observers that INF's memory was changed. */ |
286 | ||
287 | static void | |
288 | notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len, | |
289 | const bfd_byte *data) | |
290 | { | |
291 | interps_notify_memory_changed (inf, addr, len, data); | |
292 | gdb::observers::memory_changed.notify (inf, addr, len, data); | |
293 | } | |
294 | ||
972daa01 YQ |
295 | /* Same as write_memory, but notify 'memory_changed' observers. */ |
296 | ||
297 | void | |
298 | write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr, | |
299 | ssize_t len) | |
300 | { | |
301 | write_memory (memaddr, myaddr, len); | |
ec517d10 | 302 | notify_memory_changed (current_inferior (), memaddr, len, myaddr); |
972daa01 YQ |
303 | } |
304 | ||
aff410f1 MS |
305 | /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned |
306 | integer. */ | |
c26e4683 | 307 | void |
c5504eaf MS |
308 | write_memory_unsigned_integer (CORE_ADDR addr, int len, |
309 | enum bfd_endian byte_order, | |
e17a4113 | 310 | ULONGEST value) |
c26e4683 | 311 | { |
224c3ddb | 312 | gdb_byte *buf = (gdb_byte *) alloca (len); |
c5504eaf | 313 | |
e17a4113 | 314 | store_unsigned_integer (buf, len, byte_order, value); |
c26e4683 JB |
315 | write_memory (addr, buf, len); |
316 | } | |
317 | ||
aff410f1 MS |
318 | /* Store VALUE at ADDR in the inferior as a LEN-byte signed |
319 | integer. */ | |
c26e4683 | 320 | void |
c5504eaf MS |
321 | write_memory_signed_integer (CORE_ADDR addr, int len, |
322 | enum bfd_endian byte_order, | |
e17a4113 | 323 | LONGEST value) |
c26e4683 | 324 | { |
224c3ddb | 325 | gdb_byte *buf = (gdb_byte *) alloca (len); |
c5504eaf | 326 | |
e17a4113 | 327 | store_signed_integer (buf, len, byte_order, value); |
c26e4683 JB |
328 | write_memory (addr, buf, len); |
329 | } | |
c906108c SS |
330 | \f |
331 | /* The current default bfd target. Points to storage allocated for | |
332 | gnutarget_string. */ | |
4e7625fd | 333 | const char *gnutarget; |
c906108c SS |
334 | |
335 | /* Same thing, except it is "auto" not NULL for the default case. */ | |
e0700ba4 | 336 | static std::string gnutarget_string; |
920d2a44 AC |
337 | static void |
338 | show_gnutarget_string (struct ui_file *file, int from_tty, | |
aff410f1 MS |
339 | struct cmd_list_element *c, |
340 | const char *value) | |
920d2a44 | 341 | { |
6cb06a8c TT |
342 | gdb_printf (file, |
343 | _("The current BFD target is \"%s\".\n"), value); | |
920d2a44 | 344 | } |
c906108c | 345 | |
c906108c | 346 | static void |
eb4c3f4a | 347 | set_gnutarget_command (const char *ignore, int from_tty, |
aff410f1 | 348 | struct cmd_list_element *c) |
c906108c | 349 | { |
e0700ba4 SM |
350 | const char *gend = gnutarget_string.c_str () + gnutarget_string.size (); |
351 | gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend); | |
352 | gnutarget_string | |
353 | = gnutarget_string.substr (0, gend - gnutarget_string.data ()); | |
44478ab3 | 354 | |
e0700ba4 | 355 | if (gnutarget_string == "auto") |
c906108c SS |
356 | gnutarget = NULL; |
357 | else | |
e0700ba4 | 358 | gnutarget = gnutarget_string.c_str (); |
c906108c SS |
359 | } |
360 | ||
44478ab3 TT |
361 | /* A completion function for "set gnutarget". */ |
362 | ||
eb3ff9a5 | 363 | static void |
6f937416 | 364 | complete_set_gnutarget (struct cmd_list_element *cmd, |
eb3ff9a5 | 365 | completion_tracker &tracker, |
6f937416 | 366 | const char *text, const char *word) |
44478ab3 TT |
367 | { |
368 | static const char **bfd_targets; | |
369 | ||
370 | if (bfd_targets == NULL) | |
371 | { | |
372 | int last; | |
373 | ||
374 | bfd_targets = bfd_target_list (); | |
375 | for (last = 0; bfd_targets[last] != NULL; ++last) | |
376 | ; | |
377 | ||
224c3ddb | 378 | bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2); |
44478ab3 TT |
379 | bfd_targets[last] = "auto"; |
380 | bfd_targets[last + 1] = NULL; | |
381 | } | |
382 | ||
eb3ff9a5 | 383 | complete_on_enum (tracker, bfd_targets, text, word); |
44478ab3 TT |
384 | } |
385 | ||
c906108c SS |
386 | /* Set the gnutarget. */ |
387 | void | |
a121b7c1 | 388 | set_gnutarget (const char *newtarget) |
c906108c | 389 | { |
e0700ba4 | 390 | gnutarget_string = newtarget; |
c906108c SS |
391 | set_gnutarget_command (NULL, 0, NULL); |
392 | } | |
393 | ||
5fe70629 | 394 | INIT_GDB_FILE (core) |
c906108c | 395 | { |
af7f8f52 SM |
396 | cmd_list_element *core_file_cmd |
397 | = add_cmd ("core-file", class_files, core_file_command, _("\ | |
1a966eab | 398 | Use FILE as core dump for examining memory and registers.\n\ |
0cab2f1e | 399 | Usage: core-file FILE\n\ |
c906108c | 400 | No arg means have no core file. This command has been superseded by the\n\ |
1a966eab | 401 | `target core' and `detach' commands."), &cmdlist); |
dc22ab49 | 402 | set_cmd_completer (core_file_cmd, deprecated_filename_completer); |
c906108c | 403 | |
af7f8f52 SM |
404 | set_show_commands set_show_gnutarget |
405 | = add_setshow_string_noescape_cmd ("gnutarget", class_files, | |
44478ab3 | 406 | &gnutarget_string, _("\ |
26c41df3 AC |
407 | Set the current BFD target."), _("\ |
408 | Show the current BFD target."), _("\ | |
409 | Use `set gnutarget auto' to specify automatic detection."), | |
44478ab3 TT |
410 | set_gnutarget_command, |
411 | show_gnutarget_string, | |
412 | &setlist, &showlist); | |
af7f8f52 | 413 | set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget); |
44478ab3 | 414 | |
5e84b7ee | 415 | add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist); |
c906108c SS |
416 | |
417 | if (getenv ("GNUTARGET")) | |
418 | set_gnutarget (getenv ("GNUTARGET")); | |
419 | else | |
420 | set_gnutarget ("auto"); | |
421 | } |