* corelow.c: Format to standard.
[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
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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "defs.h"
22 #include <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 "thread.h"
34
35 static void core_files_info PARAMS ((struct target_ops *));
36
37 #ifdef SOLIB_ADD
38 static int solib_add_stub PARAMS ((char *));
39 #endif
40
41 static void core_close PARAMS ((int));
42
43 static void get_core_registers PARAMS ((int));
44
45 /* Discard all vestiges of any previous core file and mark data and stack
46    spaces as empty.  */
47
48 /* ARGSUSED */
49 static void
50 core_close (quitting)
51      int quitting;
52 {
53   char *name;
54
55   inferior_pid = 0;             /* Avoid confusion from thread stuff */
56
57   if (core_bfd)
58     {
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 ()));
63       free (name);
64       core_bfd = NULL;
65 #ifdef CLEAR_SOLIB
66       CLEAR_SOLIB ();
67 #endif
68       if (core_ops.to_sections)
69         {
70           free ((PTR)core_ops.to_sections);
71           core_ops.to_sections = NULL;
72           core_ops.to_sections_end = NULL;
73         }
74     }
75 }
76
77 #ifdef SOLIB_ADD
78 /* Stub function for catch_errors around shared library hacking.  FROM_TTYP
79    is really an int * which points to from_tty.  */
80
81 static int 
82 solib_add_stub (from_ttyp)
83      char *from_ttyp;
84 {
85   SOLIB_ADD (NULL, *(int *)from_ttyp, &current_target);
86   return 0;
87 }
88 #endif /* SOLIB_ADD */
89
90 /* Look for sections whose names start with `.reg/' so that we can extract the
91    list of threads in a core file.  */
92
93 static void
94 add_to_thread_list (abfd, asect, reg_sect_arg)
95      bfd *abfd;
96      asection *asect;
97      PTR reg_sect_arg;
98 {
99   int thread_id;
100   asection *reg_sect = (asection *) reg_sect_arg;
101
102   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
103     return;
104
105   thread_id = atoi (bfd_section_name (abfd, asect) + 5);
106
107   add_thread (thread_id);
108
109 /* Warning, Will Robinson, looking at BFD private data! */
110
111   if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
112     inferior_pid = thread_id;   /* Yes, make it current */
113 }
114
115 /* This routine opens and sets up the core file bfd.  */
116
117 void
118 core_open (filename, from_tty)
119      char *filename;
120      int from_tty;
121 {
122   const char *p;
123   int siggy;
124   struct cleanup *old_chain;
125   char *temp;
126   bfd *temp_bfd;
127   int ontop;
128   int scratch_chan;
129
130   target_preopen (from_tty);
131   if (!filename)
132     {
133       error (core_bfd ? 
134        "No core file specified.  (Use `detach' to stop debugging a core file.)"
135      : "No core file specified.");
136     }
137
138   filename = tilde_expand (filename);
139   if (filename[0] != '/')
140     {
141       temp = concat (current_directory, "/", filename, NULL);
142       free (filename);
143       filename = temp;
144     }
145
146   old_chain = make_cleanup (free, filename);
147
148   scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0);
149   if (scratch_chan < 0)
150     perror_with_name (filename);
151
152   temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
153   if (temp_bfd == NULL)
154     perror_with_name (filename);
155
156   if (!bfd_check_format (temp_bfd, bfd_core))
157     {
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
161          bfd).  */
162       make_cleanup (bfd_close, temp_bfd);
163       error ("\"%s\" is not a core dump: %s",
164              filename, bfd_errmsg (bfd_get_error ()));
165     }
166
167   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
168
169   discard_cleanups (old_chain);         /* Don't free filename any more */
170   unpush_target (&core_ops);
171   core_bfd = temp_bfd;
172   old_chain = make_cleanup (core_close, core_bfd);
173
174   validate_files ();
175
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 ()));
181
182   ontop = !push_target (&core_ops);
183   discard_cleanups (old_chain);
184
185   p = bfd_core_file_failing_command (core_bfd);
186   if (p)
187     printf_filtered ("Core was generated by `%s'.\n", p);
188
189   siggy = bfd_core_file_failing_signal (core_bfd);
190   if (siggy > 0)
191     printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
192                      safe_strsignal (siggy));
193
194   /* Build up thread list from BFD sections. */
195
196   init_thread_list ();
197   bfd_map_over_sections (core_bfd, add_to_thread_list,
198                          bfd_get_section_by_name (core_bfd, ".reg"));
199
200   if (ontop)
201     {
202       /* Fetch all registers from core file.  */
203       target_fetch_registers (-1);
204
205       /* Add symbols and section mappings for any shared libraries.  */
206 #ifdef SOLIB_ADD
207       catch_errors (solib_add_stub, &from_tty, (char *)0,
208                     RETURN_MASK_ALL);
209
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;
216 #endif
217
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);
222     }
223   else
224     {
225       warning (
226 "you won't be able to access this core file until you terminate\n\
227 your %s; do ``info files''", target_longname);
228     }
229 }
230
231 void
232 core_detach (args, from_tty)
233      char *args;
234      int from_tty;
235 {
236   if (args)
237     error ("Too many arguments");
238   unpush_target (&core_ops);
239   reinit_frame_cache ();
240   if (from_tty)
241     printf_filtered ("No core file now.\n");
242 }
243
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.  */
247
248 /* We just get all the registers, so we don't use regno.  */
249
250 /* ARGSUSED */
251 static void
252 get_core_registers (regno)
253      int regno;
254 {
255   sec_ptr reg_sec;
256   unsigned size;
257   char *the_regs;
258   char secname[10];
259
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'. */
263
264   /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
265
266   if (inferior_pid)
267     sprintf (secname, ".reg/%d", inferior_pid);
268   else
269     strcpy (secname, ".reg");
270
271   reg_sec = bfd_get_section_by_name (core_bfd, secname);
272   if (!reg_sec)
273     goto cant;
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))
277     {
278       fetch_core_registers (the_regs, size, 0,
279                             (unsigned) bfd_section_vma (abfd,reg_sec));
280     }
281   else
282     {
283 cant:
284       fprintf_filtered (gdb_stderr,
285                         "Couldn't fetch registers from core file: %s\n",
286                         bfd_errmsg (bfd_get_error ()));
287     }
288
289   /* Now do it again for the float registers, if they exist.  */
290   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
291   if (reg_sec)
292     {
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,
296                                     size))
297         {
298           fetch_core_registers (the_regs, size, 2,
299                                 (unsigned) bfd_section_vma (abfd,reg_sec));
300         }
301       else
302         {
303           fprintf_filtered (gdb_stderr, 
304                             "Couldn't fetch register set 2 from core file: %s\n",
305                             bfd_errmsg (bfd_get_error ()));
306         }
307     }
308   registers_fetched ();
309 }
310
311 static void
312 core_files_info (t)
313   struct target_ops *t;
314 {
315   print_section_info (t, core_bfd);
316 }
317 \f
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).  */
320
321 static int
322 ignore (addr, contents)
323      CORE_ADDR addr;
324      char *contents;
325 {
326   return 0;
327 }
328
329 struct target_ops core_ops = {
330         "core", "Local core dump file",
331         "Use a core file as a target.  Specify the filename of the core file.",
332         core_open, core_close,
333         find_default_attach, core_detach, 0, 0, /* resume, wait */
334         get_core_registers, 
335         0, 0, /* store_regs, prepare_to_store */
336         xfer_memory, core_files_info,
337         ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */
338         0, 0, 0, 0, 0, /* terminal stuff */
339         0, 0, 0, /* kill, load, lookup sym */
340         find_default_create_inferior, 0, /* mourn_inferior */
341         0, /* can_run */
342         0, /* notice_signals */
343         core_stratum, 0, /* next */
344         0, 1, 1, 1, 0,  /* all mem, mem, stack, regs, exec */
345         0, 0,                   /* section pointers */
346         OPS_MAGIC,              /* Always the last thing */
347 };
348
349 void
350 _initialize_corelow()
351 {
352   add_target (&core_ops);
353 }