This commit was generated by cvs2svn to track changes on a CVS vendor
[binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2 Copyright 1986, 87, 89, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include <errno.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "frame.h" /* required by inferior.h */
29 #include "inferior.h"
30 #include "symtab.h"
31 #include "command.h"
32 #include "bfd.h"
33 #include "target.h"
34 #include "gdbcore.h"
35 #include "gdbthread.h"
36
37 /* List of all available core_fns. On gdb startup, each core file register
38 reader calls add_core_fns() to register information on each core format it
39 is prepared to read. */
40
41 static struct core_fns *core_file_fns = NULL;
42
43 static void core_files_info PARAMS ((struct target_ops *));
44
45 #ifdef SOLIB_ADD
46 static int solib_add_stub PARAMS ((PTR));
47 #endif
48
49 static void core_open PARAMS ((char *, int));
50
51 static void core_detach PARAMS ((char *, int));
52
53 static void core_close PARAMS ((int));
54
55 static void get_core_registers PARAMS ((int));
56
57 static void add_to_thread_list PARAMS ((bfd *, asection *, PTR));
58
59 static int ignore PARAMS ((CORE_ADDR, char *));
60
61 static char *core_file_to_sym_file PARAMS ((char *));
62
63 static int core_file_thread_alive PARAMS ((int tid));
64
65 static void init_core_ops PARAMS ((void));
66
67 void _initialize_corelow PARAMS ((void));
68
69 struct target_ops core_ops;
70
71 /* Link a new core_fns into the global core_file_fns list. Called on gdb
72 startup by the _initialize routine in each core file register reader, to
73 register information about each format the the reader is prepared to
74 handle. */
75
76 void
77 add_core_fns (cf)
78 struct core_fns *cf;
79 {
80 cf->next = core_file_fns;
81 core_file_fns = cf;
82 }
83
84
85 /* Discard all vestiges of any previous core file and mark data and stack
86 spaces as empty. */
87
88 /* ARGSUSED */
89 static void
90 core_close (quitting)
91 int quitting;
92 {
93 char *name;
94
95 if (core_bfd)
96 {
97 inferior_pid = 0; /* Avoid confusion from thread stuff */
98
99 /* Clear out solib state while the bfd is still open. See
100 comments in clear_solib in solib.c. */
101 #ifdef CLEAR_SOLIB
102 CLEAR_SOLIB ();
103 #endif
104
105 name = bfd_get_filename (core_bfd);
106 if (!bfd_close (core_bfd))
107 warning ("cannot close \"%s\": %s",
108 name, bfd_errmsg (bfd_get_error ()));
109 free (name);
110 core_bfd = NULL;
111 if (core_ops.to_sections)
112 {
113 free ((PTR) core_ops.to_sections);
114 core_ops.to_sections = NULL;
115 core_ops.to_sections_end = NULL;
116 }
117 }
118 }
119
120 #ifdef SOLIB_ADD
121 /* Stub function for catch_errors around shared library hacking. FROM_TTYP
122 is really an int * which points to from_tty. */
123
124 static int
125 solib_add_stub (from_ttyp)
126 PTR from_ttyp;
127 {
128 SOLIB_ADD (NULL, *(int *) from_ttyp, &current_target);
129 re_enable_breakpoints_in_shlibs ();
130 return 0;
131 }
132 #endif /* SOLIB_ADD */
133
134 /* Look for sections whose names start with `.reg/' so that we can extract the
135 list of threads in a core file. */
136
137 static void
138 add_to_thread_list (abfd, asect, reg_sect_arg)
139 bfd *abfd;
140 asection *asect;
141 PTR reg_sect_arg;
142 {
143 int thread_id;
144 asection *reg_sect = (asection *) reg_sect_arg;
145
146 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
147 return;
148
149 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
150
151 add_thread (thread_id);
152
153 /* Warning, Will Robinson, looking at BFD private data! */
154
155 if (reg_sect != NULL
156 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
157 inferior_pid = thread_id; /* Yes, make it current */
158 }
159
160 /* This routine opens and sets up the core file bfd. */
161
162 static void
163 core_open (filename, from_tty)
164 char *filename;
165 int from_tty;
166 {
167 const char *p;
168 int siggy;
169 struct cleanup *old_chain;
170 char *temp;
171 bfd *temp_bfd;
172 int ontop;
173 int scratch_chan;
174
175 target_preopen (from_tty);
176 if (!filename)
177 {
178 error (core_bfd ?
179 "No core file specified. (Use `detach' to stop debugging a core file.)"
180 : "No core file specified.");
181 }
182
183 filename = tilde_expand (filename);
184 if (filename[0] != '/')
185 {
186 temp = concat (current_directory, "/", filename, NULL);
187 free (filename);
188 filename = temp;
189 }
190
191 old_chain = make_cleanup (free, filename);
192
193 scratch_chan = open (filename, write_files ? O_RDWR : O_RDONLY, 0);
194 if (scratch_chan < 0)
195 perror_with_name (filename);
196
197 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
198 if (temp_bfd == NULL)
199 perror_with_name (filename);
200
201 if (!bfd_check_format (temp_bfd, bfd_core))
202 {
203 /* Do it after the err msg */
204 /* FIXME: should be checking for errors from bfd_close (for one thing,
205 on error it does not free all the storage associated with the
206 bfd). */
207 make_cleanup ((make_cleanup_func) bfd_close, temp_bfd);
208 error ("\"%s\" is not a core dump: %s",
209 filename, bfd_errmsg (bfd_get_error ()));
210 }
211
212 /* Looks semi-reasonable. Toss the old core file and work on the new. */
213
214 discard_cleanups (old_chain); /* Don't free filename any more */
215 unpush_target (&core_ops);
216 core_bfd = temp_bfd;
217 old_chain = make_cleanup ((make_cleanup_func) core_close, core_bfd);
218
219 validate_files ();
220
221 /* Find the data section */
222 if (build_section_table (core_bfd, &core_ops.to_sections,
223 &core_ops.to_sections_end))
224 error ("\"%s\": Can't find sections: %s",
225 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
226
227 ontop = !push_target (&core_ops);
228 discard_cleanups (old_chain);
229
230 p = bfd_core_file_failing_command (core_bfd);
231 if (p)
232 printf_filtered ("Core was generated by `%s'.\n", p);
233
234 siggy = bfd_core_file_failing_signal (core_bfd);
235 if (siggy > 0)
236 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
237 safe_strsignal (siggy));
238
239 /* Build up thread list from BFD sections. */
240
241 init_thread_list ();
242 bfd_map_over_sections (core_bfd, add_to_thread_list,
243 bfd_get_section_by_name (core_bfd, ".reg"));
244
245 if (ontop)
246 {
247 /* Fetch all registers from core file. */
248 target_fetch_registers (-1);
249
250 /* Add symbols and section mappings for any shared libraries. */
251 #ifdef SOLIB_ADD
252 catch_errors (solib_add_stub, &from_tty, (char *) 0,
253 RETURN_MASK_ALL);
254 #endif
255
256 /* Now, set up the frame cache, and print the top of stack. */
257 flush_cached_frames ();
258 select_frame (get_current_frame (), 0);
259 print_stack_frame (selected_frame, selected_frame_level, 1);
260 }
261 else
262 {
263 warning (
264 "you won't be able to access this core file until you terminate\n\
265 your %s; do ``info files''", target_longname);
266 }
267 }
268
269 static void
270 core_detach (args, from_tty)
271 char *args;
272 int from_tty;
273 {
274 if (args)
275 error ("Too many arguments");
276 unpush_target (&core_ops);
277 reinit_frame_cache ();
278 if (from_tty)
279 printf_filtered ("No core file now.\n");
280 }
281
282 /* Get the registers out of a core file. This is the machine-
283 independent part. Fetch_core_registers is the machine-dependent
284 part, typically implemented in the xm-file for each architecture. */
285
286 /* We just get all the registers, so we don't use regno. */
287
288 /* ARGSUSED */
289 static void
290 get_core_registers (regno)
291 int regno;
292 {
293 sec_ptr reg_sec;
294 unsigned size;
295 char *the_regs;
296 char secname[30];
297 enum bfd_flavour our_flavour = bfd_get_flavour (core_bfd);
298 struct core_fns *cf = NULL;
299
300 if (core_file_fns == NULL)
301 {
302 fprintf_filtered (gdb_stderr,
303 "Can't fetch registers from this type of core file\n");
304 return;
305 }
306
307 /* Thread support. If inferior_pid is non-zero, then we have found a core
308 file with threads (or multiple processes). In that case, we need to
309 use the appropriate register section, else we just use `.reg'. */
310
311 /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
312
313 if (inferior_pid)
314 sprintf (secname, ".reg/%d", inferior_pid);
315 else
316 strcpy (secname, ".reg");
317
318 reg_sec = bfd_get_section_by_name (core_bfd, secname);
319 if (!reg_sec)
320 goto cant;
321 size = bfd_section_size (core_bfd, reg_sec);
322 the_regs = alloca (size);
323 /* Look for the core functions that match this flavor. Default to the
324 first one if nothing matches. */
325 for (cf = core_file_fns; cf != NULL; cf = cf->next)
326 {
327 if (our_flavour == cf->core_flavour)
328 {
329 break;
330 }
331 }
332 if (cf == NULL)
333 {
334 cf = core_file_fns;
335 }
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)
339 {
340 (cf->core_read_registers (the_regs, size, 0,
341 (unsigned) bfd_section_vma (abfd, reg_sec)));
342 }
343 else
344 {
345 cant:
346 fprintf_filtered (gdb_stderr,
347 "Couldn't fetch registers from core file: %s\n",
348 bfd_errmsg (bfd_get_error ()));
349 }
350
351 /* Now do it again for the float registers, if they exist. */
352 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
353 if (reg_sec)
354 {
355 size = bfd_section_size (core_bfd, reg_sec);
356 the_regs = alloca (size);
357 if (cf != NULL &&
358 bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr) 0, size) &&
359 cf->core_read_registers != NULL)
360 {
361 (cf->core_read_registers (the_regs, size, 2,
362 (unsigned) bfd_section_vma (abfd, reg_sec)));
363 }
364 else
365 {
366 fprintf_filtered (gdb_stderr,
367 "Couldn't fetch register set 2 from core file: %s\n",
368 bfd_errmsg (bfd_get_error ()));
369 }
370 }
371 registers_fetched ();
372 }
373
374 static char *
375 core_file_to_sym_file (core)
376 char *core;
377 {
378 CONST char *failing_command;
379 char *p;
380 char *temp;
381 bfd *temp_bfd;
382 int scratch_chan;
383
384 if (!core)
385 error ("No core file specified.");
386
387 core = tilde_expand (core);
388 if (core[0] != '/')
389 {
390 temp = concat (current_directory, "/", core, NULL);
391 core = temp;
392 }
393
394 scratch_chan = open (core, write_files ? O_RDWR : O_RDONLY, 0);
395 if (scratch_chan < 0)
396 perror_with_name (core);
397
398 temp_bfd = bfd_fdopenr (core, gnutarget, scratch_chan);
399 if (temp_bfd == NULL)
400 perror_with_name (core);
401
402 if (!bfd_check_format (temp_bfd, bfd_core))
403 {
404 /* Do it after the err msg */
405 /* FIXME: should be checking for errors from bfd_close (for one thing,
406 on error it does not free all the storage associated with the
407 bfd). */
408 make_cleanup ((make_cleanup_func) bfd_close, temp_bfd);
409 error ("\"%s\" is not a core dump: %s",
410 core, bfd_errmsg (bfd_get_error ()));
411 }
412
413 /* Find the data section */
414 if (build_section_table (temp_bfd, &core_ops.to_sections,
415 &core_ops.to_sections_end))
416 error ("\"%s\": Can't find sections: %s",
417 bfd_get_filename (temp_bfd), bfd_errmsg (bfd_get_error ()));
418
419 failing_command = bfd_core_file_failing_command (temp_bfd);
420
421 bfd_close (temp_bfd);
422
423 /* If we found a filename, remember that it is probably saved
424 relative to the executable that created it. If working directory
425 isn't there now, we may not be able to find the executable. Rather
426 than trying to be sauve about finding it, just check if the file
427 exists where we are now. If not, then punt and tell our client
428 we couldn't find the sym file.
429 */
430 p = (char *) failing_command;
431 if ((p != NULL) && (access (p, F_OK) != 0))
432 p = NULL;
433
434 return p;
435 }
436
437 static void
438 core_files_info (t)
439 struct target_ops *t;
440 {
441 print_section_info (t, core_bfd);
442 }
443 \f
444 /* If mourn is being called in all the right places, this could be say
445 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
446
447 static int
448 ignore (addr, contents)
449 CORE_ADDR addr;
450 char *contents;
451 {
452 return 0;
453 }
454
455
456 /* Okay, let's be honest: threads gleaned from a core file aren't
457 exactly lively, are they? On the other hand, if we don't claim
458 that each & every one is alive, then we don't get any of them
459 to appear in an "info thread" command, which is quite a useful
460 behaviour.
461 */
462 static int
463 core_file_thread_alive (tid)
464 int tid;
465 {
466 return 1;
467 }
468
469 /* Fill in core_ops with its defined operations and properties. */
470
471 static void
472 init_core_ops ()
473 {
474 core_ops.to_shortname = "core";
475 core_ops.to_longname = "Local core dump file";
476 core_ops.to_doc =
477 "Use a core file as a target. Specify the filename of the core file.";
478 core_ops.to_open = core_open;
479 core_ops.to_close = core_close;
480 core_ops.to_attach = find_default_attach;
481 core_ops.to_require_attach = find_default_require_attach;
482 core_ops.to_detach = core_detach;
483 core_ops.to_require_detach = find_default_require_detach;
484 core_ops.to_fetch_registers = get_core_registers;
485 core_ops.to_xfer_memory = xfer_memory;
486 core_ops.to_files_info = core_files_info;
487 core_ops.to_insert_breakpoint = ignore;
488 core_ops.to_remove_breakpoint = ignore;
489 core_ops.to_create_inferior = find_default_create_inferior;
490 core_ops.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior;
491 core_ops.to_thread_alive = core_file_thread_alive;
492 core_ops.to_core_file_to_sym_file = core_file_to_sym_file;
493 core_ops.to_stratum = core_stratum;
494 core_ops.to_has_memory = 1;
495 core_ops.to_has_stack = 1;
496 core_ops.to_has_registers = 1;
497 core_ops.to_magic = OPS_MAGIC;
498 }
499
500 /* non-zero if we should not do the add_target call in
501 _initialize_corelow; not initialized (i.e., bss) so that
502 the target can initialize it (i.e., data) if appropriate.
503 This needs to be set at compile time because we don't know
504 for sure whether the target's initialize routine is called
505 before us or after us. */
506 int coreops_suppress_target;
507
508 void
509 _initialize_corelow ()
510 {
511 init_core_ops ();
512
513 if (!coreops_suppress_target)
514 add_target (&core_ops);
515 }