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