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,
167 /* Now, set up the frame cache, and print the top of stack */
168 set_current_frame (create_new_frame (read_fp (),
170 select_frame (get_current_frame (), 0);
171 print_stack_frame (selected_frame
, selected_frame_level
, 1);
174 "you won't be able to access this core file until you terminate\n\
175 your %s; do ``info files''", current_target
->to_longname
);
180 core_detach (args
, from_tty
)
185 error ("Too many arguments");
186 unpush_target (&core_ops
);
188 printf_filtered ("No core file now.\n");
191 /* Get the registers out of a core file. This is the machine-
192 independent part. Fetch_core_registers is the machine-dependent
193 part, typically implemented in the xm-file for each architecture. */
195 /* We just get all the registers, so we don't use regno. */
198 get_core_registers (regno
)
205 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg");
206 if (!reg_sec
) goto cant
;
207 size
= bfd_section_size (core_bfd
, reg_sec
);
208 the_regs
= alloca (size
);
209 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
, (file_ptr
)0, size
))
211 fetch_core_registers (the_regs
, size
, 0,
212 (unsigned) bfd_section_vma (abfd
,reg_sec
));
217 fprintf_filtered (stderr
, "Couldn't fetch registers from core file: %s\n",
218 bfd_errmsg (bfd_error
));
221 /* Now do it again for the float registers, if they exist. */
222 reg_sec
= bfd_get_section_by_name (core_bfd
, ".reg2");
224 size
= bfd_section_size (core_bfd
, reg_sec
);
225 the_regs
= alloca (size
);
226 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
, (file_ptr
)0,
229 fetch_core_registers (the_regs
, size
, 2,
230 (unsigned) bfd_section_vma (abfd
,reg_sec
));
234 fprintf_filtered (stderr
, "Couldn't fetch register set 2 from core file: %s\n",
235 bfd_errmsg (bfd_error
));
243 struct target_ops
*t
;
245 print_section_info (t
, core_bfd
);
248 struct target_ops core_ops
= {
249 "core", "Local core dump file",
250 "Use a core file as a target. Specify the filename of the core file.",
251 core_open
, core_close
,
252 find_default_attach
, core_detach
, 0, 0, /* resume, wait */
254 0, 0, /* store_regs, prepare_to_store */
255 xfer_memory
, core_files_info
,
256 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
257 0, 0, 0, 0, 0, /* terminal stuff */
258 0, 0, 0, /* kill, load, lookup sym */
259 find_default_create_inferior
, 0, /* mourn_inferior */
261 0, /* notice_signals */
262 core_stratum
, 0, /* next */
263 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
264 0, 0, /* section pointers */
265 OPS_MAGIC
, /* Always the last thing */
269 _initialize_corelow()
271 add_target (&core_ops
);