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