gdb-3.5
[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 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "frame.h" /* required by inferior.h */
24 #include "inferior.h"
25
26 #ifdef USG
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #endif
30
31 #ifdef COFF_ENCAPSULATE
32 #include "a.out.encap.h"
33 #else
34 #include <a.out.h>
35 #endif
36 #ifndef N_MAGIC
37 #ifdef COFF_FORMAT
38 #define N_MAGIC(exec) ((exec).magic)
39 #else
40 #define N_MAGIC(exec) ((exec).a_magic)
41 #endif
42 #endif
43 #include <signal.h>
44 #include <sys/param.h>
45 #include <sys/dir.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48
49 #ifdef UMAX_CORE
50 #include <sys/ptrace.h>
51 #else
52 #include <sys/user.h>
53 #endif
54
55 #ifndef N_TXTADDR
56 #define N_TXTADDR(hdr) 0
57 #endif /* no N_TXTADDR */
58
59 #ifndef N_DATADDR
60 #define N_DATADDR(hdr) hdr.a_text
61 #endif /* no N_DATADDR */
62
63 #ifndef COFF_FORMAT
64 #ifndef AOUTHDR
65 #define AOUTHDR struct exec
66 #endif
67 #endif
68
69 extern char *sys_siglist[];
70
71 extern core_file_command (), exec_file_command ();
72
73 /* Hook for `exec_file_command' command to call. */
74
75 void (*exec_file_display_hook) ();
76
77 /* File names of core file and executable file. */
78
79 char *corefile;
80 char *execfile;
81
82 /* Descriptors on which core file and executable file are open.
83 Note that the execchan is closed when an inferior is created
84 and reopened if the inferior dies or is killed. */
85
86 int corechan;
87 int execchan;
88
89 /* Last modification time of executable file.
90 Also used in source.c to compare against mtime of a source file. */
91
92 int exec_mtime;
93
94 /* Virtual addresses of bounds of the two areas of memory in the core file. */
95
96 CORE_ADDR data_start;
97 CORE_ADDR data_end;
98 CORE_ADDR stack_start;
99 CORE_ADDR stack_end;
100
101 #if defined (REG_STACK_SEGMENT)
102 /* Start and end of the register stack segment. */
103 CORE_ADDR reg_stack_start;
104 CORE_ADDR reg_stack_end;
105 #endif /* REG_STACK_SEGMENT */
106
107 /* Virtual addresses of bounds of two areas of memory in the exec file.
108 Note that the data area in the exec file is used only when there is no core file. */
109
110 CORE_ADDR text_start;
111 CORE_ADDR text_end;
112
113 CORE_ADDR exec_data_start;
114 CORE_ADDR exec_data_end;
115
116 /* Offset within executable file of start of text area data. */
117
118 int text_offset;
119
120 /* Offset within executable file of start of data area data. */
121
122 int exec_data_offset;
123
124 /* Offset within core file of start of data area data. */
125
126 int data_offset;
127
128 /* Offset within core file of start of stack area data. */
129
130 int stack_offset;
131
132 #ifdef COFF_FORMAT
133 /* various coff data structures */
134
135 FILHDR file_hdr;
136 SCNHDR text_hdr;
137 SCNHDR data_hdr;
138
139 #endif /* not COFF_FORMAT */
140
141 /* a.out header saved in core file. */
142
143 AOUTHDR core_aouthdr;
144
145 /* a.out header of exec file. */
146
147 AOUTHDR exec_aouthdr;
148
149 void validate_files ();
150 unsigned int register_addr ();
151 \f
152 /* Call this to specify the hook for exec_file_command to call back.
153 This is called from the x-window display code. */
154
155 void
156 specify_exec_file_hook (hook)
157 void (*hook) ();
158 {
159 exec_file_display_hook = hook;
160 }
161
162 /* The exec file must be closed before running an inferior.
163 If it is needed again after the inferior dies, it must
164 be reopened. */
165
166 void
167 close_exec_file ()
168 {
169 if (execchan >= 0)
170 close (execchan);
171 execchan = -1;
172 }
173
174 void
175 reopen_exec_file ()
176 {
177 if (execchan < 0 && execfile != 0)
178 {
179 char *filename = concat (execfile, "", "");
180 exec_file_command (filename, 0);
181 free (filename);
182 }
183 }
184 \f
185 /* If we have both a core file and an exec file,
186 print a warning if they don't go together.
187 This should really check that the core file came
188 from that exec file, but I don't know how to do it. */
189
190 void
191 validate_files ()
192 {
193 if (execfile != 0 && corefile != 0)
194 {
195 struct stat st_core;
196
197 if (fstat (corechan, &st_core) < 0)
198 /* It might be a good idea to print an error message.
199 On the other hand, if the user tries to *do* anything with
200 the core file, (s)he'll find out soon enough. */
201 return;
202
203 if (N_MAGIC (core_aouthdr) != 0
204 && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
205 printf ("Warning: core file does not match specified executable file.\n");
206 else if (exec_mtime > st_core.st_mtime)
207 printf ("Warning: exec file is newer than core file.\n");
208 }
209 }
210
211 /* Return the name of the executable file as a string.
212 ERR nonzero means get error if there is none specified;
213 otherwise return 0 in that case. */
214
215 char *
216 get_exec_file (err)
217 int err;
218 {
219 if (err && execfile == 0)
220 error ("No executable file specified.\n\
221 Use the \"exec-file\" and \"symbol-file\" commands.");
222 return execfile;
223 }
224
225 int
226 have_core_file_p ()
227 {
228 return corefile != 0;
229 }
230
231 static void
232 files_info ()
233 {
234 char *symfile;
235 extern char *get_sym_file ();
236
237 if (execfile)
238 printf ("Executable file \"%s\".\n", execfile);
239 else
240 printf ("No executable file\n");
241 if (corefile == 0)
242 printf ("No core dump file\n");
243 else
244 printf ("Core dump file \"%s\".\n", corefile);
245
246 if (have_inferior_p ())
247 printf ("Using the running image of the program, rather than these files.\n");
248
249 symfile = get_sym_file ();
250 if (symfile != 0)
251 printf ("Symbols from \"%s\".\n", symfile);
252
253 #ifdef FILES_INFO_HOOK
254 if (FILES_INFO_HOOK ())
255 return;
256 #endif
257
258 if (! have_inferior_p ())
259 {
260 if (execfile)
261 {
262 printf ("Text segment in executable from 0x%x to 0x%x.\n",
263 text_start, text_end);
264 printf ("Data segment in executable from 0x%x to 0x%x.\n",
265 exec_data_start, exec_data_end);
266 if (corefile)
267 printf ("(But since we have a core file, we're using...)\n");
268 }
269 if (corefile)
270 {
271 printf ("Data segment in core file from 0x%x to 0x%x.\n",
272 data_start, data_end);
273 printf ("Stack segment in core file from 0x%x to 0x%x.\n",
274 stack_start, stack_end);
275 }
276 }
277 }
278 \f
279 /* Read "memory data" from core file and/or executable file.
280 Returns zero if successful, 1 if xfer_core_file failed, errno value if
281 ptrace failed. */
282
283 int
284 read_memory (memaddr, myaddr, len)
285 CORE_ADDR memaddr;
286 char *myaddr;
287 int len;
288 {
289 if (len == 0)
290 return 0;
291
292 if (have_inferior_p ())
293 {
294 if (remote_debugging)
295 return remote_read_inferior_memory (memaddr, myaddr, len);
296 else
297 return read_inferior_memory (memaddr, myaddr, len);
298 }
299 else
300 return xfer_core_file (memaddr, myaddr, len);
301 }
302
303 /* Write LEN bytes of data starting at address MYADDR
304 into debugged program memory at address MEMADDR.
305 Returns zero if successful, or an errno value if ptrace failed. */
306
307 int
308 write_memory (memaddr, myaddr, len)
309 CORE_ADDR memaddr;
310 char *myaddr;
311 int len;
312 {
313 if (have_inferior_p ())
314 {
315 if (remote_debugging)
316 return remote_write_inferior_memory (memaddr, myaddr, len);
317 else
318 return write_inferior_memory (memaddr, myaddr, len);
319 }
320 else
321 error ("Can write memory only when program being debugged is running.");
322 }
323
324 #ifndef XFER_CORE_FILE
325 /* Read from the program's memory (except for inferior processes).
326 This function is misnamed, since it only reads, never writes; and
327 since it will use the core file and/or executable file as necessary.
328
329 It should be extended to write as well as read, FIXME, for patching files.
330
331 Return 0 if address could be read, 1 if not. */
332
333 int
334 xfer_core_file (memaddr, myaddr, len)
335 CORE_ADDR memaddr;
336 char *myaddr;
337 int len;
338 {
339 register int i;
340 register int val;
341 int xferchan;
342 char **xferfile;
343 int fileptr;
344 int returnval = 0;
345
346 while (len > 0)
347 {
348 xferfile = 0;
349 xferchan = 0;
350
351 /* Determine which file the next bunch of addresses reside in,
352 and where in the file. Set the file's read/write pointer
353 to point at the proper place for the desired address
354 and set xferfile and xferchan for the correct file.
355
356 If desired address is nonexistent, leave them zero.
357
358 i is set to the number of bytes that can be handled
359 along with the next address.
360
361 We put the most likely tests first for efficiency. */
362
363 /* Note that if there is no core file
364 data_start and data_end are equal. */
365 if (memaddr >= data_start && memaddr < data_end)
366 {
367 i = min (len, data_end - memaddr);
368 fileptr = memaddr - data_start + data_offset;
369 xferfile = &corefile;
370 xferchan = corechan;
371 }
372 /* Note that if there is no core file
373 stack_start and stack_end are equal. */
374 else if (memaddr >= stack_start && memaddr < stack_end)
375 {
376 i = min (len, stack_end - memaddr);
377 fileptr = memaddr - stack_start + stack_offset;
378 xferfile = &corefile;
379 xferchan = corechan;
380 }
381 #ifdef REG_STACK_SEGMENT
382 /* Pyramids have an extra segment in the virtual address space
383 for the (control) stack of register-window frames */
384 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
385 {
386 i = min (len, reg_stack_end - memaddr);
387 fileptr = memaddr - reg_stack_start + reg_stack_offset;
388 xferfile = &corefile;
389 xferchan = corechan;
390 }
391 #endif /* REG_STACK_SEGMENT */
392
393 else if (corechan < 0
394 && memaddr >= exec_data_start && memaddr < exec_data_end)
395 {
396 i = min (len, exec_data_end - memaddr);
397 fileptr = memaddr - exec_data_start + exec_data_offset;
398 xferfile = &execfile;
399 xferchan = execchan;
400 }
401 else if (memaddr >= text_start && memaddr < text_end)
402 {
403 i = min (len, text_end - memaddr);
404 fileptr = memaddr - text_start + text_offset;
405 xferfile = &execfile;
406 xferchan = execchan;
407 }
408 else if (memaddr < text_start)
409 {
410 i = min (len, text_start - memaddr);
411 }
412 else if (memaddr >= text_end
413 && memaddr < (corechan >= 0? data_start : exec_data_start))
414 {
415 i = min (len, data_start - memaddr);
416 }
417 else if (corechan >= 0
418 && memaddr >= data_end && memaddr < stack_start)
419 {
420 i = min (len, stack_start - memaddr);
421 }
422 else if (corechan < 0 && memaddr >= exec_data_end)
423 {
424 /* Since there is nothing at higher addresses than data
425 (without a core file or an inferior, there is no
426 stack, set i to do the rest of the operation now. */
427 i = len;
428 }
429 #ifdef REG_STACK_SEGMENT
430 else if (memaddr >= reg_stack_end && reg_stack_end != 0)
431 {
432 i = min (len, reg_stack_start - memaddr);
433 }
434 else if (memaddr >= stack_end && memaddr < reg_stack_start)
435 #else /* no REG_STACK_SEGMENT. */
436 else if (memaddr >= stack_end && stack_end != 0)
437 #endif /* no REG_STACK_SEGMENT. */
438 {
439 /* Since there is nothing at higher addresses than
440 the stack, set i to do the rest of the operation now. */
441 i = len;
442 }
443 else
444 {
445 /* Address did not classify into one of the known ranges.
446 This shouldn't happen; we catch the endpoints. */
447 fatal ("Internal: Bad case logic in xfer_core_file.");
448 }
449
450 /* Now we know which file to use.
451 Set up its pointer and transfer the data. */
452 if (xferfile)
453 {
454 if (*xferfile == 0)
455 if (xferfile == &execfile)
456 error ("No program file to examine.");
457 else
458 error ("No core dump file or running program to examine.");
459 val = lseek (xferchan, fileptr, 0);
460 if (val < 0)
461 perror_with_name (*xferfile);
462 val = myread (xferchan, myaddr, i);
463 if (val < 0)
464 perror_with_name (*xferfile);
465 }
466 /* If this address is for nonexistent memory,
467 read zeros if reading, or do nothing if writing.
468 Actually, we never right. */
469 else
470 {
471 bzero (myaddr, i);
472 returnval = 1;
473 }
474
475 memaddr += i;
476 myaddr += i;
477 len -= i;
478 }
479 return returnval;
480 }
481 #endif /* XFER_CORE_FILE */
482 \f
483 /* My replacement for the read system call.
484 Used like `read' but keeps going if `read' returns too soon. */
485
486 int
487 myread (desc, addr, len)
488 int desc;
489 char *addr;
490 int len;
491 {
492 register int val;
493 int orglen = len;
494
495 while (len > 0)
496 {
497 val = read (desc, addr, len);
498 if (val < 0)
499 return val;
500 if (val == 0)
501 return orglen - len;
502 len -= val;
503 addr += val;
504 }
505 return orglen;
506 }
507 \f
508 #ifdef REGISTER_U_ADDR
509
510 /* Return the address in the core dump or inferior of register REGNO.
511 BLOCKEND is the address of the end of the user structure. */
512
513 unsigned int
514 register_addr (regno, blockend)
515 int regno;
516 int blockend;
517 {
518 int addr;
519
520 if (regno < 0 || regno >= NUM_REGS)
521 error ("Invalid register number %d.", regno);
522
523 REGISTER_U_ADDR (addr, blockend, regno);
524
525 return addr;
526 }
527
528 #endif /* REGISTER_U_ADDR */
529 \f
530 void
531 _initialize_core()
532 {
533 corechan = -1;
534 execchan = -1;
535 corefile = 0;
536 execfile = 0;
537 exec_file_display_hook = 0;
538
539 text_start = 0;
540 text_end = 0;
541 data_start = 0;
542 data_end = 0;
543 exec_data_start = 0;
544 exec_data_end = 0;
545 stack_start = STACK_END_ADDR;
546 stack_end = STACK_END_ADDR;
547
548 add_com ("core-file", class_files, core_file_command,
549 "Use FILE as core dump for examining memory and registers.\n\
550 No arg means have no core file.");
551 add_com ("exec-file", class_files, exec_file_command,
552 "Use FILE as program for getting contents of pure memory.\n\
553 If FILE cannot be found as specified, your execution directory path\n\
554 is searched for a command of that name.\n\
555 No arg means have no executable file.");
556 add_info ("files", files_info, "Names of files being debugged.");
557 }
558