1 /* Core dump and executable file functions below target vector, 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 */
33 core_files_info PARAMS ((struct target_ops *));
37 solib_add_stub PARAMS ((char *));
41 core_close PARAMS ((int));
44 get_core_registers PARAMS ((int));
46 /* Discard all vestiges of any previous core file
47 and mark data and stack spaces as empty. */
55 free (bfd_get_filename (core_bfd));
61 if (core_ops.to_sections) {
62 free ((PTR)core_ops.to_sections);
63 core_ops.to_sections = NULL;
64 core_ops.to_sections_end = NULL;
70 /* Stub function for catch_errors around shared library hacking. */
73 solib_add_stub (from_tty)
76 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
79 #endif /* SOLIB_ADD */
81 /* This routine opens and sets up the core file bfd */
84 core_open (filename, from_tty)
90 struct cleanup *old_chain;
96 target_preopen (from_tty);
100 "No core file specified. (Use `detach' to stop debugging a core file.)"
101 : "No core file specified.");
104 filename = tilde_expand (filename);
105 if (filename[0] != '/') {
106 temp = concat (current_directory, "/", filename, NULL);
111 old_chain = make_cleanup (free, filename);
113 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
114 if (scratch_chan < 0)
115 perror_with_name (filename);
117 temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
118 if (temp_bfd == NULL)
120 perror_with_name (filename);
123 if (!bfd_check_format (temp_bfd, bfd_core))
125 /* Do it after the err msg */
126 make_cleanup (bfd_close, temp_bfd);
127 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
130 /* Looks semi-reasonable. Toss the old core file and work on the new. */
132 discard_cleanups (old_chain); /* Don't free filename any more */
133 unpush_target (&core_ops);
135 old_chain = make_cleanup (core_close, core_bfd);
139 /* Find the data section */
140 if (build_section_table (core_bfd, &core_ops.to_sections,
141 &core_ops.to_sections_end))
142 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
143 bfd_errmsg (bfd_error));
145 ontop = !push_target (&core_ops);
146 discard_cleanups (old_chain);
148 p = bfd_core_file_failing_command (core_bfd);
150 printf_filtered ("Core was generated by `%s'.\n", p);
152 siggy = bfd_core_file_failing_signal (core_bfd);
154 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
155 safe_strsignal (siggy));
158 /* Fetch all registers from core file */
159 target_fetch_registers (-1);
161 /* Add symbols and section mappings for any shared libraries */
163 catch_errors (solib_add_stub, (char *)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_stack_frame (selected_frame, selected_frame_level, 1);
173 "you won't be able to access this core file until you terminate\n\
174 your %s; do ``info files''", current_target->to_longname);
179 core_detach (args, from_tty)
184 error ("Too many arguments");
185 unpush_target (&core_ops);
187 printf_filtered ("No core file now.\n");
190 /* Get the registers out of a core file. This is the machine-
191 independent part. Fetch_core_registers is the machine-dependent
192 part, typically implemented in the xm-file for each architecture. */
194 /* We just get all the registers, so we don't use regno. */
197 get_core_registers (regno)
204 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
205 if (!reg_sec) goto cant;
206 size = bfd_section_size (core_bfd, reg_sec);
207 the_regs = alloca (size);
208 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
210 fetch_core_registers (the_regs, size, 0,
211 (unsigned) bfd_section_vma (abfd,reg_sec));
216 fprintf_filtered (stderr, "Couldn't fetch registers from core file: %s\n",
217 bfd_errmsg (bfd_error));
220 /* Now do it again for the float registers, if they exist. */
221 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
223 size = bfd_section_size (core_bfd, reg_sec);
224 the_regs = alloca (size);
225 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
228 fetch_core_registers (the_regs, size, 2,
229 (unsigned) bfd_section_vma (abfd,reg_sec));
233 fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n",
234 bfd_errmsg (bfd_error));
242 struct target_ops *t;
244 print_section_info (t, core_bfd);
247 struct target_ops core_ops = {
248 "core", "Local core dump file",
249 "Use a core file as a target. Specify the filename of the core file.",
250 core_open, core_close,
251 find_default_attach, core_detach, 0, 0, /* resume, wait */
253 0, 0, /* store_regs, prepare_to_store */
254 xfer_memory, core_files_info,
255 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
256 0, 0, 0, 0, 0, /* terminal stuff */
257 0, 0, 0, /* kill, load, lookup sym */
258 find_default_create_inferior, 0, /* mourn_inferior */
260 0, /* notice_signals */
261 core_stratum, 0, /* next */
262 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
263 0, 0, /* section pointers */
264 OPS_MAGIC, /* Always the last thing */
268 _initialize_corelow()
270 add_target (&core_ops);