c56281b289b6b9264a1f8fdf88577e9314ca2b38
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. */
26 #include "frame.h" /* required by inferior.h */
34 extern int xfer_memory ();
35 extern void child_attach (), child_create_inferior ();
38 extern char *sys_errlist
[];
39 extern char *sys_siglist
[];
41 extern char registers
[];
43 /* Hook for `exec_file_command' command to call. */
45 void (*exec_file_display_hook
) () = NULL
;
47 /* Binary file diddling handle for the core file. */
52 extern struct target_ops core_ops
;
55 /* Discard all vestiges of any previous core file
56 and mark data and stack spaces as empty. */
64 free (bfd_get_filename (core_bfd
));
70 if (core_ops
.sections
) {
71 free (core_ops
.sections
);
72 core_ops
.sections
= NULL
;
73 core_ops
.sections_end
= NULL
;
79 /* Stub function for catch_errors around shared library hacking. */
82 solib_add_stub (from_tty
)
85 SOLIB_ADD (NULL
, (int)from_tty
, &core_ops
);
88 #endif /* SOLIB_ADD */
90 /* This routine opens and sets up the core file bfd */
93 core_open (filename
, from_tty
)
99 struct cleanup
*old_chain
;
105 target_preopen (from_tty
);
109 "No core file specified. (Use `detach' to stop debugging a core file.)"
110 : "No core file specified.");
113 filename
= tilde_expand (filename
);
114 if (filename
[0] != '/') {
115 temp
= concat (current_directory
, "/", filename
);
120 old_chain
= make_cleanup (free
, filename
);
122 scratch_chan
= open (filename
, write_files
? O_RDWR
: O_RDONLY
, 0);
123 if (scratch_chan
< 0)
124 perror_with_name (filename
);
126 temp_bfd
= bfd_fdopenr (filename
, NULL
, scratch_chan
);
127 if (temp_bfd
== NULL
)
129 perror_with_name (filename
);
132 if (!bfd_check_format (temp_bfd
, bfd_core
))
134 bfd_close (temp_bfd
);
135 error ("\"%s\" does not appear to be a core dump", filename
);
138 /* Looks semi-reasonable. Toss the old core file and work on the new. */
140 discard_cleanups (old_chain
); /* Don't free filename any more */
141 unpush_target (&core_ops
);
143 old_chain
= make_cleanup (core_close
, core_bfd
);
147 /* Find the data section */
148 if (build_section_table (core_bfd
, &core_ops
.sections
,
149 &core_ops
.sections_end
))
150 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd
),
151 bfd_errmsg (bfd_error
));
153 ontop
= !push_target (&core_ops
);
154 discard_cleanups (old_chain
);
156 p
= bfd_core_file_failing_command (core_bfd
);
158 printf ("Core file invoked as `%s'.\n", p
);
160 siggy
= bfd_core_file_failing_signal (core_bfd
);
162 printf ("Program terminated with signal %d, %s.\n", siggy
,
163 siggy
< NSIG
? sys_siglist
[siggy
] : "(undocumented)");
166 /* Fetch all registers from core file */
167 target_fetch_registers (-1);
169 /* Add symbols and section mappings for any shared libraries */
171 (void) catch_errors (solib_add_stub
, (char *)from_tty
, (char *)0);
174 /* Now, set up the frame cache, and print the top of stack */
175 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
177 select_frame (get_current_frame (), 0);
178 print_stack_frame (selected_frame
, selected_frame_level
, 1);
181 "Warning: you won't be able to access this core file until you terminate\n\
182 your %s; do ``info files''\n", current_target
->to_longname
);
187 core_detach (args
, from_tty
)
192 error ("Too many arguments");
193 unpush_target (&core_ops
);
195 printf ("No core file now.\n");
198 /* Backward compatability with old way of specifying core files. */
201 core_file_command (filename
, from_tty
)
205 dont_repeat (); /* Either way, seems bogus. */
207 core_detach (filename
, from_tty
);
209 core_open (filename
, from_tty
);
213 /* Call this to specify the hook for exec_file_command to call back.
214 This is called from the x-window display code. */
217 specify_exec_file_hook (hook
)
220 exec_file_display_hook
= hook
;
223 /* The exec file must be closed before running an inferior.
224 If it is needed again after the inferior dies, it must
232 bfd_tempclose (exec_bfd
);
241 bfd_reopen (exec_bfd
);
245 /* If we have both a core file and an exec file,
246 print a warning if they don't go together. */
251 if (exec_bfd
&& core_bfd
)
253 if (!core_file_matches_executable_p (core_bfd
, exec_bfd
))
254 printf ("Warning: core file may not match specified executable file.\n");
255 else if (bfd_get_mtime(exec_bfd
) > bfd_get_mtime(core_bfd
))
256 printf ("Warning: exec file is newer than core file.\n");
260 /* Return the name of the executable file as a string.
261 ERR nonzero means get error if there is none specified;
262 otherwise return 0 in that case. */
268 if (exec_bfd
) return bfd_get_filename(exec_bfd
);
269 if (!err
) return NULL
;
271 error ("No executable file specified.\n\
272 Use the \"file\" or \"exec-file\" command.");
278 struct target_ops
*t
;
280 struct section_table
*p
;
282 printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd
));
284 for (p
= t
->sections
; p
< t
->sections_end
; p
++) {
285 printf("\t%s", local_hex_string_custom (p
->addr
, "08"));
286 printf(" - %s is %s", local_hex_string_custom (p
->endaddr
, "08"),
287 bfd_section_name (p
->bfd
, p
->sec_ptr
));
288 if (p
->bfd
!= core_bfd
) {
289 printf(" in %s", bfd_get_filename (p
->bfd
));
296 memory_error (status
, memaddr
)
303 /* Actually, address between memaddr and memaddr + len
304 was out of bounds. */
305 error ("Cannot access memory at address %s.", local_hex_string(memaddr
));
309 if (status
>= sys_nerr
|| status
< 0)
310 error ("Error accessing memory address %s: unknown error (%d).",
311 local_hex_string(memaddr
), status
);
313 error ("Error accessing memory address %s: %s.",
314 local_hex_string(memaddr
), sys_errlist
[status
]);
318 /* Same as target_read_memory, but report an error if can't read. */
320 read_memory (memaddr
, myaddr
, len
)
326 status
= target_read_memory (memaddr
, myaddr
, len
);
328 memory_error (status
, memaddr
);
331 /* Same as target_write_memory, but report an error if can't write. */
333 write_memory (memaddr
, myaddr
, len
)
340 status
= target_write_memory (memaddr
, myaddr
, len
);
342 memory_error (status
, memaddr
);
345 /* Read an integer from debugged memory, given address and number of bytes. */
348 read_memory_integer (memaddr
, len
)
357 if (len
== sizeof (char))
359 read_memory (memaddr
, &cbuf
, len
);
362 if (len
== sizeof (short))
364 read_memory (memaddr
, (char *)&sbuf
, len
);
365 SWAP_TARGET_AND_HOST (&sbuf
, sizeof (short));
368 if (len
== sizeof (int))
370 read_memory (memaddr
, (char *)&ibuf
, len
);
371 SWAP_TARGET_AND_HOST (&ibuf
, sizeof (int));
374 if (len
== sizeof (lbuf
))
376 read_memory (memaddr
, (char *)&lbuf
, len
);
377 SWAP_TARGET_AND_HOST (&lbuf
, sizeof (lbuf
));
380 error ("Cannot handle integers of %d bytes.", len
);
381 return -1; /* for lint */
384 /* Get the registers out of a core file. This is the machine-
385 independent part. Fetch_core_registers is the machine-dependent
386 part, typically implemented in the xm-file for each architecture. */
388 /* We just get all the registers, so we don't use regno. */
391 get_core_registers (regno
)
398 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg");
399 size
= bfd_section_size (core_bfd
, reg_sec
);
400 the_regs
= alloca (size
);
401 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
404 fetch_core_registers (the_regs
, size
, 0);
408 fprintf (stderr
, "Couldn't fetch registers from core file: %s\n",
409 bfd_errmsg (bfd_error
));
412 /* Now do it again for the float registers, if they exist. */
413 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg2");
415 size
= bfd_section_size (core_bfd
, reg_sec
);
416 the_regs
= alloca (size
);
417 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
,
420 fetch_core_registers (the_regs
, size
, 2);
424 fprintf (stderr
, "Couldn't fetch register set 2 from core file: %s\n",
425 bfd_errmsg (bfd_error
));
431 struct target_ops core_ops
= {
432 "core", "Local core dump file",
433 "Use a core file as a target. Specify the filename of the core file.",
434 core_open
, core_close
,
435 child_attach
, core_detach
, 0, 0, /* resume, wait */
437 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
438 xfer_memory
, core_files_info
,
439 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
440 0, 0, 0, 0, 0, /* terminal stuff */
441 0, 0, 0, 0, /* kill, load, call fn, lookup sym */
442 child_create_inferior
, 0, /* mourn_inferior */
443 core_stratum
, 0, /* next */
444 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
445 0, 0, /* section pointers */
446 OPS_MAGIC
, /* Always the last thing */
453 add_com ("core-file", class_files
, core_file_command
,
454 "Use FILE as core dump for examining memory and registers.\n\
455 No arg means have no core file. This command has been superseded by the\n\
456 `target core' and `detach' commands.");
457 add_target (&core_ops
);
This page took 0.055018 seconds and 4 git commands to generate.