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 *));
52 extern char registers[];
54 /* Hook for `exec_file_command' command to call. */
56 void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
58 /* Binary file diddling handle for the core file. */
63 extern struct target_ops core_ops;
66 /* Discard all vestiges of any previous core file
67 and mark data and stack spaces as empty. */
75 free (bfd_get_filename (core_bfd));
81 if (core_ops.to_sections) {
82 free ((PTR)core_ops.to_sections);
83 core_ops.to_sections = NULL;
84 core_ops.to_sections_end = NULL;
90 /* Stub function for catch_errors around shared library hacking. */
93 solib_add_stub (from_tty)
96 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
99 #endif /* SOLIB_ADD */
101 /* This routine opens and sets up the core file bfd */
104 core_open (filename, from_tty)
110 struct cleanup *old_chain;
116 target_preopen (from_tty);
120 "No core file specified. (Use `detach' to stop debugging a core file.)"
121 : "No core file specified.");
124 filename = tilde_expand (filename);
125 if (filename[0] != '/') {
126 temp = concat (current_directory, "/", filename, NULL);
131 old_chain = make_cleanup (free, filename);
133 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
134 if (scratch_chan < 0)
135 perror_with_name (filename);
137 temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
138 if (temp_bfd == NULL)
140 perror_with_name (filename);
143 if (!bfd_check_format (temp_bfd, bfd_core))
145 /* Do it after the err msg */
146 make_cleanup (bfd_close, temp_bfd);
147 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
150 /* Looks semi-reasonable. Toss the old core file and work on the new. */
152 discard_cleanups (old_chain); /* Don't free filename any more */
153 unpush_target (&core_ops);
155 old_chain = make_cleanup (core_close, core_bfd);
159 /* Find the data section */
160 if (build_section_table (core_bfd, &core_ops.to_sections,
161 &core_ops.to_sections_end))
162 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
163 bfd_errmsg (bfd_error));
165 ontop = !push_target (&core_ops);
166 discard_cleanups (old_chain);
168 p = bfd_core_file_failing_command (core_bfd);
170 printf_filtered ("Core was generated by `%s'.\n", p);
172 siggy = bfd_core_file_failing_signal (core_bfd);
174 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
175 safe_strsignal (siggy));
178 /* Fetch all registers from core file */
179 target_fetch_registers (-1);
181 /* Add symbols and section mappings for any shared libraries */
183 catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
186 /* Now, set up the frame cache, and print the top of stack */
187 set_current_frame (create_new_frame (read_register (FP_REGNUM),
189 select_frame (get_current_frame (), 0);
190 print_stack_frame (selected_frame, selected_frame_level, 1);
193 "you won't be able to access this core file until you terminate\n\
194 your %s; do ``info files''", current_target->to_longname);
199 core_detach (args, from_tty)
204 error ("Too many arguments");
205 unpush_target (&core_ops);
207 printf_filtered ("No core file now.\n");
210 /* Backward compatability with old way of specifying core files. */
213 core_file_command (filename, from_tty)
217 dont_repeat (); /* Either way, seems bogus. */
219 core_detach (filename, from_tty);
221 core_open (filename, from_tty);
225 /* Call this to specify the hook for exec_file_command to call back.
226 This is called from the x-window display code. */
229 specify_exec_file_hook (hook)
230 void (*hook) PARAMS ((char *));
232 exec_file_display_hook = hook;
235 /* The exec file must be closed before running an inferior.
236 If it is needed again after the inferior dies, it must
244 bfd_tempclose (exec_bfd);
253 bfd_reopen (exec_bfd);
257 /* If we have both a core file and an exec file,
258 print a warning if they don't go together. */
263 if (exec_bfd && core_bfd)
265 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
266 warning ("core file may not match specified executable file.");
267 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
268 warning ("exec file is newer than core file.");
272 /* Return the name of the executable file as a string.
273 ERR nonzero means get error if there is none specified;
274 otherwise return 0 in that case. */
280 if (exec_bfd) return bfd_get_filename(exec_bfd);
281 if (!err) return NULL;
283 error ("No executable file specified.\n\
284 Use the \"file\" or \"exec-file\" command.");
290 struct target_ops *t;
292 print_section_info (t, core_bfd);
295 /* Report a memory error with error(). */
298 memory_error (status, memaddr)
305 /* Actually, address between memaddr and memaddr + len
306 was out of bounds. */
307 error ("Cannot access memory at address %s.", local_hex_string(memaddr));
311 error ("Error accessing memory address %s: %s.",
312 local_hex_string (memaddr), safe_strerror (status));
316 /* Same as target_read_memory, but report an error if can't read. */
318 read_memory (memaddr, myaddr, len)
324 status = target_read_memory (memaddr, myaddr, len);
326 memory_error (status, memaddr);
329 /* Same as target_write_memory, but report an error if can't write. */
331 write_memory (memaddr, myaddr, len)
338 status = target_write_memory (memaddr, myaddr, len);
340 memory_error (status, memaddr);
343 /* Read an integer from debugged memory, given address and number of bytes. */
346 read_memory_integer (memaddr, len)
355 if (len == sizeof (char))
357 read_memory (memaddr, &cbuf, len);
360 if (len == sizeof (short))
362 read_memory (memaddr, (char *)&sbuf, len);
363 SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
366 if (len == sizeof (int))
368 read_memory (memaddr, (char *)&ibuf, len);
369 SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
372 if (len == sizeof (lbuf))
374 read_memory (memaddr, (char *)&lbuf, len);
375 SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
378 error ("Cannot handle integers of %d bytes.", len);
379 return -1; /* for lint */
382 /* Get the registers out of a core file. This is the machine-
383 independent part. Fetch_core_registers is the machine-dependent
384 part, typically implemented in the xm-file for each architecture. */
386 /* We just get all the registers, so we don't use regno. */
389 get_core_registers (regno)
396 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
397 if (!reg_sec) goto cant;
398 size = bfd_section_size (core_bfd, reg_sec);
399 the_regs = alloca (size);
400 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
402 fetch_core_registers (the_regs, size, 0,
403 (unsigned) bfd_section_vma (abfd,reg_sec));
408 fprintf_filtered (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, (file_ptr)0,
420 fetch_core_registers (the_regs, size, 2,
421 (unsigned) bfd_section_vma (abfd,reg_sec));
425 fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n",
426 bfd_errmsg (bfd_error));
432 struct target_ops core_ops = {
433 "core", "Local core dump file",
434 "Use a core file as a target. Specify the filename of the core file.",
435 core_open, core_close,
436 find_default_attach, core_detach, 0, 0, /* resume, wait */
438 0, 0, /* store_regs, prepare_to_store */
439 xfer_memory, core_files_info,
440 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
441 0, 0, 0, 0, 0, /* terminal stuff */
442 0, 0, 0, /* kill, load, lookup sym */
443 find_default_create_inferior, 0, /* mourn_inferior */
445 core_stratum, 0, /* next */
446 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
447 0, 0, /* section pointers */
448 OPS_MAGIC, /* Always the last thing */
455 add_com ("core-file", class_files, core_file_command,
456 "Use FILE as core dump for examining memory and registers.\n\
457 No arg means have no core file. This command has been superseded by the\n\
458 `target core' and `detach' commands.");
459 add_target (&core_ops);