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