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 | ||
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; | |
d0237a54 JK |
67 | #ifdef CLEAR_SOLIB |
68 | CLEAR_SOLIB (); | |
69 | #endif | |
dd3b648e RP |
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 | ||
f2fc6e7a | 87 | target_preopen (from_tty); |
dd3b648e RP |
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); | |
269c3011 | 130 | make_cleanup (unpush_target, &core_ops); |
dd3b648e RP |
131 | |
132 | p = bfd_core_file_failing_command (core_bfd); | |
133 | if (p) | |
134 | printf ("Core file invoked as `%s'.\n", p); | |
135 | ||
136 | siggy = bfd_core_file_failing_signal (core_bfd); | |
137 | if (siggy > 0) | |
138 | printf ("Program terminated with signal %d, %s.\n", siggy, | |
139 | siggy < NSIG ? sys_siglist[siggy] : "(undocumented)"); | |
140 | ||
141 | if (ontop) { | |
142 | /* Fetch all registers from core file */ | |
143 | target_fetch_registers (-1); | |
144 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), | |
145 | read_pc ())); | |
146 | select_frame (get_current_frame (), 0); | |
d0237a54 JK |
147 | #ifdef SOLIB_ADD |
148 | SOLIB_ADD (NULL, from_tty); | |
149 | #endif | |
dd3b648e RP |
150 | print_sel_frame (0); /* Print the top frame and source line */ |
151 | } else { | |
152 | printf ( | |
153 | "Warning: you won't be able to access this core file until you terminate\n\ | |
154 | your %s; do ``info files''\n", current_target->to_longname); | |
155 | } | |
156 | ||
157 | discard_cleanups (old_chain); | |
158 | } | |
159 | ||
160 | void | |
161 | core_detach (args, from_tty) | |
162 | char *args; | |
163 | int from_tty; | |
164 | { | |
dd3b648e RP |
165 | if (args) |
166 | error ("Too many arguments"); | |
3f2e006b | 167 | unpush_target (&core_ops); |
dd3b648e RP |
168 | if (from_tty) |
169 | printf ("No core file now.\n"); | |
170 | } | |
171 | ||
172 | /* Backward compatability with old way of specifying core files. */ | |
173 | ||
174 | void | |
175 | core_file_command (filename, from_tty) | |
176 | char *filename; | |
177 | int from_tty; | |
178 | { | |
3f2e006b | 179 | dont_repeat (); /* Either way, seems bogus. */ |
dd3b648e RP |
180 | if (!filename) |
181 | core_detach (filename, from_tty); | |
182 | else | |
183 | core_open (filename, from_tty); | |
184 | } | |
185 | ||
186 | \f | |
187 | /* Call this to specify the hook for exec_file_command to call back. | |
188 | This is called from the x-window display code. */ | |
189 | ||
190 | void | |
191 | specify_exec_file_hook (hook) | |
192 | void (*hook) (); | |
193 | { | |
194 | exec_file_display_hook = hook; | |
195 | } | |
196 | ||
197 | /* The exec file must be closed before running an inferior. | |
198 | If it is needed again after the inferior dies, it must | |
199 | be reopened. */ | |
200 | ||
201 | void | |
202 | close_exec_file () | |
203 | { | |
204 | #ifdef FIXME | |
205 | if (exec_bfd) | |
206 | bfd_tempclose (exec_bfd); | |
207 | #endif | |
208 | } | |
209 | ||
210 | void | |
211 | reopen_exec_file () | |
212 | { | |
213 | #ifdef FIXME | |
214 | if (exec_bfd) | |
215 | bfd_reopen (exec_bfd); | |
216 | #endif | |
217 | } | |
218 | \f | |
219 | /* If we have both a core file and an exec file, | |
220 | print a warning if they don't go together. | |
221 | This should really check that the core file came | |
222 | from that exec file, but I don't know how to do it. */ | |
223 | ||
224 | void | |
225 | validate_files () | |
226 | { | |
227 | if (exec_bfd && core_bfd) | |
228 | { | |
229 | if (core_file_matches_executable_p (core_bfd, exec_bfd)) | |
230 | printf ("Warning: core file does not match specified executable file.\n"); | |
231 | else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd)) | |
232 | printf ("Warning: exec file is newer than core file.\n"); | |
233 | } | |
234 | } | |
235 | ||
236 | /* Return the name of the executable file as a string. | |
237 | ERR nonzero means get error if there is none specified; | |
238 | otherwise return 0 in that case. */ | |
239 | ||
240 | char * | |
241 | get_exec_file (err) | |
242 | int err; | |
243 | { | |
244 | if (exec_bfd) return bfd_get_filename(exec_bfd); | |
245 | if (!err) return NULL; | |
246 | ||
247 | error ("No executable file specified.\n\ | |
248 | Use the \"file\" or \"exec-file\" command."); | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | static void | |
253 | core_files_info () | |
254 | { | |
255 | struct section_table *p; | |
256 | ||
257 | printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd)); | |
258 | ||
259 | for (p = core_sections; p < core_sections_end; p++) | |
260 | printf("\tcore file from 0x%08x to 0x%08x is %s\n", | |
261 | p->addr, p->endaddr, | |
262 | bfd_section_name (core_bfd, p->sec_ptr)); | |
263 | } | |
264 | \f | |
265 | void | |
266 | memory_error (status, memaddr) | |
267 | int status; | |
268 | CORE_ADDR memaddr; | |
269 | { | |
270 | ||
271 | if (status == EIO) | |
272 | { | |
273 | /* Actually, address between memaddr and memaddr + len | |
274 | was out of bounds. */ | |
275 | error ("Cannot access memory: address 0x%x out of bounds.", memaddr); | |
276 | } | |
277 | else | |
278 | { | |
279 | if (status >= sys_nerr || status < 0) | |
280 | error ("Error accessing memory address 0x%x: unknown error (%d).", | |
281 | memaddr, status); | |
282 | else | |
283 | error ("Error accessing memory address 0x%x: %s.", | |
284 | memaddr, sys_errlist[status]); | |
285 | } | |
286 | } | |
287 | ||
288 | /* Same as target_read_memory, but report an error if can't read. */ | |
289 | void | |
290 | read_memory (memaddr, myaddr, len) | |
291 | CORE_ADDR memaddr; | |
292 | char *myaddr; | |
293 | int len; | |
294 | { | |
295 | int status; | |
296 | status = target_read_memory (memaddr, myaddr, len); | |
297 | if (status != 0) | |
298 | memory_error (status, memaddr); | |
299 | } | |
300 | ||
301 | /* Same as target_write_memory, but report an error if can't write. */ | |
302 | void | |
303 | write_memory (memaddr, myaddr, len) | |
304 | CORE_ADDR memaddr; | |
305 | char *myaddr; | |
306 | int len; | |
307 | { | |
308 | int status; | |
309 | ||
310 | status = target_write_memory (memaddr, myaddr, len); | |
311 | if (status != 0) | |
312 | memory_error (status, memaddr); | |
313 | } | |
314 | ||
315 | /* Read an integer from debugged memory, given address and number of bytes. */ | |
316 | ||
317 | long | |
318 | read_memory_integer (memaddr, len) | |
319 | CORE_ADDR memaddr; | |
320 | int len; | |
321 | { | |
322 | char cbuf; | |
323 | short sbuf; | |
324 | int ibuf; | |
325 | long lbuf; | |
326 | ||
327 | if (len == sizeof (char)) | |
328 | { | |
329 | read_memory (memaddr, &cbuf, len); | |
330 | return cbuf; | |
331 | } | |
332 | if (len == sizeof (short)) | |
333 | { | |
334 | read_memory (memaddr, (char *)&sbuf, len); | |
335 | SWAP_TARGET_AND_HOST (&sbuf, sizeof (short)); | |
336 | return sbuf; | |
337 | } | |
338 | if (len == sizeof (int)) | |
339 | { | |
340 | read_memory (memaddr, (char *)&ibuf, len); | |
341 | SWAP_TARGET_AND_HOST (&ibuf, sizeof (int)); | |
342 | return ibuf; | |
343 | } | |
344 | if (len == sizeof (lbuf)) | |
345 | { | |
346 | read_memory (memaddr, (char *)&lbuf, len); | |
347 | SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf)); | |
348 | return lbuf; | |
349 | } | |
350 | error ("Cannot handle integers of %d bytes.", len); | |
351 | return -1; /* for lint */ | |
352 | } | |
353 | \f | |
354 | /* Read or write the core file. | |
355 | ||
356 | Args are address within core file, address within gdb address-space, | |
357 | length, and a flag indicating whether to read or write. | |
358 | ||
359 | Result is a length: | |
360 | ||
361 | 0: We cannot handle this address and length. | |
362 | > 0: We have handled N bytes starting at this address. | |
363 | (If N == length, we did it all.) We might be able | |
364 | to handle more bytes beyond this length, but no | |
365 | promises. | |
366 | < 0: We cannot handle this address, but if somebody | |
367 | else handles (-N) bytes, we can start from there. | |
368 | ||
369 | The actual work is done by xfer_memory in exec.c, which we share | |
370 | in common with exec_xfer_memory(). */ | |
371 | ||
372 | static int | |
373 | core_xfer_memory (memaddr, myaddr, len, write) | |
374 | CORE_ADDR memaddr; | |
375 | char *myaddr; | |
376 | int len; | |
377 | int write; | |
378 | { | |
d0237a54 JK |
379 | int res; |
380 | res = xfer_memory (memaddr, myaddr, len, write, | |
dd3b648e | 381 | core_bfd, core_sections, core_sections_end); |
d0237a54 JK |
382 | #ifdef SOLIB_XFER_MEMORY |
383 | if (res == 0) | |
384 | res = SOLIB_XFER_MEMORY (memaddr, myaddr, len, write); | |
385 | #endif | |
386 | return res; | |
dd3b648e RP |
387 | } |
388 | \f | |
389 | /* Get the registers out of a core file. This is the machine- | |
390 | independent part. Fetch_core_registers is the machine-dependent | |
391 | part, typically implemented in the xm-file for each architecture. */ | |
392 | ||
393 | static int | |
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"); | |
402 | size = bfd_section_size (core_bfd, reg_sec); | |
403 | the_regs = alloca (size); | |
404 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, | |
405 | (unsigned)0, size)) | |
406 | { | |
407 | fetch_core_registers (the_regs, size, 0); | |
408 | } | |
409 | else | |
410 | { | |
411 | fprintf (stderr, "Couldn't fetch registers from core file: %s\n", | |
412 | bfd_errmsg (bfd_error)); | |
413 | } | |
414 | ||
415 | /* Now do it again for the float registers, if they exist. */ | |
416 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); | |
417 | if (reg_sec) { | |
418 | size = bfd_section_size (core_bfd, reg_sec); | |
419 | the_regs = alloca (size); | |
420 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, | |
421 | (unsigned)0, size)) | |
422 | { | |
423 | fetch_core_registers (the_regs, size, 2); | |
424 | } | |
425 | else | |
426 | { | |
427 | fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n", | |
428 | bfd_errmsg (bfd_error)); | |
429 | } | |
430 | } | |
431 | registers_fetched(); | |
432 | return 0; /* FIXME, what result goes here? */ | |
433 | } | |
434 | \f | |
435 | struct target_ops core_ops = { | |
436 | "core", "Local core dump file", | |
f2fc6e7a | 437 | "Use a core file as a target. Specify the filename of the core file.", |
dd3b648e RP |
438 | core_open, core_close, |
439 | child_attach, core_detach, 0, 0, /* resume, wait */ | |
440 | get_core_registers, | |
441 | 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */ | |
442 | core_xfer_memory, core_files_info, | |
443 | 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */ | |
444 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
445 | 0, 0, 0, 0, 0, /* kill, load, add_syms, call fn, lookup sym */ | |
446 | child_create_inferior, 0, /* mourn_inferior */ | |
447 | core_stratum, 0, /* next */ | |
448 | 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */ | |
449 | OPS_MAGIC, /* Always the last thing */ | |
450 | }; | |
451 | ||
452 | void | |
453 | _initialize_core() | |
454 | { | |
455 | ||
456 | add_com ("core-file", class_files, core_file_command, | |
457 | "Use FILE as core dump for examining memory and registers.\n\ | |
458 | No arg means have no core file. This command has been superseded by the\n\ | |
459 | `target core' and `detach' commands."); | |
460 | add_target (&core_ops); | |
461 | } |