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