1 /* Core dump and executable file functions below target vector, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
26 #include "frame.h" /* required by inferior.h */
35 static void core_files_info PARAMS ((struct target_ops *));
38 static int solib_add_stub PARAMS ((char *));
41 static void core_open PARAMS ((char *, int));
43 static void core_detach PARAMS ((char *, int));
45 static void core_close PARAMS ((int));
47 static void get_core_registers PARAMS ((int));
49 /* Discard all vestiges of any previous core file and mark data and stack
59 inferior_pid = 0; /* Avoid confusion from thread stuff */
63 name = bfd_get_filename (core_bfd);
64 if (!bfd_close (core_bfd))
65 warning ("cannot close \"%s\": %s",
66 name, bfd_errmsg (bfd_get_error ()));
72 if (core_ops.to_sections)
74 free ((PTR)core_ops.to_sections);
75 core_ops.to_sections = NULL;
76 core_ops.to_sections_end = NULL;
82 /* Stub function for catch_errors around shared library hacking. FROM_TTYP
83 is really an int * which points to from_tty. */
86 solib_add_stub (from_ttyp)
89 SOLIB_ADD (NULL, *(int *)from_ttyp, ¤t_target);
92 #endif /* SOLIB_ADD */
94 /* Look for sections whose names start with `.reg/' so that we can extract the
95 list of threads in a core file. */
98 add_to_thread_list (abfd, asect, reg_sect_arg)
104 asection *reg_sect = (asection *) reg_sect_arg;
106 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
109 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
111 add_thread (thread_id);
113 /* Warning, Will Robinson, looking at BFD private data! */
115 if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
116 inferior_pid = thread_id; /* Yes, make it current */
119 /* This routine opens and sets up the core file bfd. */
122 core_open (filename, from_tty)
128 struct cleanup *old_chain;
134 target_preopen (from_tty);
138 "No core file specified. (Use `detach' to stop debugging a core file.)"
139 : "No core file specified.");
142 filename = tilde_expand (filename);
143 if (filename[0] != '/')
145 temp = concat (current_directory, "/", filename, NULL);
150 old_chain = make_cleanup (free, filename);
152 scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0);
153 if (scratch_chan < 0)
154 perror_with_name (filename);
156 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
157 if (temp_bfd == NULL)
158 perror_with_name (filename);
160 if (!bfd_check_format (temp_bfd, bfd_core))
162 /* Do it after the err msg */
163 /* FIXME: should be checking for errors from bfd_close (for one thing,
164 on error it does not free all the storage associated with the
166 make_cleanup (bfd_close, temp_bfd);
167 error ("\"%s\" is not a core dump: %s",
168 filename, bfd_errmsg (bfd_get_error ()));
171 /* Looks semi-reasonable. Toss the old core file and work on the new. */
173 discard_cleanups (old_chain); /* Don't free filename any more */
174 unpush_target (&core_ops);
176 old_chain = make_cleanup (core_close, core_bfd);
180 /* Find the data section */
181 if (build_section_table (core_bfd, &core_ops.to_sections,
182 &core_ops.to_sections_end))
183 error ("\"%s\": Can't find sections: %s",
184 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
186 ontop = !push_target (&core_ops);
187 discard_cleanups (old_chain);
189 p = bfd_core_file_failing_command (core_bfd);
191 printf_filtered ("Core was generated by `%s'.\n", p);
193 siggy = bfd_core_file_failing_signal (core_bfd);
195 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
196 safe_strsignal (siggy));
198 /* Build up thread list from BFD sections. */
201 bfd_map_over_sections (core_bfd, add_to_thread_list,
202 bfd_get_section_by_name (core_bfd, ".reg"));
206 /* Fetch all registers from core file. */
207 target_fetch_registers (-1);
209 /* Add symbols and section mappings for any shared libraries. */
211 catch_errors (solib_add_stub, &from_tty, (char *)0,
214 /* solib_add_stub usually modifies current_target.to_sections, which
215 has to be reflected in core_ops to enable proper freeing of
216 the to_sections vector in core_close and correct section
217 mapping in xfer_memory and core_files_info. */
218 core_ops.to_sections = current_target.to_sections;
219 core_ops.to_sections_end = current_target.to_sections_end;
222 /* Now, set up the frame cache, and print the top of stack. */
223 flush_cached_frames ();
224 select_frame (get_current_frame (), 0);
225 print_stack_frame (selected_frame, selected_frame_level, 1);
230 "you won't be able to access this core file until you terminate\n\
231 your %s; do ``info files''", target_longname);
236 core_detach (args, from_tty)
241 error ("Too many arguments");
242 unpush_target (&core_ops);
243 reinit_frame_cache ();
245 printf_filtered ("No core file now.\n");
248 /* Get the registers out of a core file. This is the machine-
249 independent part. Fetch_core_registers is the machine-dependent
250 part, typically implemented in the xm-file for each architecture. */
252 /* We just get all the registers, so we don't use regno. */
256 get_core_registers (regno)
264 /* Thread support. If inferior_pid is non-zero, then we have found a core
265 file with threads (or multiple processes). In that case, we need to
266 use the appropriate register section, else we just use `.reg'. */
268 /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
271 sprintf (secname, ".reg/%d", inferior_pid);
273 strcpy (secname, ".reg");
275 reg_sec = bfd_get_section_by_name (core_bfd, secname);
278 size = bfd_section_size (core_bfd, reg_sec);
279 the_regs = alloca (size);
280 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
282 fetch_core_registers (the_regs, size, 0,
283 (unsigned) bfd_section_vma (abfd,reg_sec));
288 fprintf_filtered (gdb_stderr,
289 "Couldn't fetch registers from core file: %s\n",
290 bfd_errmsg (bfd_get_error ()));
293 /* Now do it again for the float registers, if they exist. */
294 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
297 size = bfd_section_size (core_bfd, reg_sec);
298 the_regs = alloca (size);
299 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
302 fetch_core_registers (the_regs, size, 2,
303 (unsigned) bfd_section_vma (abfd,reg_sec));
307 fprintf_filtered (gdb_stderr,
308 "Couldn't fetch register set 2 from core file: %s\n",
309 bfd_errmsg (bfd_get_error ()));
312 registers_fetched ();
317 struct target_ops *t;
319 print_section_info (t, core_bfd);
322 /* If mourn is being called in all the right places, this could be say
323 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
326 ignore (addr, contents)
333 struct target_ops core_ops = {
334 "core", /* to_shortname */
335 "Local core dump file", /* to_longname */
336 "Use a core file as a target. Specify the filename of the core file.", /* to_doc */
337 core_open, /* to_open */
338 core_close, /* to_close */
339 find_default_attach, /* to_attach */
340 core_detach, /* to_detach */
343 get_core_registers, /* to_fetch_registers */
344 0, /* to_store_registers */
345 0, /* to_prepare_to_store */
346 xfer_memory, /* to_xfer_memory */
347 core_files_info, /* to_files_info */
348 ignore, /* to_insert_breakpoint */
349 ignore, /* to_remove_breakpoint */
350 0, /* to_terminal_init */
351 0, /* to_terminal_inferior */
352 0, /* to_terminal_ours_for_output */
353 0, /* to_terminal_ours */
354 0, /* to_terminal_info */
357 0, /* to_lookup_symbol */
358 find_default_create_inferior, /* to_create_inferior */
359 0, /* to_mourn_inferior */
361 0, /* to_notice_signals */
362 0, /* to_thread_alive */
364 core_stratum, /* to_stratum */
366 0, /* to_has_all_memory */
367 1, /* to_has_memory */
368 1, /* to_has_stack */
369 1, /* to_has_registers */
370 0, /* to_has_execution */
372 0, /* to_sections_end */
373 OPS_MAGIC, /* to_magic */
377 _initialize_corelow()
379 add_target (&core_ops);