GDB (xrefs)
Loading...
Searching...
No Matches
corefile.c
Go to the documentation of this file.
1/* Core dump and executable file functions above target vector, for GDB.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include <signal.h>
22#include <fcntl.h>
23#include "inferior.h"
24#include "symtab.h"
25#include "command.h"
26#include "gdbcmd.h"
27#include "bfd.h"
28#include "target.h"
29#include "gdbcore.h"
30#include "dis-asm.h"
31#include <sys/stat.h>
32#include "completer.h"
33#include "observable.h"
34#include "cli/cli-utils.h"
35#include "gdbarch.h"
36#include "interps.h"
37
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. */
46
47typedef void (*hook_type) (const char *);
48
50static hook_type *exec_file_extra_hooks; /* Array of additional
51 hooks. */
52static int exec_file_hook_count = 0; /* Size of array. */
53
54
55
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. */
59
60static void
61call_extra_exec_file_hooks (const char *filename)
62{
63 int i;
64
65 for (i = 0; i < exec_file_hook_count; i++)
66 (*exec_file_extra_hooks[i]) (filename);
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
73specify_exec_file_hook (void (*hook) (const char *))
74{
75 hook_type *new_array;
76
78 {
79 /* There's already a hook installed. Arrange to have both it
80 and the subsequent hooks called. */
81 if (exec_file_hook_count == 0)
82 {
83 /* If this is the first extra hook, initialize the hook
84 array. */
89 }
90
91 /* Grow the hook array by one and add the new hook to the end.
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. */
95 new_array = (hook_type *)
98 exec_file_extra_hooks = new_array;
100 }
101 else
103}
104
105void
107{
108 int res;
109 struct stat st;
110
111 /* Don't do anything if there isn't an exec file. */
112 if (current_program_space->exec_bfd () == NULL)
113 return;
114
115 /* If the timestamp of the exec file has changed, reopen it. */
116 std::string filename = bfd_get_filename (current_program_space->exec_bfd ());
117 res = stat (filename.c_str (), &st);
118
119 if (res == 0
121 && current_program_space->ebfd_mtime != st.st_mtime)
122 exec_file_attach (filename.c_str (), 0);
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 ();
128}
129
130/* If we have both a core file and an exec file,
131 print a warning if they don't go together. */
132
133void
135{
137 {
138 if (!core_file_matches_executable_p (core_bfd,
140 warning (_("core file may not match specified executable file."));
141 else if (bfd_get_mtime (current_program_space->exec_bfd ())
142 > bfd_get_mtime (core_bfd))
143 warning (_("exec file is newer than core file."));
144 }
145}
146
147/* See gdbsupport/common-inferior.h. */
148
149const char *
151{
152 if (current_program_space->exec_filename != nullptr)
154 if (!err)
155 return NULL;
156
157 error (_("No executable file specified.\n\
158Use the \"file\" or \"exec-file\" command."));
159}
160
161
162std::string
164 struct gdbarch *gdbarch, CORE_ADDR memaddr)
165{
166 switch (err)
167 {
168 case TARGET_XFER_E_IO:
169 /* Actually, address between memaddr and memaddr + len was out of
170 bounds. */
171 return string_printf (_("Cannot access memory at address %s"),
172 paddress (gdbarch, memaddr));
174 return string_printf (_("Memory at address %s unavailable."),
175 paddress (gdbarch, memaddr));
176 default:
177 internal_error ("unhandled target_xfer_status: %s (%s)",
179 plongest (err));
180 }
181}
182
183/* Report a memory error by throwing a suitable exception. */
184
185void
186memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
187{
188 enum errors exception = GDB_NO_ERROR;
189
190 /* Build error string. */
191 std::string str = memory_error_message (err, target_gdbarch (), memaddr);
192
193 /* Choose the right error to throw. */
194 switch (err)
195 {
196 case TARGET_XFER_E_IO:
197 exception = MEMORY_ERROR;
198 break;
200 exception = NOT_AVAILABLE_ERROR;
201 break;
202 }
203
204 /* Throw it. */
205 throw_error (exception, ("%s"), str.c_str ());
206}
207
208/* Helper function. */
209
210static void
211read_memory_object (enum target_object object, CORE_ADDR memaddr,
212 gdb_byte *myaddr, ssize_t len)
213{
214 ULONGEST xfered = 0;
215
216 while (xfered < len)
217 {
219 ULONGEST xfered_len;
220
221 status = target_xfer_partial (current_inferior ()->top_target (), object,
222 NULL, myaddr + xfered, NULL,
223 memaddr + xfered, len - xfered,
224 &xfered_len);
225
226 if (status != TARGET_XFER_OK)
228 memaddr + xfered);
229
230 xfered += xfered_len;
231 QUIT;
232 }
233}
234
235/* Same as target_read_memory, but report an error if can't read. */
236
237void
238read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
239{
240 read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
241}
242
243/* Same as target_read_stack, but report an error if can't read. */
244
245void
246read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
247{
248 read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
249}
250
251/* Same as target_read_code, but report an error if can't read. */
252
253void
254read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
255{
256 read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
257}
258
259/* Read memory at MEMADDR of length LEN and put the contents in
260 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
261 if successful. */
262
263int
264safe_read_memory_integer (CORE_ADDR memaddr, int len,
265 enum bfd_endian byte_order,
266 LONGEST *return_value)
267{
268 gdb_byte buf[sizeof (LONGEST)];
269
270 if (target_read_memory (memaddr, buf, len))
271 return 0;
272
273 *return_value = extract_signed_integer (buf, len, byte_order);
274 return 1;
275}
276
277/* Read memory at MEMADDR of length LEN and put the contents in
278 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
279 if successful. */
280
281int
282safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
283 enum bfd_endian byte_order,
284 ULONGEST *return_value)
285{
286 gdb_byte buf[sizeof (ULONGEST)];
287
288 if (target_read_memory (memaddr, buf, len))
289 return 0;
290
291 *return_value = extract_unsigned_integer (buf, len, byte_order);
292 return 1;
293}
294
295LONGEST
296read_memory_integer (CORE_ADDR memaddr, int len,
297 enum bfd_endian byte_order)
298{
299 gdb_byte buf[sizeof (LONGEST)];
300
301 read_memory (memaddr, buf, len);
302 return extract_signed_integer (buf, len, byte_order);
303}
304
305ULONGEST
306read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
307 enum bfd_endian byte_order)
308{
309 gdb_byte buf[sizeof (ULONGEST)];
310
311 read_memory (memaddr, buf, len);
312 return extract_unsigned_integer (buf, len, byte_order);
313}
314
315LONGEST
316read_code_integer (CORE_ADDR memaddr, int len,
317 enum bfd_endian byte_order)
318{
319 gdb_byte buf[sizeof (LONGEST)];
320
321 read_code (memaddr, buf, len);
322 return extract_signed_integer (buf, len, byte_order);
323}
324
325ULONGEST
326read_code_unsigned_integer (CORE_ADDR memaddr, int len,
327 enum bfd_endian byte_order)
328{
329 gdb_byte buf[sizeof (ULONGEST)];
330
331 read_code (memaddr, buf, len);
332 return extract_unsigned_integer (buf, len, byte_order);
333}
334
335CORE_ADDR
336read_memory_typed_address (CORE_ADDR addr, struct type *type)
337{
338 gdb_byte *buf = (gdb_byte *) alloca (type->length ());
339
340 read_memory (addr, buf, type->length ());
341 return extract_typed_address (buf, type);
342}
343
344/* See gdbcore.h. */
345
346void
347write_memory (CORE_ADDR memaddr,
348 const bfd_byte *myaddr, ssize_t len)
349{
350 int status;
351
352 status = target_write_memory (memaddr, myaddr, len);
353 if (status != 0)
355}
356
357/* Notify interpreters and observers that INF's memory was changed. */
358
359static void
360notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
361 const bfd_byte *data)
362{
363 interps_notify_memory_changed (inf, addr, len, data);
364 gdb::observers::memory_changed.notify (inf, addr, len, data);
365}
366
367/* Same as write_memory, but notify 'memory_changed' observers. */
368
369void
370write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
371 ssize_t len)
372{
373 write_memory (memaddr, myaddr, len);
374 notify_memory_changed (current_inferior (), memaddr, len, myaddr);
375}
376
377/* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
378 integer. */
379void
380write_memory_unsigned_integer (CORE_ADDR addr, int len,
381 enum bfd_endian byte_order,
382 ULONGEST value)
383{
384 gdb_byte *buf = (gdb_byte *) alloca (len);
385
386 store_unsigned_integer (buf, len, byte_order, value);
387 write_memory (addr, buf, len);
388}
389
390/* Store VALUE at ADDR in the inferior as a LEN-byte signed
391 integer. */
392void
393write_memory_signed_integer (CORE_ADDR addr, int len,
394 enum bfd_endian byte_order,
395 LONGEST value)
396{
397 gdb_byte *buf = (gdb_byte *) alloca (len);
398
399 store_signed_integer (buf, len, byte_order, value);
400 write_memory (addr, buf, len);
401}
402
403/* The current default bfd target. Points to storage allocated for
404 gnutarget_string. */
405const char *gnutarget;
406
407/* Same thing, except it is "auto" not NULL for the default case. */
408static std::string gnutarget_string;
409static void
410show_gnutarget_string (struct ui_file *file, int from_tty,
411 struct cmd_list_element *c,
412 const char *value)
413{
414 gdb_printf (file,
415 _("The current BFD target is \"%s\".\n"), value);
416}
417
418static void
419set_gnutarget_command (const char *ignore, int from_tty,
420 struct cmd_list_element *c)
421{
422 const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
423 gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
425 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
426
427 if (gnutarget_string == "auto")
428 gnutarget = NULL;
429 else
430 gnutarget = gnutarget_string.c_str ();
431}
432
433/* A completion function for "set gnutarget". */
434
435static void
437 completion_tracker &tracker,
438 const char *text, const char *word)
439{
440 static const char **bfd_targets;
441
442 if (bfd_targets == NULL)
443 {
444 int last;
445
446 bfd_targets = bfd_target_list ();
447 for (last = 0; bfd_targets[last] != NULL; ++last)
448 ;
449
450 bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
451 bfd_targets[last] = "auto";
452 bfd_targets[last + 1] = NULL;
453 }
454
455 complete_on_enum (tracker, bfd_targets, text, word);
456}
457
458/* Set the gnutarget. */
459void
460set_gnutarget (const char *newtarget)
461{
462 gnutarget_string = newtarget;
463 set_gnutarget_command (NULL, 0, NULL);
464}
465
466void _initialize_core ();
467void
469{
470 cmd_list_element *core_file_cmd
471 = add_cmd ("core-file", class_files, core_file_command, _("\
472Use FILE as core dump for examining memory and registers.\n\
473Usage: core-file FILE\n\
474No arg means have no core file. This command has been superseded by the\n\
475`target core' and `detach' commands."), &cmdlist);
476 set_cmd_completer (core_file_cmd, filename_completer);
477
478
479 set_show_commands set_show_gnutarget
481 &gnutarget_string, _("\
482Set the current BFD target."), _("\
483Show the current BFD target."), _("\
484Use `set gnutarget auto' to specify automatic detection."),
487 &setlist, &showlist);
488 set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
489
490 add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
491
492 if (getenv ("GNUTARGET"))
493 set_gnutarget (getenv ("GNUTARGET"));
494 else
495 set_gnutarget ("auto");
496}
void * xrealloc(void *ptr, size_t size)
Definition alloc.c:65
struct gdbarch * target_gdbarch(void)
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * cmdlist
Definition cli-cmds.c:87
struct cmd_list_element * setlist
Definition cli-cmds.c:119
struct cmd_list_element * add_alias_cmd(const char *name, cmd_list_element *target, enum command_class theclass, int abbrev_flag, struct cmd_list_element **list)
Definition cli-decode.c:294
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition cli-decode.c:117
set_show_commands add_setshow_string_noescape_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:953
void complete_on_enum(completion_tracker &tracker, const char *const *enumlist, const char *text, const char *word)
const char * remove_trailing_whitespace(const char *start, const char *s)
Definition cli-utils.c:372
@ class_files
Definition command.h:57
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition completer.c:204
void validate_files(void)
Definition corefile.c:134
void write_memory(CORE_ADDR memaddr, const bfd_byte *myaddr, ssize_t len)
Definition corefile.c:347
CORE_ADDR read_memory_typed_address(CORE_ADDR addr, struct type *type)
Definition corefile.c:336
static void complete_set_gnutarget(struct cmd_list_element *cmd, completion_tracker &tracker, const char *text, const char *word)
Definition corefile.c:436
void read_stack(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:246
static void show_gnutarget_string(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition corefile.c:410
ULONGEST read_memory_unsigned_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
Definition corefile.c:306
void reopen_exec_file(void)
Definition corefile.c:106
static void set_gnutarget_command(const char *ignore, int from_tty, struct cmd_list_element *c)
Definition corefile.c:419
static void read_memory_object(enum target_object object, CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:211
static void call_extra_exec_file_hooks(const char *filename)
Definition corefile.c:61
void write_memory_unsigned_integer(CORE_ADDR addr, int len, enum bfd_endian byte_order, ULONGEST value)
Definition corefile.c:380
void read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:238
void specify_exec_file_hook(void(*hook)(const char *))
Definition corefile.c:73
const char * get_exec_file(int err)
Definition corefile.c:150
int safe_read_memory_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order, LONGEST *return_value)
Definition corefile.c:264
LONGEST read_memory_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
Definition corefile.c:296
void write_memory_with_notification(CORE_ADDR memaddr, const bfd_byte *myaddr, ssize_t len)
Definition corefile.c:370
int safe_read_memory_unsigned_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order, ULONGEST *return_value)
Definition corefile.c:282
ULONGEST read_code_unsigned_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
Definition corefile.c:326
hook_type deprecated_exec_file_display_hook
Definition corefile.c:49
void read_code(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:254
static void notify_memory_changed(inferior *inf, CORE_ADDR addr, ssize_t len, const bfd_byte *data)
Definition corefile.c:360
void write_memory_signed_integer(CORE_ADDR addr, int len, enum bfd_endian byte_order, LONGEST value)
Definition corefile.c:393
static std::string gnutarget_string
Definition corefile.c:408
void set_gnutarget(const char *newtarget)
Definition corefile.c:460
static int exec_file_hook_count
Definition corefile.c:52
const char * gnutarget
Definition corefile.c:405
LONGEST read_code_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
Definition corefile.c:316
void memory_error(enum target_xfer_status err, CORE_ADDR memaddr)
Definition corefile.c:186
static hook_type * exec_file_extra_hooks
Definition corefile.c:50
void _initialize_core()
Definition corefile.c:468
void(* hook_type)(const char *)
Definition corefile.c:47
std::string memory_error_message(enum target_xfer_status err, struct gdbarch *gdbarch, CORE_ADDR memaddr)
Definition corefile.c:163
void core_file_command(const char *filename, int from_tty)
Definition corelow.c:395
static void store_signed_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, LONGEST val)
Definition defs.h:508
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition defs.h:515
CORE_ADDR extract_typed_address(const gdb_byte *buf, struct type *type)
Definition findvar.c:152
static LONGEST extract_signed_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:465
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
#define QUIT
Definition defs.h:187
void exec_file_attach(const char *filename, int from_tty)
Definition exec.c:365
#define core_bfd
Definition gdbcore.h:130
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
struct inferior * current_inferior(void)
Definition inferior.c:55
void interps_notify_memory_changed(inferior *inf, CORE_ADDR addr, ssize_t len, const bfd_byte *data)
Definition interps.c:571
observable< struct inferior *, CORE_ADDR, ssize_t, const bfd_byte * > memory_changed
struct program_space * current_program_space
Definition progspace.c:40
Definition gnu-nat.c:153
bfd * exec_bfd() const
Definition progspace.h:268
gdb::unique_xmalloc_ptr< char > exec_filename
Definition progspace.h:325
cmd_list_element * set
Definition command.h:422
ULONGEST length() const
Definition gdbtypes.h:983
Definition value.h:130
const char * target_xfer_status_to_string(enum target_xfer_status status)
Definition target.c:1370
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition target.c:1861
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition target.c:1785
target_xfer_partial_ftype target_xfer_partial
target_xfer_status
Definition target.h:219
@ TARGET_XFER_E_IO
Definition target.h:232
@ TARGET_XFER_EOF
Definition target.h:224
@ TARGET_XFER_OK
Definition target.h:221
@ TARGET_XFER_UNAVAILABLE
Definition target.h:227
target_object
Definition target.h:143
@ TARGET_OBJECT_STACK_MEMORY
Definition target.h:155
@ TARGET_OBJECT_MEMORY
Definition target.h:147
@ TARGET_OBJECT_CODE_MEMORY
Definition target.h:158
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3166
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886