* Makefile.in, breakpoint.c, corelow.c, fork-child.c, inflow.c,
[external/binutils.git] / gdb / corelow.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include <errno.h>
24 #include <signal.h>
25 #include <fcntl.h>
26 #include "frame.h"  /* required by inferior.h */
27 #include "inferior.h"
28 #include "symtab.h"
29 #include "command.h"
30 #include "bfd.h"
31 #include "target.h"
32 #include "gdbcore.h"
33 #include "gdbthread.h"
34
35 /* List of all available core_fns.  On gdb startup, each core file register
36    reader calls add_core_fns() to register information on each core format it
37    is prepared to read. */
38
39 static struct core_fns *core_file_fns = NULL;
40
41 static void core_files_info PARAMS ((struct target_ops *));
42
43 #ifdef SOLIB_ADD
44 static int solib_add_stub PARAMS ((char *));
45 #endif
46
47 static void core_open PARAMS ((char *, int));
48
49 static void core_detach PARAMS ((char *, int));
50
51 static void core_close PARAMS ((int));
52
53 static void get_core_registers PARAMS ((int));
54
55 /* Link a new core_fns into the global core_file_fns list.  Called on gdb
56    startup by the _initialize routine in each core file register reader, to
57    register information about each format the the reader is prepared to
58    handle. */
59
60 void
61 add_core_fns (cf)
62      struct core_fns *cf;
63 {
64   cf -> next = core_file_fns;
65   core_file_fns = cf;
66 }
67
68
69 /* Discard all vestiges of any previous core file and mark data and stack
70    spaces as empty.  */
71
72 /* ARGSUSED */
73 static void
74 core_close (quitting)
75      int quitting;
76 {
77   char *name;
78
79   inferior_pid = 0;             /* Avoid confusion from thread stuff */
80
81   if (core_bfd)
82     {
83       name = bfd_get_filename (core_bfd);
84       if (!bfd_close (core_bfd))
85         warning ("cannot close \"%s\": %s",
86                  name, bfd_errmsg (bfd_get_error ()));
87       free (name);
88       core_bfd = NULL;
89 #ifdef CLEAR_SOLIB
90       CLEAR_SOLIB ();
91 #endif
92       if (core_ops.to_sections)
93         {
94           free ((PTR)core_ops.to_sections);
95           core_ops.to_sections = NULL;
96           core_ops.to_sections_end = NULL;
97         }
98     }
99 }
100
101 #ifdef SOLIB_ADD
102 /* Stub function for catch_errors around shared library hacking.  FROM_TTYP
103    is really an int * which points to from_tty.  */
104
105 static int 
106 solib_add_stub (from_ttyp)
107      char *from_ttyp;
108 {
109   SOLIB_ADD (NULL, *(int *)from_ttyp, &current_target);
110   re_enable_breakpoints_in_shlibs ();
111   return 0;
112 }
113 #endif /* SOLIB_ADD */
114
115 /* Look for sections whose names start with `.reg/' so that we can extract the
116    list of threads in a core file.  */
117
118 static void
119 add_to_thread_list (abfd, asect, reg_sect_arg)
120      bfd *abfd;
121      asection *asect;
122      PTR reg_sect_arg;
123 {
124   int thread_id;
125   asection *reg_sect = (asection *) reg_sect_arg;
126
127   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
128     return;
129
130   thread_id = atoi (bfd_section_name (abfd, asect) + 5);
131
132   add_thread (thread_id);
133
134 /* Warning, Will Robinson, looking at BFD private data! */
135
136   if (reg_sect != NULL
137       && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
138     inferior_pid = thread_id;   /* Yes, make it current */
139 }
140
141 /* This routine opens and sets up the core file bfd.  */
142
143 static void
144 core_open (filename, from_tty)
145      char *filename;
146      int from_tty;
147 {
148   const char *p;
149   int siggy;
150   struct cleanup *old_chain;
151   char *temp;
152   bfd *temp_bfd;
153   int ontop;
154   int scratch_chan;
155
156   target_preopen (from_tty);
157   if (!filename)
158     {
159       error (core_bfd ? 
160        "No core file specified.  (Use `detach' to stop debugging a core file.)"
161      : "No core file specified.");
162     }
163
164   filename = tilde_expand (filename);
165   if (filename[0] != '/')
166     {
167       temp = concat (current_directory, "/", filename, NULL);
168       free (filename);
169       filename = temp;
170     }
171
172   old_chain = make_cleanup (free, filename);
173
174   scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0);
175   if (scratch_chan < 0)
176     perror_with_name (filename);
177
178   temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
179   if (temp_bfd == NULL)
180     perror_with_name (filename);
181
182   if (!bfd_check_format (temp_bfd, bfd_core))
183     {
184       /* Do it after the err msg */
185       /* FIXME: should be checking for errors from bfd_close (for one thing,
186          on error it does not free all the storage associated with the
187          bfd).  */
188       make_cleanup (bfd_close, temp_bfd);
189       error ("\"%s\" is not a core dump: %s",
190              filename, bfd_errmsg (bfd_get_error ()));
191     }
192
193   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
194
195   discard_cleanups (old_chain);         /* Don't free filename any more */
196   unpush_target (&core_ops);
197   core_bfd = temp_bfd;
198   old_chain = make_cleanup (core_close, core_bfd);
199
200   validate_files ();
201
202   /* Find the data section */
203   if (build_section_table (core_bfd, &core_ops.to_sections,
204                            &core_ops.to_sections_end))
205     error ("\"%s\": Can't find sections: %s",
206            bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
207
208   ontop = !push_target (&core_ops);
209   discard_cleanups (old_chain);
210
211   p = bfd_core_file_failing_command (core_bfd);
212   if (p)
213     printf_filtered ("Core was generated by `%s'.\n", p);
214
215   siggy = bfd_core_file_failing_signal (core_bfd);
216   if (siggy > 0)
217     printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
218                      safe_strsignal (siggy));
219
220   /* Build up thread list from BFD sections. */
221
222   init_thread_list ();
223   bfd_map_over_sections (core_bfd, add_to_thread_list,
224                          bfd_get_section_by_name (core_bfd, ".reg"));
225
226   if (ontop)
227     {
228       /* Fetch all registers from core file.  */
229       target_fetch_registers (-1);
230
231       /* Add symbols and section mappings for any shared libraries.  */
232 #ifdef SOLIB_ADD
233       catch_errors (solib_add_stub, &from_tty, (char *)0,
234                     RETURN_MASK_ALL);
235 #endif
236
237       /* Now, set up the frame cache, and print the top of stack.  */
238       flush_cached_frames ();
239       select_frame (get_current_frame (), 0);
240       print_stack_frame (selected_frame, selected_frame_level, 1);
241     }
242   else
243     {
244       warning (
245 "you won't be able to access this core file until you terminate\n\
246 your %s; do ``info files''", target_longname);
247     }
248 }
249
250 static void
251 core_detach (args, from_tty)
252      char *args;
253      int from_tty;
254 {
255   if (args)
256     error ("Too many arguments");
257   unpush_target (&core_ops);
258   reinit_frame_cache ();
259   if (from_tty)
260     printf_filtered ("No core file now.\n");
261 }
262
263 /* Get the registers out of a core file.  This is the machine-
264    independent part.  Fetch_core_registers is the machine-dependent
265    part, typically implemented in the xm-file for each architecture.  */
266
267 /* We just get all the registers, so we don't use regno.  */
268
269 /* ARGSUSED */
270 static void
271 get_core_registers (regno)
272      int regno;
273 {
274   sec_ptr reg_sec;
275   unsigned size;
276   char *the_regs;
277   char secname[10];
278   enum bfd_flavour our_flavour = bfd_get_flavour (core_bfd);
279   struct core_fns *cf;
280
281   if (core_file_fns == NULL)
282     {
283       fprintf_filtered (gdb_stderr,
284                         "Can't fetch registers from this type of core file\n");
285       return;
286     }
287
288   /* Thread support.  If inferior_pid is non-zero, then we have found a core
289      file with threads (or multiple processes).  In that case, we need to
290      use the appropriate register section, else we just use `.reg'. */
291
292   /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
293
294   if (inferior_pid)
295     sprintf (secname, ".reg/%d", inferior_pid);
296   else
297     strcpy (secname, ".reg");
298
299   reg_sec = bfd_get_section_by_name (core_bfd, secname);
300   if (!reg_sec)
301     goto cant;
302   size = bfd_section_size (core_bfd, reg_sec);
303   the_regs = alloca (size);
304   /* Look for the core functions that match this flavor.  Default to the
305      first one if nothing matches. */
306   for (cf = core_file_fns; cf != NULL; cf = cf -> next)
307     {
308       if (our_flavour == cf -> core_flavour)
309         {
310           break;
311         }
312     }
313   if (cf == NULL)
314     {
315       cf = core_file_fns;
316     }
317   if (cf != NULL &&
318       bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size) &&
319       cf -> core_read_registers != NULL)
320     {
321       (cf -> core_read_registers (the_regs, size, 0,
322                                   (unsigned) bfd_section_vma (abfd,reg_sec)));
323     }
324   else
325     {
326 cant:
327       fprintf_filtered (gdb_stderr,
328                         "Couldn't fetch registers from core file: %s\n",
329                         bfd_errmsg (bfd_get_error ()));
330     }
331
332   /* Now do it again for the float registers, if they exist.  */
333   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
334   if (reg_sec)
335     {
336       size = bfd_section_size (core_bfd, reg_sec);
337       the_regs = alloca (size);
338       if (cf != NULL &&
339           bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size) &&
340           cf -> core_read_registers != NULL)
341         {
342           (cf -> core_read_registers (the_regs, size, 2,
343                                       (unsigned) bfd_section_vma (abfd,reg_sec)));
344         }
345       else
346         {
347           fprintf_filtered (gdb_stderr, 
348                             "Couldn't fetch register set 2 from core file: %s\n",
349                             bfd_errmsg (bfd_get_error ()));
350         }
351     }
352   registers_fetched ();
353 }
354
355 static void
356 core_files_info (t)
357   struct target_ops *t;
358 {
359   print_section_info (t, core_bfd);
360 }
361 \f
362 /* If mourn is being called in all the right places, this could be say
363    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
364
365 static int
366 ignore (addr, contents)
367      CORE_ADDR addr;
368      char *contents;
369 {
370   return 0;
371 }
372
373 struct target_ops core_ops = {
374   "core",                       /* to_shortname */
375   "Local core dump file",       /* to_longname */
376   "Use a core file as a target.  Specify the filename of the core file.", /* to_doc */
377   core_open,                    /* to_open */
378   core_close,                   /* to_close */
379   find_default_attach,          /* to_attach */
380   core_detach,                  /* to_detach */
381   0,                            /* to_resume */
382   0,                            /* to_wait */
383   get_core_registers,           /* to_fetch_registers */
384   0,                            /* to_store_registers */
385   0,                            /* to_prepare_to_store */
386   xfer_memory,                  /* to_xfer_memory */
387   core_files_info,              /* to_files_info */
388   ignore,                       /* to_insert_breakpoint */
389   ignore,                       /* to_remove_breakpoint */
390   0,                            /* to_terminal_init */
391   0,                            /* to_terminal_inferior */
392   0,                            /* to_terminal_ours_for_output */
393   0,                            /* to_terminal_ours */
394   0,                            /* to_terminal_info */
395   0,                            /* to_kill */
396   0,                            /* to_load */
397   0,                            /* to_lookup_symbol */
398   find_default_create_inferior, /* to_create_inferior */
399   0,                            /* to_mourn_inferior */
400   0,                            /* to_can_run */
401   0,                            /* to_notice_signals */
402   0,                            /* to_thread_alive */
403   0,                            /* to_stop */
404   core_stratum,                 /* to_stratum */
405   0,                            /* to_next */
406   0,                            /* to_has_all_memory */
407   1,                            /* to_has_memory */
408   1,                            /* to_has_stack */
409   1,                            /* to_has_registers */
410   0,                            /* to_has_execution */
411   0,                            /* to_sections */
412   0,                            /* to_sections_end */
413   OPS_MAGIC,                    /* to_magic */
414 };
415
416 void
417 _initialize_corelow()
418 {
419   add_target (&core_ops);
420 }