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