Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Core dump and executable file functions below target vector, for GDB. |
2 | Copyright 1986, 87, 89, 91, 92, 93, 94, 95, 96, 97, 1998 | |
3 | Free Software Foundation, Inc. | |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "gdb_string.h" | |
24 | #include <errno.h> | |
25 | #include <signal.h> | |
26 | #include <fcntl.h> | |
c5aa993b | 27 | #include "frame.h" /* required by inferior.h */ |
c906108c SS |
28 | #include "inferior.h" |
29 | #include "symtab.h" | |
30 | #include "command.h" | |
31 | #include "bfd.h" | |
32 | #include "target.h" | |
33 | #include "gdbcore.h" | |
34 | #include "gdbthread.h" | |
35 | ||
36 | /* List of all available core_fns. On gdb startup, each core file register | |
37 | reader calls add_core_fns() to register information on each core format it | |
38 | is prepared to read. */ | |
39 | ||
40 | static struct core_fns *core_file_fns = NULL; | |
41 | ||
42 | static void core_files_info PARAMS ((struct target_ops *)); | |
43 | ||
44 | #ifdef SOLIB_ADD | |
45 | static int solib_add_stub PARAMS ((PTR)); | |
46 | #endif | |
47 | ||
48 | static void core_open PARAMS ((char *, int)); | |
49 | ||
50 | static void core_detach PARAMS ((char *, int)); | |
51 | ||
52 | static void core_close PARAMS ((int)); | |
53 | ||
54 | static void get_core_registers PARAMS ((int)); | |
55 | ||
56 | static void add_to_thread_list PARAMS ((bfd *, asection *, PTR)); | |
57 | ||
58 | static int ignore PARAMS ((CORE_ADDR, char *)); | |
59 | ||
60 | static char *core_file_to_sym_file PARAMS ((char *)); | |
61 | ||
62 | static int core_file_thread_alive PARAMS ((int tid)); | |
63 | ||
64 | static void init_core_ops PARAMS ((void)); | |
65 | ||
66 | void _initialize_corelow PARAMS ((void)); | |
67 | ||
68 | struct target_ops core_ops; | |
69 | ||
70 | /* Link a new core_fns into the global core_file_fns list. Called on gdb | |
71 | startup by the _initialize routine in each core file register reader, to | |
72 | register information about each format the the reader is prepared to | |
73 | handle. */ | |
74 | ||
75 | void | |
76 | add_core_fns (cf) | |
77 | struct core_fns *cf; | |
78 | { | |
c5aa993b | 79 | cf->next = core_file_fns; |
c906108c SS |
80 | core_file_fns = cf; |
81 | } | |
82 | ||
83 | ||
84 | /* Discard all vestiges of any previous core file and mark data and stack | |
85 | spaces as empty. */ | |
86 | ||
87 | /* ARGSUSED */ | |
88 | static void | |
89 | core_close (quitting) | |
90 | int quitting; | |
91 | { | |
92 | char *name; | |
93 | ||
94 | if (core_bfd) | |
95 | { | |
96 | inferior_pid = 0; /* Avoid confusion from thread stuff */ | |
97 | ||
7a292a7a | 98 | /* Clear out solib state while the bfd is still open. See |
c5aa993b | 99 | comments in clear_solib in solib.c. */ |
7a292a7a SS |
100 | #ifdef CLEAR_SOLIB |
101 | CLEAR_SOLIB (); | |
102 | #endif | |
103 | ||
c906108c SS |
104 | name = bfd_get_filename (core_bfd); |
105 | if (!bfd_close (core_bfd)) | |
106 | warning ("cannot close \"%s\": %s", | |
107 | name, bfd_errmsg (bfd_get_error ())); | |
108 | free (name); | |
109 | core_bfd = NULL; | |
c906108c SS |
110 | if (core_ops.to_sections) |
111 | { | |
c5aa993b | 112 | free ((PTR) core_ops.to_sections); |
c906108c SS |
113 | core_ops.to_sections = NULL; |
114 | core_ops.to_sections_end = NULL; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | #ifdef SOLIB_ADD | |
120 | /* Stub function for catch_errors around shared library hacking. FROM_TTYP | |
121 | is really an int * which points to from_tty. */ | |
122 | ||
c5aa993b | 123 | static int |
c906108c SS |
124 | solib_add_stub (from_ttyp) |
125 | PTR from_ttyp; | |
126 | { | |
c5aa993b | 127 | SOLIB_ADD (NULL, *(int *) from_ttyp, ¤t_target); |
c906108c SS |
128 | re_enable_breakpoints_in_shlibs (); |
129 | return 0; | |
130 | } | |
131 | #endif /* SOLIB_ADD */ | |
132 | ||
133 | /* Look for sections whose names start with `.reg/' so that we can extract the | |
134 | list of threads in a core file. */ | |
135 | ||
136 | static void | |
137 | add_to_thread_list (abfd, asect, reg_sect_arg) | |
138 | bfd *abfd; | |
139 | asection *asect; | |
140 | PTR reg_sect_arg; | |
141 | { | |
142 | int thread_id; | |
143 | asection *reg_sect = (asection *) reg_sect_arg; | |
144 | ||
145 | if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) | |
146 | return; | |
147 | ||
148 | thread_id = atoi (bfd_section_name (abfd, asect) + 5); | |
149 | ||
150 | add_thread (thread_id); | |
151 | ||
152 | /* Warning, Will Robinson, looking at BFD private data! */ | |
153 | ||
154 | if (reg_sect != NULL | |
c5aa993b | 155 | && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ |
c906108c SS |
156 | inferior_pid = thread_id; /* Yes, make it current */ |
157 | } | |
158 | ||
159 | /* This routine opens and sets up the core file bfd. */ | |
160 | ||
161 | static void | |
162 | core_open (filename, from_tty) | |
163 | char *filename; | |
164 | int from_tty; | |
165 | { | |
166 | const char *p; | |
167 | int siggy; | |
168 | struct cleanup *old_chain; | |
169 | char *temp; | |
170 | bfd *temp_bfd; | |
171 | int ontop; | |
172 | int scratch_chan; | |
173 | ||
174 | target_preopen (from_tty); | |
175 | if (!filename) | |
176 | { | |
c5aa993b JM |
177 | error (core_bfd ? |
178 | "No core file specified. (Use `detach' to stop debugging a core file.)" | |
179 | : "No core file specified."); | |
c906108c SS |
180 | } |
181 | ||
182 | filename = tilde_expand (filename); | |
183 | if (filename[0] != '/') | |
184 | { | |
185 | temp = concat (current_directory, "/", filename, NULL); | |
186 | free (filename); | |
187 | filename = temp; | |
188 | } | |
189 | ||
190 | old_chain = make_cleanup (free, filename); | |
191 | ||
192 | scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0); | |
193 | if (scratch_chan < 0) | |
194 | perror_with_name (filename); | |
195 | ||
196 | temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan); | |
197 | if (temp_bfd == NULL) | |
198 | perror_with_name (filename); | |
199 | ||
200 | if (!bfd_check_format (temp_bfd, bfd_core)) | |
201 | { | |
202 | /* Do it after the err msg */ | |
203 | /* FIXME: should be checking for errors from bfd_close (for one thing, | |
c5aa993b JM |
204 | on error it does not free all the storage associated with the |
205 | bfd). */ | |
c906108c SS |
206 | make_cleanup ((make_cleanup_func) bfd_close, temp_bfd); |
207 | error ("\"%s\" is not a core dump: %s", | |
208 | filename, bfd_errmsg (bfd_get_error ())); | |
209 | } | |
210 | ||
211 | /* Looks semi-reasonable. Toss the old core file and work on the new. */ | |
212 | ||
c5aa993b | 213 | discard_cleanups (old_chain); /* Don't free filename any more */ |
c906108c SS |
214 | unpush_target (&core_ops); |
215 | core_bfd = temp_bfd; | |
216 | old_chain = make_cleanup ((make_cleanup_func) core_close, core_bfd); | |
217 | ||
218 | validate_files (); | |
219 | ||
220 | /* Find the data section */ | |
221 | if (build_section_table (core_bfd, &core_ops.to_sections, | |
222 | &core_ops.to_sections_end)) | |
223 | error ("\"%s\": Can't find sections: %s", | |
224 | bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); | |
225 | ||
226 | ontop = !push_target (&core_ops); | |
227 | discard_cleanups (old_chain); | |
228 | ||
229 | p = bfd_core_file_failing_command (core_bfd); | |
230 | if (p) | |
231 | printf_filtered ("Core was generated by `%s'.\n", p); | |
232 | ||
233 | siggy = bfd_core_file_failing_signal (core_bfd); | |
234 | if (siggy > 0) | |
235 | printf_filtered ("Program terminated with signal %d, %s.\n", siggy, | |
236 | safe_strsignal (siggy)); | |
237 | ||
238 | /* Build up thread list from BFD sections. */ | |
239 | ||
240 | init_thread_list (); | |
241 | bfd_map_over_sections (core_bfd, add_to_thread_list, | |
242 | bfd_get_section_by_name (core_bfd, ".reg")); | |
243 | ||
244 | if (ontop) | |
245 | { | |
246 | /* Fetch all registers from core file. */ | |
247 | target_fetch_registers (-1); | |
248 | ||
249 | /* Add symbols and section mappings for any shared libraries. */ | |
250 | #ifdef SOLIB_ADD | |
c5aa993b | 251 | catch_errors (solib_add_stub, &from_tty, (char *) 0, |
c906108c SS |
252 | RETURN_MASK_ALL); |
253 | #endif | |
254 | ||
255 | /* Now, set up the frame cache, and print the top of stack. */ | |
256 | flush_cached_frames (); | |
257 | select_frame (get_current_frame (), 0); | |
258 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
259 | } | |
260 | else | |
261 | { | |
262 | warning ( | |
c5aa993b | 263 | "you won't be able to access this core file until you terminate\n\ |
c906108c SS |
264 | your %s; do ``info files''", target_longname); |
265 | } | |
266 | } | |
267 | ||
268 | static void | |
269 | core_detach (args, from_tty) | |
270 | char *args; | |
271 | int from_tty; | |
272 | { | |
273 | if (args) | |
274 | error ("Too many arguments"); | |
275 | unpush_target (&core_ops); | |
276 | reinit_frame_cache (); | |
277 | if (from_tty) | |
278 | printf_filtered ("No core file now.\n"); | |
279 | } | |
280 | ||
281 | /* Get the registers out of a core file. This is the machine- | |
282 | independent part. Fetch_core_registers is the machine-dependent | |
283 | part, typically implemented in the xm-file for each architecture. */ | |
284 | ||
285 | /* We just get all the registers, so we don't use regno. */ | |
286 | ||
287 | /* ARGSUSED */ | |
288 | static void | |
289 | get_core_registers (regno) | |
290 | int regno; | |
291 | { | |
292 | sec_ptr reg_sec; | |
293 | unsigned size; | |
294 | char *the_regs; | |
295 | char secname[30]; | |
296 | enum bfd_flavour our_flavour = bfd_get_flavour (core_bfd); | |
297 | struct core_fns *cf = NULL; | |
298 | ||
299 | if (core_file_fns == NULL) | |
300 | { | |
301 | fprintf_filtered (gdb_stderr, | |
c5aa993b | 302 | "Can't fetch registers from this type of core file\n"); |
c906108c SS |
303 | return; |
304 | } | |
305 | ||
306 | /* Thread support. If inferior_pid is non-zero, then we have found a core | |
307 | file with threads (or multiple processes). In that case, we need to | |
308 | use the appropriate register section, else we just use `.reg'. */ | |
309 | ||
310 | /* XXX - same thing needs to be done for floating-point (.reg2) sections. */ | |
311 | ||
312 | if (inferior_pid) | |
313 | sprintf (secname, ".reg/%d", inferior_pid); | |
314 | else | |
315 | strcpy (secname, ".reg"); | |
316 | ||
317 | reg_sec = bfd_get_section_by_name (core_bfd, secname); | |
318 | if (!reg_sec) | |
319 | goto cant; | |
320 | size = bfd_section_size (core_bfd, reg_sec); | |
321 | the_regs = alloca (size); | |
322 | /* Look for the core functions that match this flavor. Default to the | |
323 | first one if nothing matches. */ | |
c5aa993b | 324 | for (cf = core_file_fns; cf != NULL; cf = cf->next) |
c906108c | 325 | { |
c5aa993b | 326 | if (our_flavour == cf->core_flavour) |
c906108c SS |
327 | { |
328 | break; | |
329 | } | |
330 | } | |
331 | if (cf == NULL) | |
332 | { | |
333 | cf = core_file_fns; | |
334 | } | |
335 | if (cf != NULL && | |
c5aa993b JM |
336 | bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr) 0, size) && |
337 | cf->core_read_registers != NULL) | |
c906108c | 338 | { |
c5aa993b JM |
339 | (cf->core_read_registers (the_regs, size, 0, |
340 | (unsigned) bfd_section_vma (abfd, reg_sec))); | |
c906108c SS |
341 | } |
342 | else | |
343 | { | |
c5aa993b | 344 | cant: |
c906108c SS |
345 | fprintf_filtered (gdb_stderr, |
346 | "Couldn't fetch registers from core file: %s\n", | |
347 | bfd_errmsg (bfd_get_error ())); | |
348 | } | |
349 | ||
350 | /* Now do it again for the float registers, if they exist. */ | |
351 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); | |
352 | if (reg_sec) | |
353 | { | |
354 | size = bfd_section_size (core_bfd, reg_sec); | |
355 | the_regs = alloca (size); | |
356 | if (cf != NULL && | |
c5aa993b JM |
357 | bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr) 0, size) && |
358 | cf->core_read_registers != NULL) | |
c906108c | 359 | { |
c5aa993b JM |
360 | (cf->core_read_registers (the_regs, size, 2, |
361 | (unsigned) bfd_section_vma (abfd, reg_sec))); | |
c906108c SS |
362 | } |
363 | else | |
364 | { | |
c5aa993b JM |
365 | fprintf_filtered (gdb_stderr, |
366 | "Couldn't fetch register set 2 from core file: %s\n", | |
c906108c SS |
367 | bfd_errmsg (bfd_get_error ())); |
368 | } | |
369 | } | |
370 | registers_fetched (); | |
371 | } | |
372 | ||
373 | static char * | |
374 | core_file_to_sym_file (core) | |
c5aa993b | 375 | char *core; |
c906108c | 376 | { |
c5aa993b JM |
377 | CONST char *failing_command; |
378 | char *p; | |
379 | char *temp; | |
380 | bfd *temp_bfd; | |
381 | int scratch_chan; | |
c906108c | 382 | |
c5aa993b | 383 | if (!core) |
c906108c SS |
384 | error ("No core file specified."); |
385 | ||
386 | core = tilde_expand (core); | |
387 | if (core[0] != '/') | |
388 | { | |
389 | temp = concat (current_directory, "/", core, NULL); | |
390 | core = temp; | |
391 | } | |
392 | ||
393 | scratch_chan = open (core, write_files ? O_RDWR : O_RDONLY, 0); | |
394 | if (scratch_chan < 0) | |
395 | perror_with_name (core); | |
396 | ||
397 | temp_bfd = bfd_fdopenr (core, gnutarget, scratch_chan); | |
398 | if (temp_bfd == NULL) | |
399 | perror_with_name (core); | |
400 | ||
401 | if (!bfd_check_format (temp_bfd, bfd_core)) | |
402 | { | |
403 | /* Do it after the err msg */ | |
404 | /* FIXME: should be checking for errors from bfd_close (for one thing, | |
c5aa993b JM |
405 | on error it does not free all the storage associated with the |
406 | bfd). */ | |
c906108c SS |
407 | make_cleanup ((make_cleanup_func) bfd_close, temp_bfd); |
408 | error ("\"%s\" is not a core dump: %s", | |
409 | core, bfd_errmsg (bfd_get_error ())); | |
410 | } | |
411 | ||
412 | /* Find the data section */ | |
413 | if (build_section_table (temp_bfd, &core_ops.to_sections, | |
414 | &core_ops.to_sections_end)) | |
415 | error ("\"%s\": Can't find sections: %s", | |
416 | bfd_get_filename (temp_bfd), bfd_errmsg (bfd_get_error ())); | |
417 | ||
418 | failing_command = bfd_core_file_failing_command (temp_bfd); | |
419 | ||
420 | bfd_close (temp_bfd); | |
421 | ||
422 | /* If we found a filename, remember that it is probably saved | |
423 | relative to the executable that created it. If working directory | |
424 | isn't there now, we may not be able to find the executable. Rather | |
425 | than trying to be sauve about finding it, just check if the file | |
426 | exists where we are now. If not, then punt and tell our client | |
427 | we couldn't find the sym file. | |
c5aa993b | 428 | */ |
c906108c SS |
429 | p = (char *) failing_command; |
430 | if ((p != NULL) && (access (p, F_OK) != 0)) | |
431 | p = NULL; | |
432 | ||
433 | return p; | |
434 | } | |
435 | ||
436 | static void | |
437 | core_files_info (t) | |
c5aa993b | 438 | struct target_ops *t; |
c906108c SS |
439 | { |
440 | print_section_info (t, core_bfd); | |
441 | } | |
442 | \f | |
443 | /* If mourn is being called in all the right places, this could be say | |
444 | `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ | |
445 | ||
446 | static int | |
447 | ignore (addr, contents) | |
448 | CORE_ADDR addr; | |
449 | char *contents; | |
450 | { | |
451 | return 0; | |
452 | } | |
453 | ||
454 | ||
455 | /* Okay, let's be honest: threads gleaned from a core file aren't | |
456 | exactly lively, are they? On the other hand, if we don't claim | |
457 | that each & every one is alive, then we don't get any of them | |
458 | to appear in an "info thread" command, which is quite a useful | |
459 | behaviour. | |
c5aa993b | 460 | */ |
c906108c SS |
461 | static int |
462 | core_file_thread_alive (tid) | |
c5aa993b | 463 | int tid; |
c906108c SS |
464 | { |
465 | return 1; | |
466 | } | |
467 | ||
468 | /* Fill in core_ops with its defined operations and properties. */ | |
469 | ||
470 | static void | |
471 | init_core_ops () | |
472 | { | |
473 | core_ops.to_shortname = "core"; | |
474 | core_ops.to_longname = "Local core dump file"; | |
475 | core_ops.to_doc = | |
476 | "Use a core file as a target. Specify the filename of the core file."; | |
477 | core_ops.to_open = core_open; | |
478 | core_ops.to_close = core_close; | |
479 | core_ops.to_attach = find_default_attach; | |
480 | core_ops.to_require_attach = find_default_require_attach; | |
481 | core_ops.to_detach = core_detach; | |
482 | core_ops.to_require_detach = find_default_require_detach; | |
483 | core_ops.to_fetch_registers = get_core_registers; | |
484 | core_ops.to_xfer_memory = xfer_memory; | |
485 | core_ops.to_files_info = core_files_info; | |
486 | core_ops.to_insert_breakpoint = ignore; | |
487 | core_ops.to_remove_breakpoint = ignore; | |
488 | core_ops.to_create_inferior = find_default_create_inferior; | |
489 | core_ops.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior; | |
490 | core_ops.to_thread_alive = core_file_thread_alive; | |
491 | core_ops.to_core_file_to_sym_file = core_file_to_sym_file; | |
492 | core_ops.to_stratum = core_stratum; | |
493 | core_ops.to_has_memory = 1; | |
494 | core_ops.to_has_stack = 1; | |
495 | core_ops.to_has_registers = 1; | |
c5aa993b | 496 | core_ops.to_magic = OPS_MAGIC; |
c906108c SS |
497 | } |
498 | ||
499 | /* non-zero if we should not do the add_target call in | |
500 | _initialize_corelow; not initialized (i.e., bss) so that | |
501 | the target can initialize it (i.e., data) if appropriate. | |
502 | This needs to be set at compile time because we don't know | |
503 | for sure whether the target's initialize routine is called | |
504 | before us or after us. */ | |
505 | int coreops_suppress_target; | |
506 | ||
507 | void | |
508 | _initialize_corelow () | |
509 | { | |
510 | init_core_ops (); | |
511 | ||
512 | if (!coreops_suppress_target) | |
513 | add_target (&core_ops); | |
514 | } |