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