New target macros for getting at the pc, sp and fp.
[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   if (core_bfd) {
55     free (bfd_get_filename (core_bfd));
56     bfd_close (core_bfd);
57     core_bfd = NULL;
58 #ifdef CLEAR_SOLIB
59     CLEAR_SOLIB ();
60 #endif
61     if (core_ops.to_sections) {
62       free ((PTR)core_ops.to_sections);
63       core_ops.to_sections = NULL;
64       core_ops.to_sections_end = NULL;
65     }
66   }
67 }
68
69 #ifdef SOLIB_ADD
70 /* Stub function for catch_errors around shared library hacking. */
71
72 static int 
73 solib_add_stub (from_tty)
74      char *from_tty;
75 {
76     SOLIB_ADD (NULL, (int)from_tty, &core_ops);
77     return 0;
78 }
79 #endif /* SOLIB_ADD */
80
81 /* This routine opens and sets up the core file bfd */
82
83 void
84 core_open (filename, from_tty)
85      char *filename;
86      int from_tty;
87 {
88   const char *p;
89   int siggy;
90   struct cleanup *old_chain;
91   char *temp;
92   bfd *temp_bfd;
93   int ontop;
94   int scratch_chan;
95
96   target_preopen (from_tty);
97   if (!filename)
98     {
99       error (core_bfd? 
100        "No core file specified.  (Use `detach' to stop debugging a core file.)"
101      : "No core file specified.");
102     }
103
104   filename = tilde_expand (filename);
105   if (filename[0] != '/') {
106     temp = concat (current_directory, "/", filename, NULL);
107     free (filename);
108     filename = temp;
109   }
110
111   old_chain = make_cleanup (free, filename);
112
113   scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
114   if (scratch_chan < 0)
115     perror_with_name (filename);
116
117   temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
118   if (temp_bfd == NULL)
119     {
120       perror_with_name (filename);
121     }
122
123   if (!bfd_check_format (temp_bfd, bfd_core))
124     {
125       /* Do it after the err msg */
126       make_cleanup (bfd_close, temp_bfd);
127       error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
128     }
129
130   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
131
132   discard_cleanups (old_chain);         /* Don't free filename any more */
133   unpush_target (&core_ops);
134   core_bfd = temp_bfd;
135   old_chain = make_cleanup (core_close, core_bfd);
136
137   validate_files ();
138
139   /* Find the data section */
140   if (build_section_table (core_bfd, &core_ops.to_sections,
141                            &core_ops.to_sections_end))
142     error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
143            bfd_errmsg (bfd_error));
144
145   ontop = !push_target (&core_ops);
146   discard_cleanups (old_chain);
147
148   p = bfd_core_file_failing_command (core_bfd);
149   if (p)
150     printf_filtered ("Core was generated by `%s'.\n", p);
151
152   siggy = bfd_core_file_failing_signal (core_bfd);
153   if (siggy > 0)
154     printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
155             safe_strsignal (siggy));
156
157   if (ontop) {
158     /* Fetch all registers from core file */
159     target_fetch_registers (-1);
160
161     /* Add symbols and section mappings for any shared libraries */
162 #ifdef SOLIB_ADD
163     catch_errors (solib_add_stub, (char *)from_tty, (char *)0,
164                   RETURN_MASK_ALL);
165 #endif
166
167     /* Now, set up the frame cache, and print the top of stack */
168     set_current_frame (create_new_frame (read_fp (),
169                                          read_pc ()));
170     select_frame (get_current_frame (), 0);
171     print_stack_frame (selected_frame, selected_frame_level, 1);
172   } else {
173     warning (
174 "you won't be able to access this core file until you terminate\n\
175 your %s; do ``info files''", current_target->to_longname);
176   }
177 }
178
179 void
180 core_detach (args, from_tty)
181      char *args;
182      int from_tty;
183 {
184   if (args)
185     error ("Too many arguments");
186   unpush_target (&core_ops);
187   if (from_tty)
188     printf_filtered ("No core file now.\n");
189 }
190
191 /* Get the registers out of a core file.  This is the machine-
192    independent part.  Fetch_core_registers is the machine-dependent
193    part, typically implemented in the xm-file for each architecture.  */
194
195 /* We just get all the registers, so we don't use regno.  */
196 /* ARGSUSED */
197 static void
198 get_core_registers (regno)
199      int regno;
200 {
201   sec_ptr reg_sec;
202   unsigned size;
203   char *the_regs;
204
205   reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
206   if (!reg_sec) goto cant;
207   size = bfd_section_size (core_bfd, reg_sec);
208   the_regs = alloca (size);
209   if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
210     {
211       fetch_core_registers (the_regs, size, 0,
212                             (unsigned) bfd_section_vma (abfd,reg_sec));
213     }
214   else
215     {
216 cant:
217       fprintf_filtered (stderr, "Couldn't fetch registers from core file: %s\n",
218                bfd_errmsg (bfd_error));
219     }
220
221   /* Now do it again for the float registers, if they exist.  */
222   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
223   if (reg_sec) {
224     size = bfd_section_size (core_bfd, reg_sec);
225     the_regs = alloca (size);
226     if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
227                                   size))
228       {
229         fetch_core_registers (the_regs, size, 2,
230                               (unsigned) bfd_section_vma (abfd,reg_sec));
231       }
232     else
233       {
234         fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n",
235                  bfd_errmsg (bfd_error));
236       }
237   }
238   registers_fetched();
239 }
240
241 static void
242 core_files_info (t)
243   struct target_ops *t;
244 {
245   print_section_info (t, core_bfd);
246 }
247 \f
248 struct target_ops core_ops = {
249         "core", "Local core dump file",
250         "Use a core file as a target.  Specify the filename of the core file.",
251         core_open, core_close,
252         find_default_attach, core_detach, 0, 0, /* resume, wait */
253         get_core_registers, 
254         0, 0, /* store_regs, prepare_to_store */
255         xfer_memory, core_files_info,
256         0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
257         0, 0, 0, 0, 0, /* terminal stuff */
258         0, 0, 0, /* kill, load, lookup sym */
259         find_default_create_inferior, 0, /* mourn_inferior */
260         0, /* can_run */
261         0, /* notice_signals */
262         core_stratum, 0, /* next */
263         0, 1, 1, 1, 0,  /* all mem, mem, stack, regs, exec */
264         0, 0,                   /* section pointers */
265         OPS_MAGIC,              /* Always the last thing */
266 };
267
268 void
269 _initialize_corelow()
270 {
271   add_target (&core_ops);
272 }