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
;
77 /* This routine opens and sets up the core file bfd */
80 core_open (filename
, from_tty
)
86 struct cleanup
*old_chain
;
91 target_preopen (from_tty
);
95 "No core file specified. (Use `detach' to stop debugging a core file.)"
96 : "No core file specified.");
99 filename
= tilde_expand (filename
);
100 if (filename
[0] != '/') {
101 temp
= concat (current_directory
, "/", filename
);
106 old_chain
= make_cleanup (free
, filename
);
107 temp_bfd
= bfd_openr (filename
, NULL
);
108 if (temp_bfd
== NULL
)
110 perror_with_name (filename
);
113 if (!bfd_check_format (temp_bfd
, bfd_core
))
115 bfd_close (temp_bfd
);
116 error ("\"%s\" does not appear to be a core dump", filename
);
119 /* Looks semi-reasonable. Toss the old core file and work on the new. */
121 discard_cleanups (old_chain
); /* Don't free filename any more */
122 unpush_target (&core_ops
);
124 old_chain
= make_cleanup (core_close
, core_bfd
);
128 /* Find the data section */
129 if (build_section_table (core_bfd
, &core_ops
.sections
,
130 &core_ops
.sections_end
))
131 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd
),
132 bfd_errmsg (bfd_error
));
134 ontop
= !push_target (&core_ops
);
135 discard_cleanups (old_chain
);
137 p
= bfd_core_file_failing_command (core_bfd
);
139 printf ("Core file invoked as `%s'.\n", p
);
141 siggy
= bfd_core_file_failing_signal (core_bfd
);
143 printf ("Program terminated with signal %d, %s.\n", siggy
,
144 siggy
< NSIG
? sys_siglist
[siggy
] : "(undocumented)");
147 /* Fetch all registers from core file */
148 target_fetch_registers (-1);
149 /* Add symbols and section mappings for any shared libraries */
151 SOLIB_ADD (NULL
, from_tty
, &core_ops
);
153 /* Now, set up the frame cache, and print the top of stack */
154 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
156 select_frame (get_current_frame (), 0);
157 print_sel_frame (0); /* Print the top frame and source line */
160 "Warning: you won't be able to access this core file until you terminate\n\
161 your %s; do ``info files''\n", current_target
->to_longname
);
166 core_detach (args
, from_tty
)
171 error ("Too many arguments");
172 unpush_target (&core_ops
);
174 printf ("No core file now.\n");
177 /* Backward compatability with old way of specifying core files. */
180 core_file_command (filename
, from_tty
)
184 dont_repeat (); /* Either way, seems bogus. */
186 core_detach (filename
, from_tty
);
188 core_open (filename
, from_tty
);
192 /* Call this to specify the hook for exec_file_command to call back.
193 This is called from the x-window display code. */
196 specify_exec_file_hook (hook
)
199 exec_file_display_hook
= hook
;
202 /* The exec file must be closed before running an inferior.
203 If it is needed again after the inferior dies, it must
211 bfd_tempclose (exec_bfd
);
220 bfd_reopen (exec_bfd
);
224 /* If we have both a core file and an exec file,
225 print a warning if they don't go together. */
230 if (exec_bfd
&& core_bfd
)
232 if (core_file_matches_executable_p (core_bfd
, exec_bfd
))
233 printf ("Warning: core file does not match specified executable file.\n");
234 else if (bfd_get_mtime(exec_bfd
) > bfd_get_mtime(core_bfd
))
235 printf ("Warning: exec file is newer than core file.\n");
239 /* Return the name of the executable file as a string.
240 ERR nonzero means get error if there is none specified;
241 otherwise return 0 in that case. */
247 if (exec_bfd
) return bfd_get_filename(exec_bfd
);
248 if (!err
) return NULL
;
250 error ("No executable file specified.\n\
251 Use the \"file\" or \"exec-file\" command.");
257 struct target_ops
*t
;
259 struct section_table
*p
;
261 printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd
));
263 for (p
= t
->sections
; p
< t
->sections_end
; p
++)
264 if (p
->bfd
== core_bfd
)
265 printf("\tcore file from 0x%08x to 0x%08x is %s\n",
267 bfd_section_name (p
->bfd
, p
->sec_ptr
));
269 printf("\tshared lib from 0x%08x to 0x%08x is %s in %s\n",
271 bfd_section_name (p
->bfd
, p
->sec_ptr
),
272 bfd_get_filename (p
->bfd
));
277 memory_error (status
, memaddr
)
284 /* Actually, address between memaddr and memaddr + len
285 was out of bounds. */
286 error ("Cannot access memory at address 0x%x.", memaddr
);
290 if (status
>= sys_nerr
|| status
< 0)
291 error ("Error accessing memory address 0x%x: unknown error (%d).",
294 error ("Error accessing memory address 0x%x: %s.",
295 memaddr
, sys_errlist
[status
]);
299 /* Same as target_read_memory, but report an error if can't read. */
301 read_memory (memaddr
, myaddr
, len
)
307 status
= target_read_memory (memaddr
, myaddr
, len
);
309 memory_error (status
, memaddr
);
312 /* Same as target_write_memory, but report an error if can't write. */
314 write_memory (memaddr
, myaddr
, len
)
321 status
= target_write_memory (memaddr
, myaddr
, len
);
323 memory_error (status
, memaddr
);
326 /* Read an integer from debugged memory, given address and number of bytes. */
329 read_memory_integer (memaddr
, len
)
338 if (len
== sizeof (char))
340 read_memory (memaddr
, &cbuf
, len
);
343 if (len
== sizeof (short))
345 read_memory (memaddr
, (char *)&sbuf
, len
);
346 SWAP_TARGET_AND_HOST (&sbuf
, sizeof (short));
349 if (len
== sizeof (int))
351 read_memory (memaddr
, (char *)&ibuf
, len
);
352 SWAP_TARGET_AND_HOST (&ibuf
, sizeof (int));
355 if (len
== sizeof (lbuf
))
357 read_memory (memaddr
, (char *)&lbuf
, len
);
358 SWAP_TARGET_AND_HOST (&lbuf
, sizeof (lbuf
));
361 error ("Cannot handle integers of %d bytes.", len
);
362 return -1; /* for lint */
365 /* Get the registers out of a core file. This is the machine-
366 independent part. Fetch_core_registers is the machine-dependent
367 part, typically implemented in the xm-file for each architecture. */
369 /* We just get all the registers, so we don't use regno. */
372 get_core_registers (regno
)
379 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg");
380 size
= bfd_section_size (core_bfd
, reg_sec
);
381 the_regs
= alloca (size
);
382 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
385 fetch_core_registers (the_regs
, size
, 0);
389 fprintf (stderr
, "Couldn't fetch registers from core file: %s\n",
390 bfd_errmsg (bfd_error
));
393 /* Now do it again for the float registers, if they exist. */
394 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg2");
396 size
= bfd_section_size (core_bfd
, reg_sec
);
397 the_regs
= alloca (size
);
398 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
401 fetch_core_registers (the_regs
, size
, 2);
405 fprintf (stderr
, "Couldn't fetch register set 2 from core file: %s\n",
406 bfd_errmsg (bfd_error
));
412 struct target_ops core_ops
= {
413 "core", "Local core dump file",
414 "Use a core file as a target. Specify the filename of the core file.",
415 core_open
, core_close
,
416 child_attach
, core_detach
, 0, 0, /* resume, wait */
418 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
419 xfer_memory
, core_files_info
,
420 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
421 0, 0, 0, 0, 0, /* terminal stuff */
422 0, 0, 0, 0, /* kill, load, call fn, lookup sym */
423 child_create_inferior
, 0, /* mourn_inferior */
424 core_stratum
, 0, /* next */
425 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
426 0, 0, /* section pointers */
427 OPS_MAGIC
, /* Always the last thing */
434 add_com ("core-file", class_files
, core_file_command
,
435 "Use FILE as core dump for examining memory and registers.\n\
436 No arg means have no core file. This command has been superseded by the\n\
437 `target core' and `detach' commands.");
438 add_target (&core_ops
);