7b4ac7e1 |
1 | /* Work with core dump and executable files, for GDB. |
e91b87a3 |
2 | Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. |
7b4ac7e1 |
3 | |
4187119d |
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. */ |
7b4ac7e1 |
19 | |
7a67dd45 |
20 | #include <stdio.h> |
7b4ac7e1 |
21 | #include "defs.h" |
22 | #include "param.h" |
4187119d |
23 | #include "frame.h" /* required by inferior.h */ |
24 | #include "inferior.h" |
7b4ac7e1 |
25 | |
e91b87a3 |
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 |
7b4ac7e1 |
34 | #include <a.out.h> |
e91b87a3 |
35 | #endif |
e91b87a3 |
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 |
632ea0cc |
43 | #include <signal.h> |
7b4ac7e1 |
44 | #include <sys/param.h> |
45 | #include <sys/dir.h> |
46 | #include <sys/file.h> |
47 | #include <sys/stat.h> |
48 | |
7b4ac7e1 |
49 | #ifdef UMAX_CORE |
50 | #include <sys/ptrace.h> |
e91b87a3 |
51 | #else |
7b4ac7e1 |
52 | #include <sys/user.h> |
e91b87a3 |
53 | #endif |
7b4ac7e1 |
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 | |
7b4ac7e1 |
63 | #ifndef COFF_FORMAT |
4187119d |
64 | #ifndef AOUTHDR |
e91b87a3 |
65 | #define AOUTHDR struct exec |
7b4ac7e1 |
66 | #endif |
4187119d |
67 | #endif |
7b4ac7e1 |
68 | |
632ea0cc |
69 | extern char *sys_siglist[]; |
70 | |
e91b87a3 |
71 | extern core_file_command (), exec_file_command (); |
7b4ac7e1 |
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 | |
e91b87a3 |
79 | char *corefile; |
80 | char *execfile; |
7b4ac7e1 |
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 | |
e91b87a3 |
86 | int corechan; |
87 | int execchan; |
7b4ac7e1 |
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 | |
e91b87a3 |
96 | CORE_ADDR data_start; |
97 | CORE_ADDR data_end; |
98 | CORE_ADDR stack_start; |
99 | CORE_ADDR stack_end; |
7b4ac7e1 |
100 | |
7a67dd45 |
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 | |
7b4ac7e1 |
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 | |
632ea0cc |
110 | CORE_ADDR text_start; |
111 | CORE_ADDR text_end; |
112 | |
e91b87a3 |
113 | CORE_ADDR exec_data_start; |
114 | CORE_ADDR exec_data_end; |
7b4ac7e1 |
115 | |
1c997a4a |
116 | /* Offset within executable file of start of text area data. */ |
7b4ac7e1 |
117 | |
e91b87a3 |
118 | int text_offset; |
7b4ac7e1 |
119 | |
1c997a4a |
120 | /* Offset within executable file of start of data area data. */ |
7b4ac7e1 |
121 | |
e91b87a3 |
122 | int exec_data_offset; |
7b4ac7e1 |
123 | |
1c997a4a |
124 | /* Offset within core file of start of data area data. */ |
7b4ac7e1 |
125 | |
e91b87a3 |
126 | int data_offset; |
7b4ac7e1 |
127 | |
1c997a4a |
128 | /* Offset within core file of start of stack area data. */ |
7b4ac7e1 |
129 | |
e91b87a3 |
130 | int stack_offset; |
7b4ac7e1 |
131 | |
132 | #ifdef COFF_FORMAT |
133 | /* various coff data structures */ |
134 | |
e91b87a3 |
135 | FILHDR file_hdr; |
136 | SCNHDR text_hdr; |
137 | SCNHDR data_hdr; |
7b4ac7e1 |
138 | |
139 | #endif /* not COFF_FORMAT */ |
140 | |
141 | /* a.out header saved in core file. */ |
142 | |
e91b87a3 |
143 | AOUTHDR core_aouthdr; |
7b4ac7e1 |
144 | |
145 | /* a.out header of exec file. */ |
146 | |
e91b87a3 |
147 | AOUTHDR exec_aouthdr; |
7b4ac7e1 |
148 | |
e91b87a3 |
149 | void validate_files (); |
7b4ac7e1 |
150 | unsigned int register_addr (); |
151 | \f |
7b4ac7e1 |
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 | |
e91b87a3 |
155 | void |
7b4ac7e1 |
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 | |
e91b87a3 |
166 | void |
7b4ac7e1 |
167 | close_exec_file () |
168 | { |
169 | if (execchan >= 0) |
170 | close (execchan); |
171 | execchan = -1; |
172 | } |
173 | |
e91b87a3 |
174 | void |
7b4ac7e1 |
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 | |
e91b87a3 |
190 | void |
7b4ac7e1 |
191 | validate_files () |
192 | { |
193 | if (execfile != 0 && corefile != 0) |
194 | { |
195 | struct stat st_core; |
196 | |
7a67dd45 |
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; |
7b4ac7e1 |
202 | |
e91b87a3 |
203 | if (N_MAGIC (core_aouthdr) != 0 |
7b4ac7e1 |
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 | |
3bf57d21 |
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 | |
7b4ac7e1 |
215 | char * |
3bf57d21 |
216 | get_exec_file (err) |
217 | int err; |
7b4ac7e1 |
218 | { |
3bf57d21 |
219 | if (err && execfile == 0) |
7b4ac7e1 |
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"); |
4187119d |
241 | if (corefile == 0) |
e91b87a3 |
242 | printf ("No core dump file\n"); |
4187119d |
243 | else |
244 | printf ("Core dump file \"%s\".\n", corefile); |
7b4ac7e1 |
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) |
4187119d |
251 | printf ("Symbols from \"%s\".\n", symfile); |
252 | |
253 | #ifdef FILES_INFO_HOOK |
254 | if (FILES_INFO_HOOK ()) |
255 | return; |
256 | #endif |
7b4ac7e1 |
257 | |
258 | if (! have_inferior_p ()) |
259 | { |
260 | if (execfile) |
261 | { |
e91b87a3 |
262 | printf ("Text segment in executable from 0x%x to 0x%x.\n", |
7b4ac7e1 |
263 | text_start, text_end); |
e91b87a3 |
264 | printf ("Data segment in executable from 0x%x to 0x%x.\n", |
265 | exec_data_start, exec_data_end); |
266 | if (corefile) |
4187119d |
267 | printf ("(But since we have a core file, we're using...)\n"); |
7b4ac7e1 |
268 | } |
269 | if (corefile) |
270 | { |
4187119d |
271 | printf ("Data segment in core file from 0x%x to 0x%x.\n", |
e91b87a3 |
272 | data_start, data_end); |
273 | printf ("Stack segment in core file from 0x%x to 0x%x.\n", |
274 | stack_start, stack_end); |
7b4ac7e1 |
275 | } |
276 | } |
277 | } |
278 | \f |
e91b87a3 |
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. */ |
7b4ac7e1 |
282 | |
e91b87a3 |
283 | int |
7b4ac7e1 |
284 | read_memory (memaddr, myaddr, len) |
285 | CORE_ADDR memaddr; |
286 | char *myaddr; |
287 | int len; |
288 | { |
4187119d |
289 | if (len == 0) |
290 | return 0; |
291 | |
7b4ac7e1 |
292 | if (have_inferior_p ()) |
4187119d |
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 | } |
7b4ac7e1 |
299 | else |
e91b87a3 |
300 | return xfer_core_file (memaddr, myaddr, len); |
7b4ac7e1 |
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 ()) |
4187119d |
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 | } |
7b4ac7e1 |
320 | else |
321 | error ("Can write memory only when program being debugged is running."); |
322 | } |
323 | |
4187119d |
324 | #ifndef XFER_CORE_FILE |
e91b87a3 |
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 |
7b4ac7e1 |
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; |
e91b87a3 |
344 | int returnval = 0; |
7b4ac7e1 |
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. |
e91b87a3 |
355 | |
7b4ac7e1 |
356 | If desired address is nonexistent, leave them zero. |
e91b87a3 |
357 | |
7b4ac7e1 |
358 | i is set to the number of bytes that can be handled |
e91b87a3 |
359 | along with the next address. |
360 | |
361 | We put the most likely tests first for efficiency. */ |
7b4ac7e1 |
362 | |
7b4ac7e1 |
363 | /* Note that if there is no core file |
364 | data_start and data_end are equal. */ |
e91b87a3 |
365 | if (memaddr >= data_start && memaddr < data_end) |
7b4ac7e1 |
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 | } |
7a67dd45 |
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 | |
7b4ac7e1 |
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 | } |
e91b87a3 |
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 | } |
4187119d |
417 | else if (corechan >= 0 |
418 | && memaddr >= data_end && memaddr < stack_start) |
e91b87a3 |
419 | { |
420 | i = min (len, stack_start - memaddr); |
421 | } |
4187119d |
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 | } |
7a67dd45 |
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. */ |
e91b87a3 |
436 | else if (memaddr >= stack_end && stack_end != 0) |
7a67dd45 |
437 | #endif /* no REG_STACK_SEGMENT. */ |
e91b87a3 |
438 | { |
4187119d |
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; |
e91b87a3 |
442 | } |
443 | else |
444 | { |
445 | /* Address did not classify into one of the known ranges. |
4187119d |
446 | This shouldn't happen; we catch the endpoints. */ |
447 | fatal ("Internal: Bad case logic in xfer_core_file."); |
e91b87a3 |
448 | } |
7b4ac7e1 |
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, |
e91b87a3 |
467 | read zeros if reading, or do nothing if writing. |
4187119d |
468 | Actually, we never right. */ |
7b4ac7e1 |
469 | else |
e91b87a3 |
470 | { |
471 | bzero (myaddr, i); |
472 | returnval = 1; |
473 | } |
7b4ac7e1 |
474 | |
475 | memaddr += i; |
476 | myaddr += i; |
477 | len -= i; |
478 | } |
e91b87a3 |
479 | return returnval; |
7b4ac7e1 |
480 | } |
4187119d |
481 | #endif /* XFER_CORE_FILE */ |
7b4ac7e1 |
482 | \f |
483 | /* My replacement for the read system call. |
484 | Used like `read' but keeps going if `read' returns too soon. */ |
485 | |
e91b87a3 |
486 | int |
7b4ac7e1 |
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 | } |
3bf57d21 |
505 | return orglen; |
7b4ac7e1 |
506 | } |
507 | \f |
3bf57d21 |
508 | #ifdef REGISTER_U_ADDR |
7b4ac7e1 |
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 | |
7b4ac7e1 |
523 | REGISTER_U_ADDR (addr, blockend, regno); |
524 | |
525 | return addr; |
526 | } |
527 | |
3bf57d21 |
528 | #endif /* REGISTER_U_ADDR */ |
7b4ac7e1 |
529 | \f |
e91b87a3 |
530 | void |
531 | _initialize_core() |
7b4ac7e1 |
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 | |