* corelow.c (solib_add_stub, core_open): Pass address of from_tty
[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 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include <errno.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h"  /* required by inferior.h */
25 #include "inferior.h"
26 #include "symtab.h"
27 #include "command.h"
28 #include "bfd.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "thread.h"
32
33 static void
34 core_files_info PARAMS ((struct target_ops *));
35
36 #ifdef SOLIB_ADD
37 static int 
38 solib_add_stub PARAMS ((char *));
39 #endif
40
41 static void
42 core_close PARAMS ((int));
43
44 static void
45 get_core_registers PARAMS ((int));
46
47 /* Discard all vestiges of any previous core file
48    and mark data and stack spaces as empty.  */
49
50 /* ARGSUSED */
51 static void
52 core_close (quitting)
53      int quitting;
54 {
55   inferior_pid = 0;             /* Avoid confusion from thread stuff */
56
57   if (core_bfd) {
58     free (bfd_get_filename (core_bfd));
59     bfd_close (core_bfd);
60     core_bfd = NULL;
61 #ifdef CLEAR_SOLIB
62     CLEAR_SOLIB ();
63 #endif
64     if (core_ops.to_sections) {
65       free ((PTR)core_ops.to_sections);
66       core_ops.to_sections = NULL;
67       core_ops.to_sections_end = NULL;
68     }
69   }
70 }
71
72 #ifdef SOLIB_ADD
73 /* Stub function for catch_errors around shared library hacking.  FROM_TTYP
74    is really an int * which points to from_tty.  */
75
76 static int 
77 solib_add_stub (from_ttyp)
78      char *from_ttyp;
79 {
80     SOLIB_ADD (NULL, *(int *)from_ttyp, &core_ops);
81     return 0;
82 }
83 #endif /* SOLIB_ADD */
84
85 /* Look for sections whose names start with `.reg/' so that we can extract the
86    list of threads in a core file.  */
87
88 static void
89 add_to_thread_list (abfd, asect, reg_sect_arg)
90      bfd *abfd;
91      asection *asect;
92      PTR reg_sect_arg;
93 {
94   int thread_id;
95   asection *reg_sect = (asection *) reg_sect_arg;
96
97   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
98     return;
99
100   thread_id = atoi (bfd_section_name (abfd, asect) + 5);
101
102   add_thread (thread_id);
103
104 /* Warning, Will Robinson, looking at BFD private data! */
105
106   if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
107     inferior_pid = thread_id;   /* Yes, make it current */
108 }
109
110 /* This routine opens and sets up the core file bfd */
111
112 void
113 core_open (filename, from_tty)
114      char *filename;
115      int from_tty;
116 {
117   const char *p;
118   int siggy;
119   struct cleanup *old_chain;
120   char *temp;
121   bfd *temp_bfd;
122   int ontop;
123   int scratch_chan;
124
125   target_preopen (from_tty);
126   if (!filename)
127     {
128       error (core_bfd? 
129        "No core file specified.  (Use `detach' to stop debugging a core file.)"
130      : "No core file specified.");
131     }
132
133   filename = tilde_expand (filename);
134   if (filename[0] != '/') {
135     temp = concat (current_directory, "/", filename, NULL);
136     free (filename);
137     filename = temp;
138   }
139
140   old_chain = make_cleanup (free, filename);
141
142   scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
143   if (scratch_chan < 0)
144     perror_with_name (filename);
145
146   temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
147   if (temp_bfd == NULL)
148     {
149       perror_with_name (filename);
150     }
151
152   if (!bfd_check_format (temp_bfd, bfd_core))
153     {
154       /* Do it after the err msg */
155       make_cleanup (bfd_close, temp_bfd);
156       error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
157     }
158
159   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
160
161   discard_cleanups (old_chain);         /* Don't free filename any more */
162   unpush_target (&core_ops);
163   core_bfd = temp_bfd;
164   old_chain = make_cleanup (core_close, core_bfd);
165
166   validate_files ();
167
168   /* Find the data section */
169   if (build_section_table (core_bfd, &core_ops.to_sections,
170                            &core_ops.to_sections_end))
171     error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
172            bfd_errmsg (bfd_error));
173
174   ontop = !push_target (&core_ops);
175   discard_cleanups (old_chain);
176
177   p = bfd_core_file_failing_command (core_bfd);
178   if (p)
179     printf_filtered ("Core was generated by `%s'.\n", p);
180
181   siggy = bfd_core_file_failing_signal (core_bfd);
182   if (siggy > 0)
183     printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
184             safe_strsignal (siggy));
185
186   /* Build up thread list from BFD sections. */
187
188   init_thread_list ();
189   bfd_map_over_sections (core_bfd, add_to_thread_list,
190                          bfd_get_section_by_name (core_bfd, ".reg"));
191
192   if (ontop) {
193     /* Fetch all registers from core file */
194     target_fetch_registers (-1);
195
196     /* Add symbols and section mappings for any shared libraries */
197 #ifdef SOLIB_ADD
198     catch_errors (solib_add_stub, &from_tty, (char *)0,
199                   RETURN_MASK_ALL);
200 #endif
201
202     /* Now, set up the frame cache, and print the top of stack */
203     set_current_frame (create_new_frame (read_fp (),
204                                          read_pc ()));
205     select_frame (get_current_frame (), 0);
206     print_stack_frame (selected_frame, selected_frame_level, 1);
207   } else {
208     warning (
209 "you won't be able to access this core file until you terminate\n\
210 your %s; do ``info files''", current_target->to_longname);
211   }
212 }
213
214 void
215 core_detach (args, from_tty)
216      char *args;
217      int from_tty;
218 {
219   if (args)
220     error ("Too many arguments");
221   unpush_target (&core_ops);
222   reinit_frame_cache ();
223   if (from_tty)
224     printf_filtered ("No core file now.\n");
225 }
226
227 /* Get the registers out of a core file.  This is the machine-
228    independent part.  Fetch_core_registers is the machine-dependent
229    part, typically implemented in the xm-file for each architecture.  */
230
231 /* We just get all the registers, so we don't use regno.  */
232 /* ARGSUSED */
233 static void
234 get_core_registers (regno)
235      int regno;
236 {
237   sec_ptr reg_sec;
238   unsigned size;
239   char *the_regs;
240   char secname[10];
241
242   /* Thread support.  If inferior_pid is non-zero, then we have found a core
243      file with threads (or multiple processes).  In that case, we need to
244      use the appropriate register section, else we just use `.reg'. */
245
246   /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
247
248   if (inferior_pid)
249     sprintf (secname, ".reg/%d", inferior_pid);
250   else
251     strcpy (secname, ".reg");
252
253   reg_sec = bfd_get_section_by_name (core_bfd, secname);
254   if (!reg_sec) goto cant;
255   size = bfd_section_size (core_bfd, reg_sec);
256   the_regs = alloca (size);
257   if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
258     {
259       fetch_core_registers (the_regs, size, 0,
260                             (unsigned) bfd_section_vma (abfd,reg_sec));
261     }
262   else
263     {
264 cant:
265       fprintf_filtered (gdb_stderr, "Couldn't fetch registers from core file: %s\n",
266                bfd_errmsg (bfd_error));
267     }
268
269   /* Now do it again for the float registers, if they exist.  */
270   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
271   if (reg_sec) {
272     size = bfd_section_size (core_bfd, reg_sec);
273     the_regs = alloca (size);
274     if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
275                                   size))
276       {
277         fetch_core_registers (the_regs, size, 2,
278                               (unsigned) bfd_section_vma (abfd,reg_sec));
279       }
280     else
281       {
282         fprintf_filtered (gdb_stderr, "Couldn't fetch register set 2 from core file: %s\n",
283                  bfd_errmsg (bfd_error));
284       }
285   }
286   registers_fetched();
287 }
288
289 static void
290 core_files_info (t)
291   struct target_ops *t;
292 {
293   print_section_info (t, core_bfd);
294 }
295 \f
296 /* If mourn is being called in all the right places, this could be say
297    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
298
299 static int
300 ignore (addr, contents)
301      CORE_ADDR addr;
302      char *contents;
303 {
304   return 0;
305 }
306
307 struct target_ops core_ops = {
308         "core", "Local core dump file",
309         "Use a core file as a target.  Specify the filename of the core file.",
310         core_open, core_close,
311         find_default_attach, core_detach, 0, 0, /* resume, wait */
312         get_core_registers, 
313         0, 0, /* store_regs, prepare_to_store */
314         xfer_memory, core_files_info,
315         ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */
316         0, 0, 0, 0, 0, /* terminal stuff */
317         0, 0, 0, /* kill, load, lookup sym */
318         find_default_create_inferior, 0, /* mourn_inferior */
319         0, /* can_run */
320         0, /* notice_signals */
321         core_stratum, 0, /* next */
322         0, 1, 1, 1, 0,  /* all mem, mem, stack, regs, exec */
323         0, 0,                   /* section pointers */
324         OPS_MAGIC,              /* Always the last thing */
325 };
326
327 void
328 _initialize_corelow()
329 {
330   add_target (&core_ops);
331 }