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