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