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