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 */
35 static void core_files_info PARAMS ((struct target_ops *));
38 static int solib_add_stub PARAMS ((char *));
41 static void core_close PARAMS ((int));
43 static void get_core_registers PARAMS ((int));
45 /* Discard all vestiges of any previous core file and mark data and stack
55 inferior_pid = 0; /* Avoid confusion from thread stuff */
59 name = bfd_get_filename (core_bfd);
60 if (!bfd_close (core_bfd))
61 warning ("cannot close \"%s\": %s",
62 name, bfd_errmsg (bfd_get_error ()));
68 if (core_ops.to_sections)
70 free ((PTR)core_ops.to_sections);
71 core_ops.to_sections = NULL;
72 core_ops.to_sections_end = NULL;
78 /* Stub function for catch_errors around shared library hacking. FROM_TTYP
79 is really an int * which points to from_tty. */
82 solib_add_stub (from_ttyp)
85 SOLIB_ADD (NULL, *(int *)from_ttyp, ¤t_target);
88 #endif /* SOLIB_ADD */
90 /* Look for sections whose names start with `.reg/' so that we can extract the
91 list of threads in a core file. */
94 add_to_thread_list (abfd, asect, reg_sect_arg)
100 asection *reg_sect = (asection *) reg_sect_arg;
102 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
105 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
107 add_thread (thread_id);
109 /* Warning, Will Robinson, looking at BFD private data! */
111 if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
112 inferior_pid = thread_id; /* Yes, make it current */
115 /* This routine opens and sets up the core file bfd. */
118 core_open (filename, from_tty)
124 struct cleanup *old_chain;
130 target_preopen (from_tty);
134 "No core file specified. (Use `detach' to stop debugging a core file.)"
135 : "No core file specified.");
138 filename = tilde_expand (filename);
139 if (filename[0] != '/')
141 temp = concat (current_directory, "/", filename, NULL);
146 old_chain = make_cleanup (free, filename);
148 scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0);
149 if (scratch_chan < 0)
150 perror_with_name (filename);
152 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
153 if (temp_bfd == NULL)
154 perror_with_name (filename);
156 if (!bfd_check_format (temp_bfd, bfd_core))
158 /* Do it after the err msg */
159 /* FIXME: should be checking for errors from bfd_close (for one thing,
160 on error it does not free all the storage associated with the
162 make_cleanup (bfd_close, temp_bfd);
163 error ("\"%s\" is not a core dump: %s",
164 filename, bfd_errmsg (bfd_get_error ()));
167 /* Looks semi-reasonable. Toss the old core file and work on the new. */
169 discard_cleanups (old_chain); /* Don't free filename any more */
170 unpush_target (&core_ops);
172 old_chain = make_cleanup (core_close, core_bfd);
176 /* Find the data section */
177 if (build_section_table (core_bfd, &core_ops.to_sections,
178 &core_ops.to_sections_end))
179 error ("\"%s\": Can't find sections: %s",
180 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
182 ontop = !push_target (&core_ops);
183 discard_cleanups (old_chain);
185 p = bfd_core_file_failing_command (core_bfd);
187 printf_filtered ("Core was generated by `%s'.\n", p);
189 siggy = bfd_core_file_failing_signal (core_bfd);
191 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
192 safe_strsignal (siggy));
194 /* Build up thread list from BFD sections. */
197 bfd_map_over_sections (core_bfd, add_to_thread_list,
198 bfd_get_section_by_name (core_bfd, ".reg"));
202 /* Fetch all registers from core file. */
203 target_fetch_registers (-1);
205 /* Add symbols and section mappings for any shared libraries. */
207 catch_errors (solib_add_stub, &from_tty, (char *)0,
210 /* solib_add_stub usually modifies current_target.to_sections, which
211 has to be reflected in core_ops to enable proper freeing of
212 the to_sections vector in core_close and correct section
213 mapping in xfer_memory and core_files_info. */
214 core_ops.to_sections = current_target.to_sections;
215 core_ops.to_sections_end = current_target.to_sections_end;
218 /* Now, set up the frame cache, and print the top of stack. */
219 flush_cached_frames ();
220 select_frame (get_current_frame (), 0);
221 print_stack_frame (selected_frame, selected_frame_level, 1);
226 "you won't be able to access this core file until you terminate\n\
227 your %s; do ``info files''", target_longname);
232 core_detach (args, from_tty)
237 error ("Too many arguments");
238 unpush_target (&core_ops);
239 reinit_frame_cache ();
241 printf_filtered ("No core file now.\n");
244 /* Get the registers out of a core file. This is the machine-
245 independent part. Fetch_core_registers is the machine-dependent
246 part, typically implemented in the xm-file for each architecture. */
248 /* We just get all the registers, so we don't use regno. */
252 get_core_registers (regno)
260 /* Thread support. If inferior_pid is non-zero, then we have found a core
261 file with threads (or multiple processes). In that case, we need to
262 use the appropriate register section, else we just use `.reg'. */
264 /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
267 sprintf (secname, ".reg/%d", inferior_pid);
269 strcpy (secname, ".reg");
271 reg_sec = bfd_get_section_by_name (core_bfd, secname);
274 size = bfd_section_size (core_bfd, reg_sec);
275 the_regs = alloca (size);
276 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
278 fetch_core_registers (the_regs, size, 0,
279 (unsigned) bfd_section_vma (abfd,reg_sec));
284 fprintf_filtered (gdb_stderr,
285 "Couldn't fetch registers from core file: %s\n",
286 bfd_errmsg (bfd_get_error ()));
289 /* Now do it again for the float registers, if they exist. */
290 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
293 size = bfd_section_size (core_bfd, reg_sec);
294 the_regs = alloca (size);
295 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
298 fetch_core_registers (the_regs, size, 2,
299 (unsigned) bfd_section_vma (abfd,reg_sec));
303 fprintf_filtered (gdb_stderr,
304 "Couldn't fetch register set 2 from core file: %s\n",
305 bfd_errmsg (bfd_get_error ()));
308 registers_fetched ();
313 struct target_ops *t;
315 print_section_info (t, core_bfd);
318 /* If mourn is being called in all the right places, this could be say
319 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
322 ignore (addr, contents)
329 struct target_ops core_ops = {
330 "core", /* to_shortname */
331 "Local core dump file", /* to_longname */
332 "Use a core file as a target. Specify the filename of the core file.", /* to_doc */
333 core_open, /* to_open */
334 core_close, /* to_close */
335 find_default_attach, /* to_attach */
336 core_detach, /* to_detach */
339 get_core_registers, /* to_fetch_registers */
340 0, /* to_store_registers */
341 0, /* to_prepare_to_store */
342 xfer_memory, /* to_xfer_memory */
343 core_files_info, /* to_files_info */
344 ignore, /* to_insert_breakpoint */
345 ignore, /* to_remove_breakpoint */
346 0, /* to_terminal_init */
347 0, /* to_terminal_inferior */
348 0, /* to_terminal_ours_for_output */
349 0, /* to_terminal_ours */
350 0, /* to_terminal_info */
353 0, /* to_lookup_symbol */
354 find_default_create_inferior, /* to_create_inferior */
355 0, /* to_mourn_inferior */
357 0, /* to_notice_signals */
359 core_stratum, /* to_stratum */
361 0, /* to_has_all_memory */
362 1, /* to_has_memory */
363 1, /* to_has_stack */
364 1, /* to_has_registers */
365 0, /* to_has_execution */
367 0, /* to_sections_end */
368 OPS_MAGIC, /* to_magic */
372 _initialize_corelow()
374 add_target (&core_ops);