* Shared libaray/corefile changes from Peter Schauer:
[deliverable/binutils-gdb.git] / gdb / core.c
1 /* Work with core dump and executable files, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include "defs.h"
24 #include "param.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 struct section_table *core_sections, *core_sections_end;
47
48 /* Binary file diddling handle for the core file. */
49
50 bfd *core_bfd = NULL;
51
52 /* Forward decl */
53 extern struct target_ops core_ops;
54
55 \f
56 /* Discard all vestiges of any previous core file
57 and mark data and stack spaces as empty. */
58
59 void
60 core_close (quitting)
61 int quitting;
62 {
63 if (core_bfd) {
64 free (bfd_get_filename (core_bfd));
65 bfd_close (core_bfd);
66 core_bfd = NULL;
67 #ifdef CLEAR_SOLIB
68 CLEAR_SOLIB ();
69 #endif
70 }
71 }
72
73 /* This routine opens and sets up the core file bfd */
74
75 void
76 core_open (filename, from_tty)
77 char *filename;
78 int from_tty;
79 {
80 char *p;
81 int siggy;
82 struct cleanup *old_chain;
83 char *temp;
84 bfd *temp_bfd;
85 int ontop;
86
87 target_preopen (from_tty);
88 if (!filename)
89 {
90 error (core_bfd?
91 "No core file specified. (Use `detach' to stop debugging a core file.)"
92 : "No core file specified.");
93 }
94
95 filename = tilde_expand (filename);
96 if (filename[0] != '/') {
97 temp = concat (current_directory, "/", filename);
98 free (filename);
99 filename = temp;
100 }
101
102 old_chain = make_cleanup (free, filename);
103 temp_bfd = bfd_openr (filename, NULL);
104 if (temp_bfd == NULL)
105 {
106 perror_with_name (filename);
107 }
108
109 if (!bfd_check_format (temp_bfd, bfd_core))
110 {
111 bfd_close (temp_bfd);
112 error ("\"%s\" does not appear to be a core dump", filename);
113 }
114
115 /* Looks semi-reasonable. Toss the old core file and work on the new. */
116
117 discard_cleanups (old_chain); /* Don't free filename any more */
118 unpush_target (&core_ops);
119 core_bfd = temp_bfd;
120 old_chain = make_cleanup (core_close, core_bfd);
121
122 validate_files ();
123
124 /* Find the data section */
125 if (build_section_table (core_bfd, &core_sections, &core_sections_end))
126 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
127 bfd_errmsg (bfd_error));
128
129 ontop = !push_target (&core_ops);
130
131 p = bfd_core_file_failing_command (core_bfd);
132 if (p)
133 printf ("Core file invoked as `%s'.\n", p);
134
135 siggy = bfd_core_file_failing_signal (core_bfd);
136 if (siggy > 0)
137 printf ("Program terminated with signal %d, %s.\n", siggy,
138 siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
139
140 if (ontop) {
141 /* Fetch all registers from core file */
142 target_fetch_registers (-1);
143 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
144 read_pc ()));
145 select_frame (get_current_frame (), 0);
146 #if 0
147 /* Shouldn't be necessary to read in symbols. */
148 #ifdef SOLIB_ADD
149 SOLIB_ADD (NULL, from_tty);
150 #endif
151 #endif /* 0 */
152 print_sel_frame (0); /* Print the top frame and source line */
153 } else {
154 printf (
155 "Warning: you won't be able to access this core file until you terminate\n\
156 your %s; do ``info files''\n", current_target->to_longname);
157 }
158
159 discard_cleanups (old_chain);
160 }
161
162 void
163 core_detach (args, from_tty)
164 char *args;
165 int from_tty;
166 {
167 if (args)
168 error ("Too many arguments");
169 unpush_target (&core_ops);
170 if (from_tty)
171 printf ("No core file now.\n");
172 }
173
174 /* Backward compatability with old way of specifying core files. */
175
176 void
177 core_file_command (filename, from_tty)
178 char *filename;
179 int from_tty;
180 {
181 dont_repeat (); /* Either way, seems bogus. */
182 if (!filename)
183 core_detach (filename, from_tty);
184 else
185 core_open (filename, from_tty);
186 }
187
188 \f
189 /* Call this to specify the hook for exec_file_command to call back.
190 This is called from the x-window display code. */
191
192 void
193 specify_exec_file_hook (hook)
194 void (*hook) ();
195 {
196 exec_file_display_hook = hook;
197 }
198
199 /* The exec file must be closed before running an inferior.
200 If it is needed again after the inferior dies, it must
201 be reopened. */
202
203 void
204 close_exec_file ()
205 {
206 #ifdef FIXME
207 if (exec_bfd)
208 bfd_tempclose (exec_bfd);
209 #endif
210 }
211
212 void
213 reopen_exec_file ()
214 {
215 #ifdef FIXME
216 if (exec_bfd)
217 bfd_reopen (exec_bfd);
218 #endif
219 }
220 \f
221 /* If we have both a core file and an exec file,
222 print a warning if they don't go together.
223 This should really check that the core file came
224 from that exec file, but I don't know how to do it. */
225
226 void
227 validate_files ()
228 {
229 if (exec_bfd && core_bfd)
230 {
231 if (core_file_matches_executable_p (core_bfd, exec_bfd))
232 printf ("Warning: core file does not match specified executable file.\n");
233 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
234 printf ("Warning: exec file is newer than core file.\n");
235 }
236 }
237
238 /* Return the name of the executable file as a string.
239 ERR nonzero means get error if there is none specified;
240 otherwise return 0 in that case. */
241
242 char *
243 get_exec_file (err)
244 int err;
245 {
246 if (exec_bfd) return bfd_get_filename(exec_bfd);
247 if (!err) return NULL;
248
249 error ("No executable file specified.\n\
250 Use the \"file\" or \"exec-file\" command.");
251 return NULL;
252 }
253
254 static void
255 core_files_info ()
256 {
257 struct section_table *p;
258
259 printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd));
260
261 for (p = core_sections; p < core_sections_end; p++)
262 printf("\tcore file from 0x%08x to 0x%08x is %s\n",
263 p->addr, p->endaddr,
264 bfd_section_name (core_bfd, p->sec_ptr));
265 }
266 \f
267 void
268 memory_error (status, memaddr)
269 int status;
270 CORE_ADDR memaddr;
271 {
272
273 if (status == EIO)
274 {
275 /* Actually, address between memaddr and memaddr + len
276 was out of bounds. */
277 error ("Cannot access memory: address 0x%x out of bounds.", memaddr);
278 }
279 else
280 {
281 if (status >= sys_nerr || status < 0)
282 error ("Error accessing memory address 0x%x: unknown error (%d).",
283 memaddr, status);
284 else
285 error ("Error accessing memory address 0x%x: %s.",
286 memaddr, sys_errlist[status]);
287 }
288 }
289
290 /* Same as target_read_memory, but report an error if can't read. */
291 void
292 read_memory (memaddr, myaddr, len)
293 CORE_ADDR memaddr;
294 char *myaddr;
295 int len;
296 {
297 int status;
298 status = target_read_memory (memaddr, myaddr, len);
299 if (status != 0)
300 memory_error (status, memaddr);
301 }
302
303 /* Same as target_write_memory, but report an error if can't write. */
304 void
305 write_memory (memaddr, myaddr, len)
306 CORE_ADDR memaddr;
307 char *myaddr;
308 int len;
309 {
310 int status;
311
312 status = target_write_memory (memaddr, myaddr, len);
313 if (status != 0)
314 memory_error (status, memaddr);
315 }
316
317 /* Read an integer from debugged memory, given address and number of bytes. */
318
319 long
320 read_memory_integer (memaddr, len)
321 CORE_ADDR memaddr;
322 int len;
323 {
324 char cbuf;
325 short sbuf;
326 int ibuf;
327 long lbuf;
328
329 if (len == sizeof (char))
330 {
331 read_memory (memaddr, &cbuf, len);
332 return cbuf;
333 }
334 if (len == sizeof (short))
335 {
336 read_memory (memaddr, (char *)&sbuf, len);
337 SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
338 return sbuf;
339 }
340 if (len == sizeof (int))
341 {
342 read_memory (memaddr, (char *)&ibuf, len);
343 SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
344 return ibuf;
345 }
346 if (len == sizeof (lbuf))
347 {
348 read_memory (memaddr, (char *)&lbuf, len);
349 SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
350 return lbuf;
351 }
352 error ("Cannot handle integers of %d bytes.", len);
353 return -1; /* for lint */
354 }
355 \f
356 /* Read or write the core file.
357
358 Args are address within core file, address within gdb address-space,
359 length, and a flag indicating whether to read or write.
360
361 Result is a length:
362
363 0: We cannot handle this address and length.
364 > 0: We have handled N bytes starting at this address.
365 (If N == length, we did it all.) We might be able
366 to handle more bytes beyond this length, but no
367 promises.
368 < 0: We cannot handle this address, but if somebody
369 else handles (-N) bytes, we can start from there.
370
371 The actual work is done by xfer_memory in exec.c, which we share
372 in common with exec_xfer_memory(). */
373
374 static int
375 core_xfer_memory (memaddr, myaddr, len, write)
376 CORE_ADDR memaddr;
377 char *myaddr;
378 int len;
379 int write;
380 {
381 int res;
382 res = xfer_memory (memaddr, myaddr, len, write,
383 core_bfd, core_sections, core_sections_end);
384 #ifdef SOLIB_XFER_MEMORY
385 if (res == 0)
386 res = SOLIB_XFER_MEMORY (memaddr, myaddr, len, write);
387 #endif
388 return res;
389 }
390 \f
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
395 static int
396 get_core_registers (regno)
397 int regno;
398 {
399 sec_ptr reg_sec;
400 unsigned size;
401 char *the_regs;
402
403 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
404 size = bfd_section_size (core_bfd, reg_sec);
405 the_regs = alloca (size);
406 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
407 (unsigned)0, size))
408 {
409 fetch_core_registers (the_regs, size, 0);
410 }
411 else
412 {
413 fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
414 bfd_errmsg (bfd_error));
415 }
416
417 /* Now do it again for the float registers, if they exist. */
418 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
419 if (reg_sec) {
420 size = bfd_section_size (core_bfd, reg_sec);
421 the_regs = alloca (size);
422 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
423 (unsigned)0, size))
424 {
425 fetch_core_registers (the_regs, size, 2);
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 return 0; /* FIXME, what result goes here? */
435 }
436 \f
437 struct target_ops core_ops = {
438 "core", "Local core dump file",
439 "Use a core file as a target. Specify the filename of the core file.",
440 core_open, core_close,
441 child_attach, core_detach, 0, 0, /* resume, wait */
442 get_core_registers,
443 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
444 core_xfer_memory, core_files_info,
445 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
446 0, 0, 0, 0, 0, /* terminal stuff */
447 0, 0, 0, 0, 0, /* kill, load, add_syms, call fn, lookup sym */
448 child_create_inferior, 0, /* mourn_inferior */
449 core_stratum, 0, /* next */
450 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
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.038733 seconds and 5 git commands to generate.