home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gdb / config / sun3-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  16.8 KB  |  632 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
  7.  */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#)sun3-dep.c    6.3 (Berkeley) 5/8/91";
  11. #endif /* not lint */
  12.  
  13. /* XXX 
  14.    Added some bogus code so program will compile.  The problem is that
  15.    this module needs to be updated to use the new remote memory interface.
  16.    In the meantime, remote debugging is broken.  (mccanne)
  17. */
  18. #define remote_store_word do_nothing
  19. #define remote_fetch_word do_nothing
  20. static do_nothing() {}
  21.  
  22. /* Machine-dependent code which would otherwise be in inflow.c and core.c,
  23.    for GDB, the GNU debugger.
  24.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  25.  
  26. This file is part of GDB.
  27.  
  28. GDB is free software; you can redistribute it and/or modify
  29. it under the terms of the GNU General Public License as published by
  30. the Free Software Foundation; either version 1, or (at your option)
  31. any later version.
  32.  
  33. GDB is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36. GNU General Public License for more details.
  37.  
  38. You should have received a copy of the GNU General Public License
  39. along with GDB; see the file COPYING.  If not, write to
  40. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  41.  
  42. #include <stdio.h>
  43. #include "defs.h"
  44. #include "param.h"
  45. #include "frame.h"
  46. #include "inferior.h"
  47.  
  48. #include <sys/param.h>
  49. #include <sys/dir.h>
  50. #include <sys/user.h>
  51. #include <signal.h>
  52. #include <sys/ioctl.h>
  53. #include <fcntl.h>
  54.  
  55. #include <sys/ptrace.h>
  56. #include <machine/reg.h>
  57.  
  58. #include <a.out.h>
  59. #include <sys/file.h>
  60. #include <sys/stat.h>
  61. #include <sys/core.h>
  62.  
  63. extern int errno;
  64. extern int attach_flag;
  65.  
  66. /* This function simply calls ptrace with the given arguments.  
  67.    It exists so that all calls to ptrace are isolated in this 
  68.    machine-dependent file. */
  69. int
  70. call_ptrace (request, pid, arg3, arg4)
  71.      int request, pid, arg3, arg4;
  72. {
  73.   return ptrace (request, pid, arg3, arg4);
  74. }
  75.  
  76. kill_inferior ()
  77. {
  78.   if (remote_debugging)
  79.     return;
  80.   if (inferior_pid == 0)
  81.     return;
  82.   ptrace (8, inferior_pid, 0, 0);
  83.   wait (0);
  84.   inferior_died ();
  85. }
  86.  
  87. /* This is used when GDB is exiting.  It gives less chance of error.*/
  88.  
  89. kill_inferior_fast ()
  90. {
  91.   if (remote_debugging)
  92.     return;
  93.   if (inferior_pid == 0)
  94.     return;
  95.   ptrace (8, inferior_pid, 0, 0);
  96.   wait (0);
  97. }
  98.  
  99. /* Resume execution of the inferior process.
  100.    If STEP is nonzero, single-step it.
  101.    If SIGNAL is nonzero, give it that signal.  */
  102.  
  103. void
  104. resume (step, signal)
  105.      int step;
  106.      int signal;
  107. {
  108.   errno = 0;
  109.   if (remote_debugging)
  110.     remote_resume (step, signal);
  111.   else
  112.     {
  113.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  114.       if (errno)
  115.     perror_with_name ("ptrace");
  116.     }
  117. }
  118.  
  119. #ifdef ATTACH_DETACH
  120.  
  121. /* Start debugging the process whose number is PID.  */
  122.  
  123. attach (pid)
  124.      int pid;
  125. {
  126.   errno = 0;
  127.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  128.   if (errno)
  129.     perror_with_name ("ptrace");
  130.   attach_flag = 1;
  131.   return pid;
  132. }
  133.  
  134. /* Stop debugging the process whose number is PID
  135.    and continue it with signal number SIGNAL.
  136.    SIGNAL = 0 means just continue it.  */
  137.  
  138. void
  139. detach (signal)
  140.      int signal;
  141. {
  142.   errno = 0;
  143.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  144.   if (errno)
  145.     perror_with_name ("ptrace");
  146.   attach_flag = 0;
  147. }
  148. #endif /* ATTACH_DETACH */
  149.  
  150. void
  151. fetch_inferior_registers ()
  152. {
  153.   struct regs inferior_registers;
  154. #ifdef FP0_REGNUM
  155.   struct fp_status inferior_fp_registers;
  156. #endif
  157.   extern char registers[];
  158.  
  159.   if (remote_debugging)
  160.     remote_fetch_registers (registers);
  161.   else
  162.     {
  163.       ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  164. #ifdef FP0_REGNUM
  165.       ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  166. #endif 
  167.  
  168.       bcopy (&inferior_registers, registers, 16 * 4);
  169. #ifdef FP0_REGNUM
  170.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  171.          sizeof inferior_fp_registers.fps_regs);
  172. #endif 
  173.       *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
  174.       *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  175. #ifdef FP0_REGNUM
  176.       bcopy (&inferior_fp_registers.fps_control,
  177.          ®isters[REGISTER_BYTE (FPC_REGNUM)],
  178.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  179. #endif 
  180.     }
  181. }
  182.  
  183. /* Store our register values back into the inferior.
  184.    If REGNO is -1, do this for all registers.
  185.    Otherwise, REGNO specifies which register (so we can save time).  */
  186.  
  187. store_inferior_registers (regno)
  188.      int regno;
  189. {
  190.   struct regs inferior_registers;
  191.   struct fp_status inferior_fp_registers;
  192.   extern char registers[];
  193.  
  194.   if (remote_debugging)
  195.     remote_store_registers (registers);
  196.   else
  197.     {
  198.       bcopy (registers, &inferior_registers, 16 * 4);
  199. #ifdef FP0_REGNUM
  200.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  201.          sizeof inferior_fp_registers.fps_regs);
  202. #endif
  203.       inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  204.       inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  205.  
  206. #ifdef FP0_REGNUM
  207.       bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)],
  208.          &inferior_fp_registers.fps_control,
  209.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  210. #endif
  211.  
  212.       ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  213. #if FP0_REGNUM
  214.       ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  215. #endif
  216.     }
  217. }
  218.  
  219. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  220.    in the NEW_SUN_PTRACE case.
  221.    It ought to be straightforward.  But it appears that writing did
  222.    not write the data that I specified.  I cannot understand where
  223.    it got the data that it actually did write.  */
  224.  
  225. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  226.    to debugger memory starting at MYADDR. 
  227.    On failure (cannot read from inferior, usually because address is out
  228.    of bounds) returns the value of errno. */
  229.  
  230. int
  231. read_inferior_memory (memaddr, myaddr, len)
  232.      CORE_ADDR memaddr;
  233.      char *myaddr;
  234.      int len;
  235. {
  236.   register int i;
  237.   /* Round starting address down to longword boundary.  */
  238.   register CORE_ADDR addr = memaddr & - sizeof (int);
  239.   /* Round ending address up; get number of longwords that makes.  */
  240.   register int count
  241.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  242.   /* Allocate buffer of that many longwords.  */
  243.   register int *buffer = (int *) alloca (count * sizeof (int));
  244.   extern int errno;
  245.  
  246.   /* Read all the longwords */
  247.   for (i = 0; i < count; i++, addr += sizeof (int))
  248.     {
  249.       errno = 0;
  250.       if (remote_debugging)
  251.     buffer[i] = remote_fetch_word (addr);
  252.       else
  253.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  254.       if (errno)
  255.     return errno;
  256.     }
  257.  
  258.   /* Copy appropriate bytes out of the buffer.  */
  259.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  260.   return 0;
  261. }
  262.  
  263. /* Copy LEN bytes of data from debugger memory at MYADDR
  264.    to inferior's memory at MEMADDR.
  265.    On failure (cannot write the inferior)
  266.    returns the value of errno.  */
  267.  
  268. int
  269. write_inferior_memory (memaddr, myaddr, len)
  270.      CORE_ADDR memaddr;
  271.      char *myaddr;
  272.      int len;
  273. {
  274.   register int i;
  275.   /* Round starting address down to longword boundary.  */
  276.   register CORE_ADDR addr = memaddr & - sizeof (int);
  277.   /* Round ending address up; get number of longwords that makes.  */
  278.   register int count
  279.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  280.   /* Allocate buffer of that many longwords.  */
  281.   register int *buffer = (int *) alloca (count * sizeof (int));
  282.   extern int errno;
  283.  
  284.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  285.  
  286.   if (remote_debugging)
  287.     buffer[0] = remote_fetch_word (addr);
  288.   else
  289.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  290.  
  291.   if (count > 1)
  292.     {
  293.       if (remote_debugging)
  294.     buffer[count - 1]
  295.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  296.       else
  297.     buffer[count - 1]
  298.       = ptrace (1, inferior_pid,
  299.             addr + (count - 1) * sizeof (int), 0);
  300.     }
  301.  
  302.   /* Copy data to be written over corresponding part of buffer */
  303.  
  304.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  305.  
  306.   /* Write the entire buffer.  */
  307.  
  308.   for (i = 0; i < count; i++, addr += sizeof (int))
  309.     {
  310.       errno = 0;
  311.       if (remote_debugging)
  312.     remote_store_word (addr, buffer[i]);
  313.       else
  314.     ptrace (4, inferior_pid, addr, buffer[i]);
  315.       if (errno)
  316.     return errno;
  317.     }
  318.  
  319.   return 0;
  320. }
  321.  
  322.  
  323. /* Machine-dependent code which would otherwise be in core.c */
  324. /* Work with core dump and executable files, for GDB. */
  325.  
  326. #ifndef N_TXTADDR
  327. #define N_TXTADDR(hdr) 0
  328. #endif /* no N_TXTADDR */
  329.  
  330. #ifndef N_DATADDR
  331. #define N_DATADDR(hdr) hdr.a_text
  332. #endif /* no N_DATADDR */
  333.  
  334. /* Non-zero if this is an object (.o) file, rather than an executable.
  335.    Distinguishing between the two is rarely necessary (and seems like
  336.    a hack, but there is no other way to get the text and data
  337.    addresses--N_TXTADDR should probably take care of
  338.    this, but it doesn't).  */
  339. /* This definition will not work
  340.    if someone decides to make ld preserve relocation info.  */
  341. #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
  342.   
  343. /* Make COFF and non-COFF names for things a little more compatible
  344.    to reduce conditionals later.  */
  345.  
  346. #ifdef COFF_FORMAT
  347. #define a_magic magic
  348. #endif
  349.  
  350. #ifndef COFF_FORMAT
  351. #ifndef AOUTHDR
  352. #define AOUTHDR struct exec
  353. #endif
  354. #endif
  355.  
  356. extern char *sys_siglist[];
  357.  
  358. /* Hook for `exec_file_command' command to call.  */
  359.  
  360. extern void (*exec_file_display_hook) ();
  361.    
  362. /* File names of core file and executable file.  */
  363.  
  364. extern char *corefile;
  365. extern char *execfile;
  366.  
  367. /* Descriptors on which core file and executable file are open.
  368.    Note that the execchan is closed when an inferior is created
  369.    and reopened if the inferior dies or is killed.  */
  370.  
  371. extern int corechan;
  372. extern int execchan;
  373.  
  374. /* Last modification time of executable file.
  375.    Also used in source.c to compare against mtime of a source file.  */
  376.  
  377. extern int exec_mtime;
  378.  
  379. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  380.  
  381. extern CORE_ADDR data_start;
  382. extern CORE_ADDR data_end;
  383. extern CORE_ADDR stack_start;
  384. extern CORE_ADDR stack_end;
  385.  
  386. /* Virtual addresses of bounds of two areas of memory in the exec file.
  387.    Note that the data area in the exec file is used only when there is no core file.  */
  388.  
  389. extern CORE_ADDR text_start;
  390. extern CORE_ADDR text_end;
  391.  
  392. extern CORE_ADDR exec_data_start;
  393. extern CORE_ADDR exec_data_end;
  394.  
  395. /* Address in executable file of start of text area data.  */
  396.  
  397. extern int text_offset;
  398.  
  399. /* Address in executable file of start of data area data.  */
  400.  
  401. extern int exec_data_offset;
  402.  
  403. /* Address in core file of start of data area data.  */
  404.  
  405. extern int data_offset;
  406.  
  407. /* Address in core file of start of stack area data.  */
  408.  
  409. extern int stack_offset;
  410.   
  411. #ifdef COFF_FORMAT
  412. /* various coff data structures */
  413.  
  414. extern FILHDR file_hdr;
  415. extern SCNHDR text_hdr;
  416. extern SCNHDR data_hdr;
  417.  
  418. #endif /* not COFF_FORMAT */
  419.  
  420. /* a.out header saved in core file.  */
  421.   
  422. extern AOUTHDR core_aouthdr;
  423.  
  424. /* a.out header of exec file.  */
  425.  
  426. extern AOUTHDR exec_aouthdr;
  427.  
  428. extern void validate_files ();
  429.  
  430. core_file_command (filename, from_tty)
  431.      char *filename;
  432.      int from_tty;
  433. {
  434.   int val;
  435.   extern char registers[];
  436.  
  437.   /* Discard all vestiges of any previous core file
  438.      and mark data and stack spaces as empty.  */
  439.  
  440.   if (corefile)
  441.     free (corefile);
  442.   corefile = 0;
  443.  
  444.   if (corechan >= 0)
  445.     close (corechan);
  446.   corechan = -1;
  447.  
  448.   data_start = 0;
  449.   data_end = 0;
  450.   stack_start = STACK_END_ADDR;
  451.   stack_end = STACK_END_ADDR;
  452.  
  453.   /* Now, if a new core file was specified, open it and digest it.  */
  454.  
  455.   if (filename)
  456.     {
  457.       filename = tilde_expand (filename);
  458.       make_cleanup (free, filename);
  459.       
  460.       if (have_inferior_p ())
  461.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  462.       corechan = open (filename, O_RDONLY, 0);
  463.       if (corechan < 0)
  464.     perror_with_name (filename);
  465.  
  466.       {
  467.     struct core corestr;
  468.  
  469.     val = myread (corechan, &corestr, sizeof corestr);
  470.     if (val < 0)
  471.       perror_with_name (filename);
  472.     if (corestr.c_magic != CORE_MAGIC)
  473.       error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  474.          filename, corestr.c_magic, (int) CORE_MAGIC);
  475.     else if (sizeof (struct core) != corestr.c_len)
  476.       error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  477.          filename, corestr.c_len, (int) sizeof (struct core));
  478.  
  479.     data_start = exec_data_start;
  480.     data_end = data_start + corestr.c_dsize;
  481.     stack_start = stack_end - corestr.c_ssize;
  482.     data_offset = sizeof corestr;
  483.     stack_offset = sizeof corestr + corestr.c_dsize;
  484.  
  485.     bcopy (&corestr.c_regs, registers, 16 * 4);
  486.     *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  487.     *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  488. #ifdef FP0_REGNUM
  489. #ifdef FPU
  490.      bcopy (corestr.c_fpu.f_fpstatus.fps_regs,
  491.             ®isters[REGISTER_BYTE (FP0_REGNUM)],
  492.             sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  493.      bcopy (&corestr.c_fpu.f_fpstatus.fps_control,
  494.             ®isters[REGISTER_BYTE (FPC_REGNUM)],
  495.             sizeof corestr.c_fpu.f_fpstatus - sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  496. #else
  497.     bcopy (corestr.c_fpstatus.fps_regs,
  498.            ®isters[REGISTER_BYTE (FP0_REGNUM)],
  499.            sizeof corestr.c_fpstatus.fps_regs);
  500.     bcopy (&corestr.c_fpstatus.fps_control,
  501.            ®isters[REGISTER_BYTE (FPC_REGNUM)],
  502.            sizeof corestr.c_fpstatus - sizeof corestr.c_fpstatus.fps_regs);
  503. #endif
  504. #endif
  505.     bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  506.  
  507.     printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  508.     if (corestr.c_signo > 0)
  509.       printf ("Program terminated with signal %d, %s.\n",
  510.             corestr.c_signo,
  511.             corestr.c_signo < NSIG
  512.             ? sys_siglist[corestr.c_signo]
  513.             : "(undocumented)");
  514.       }
  515.       if (filename[0] == '/')
  516.     corefile = savestring (filename, strlen (filename));
  517.       else
  518.     {
  519.       corefile = concat (current_directory, "/", filename);
  520.     }
  521.  
  522.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  523.                         read_pc ()));
  524.       select_frame (get_current_frame (), 0);
  525.       validate_files ();
  526.     }
  527.   else if (from_tty)
  528.     printf ("No core file now.\n");
  529. }
  530.  
  531. exec_file_command (filename, from_tty)
  532.      char *filename;
  533.      int from_tty;
  534. {
  535.   int val;
  536.  
  537.   /* Eliminate all traces of old exec file.
  538.      Mark text segment as empty.  */
  539.  
  540.   if (execfile)
  541.     free (execfile);
  542.   execfile = 0;
  543.   data_start = 0;
  544.   data_end -= exec_data_start;
  545.   text_start = 0;
  546.   text_end = 0;
  547.   exec_data_start = 0;
  548.   exec_data_end = 0;
  549.   if (execchan >= 0)
  550.     close (execchan);
  551.   execchan = -1;
  552.  
  553.   /* Now open and digest the file the user requested, if any.  */
  554.  
  555.   if (filename)
  556.     {
  557.       filename = tilde_expand (filename);
  558.       make_cleanup (free, filename);
  559.       
  560.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  561.             &execfile);
  562.       if (execchan < 0)
  563.     perror_with_name (filename);
  564.  
  565. #ifdef COFF_FORMAT
  566.       {
  567.     int aout_hdrsize;
  568.     int num_sections;
  569.  
  570.     if (read_file_hdr (execchan, &file_hdr) < 0)
  571.       error ("\"%s\": not in executable format.", execfile);
  572.  
  573.     aout_hdrsize = file_hdr.f_opthdr;
  574.     num_sections = file_hdr.f_nscns;
  575.  
  576.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  577.       error ("\"%s\": can't read optional aouthdr", execfile);
  578.  
  579.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  580.                   aout_hdrsize) < 0)
  581.       error ("\"%s\": can't read text section header", execfile);
  582.  
  583.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  584.                   aout_hdrsize) < 0)
  585.       error ("\"%s\": can't read data section header", execfile);
  586.  
  587.     text_start = exec_aouthdr.text_start;
  588.     text_end = text_start + exec_aouthdr.tsize;
  589.     text_offset = text_hdr.s_scnptr;
  590.     exec_data_start = exec_aouthdr.data_start;
  591.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  592.     exec_data_offset = data_hdr.s_scnptr;
  593.     data_start = exec_data_start;
  594.     data_end += exec_data_start;
  595.     exec_mtime = file_hdr.f_timdat;
  596.       }
  597. #else /* not COFF_FORMAT */
  598.       {
  599.     struct stat st_exec;
  600.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  601.  
  602.     if (val < 0)
  603.       perror_with_name (filename);
  604.  
  605.     text_start =
  606.       IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
  607.         exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  608.       ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  609.     text_offset = N_TXTOFF (exec_aouthdr);
  610.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  611.  
  612.     text_end = text_start + exec_aouthdr.a_text;
  613.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  614.     data_start = exec_data_start;
  615.     data_end += exec_data_start;
  616.  
  617.     fstat (execchan, &st_exec);
  618.     exec_mtime = st_exec.st_mtime;
  619.       }
  620. #endif /* not COFF_FORMAT */
  621.  
  622.       validate_files ();
  623.     }
  624.   else if (from_tty)
  625.     printf ("No exec file now.\n");
  626.  
  627.   /* Tell display code (if any) about the changed file name.  */
  628.   if (exec_file_display_hook)
  629.     (*exec_file_display_hook) (filename);
  630. }
  631.  
  632.