]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/corefile.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / corefile.c
CommitLineData
c906108c 1/* Core dump and executable file functions above target vector, for GDB.
1bac305b 2
1d506c26 3 Copyright (C) 1986-2024 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
SS
19
20#include "defs.h"
d55e5aa6 21#include <signal.h>
4de283e4
TT
22#include <fcntl.h>
23#include "inferior.h"
24#include "symtab.h"
c906108c
SS
25#include "command.h"
26#include "gdbcmd.h"
4de283e4
TT
27#include "bfd.h"
28#include "target.h"
c906108c 29#include "gdbcore.h"
4de283e4
TT
30#include "dis-asm.h"
31#include <sys/stat.h>
32#include "completer.h"
76727919 33#include "observable.h"
4de283e4 34#include "cli/cli-utils.h"
0d12e84c 35#include "gdbarch.h"
ec517d10 36#include "interps.h"
c906108c 37
9a4105ab
AC
38/* You can have any number of hooks for `exec_file_command' command to
39 call. If there's only one hook, it is set in exec_file_display
40 hook. If there are two or more hooks, they are set in
41 exec_file_extra_hooks[], and deprecated_exec_file_display_hook is
42 set to a function that calls all of them. This extra complexity is
43 needed to preserve compatibility with old code that assumed that
44 only one hook could be set, and which called
45 deprecated_exec_file_display_hook directly. */
c906108c 46
5f08566b 47typedef void (*hook_type) (const char *);
c906108c 48
aff410f1
MS
49hook_type deprecated_exec_file_display_hook; /* The original hook. */
50static hook_type *exec_file_extra_hooks; /* Array of additional
51 hooks. */
52static int exec_file_hook_count = 0; /* Size of array. */
c906108c 53
c906108c 54\f
c5aa993b 55
de6854b5
MS
56/* If there are two or more functions that wish to hook into
57 exec_file_command, this function will call all of the hook
58 functions. */
c906108c
SS
59
60static void
5f08566b 61call_extra_exec_file_hooks (const char *filename)
c906108c
SS
62{
63 int i;
64
65 for (i = 0; i < exec_file_hook_count; i++)
c5aa993b 66 (*exec_file_extra_hooks[i]) (filename);
c906108c
SS
67}
68
69/* Call this to specify the hook for exec_file_command to call back.
70 This is called from the x-window display code. */
71
72void
5f08566b 73specify_exec_file_hook (void (*hook) (const char *))
c906108c
SS
74{
75 hook_type *new_array;
76
9a4105ab 77 if (deprecated_exec_file_display_hook != NULL)
c906108c
SS
78 {
79 /* There's already a hook installed. Arrange to have both it
aff410f1 80 and the subsequent hooks called. */
c906108c
SS
81 if (exec_file_hook_count == 0)
82 {
aff410f1
MS
83 /* If this is the first extra hook, initialize the hook
84 array. */
8d749320 85 exec_file_extra_hooks = XNEW (hook_type);
9a4105ab
AC
86 exec_file_extra_hooks[0] = deprecated_exec_file_display_hook;
87 deprecated_exec_file_display_hook = call_extra_exec_file_hooks;
c906108c
SS
88 exec_file_hook_count = 1;
89 }
90
91 /* Grow the hook array by one and add the new hook to the end.
dda83cd7
SM
92 Yes, it's inefficient to grow it by one each time but since
93 this is hardly ever called it's not a big deal. */
c906108c 94 exec_file_hook_count++;
aff410f1
MS
95 new_array = (hook_type *)
96 xrealloc (exec_file_extra_hooks,
97 exec_file_hook_count * sizeof (hook_type));
c906108c
SS
98 exec_file_extra_hooks = new_array;
99 exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
100 }
101 else
9a4105ab 102 deprecated_exec_file_display_hook = hook;
c906108c
SS
103}
104
c906108c 105void
fba45db2 106reopen_exec_file (void)
c906108c 107{
70fd94b2 108 bfd *exec_bfd = current_program_space->exec_bfd ();
c906108c 109
aff410f1 110 /* Don't do anything if there isn't an exec file. */
70fd94b2 111 if (exec_bfd == nullptr)
c906108c 112 return;
c5aa993b 113
70fd94b2
AB
114 /* The main executable can't be an in-memory BFD object. If it was then
115 the use of bfd_stat below would not work as expected. */
116 gdb_assert ((exec_bfd->flags & BFD_IN_MEMORY) == 0);
117
aff410f1 118 /* If the timestamp of the exec file has changed, reopen it. */
70fd94b2
AB
119 struct stat st;
120 int res = bfd_stat (exec_bfd, &st);
c906108c 121
5a36e715 122 if (res == 0
70fd94b2 123 && current_program_space->ebfd_mtime != 0
5a36e715 124 && current_program_space->ebfd_mtime != st.st_mtime)
70fd94b2 125 exec_file_attach (bfd_get_filename (exec_bfd), 0);
c906108c
SS
126}
127\f
128/* If we have both a core file and an exec file,
129 print a warning if they don't go together. */
130
131void
fba45db2 132validate_files (void)
c906108c 133{
7e10abd1 134 if (current_program_space->exec_bfd () && core_bfd)
c906108c 135 {
7e10abd1
TT
136 if (!core_file_matches_executable_p (core_bfd,
137 current_program_space->exec_bfd ()))
8a3fe4f8 138 warning (_("core file may not match specified executable file."));
7e10abd1
TT
139 else if (bfd_get_mtime (current_program_space->exec_bfd ())
140 > bfd_get_mtime (core_bfd))
8a3fe4f8 141 warning (_("exec file is newer than core file."));
c906108c
SS
142 }
143}
144
268a13a5 145/* See gdbsupport/common-inferior.h. */
c906108c 146
d9fa87f4 147const char *
fba45db2 148get_exec_file (int err)
c906108c 149{
c20cb686
TT
150 if (current_program_space->exec_filename != nullptr)
151 return current_program_space->exec_filename.get ();
c5aa993b
JM
152 if (!err)
153 return NULL;
c906108c 154
8a3fe4f8
AC
155 error (_("No executable file specified.\n\
156Use the \"file\" or \"exec-file\" command."));
c906108c 157}
c906108c 158\f
c5aa993b 159
1ccbe998 160std::string
9b409511 161memory_error_message (enum target_xfer_status err,
578d3588 162 struct gdbarch *gdbarch, CORE_ADDR memaddr)
6be7b56e
PA
163{
164 switch (err)
165 {
166 case TARGET_XFER_E_IO:
167 /* Actually, address between memaddr and memaddr + len was out of
168 bounds. */
1ccbe998
TT
169 return string_printf (_("Cannot access memory at address %s"),
170 paddress (gdbarch, memaddr));
bc113b4e 171 case TARGET_XFER_UNAVAILABLE:
1ccbe998
TT
172 return string_printf (_("Memory at address %s unavailable."),
173 paddress (gdbarch, memaddr));
6be7b56e 174 default:
f34652de 175 internal_error ("unhandled target_xfer_status: %s (%s)",
9b409511 176 target_xfer_status_to_string (err),
6be7b56e
PA
177 plongest (err));
178 }
179}
180
578d3588 181/* Report a memory error by throwing a suitable exception. */
c906108c
SS
182
183void
9b409511 184memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
c906108c 185{
8635b3bf 186 enum errors exception = GDB_NO_ERROR;
578d3588
PA
187
188 /* Build error string. */
99d9c3b9
SM
189 std::string str
190 = memory_error_message (err, current_inferior ()->arch (), memaddr);
578d3588
PA
191
192 /* Choose the right error to throw. */
193 switch (err)
194 {
195 case TARGET_XFER_E_IO:
8635b3bf 196 exception = MEMORY_ERROR;
578d3588 197 break;
bc113b4e 198 case TARGET_XFER_UNAVAILABLE:
8635b3bf 199 exception = NOT_AVAILABLE_ERROR;
578d3588
PA
200 break;
201 }
202
203 /* Throw it. */
1ccbe998 204 throw_error (exception, ("%s"), str.c_str ());
c906108c
SS
205}
206
edf689f0 207/* Helper function. */
4e5d721f 208
edf689f0
YQ
209static void
210read_memory_object (enum target_object object, CORE_ADDR memaddr,
211 gdb_byte *myaddr, ssize_t len)
c906108c 212{
9b409511 213 ULONGEST xfered = 0;
c5504eaf 214
6be7b56e
PA
215 while (xfered < len)
216 {
9b409511
YQ
217 enum target_xfer_status status;
218 ULONGEST xfered_len;
6be7b56e 219
328d42d8
SM
220 status = target_xfer_partial (current_inferior ()->top_target (), object,
221 NULL, myaddr + xfered, NULL,
9b409511
YQ
222 memaddr + xfered, len - xfered,
223 &xfered_len);
224
5c328c05
YQ
225 if (status != TARGET_XFER_OK)
226 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
227 memaddr + xfered);
9b409511 228
9b409511 229 xfered += xfered_len;
6be7b56e
PA
230 QUIT;
231 }
c906108c
SS
232}
233
edf689f0
YQ
234/* Same as target_read_memory, but report an error if can't read. */
235
236void
237read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
238{
239 read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
240}
241
4e5d721f
DE
242/* Same as target_read_stack, but report an error if can't read. */
243
244void
45aa4659 245read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
4e5d721f 246{
edf689f0 247 read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
4e5d721f
DE
248}
249
0865b04a
YQ
250/* Same as target_read_code, but report an error if can't read. */
251
252void
253read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
254{
edf689f0 255 read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
0865b04a
YQ
256}
257
ee8ff470
KB
258/* Read memory at MEMADDR of length LEN and put the contents in
259 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
260 if successful. */
261
16a0f3e7 262int
c5504eaf
MS
263safe_read_memory_integer (CORE_ADDR memaddr, int len,
264 enum bfd_endian byte_order,
e17a4113 265 LONGEST *return_value)
16a0f3e7 266{
5e43d467 267 gdb_byte buf[sizeof (LONGEST)];
16a0f3e7 268
5e43d467
UW
269 if (target_read_memory (memaddr, buf, len))
270 return 0;
16a0f3e7 271
5e43d467
UW
272 *return_value = extract_signed_integer (buf, len, byte_order);
273 return 1;
16a0f3e7
EZ
274}
275
cc2c4da8
MK
276/* Read memory at MEMADDR of length LEN and put the contents in
277 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
278 if successful. */
279
280int
281safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
282 enum bfd_endian byte_order,
283 ULONGEST *return_value)
284{
285 gdb_byte buf[sizeof (ULONGEST)];
286
287 if (target_read_memory (memaddr, buf, len))
288 return 0;
289
290 *return_value = extract_unsigned_integer (buf, len, byte_order);
291 return 1;
292}
293
c906108c 294LONGEST
aff410f1
MS
295read_memory_integer (CORE_ADDR memaddr, int len,
296 enum bfd_endian byte_order)
c906108c 297{
dfb65433 298 gdb_byte buf[sizeof (LONGEST)];
c906108c
SS
299
300 read_memory (memaddr, buf, len);
e17a4113 301 return extract_signed_integer (buf, len, byte_order);
c906108c
SS
302}
303
304ULONGEST
aff410f1
MS
305read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
306 enum bfd_endian byte_order)
c906108c 307{
dfb65433 308 gdb_byte buf[sizeof (ULONGEST)];
c906108c
SS
309
310 read_memory (memaddr, buf, len);
e17a4113 311 return extract_unsigned_integer (buf, len, byte_order);
c906108c
SS
312}
313
0865b04a
YQ
314LONGEST
315read_code_integer (CORE_ADDR memaddr, int len,
316 enum bfd_endian byte_order)
317{
318 gdb_byte buf[sizeof (LONGEST)];
319
320 read_code (memaddr, buf, len);
321 return extract_signed_integer (buf, len, byte_order);
322}
323
324ULONGEST
325read_code_unsigned_integer (CORE_ADDR memaddr, int len,
326 enum bfd_endian byte_order)
327{
328 gdb_byte buf[sizeof (ULONGEST)];
329
330 read_code (memaddr, buf, len);
331 return extract_unsigned_integer (buf, len, byte_order);
332}
333
0d540cdf
KD
334CORE_ADDR
335read_memory_typed_address (CORE_ADDR addr, struct type *type)
336{
df86565b 337 gdb_byte *buf = (gdb_byte *) alloca (type->length ());
c5504eaf 338
df86565b 339 read_memory (addr, buf, type->length ());
0d540cdf
KD
340 return extract_typed_address (buf, type);
341}
342
cb6f16cf
SM
343/* See gdbcore.h. */
344
c26e4683 345void
aff410f1 346write_memory (CORE_ADDR memaddr,
45aa4659 347 const bfd_byte *myaddr, ssize_t len)
c26e4683
JB
348{
349 int status;
c5504eaf 350
00630ca8 351 status = target_write_memory (memaddr, myaddr, len);
c26e4683 352 if (status != 0)
d09f2c3f 353 memory_error (TARGET_XFER_E_IO, memaddr);
c26e4683
JB
354}
355
ec517d10
SM
356/* Notify interpreters and observers that INF's memory was changed. */
357
358static void
359notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
360 const bfd_byte *data)
361{
362 interps_notify_memory_changed (inf, addr, len, data);
363 gdb::observers::memory_changed.notify (inf, addr, len, data);
364}
365
972daa01
YQ
366/* Same as write_memory, but notify 'memory_changed' observers. */
367
368void
369write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
370 ssize_t len)
371{
372 write_memory (memaddr, myaddr, len);
ec517d10 373 notify_memory_changed (current_inferior (), memaddr, len, myaddr);
972daa01
YQ
374}
375
aff410f1
MS
376/* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
377 integer. */
c26e4683 378void
c5504eaf
MS
379write_memory_unsigned_integer (CORE_ADDR addr, int len,
380 enum bfd_endian byte_order,
e17a4113 381 ULONGEST value)
c26e4683 382{
224c3ddb 383 gdb_byte *buf = (gdb_byte *) alloca (len);
c5504eaf 384
e17a4113 385 store_unsigned_integer (buf, len, byte_order, value);
c26e4683
JB
386 write_memory (addr, buf, len);
387}
388
aff410f1
MS
389/* Store VALUE at ADDR in the inferior as a LEN-byte signed
390 integer. */
c26e4683 391void
c5504eaf
MS
392write_memory_signed_integer (CORE_ADDR addr, int len,
393 enum bfd_endian byte_order,
e17a4113 394 LONGEST value)
c26e4683 395{
224c3ddb 396 gdb_byte *buf = (gdb_byte *) alloca (len);
c5504eaf 397
e17a4113 398 store_signed_integer (buf, len, byte_order, value);
c26e4683
JB
399 write_memory (addr, buf, len);
400}
c906108c
SS
401\f
402/* The current default bfd target. Points to storage allocated for
403 gnutarget_string. */
4e7625fd 404const char *gnutarget;
c906108c
SS
405
406/* Same thing, except it is "auto" not NULL for the default case. */
e0700ba4 407static std::string gnutarget_string;
920d2a44
AC
408static void
409show_gnutarget_string (struct ui_file *file, int from_tty,
aff410f1
MS
410 struct cmd_list_element *c,
411 const char *value)
920d2a44 412{
6cb06a8c
TT
413 gdb_printf (file,
414 _("The current BFD target is \"%s\".\n"), value);
920d2a44 415}
c906108c 416
c906108c 417static void
eb4c3f4a 418set_gnutarget_command (const char *ignore, int from_tty,
aff410f1 419 struct cmd_list_element *c)
c906108c 420{
e0700ba4
SM
421 const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
422 gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
423 gnutarget_string
424 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
44478ab3 425
e0700ba4 426 if (gnutarget_string == "auto")
c906108c
SS
427 gnutarget = NULL;
428 else
e0700ba4 429 gnutarget = gnutarget_string.c_str ();
c906108c
SS
430}
431
44478ab3
TT
432/* A completion function for "set gnutarget". */
433
eb3ff9a5 434static void
6f937416 435complete_set_gnutarget (struct cmd_list_element *cmd,
eb3ff9a5 436 completion_tracker &tracker,
6f937416 437 const char *text, const char *word)
44478ab3
TT
438{
439 static const char **bfd_targets;
440
441 if (bfd_targets == NULL)
442 {
443 int last;
444
445 bfd_targets = bfd_target_list ();
446 for (last = 0; bfd_targets[last] != NULL; ++last)
447 ;
448
224c3ddb 449 bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
44478ab3
TT
450 bfd_targets[last] = "auto";
451 bfd_targets[last + 1] = NULL;
452 }
453
eb3ff9a5 454 complete_on_enum (tracker, bfd_targets, text, word);
44478ab3
TT
455}
456
c906108c
SS
457/* Set the gnutarget. */
458void
a121b7c1 459set_gnutarget (const char *newtarget)
c906108c 460{
e0700ba4 461 gnutarget_string = newtarget;
c906108c
SS
462 set_gnutarget_command (NULL, 0, NULL);
463}
464
6c265988 465void _initialize_core ();
c906108c 466void
6c265988 467_initialize_core ()
c906108c 468{
af7f8f52
SM
469 cmd_list_element *core_file_cmd
470 = add_cmd ("core-file", class_files, core_file_command, _("\
1a966eab 471Use FILE as core dump for examining memory and registers.\n\
0cab2f1e 472Usage: core-file FILE\n\
c906108c 473No arg means have no core file. This command has been superseded by the\n\
1a966eab 474`target core' and `detach' commands."), &cmdlist);
af7f8f52 475 set_cmd_completer (core_file_cmd, filename_completer);
c906108c 476
26c41df3 477
af7f8f52
SM
478 set_show_commands set_show_gnutarget
479 = add_setshow_string_noescape_cmd ("gnutarget", class_files,
44478ab3 480 &gnutarget_string, _("\
26c41df3
AC
481Set the current BFD target."), _("\
482Show the current BFD target."), _("\
483Use `set gnutarget auto' to specify automatic detection."),
44478ab3
TT
484 set_gnutarget_command,
485 show_gnutarget_string,
486 &setlist, &showlist);
af7f8f52 487 set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
44478ab3 488
5e84b7ee 489 add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
c906108c
SS
490
491 if (getenv ("GNUTARGET"))
492 set_gnutarget (getenv ("GNUTARGET"));
493 else
494 set_gnutarget ("auto");
495}