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