home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / core.c < prev    next >
C/C++ Source or Header  |  1993-04-02  |  14KB  |  577 lines

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