1 /* Core dump and executable file functions below target vector, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "frame.h" /* required by inferior.h */
36 core_files_info PARAMS ((struct target_ops *));
40 solib_add_stub PARAMS ((char *));
44 core_close PARAMS ((int));
47 get_core_registers PARAMS ((int));
49 /* Discard all vestiges of any previous core file
50 and mark data and stack spaces as empty. */
57 inferior_pid = 0; /* Avoid confusion from thread stuff */
60 free (bfd_get_filename (core_bfd));
66 if (core_ops.to_sections) {
67 free ((PTR)core_ops.to_sections);
68 core_ops.to_sections = NULL;
69 core_ops.to_sections_end = NULL;
75 /* Stub function for catch_errors around shared library hacking. FROM_TTYP
76 is really an int * which points to from_tty. */
79 solib_add_stub (from_ttyp)
82 SOLIB_ADD (NULL, *(int *)from_ttyp, ¤t_target);
85 #endif /* SOLIB_ADD */
87 /* Look for sections whose names start with `.reg/' so that we can extract the
88 list of threads in a core file. */
91 add_to_thread_list (abfd, asect, reg_sect_arg)
97 asection *reg_sect = (asection *) reg_sect_arg;
99 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
102 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
104 add_thread (thread_id);
106 /* Warning, Will Robinson, looking at BFD private data! */
108 if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
109 inferior_pid = thread_id; /* Yes, make it current */
112 /* This routine opens and sets up the core file bfd */
115 core_open (filename, from_tty)
121 struct cleanup *old_chain;
127 target_preopen (from_tty);
131 "No core file specified. (Use `detach' to stop debugging a core file.)"
132 : "No core file specified.");
135 filename = tilde_expand (filename);
136 if (filename[0] != '/') {
137 temp = concat (current_directory, "/", filename, NULL);
142 old_chain = make_cleanup (free, filename);
144 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
145 if (scratch_chan < 0)
146 perror_with_name (filename);
148 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
149 if (temp_bfd == NULL)
151 perror_with_name (filename);
154 if (!bfd_check_format (temp_bfd, bfd_core))
156 /* Do it after the err msg */
157 make_cleanup (bfd_close, temp_bfd);
158 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_get_error ()));
161 /* Looks semi-reasonable. Toss the old core file and work on the new. */
163 discard_cleanups (old_chain); /* Don't free filename any more */
164 unpush_target (&core_ops);
166 old_chain = make_cleanup (core_close, core_bfd);
170 /* Find the data section */
171 if (build_section_table (core_bfd, &core_ops.to_sections,
172 &core_ops.to_sections_end))
173 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
174 bfd_errmsg (bfd_get_error ()));
176 ontop = !push_target (&core_ops);
177 discard_cleanups (old_chain);
179 p = bfd_core_file_failing_command (core_bfd);
181 printf_filtered ("Core was generated by `%s'.\n", p);
183 siggy = bfd_core_file_failing_signal (core_bfd);
185 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
186 safe_strsignal (siggy));
188 /* Build up thread list from BFD sections. */
191 bfd_map_over_sections (core_bfd, add_to_thread_list,
192 bfd_get_section_by_name (core_bfd, ".reg"));
195 /* Fetch all registers from core file */
196 target_fetch_registers (-1);
198 /* Add symbols and section mappings for any shared libraries */
200 catch_errors (solib_add_stub, &from_tty, (char *)0,
203 /* solib_add_stub usually modifies current_target.to_sections, which
204 has to be reflected in core_ops to enable proper freeing of
205 the to_sections vector in core_close and correct section
206 mapping in xfer_memory and core_files_info. */
207 core_ops.to_sections = current_target.to_sections;
208 core_ops.to_sections_end = current_target.to_sections_end;
211 /* Now, set up the frame cache, and print the top of stack */
212 flush_cached_frames ();
213 select_frame (get_current_frame (), 0);
214 print_stack_frame (selected_frame, selected_frame_level, 1);
217 "you won't be able to access this core file until you terminate\n\
218 your %s; do ``info files''", target_longname);
223 core_detach (args, from_tty)
228 error ("Too many arguments");
229 unpush_target (&core_ops);
230 reinit_frame_cache ();
232 printf_filtered ("No core file now.\n");
235 /* Get the registers out of a core file. This is the machine-
236 independent part. Fetch_core_registers is the machine-dependent
237 part, typically implemented in the xm-file for each architecture. */
239 /* We just get all the registers, so we don't use regno. */
242 get_core_registers (regno)
250 /* Thread support. If inferior_pid is non-zero, then we have found a core
251 file with threads (or multiple processes). In that case, we need to
252 use the appropriate register section, else we just use `.reg'. */
254 /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
257 sprintf (secname, ".reg/%d", inferior_pid);
259 strcpy (secname, ".reg");
261 reg_sec = bfd_get_section_by_name (core_bfd, secname);
262 if (!reg_sec) goto cant;
263 size = bfd_section_size (core_bfd, reg_sec);
264 the_regs = alloca (size);
265 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
267 fetch_core_registers (the_regs, size, 0,
268 (unsigned) bfd_section_vma (abfd,reg_sec));
273 fprintf_filtered (gdb_stderr, "Couldn't fetch registers from core file: %s\n",
274 bfd_errmsg (bfd_get_error ()));
277 /* Now do it again for the float registers, if they exist. */
278 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
280 size = bfd_section_size (core_bfd, reg_sec);
281 the_regs = alloca (size);
282 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
285 fetch_core_registers (the_regs, size, 2,
286 (unsigned) bfd_section_vma (abfd,reg_sec));
290 fprintf_filtered (gdb_stderr, "Couldn't fetch register set 2 from core file: %s\n",
291 bfd_errmsg (bfd_get_error ()));
299 struct target_ops *t;
301 print_section_info (t, core_bfd);
304 /* If mourn is being called in all the right places, this could be say
305 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
308 ignore (addr, contents)
315 struct target_ops core_ops = {
316 "core", "Local core dump file",
317 "Use a core file as a target. Specify the filename of the core file.",
318 core_open, core_close,
319 find_default_attach, core_detach, 0, 0, /* resume, wait */
321 0, 0, /* store_regs, prepare_to_store */
322 xfer_memory, core_files_info,
323 ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */
324 0, 0, 0, 0, 0, /* terminal stuff */
325 0, 0, 0, /* kill, load, lookup sym */
326 find_default_create_inferior, 0, /* mourn_inferior */
328 0, /* notice_signals */
329 core_stratum, 0, /* next */
330 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
331 0, 0, /* section pointers */
332 OPS_MAGIC, /* Always the last thing */
336 _initialize_corelow()
338 add_target (&core_ops);