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