A ton of changes to improve C++ debugging. See ChangeLog.
[deliverable/binutils-gdb.git] / gdb / core.c
1 /* Work with core dump and executable files, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 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 "defs.h"
21 #include <errno.h>
22 #include <signal.h>
23 #include <fcntl.h>
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
32 #ifdef SOLIB_ADD
33 static int
34 solib_add_stub PARAMS ((char *));
35 #endif
36
37 static void
38 core_close PARAMS ((int));
39
40 static void
41 core_open PARAMS ((char *, int));
42
43 static void
44 core_detach PARAMS ((char *, int));
45
46 static void
47 get_core_registers PARAMS ((int));
48
49 static void
50 core_files_info PARAMS ((struct target_ops *));
51
52 extern char registers[];
53
54 /* Hook for `exec_file_command' command to call. */
55
56 void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
57
58 /* Binary file diddling handle for the core file. */
59
60 bfd *core_bfd = NULL;
61
62 /* Forward decl */
63 extern 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
69 /* ARGSUSED */
70 static void
71 core_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;
78 #ifdef CLEAR_SOLIB
79 CLEAR_SOLIB ();
80 #endif
81 if (core_ops.to_sections) {
82 free ((PTR)core_ops.to_sections);
83 core_ops.to_sections = NULL;
84 core_ops.to_sections_end = NULL;
85 }
86 }
87 }
88
89 #ifdef SOLIB_ADD
90 /* Stub function for catch_errors around shared library hacking. */
91
92 static int
93 solib_add_stub (from_tty)
94 char *from_tty;
95 {
96 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
97 return 0;
98 }
99 #endif /* SOLIB_ADD */
100
101 /* This routine opens and sets up the core file bfd */
102
103 static void
104 core_open (filename, from_tty)
105 char *filename;
106 int from_tty;
107 {
108 const char *p;
109 int siggy;
110 struct cleanup *old_chain;
111 char *temp;
112 bfd *temp_bfd;
113 int ontop;
114 int scratch_chan;
115
116 target_preopen (from_tty);
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] != '/') {
126 temp = concat (current_directory, "/", filename, NULL);
127 free (filename);
128 filename = temp;
129 }
130
131 old_chain = make_cleanup (free, filename);
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);
138 if (temp_bfd == NULL)
139 {
140 perror_with_name (filename);
141 }
142
143 if (!bfd_check_format (temp_bfd, bfd_core))
144 {
145 /* Do it after the err msg */
146 make_cleanup (bfd_close, temp_bfd);
147 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
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 */
160 if (build_section_table (core_bfd, &core_ops.to_sections,
161 &core_ops.to_sections_end))
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);
166 discard_cleanups (old_chain);
167
168 p = bfd_core_file_failing_command (core_bfd);
169 if (p)
170 printf ("Core was generated by `%s'.\n", p);
171
172 siggy = bfd_core_file_failing_signal (core_bfd);
173 if (siggy > 0)
174 printf ("Program terminated with signal %d, %s.\n", siggy,
175 safe_strsignal (siggy));
176
177 if (ontop) {
178 /* Fetch all registers from core file */
179 target_fetch_registers (-1);
180
181 /* Add symbols and section mappings for any shared libraries */
182 #ifdef SOLIB_ADD
183 catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
184 #endif
185
186 /* Now, set up the frame cache, and print the top of stack */
187 set_current_frame (create_new_frame (read_register (FP_REGNUM),
188 read_pc ()));
189 select_frame (get_current_frame (), 0);
190 print_stack_frame (selected_frame, selected_frame_level, 1);
191 } else {
192 warning (
193 "you won't be able to access this core file until you terminate\n\
194 your %s; do ``info files''", current_target->to_longname);
195 }
196 }
197
198 static void
199 core_detach (args, from_tty)
200 char *args;
201 int from_tty;
202 {
203 if (args)
204 error ("Too many arguments");
205 unpush_target (&core_ops);
206 if (from_tty)
207 printf ("No core file now.\n");
208 }
209
210 /* Backward compatability with old way of specifying core files. */
211
212 void
213 core_file_command (filename, from_tty)
214 char *filename;
215 int from_tty;
216 {
217 dont_repeat (); /* Either way, seems bogus. */
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
228 void
229 specify_exec_file_hook (hook)
230 void (*hook) PARAMS ((char *));
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
239 void
240 close_exec_file ()
241 {
242 #ifdef FIXME
243 if (exec_bfd)
244 bfd_tempclose (exec_bfd);
245 #endif
246 }
247
248 void
249 reopen_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,
258 print a warning if they don't go together. */
259
260 void
261 validate_files ()
262 {
263 if (exec_bfd && core_bfd)
264 {
265 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
266 warning ("core file may not match specified executable file.");
267 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
268 warning ("exec file is newer than core file.");
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
276 char *
277 get_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\
284 Use the \"file\" or \"exec-file\" command.");
285 return NULL;
286 }
287
288 static void
289 core_files_info (t)
290 struct target_ops *t;
291 {
292 print_section_info (t, core_bfd);
293 }
294 \f
295 /* Report a memory error with error(). */
296
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 error ("Error accessing memory address %s: %s.",
312 local_hex_string (memaddr), safe_strerror (status));
313 }
314 }
315
316 /* Same as target_read_memory, but report an error if can't read. */
317 void
318 read_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. */
330 void
331 write_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
345 long
346 read_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
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
386 /* We just get all the registers, so we don't use regno. */
387 /* ARGSUSED */
388 static void
389 get_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");
397 if (!reg_sec) goto cant;
398 size = bfd_section_size (core_bfd, reg_sec);
399 the_regs = alloca (size);
400 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
401 {
402 fetch_core_registers (the_regs, size, 0,
403 (unsigned) bfd_section_vma (abfd,reg_sec));
404 }
405 else
406 {
407 cant:
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);
417 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
418 size))
419 {
420 fetch_core_registers (the_regs, size, 2,
421 (unsigned) bfd_section_vma (abfd,reg_sec));
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();
430 }
431 \f
432 struct target_ops core_ops = {
433 "core", "Local core dump file",
434 "Use a core file as a target. Specify the filename of the core file.",
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 */
439 xfer_memory, core_files_info,
440 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
441 0, 0, 0, 0, 0, /* terminal stuff */
442 0, 0, 0, /* kill, load, lookup sym */
443 child_create_inferior, 0, /* mourn_inferior */
444 core_stratum, 0, /* next */
445 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
446 0, 0, /* section pointers */
447 OPS_MAGIC, /* Always the last thing */
448 };
449
450 void
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\
456 No 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.038287 seconds and 4 git commands to generate.