1 /* Work with core dump and executable files, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 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. */
24 #include "frame.h" /* required by inferior.h */
34 solib_add_stub PARAMS ((char *));
38 core_close PARAMS ((int));
41 core_open PARAMS ((char *, int));
44 core_detach PARAMS ((char *, int));
47 get_core_registers PARAMS ((int));
50 core_files_info PARAMS ((struct target_ops *));
53 extern char *sys_errlist[];
54 extern char *sys_siglist[];
56 extern char registers[];
58 /* Hook for `exec_file_command' command to call. */
60 void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
62 /* Binary file diddling handle for the core file. */
67 extern struct target_ops core_ops;
70 /* Discard all vestiges of any previous core file
71 and mark data and stack spaces as empty. */
79 free (bfd_get_filename (core_bfd));
85 if (core_ops.to_sections) {
86 free (core_ops.to_sections);
87 core_ops.to_sections = NULL;
88 core_ops.to_sections_end = NULL;
94 /* Stub function for catch_errors around shared library hacking. */
97 solib_add_stub (from_tty)
100 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
103 #endif /* SOLIB_ADD */
105 /* This routine opens and sets up the core file bfd */
108 core_open (filename, from_tty)
114 struct cleanup *old_chain;
120 target_preopen (from_tty);
124 "No core file specified. (Use `detach' to stop debugging a core file.)"
125 : "No core file specified.");
128 filename = tilde_expand (filename);
129 if (filename[0] != '/') {
130 temp = concat (current_directory, "/", filename, NULL);
135 old_chain = make_cleanup (free, filename);
137 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
138 if (scratch_chan < 0)
139 perror_with_name (filename);
141 temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
142 if (temp_bfd == NULL)
144 perror_with_name (filename);
147 if (!bfd_check_format (temp_bfd, bfd_core))
149 /* Do it after the err msg */
150 make_cleanup (bfd_close, temp_bfd);
151 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
154 /* Looks semi-reasonable. Toss the old core file and work on the new. */
156 discard_cleanups (old_chain); /* Don't free filename any more */
157 unpush_target (&core_ops);
159 old_chain = make_cleanup (core_close, core_bfd);
163 /* Find the data section */
164 if (build_section_table (core_bfd, &core_ops.to_sections,
165 &core_ops.to_sections_end))
166 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
167 bfd_errmsg (bfd_error));
169 ontop = !push_target (&core_ops);
170 discard_cleanups (old_chain);
172 p = bfd_core_file_failing_command (core_bfd);
174 printf ("Core was generated by `%s'.\n", p);
176 siggy = bfd_core_file_failing_signal (core_bfd);
178 printf ("Program terminated with signal %d, %s.\n", siggy,
179 siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
182 /* Fetch all registers from core file */
183 target_fetch_registers (-1);
185 /* Add symbols and section mappings for any shared libraries */
187 (void) catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
190 /* Now, set up the frame cache, and print the top of stack */
191 set_current_frame (create_new_frame (read_register (FP_REGNUM),
193 select_frame (get_current_frame (), 0);
194 print_stack_frame (selected_frame, selected_frame_level, 1);
197 "Warning: you won't be able to access this core file until you terminate\n\
198 your %s; do ``info files''\n", current_target->to_longname);
203 core_detach (args, from_tty)
208 error ("Too many arguments");
209 unpush_target (&core_ops);
211 printf ("No core file now.\n");
214 /* Backward compatability with old way of specifying core files. */
217 core_file_command (filename, from_tty)
221 dont_repeat (); /* Either way, seems bogus. */
223 core_detach (filename, from_tty);
225 core_open (filename, from_tty);
229 /* Call this to specify the hook for exec_file_command to call back.
230 This is called from the x-window display code. */
233 specify_exec_file_hook (hook)
234 void (*hook) PARAMS ((char *));
236 exec_file_display_hook = hook;
239 /* The exec file must be closed before running an inferior.
240 If it is needed again after the inferior dies, it must
248 bfd_tempclose (exec_bfd);
257 bfd_reopen (exec_bfd);
261 /* If we have both a core file and an exec file,
262 print a warning if they don't go together. */
267 if (exec_bfd && core_bfd)
269 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
270 printf ("Warning: core file may not match specified executable file.\n");
271 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
272 printf ("Warning: exec file is newer than core file.\n");
276 /* Return the name of the executable file as a string.
277 ERR nonzero means get error if there is none specified;
278 otherwise return 0 in that case. */
284 if (exec_bfd) return bfd_get_filename(exec_bfd);
285 if (!err) return NULL;
287 error ("No executable file specified.\n\
288 Use the \"file\" or \"exec-file\" command.");
294 struct target_ops *t;
296 print_section_info (t, core_bfd);
299 /* Report a memory error with error(). */
302 memory_error (status, memaddr)
309 /* Actually, address between memaddr and memaddr + len
310 was out of bounds. */
311 error ("Cannot access memory at address %s.", local_hex_string(memaddr));
315 if (status >= sys_nerr || status < 0)
316 error ("Error accessing memory address %s: unknown error (%d).",
317 local_hex_string(memaddr), status);
319 error ("Error accessing memory address %s: %s.",
320 local_hex_string(memaddr), sys_errlist[status]);
324 /* Same as target_read_memory, but report an error if can't read. */
326 read_memory (memaddr, myaddr, len)
332 status = target_read_memory (memaddr, myaddr, len);
334 memory_error (status, memaddr);
337 /* Same as target_write_memory, but report an error if can't write. */
339 write_memory (memaddr, myaddr, len)
346 status = target_write_memory (memaddr, myaddr, len);
348 memory_error (status, memaddr);
351 /* Read an integer from debugged memory, given address and number of bytes. */
354 read_memory_integer (memaddr, len)
363 if (len == sizeof (char))
365 read_memory (memaddr, &cbuf, len);
368 if (len == sizeof (short))
370 read_memory (memaddr, (char *)&sbuf, len);
371 SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
374 if (len == sizeof (int))
376 read_memory (memaddr, (char *)&ibuf, len);
377 SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
380 if (len == sizeof (lbuf))
382 read_memory (memaddr, (char *)&lbuf, len);
383 SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
386 error ("Cannot handle integers of %d bytes.", len);
387 return -1; /* for lint */
390 /* Get the registers out of a core file. This is the machine-
391 independent part. Fetch_core_registers is the machine-dependent
392 part, typically implemented in the xm-file for each architecture. */
394 /* We just get all the registers, so we don't use regno. */
397 get_core_registers (regno)
404 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
405 if (!reg_sec) goto cant;
406 size = bfd_section_size (core_bfd, reg_sec);
407 the_regs = alloca (size);
408 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
410 fetch_core_registers (the_regs, size, 0,
411 (unsigned) bfd_section_vma (abfd,reg_sec));
416 fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
417 bfd_errmsg (bfd_error));
420 /* Now do it again for the float registers, if they exist. */
421 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
423 size = bfd_section_size (core_bfd, reg_sec);
424 the_regs = alloca (size);
425 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
428 fetch_core_registers (the_regs, size, 2,
429 (unsigned) bfd_section_vma (abfd,reg_sec));
433 fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
434 bfd_errmsg (bfd_error));
440 struct target_ops core_ops = {
441 "core", "Local core dump file",
442 "Use a core file as a target. Specify the filename of the core file.",
443 core_open, core_close,
444 child_attach, core_detach, 0, 0, /* resume, wait */
446 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
447 xfer_memory, core_files_info,
448 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
449 0, 0, 0, 0, 0, /* terminal stuff */
450 0, 0, 0, /* kill, load, lookup sym */
451 child_create_inferior, 0, /* mourn_inferior */
452 core_stratum, 0, /* next */
453 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
454 0, 0, /* section pointers */
455 OPS_MAGIC, /* Always the last thing */
462 add_com ("core-file", class_files, core_file_command,
463 "Use FILE as core dump for examining memory and registers.\n\
464 No arg means have no core file. This command has been superseded by the\n\
465 `target core' and `detach' commands.");
466 add_target (&core_ops);