1 /* Work with core dump and executable files, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "frame.h" /* required by inferior.h */
33 extern int xfer_memory ();
34 extern void child_attach (), child_create_inferior ();
37 extern char *sys_errlist
[];
38 extern char *sys_siglist
[];
40 extern char registers
[];
42 /* Hook for `exec_file_command' command to call. */
44 void (*exec_file_display_hook
) () = NULL
;
46 /* Binary file diddling handle for the core file. */
51 extern struct target_ops core_ops
;
54 /* Discard all vestiges of any previous core file
55 and mark data and stack spaces as empty. */
63 free (bfd_get_filename (core_bfd
));
69 if (core_ops
.sections
) {
70 free (core_ops
.sections
);
71 core_ops
.sections
= NULL
;
72 core_ops
.sections_end
= NULL
;
78 /* Stub function for catch_errors around shared library hacking. */
81 solib_add_stub (from_tty
)
84 SOLIB_ADD (NULL
, from_tty
, &core_ops
);
87 #endif /* SOLIB_ADD */
89 /* This routine opens and sets up the core file bfd */
92 core_open (filename
, from_tty
)
98 struct cleanup
*old_chain
;
103 target_preopen (from_tty
);
107 "No core file specified. (Use `detach' to stop debugging a core file.)"
108 : "No core file specified.");
111 filename
= tilde_expand (filename
);
112 if (filename
[0] != '/') {
113 temp
= concat (current_directory
, "/", filename
);
118 old_chain
= make_cleanup (free
, filename
);
119 temp_bfd
= bfd_openr (filename
, NULL
);
120 if (temp_bfd
== NULL
)
122 perror_with_name (filename
);
125 if (!bfd_check_format (temp_bfd
, bfd_core
))
127 bfd_close (temp_bfd
);
128 error ("\"%s\" does not appear to be a core dump", filename
);
131 /* Looks semi-reasonable. Toss the old core file and work on the new. */
133 discard_cleanups (old_chain
); /* Don't free filename any more */
134 unpush_target (&core_ops
);
136 old_chain
= make_cleanup (core_close
, core_bfd
);
140 /* Find the data section */
141 if (build_section_table (core_bfd
, &core_ops
.sections
,
142 &core_ops
.sections_end
))
143 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd
),
144 bfd_errmsg (bfd_error
));
146 ontop
= !push_target (&core_ops
);
147 discard_cleanups (old_chain
);
149 p
= bfd_core_file_failing_command (core_bfd
);
151 printf ("Core file invoked as `%s'.\n", p
);
153 siggy
= bfd_core_file_failing_signal (core_bfd
);
155 printf ("Program terminated with signal %d, %s.\n", siggy
,
156 siggy
< NSIG
? sys_siglist
[siggy
] : "(undocumented)");
159 /* Fetch all registers from core file */
160 target_fetch_registers (-1);
162 /* Add symbols and section mappings for any shared libraries */
164 (void) catch_errors (solib_add_stub
, from_tty
, (char *)0);
166 /* Now, set up the frame cache, and print the top of stack */
167 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
169 select_frame (get_current_frame (), 0);
170 print_sel_frame (0); /* Print the top frame and source line */
173 "Warning: you won't be able to access this core file until you terminate\n\
174 your %s; do ``info files''\n", current_target
->to_longname
);
179 core_detach (args
, from_tty
)
184 error ("Too many arguments");
185 unpush_target (&core_ops
);
187 printf ("No core file now.\n");
190 /* Backward compatability with old way of specifying core files. */
193 core_file_command (filename
, from_tty
)
197 dont_repeat (); /* Either way, seems bogus. */
199 core_detach (filename
, from_tty
);
201 core_open (filename
, from_tty
);
205 /* Call this to specify the hook for exec_file_command to call back.
206 This is called from the x-window display code. */
209 specify_exec_file_hook (hook
)
212 exec_file_display_hook
= hook
;
215 /* The exec file must be closed before running an inferior.
216 If it is needed again after the inferior dies, it must
224 bfd_tempclose (exec_bfd
);
233 bfd_reopen (exec_bfd
);
237 /* If we have both a core file and an exec file,
238 print a warning if they don't go together. */
243 if (exec_bfd
&& core_bfd
)
245 if (core_file_matches_executable_p (core_bfd
, exec_bfd
))
246 printf ("Warning: core file does not match specified executable file.\n");
247 else if (bfd_get_mtime(exec_bfd
) > bfd_get_mtime(core_bfd
))
248 printf ("Warning: exec file is newer than core file.\n");
252 /* Return the name of the executable file as a string.
253 ERR nonzero means get error if there is none specified;
254 otherwise return 0 in that case. */
260 if (exec_bfd
) return bfd_get_filename(exec_bfd
);
261 if (!err
) return NULL
;
263 error ("No executable file specified.\n\
264 Use the \"file\" or \"exec-file\" command.");
270 struct target_ops
*t
;
272 struct section_table
*p
;
274 printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd
));
276 for (p
= t
->sections
; p
< t
->sections_end
; p
++)
277 if (p
->bfd
== core_bfd
)
278 printf("\tcore file from 0x%08x to 0x%08x is %s\n",
280 bfd_section_name (p
->bfd
, p
->sec_ptr
));
282 printf("\tshared lib from 0x%08x to 0x%08x is %s in %s\n",
284 bfd_section_name (p
->bfd
, p
->sec_ptr
),
285 bfd_get_filename (p
->bfd
));
290 memory_error (status
, memaddr
)
297 /* Actually, address between memaddr and memaddr + len
298 was out of bounds. */
299 error ("Cannot access memory at address 0x%x.", memaddr
);
303 if (status
>= sys_nerr
|| status
< 0)
304 error ("Error accessing memory address 0x%x: unknown error (%d).",
307 error ("Error accessing memory address 0x%x: %s.",
308 memaddr
, sys_errlist
[status
]);
312 /* Same as target_read_memory, but report an error if can't read. */
314 read_memory (memaddr
, myaddr
, len
)
320 status
= target_read_memory (memaddr
, myaddr
, len
);
322 memory_error (status
, memaddr
);
325 /* Same as target_write_memory, but report an error if can't write. */
327 write_memory (memaddr
, myaddr
, len
)
334 status
= target_write_memory (memaddr
, myaddr
, len
);
336 memory_error (status
, memaddr
);
339 /* Read an integer from debugged memory, given address and number of bytes. */
342 read_memory_integer (memaddr
, len
)
351 if (len
== sizeof (char))
353 read_memory (memaddr
, &cbuf
, len
);
356 if (len
== sizeof (short))
358 read_memory (memaddr
, (char *)&sbuf
, len
);
359 SWAP_TARGET_AND_HOST (&sbuf
, sizeof (short));
362 if (len
== sizeof (int))
364 read_memory (memaddr
, (char *)&ibuf
, len
);
365 SWAP_TARGET_AND_HOST (&ibuf
, sizeof (int));
368 if (len
== sizeof (lbuf
))
370 read_memory (memaddr
, (char *)&lbuf
, len
);
371 SWAP_TARGET_AND_HOST (&lbuf
, sizeof (lbuf
));
374 error ("Cannot handle integers of %d bytes.", len
);
375 return -1; /* for lint */
378 /* Get the registers out of a core file. This is the machine-
379 independent part. Fetch_core_registers is the machine-dependent
380 part, typically implemented in the xm-file for each architecture. */
382 /* We just get all the registers, so we don't use regno. */
385 get_core_registers (regno
)
392 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg");
393 size
= bfd_section_size (core_bfd
, reg_sec
);
394 the_regs
= alloca (size
);
395 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
398 fetch_core_registers (the_regs
, size
, 0);
402 fprintf (stderr
, "Couldn't fetch registers from core file: %s\n",
403 bfd_errmsg (bfd_error
));
406 /* Now do it again for the float registers, if they exist. */
407 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg2");
409 size
= bfd_section_size (core_bfd
, reg_sec
);
410 the_regs
= alloca (size
);
411 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
414 fetch_core_registers (the_regs
, size
, 2);
418 fprintf (stderr
, "Couldn't fetch register set 2 from core file: %s\n",
419 bfd_errmsg (bfd_error
));
425 struct target_ops core_ops
= {
426 "core", "Local core dump file",
427 "Use a core file as a target. Specify the filename of the core file.",
428 core_open
, core_close
,
429 child_attach
, core_detach
, 0, 0, /* resume, wait */
431 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
432 xfer_memory
, core_files_info
,
433 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
434 0, 0, 0, 0, 0, /* terminal stuff */
435 0, 0, 0, 0, /* kill, load, call fn, lookup sym */
436 child_create_inferior
, 0, /* mourn_inferior */
437 core_stratum
, 0, /* next */
438 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
439 0, 0, /* section pointers */
440 OPS_MAGIC
, /* Always the last thing */
447 add_com ("core-file", class_files
, core_file_command
,
448 "Use FILE as core dump for examining memory and registers.\n\
449 No arg means have no core file. This command has been superseded by the\n\
450 `target core' and `detach' commands.");
451 add_target (&core_ops
);
This page took 0.038395 seconds and 4 git commands to generate.