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