Commit | Line | Data |
---|---|---|
8afd6ca5 RP |
1 | /* Core dump and executable file functions below target vector, for GDB. |
2 | Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include <errno.h> | |
22 | #include <signal.h> | |
23 | #include <fcntl.h> | |
24 | #include "frame.h" /* required by inferior.h */ | |
25 | #include "inferior.h" | |
26 | #include "symtab.h" | |
27 | #include "command.h" | |
28 | #include "bfd.h" | |
29 | #include "target.h" | |
30 | #include "gdbcore.h" | |
100f92e2 | 31 | #include "thread.h" |
8afd6ca5 RP |
32 | |
33 | static void | |
34 | core_files_info PARAMS ((struct target_ops *)); | |
35 | ||
36 | #ifdef SOLIB_ADD | |
37 | static int | |
38 | solib_add_stub PARAMS ((char *)); | |
39 | #endif | |
40 | ||
41 | static void | |
42 | core_close PARAMS ((int)); | |
43 | ||
44 | static void | |
45 | get_core_registers PARAMS ((int)); | |
46 | ||
47 | /* Discard all vestiges of any previous core file | |
48 | and mark data and stack spaces as empty. */ | |
49 | ||
50 | /* ARGSUSED */ | |
51 | static void | |
52 | core_close (quitting) | |
53 | int quitting; | |
54 | { | |
d113e6b2 SG |
55 | inferior_pid = 0; /* Avoid confusion from thread stuff */ |
56 | ||
8afd6ca5 RP |
57 | if (core_bfd) { |
58 | free (bfd_get_filename (core_bfd)); | |
59 | bfd_close (core_bfd); | |
60 | core_bfd = NULL; | |
61 | #ifdef CLEAR_SOLIB | |
62 | CLEAR_SOLIB (); | |
63 | #endif | |
64 | if (core_ops.to_sections) { | |
65 | free ((PTR)core_ops.to_sections); | |
66 | core_ops.to_sections = NULL; | |
67 | core_ops.to_sections_end = NULL; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | #ifdef SOLIB_ADD | |
73 | /* Stub function for catch_errors around shared library hacking. */ | |
74 | ||
75 | static int | |
76 | solib_add_stub (from_tty) | |
77 | char *from_tty; | |
78 | { | |
79 | SOLIB_ADD (NULL, (int)from_tty, &core_ops); | |
80 | return 0; | |
81 | } | |
82 | #endif /* SOLIB_ADD */ | |
83 | ||
d113e6b2 SG |
84 | /* Look for sections whose names start with `.reg/' so that we can extract the |
85 | list of threads in a core file. */ | |
86 | ||
87 | static void | |
7c5d526e | 88 | add_to_thread_list (abfd, asect, reg_sect_arg) |
d113e6b2 SG |
89 | bfd *abfd; |
90 | asection *asect; | |
7c5d526e | 91 | PTR reg_sect_arg; |
d113e6b2 SG |
92 | { |
93 | int thread_id; | |
4cc5b060 | 94 | asection *reg_sect = (asection *) reg_sect_arg; |
d113e6b2 SG |
95 | |
96 | if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) | |
97 | return; | |
98 | ||
99 | thread_id = atoi (bfd_section_name (abfd, asect) + 5); | |
100 | ||
101 | add_thread (thread_id); | |
102 | ||
103 | /* Warning, Will Robinson, looking at BFD private data! */ | |
104 | ||
105 | if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */ | |
106 | inferior_pid = thread_id; /* Yes, make it current */ | |
107 | } | |
108 | ||
8afd6ca5 RP |
109 | /* This routine opens and sets up the core file bfd */ |
110 | ||
111 | void | |
112 | core_open (filename, from_tty) | |
113 | char *filename; | |
114 | int from_tty; | |
115 | { | |
116 | const char *p; | |
117 | int siggy; | |
118 | struct cleanup *old_chain; | |
119 | char *temp; | |
120 | bfd *temp_bfd; | |
121 | int ontop; | |
122 | int scratch_chan; | |
123 | ||
124 | target_preopen (from_tty); | |
125 | if (!filename) | |
126 | { | |
127 | error (core_bfd? | |
128 | "No core file specified. (Use `detach' to stop debugging a core file.)" | |
129 | : "No core file specified."); | |
130 | } | |
131 | ||
132 | filename = tilde_expand (filename); | |
133 | if (filename[0] != '/') { | |
134 | temp = concat (current_directory, "/", filename, NULL); | |
135 | free (filename); | |
136 | filename = temp; | |
137 | } | |
138 | ||
139 | old_chain = make_cleanup (free, filename); | |
140 | ||
141 | scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0); | |
142 | if (scratch_chan < 0) | |
143 | perror_with_name (filename); | |
144 | ||
0685d95f | 145 | temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan); |
8afd6ca5 RP |
146 | if (temp_bfd == NULL) |
147 | { | |
148 | perror_with_name (filename); | |
149 | } | |
150 | ||
151 | if (!bfd_check_format (temp_bfd, bfd_core)) | |
152 | { | |
153 | /* Do it after the err msg */ | |
154 | make_cleanup (bfd_close, temp_bfd); | |
155 | error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error)); | |
156 | } | |
157 | ||
158 | /* Looks semi-reasonable. Toss the old core file and work on the new. */ | |
159 | ||
160 | discard_cleanups (old_chain); /* Don't free filename any more */ | |
161 | unpush_target (&core_ops); | |
162 | core_bfd = temp_bfd; | |
163 | old_chain = make_cleanup (core_close, core_bfd); | |
164 | ||
165 | validate_files (); | |
166 | ||
167 | /* Find the data section */ | |
168 | if (build_section_table (core_bfd, &core_ops.to_sections, | |
169 | &core_ops.to_sections_end)) | |
170 | error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd), | |
171 | bfd_errmsg (bfd_error)); | |
172 | ||
173 | ontop = !push_target (&core_ops); | |
174 | discard_cleanups (old_chain); | |
175 | ||
176 | p = bfd_core_file_failing_command (core_bfd); | |
177 | if (p) | |
178 | printf_filtered ("Core was generated by `%s'.\n", p); | |
179 | ||
180 | siggy = bfd_core_file_failing_signal (core_bfd); | |
181 | if (siggy > 0) | |
182 | printf_filtered ("Program terminated with signal %d, %s.\n", siggy, | |
183 | safe_strsignal (siggy)); | |
184 | ||
d113e6b2 SG |
185 | /* Build up thread list from BFD sections. */ |
186 | ||
187 | init_thread_list (); | |
188 | bfd_map_over_sections (core_bfd, add_to_thread_list, | |
189 | bfd_get_section_by_name (core_bfd, ".reg")); | |
190 | ||
8afd6ca5 RP |
191 | if (ontop) { |
192 | /* Fetch all registers from core file */ | |
193 | target_fetch_registers (-1); | |
194 | ||
195 | /* Add symbols and section mappings for any shared libraries */ | |
196 | #ifdef SOLIB_ADD | |
9748446f JK |
197 | catch_errors (solib_add_stub, (char *)from_tty, (char *)0, |
198 | RETURN_MASK_ALL); | |
8afd6ca5 RP |
199 | #endif |
200 | ||
201 | /* Now, set up the frame cache, and print the top of stack */ | |
85e07872 | 202 | set_current_frame (create_new_frame (read_fp (), |
8afd6ca5 RP |
203 | read_pc ())); |
204 | select_frame (get_current_frame (), 0); | |
205 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
206 | } else { | |
207 | warning ( | |
208 | "you won't be able to access this core file until you terminate\n\ | |
209 | your %s; do ``info files''", current_target->to_longname); | |
210 | } | |
211 | } | |
212 | ||
213 | void | |
214 | core_detach (args, from_tty) | |
215 | char *args; | |
216 | int from_tty; | |
217 | { | |
218 | if (args) | |
219 | error ("Too many arguments"); | |
220 | unpush_target (&core_ops); | |
c5198d93 | 221 | reinit_frame_cache (); |
8afd6ca5 RP |
222 | if (from_tty) |
223 | printf_filtered ("No core file now.\n"); | |
224 | } | |
225 | ||
226 | /* Get the registers out of a core file. This is the machine- | |
227 | independent part. Fetch_core_registers is the machine-dependent | |
228 | part, typically implemented in the xm-file for each architecture. */ | |
229 | ||
230 | /* We just get all the registers, so we don't use regno. */ | |
231 | /* ARGSUSED */ | |
232 | static void | |
233 | get_core_registers (regno) | |
234 | int regno; | |
235 | { | |
236 | sec_ptr reg_sec; | |
237 | unsigned size; | |
238 | char *the_regs; | |
d113e6b2 SG |
239 | char secname[10]; |
240 | ||
241 | /* Thread support. If inferior_pid is non-zero, then we have found a core | |
242 | file with threads (or multiple processes). In that case, we need to | |
243 | use the appropriate register section, else we just use `.reg'. */ | |
244 | ||
245 | /* XXX - same thing needs to be done for floating-point (.reg2) sections. */ | |
246 | ||
247 | if (inferior_pid) | |
248 | sprintf (secname, ".reg/%d", inferior_pid); | |
249 | else | |
250 | strcpy (secname, ".reg"); | |
8afd6ca5 | 251 | |
d113e6b2 | 252 | reg_sec = bfd_get_section_by_name (core_bfd, secname); |
8afd6ca5 RP |
253 | if (!reg_sec) goto cant; |
254 | size = bfd_section_size (core_bfd, reg_sec); | |
255 | the_regs = alloca (size); | |
256 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size)) | |
257 | { | |
258 | fetch_core_registers (the_regs, size, 0, | |
259 | (unsigned) bfd_section_vma (abfd,reg_sec)); | |
260 | } | |
261 | else | |
262 | { | |
263 | cant: | |
264 | fprintf_filtered (stderr, "Couldn't fetch registers from core file: %s\n", | |
265 | bfd_errmsg (bfd_error)); | |
266 | } | |
267 | ||
268 | /* Now do it again for the float registers, if they exist. */ | |
269 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); | |
270 | if (reg_sec) { | |
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, | |
274 | size)) | |
275 | { | |
276 | fetch_core_registers (the_regs, size, 2, | |
277 | (unsigned) bfd_section_vma (abfd,reg_sec)); | |
278 | } | |
279 | else | |
280 | { | |
281 | fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n", | |
282 | bfd_errmsg (bfd_error)); | |
283 | } | |
284 | } | |
285 | registers_fetched(); | |
286 | } | |
287 | ||
288 | static void | |
289 | core_files_info (t) | |
290 | struct target_ops *t; | |
291 | { | |
292 | print_section_info (t, core_bfd); | |
293 | } | |
294 | \f | |
f47e56c9 | 295 | /* If mourn is being called in all the right places, this could be say |
cf3e377e | 296 | `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ |
f47e56c9 JK |
297 | |
298 | static int | |
299 | ignore (addr, contents) | |
300 | CORE_ADDR addr; | |
301 | char *contents; | |
302 | { | |
100f92e2 | 303 | return 0; |
f47e56c9 JK |
304 | } |
305 | ||
8afd6ca5 RP |
306 | struct target_ops core_ops = { |
307 | "core", "Local core dump file", | |
308 | "Use a core file as a target. Specify the filename of the core file.", | |
309 | core_open, core_close, | |
310 | find_default_attach, core_detach, 0, 0, /* resume, wait */ | |
311 | get_core_registers, | |
312 | 0, 0, /* store_regs, prepare_to_store */ | |
313 | xfer_memory, core_files_info, | |
f47e56c9 | 314 | ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */ |
8afd6ca5 RP |
315 | 0, 0, 0, 0, 0, /* terminal stuff */ |
316 | 0, 0, 0, /* kill, load, lookup sym */ | |
317 | find_default_create_inferior, 0, /* mourn_inferior */ | |
318 | 0, /* can_run */ | |
3950a34e | 319 | 0, /* notice_signals */ |
8afd6ca5 RP |
320 | core_stratum, 0, /* next */ |
321 | 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */ | |
322 | 0, 0, /* section pointers */ | |
323 | OPS_MAGIC, /* Always the last thing */ | |
324 | }; | |
325 | ||
326 | void | |
327 | _initialize_corelow() | |
328 | { | |
329 | add_target (&core_ops); | |
330 | } |