1 /* Work with core dump and executable files, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
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)
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.
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. */
23 #include "frame.h" /* required by inferior.h */
27 #include <sys/types.h>
31 #ifdef COFF_ENCAPSULATE
32 #include "a.out.encap.h"
38 #define N_MAGIC(exec) ((exec).magic)
40 #define N_MAGIC(exec) ((exec).a_magic)
44 #include <sys/param.h>
50 #include <sys/ptrace.h>
56 #define N_TXTADDR(hdr) 0
57 #endif /* no N_TXTADDR */
60 #define N_DATADDR(hdr) hdr.a_text
61 #endif /* no N_DATADDR */
65 #define AOUTHDR struct exec
69 extern char *sys_siglist
[];
71 extern core_file_command (), exec_file_command ();
73 /* Hook for `exec_file_command' command to call. */
75 void (*exec_file_display_hook
) ();
77 /* File names of core file and executable file. */
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. */
89 /* Last modification time of executable file.
90 Also used in source.c to compare against mtime of a source file. */
94 /* Virtual addresses of bounds of the two areas of memory in the core file. */
98 CORE_ADDR stack_start
;
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 */
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. */
110 CORE_ADDR text_start
;
113 CORE_ADDR exec_data_start
;
114 CORE_ADDR exec_data_end
;
116 /* Offset within executable file of start of text area data. */
120 /* Offset within executable file of start of data area data. */
122 int exec_data_offset
;
124 /* Offset within core file of start of data area data. */
128 /* Offset within core file of start of stack area data. */
133 /* various coff data structures */
139 #endif /* not COFF_FORMAT */
141 /* a.out header saved in core file. */
143 AOUTHDR core_aouthdr
;
145 /* a.out header of exec file. */
147 AOUTHDR exec_aouthdr
;
149 void validate_files ();
150 unsigned int register_addr ();
152 /* Call this to specify the hook for exec_file_command to call back.
153 This is called from the x-window display code. */
156 specify_exec_file_hook (hook
)
159 exec_file_display_hook
= hook
;
162 /* The exec file must be closed before running an inferior.
163 If it is needed again after the inferior dies, it must
177 if (execchan
< 0 && execfile
!= 0)
179 char *filename
= concat (execfile
, "", "");
180 exec_file_command (filename
, 0);
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. */
193 if (execfile
!= 0 && corefile
!= 0)
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. */
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");
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. */
219 if (err
&& execfile
== 0)
220 error ("No executable file specified.\n\
221 Use the \"exec-file\" and \"symbol-file\" commands.");
228 return corefile
!= 0;
235 extern char *get_sym_file ();
238 printf ("Executable file \"%s\".\n", execfile
);
240 printf ("No executable file\n");
242 printf ("No core dump file\n");
244 printf ("Core dump file \"%s\".\n", corefile
);
246 if (have_inferior_p ())
247 printf ("Using the running image of the program, rather than these files.\n");
249 symfile
= get_sym_file ();
251 printf ("Symbols from \"%s\".\n", symfile
);
253 #ifdef FILES_INFO_HOOK
254 if (FILES_INFO_HOOK ())
258 if (! have_inferior_p ())
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
);
267 printf ("(But since we have a core file, we're using...)\n");
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
);
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
284 read_memory (memaddr
, myaddr
, len
)
292 if (have_inferior_p ())
294 if (remote_debugging
)
295 return remote_read_inferior_memory (memaddr
, myaddr
, len
);
297 return read_inferior_memory (memaddr
, myaddr
, len
);
300 return xfer_core_file (memaddr
, myaddr
, len
);
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. */
308 write_memory (memaddr
, myaddr
, len
)
313 if (have_inferior_p ())
315 if (remote_debugging
)
316 return remote_write_inferior_memory (memaddr
, myaddr
, len
);
318 return write_inferior_memory (memaddr
, myaddr
, len
);
321 error ("Can write memory only when program being debugged is running.");
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.
329 It should be extended to write as well as read, FIXME, for patching files.
331 Return 0 if address could be read, 1 if not. */
334 xfer_core_file (memaddr
, myaddr
, len
)
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.
356 If desired address is nonexistent, leave them zero.
358 i is set to the number of bytes that can be handled
359 along with the next address.
361 We put the most likely tests first for efficiency. */
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
)
367 i
= min (len
, data_end
- memaddr
);
368 fileptr
= memaddr
- data_start
+ data_offset
;
369 xferfile
= &corefile
;
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
)
376 i
= min (len
, stack_end
- memaddr
);
377 fileptr
= memaddr
- stack_start
+ stack_offset
;
378 xferfile
= &corefile
;
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
)
386 i
= min (len
, reg_stack_end
- memaddr
);
387 fileptr
= memaddr
- reg_stack_start
+ reg_stack_offset
;
388 xferfile
= &corefile
;
391 #endif /* REG_STACK_SEGMENT */
393 else if (corechan
< 0
394 && memaddr
>= exec_data_start
&& memaddr
< exec_data_end
)
396 i
= min (len
, exec_data_end
- memaddr
);
397 fileptr
= memaddr
- exec_data_start
+ exec_data_offset
;
398 xferfile
= &execfile
;
401 else if (memaddr
>= text_start
&& memaddr
< text_end
)
403 i
= min (len
, text_end
- memaddr
);
404 fileptr
= memaddr
- text_start
+ text_offset
;
405 xferfile
= &execfile
;
408 else if (memaddr
< text_start
)
410 i
= min (len
, text_start
- memaddr
);
412 else if (memaddr
>= text_end
413 && memaddr
< (corechan
>= 0? data_start
: exec_data_start
))
415 i
= min (len
, data_start
- memaddr
);
417 else if (corechan
>= 0
418 && memaddr
>= data_end
&& memaddr
< stack_start
)
420 i
= min (len
, stack_start
- memaddr
);
422 else if (corechan
< 0 && memaddr
>= exec_data_end
)
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. */
429 #ifdef REG_STACK_SEGMENT
430 else if (memaddr
>= reg_stack_end
&& reg_stack_end
!= 0)
432 i
= min (len
, reg_stack_start
- memaddr
);
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. */
439 /* Since there is nothing at higher addresses than
440 the stack, set i to do the rest of the operation now. */
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.");
450 /* Now we know which file to use.
451 Set up its pointer and transfer the data. */
455 if (xferfile
== &execfile
)
456 error ("No program file to examine.");
458 error ("No core dump file or running program to examine.");
459 val
= lseek (xferchan
, fileptr
, 0);
461 perror_with_name (*xferfile
);
462 val
= myread (xferchan
, myaddr
, i
);
464 perror_with_name (*xferfile
);
466 /* If this address is for nonexistent memory,
467 read zeros if reading, or do nothing if writing.
468 Actually, we never right. */
481 #endif /* XFER_CORE_FILE */
483 /* My replacement for the read system call.
484 Used like `read' but keeps going if `read' returns too soon. */
487 myread (desc
, addr
, len
)
497 val
= read (desc
, addr
, len
);
508 #ifdef REGISTER_U_ADDR
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. */
514 register_addr (regno
, blockend
)
520 if (regno
< 0 || regno
>= NUM_REGS
)
521 error ("Invalid register number %d.", regno
);
523 REGISTER_U_ADDR (addr
, blockend
, regno
);
528 #endif /* REGISTER_U_ADDR */
537 exec_file_display_hook
= 0;
545 stack_start
= STACK_END_ADDR
;
546 stack_end
= STACK_END_ADDR
;
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.");