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