* core.c (core_open, core_close, core_xfer_memory): Move
[deliverable/binutils-gdb.git] / gdb / core.c
1 /* Work with core dump and executable files, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include "defs.h"
24 #include "param.h"
25 #include "frame.h" /* required by inferior.h */
26 #include "inferior.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "gdbcore.h"
32
33 extern int xfer_memory ();
34 extern void child_attach (), child_create_inferior ();
35
36 extern int sys_nerr;
37 extern char *sys_errlist[];
38 extern char *sys_siglist[];
39
40 extern char registers[];
41
42 /* Hook for `exec_file_command' command to call. */
43
44 void (*exec_file_display_hook) () = NULL;
45
46 /* Binary file diddling handle for the core file. */
47
48 bfd *core_bfd = NULL;
49
50 /* Forward decl */
51 extern struct target_ops core_ops;
52
53 \f
54 /* Discard all vestiges of any previous core file
55 and mark data and stack spaces as empty. */
56
57 /* ARGSUSED */
58 void
59 core_close (quitting)
60 int quitting;
61 {
62 if (core_bfd) {
63 free (bfd_get_filename (core_bfd));
64 bfd_close (core_bfd);
65 core_bfd = NULL;
66 #ifdef CLEAR_SOLIB
67 CLEAR_SOLIB ();
68 #endif
69 if (core_ops.sections) {
70 free (core_ops.sections);
71 core_ops.sections = NULL;
72 core_ops.sections_end = NULL;
73 }
74 }
75 }
76
77 /* This routine opens and sets up the core file bfd */
78
79 void
80 core_open (filename, from_tty)
81 char *filename;
82 int from_tty;
83 {
84 const char *p;
85 int siggy;
86 struct cleanup *old_chain;
87 char *temp;
88 bfd *temp_bfd;
89 int ontop;
90
91 target_preopen (from_tty);
92 if (!filename)
93 {
94 error (core_bfd?
95 "No core file specified. (Use `detach' to stop debugging a core file.)"
96 : "No core file specified.");
97 }
98
99 filename = tilde_expand (filename);
100 if (filename[0] != '/') {
101 temp = concat (current_directory, "/", filename);
102 free (filename);
103 filename = temp;
104 }
105
106 old_chain = make_cleanup (free, filename);
107 temp_bfd = bfd_openr (filename, NULL);
108 if (temp_bfd == NULL)
109 {
110 perror_with_name (filename);
111 }
112
113 if (!bfd_check_format (temp_bfd, bfd_core))
114 {
115 bfd_close (temp_bfd);
116 error ("\"%s\" does not appear to be a core dump", filename);
117 }
118
119 /* Looks semi-reasonable. Toss the old core file and work on the new. */
120
121 discard_cleanups (old_chain); /* Don't free filename any more */
122 unpush_target (&core_ops);
123 core_bfd = temp_bfd;
124 old_chain = make_cleanup (core_close, core_bfd);
125
126 validate_files ();
127
128 /* Find the data section */
129 if (build_section_table (core_bfd, &core_ops.sections,
130 &core_ops.sections_end))
131 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
132 bfd_errmsg (bfd_error));
133
134 ontop = !push_target (&core_ops);
135 discard_cleanups (old_chain);
136
137 p = bfd_core_file_failing_command (core_bfd);
138 if (p)
139 printf ("Core file invoked as `%s'.\n", p);
140
141 siggy = bfd_core_file_failing_signal (core_bfd);
142 if (siggy > 0)
143 printf ("Program terminated with signal %d, %s.\n", siggy,
144 siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
145
146 if (ontop) {
147 /* Fetch all registers from core file */
148 target_fetch_registers (-1);
149 /* Add symbols and section mappings for any shared libraries */
150 #ifdef SOLIB_ADD
151 SOLIB_ADD (NULL, from_tty, &core_ops);
152 #endif
153 /* Now, set up the frame cache, and print the top of stack */
154 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
155 read_pc ()));
156 select_frame (get_current_frame (), 0);
157 print_sel_frame (0); /* Print the top frame and source line */
158 } else {
159 printf (
160 "Warning: you won't be able to access this core file until you terminate\n\
161 your %s; do ``info files''\n", current_target->to_longname);
162 }
163 }
164
165 void
166 core_detach (args, from_tty)
167 char *args;
168 int from_tty;
169 {
170 if (args)
171 error ("Too many arguments");
172 unpush_target (&core_ops);
173 if (from_tty)
174 printf ("No core file now.\n");
175 }
176
177 /* Backward compatability with old way of specifying core files. */
178
179 void
180 core_file_command (filename, from_tty)
181 char *filename;
182 int from_tty;
183 {
184 dont_repeat (); /* Either way, seems bogus. */
185 if (!filename)
186 core_detach (filename, from_tty);
187 else
188 core_open (filename, from_tty);
189 }
190
191 \f
192 /* Call this to specify the hook for exec_file_command to call back.
193 This is called from the x-window display code. */
194
195 void
196 specify_exec_file_hook (hook)
197 void (*hook) ();
198 {
199 exec_file_display_hook = hook;
200 }
201
202 /* The exec file must be closed before running an inferior.
203 If it is needed again after the inferior dies, it must
204 be reopened. */
205
206 void
207 close_exec_file ()
208 {
209 #ifdef FIXME
210 if (exec_bfd)
211 bfd_tempclose (exec_bfd);
212 #endif
213 }
214
215 void
216 reopen_exec_file ()
217 {
218 #ifdef FIXME
219 if (exec_bfd)
220 bfd_reopen (exec_bfd);
221 #endif
222 }
223 \f
224 /* If we have both a core file and an exec file,
225 print a warning if they don't go together. */
226
227 void
228 validate_files ()
229 {
230 if (exec_bfd && core_bfd)
231 {
232 if (core_file_matches_executable_p (core_bfd, exec_bfd))
233 printf ("Warning: core file does not match specified executable file.\n");
234 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
235 printf ("Warning: exec file is newer than core file.\n");
236 }
237 }
238
239 /* Return the name of the executable file as a string.
240 ERR nonzero means get error if there is none specified;
241 otherwise return 0 in that case. */
242
243 char *
244 get_exec_file (err)
245 int err;
246 {
247 if (exec_bfd) return bfd_get_filename(exec_bfd);
248 if (!err) return NULL;
249
250 error ("No executable file specified.\n\
251 Use the \"file\" or \"exec-file\" command.");
252 return NULL;
253 }
254
255 static void
256 core_files_info (t)
257 struct target_ops *t;
258 {
259 struct section_table *p;
260
261 printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd));
262
263 for (p = t->sections; p < t->sections_end; p++)
264 if (p->bfd == core_bfd)
265 printf("\tcore file from 0x%08x to 0x%08x is %s\n",
266 p->addr, p->endaddr,
267 bfd_section_name (p->bfd, p->sec_ptr));
268 else {
269 printf("\tshared lib from 0x%08x to 0x%08x is %s in %s\n",
270 p->addr, p->endaddr,
271 bfd_section_name (p->bfd, p->sec_ptr),
272 bfd_get_filename (p->bfd));
273 }
274 }
275 \f
276 void
277 memory_error (status, memaddr)
278 int status;
279 CORE_ADDR memaddr;
280 {
281
282 if (status == EIO)
283 {
284 /* Actually, address between memaddr and memaddr + len
285 was out of bounds. */
286 error ("Cannot access memory at address 0x%x.", memaddr);
287 }
288 else
289 {
290 if (status >= sys_nerr || status < 0)
291 error ("Error accessing memory address 0x%x: unknown error (%d).",
292 memaddr, status);
293 else
294 error ("Error accessing memory address 0x%x: %s.",
295 memaddr, sys_errlist[status]);
296 }
297 }
298
299 /* Same as target_read_memory, but report an error if can't read. */
300 void
301 read_memory (memaddr, myaddr, len)
302 CORE_ADDR memaddr;
303 char *myaddr;
304 int len;
305 {
306 int status;
307 status = target_read_memory (memaddr, myaddr, len);
308 if (status != 0)
309 memory_error (status, memaddr);
310 }
311
312 /* Same as target_write_memory, but report an error if can't write. */
313 void
314 write_memory (memaddr, myaddr, len)
315 CORE_ADDR memaddr;
316 char *myaddr;
317 int len;
318 {
319 int status;
320
321 status = target_write_memory (memaddr, myaddr, len);
322 if (status != 0)
323 memory_error (status, memaddr);
324 }
325
326 /* Read an integer from debugged memory, given address and number of bytes. */
327
328 long
329 read_memory_integer (memaddr, len)
330 CORE_ADDR memaddr;
331 int len;
332 {
333 char cbuf;
334 short sbuf;
335 int ibuf;
336 long lbuf;
337
338 if (len == sizeof (char))
339 {
340 read_memory (memaddr, &cbuf, len);
341 return cbuf;
342 }
343 if (len == sizeof (short))
344 {
345 read_memory (memaddr, (char *)&sbuf, len);
346 SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
347 return sbuf;
348 }
349 if (len == sizeof (int))
350 {
351 read_memory (memaddr, (char *)&ibuf, len);
352 SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
353 return ibuf;
354 }
355 if (len == sizeof (lbuf))
356 {
357 read_memory (memaddr, (char *)&lbuf, len);
358 SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
359 return lbuf;
360 }
361 error ("Cannot handle integers of %d bytes.", len);
362 return -1; /* for lint */
363 }
364 \f
365 /* Get the registers out of a core file. This is the machine-
366 independent part. Fetch_core_registers is the machine-dependent
367 part, typically implemented in the xm-file for each architecture. */
368
369 /* We just get all the registers, so we don't use regno. */
370 /* ARGSUSED */
371 static void
372 get_core_registers (regno)
373 int regno;
374 {
375 sec_ptr reg_sec;
376 unsigned size;
377 char *the_regs;
378
379 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
380 size = bfd_section_size (core_bfd, reg_sec);
381 the_regs = alloca (size);
382 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
383 (unsigned)0, size))
384 {
385 fetch_core_registers (the_regs, size, 0);
386 }
387 else
388 {
389 fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
390 bfd_errmsg (bfd_error));
391 }
392
393 /* Now do it again for the float registers, if they exist. */
394 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
395 if (reg_sec) {
396 size = bfd_section_size (core_bfd, reg_sec);
397 the_regs = alloca (size);
398 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
399 (unsigned)0, size))
400 {
401 fetch_core_registers (the_regs, size, 2);
402 }
403 else
404 {
405 fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
406 bfd_errmsg (bfd_error));
407 }
408 }
409 registers_fetched();
410 }
411 \f
412 struct target_ops core_ops = {
413 "core", "Local core dump file",
414 "Use a core file as a target. Specify the filename of the core file.",
415 core_open, core_close,
416 child_attach, core_detach, 0, 0, /* resume, wait */
417 get_core_registers,
418 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
419 xfer_memory, core_files_info,
420 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
421 0, 0, 0, 0, 0, /* terminal stuff */
422 0, 0, 0, 0, /* kill, load, call fn, lookup sym */
423 child_create_inferior, 0, /* mourn_inferior */
424 core_stratum, 0, /* next */
425 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
426 0, 0, /* section pointers */
427 OPS_MAGIC, /* Always the last thing */
428 };
429
430 void
431 _initialize_core()
432 {
433
434 add_com ("core-file", class_files, core_file_command,
435 "Use FILE as core dump for examining memory and registers.\n\
436 No arg means have no core file. This command has been superseded by the\n\
437 `target core' and `detach' commands.");
438 add_target (&core_ops);
439 }
This page took 0.038254 seconds and 5 git commands to generate.