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