* corelow.c (add_solib_stub): Copy to_sections changes from
[deliverable/binutils-gdb.git] / gdb / corelow.c
CommitLineData
8afd6ca5 1/* Core dump and executable file functions below target vector, for GDB.
ba47c66a
PS
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
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
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "defs.h"
ba47c66a 22#include <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
RP
34
35static void
36core_files_info PARAMS ((struct target_ops *));
37
38#ifdef SOLIB_ADD
39static int
40solib_add_stub PARAMS ((char *));
41#endif
42
43static void
44core_close PARAMS ((int));
45
46static void
47get_core_registers PARAMS ((int));
48
49/* Discard all vestiges of any previous core file
50 and mark data and stack spaces as empty. */
51
52/* ARGSUSED */
53static void
54core_close (quitting)
55 int quitting;
56{
d113e6b2
SG
57 inferior_pid = 0; /* Avoid confusion from thread stuff */
58
8afd6ca5
RP
59 if (core_bfd) {
60 free (bfd_get_filename (core_bfd));
61 bfd_close (core_bfd);
62 core_bfd = NULL;
63#ifdef CLEAR_SOLIB
64 CLEAR_SOLIB ();
65#endif
66 if (core_ops.to_sections) {
67 free ((PTR)core_ops.to_sections);
68 core_ops.to_sections = NULL;
69 core_ops.to_sections_end = NULL;
70 }
71 }
72}
73
74#ifdef SOLIB_ADD
842cf831
JK
75/* Stub function for catch_errors around shared library hacking. FROM_TTYP
76 is really an int * which points to from_tty. */
8afd6ca5
RP
77
78static int
842cf831
JK
79solib_add_stub (from_ttyp)
80 char *from_ttyp;
8afd6ca5 81{
26a859ec
PS
82 SOLIB_ADD (NULL, *(int *)from_ttyp, &core_ops);
83
84 /* SOLIB_ADD usually modifies core_ops.to_sections, which has to
85 be reflected in current_target. */
86 current_target.to_sections = core_ops.to_sections;
87 current_target.to_sections_end = core_ops.to_sections_end;
88 return 0;
8afd6ca5
RP
89}
90#endif /* SOLIB_ADD */
91
d113e6b2
SG
92/* Look for sections whose names start with `.reg/' so that we can extract the
93 list of threads in a core file. */
94
95static void
7c5d526e 96add_to_thread_list (abfd, asect, reg_sect_arg)
d113e6b2
SG
97 bfd *abfd;
98 asection *asect;
7c5d526e 99 PTR reg_sect_arg;
d113e6b2
SG
100{
101 int thread_id;
4cc5b060 102 asection *reg_sect = (asection *) reg_sect_arg;
d113e6b2
SG
103
104 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
105 return;
106
107 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
108
109 add_thread (thread_id);
110
111/* Warning, Will Robinson, looking at BFD private data! */
112
113 if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
114 inferior_pid = thread_id; /* Yes, make it current */
115}
116
8afd6ca5
RP
117/* This routine opens and sets up the core file bfd */
118
119void
120core_open (filename, from_tty)
121 char *filename;
122 int from_tty;
123{
124 const char *p;
125 int siggy;
126 struct cleanup *old_chain;
127 char *temp;
128 bfd *temp_bfd;
129 int ontop;
130 int scratch_chan;
131
132 target_preopen (from_tty);
133 if (!filename)
134 {
135 error (core_bfd?
136 "No core file specified. (Use `detach' to stop debugging a core file.)"
137 : "No core file specified.");
138 }
139
140 filename = tilde_expand (filename);
141 if (filename[0] != '/') {
142 temp = concat (current_directory, "/", filename, NULL);
143 free (filename);
144 filename = temp;
145 }
146
147 old_chain = make_cleanup (free, filename);
148
149 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
150 if (scratch_chan < 0)
151 perror_with_name (filename);
152
0685d95f 153 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
8afd6ca5
RP
154 if (temp_bfd == NULL)
155 {
156 perror_with_name (filename);
157 }
158
159 if (!bfd_check_format (temp_bfd, bfd_core))
160 {
161 /* Do it after the err msg */
162 make_cleanup (bfd_close, temp_bfd);
c4a081e1 163 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_get_error ()));
8afd6ca5
RP
164 }
165
166 /* Looks semi-reasonable. Toss the old core file and work on the new. */
167
168 discard_cleanups (old_chain); /* Don't free filename any more */
169 unpush_target (&core_ops);
170 core_bfd = temp_bfd;
171 old_chain = make_cleanup (core_close, core_bfd);
172
173 validate_files ();
174
175 /* Find the data section */
176 if (build_section_table (core_bfd, &core_ops.to_sections,
177 &core_ops.to_sections_end))
178 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
c4a081e1 179 bfd_errmsg (bfd_get_error ()));
8afd6ca5
RP
180
181 ontop = !push_target (&core_ops);
182 discard_cleanups (old_chain);
183
184 p = bfd_core_file_failing_command (core_bfd);
185 if (p)
186 printf_filtered ("Core was generated by `%s'.\n", p);
187
188 siggy = bfd_core_file_failing_signal (core_bfd);
189 if (siggy > 0)
190 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
191 safe_strsignal (siggy));
192
d113e6b2
SG
193 /* Build up thread list from BFD sections. */
194
195 init_thread_list ();
196 bfd_map_over_sections (core_bfd, add_to_thread_list,
197 bfd_get_section_by_name (core_bfd, ".reg"));
198
8afd6ca5
RP
199 if (ontop) {
200 /* Fetch all registers from core file */
201 target_fetch_registers (-1);
202
203 /* Add symbols and section mappings for any shared libraries */
204#ifdef SOLIB_ADD
842cf831 205 catch_errors (solib_add_stub, &from_tty, (char *)0,
9748446f 206 RETURN_MASK_ALL);
8afd6ca5
RP
207#endif
208
209 /* Now, set up the frame cache, and print the top of stack */
85e07872 210 set_current_frame (create_new_frame (read_fp (),
8afd6ca5
RP
211 read_pc ()));
212 select_frame (get_current_frame (), 0);
213 print_stack_frame (selected_frame, selected_frame_level, 1);
214 } else {
215 warning (
216"you won't be able to access this core file until you terminate\n\
cad1498f 217your %s; do ``info files''", target_longname);
8afd6ca5
RP
218 }
219}
220
221void
222core_detach (args, from_tty)
223 char *args;
224 int from_tty;
225{
226 if (args)
227 error ("Too many arguments");
228 unpush_target (&core_ops);
c5198d93 229 reinit_frame_cache ();
8afd6ca5
RP
230 if (from_tty)
231 printf_filtered ("No core file now.\n");
232}
233
234/* Get the registers out of a core file. This is the machine-
235 independent part. Fetch_core_registers is the machine-dependent
236 part, typically implemented in the xm-file for each architecture. */
237
238/* We just get all the registers, so we don't use regno. */
239/* ARGSUSED */
240static void
241get_core_registers (regno)
242 int regno;
243{
244 sec_ptr reg_sec;
245 unsigned size;
246 char *the_regs;
d113e6b2
SG
247 char secname[10];
248
249 /* Thread support. If inferior_pid is non-zero, then we have found a core
250 file with threads (or multiple processes). In that case, we need to
251 use the appropriate register section, else we just use `.reg'. */
252
253 /* XXX - same thing needs to be done for floating-point (.reg2) sections. */
254
255 if (inferior_pid)
256 sprintf (secname, ".reg/%d", inferior_pid);
257 else
258 strcpy (secname, ".reg");
8afd6ca5 259
d113e6b2 260 reg_sec = bfd_get_section_by_name (core_bfd, secname);
8afd6ca5
RP
261 if (!reg_sec) goto cant;
262 size = bfd_section_size (core_bfd, reg_sec);
263 the_regs = alloca (size);
264 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
265 {
266 fetch_core_registers (the_regs, size, 0,
267 (unsigned) bfd_section_vma (abfd,reg_sec));
268 }
269 else
270 {
271cant:
199b2450 272 fprintf_filtered (gdb_stderr, "Couldn't fetch registers from core file: %s\n",
c4a081e1 273 bfd_errmsg (bfd_get_error ()));
8afd6ca5
RP
274 }
275
276 /* Now do it again for the float registers, if they exist. */
277 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
278 if (reg_sec) {
279 size = bfd_section_size (core_bfd, reg_sec);
280 the_regs = alloca (size);
281 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
282 size))
283 {
284 fetch_core_registers (the_regs, size, 2,
285 (unsigned) bfd_section_vma (abfd,reg_sec));
286 }
287 else
288 {
199b2450 289 fprintf_filtered (gdb_stderr, "Couldn't fetch register set 2 from core file: %s\n",
c4a081e1 290 bfd_errmsg (bfd_get_error ()));
8afd6ca5
RP
291 }
292 }
293 registers_fetched();
294}
295
296static void
297core_files_info (t)
298 struct target_ops *t;
299{
300 print_section_info (t, core_bfd);
301}
302\f
f47e56c9 303/* If mourn is being called in all the right places, this could be say
cf3e377e 304 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
f47e56c9
JK
305
306static int
307ignore (addr, contents)
308 CORE_ADDR addr;
309 char *contents;
310{
100f92e2 311 return 0;
f47e56c9
JK
312}
313
8afd6ca5
RP
314struct target_ops core_ops = {
315 "core", "Local core dump file",
316 "Use a core file as a target. Specify the filename of the core file.",
317 core_open, core_close,
318 find_default_attach, core_detach, 0, 0, /* resume, wait */
319 get_core_registers,
320 0, 0, /* store_regs, prepare_to_store */
321 xfer_memory, core_files_info,
f47e56c9 322 ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */
8afd6ca5
RP
323 0, 0, 0, 0, 0, /* terminal stuff */
324 0, 0, 0, /* kill, load, lookup sym */
325 find_default_create_inferior, 0, /* mourn_inferior */
326 0, /* can_run */
3950a34e 327 0, /* notice_signals */
8afd6ca5
RP
328 core_stratum, 0, /* next */
329 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
330 0, 0, /* section pointers */
331 OPS_MAGIC, /* Always the last thing */
332};
333
334void
335_initialize_corelow()
336{
337 add_target (&core_ops);
338}
This page took 0.122387 seconds and 4 git commands to generate.