1 /* Work with core dump and executable files, for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
21 #include "initialize.h"
28 #include <sys/param.h>
33 /* Recognize COFF format systems because a.out.h defines AOUTHDR. */
40 #else /* not NEW_SUN_CORE */
42 #include <sys/ptrace.h>
43 #else /* not UMAX_CORE */
49 #define e_PS e_regs[PS]
50 #define e_PC e_regs[PC]
51 #endif /* HPUX_VERSION_5 */
53 #endif /* not UMAX_CORE */
54 #endif /* not NEW_SUN_CORE */
57 #define N_TXTADDR(hdr) 0
58 #endif /* no N_TXTADDR */
61 #define N_DATADDR(hdr) hdr.a_text
62 #endif /* no N_DATADDR */
64 /* Make COFF and non-COFF names for things a little more compatible
65 to reduce conditionals later. */
72 #define AOUTHDR struct exec
75 extern char *sys_siglist
[];
79 /* Hook for `exec_file_command' command to call. */
81 void (*exec_file_display_hook
) ();
83 /* File names of core file and executable file. */
85 static char *corefile
;
86 static char *execfile
;
88 /* Descriptors on which core file and executable file are open.
89 Note that the execchan is closed when an inferior is created
90 and reopened if the inferior dies or is killed. */
95 /* Last modification time of executable file.
96 Also used in source.c to compare against mtime of a source file. */
100 /* Virtual addresses of bounds of the two areas of memory in the core file. */
102 static CORE_ADDR data_start
;
103 static CORE_ADDR data_end
;
104 static CORE_ADDR stack_start
;
105 static CORE_ADDR stack_end
;
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 static CORE_ADDR exec_data_start
;
114 static CORE_ADDR exec_data_end
;
116 /* Address in executable file of start of text area data. */
118 static int text_offset
;
120 /* Address in executable file of start of data area data. */
122 static int exec_data_offset
;
124 /* Address in core file of start of data area data. */
126 static int data_offset
;
128 /* Address in core file of start of stack area data. */
130 static int stack_offset
;
133 /* various coff data structures */
135 static FILHDR file_hdr
;
136 static SCNHDR text_hdr
;
137 static SCNHDR data_hdr
;
139 #endif /* not COFF_FORMAT */
141 /* a.out header saved in core file. */
143 static AOUTHDR core_aouthdr
;
145 /* a.out header of exec file. */
147 static AOUTHDR exec_aouthdr
;
149 static void validate_files ();
150 unsigned int register_addr ();
152 core_file_command (filename
, from_tty
)
157 extern char registers
[];
159 /* Discard all vestiges of any previous core file
160 and mark data and stack spaces as empty. */
172 stack_start
= STACK_END_ADDR
;
173 stack_end
= STACK_END_ADDR
;
175 /* Now, if a new core file was specified, open it and digest it. */
179 if (have_inferior_p ())
180 error ("To look at a core file, you must kill the inferior with \"kill\".");
181 corechan
= open (filename
, O_RDONLY
, 0);
183 perror_with_name (filename
);
188 val
= myread (corechan
, &corestr
, sizeof corestr
);
190 perror_with_name (filename
);
191 if (corestr
.c_magic
!= CORE_MAGIC
)
192 error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
193 filename
, corestr
.c_magic
, (int) CORE_MAGIC
);
194 else if (sizeof (struct core
) != corestr
.c_len
)
195 error ("\"%s\" has an invalid struct core length (%d, expected %d)",
196 filename
, corestr
.c_len
, (int) sizeof (struct core
));
198 data_start
= exec_data_start
;
199 data_end
= data_start
+ corestr
.c_dsize
;
200 stack_start
= stack_end
- corestr
.c_ssize
;
201 data_offset
= sizeof corestr
;
202 stack_offset
= sizeof corestr
+ corestr
.c_dsize
;
204 #if defined(sun2) || defined(sun3)
205 bcopy (&corestr
.c_regs
, registers
, 16 * 4);
206 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = corestr
.c_regs
.r_ps
;
207 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = corestr
.c_regs
.r_pc
;
208 bcopy (corestr
.c_fpstatus
.fps_regs
,
209 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
210 sizeof corestr
.c_fpstatus
.fps_regs
);
211 bcopy (&corestr
.c_fpstatus
.fps_control
,
212 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
213 sizeof corestr
.c_fpstatus
- sizeof corestr
.c_fpstatus
.fps_regs
);
216 /* G0 *always* holds 0. */
217 *(int *)®isters
[REGISTER_BYTE (0)] = 0;
218 /* The globals and output registers. I don't know where
219 to get the locals and input registers from the core file. */
220 bcopy (&corestr
.c_regs
.r_g1
, registers
, 15 * 4);
221 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = corestr
.c_regs
.r_ps
;
222 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = corestr
.c_regs
.r_pc
;
223 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)] = corestr
.c_regs
.r_npc
;
224 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)] = corestr
.c_regs
.r_y
;
225 bcopy (corestr
.c_fpu
.fpu_regs
,
226 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
227 sizeof corestr
.c_fpu
.fpu_regs
);
229 bcopy (&corestr
.c_fpu
.fpu_fsr
,
230 ®isters
[REGISTER_BYTE (FPS_REGNUM
)],
231 sizeof (FPU_FSR_TYPE
));
235 bcopy (&corestr
.c_aouthdr
, &core_aouthdr
, sizeof (struct exec
));
237 printf ("Core file is from \"%s\".\n", corestr
.c_cmdname
);
238 if (corestr
.c_signo
> 0)
239 printf ("Program terminated with signal %d, %s.\n",
241 corestr
.c_signo
< NSIG
242 ? sys_siglist
[corestr
.c_signo
]
245 #else /* not NEW_SUN_CORE */
246 /* 4.2-style (and perhaps also sysV-style) core dump file. */
249 struct ptrace_user u
;
255 val
= myread (corechan
, &u
, sizeof u
);
257 perror_with_name (filename
);
258 data_start
= exec_data_start
;
261 data_end
= data_start
+ u
.pt_dsize
;
262 stack_start
= stack_end
- u
.pt_ssize
;
263 data_offset
= sizeof u
;
264 stack_offset
= data_offset
+ u
.pt_dsize
;
267 bcopy (&u
.pt_aouthdr
, &core_aouthdr
, sizeof (AOUTHDR
));
268 printf ("Core file is from \"%s\".\n", u
.pt_comm
);
270 printf ("Program terminated with signal %d, %s.\n",
273 ? sys_siglist
[u
.pt_signal
]
275 #else /* not UMAX_CORE */
276 data_end
= data_start
+ NBPG
* u
.u_dsize
;
277 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
278 data_offset
= NBPG
* UPAGES
;
279 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
280 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
282 /* I don't know where to find this info.
283 So, for now, mark it as not available. */
284 core_aouthdr
.a_magic
= 0;
285 #endif /* not UMAX_CORE */
287 /* Read the register values out of the core file and store
288 them where `read_register' will find them. */
293 struct exception_stack es
;
296 val
= lseek (corechan
, (REGISTER_ADDR (reg_offset
, 0)), 0);
298 perror_with_name (filename
);
299 val
= myread (corechan
, es
,
300 ((char *) &es
.e_regs
[R0
] - (char *) &es
.e_offset
));
302 perror_with_name (filename
);
303 for (regno
= 0; (regno
< PS_REGNUM
); regno
++)
304 supply_register (regno
, &es
.e_regs
[regno
+ R0
]);
306 supply_register (regno
++, &val
);
307 supply_register (regno
++, &es
.e_PC
);
308 for (; (regno
< NUM_REGS
); regno
++)
310 char buf
[MAX_REGISTER_RAW_SIZE
];
312 val
= lseek (corechan
, (FP_REGISTER_ADDR (u
, regno
)), 0);
314 perror_with_name (filename
);
316 val
= myread (corechan
, buf
, sizeof buf
);
318 perror_with_name (filename
);
319 supply_register (regno
, buf
);
322 #else /* not HP9K320 */
326 for (regno
= 0; regno
< NUM_REGS
; regno
++)
328 char buf
[MAX_REGISTER_RAW_SIZE
];
330 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
332 perror_with_name (filename
);
334 val
= myread (corechan
, buf
, sizeof buf
);
336 perror_with_name (filename
);
337 supply_register (regno
, buf
);
340 #endif /* not HP9K320 */
342 #endif /* not NEW_SUN_CORE */
343 if (filename
[0] == '/')
344 corefile
= savestring (filename
, strlen (filename
));
347 corefile
= concat (current_directory
, "/", filename
);
350 set_current_frame (read_register (FP_REGNUM
));
351 select_frame (get_current_frame (), 0);
355 printf ("No core file now.\n");
358 exec_file_command (filename
, from_tty
)
364 /* Eliminate all traces of old exec file.
365 Mark text segment as empty. */
371 data_end
-= exec_data_start
;
380 /* Now open and digest the file the user requested, if any. */
384 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
387 perror_with_name (filename
);
394 if (read_file_hdr (execchan
, &file_hdr
) < 0)
395 error ("\"%s\": not in executable format.", execfile
);
397 aout_hdrsize
= file_hdr
.f_opthdr
;
398 num_sections
= file_hdr
.f_nscns
;
400 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
401 error ("\"%s\": can't read optional aouthdr", execfile
);
403 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
) < 0)
404 error ("\"%s\": can't read text section header", execfile
);
406 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
) < 0)
407 error ("\"%s\": can't read data section header", execfile
);
409 text_start
= exec_aouthdr
.text_start
;
410 text_end
= text_start
+ exec_aouthdr
.tsize
;
411 text_offset
= text_hdr
.s_scnptr
;
412 exec_data_start
= exec_aouthdr
.data_start
;
413 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
414 exec_data_offset
= data_hdr
.s_scnptr
;
415 data_start
= exec_data_start
;
416 data_end
+= exec_data_start
;
417 exec_mtime
= file_hdr
.f_timdat
;
419 #else /* not COFF_FORMAT */
426 val
= myread (execchan
, &exec_coffhdr
, sizeof exec_coffhdr
);
428 perror_with_name (filename
);
430 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
433 perror_with_name (filename
);
435 text_start
= N_TXTADDR (exec_aouthdr
);
436 exec_data_start
= N_DATADDR (exec_aouthdr
);
438 text_offset
= N_TXTOFF (exec_coffhdr
, exec_aouthdr
);
439 exec_data_offset
= N_TXTOFF (exec_coffhdr
, exec_aouthdr
)
440 + exec_aouthdr
.a_text
;
442 text_offset
= N_TXTOFF (exec_aouthdr
);
443 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
445 text_end
= text_start
+ exec_aouthdr
.a_text
;
446 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
447 data_start
= exec_data_start
;
448 data_end
+= exec_data_start
;
450 fstat (execchan
, &st_exec
);
451 exec_mtime
= st_exec
.st_mtime
;
453 #endif /* not COFF_FORMAT */
458 printf ("No exec file now.\n");
460 /* Tell display code (if any) about the changed file name. */
461 if (exec_file_display_hook
)
462 (*exec_file_display_hook
) (filename
);
465 /* Call this to specify the hook for exec_file_command to call back.
466 This is called from the x-window display code. */
468 specify_exec_file_hook (hook
)
471 exec_file_display_hook
= hook
;
474 /* The exec file must be closed before running an inferior.
475 If it is needed again after the inferior dies, it must
487 if (execchan
< 0 && execfile
!= 0)
489 char *filename
= concat (execfile
, "", "");
490 exec_file_command (filename
, 0);
495 /* If we have both a core file and an exec file,
496 print a warning if they don't go together.
497 This should really check that the core file came
498 from that exec file, but I don't know how to do it. */
503 if (execfile
!= 0 && corefile
!= 0)
507 fstat (corechan
, &st_core
);
509 if (core_aouthdr
.a_magic
!= 0
510 && bcmp (&core_aouthdr
, &exec_aouthdr
, sizeof core_aouthdr
))
511 printf ("Warning: core file does not match specified executable file.\n");
512 else if (exec_mtime
> st_core
.st_mtime
)
513 printf ("Warning: exec file is newer than core file.\n");
517 /* Return the name of the executable file as a string.
518 ERR nonzero means get error if there is none specified;
519 otherwise return 0 in that case. */
525 if (err
&& execfile
== 0)
526 error ("No executable file specified.\n\
527 Use the \"exec-file\" and \"symbol-file\" commands.");
534 return corefile
!= 0;
541 extern char *get_sym_file ();
544 printf ("Executable file \"%s\".\n", execfile
);
546 printf ("No executable file\n");
548 printf ("No core dump file\n");
550 printf ("Core dump file \"%s\".\n", corefile
);
552 if (have_inferior_p ())
553 printf ("Using the running image of the program, rather than these files.\n");
555 symfile
= get_sym_file ();
557 printf ("Symbols loaded from \"%s\".\n", symfile
);
559 if (! have_inferior_p ())
563 printf ("Text segment from 0x%x to 0x%x.\n",
564 text_start
, text_end
);
568 printf ("Data segment from 0x%x to 0x%x.\nStack segment from 0x%x to 0x%x.\n",
569 data_start
, data_end
, stack_start
, stack_end
);
573 printf ("Data segment in executable from 0x%x to 0x%x.\n",
574 exec_data_start
, exec_data_end
);
579 /* Read "memory data" from core file and/or executable file */
581 read_memory (memaddr
, myaddr
, len
)
586 if (have_inferior_p ())
587 read_inferior_memory (memaddr
, myaddr
, len
);
589 xfer_core_file (memaddr
, myaddr
, len
, 0);
592 /* Write LEN bytes of data starting at address MYADDR
593 into debugged program memory at address MEMADDR.
594 Returns zero if successful, or an errno value if ptrace failed. */
597 write_memory (memaddr
, myaddr
, len
)
602 if (have_inferior_p ())
603 return write_inferior_memory (memaddr
, myaddr
, len
);
605 error ("Can write memory only when program being debugged is running.");
608 xfer_core_file (memaddr
, myaddr
, len
)
624 /* Determine which file the next bunch of addresses reside in,
625 and where in the file. Set the file's read/write pointer
626 to point at the proper place for the desired address
627 and set xferfile and xferchan for the correct file.
628 If desired address is nonexistent, leave them zero.
629 i is set to the number of bytes that can be handled
630 along with the next address. */
632 if (memaddr
< text_start
)
634 i
= min (len
, text_start
- memaddr
);
636 else if (memaddr
>= text_end
&& memaddr
< data_start
)
638 i
= min (len
, data_start
- memaddr
);
640 else if (memaddr
>= (corechan
>= 0 ? data_end
: exec_data_end
)
641 && memaddr
< stack_start
)
643 i
= min (len
, stack_start
- memaddr
);
645 else if (memaddr
>= stack_end
&& stack_end
!= 0)
647 i
= min (len
, - memaddr
);
649 /* Note that if there is no core file
650 data_start and data_end are equal. */
651 else if (memaddr
>= data_start
&& memaddr
< data_end
)
653 i
= min (len
, data_end
- memaddr
);
654 fileptr
= memaddr
- data_start
+ data_offset
;
655 xferfile
= &corefile
;
658 /* Note that if there is no core file
659 stack_start and stack_end are equal. */
660 else if (memaddr
>= stack_start
&& memaddr
< stack_end
)
662 i
= min (len
, stack_end
- memaddr
);
663 fileptr
= memaddr
- stack_start
+ stack_offset
;
664 xferfile
= &corefile
;
667 else if (corechan
< 0
668 && memaddr
>= exec_data_start
&& memaddr
< exec_data_end
)
670 i
= min (len
, exec_data_end
- memaddr
);
671 fileptr
= memaddr
- exec_data_start
+ exec_data_offset
;
672 xferfile
= &execfile
;
675 else if (memaddr
>= text_start
&& memaddr
< text_end
)
677 i
= min (len
, text_end
- memaddr
);
678 fileptr
= memaddr
- text_start
+ text_offset
;
679 xferfile
= &execfile
;
683 /* Now we know which file to use.
684 Set up its pointer and transfer the data. */
688 if (xferfile
== &execfile
)
689 error ("No program file to examine.");
691 error ("No core dump file or running program to examine.");
692 val
= lseek (xferchan
, fileptr
, 0);
694 perror_with_name (*xferfile
);
695 val
= myread (xferchan
, myaddr
, i
);
697 perror_with_name (*xferfile
);
699 /* If this address is for nonexistent memory,
700 read zeros if reading, or do nothing if writing. */
710 /* My replacement for the read system call.
711 Used like `read' but keeps going if `read' returns too soon. */
713 myread (desc
, addr
, len
)
723 val
= read (desc
, addr
, len
);
734 #ifdef REGISTER_U_ADDR
736 /* Return the address in the core dump or inferior of register REGNO.
737 BLOCKEND is the address of the end of the user structure. */
740 register_addr (regno
, blockend
)
746 if (regno
< 0 || regno
>= NUM_REGS
)
747 error ("Invalid register number %d.", regno
);
749 REGISTER_U_ADDR (addr
, blockend
, regno
);
754 #endif /* REGISTER_U_ADDR */
763 exec_file_display_hook
= 0;
771 stack_start
= STACK_END_ADDR
;
772 stack_end
= STACK_END_ADDR
;
774 add_com ("core-file", class_files
, core_file_command
,
775 "Use FILE as core dump for examining memory and registers.\n\
776 No arg means have no core file.");
777 add_com ("exec-file", class_files
, exec_file_command
,
778 "Use FILE as program for getting contents of pure memory.\n\
779 If FILE cannot be found as specified, your execution directory path\n\
780 is searched for a command of that name.\n\
781 No arg means have no executable file.");
782 add_info ("files", files_info
, "Names of files being debugged.");