Commit | Line | Data |
---|---|---|
8afd6ca5 | 1 | /* Core dump and executable file functions below target vector, for GDB. |
95b71071 | 2 | Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997 |
ba47c66a | 3 | Free Software Foundation, Inc. |
8afd6ca5 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
6c9638b4 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
8afd6ca5 RP |
20 | |
21 | #include "defs.h" | |
2b576293 | 22 | #include "gdb_string.h" |
8afd6ca5 RP |
23 | #include <errno.h> |
24 | #include <signal.h> | |
25 | #include <fcntl.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" | |
fdfa3315 | 33 | #include "gdbthread.h" |
8afd6ca5 | 34 | |
a1df8e78 FF |
35 | /* List of all available core_fns. On gdb startup, each core file register |
36 | reader calls add_core_fns() to register information on each core format it | |
37 | is prepared to read. */ | |
38 | ||
39 | static struct core_fns *core_file_fns = NULL; | |
40 | ||
62a64dde | 41 | static void core_files_info PARAMS ((struct target_ops *)); |
8afd6ca5 RP |
42 | |
43 | #ifdef SOLIB_ADD | |
62a64dde | 44 | static int solib_add_stub PARAMS ((char *)); |
8afd6ca5 RP |
45 | #endif |
46 | ||
0d2d8412 SS |
47 | static void core_open PARAMS ((char *, int)); |
48 | ||
49 | static void core_detach PARAMS ((char *, int)); | |
50 | ||
62a64dde | 51 | static void core_close PARAMS ((int)); |
8afd6ca5 | 52 | |
62a64dde | 53 | static void get_core_registers PARAMS ((int)); |
8afd6ca5 | 54 | |
b607efe7 FF |
55 | static void add_to_thread_list PARAMS ((bfd *, asection *, PTR)); |
56 | ||
57 | static int ignore PARAMS ((CORE_ADDR, char *)); | |
58 | ||
a1df8e78 FF |
59 | /* Link a new core_fns into the global core_file_fns list. Called on gdb |
60 | startup by the _initialize routine in each core file register reader, to | |
61 | register information about each format the the reader is prepared to | |
62 | handle. */ | |
63 | ||
64 | void | |
65 | add_core_fns (cf) | |
66 | struct core_fns *cf; | |
67 | { | |
68 | cf -> next = core_file_fns; | |
69 | core_file_fns = cf; | |
70 | } | |
71 | ||
72 | ||
62a64dde SS |
73 | /* Discard all vestiges of any previous core file and mark data and stack |
74 | spaces as empty. */ | |
8afd6ca5 RP |
75 | |
76 | /* ARGSUSED */ | |
77 | static void | |
78 | core_close (quitting) | |
79 | int quitting; | |
80 | { | |
9de0904c | 81 | char *name; |
62a64dde | 82 | |
62a64dde SS |
83 | if (core_bfd) |
84 | { | |
972256e7 PS |
85 | inferior_pid = 0; /* Avoid confusion from thread stuff */ |
86 | ||
62a64dde SS |
87 | name = bfd_get_filename (core_bfd); |
88 | if (!bfd_close (core_bfd)) | |
89 | warning ("cannot close \"%s\": %s", | |
90 | name, bfd_errmsg (bfd_get_error ())); | |
91 | free (name); | |
92 | core_bfd = NULL; | |
8afd6ca5 | 93 | #ifdef CLEAR_SOLIB |
62a64dde | 94 | CLEAR_SOLIB (); |
8afd6ca5 | 95 | #endif |
62a64dde SS |
96 | if (core_ops.to_sections) |
97 | { | |
98 | free ((PTR)core_ops.to_sections); | |
99 | core_ops.to_sections = NULL; | |
100 | core_ops.to_sections_end = NULL; | |
101 | } | |
8afd6ca5 | 102 | } |
8afd6ca5 RP |
103 | } |
104 | ||
105 | #ifdef SOLIB_ADD | |
842cf831 JK |
106 | /* Stub function for catch_errors around shared library hacking. FROM_TTYP |
107 | is really an int * which points to from_tty. */ | |
8afd6ca5 RP |
108 | |
109 | static int | |
842cf831 JK |
110 | solib_add_stub (from_ttyp) |
111 | char *from_ttyp; | |
8afd6ca5 | 112 | { |
9137a6f4 | 113 | SOLIB_ADD (NULL, *(int *)from_ttyp, ¤t_target); |
3c5124da | 114 | re_enable_breakpoints_in_shlibs (); |
26a859ec | 115 | return 0; |
8afd6ca5 RP |
116 | } |
117 | #endif /* SOLIB_ADD */ | |
118 | ||
d113e6b2 SG |
119 | /* Look for sections whose names start with `.reg/' so that we can extract the |
120 | list of threads in a core file. */ | |
121 | ||
122 | static void | |
7c5d526e | 123 | add_to_thread_list (abfd, asect, reg_sect_arg) |
d113e6b2 SG |
124 | bfd *abfd; |
125 | asection *asect; | |
7c5d526e | 126 | PTR reg_sect_arg; |
d113e6b2 SG |
127 | { |
128 | int thread_id; | |
4cc5b060 | 129 | asection *reg_sect = (asection *) reg_sect_arg; |
d113e6b2 SG |
130 | |
131 | if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) | |
132 | return; | |
133 | ||
134 | thread_id = atoi (bfd_section_name (abfd, asect) + 5); | |
135 | ||
136 | add_thread (thread_id); | |
137 | ||
138 | /* Warning, Will Robinson, looking at BFD private data! */ | |
139 | ||
8eff3c7f SG |
140 | if (reg_sect != NULL |
141 | && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ | |
d113e6b2 SG |
142 | inferior_pid = thread_id; /* Yes, make it current */ |
143 | } | |
144 | ||
62a64dde | 145 | /* This routine opens and sets up the core file bfd. */ |
8afd6ca5 | 146 | |
0d2d8412 | 147 | static void |
8afd6ca5 RP |
148 | core_open (filename, from_tty) |
149 | char *filename; | |
150 | int from_tty; | |
151 | { | |
152 | const char *p; | |
153 | int siggy; | |
154 | struct cleanup *old_chain; | |
155 | char *temp; | |
156 | bfd *temp_bfd; | |
157 | int ontop; | |
158 | int scratch_chan; | |
159 | ||
160 | target_preopen (from_tty); | |
161 | if (!filename) | |
162 | { | |
62a64dde | 163 | error (core_bfd ? |
8afd6ca5 RP |
164 | "No core file specified. (Use `detach' to stop debugging a core file.)" |
165 | : "No core file specified."); | |
166 | } | |
167 | ||
168 | filename = tilde_expand (filename); | |
62a64dde SS |
169 | if (filename[0] != '/') |
170 | { | |
171 | temp = concat (current_directory, "/", filename, NULL); | |
172 | free (filename); | |
173 | filename = temp; | |
174 | } | |
8afd6ca5 RP |
175 | |
176 | old_chain = make_cleanup (free, filename); | |
177 | ||
62a64dde | 178 | scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0); |
8afd6ca5 RP |
179 | if (scratch_chan < 0) |
180 | perror_with_name (filename); | |
181 | ||
0685d95f | 182 | temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan); |
8afd6ca5 | 183 | if (temp_bfd == NULL) |
62a64dde | 184 | perror_with_name (filename); |
8afd6ca5 RP |
185 | |
186 | if (!bfd_check_format (temp_bfd, bfd_core)) | |
187 | { | |
188 | /* Do it after the err msg */ | |
9de0904c JK |
189 | /* FIXME: should be checking for errors from bfd_close (for one thing, |
190 | on error it does not free all the storage associated with the | |
191 | bfd). */ | |
8afd6ca5 | 192 | make_cleanup (bfd_close, temp_bfd); |
62a64dde SS |
193 | error ("\"%s\" is not a core dump: %s", |
194 | filename, bfd_errmsg (bfd_get_error ())); | |
8afd6ca5 RP |
195 | } |
196 | ||
197 | /* Looks semi-reasonable. Toss the old core file and work on the new. */ | |
198 | ||
199 | discard_cleanups (old_chain); /* Don't free filename any more */ | |
200 | unpush_target (&core_ops); | |
201 | core_bfd = temp_bfd; | |
202 | old_chain = make_cleanup (core_close, core_bfd); | |
203 | ||
204 | validate_files (); | |
205 | ||
206 | /* Find the data section */ | |
207 | if (build_section_table (core_bfd, &core_ops.to_sections, | |
208 | &core_ops.to_sections_end)) | |
62a64dde SS |
209 | error ("\"%s\": Can't find sections: %s", |
210 | bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); | |
8afd6ca5 RP |
211 | |
212 | ontop = !push_target (&core_ops); | |
213 | discard_cleanups (old_chain); | |
214 | ||
215 | p = bfd_core_file_failing_command (core_bfd); | |
216 | if (p) | |
217 | printf_filtered ("Core was generated by `%s'.\n", p); | |
218 | ||
219 | siggy = bfd_core_file_failing_signal (core_bfd); | |
220 | if (siggy > 0) | |
221 | printf_filtered ("Program terminated with signal %d, %s.\n", siggy, | |
62a64dde | 222 | safe_strsignal (siggy)); |
8afd6ca5 | 223 | |
d113e6b2 SG |
224 | /* Build up thread list from BFD sections. */ |
225 | ||
226 | init_thread_list (); | |
227 | bfd_map_over_sections (core_bfd, add_to_thread_list, | |
228 | bfd_get_section_by_name (core_bfd, ".reg")); | |
229 | ||
62a64dde SS |
230 | if (ontop) |
231 | { | |
232 | /* Fetch all registers from core file. */ | |
233 | target_fetch_registers (-1); | |
8afd6ca5 | 234 | |
62a64dde | 235 | /* Add symbols and section mappings for any shared libraries. */ |
8afd6ca5 | 236 | #ifdef SOLIB_ADD |
62a64dde SS |
237 | catch_errors (solib_add_stub, &from_tty, (char *)0, |
238 | RETURN_MASK_ALL); | |
8afd6ca5 RP |
239 | #endif |
240 | ||
62a64dde SS |
241 | /* Now, set up the frame cache, and print the top of stack. */ |
242 | flush_cached_frames (); | |
243 | select_frame (get_current_frame (), 0); | |
244 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
245 | } | |
246 | else | |
247 | { | |
248 | warning ( | |
8afd6ca5 | 249 | "you won't be able to access this core file until you terminate\n\ |
cad1498f | 250 | your %s; do ``info files''", target_longname); |
62a64dde | 251 | } |
8afd6ca5 RP |
252 | } |
253 | ||
0d2d8412 | 254 | static void |
8afd6ca5 RP |
255 | core_detach (args, from_tty) |
256 | char *args; | |
257 | int from_tty; | |
258 | { | |
259 | if (args) | |
260 | error ("Too many arguments"); | |
261 | unpush_target (&core_ops); | |
c5198d93 | 262 | reinit_frame_cache (); |
8afd6ca5 RP |
263 | if (from_tty) |
264 | printf_filtered ("No core file now.\n"); | |
265 | } | |
266 | ||
267 | /* Get the registers out of a core file. This is the machine- | |
268 | independent part. Fetch_core_registers is the machine-dependent | |
269 | part, typically implemented in the xm-file for each architecture. */ | |
270 | ||
271 | /* We just get all the registers, so we don't use regno. */ | |
62a64dde | 272 | |
8afd6ca5 RP |
273 | /* ARGSUSED */ |
274 | static void | |
275 | get_core_registers (regno) | |
276 | int regno; | |
277 | { | |
278 | sec_ptr reg_sec; | |
279 | unsigned size; | |
280 | char *the_regs; | |
d113e6b2 | 281 | char secname[10]; |
a1df8e78 | 282 | enum bfd_flavour our_flavour = bfd_get_flavour (core_bfd); |
95b71071 | 283 | struct core_fns *cf = NULL; |
a1df8e78 FF |
284 | |
285 | if (core_file_fns == NULL) | |
286 | { | |
287 | fprintf_filtered (gdb_stderr, | |
288 | "Can't fetch registers from this type of core file\n"); | |
289 | return; | |
290 | } | |
d113e6b2 SG |
291 | |
292 | /* Thread support. If inferior_pid is non-zero, then we have found a core | |
293 | file with threads (or multiple processes). In that case, we need to | |
294 | use the appropriate register section, else we just use `.reg'. */ | |
295 | ||
296 | /* XXX - same thing needs to be done for floating-point (.reg2) sections. */ | |
297 | ||
298 | if (inferior_pid) | |
299 | sprintf (secname, ".reg/%d", inferior_pid); | |
300 | else | |
301 | strcpy (secname, ".reg"); | |
8afd6ca5 | 302 | |
d113e6b2 | 303 | reg_sec = bfd_get_section_by_name (core_bfd, secname); |
62a64dde SS |
304 | if (!reg_sec) |
305 | goto cant; | |
8afd6ca5 RP |
306 | size = bfd_section_size (core_bfd, reg_sec); |
307 | the_regs = alloca (size); | |
a1df8e78 FF |
308 | /* Look for the core functions that match this flavor. Default to the |
309 | first one if nothing matches. */ | |
310 | for (cf = core_file_fns; cf != NULL; cf = cf -> next) | |
311 | { | |
312 | if (our_flavour == cf -> core_flavour) | |
313 | { | |
314 | break; | |
315 | } | |
316 | } | |
317 | if (cf == NULL) | |
318 | { | |
319 | cf = core_file_fns; | |
320 | } | |
321 | if (cf != NULL && | |
322 | bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size) && | |
323 | cf -> core_read_registers != NULL) | |
8afd6ca5 | 324 | { |
a1df8e78 FF |
325 | (cf -> core_read_registers (the_regs, size, 0, |
326 | (unsigned) bfd_section_vma (abfd,reg_sec))); | |
8afd6ca5 RP |
327 | } |
328 | else | |
329 | { | |
330 | cant: | |
62a64dde SS |
331 | fprintf_filtered (gdb_stderr, |
332 | "Couldn't fetch registers from core file: %s\n", | |
333 | bfd_errmsg (bfd_get_error ())); | |
8afd6ca5 RP |
334 | } |
335 | ||
336 | /* Now do it again for the float registers, if they exist. */ | |
337 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); | |
62a64dde SS |
338 | if (reg_sec) |
339 | { | |
340 | size = bfd_section_size (core_bfd, reg_sec); | |
341 | the_regs = alloca (size); | |
a1df8e78 FF |
342 | if (cf != NULL && |
343 | bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size) && | |
344 | cf -> core_read_registers != NULL) | |
62a64dde | 345 | { |
a1df8e78 FF |
346 | (cf -> core_read_registers (the_regs, size, 2, |
347 | (unsigned) bfd_section_vma (abfd,reg_sec))); | |
62a64dde SS |
348 | } |
349 | else | |
350 | { | |
351 | fprintf_filtered (gdb_stderr, | |
352 | "Couldn't fetch register set 2 from core file: %s\n", | |
353 | bfd_errmsg (bfd_get_error ())); | |
354 | } | |
355 | } | |
356 | registers_fetched (); | |
8afd6ca5 RP |
357 | } |
358 | ||
359 | static void | |
360 | core_files_info (t) | |
361 | struct target_ops *t; | |
362 | { | |
363 | print_section_info (t, core_bfd); | |
364 | } | |
365 | \f | |
f47e56c9 | 366 | /* If mourn is being called in all the right places, this could be say |
cf3e377e | 367 | `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ |
f47e56c9 JK |
368 | |
369 | static int | |
370 | ignore (addr, contents) | |
371 | CORE_ADDR addr; | |
372 | char *contents; | |
373 | { | |
100f92e2 | 374 | return 0; |
f47e56c9 JK |
375 | } |
376 | ||
8afd6ca5 | 377 | struct target_ops core_ops = { |
78b459a7 SG |
378 | "core", /* to_shortname */ |
379 | "Local core dump file", /* to_longname */ | |
380 | "Use a core file as a target. Specify the filename of the core file.", /* to_doc */ | |
381 | core_open, /* to_open */ | |
382 | core_close, /* to_close */ | |
383 | find_default_attach, /* to_attach */ | |
384 | core_detach, /* to_detach */ | |
385 | 0, /* to_resume */ | |
386 | 0, /* to_wait */ | |
387 | get_core_registers, /* to_fetch_registers */ | |
388 | 0, /* to_store_registers */ | |
389 | 0, /* to_prepare_to_store */ | |
390 | xfer_memory, /* to_xfer_memory */ | |
391 | core_files_info, /* to_files_info */ | |
392 | ignore, /* to_insert_breakpoint */ | |
393 | ignore, /* to_remove_breakpoint */ | |
394 | 0, /* to_terminal_init */ | |
395 | 0, /* to_terminal_inferior */ | |
396 | 0, /* to_terminal_ours_for_output */ | |
397 | 0, /* to_terminal_ours */ | |
398 | 0, /* to_terminal_info */ | |
399 | 0, /* to_kill */ | |
400 | 0, /* to_load */ | |
401 | 0, /* to_lookup_symbol */ | |
402 | find_default_create_inferior, /* to_create_inferior */ | |
403 | 0, /* to_mourn_inferior */ | |
404 | 0, /* to_can_run */ | |
405 | 0, /* to_notice_signals */ | |
43fc25c8 | 406 | 0, /* to_thread_alive */ |
78b459a7 SG |
407 | 0, /* to_stop */ |
408 | core_stratum, /* to_stratum */ | |
409 | 0, /* to_next */ | |
410 | 0, /* to_has_all_memory */ | |
411 | 1, /* to_has_memory */ | |
412 | 1, /* to_has_stack */ | |
413 | 1, /* to_has_registers */ | |
414 | 0, /* to_has_execution */ | |
415 | 0, /* to_sections */ | |
416 | 0, /* to_sections_end */ | |
417 | OPS_MAGIC, /* to_magic */ | |
8afd6ca5 RP |
418 | }; |
419 | ||
420 | void | |
421 | _initialize_corelow() | |
422 | { | |
423 | add_target (&core_ops); | |
424 | } |