home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / mips-nat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-10  |  6.7 KB  |  239 lines

  1. /* Low level DECstation interface to ptrace, for GDB when running native.
  2.    Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  4.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "inferior.h"
  24. #include "gdbcore.h"
  25. #include <sys/ptrace.h>
  26. #include <sys/types.h>
  27. #include <sys/param.h>
  28. #include <sys/user.h>
  29. #undef JB_S0
  30. #undef JB_S1
  31. #undef JB_S2
  32. #undef JB_S3
  33. #undef JB_S4
  34. #undef JB_S5
  35. #undef JB_S6
  36. #undef JB_S7
  37. #undef JB_SP
  38. #undef JB_S8
  39. #undef JB_PC
  40. #undef JB_SR
  41. #undef NJBREGS
  42. #include <setjmp.h>        /* For JB_XXX.  */
  43.  
  44. /* Size of elements in jmpbuf */
  45.  
  46. #define JB_ELEMENT_SIZE 4
  47.  
  48. /* Map gdb internal register number to ptrace ``address''.
  49.    These ``addresses'' are defined in DECstation <sys/ptrace.h> */
  50.  
  51. #define REGISTER_PTRACE_ADDR(regno) \
  52.    (regno < 32 ?         GPR_BASE + regno \
  53.   : regno == PC_REGNUM ?    PC    \
  54.   : regno == CAUSE_REGNUM ?    CAUSE    \
  55.   : regno == HI_REGNUM ?    MMHI    \
  56.   : regno == LO_REGNUM ?    MMLO    \
  57.   : regno == FCRCS_REGNUM ?    FPC_CSR    \
  58.   : regno == FCRIR_REGNUM ?    FPC_EIR    \
  59.   : regno >= FP0_REGNUM ?    FPR_BASE + (regno - FP0_REGNUM) \
  60.   : 0)
  61.  
  62. static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
  63.  
  64. /* Get all registers from the inferior */
  65.  
  66. void
  67. fetch_inferior_registers (regno)
  68.      int regno;
  69. {
  70.   register unsigned int regaddr;
  71.   char buf[MAX_REGISTER_RAW_SIZE];
  72.   register int i;
  73.  
  74.   registers_fetched ();
  75.  
  76.   for (regno = 1; regno < NUM_REGS; regno++)
  77.     {
  78.       regaddr = REGISTER_PTRACE_ADDR (regno);
  79.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  80.      {
  81.        *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
  82.                      (PTRACE_ARG3_TYPE) regaddr, 0);
  83.        regaddr += sizeof (int);
  84.      }
  85.       supply_register (regno, buf);
  86.     }
  87.  
  88.   supply_register (ZERO_REGNUM, zerobuf);
  89.   /* Frame ptr reg must appear to be 0; it is faked by stack handling code. */
  90.   supply_register (FP_REGNUM, zerobuf);
  91. }
  92.  
  93. /* Store our register values back into the inferior.
  94.    If REGNO is -1, do this for all registers.
  95.    Otherwise, REGNO specifies which register (so we can save time).  */
  96.  
  97. void
  98. store_inferior_registers (regno)
  99.      int regno;
  100. {
  101.   register unsigned int regaddr;
  102.   char buf[80];
  103.  
  104.   if (regno == 0)
  105.     return;
  106.  
  107.   if (regno > 0)
  108.     {
  109.       regaddr = REGISTER_PTRACE_ADDR (regno);
  110.       errno = 0;
  111.       ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
  112.           read_register (regno));
  113.       if (errno != 0)
  114.     {
  115.       sprintf (buf, "writing register number %d", regno);
  116.       perror_with_name (buf);
  117.     }
  118.     }
  119.   else
  120.     {
  121.       for (regno = 0; regno < NUM_REGS; regno++)
  122.     {
  123.       if (regno == ZERO_REGNUM || regno == PS_REGNUM
  124.           || regno == BADVADDR_REGNUM || regno == CAUSE_REGNUM
  125.           || regno == FCRIR_REGNUM || regno == FP_REGNUM
  126.           || (regno >= FIRST_EMBED_REGNUM && regno <= LAST_EMBED_REGNUM))
  127.         continue;
  128.       regaddr = REGISTER_PTRACE_ADDR (regno);
  129.       errno = 0;
  130.       ptrace (6, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
  131.           read_register (regno));
  132.       if (errno != 0)
  133.         {
  134.           sprintf (buf, "writing all regs, number %d", regno);
  135.           perror_with_name (buf);
  136.         }
  137.     }
  138.     }
  139. }
  140.  
  141.  
  142. /* Figure out where the longjmp will land.
  143.    We expect the first arg to be a pointer to the jmp_buf structure from which
  144.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  145.    This routine returns true on success. */
  146.  
  147. int
  148. get_longjmp_target(pc)
  149.      CORE_ADDR *pc;
  150. {
  151.   CORE_ADDR jb_addr;
  152.   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
  153.  
  154.   jb_addr = read_register (A0_REGNUM);
  155.  
  156.   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
  157.               TARGET_PTR_BIT / TARGET_CHAR_BIT))
  158.     return 0;
  159.  
  160.   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  161.  
  162.   return 1;
  163. }
  164.  
  165. /* Extract the register values out of the core file and store
  166.    them where `read_register' will find them.
  167.  
  168.    CORE_REG_SECT points to the register values themselves, read into memory.
  169.    CORE_REG_SIZE is the size of that area.
  170.    WHICH says which set of registers we are handling (0 = int, 2 = float
  171.          on machines where they are discontiguous).
  172.    REG_ADDR is the offset from u.u_ar0 to the register values relative to
  173.             core_reg_sect.  This is used with old-fashioned core files to
  174.         locate the registers in a large upage-plus-stack ".reg" section.
  175.         Original upage address X is at location core_reg_sect+x+reg_addr.
  176.  */
  177.  
  178. void
  179. fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
  180.      char *core_reg_sect;
  181.      unsigned core_reg_size;
  182.      int which;
  183.      unsigned reg_addr;
  184. {
  185.   register int regno;
  186.   register unsigned int addr;
  187.   int bad_reg = -1;
  188.   register reg_ptr = -reg_addr;        /* Original u.u_ar0 is -reg_addr. */
  189.  
  190.   /* If u.u_ar0 was an absolute address in the core file, relativize it now,
  191.      so we can use it as an offset into core_reg_sect.  When we're done,
  192.      "register 0" will be at core_reg_sect+reg_ptr, and we can use
  193.      register_addr to offset to the other registers.  If this is a modern
  194.      core file without a upage, reg_ptr will be zero and this is all a big
  195.      NOP.  */
  196.   if (reg_ptr > core_reg_size)
  197. #ifdef KERNEL_U_ADDR
  198.     reg_ptr -= KERNEL_U_ADDR;
  199. #else
  200.     error ("Old mips core file can't be processed on this machine.");
  201. #endif
  202.  
  203.   for (regno = 0; regno < NUM_REGS; regno++)
  204.     {
  205.       addr = register_addr (regno, reg_ptr);
  206.       if (addr >= core_reg_size) {
  207.     if (bad_reg < 0)
  208.       bad_reg = regno;
  209.       } else {
  210.     supply_register (regno, core_reg_sect + addr);
  211.       }
  212.     }
  213.   if (bad_reg >= 0)
  214.     {
  215.       error ("Register %s not found in core file.", reg_names[bad_reg]);
  216.     }
  217.   supply_register (ZERO_REGNUM, zerobuf);
  218.   /* Frame ptr reg must appear to be 0; it is faked by stack handling code. */
  219.   supply_register (FP_REGNUM, zerobuf);
  220. }
  221.  
  222. /* Return the address in the core dump or inferior of register REGNO.
  223.    BLOCKEND is the address of the end of the user structure.  */
  224.  
  225. unsigned int
  226. register_addr (regno, blockend)
  227.      int regno;
  228.      int blockend;
  229. {
  230.   int addr;
  231.  
  232.   if (regno < 0 || regno >= NUM_REGS)
  233.     error ("Invalid register number %d.", regno);
  234.  
  235.   REGISTER_U_ADDR (addr, blockend, regno);
  236.  
  237.   return addr;
  238. }
  239.