* corelow.c (add_solib_stub): Remove copying of to_sections,
[deliverable/binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include <string.h>
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"
33 #include "thread.h"
34
35 static void
36 core_files_info PARAMS ((struct target_ops *));
37
38 #ifdef SOLIB_ADD
39 static int
40 solib_add_stub PARAMS ((char *));
41 #endif
42
43 static void
44 core_close PARAMS ((int));
45
46 static void
47 get_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 */
53 static void
54 core_close (quitting)
55 int quitting;
56 {
57 inferior_pid = 0; /* Avoid confusion from thread stuff */
58
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
75 /* Stub function for catch_errors around shared library hacking. FROM_TTYP
76 is really an int * which points to from_tty. */
77
78 static int
79 solib_add_stub (from_ttyp)
80 char *from_ttyp;
81 {
82 SOLIB_ADD (NULL, *(int *)from_ttyp, &current_target);
83 return 0;
84 }
85 #endif /* SOLIB_ADD */
86
87 /* Look for sections whose names start with `.reg/' so that we can extract the
88 list of threads in a core file. */
89
90 static void
91 add_to_thread_list (abfd, asect, reg_sect_arg)
92 bfd *abfd;
93 asection *asect;
94 PTR reg_sect_arg;
95 {
96 int thread_id;
97 asection *reg_sect = (asection *) reg_sect_arg;
98
99 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
100 return;
101
102 thread_id = atoi (bfd_section_name (abfd, asect) + 5);
103
104 add_thread (thread_id);
105
106 /* Warning, Will Robinson, looking at BFD private data! */
107
108 if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */
109 inferior_pid = thread_id; /* Yes, make it current */
110 }
111
112 /* This routine opens and sets up the core file bfd */
113
114 void
115 core_open (filename, from_tty)
116 char *filename;
117 int from_tty;
118 {
119 const char *p;
120 int siggy;
121 struct cleanup *old_chain;
122 char *temp;
123 bfd *temp_bfd;
124 int ontop;
125 int scratch_chan;
126
127 target_preopen (from_tty);
128 if (!filename)
129 {
130 error (core_bfd?
131 "No core file specified. (Use `detach' to stop debugging a core file.)"
132 : "No core file specified.");
133 }
134
135 filename = tilde_expand (filename);
136 if (filename[0] != '/') {
137 temp = concat (current_directory, "/", filename, NULL);
138 free (filename);
139 filename = temp;
140 }
141
142 old_chain = make_cleanup (free, filename);
143
144 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
145 if (scratch_chan < 0)
146 perror_with_name (filename);
147
148 temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
149 if (temp_bfd == NULL)
150 {
151 perror_with_name (filename);
152 }
153
154 if (!bfd_check_format (temp_bfd, bfd_core))
155 {
156 /* Do it after the err msg */
157 make_cleanup (bfd_close, temp_bfd);
158 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_get_error ()));
159 }
160
161 /* Looks semi-reasonable. Toss the old core file and work on the new. */
162
163 discard_cleanups (old_chain); /* Don't free filename any more */
164 unpush_target (&core_ops);
165 core_bfd = temp_bfd;
166 old_chain = make_cleanup (core_close, core_bfd);
167
168 validate_files ();
169
170 /* Find the data section */
171 if (build_section_table (core_bfd, &core_ops.to_sections,
172 &core_ops.to_sections_end))
173 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
174 bfd_errmsg (bfd_get_error ()));
175
176 ontop = !push_target (&core_ops);
177 discard_cleanups (old_chain);
178
179 p = bfd_core_file_failing_command (core_bfd);
180 if (p)
181 printf_filtered ("Core was generated by `%s'.\n", p);
182
183 siggy = bfd_core_file_failing_signal (core_bfd);
184 if (siggy > 0)
185 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
186 safe_strsignal (siggy));
187
188 /* Build up thread list from BFD sections. */
189
190 init_thread_list ();
191 bfd_map_over_sections (core_bfd, add_to_thread_list,
192 bfd_get_section_by_name (core_bfd, ".reg"));
193
194 if (ontop) {
195 /* Fetch all registers from core file */
196 target_fetch_registers (-1);
197
198 /* Add symbols and section mappings for any shared libraries */
199 #ifdef SOLIB_ADD
200 catch_errors (solib_add_stub, &from_tty, (char *)0,
201 RETURN_MASK_ALL);
202
203 /* solib_add_stub usually modifies current_target.to_sections, which
204 has to be reflected in core_ops to enable proper freeing of
205 of the to_sections vector in core_close. */
206 core_ops.to_sections = current_target.to_sections;
207 #endif
208
209 /* Now, set up the frame cache, and print the top of stack */
210 set_current_frame (create_new_frame (read_fp (),
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\
217 your %s; do ``info files''", target_longname);
218 }
219 }
220
221 void
222 core_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);
229 reinit_frame_cache ();
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 */
240 static void
241 get_core_registers (regno)
242 int regno;
243 {
244 sec_ptr reg_sec;
245 unsigned size;
246 char *the_regs;
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");
259
260 reg_sec = bfd_get_section_by_name (core_bfd, secname);
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 {
271 cant:
272 fprintf_filtered (gdb_stderr, "Couldn't fetch registers from core file: %s\n",
273 bfd_errmsg (bfd_get_error ()));
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 {
289 fprintf_filtered (gdb_stderr, "Couldn't fetch register set 2 from core file: %s\n",
290 bfd_errmsg (bfd_get_error ()));
291 }
292 }
293 registers_fetched();
294 }
295
296 static void
297 core_files_info (t)
298 struct target_ops *t;
299 {
300 print_section_info (t, core_bfd);
301 }
302 \f
303 /* If mourn is being called in all the right places, this could be say
304 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
305
306 static int
307 ignore (addr, contents)
308 CORE_ADDR addr;
309 char *contents;
310 {
311 return 0;
312 }
313
314 struct 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,
322 ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */
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 */
327 0, /* notice_signals */
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
334 void
335 _initialize_corelow()
336 {
337 add_target (&core_ops);
338 }
This page took 0.036108 seconds and 5 git commands to generate.