Fri Dec 6 23:57:34 1991 K. Richard Pixley (rich at rtl.cygnus.com)
[deliverable/binutils-gdb.git] / gdb / core.c
1 /* Work with core dump and executable files, for GDB.
2 Copyright 1986, 1987, 1989, 1991 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 <stdio.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "defs.h"
25 #include "frame.h" /* required by inferior.h */
26 #include "inferior.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "gdbcore.h"
32
33 extern int xfer_memory ();
34 extern void child_attach (), child_create_inferior ();
35
36 extern int sys_nerr;
37 extern char *sys_errlist[];
38 extern char *sys_siglist[];
39
40 extern char registers[];
41
42 /* Hook for `exec_file_command' command to call. */
43
44 void (*exec_file_display_hook) () = NULL;
45
46 /* Binary file diddling handle for the core file. */
47
48 bfd *core_bfd = NULL;
49
50 /* Forward decl */
51 extern struct target_ops core_ops;
52
53 \f
54 /* Discard all vestiges of any previous core file
55 and mark data and stack spaces as empty. */
56
57 /* ARGSUSED */
58 void
59 core_close (quitting)
60 int quitting;
61 {
62 if (core_bfd) {
63 free (bfd_get_filename (core_bfd));
64 bfd_close (core_bfd);
65 core_bfd = NULL;
66 #ifdef CLEAR_SOLIB
67 CLEAR_SOLIB ();
68 #endif
69 if (core_ops.sections) {
70 free (core_ops.sections);
71 core_ops.sections = NULL;
72 core_ops.sections_end = NULL;
73 }
74 }
75 }
76
77 #ifdef SOLIB_ADD
78 /* Stub function for catch_errors around shared library hacking. */
79
80 int
81 solib_add_stub (from_tty)
82 char *from_tty;
83 {
84 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
85 return 0;
86 }
87 #endif /* SOLIB_ADD */
88
89 /* This routine opens and sets up the core file bfd */
90
91 void
92 core_open (filename, from_tty)
93 char *filename;
94 int from_tty;
95 {
96 const char *p;
97 int siggy;
98 struct cleanup *old_chain;
99 char *temp;
100 bfd *temp_bfd;
101 int ontop;
102 int scratch_chan;
103
104 target_preopen (from_tty);
105 if (!filename)
106 {
107 error (core_bfd?
108 "No core file specified. (Use `detach' to stop debugging a core file.)"
109 : "No core file specified.");
110 }
111
112 filename = tilde_expand (filename);
113 if (filename[0] != '/') {
114 temp = concat (current_directory, "/", filename, NULL);
115 free (filename);
116 filename = temp;
117 }
118
119 old_chain = make_cleanup (free, filename);
120
121 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
122 if (scratch_chan < 0)
123 perror_with_name (filename);
124
125 temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
126 if (temp_bfd == NULL)
127 {
128 perror_with_name (filename);
129 }
130
131 if (!bfd_check_format (temp_bfd, bfd_core))
132 {
133 make_cleanup (bfd_close, temp_bfd); /* Do it after the err msg */
134 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
135 }
136
137 /* Looks semi-reasonable. Toss the old core file and work on the new. */
138
139 discard_cleanups (old_chain); /* Don't free filename any more */
140 unpush_target (&core_ops);
141 core_bfd = temp_bfd;
142 old_chain = make_cleanup (core_close, core_bfd);
143
144 validate_files ();
145
146 /* Find the data section */
147 if (build_section_table (core_bfd, &core_ops.sections,
148 &core_ops.sections_end))
149 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
150 bfd_errmsg (bfd_error));
151
152 ontop = !push_target (&core_ops);
153 discard_cleanups (old_chain);
154
155 p = bfd_core_file_failing_command (core_bfd);
156 if (p)
157 printf ("Core was generated by `%s'.\n", p);
158
159 siggy = bfd_core_file_failing_signal (core_bfd);
160 if (siggy > 0)
161 printf ("Program terminated with signal %d, %s.\n", siggy,
162 siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
163
164 if (ontop) {
165 /* Fetch all registers from core file */
166 target_fetch_registers (-1);
167
168 /* Add symbols and section mappings for any shared libraries */
169 #ifdef SOLIB_ADD
170 (void) catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
171 #endif
172
173 /* Now, set up the frame cache, and print the top of stack */
174 set_current_frame (create_new_frame (read_register (FP_REGNUM),
175 read_pc ()));
176 select_frame (get_current_frame (), 0);
177 print_stack_frame (selected_frame, selected_frame_level, 1);
178 } else {
179 printf (
180 "Warning: you won't be able to access this core file until you terminate\n\
181 your %s; do ``info files''\n", current_target->to_longname);
182 }
183 }
184
185 void
186 core_detach (args, from_tty)
187 char *args;
188 int from_tty;
189 {
190 if (args)
191 error ("Too many arguments");
192 unpush_target (&core_ops);
193 if (from_tty)
194 printf ("No core file now.\n");
195 }
196
197 /* Backward compatability with old way of specifying core files. */
198
199 void
200 core_file_command (filename, from_tty)
201 char *filename;
202 int from_tty;
203 {
204 dont_repeat (); /* Either way, seems bogus. */
205 if (!filename)
206 core_detach (filename, from_tty);
207 else
208 core_open (filename, from_tty);
209 }
210
211 \f
212 /* Call this to specify the hook for exec_file_command to call back.
213 This is called from the x-window display code. */
214
215 void
216 specify_exec_file_hook (hook)
217 void (*hook) ();
218 {
219 exec_file_display_hook = hook;
220 }
221
222 /* The exec file must be closed before running an inferior.
223 If it is needed again after the inferior dies, it must
224 be reopened. */
225
226 void
227 close_exec_file ()
228 {
229 #ifdef FIXME
230 if (exec_bfd)
231 bfd_tempclose (exec_bfd);
232 #endif
233 }
234
235 void
236 reopen_exec_file ()
237 {
238 #ifdef FIXME
239 if (exec_bfd)
240 bfd_reopen (exec_bfd);
241 #endif
242 }
243 \f
244 /* If we have both a core file and an exec file,
245 print a warning if they don't go together. */
246
247 void
248 validate_files ()
249 {
250 if (exec_bfd && core_bfd)
251 {
252 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
253 printf ("Warning: core file may not match specified executable file.\n");
254 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
255 printf ("Warning: exec file is newer than core file.\n");
256 }
257 }
258
259 /* Return the name of the executable file as a string.
260 ERR nonzero means get error if there is none specified;
261 otherwise return 0 in that case. */
262
263 char *
264 get_exec_file (err)
265 int err;
266 {
267 if (exec_bfd) return bfd_get_filename(exec_bfd);
268 if (!err) return NULL;
269
270 error ("No executable file specified.\n\
271 Use the \"file\" or \"exec-file\" command.");
272 return NULL;
273 }
274
275 static void
276 core_files_info (t)
277 struct target_ops *t;
278 {
279 struct section_table *p;
280
281 printf_filtered ("\t`%s', ", bfd_get_filename(core_bfd));
282 wrap_here (" ");
283 printf_filtered ("file type %s.\n", bfd_get_target(core_bfd));
284
285 for (p = t->sections; p < t->sections_end; p++) {
286 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
287 printf_filtered (" - %s is %s",
288 local_hex_string_custom (p->endaddr, "08"),
289 bfd_section_name (p->bfd, p->sec_ptr));
290 if (p->bfd != core_bfd) {
291 printf_filtered (" in %s", bfd_get_filename (p->bfd));
292 }
293 printf_filtered ("\n");
294 }
295 }
296 \f
297 void
298 memory_error (status, memaddr)
299 int status;
300 CORE_ADDR memaddr;
301 {
302
303 if (status == EIO)
304 {
305 /* Actually, address between memaddr and memaddr + len
306 was out of bounds. */
307 error ("Cannot access memory at address %s.", local_hex_string(memaddr));
308 }
309 else
310 {
311 if (status >= sys_nerr || status < 0)
312 error ("Error accessing memory address %s: unknown error (%d).",
313 local_hex_string(memaddr), status);
314 else
315 error ("Error accessing memory address %s: %s.",
316 local_hex_string(memaddr), sys_errlist[status]);
317 }
318 }
319
320 /* Same as target_read_memory, but report an error if can't read. */
321 void
322 read_memory (memaddr, myaddr, len)
323 CORE_ADDR memaddr;
324 char *myaddr;
325 int len;
326 {
327 int status;
328 status = target_read_memory (memaddr, myaddr, len);
329 if (status != 0)
330 memory_error (status, memaddr);
331 }
332
333 /* Same as target_write_memory, but report an error if can't write. */
334 void
335 write_memory (memaddr, myaddr, len)
336 CORE_ADDR memaddr;
337 char *myaddr;
338 int len;
339 {
340 int status;
341
342 status = target_write_memory (memaddr, myaddr, len);
343 if (status != 0)
344 memory_error (status, memaddr);
345 }
346
347 /* Read an integer from debugged memory, given address and number of bytes. */
348
349 long
350 read_memory_integer (memaddr, len)
351 CORE_ADDR memaddr;
352 int len;
353 {
354 char cbuf;
355 short sbuf;
356 int ibuf;
357 long lbuf;
358
359 if (len == sizeof (char))
360 {
361 read_memory (memaddr, &cbuf, len);
362 return cbuf;
363 }
364 if (len == sizeof (short))
365 {
366 read_memory (memaddr, (char *)&sbuf, len);
367 SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
368 return sbuf;
369 }
370 if (len == sizeof (int))
371 {
372 read_memory (memaddr, (char *)&ibuf, len);
373 SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
374 return ibuf;
375 }
376 if (len == sizeof (lbuf))
377 {
378 read_memory (memaddr, (char *)&lbuf, len);
379 SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
380 return lbuf;
381 }
382 error ("Cannot handle integers of %d bytes.", len);
383 return -1; /* for lint */
384 }
385 \f
386 /* Get the registers out of a core file. This is the machine-
387 independent part. Fetch_core_registers is the machine-dependent
388 part, typically implemented in the xm-file for each architecture. */
389
390 /* We just get all the registers, so we don't use regno. */
391 /* ARGSUSED */
392 static void
393 get_core_registers (regno)
394 int regno;
395 {
396 sec_ptr reg_sec;
397 unsigned size;
398 char *the_regs;
399
400 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
401 if (!reg_sec) goto cant;
402 size = bfd_section_size (core_bfd, reg_sec);
403 the_regs = alloca (size);
404 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
405 {
406 fetch_core_registers (the_regs, size, 0,
407 (unsigned) bfd_section_vma (abfd,reg_sec));
408 }
409 else
410 {
411 cant:
412 fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
413 bfd_errmsg (bfd_error));
414 }
415
416 /* Now do it again for the float registers, if they exist. */
417 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
418 if (reg_sec) {
419 size = bfd_section_size (core_bfd, reg_sec);
420 the_regs = alloca (size);
421 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
422 size))
423 {
424 fetch_core_registers (the_regs, size, 2,
425 (unsigned) bfd_section_vma (abfd,reg_sec));
426 }
427 else
428 {
429 fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
430 bfd_errmsg (bfd_error));
431 }
432 }
433 registers_fetched();
434 }
435 \f
436 struct target_ops core_ops = {
437 "core", "Local core dump file",
438 "Use a core file as a target. Specify the filename of the core file.",
439 core_open, core_close,
440 child_attach, core_detach, 0, 0, /* resume, wait */
441 get_core_registers,
442 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
443 xfer_memory, core_files_info,
444 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
445 0, 0, 0, 0, 0, /* terminal stuff */
446 0, 0, 0, 0, /* kill, load, call fn, lookup sym */
447 child_create_inferior, 0, /* mourn_inferior */
448 core_stratum, 0, /* next */
449 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
450 0, 0, /* section pointers */
451 OPS_MAGIC, /* Always the last thing */
452 };
453
454 void
455 _initialize_core()
456 {
457
458 add_com ("core-file", class_files, core_file_command,
459 "Use FILE as core dump for examining memory and registers.\n\
460 No arg means have no core file. This command has been superseded by the\n\
461 `target core' and `detach' commands.");
462 add_target (&core_ops);
463 }
This page took 0.039288 seconds and 4 git commands to generate.