]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/corefile.c
gdb: remove target_gdbarch
[thirdparty/binutils-gdb.git] / gdb / corefile.c
CommitLineData
c906108c 1/* Core dump and executable file functions above target vector, for GDB.
1bac305b 2
213516ef 3 Copyright (C) 1986-2023 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{
c906108c
SS
108 int res;
109 struct stat st;
c906108c 110
aff410f1 111 /* Don't do anything if there isn't an exec file. */
7e10abd1 112 if (current_program_space->exec_bfd () == NULL)
c906108c 113 return;
c5aa993b 114
aff410f1 115 /* If the timestamp of the exec file has changed, reopen it. */
7e10abd1 116 std::string filename = bfd_get_filename (current_program_space->exec_bfd ());
0638b7f9 117 res = stat (filename.c_str (), &st);
c906108c 118
5a36e715
TT
119 if (res == 0
120 && current_program_space->ebfd_mtime
121 && current_program_space->ebfd_mtime != st.st_mtime)
0638b7f9 122 exec_file_attach (filename.c_str (), 0);
939643d7
DJ
123 else
124 /* If we accessed the file since last opening it, close it now;
125 this stops GDB from holding the executable open after it
126 exits. */
127 bfd_cache_close_all ();
c906108c
SS
128}
129\f
130/* If we have both a core file and an exec file,
131 print a warning if they don't go together. */
132
133void
fba45db2 134validate_files (void)
c906108c 135{
7e10abd1 136 if (current_program_space->exec_bfd () && core_bfd)
c906108c 137 {
7e10abd1
TT
138 if (!core_file_matches_executable_p (core_bfd,
139 current_program_space->exec_bfd ()))
8a3fe4f8 140 warning (_("core file may not match specified executable file."));
7e10abd1
TT
141 else if (bfd_get_mtime (current_program_space->exec_bfd ())
142 > bfd_get_mtime (core_bfd))
8a3fe4f8 143 warning (_("exec file is newer than core file."));
c906108c
SS
144 }
145}
146
268a13a5 147/* See gdbsupport/common-inferior.h. */
c906108c 148
d9fa87f4 149const char *
fba45db2 150get_exec_file (int err)
c906108c 151{
c20cb686
TT
152 if (current_program_space->exec_filename != nullptr)
153 return current_program_space->exec_filename.get ();
c5aa993b
JM
154 if (!err)
155 return NULL;
c906108c 156
8a3fe4f8
AC
157 error (_("No executable file specified.\n\
158Use the \"file\" or \"exec-file\" command."));
c906108c 159}
c906108c 160\f
c5aa993b 161
1ccbe998 162std::string
9b409511 163memory_error_message (enum target_xfer_status err,
578d3588 164 struct gdbarch *gdbarch, CORE_ADDR memaddr)
6be7b56e
PA
165{
166 switch (err)
167 {
168 case TARGET_XFER_E_IO:
169 /* Actually, address between memaddr and memaddr + len was out of
170 bounds. */
1ccbe998
TT
171 return string_printf (_("Cannot access memory at address %s"),
172 paddress (gdbarch, memaddr));
bc113b4e 173 case TARGET_XFER_UNAVAILABLE:
1ccbe998
TT
174 return string_printf (_("Memory at address %s unavailable."),
175 paddress (gdbarch, memaddr));
6be7b56e 176 default:
f34652de 177 internal_error ("unhandled target_xfer_status: %s (%s)",
9b409511 178 target_xfer_status_to_string (err),
6be7b56e
PA
179 plongest (err));
180 }
181}
182
578d3588 183/* Report a memory error by throwing a suitable exception. */
c906108c
SS
184
185void
9b409511 186memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
c906108c 187{
8635b3bf 188 enum errors exception = GDB_NO_ERROR;
578d3588
PA
189
190 /* Build error string. */
99d9c3b9
SM
191 std::string str
192 = memory_error_message (err, current_inferior ()->arch (), memaddr);
578d3588
PA
193
194 /* Choose the right error to throw. */
195 switch (err)
196 {
197 case TARGET_XFER_E_IO:
8635b3bf 198 exception = MEMORY_ERROR;
578d3588 199 break;
bc113b4e 200 case TARGET_XFER_UNAVAILABLE:
8635b3bf 201 exception = NOT_AVAILABLE_ERROR;
578d3588
PA
202 break;
203 }
204
205 /* Throw it. */
1ccbe998 206 throw_error (exception, ("%s"), str.c_str ());
c906108c
SS
207}
208
edf689f0 209/* Helper function. */
4e5d721f 210
edf689f0
YQ
211static void
212read_memory_object (enum target_object object, CORE_ADDR memaddr,
213 gdb_byte *myaddr, ssize_t len)
c906108c 214{
9b409511 215 ULONGEST xfered = 0;
c5504eaf 216
6be7b56e
PA
217 while (xfered < len)
218 {
9b409511
YQ
219 enum target_xfer_status status;
220 ULONGEST xfered_len;
6be7b56e 221
328d42d8
SM
222 status = target_xfer_partial (current_inferior ()->top_target (), object,
223 NULL, myaddr + xfered, NULL,
9b409511
YQ
224 memaddr + xfered, len - xfered,
225 &xfered_len);
226
5c328c05
YQ
227 if (status != TARGET_XFER_OK)
228 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
229 memaddr + xfered);
9b409511 230
9b409511 231 xfered += xfered_len;
6be7b56e
PA
232 QUIT;
233 }
c906108c
SS
234}
235
edf689f0
YQ
236/* Same as target_read_memory, but report an error if can't read. */
237
238void
239read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
240{
241 read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
242}
243
4e5d721f
DE
244/* Same as target_read_stack, but report an error if can't read. */
245
246void
45aa4659 247read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
4e5d721f 248{
edf689f0 249 read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
4e5d721f
DE
250}
251
0865b04a
YQ
252/* Same as target_read_code, but report an error if can't read. */
253
254void
255read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
256{
edf689f0 257 read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
0865b04a
YQ
258}
259
ee8ff470
KB
260/* Read memory at MEMADDR of length LEN and put the contents in
261 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
262 if successful. */
263
16a0f3e7 264int
c5504eaf
MS
265safe_read_memory_integer (CORE_ADDR memaddr, int len,
266 enum bfd_endian byte_order,
e17a4113 267 LONGEST *return_value)
16a0f3e7 268{
5e43d467 269 gdb_byte buf[sizeof (LONGEST)];
16a0f3e7 270
5e43d467
UW
271 if (target_read_memory (memaddr, buf, len))
272 return 0;
16a0f3e7 273
5e43d467
UW
274 *return_value = extract_signed_integer (buf, len, byte_order);
275 return 1;
16a0f3e7
EZ
276}
277
cc2c4da8
MK
278/* Read memory at MEMADDR of length LEN and put the contents in
279 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
280 if successful. */
281
282int
283safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
284 enum bfd_endian byte_order,
285 ULONGEST *return_value)
286{
287 gdb_byte buf[sizeof (ULONGEST)];
288
289 if (target_read_memory (memaddr, buf, len))
290 return 0;
291
292 *return_value = extract_unsigned_integer (buf, len, byte_order);
293 return 1;
294}
295
c906108c 296LONGEST
aff410f1
MS
297read_memory_integer (CORE_ADDR memaddr, int len,
298 enum bfd_endian byte_order)
c906108c 299{
dfb65433 300 gdb_byte buf[sizeof (LONGEST)];
c906108c
SS
301
302 read_memory (memaddr, buf, len);
e17a4113 303 return extract_signed_integer (buf, len, byte_order);
c906108c
SS
304}
305
306ULONGEST
aff410f1
MS
307read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
308 enum bfd_endian byte_order)
c906108c 309{
dfb65433 310 gdb_byte buf[sizeof (ULONGEST)];
c906108c
SS
311
312 read_memory (memaddr, buf, len);
e17a4113 313 return extract_unsigned_integer (buf, len, byte_order);
c906108c
SS
314}
315
0865b04a
YQ
316LONGEST
317read_code_integer (CORE_ADDR memaddr, int len,
318 enum bfd_endian byte_order)
319{
320 gdb_byte buf[sizeof (LONGEST)];
321
322 read_code (memaddr, buf, len);
323 return extract_signed_integer (buf, len, byte_order);
324}
325
326ULONGEST
327read_code_unsigned_integer (CORE_ADDR memaddr, int len,
328 enum bfd_endian byte_order)
329{
330 gdb_byte buf[sizeof (ULONGEST)];
331
332 read_code (memaddr, buf, len);
333 return extract_unsigned_integer (buf, len, byte_order);
334}
335
0d540cdf
KD
336CORE_ADDR
337read_memory_typed_address (CORE_ADDR addr, struct type *type)
338{
df86565b 339 gdb_byte *buf = (gdb_byte *) alloca (type->length ());
c5504eaf 340
df86565b 341 read_memory (addr, buf, type->length ());
0d540cdf
KD
342 return extract_typed_address (buf, type);
343}
344
cb6f16cf
SM
345/* See gdbcore.h. */
346
c26e4683 347void
aff410f1 348write_memory (CORE_ADDR memaddr,
45aa4659 349 const bfd_byte *myaddr, ssize_t len)
c26e4683
JB
350{
351 int status;
c5504eaf 352
00630ca8 353 status = target_write_memory (memaddr, myaddr, len);
c26e4683 354 if (status != 0)
d09f2c3f 355 memory_error (TARGET_XFER_E_IO, memaddr);
c26e4683
JB
356}
357
ec517d10
SM
358/* Notify interpreters and observers that INF's memory was changed. */
359
360static void
361notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
362 const bfd_byte *data)
363{
364 interps_notify_memory_changed (inf, addr, len, data);
365 gdb::observers::memory_changed.notify (inf, addr, len, data);
366}
367
972daa01
YQ
368/* Same as write_memory, but notify 'memory_changed' observers. */
369
370void
371write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
372 ssize_t len)
373{
374 write_memory (memaddr, myaddr, len);
ec517d10 375 notify_memory_changed (current_inferior (), memaddr, len, myaddr);
972daa01
YQ
376}
377
aff410f1
MS
378/* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
379 integer. */
c26e4683 380void
c5504eaf
MS
381write_memory_unsigned_integer (CORE_ADDR addr, int len,
382 enum bfd_endian byte_order,
e17a4113 383 ULONGEST value)
c26e4683 384{
224c3ddb 385 gdb_byte *buf = (gdb_byte *) alloca (len);
c5504eaf 386
e17a4113 387 store_unsigned_integer (buf, len, byte_order, value);
c26e4683
JB
388 write_memory (addr, buf, len);
389}
390
aff410f1
MS
391/* Store VALUE at ADDR in the inferior as a LEN-byte signed
392 integer. */
c26e4683 393void
c5504eaf
MS
394write_memory_signed_integer (CORE_ADDR addr, int len,
395 enum bfd_endian byte_order,
e17a4113 396 LONGEST value)
c26e4683 397{
224c3ddb 398 gdb_byte *buf = (gdb_byte *) alloca (len);
c5504eaf 399
e17a4113 400 store_signed_integer (buf, len, byte_order, value);
c26e4683
JB
401 write_memory (addr, buf, len);
402}
c906108c
SS
403\f
404/* The current default bfd target. Points to storage allocated for
405 gnutarget_string. */
4e7625fd 406const char *gnutarget;
c906108c
SS
407
408/* Same thing, except it is "auto" not NULL for the default case. */
e0700ba4 409static std::string gnutarget_string;
920d2a44
AC
410static void
411show_gnutarget_string (struct ui_file *file, int from_tty,
aff410f1
MS
412 struct cmd_list_element *c,
413 const char *value)
920d2a44 414{
6cb06a8c
TT
415 gdb_printf (file,
416 _("The current BFD target is \"%s\".\n"), value);
920d2a44 417}
c906108c 418
c906108c 419static void
eb4c3f4a 420set_gnutarget_command (const char *ignore, int from_tty,
aff410f1 421 struct cmd_list_element *c)
c906108c 422{
e0700ba4
SM
423 const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
424 gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
425 gnutarget_string
426 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
44478ab3 427
e0700ba4 428 if (gnutarget_string == "auto")
c906108c
SS
429 gnutarget = NULL;
430 else
e0700ba4 431 gnutarget = gnutarget_string.c_str ();
c906108c
SS
432}
433
44478ab3
TT
434/* A completion function for "set gnutarget". */
435
eb3ff9a5 436static void
6f937416 437complete_set_gnutarget (struct cmd_list_element *cmd,
eb3ff9a5 438 completion_tracker &tracker,
6f937416 439 const char *text, const char *word)
44478ab3
TT
440{
441 static const char **bfd_targets;
442
443 if (bfd_targets == NULL)
444 {
445 int last;
446
447 bfd_targets = bfd_target_list ();
448 for (last = 0; bfd_targets[last] != NULL; ++last)
449 ;
450
224c3ddb 451 bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
44478ab3
TT
452 bfd_targets[last] = "auto";
453 bfd_targets[last + 1] = NULL;
454 }
455
eb3ff9a5 456 complete_on_enum (tracker, bfd_targets, text, word);
44478ab3
TT
457}
458
c906108c
SS
459/* Set the gnutarget. */
460void
a121b7c1 461set_gnutarget (const char *newtarget)
c906108c 462{
e0700ba4 463 gnutarget_string = newtarget;
c906108c
SS
464 set_gnutarget_command (NULL, 0, NULL);
465}
466
6c265988 467void _initialize_core ();
c906108c 468void
6c265988 469_initialize_core ()
c906108c 470{
af7f8f52
SM
471 cmd_list_element *core_file_cmd
472 = add_cmd ("core-file", class_files, core_file_command, _("\
1a966eab 473Use FILE as core dump for examining memory and registers.\n\
0cab2f1e 474Usage: core-file FILE\n\
c906108c 475No arg means have no core file. This command has been superseded by the\n\
1a966eab 476`target core' and `detach' commands."), &cmdlist);
af7f8f52 477 set_cmd_completer (core_file_cmd, filename_completer);
c906108c 478
26c41df3 479
af7f8f52
SM
480 set_show_commands set_show_gnutarget
481 = add_setshow_string_noescape_cmd ("gnutarget", class_files,
44478ab3 482 &gnutarget_string, _("\
26c41df3
AC
483Set the current BFD target."), _("\
484Show the current BFD target."), _("\
485Use `set gnutarget auto' to specify automatic detection."),
44478ab3
TT
486 set_gnutarget_command,
487 show_gnutarget_string,
488 &setlist, &showlist);
af7f8f52 489 set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
44478ab3 490
5e84b7ee 491 add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
c906108c
SS
492
493 if (getenv ("GNUTARGET"))
494 set_gnutarget (getenv ("GNUTARGET"));
495 else
496 set_gnutarget ("auto");
497}