home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / findvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-16  |  31.0 KB  |  1,243 lines

  1. /* Find a variable's value in memory, for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program 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 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program 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 this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "value.h"
  25. #include "gdbcore.h"
  26. #include "inferior.h"
  27. #include "target.h"
  28.  
  29. /* Basic byte-swapping routines.  GDB has needed these for a long time...
  30.    All extract a target-format integer at ADDR which is LEN bytes long.  */
  31.  
  32. #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
  33.   /* 8 bit characters are a pretty safe assumption these days, so we
  34.      assume it throughout all these swapping routines.  If we had to deal with
  35.      9 bit characters, we would need to make len be in bits and would have
  36.      to re-write these routines...  */
  37.   you lose
  38. #endif
  39.  
  40. LONGEST
  41. extract_signed_integer (addr, len)
  42.      PTR addr;
  43.      int len;
  44. {
  45.   LONGEST retval;
  46.   unsigned char *p;
  47.   unsigned char *startaddr = (unsigned char *)addr;
  48.   unsigned char *endaddr = startaddr + len;
  49.  
  50.   if (len > sizeof (LONGEST))
  51.     error ("\
  52. That operation is not available on integers of more than %d bytes.",
  53.        sizeof (LONGEST));
  54.  
  55.   /* Start at the most significant end of the integer, and work towards
  56.      the least significant.  */
  57. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  58.   p = startaddr;
  59. #else
  60.   p = endaddr - 1;
  61. #endif
  62.   /* Do the sign extension once at the start.  */
  63.   retval = ((LONGEST)*p ^ 0x80) - 0x80;
  64. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  65.   for (++p; p < endaddr; ++p)
  66. #else
  67.   for (--p; p >= startaddr; --p)
  68. #endif
  69.     {
  70.       retval = (retval << 8) | *p;
  71.     }
  72.   return retval;
  73. }
  74.  
  75. unsigned LONGEST
  76. extract_unsigned_integer (addr, len)
  77.      PTR addr;
  78.      int len;
  79. {
  80.   unsigned LONGEST retval;
  81.   unsigned char *p;
  82.   unsigned char *startaddr = (unsigned char *)addr;
  83.   unsigned char *endaddr = startaddr + len;
  84.  
  85.   if (len > sizeof (unsigned LONGEST))
  86.     error ("\
  87. That operation is not available on integers of more than %d bytes.",
  88.        sizeof (unsigned LONGEST));
  89.  
  90.   /* Start at the most significant end of the integer, and work towards
  91.      the least significant.  */
  92.   retval = 0;
  93. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  94.   for (p = startaddr; p < endaddr; ++p)
  95. #else
  96.   for (p = endaddr - 1; p >= startaddr; --p)
  97. #endif
  98.     {
  99.       retval = (retval << 8) | *p;
  100.     }
  101.   return retval;
  102. }
  103.  
  104. CORE_ADDR
  105. extract_address (addr, len)
  106.      PTR addr;
  107.      int len;
  108. {
  109.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  110.      whether we want this to be true eventually.  */
  111.   return extract_unsigned_integer (addr, len);
  112. }
  113.  
  114. void
  115. store_signed_integer (addr, len, val)
  116.      PTR addr;
  117.      int len;
  118.      LONGEST val;
  119. {
  120.   unsigned char *p;
  121.   unsigned char *startaddr = (unsigned char *)addr;
  122.   unsigned char *endaddr = startaddr + len;
  123.  
  124.   /* Start at the least significant end of the integer, and work towards
  125.      the most significant.  */
  126. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  127.   for (p = endaddr - 1; p >= startaddr; --p)
  128. #else
  129.   for (p = startaddr; p < endaddr; ++p)
  130. #endif
  131.     {
  132.       *p = val & 0xff;
  133.       val >>= 8;
  134.     }
  135. }
  136.  
  137. void
  138. store_unsigned_integer (addr, len, val)
  139.      PTR addr;
  140.      int len;
  141.      unsigned LONGEST val;
  142. {
  143.   unsigned char *p;
  144.   unsigned char *startaddr = (unsigned char *)addr;
  145.   unsigned char *endaddr = startaddr + len;
  146.  
  147.   /* Start at the least significant end of the integer, and work towards
  148.      the most significant.  */
  149. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  150.   for (p = endaddr - 1; p >= startaddr; --p)
  151. #else
  152.   for (p = startaddr; p < endaddr; ++p)
  153. #endif
  154.     {
  155.       *p = val & 0xff;
  156.       val >>= 8;
  157.     }
  158. }
  159.  
  160. void
  161. store_address (addr, len, val)
  162.      PTR addr;
  163.      int len;
  164.      CORE_ADDR val;
  165. {
  166.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  167.      whether we want this to be true eventually.  */
  168.   store_unsigned_integer (addr, len, (LONGEST)val);
  169. }
  170.  
  171. /* Swap LEN bytes at BUFFER between target and host byte-order.  This is
  172.    the wrong way to do byte-swapping because it assumes that you have a way
  173.    to have a host variable of exactly the right size.  Once extract_floating
  174.    and store_floating have been fixed, this can go away.  */
  175. #if TARGET_BYTE_ORDER == HOST_BYTE_ORDER
  176. #define SWAP_TARGET_AND_HOST(buffer,len)
  177. #else /* Target and host byte order differ.  */
  178. #define SWAP_TARGET_AND_HOST(buffer,len) \
  179.   {                                                                                             \
  180.     char tmp;                                 \
  181.     char *p = (char *)(buffer);                         \
  182.     char *q = ((char *)(buffer)) + len - 1;                    \
  183.     for (; p < q; p++, q--)                          \
  184.       {                                     \
  185.         tmp = *q;                             \
  186.         *q = *p;                             \
  187.         *p = tmp;                             \
  188.       }                                     \
  189.   }
  190. #endif /* Target and host byte order differ.  */
  191.  
  192. /* There are many problems with floating point cross-debugging.
  193.  
  194.    1.  These routines only handle byte-swapping, not conversion of
  195.    formats.  So if host is IEEE floating and target is VAX floating,
  196.    or vice-versa, it loses.  This means that we can't (yet) use these
  197.    routines for extendeds.  Extendeds are handled by
  198.    REGISTER_CONVERTIBLE.  What we want is to use floatformat.h, but that
  199.    doesn't yet handle VAX floating at all.
  200.  
  201.    2.  We can't deal with it if there is more than one floating point
  202.    format in use.  This has to be fixed at the unpack_double level.
  203.  
  204.    3.  We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
  205.    we want to call it which is long double where available.  */
  206.  
  207. double
  208. extract_floating (addr, len)
  209.      PTR addr;
  210.      int len;
  211. {
  212.   if (len == sizeof (float))
  213.     {
  214.       float retval;
  215.       memcpy (&retval, addr, sizeof (retval));
  216.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  217.       return retval;
  218.     }
  219.   else if (len == sizeof (double))
  220.     {
  221.       double retval;
  222.       memcpy (&retval, addr, sizeof (retval));
  223.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  224.       return retval;
  225.     }
  226.   else
  227.     {
  228.       error ("Can't deal with a floating point number of %d bytes.", len);
  229.     }
  230. }
  231.  
  232. void
  233. store_floating (addr, len, val)
  234.      PTR addr;
  235.      int len;
  236.      double val;
  237. {
  238.   if (len == sizeof (float))
  239.     {
  240.       float floatval = val;
  241.       SWAP_TARGET_AND_HOST (&floatval, sizeof (floatval));
  242.       memcpy (addr, &floatval, sizeof (floatval));
  243.     }
  244.   else if (len == sizeof (double))
  245.     {
  246.       SWAP_TARGET_AND_HOST (&val, sizeof (val));
  247.       memcpy (addr, &val, sizeof (val));
  248.     }
  249.   else
  250.     {
  251.       error ("Can't deal with a floating point number of %d bytes.", len);
  252.     }
  253. }
  254.  
  255. #if !defined (GET_SAVED_REGISTER)
  256.  
  257. /* Return the address in which frame FRAME's value of register REGNUM
  258.    has been saved in memory.  Or return zero if it has not been saved.
  259.    If REGNUM specifies the SP, the value we return is actually
  260.    the SP value, not an address where it was saved.  */
  261.  
  262. CORE_ADDR
  263. find_saved_register (frame, regnum)
  264.      FRAME frame;
  265.      int regnum;
  266. {
  267.   struct frame_info *fi;
  268.   struct frame_saved_regs saved_regs;
  269.  
  270.   register FRAME frame1 = 0;
  271.   register CORE_ADDR addr = 0;
  272.  
  273.   if (frame == 0)        /* No regs saved if want current frame */
  274.     return 0;
  275.  
  276. #ifdef HAVE_REGISTER_WINDOWS
  277.   /* We assume that a register in a register window will only be saved
  278.      in one place (since the name changes and/or disappears as you go
  279.      towards inner frames), so we only call get_frame_saved_regs on
  280.      the current frame.  This is directly in contradiction to the
  281.      usage below, which assumes that registers used in a frame must be
  282.      saved in a lower (more interior) frame.  This change is a result
  283.      of working on a register window machine; get_frame_saved_regs
  284.      always returns the registers saved within a frame, within the
  285.      context (register namespace) of that frame. */
  286.  
  287.   /* However, note that we don't want this to return anything if
  288.      nothing is saved (if there's a frame inside of this one).  Also,
  289.      callers to this routine asking for the stack pointer want the
  290.      stack pointer saved for *this* frame; this is returned from the
  291.      next frame.  */
  292.      
  293.  
  294.   if (REGISTER_IN_WINDOW_P(regnum))
  295.     {
  296.       frame1 = get_next_frame (frame);
  297.       if (!frame1) return 0;    /* Registers of this frame are
  298.                    active.  */
  299.       
  300.       /* Get the SP from the next frame in; it will be this
  301.      current frame.  */
  302.       if (regnum != SP_REGNUM)
  303.     frame1 = frame;    
  304.       
  305.       fi = get_frame_info (frame1);
  306.       get_frame_saved_regs (fi, &saved_regs);
  307.       return saved_regs.regs[regnum];    /* ... which might be zero */
  308.     }
  309. #endif /* HAVE_REGISTER_WINDOWS */
  310.  
  311.   /* Note that this next routine assumes that registers used in
  312.      frame x will be saved only in the frame that x calls and
  313.      frames interior to it.  This is not true on the sparc, but the
  314.      above macro takes care of it, so we should be all right. */
  315.   while (1)
  316.     {
  317.       QUIT;
  318.       frame1 = get_prev_frame (frame1);
  319.       if (frame1 == 0 || frame1 == frame)
  320.     break;
  321.       fi = get_frame_info (frame1);
  322.       get_frame_saved_regs (fi, &saved_regs);
  323.       if (saved_regs.regs[regnum])
  324.     addr = saved_regs.regs[regnum];
  325.     }
  326.  
  327.   return addr;
  328. }
  329.  
  330. /* Find register number REGNUM relative to FRAME and put its (raw,
  331.    target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
  332.    variable was optimized out (and thus can't be fetched).  Set *LVAL
  333.    to lval_memory, lval_register, or not_lval, depending on whether
  334.    the value was fetched from memory, from a register, or in a strange
  335.    and non-modifiable way (e.g. a frame pointer which was calculated
  336.    rather than fetched).  Set *ADDRP to the address, either in memory
  337.    on as a REGISTER_BYTE offset into the registers array.
  338.  
  339.    Note that this implementation never sets *LVAL to not_lval.  But
  340.    it can be replaced by defining GET_SAVED_REGISTER and supplying
  341.    your own.
  342.  
  343.    The argument RAW_BUFFER must point to aligned memory.  */
  344.  
  345. void
  346. get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
  347.      char *raw_buffer;
  348.      int *optimized;
  349.      CORE_ADDR *addrp;
  350.      FRAME frame;
  351.      int regnum;
  352.      enum lval_type *lval;
  353. {
  354.   CORE_ADDR addr;
  355.   /* Normal systems don't optimize out things with register numbers.  */
  356.   if (optimized != NULL)
  357.     *optimized = 0;
  358.   addr = find_saved_register (frame, regnum);
  359.   if (addr != 0)
  360.     {
  361.       if (lval != NULL)
  362.     *lval = lval_memory;
  363.       if (regnum == SP_REGNUM)
  364.     {
  365.       if (raw_buffer != NULL)
  366.         {
  367.           /* Put it back in target format.  */
  368.           store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
  369.         }
  370.       if (addrp != NULL)
  371.         *addrp = 0;
  372.       return;
  373.     }
  374.       if (raw_buffer != NULL)
  375.     read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
  376.     }
  377.   else
  378.     {
  379.       if (lval != NULL)
  380.     *lval = lval_register;
  381.       addr = REGISTER_BYTE (regnum);
  382.       if (raw_buffer != NULL)
  383.     read_register_gen (regnum, raw_buffer);
  384.     }
  385.   if (addrp != NULL)
  386.     *addrp = addr;
  387. }
  388. #endif /* GET_SAVED_REGISTER.  */
  389.  
  390. /* Copy the bytes of register REGNUM, relative to the current stack frame,
  391.    into our memory at MYADDR, in target byte order.
  392.    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
  393.  
  394.    Returns 1 if could not be read, 0 if could.  */
  395.  
  396. int
  397. read_relative_register_raw_bytes (regnum, myaddr)
  398.      int regnum;
  399.      char *myaddr;
  400. {
  401.   int optim;
  402.   if (regnum == FP_REGNUM && selected_frame)
  403.     {
  404.       /* Put it back in target format.  */
  405.       store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
  406.              FRAME_FP(selected_frame));
  407.       return 0;
  408.     }
  409.  
  410.   get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
  411.                       regnum, (enum lval_type *)NULL);
  412.   return optim;
  413. }
  414.  
  415. /* Return a `value' with the contents of register REGNUM
  416.    in its virtual format, with the type specified by
  417.    REGISTER_VIRTUAL_TYPE.  */
  418.  
  419. value_ptr
  420. value_of_register (regnum)
  421.      int regnum;
  422. {
  423.   CORE_ADDR addr;
  424.   int optim;
  425.   register value_ptr reg_val;
  426.   char raw_buffer[MAX_REGISTER_RAW_SIZE];
  427.   enum lval_type lval;
  428.  
  429.   get_saved_register (raw_buffer, &optim, &addr,
  430.               selected_frame, regnum, &lval);
  431.  
  432.   reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
  433.  
  434.   /* Convert raw data to virtual format if necessary.  */
  435.  
  436. #ifdef REGISTER_CONVERTIBLE
  437.   if (REGISTER_CONVERTIBLE (regnum))
  438.     {
  439.       REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
  440.                    raw_buffer, VALUE_CONTENTS_RAW (reg_val));
  441.     }
  442.   else
  443. #endif
  444.     memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
  445.         REGISTER_RAW_SIZE (regnum));
  446.   VALUE_LVAL (reg_val) = lval;
  447.   VALUE_ADDRESS (reg_val) = addr;
  448.   VALUE_REGNO (reg_val) = regnum;
  449.   VALUE_OPTIMIZED_OUT (reg_val) = optim;
  450.   return reg_val;
  451. }
  452.  
  453. /* Low level examining and depositing of registers.
  454.  
  455.    The caller is responsible for making
  456.    sure that the inferior is stopped before calling the fetching routines,
  457.    or it will get garbage.  (a change from GDB version 3, in which
  458.    the caller got the value from the last stop).  */
  459.  
  460. /* Contents of the registers in target byte order.
  461.    We allocate some extra slop since we do a lot of memcpy's around `registers',
  462.    and failing-soft is better than failing hard.  */
  463. char registers[REGISTER_BYTES + /* SLOP */ 256];
  464.  
  465. /* Nonzero if that register has been fetched.  */
  466. char register_valid[NUM_REGS];
  467.  
  468. /* The thread/process associated with the current set of registers.  For now,
  469.    -1 is special, and means `no current process'.  */
  470. int registers_pid = -1;
  471.  
  472. /* Indicate that registers may have changed, so invalidate the cache.  */
  473.  
  474. void
  475. registers_changed ()
  476. {
  477.   int i;
  478.   int numregs = ARCH_NUM_REGS;
  479.  
  480.   registers_pid = -1;
  481.  
  482.   for (i = 0; i < numregs; i++)
  483.     register_valid[i] = 0;
  484. }
  485.  
  486. /* Indicate that all registers have been fetched, so mark them all valid.  */
  487. void
  488. registers_fetched ()
  489. {
  490.   int i;
  491.   int numregs = ARCH_NUM_REGS;
  492.   for (i = 0; i < numregs; i++)
  493.     register_valid[i] = 1;
  494. }
  495.  
  496. /* Copy LEN bytes of consecutive data from registers
  497.    starting with the REGBYTE'th byte of register data
  498.    into memory at MYADDR.  */
  499.  
  500. void
  501. read_register_bytes (regbyte, myaddr, len)
  502.      int regbyte;
  503.      char *myaddr;
  504.      int len;
  505. {
  506.   /* Fetch all registers.  */
  507.   int i, numregs;
  508.  
  509.   if (registers_pid != inferior_pid)
  510.     {
  511.       registers_changed ();
  512.       registers_pid = inferior_pid;
  513.     }
  514.  
  515.   numregs = ARCH_NUM_REGS;
  516.   for (i = 0; i < numregs; i++)
  517.     if (!register_valid[i])
  518.       {
  519.     target_fetch_registers (-1);
  520.     break;
  521.       }
  522.   if (myaddr != NULL)
  523.     memcpy (myaddr, ®isters[regbyte], len);
  524. }
  525.  
  526. /* Read register REGNO into memory at MYADDR, which must be large enough
  527.    for REGISTER_RAW_BYTES (REGNO).  Target byte-order.
  528.    If the register is known to be the size of a CORE_ADDR or smaller,
  529.    read_register can be used instead.  */
  530. void
  531. read_register_gen (regno, myaddr)
  532.      int regno;
  533.      char *myaddr;
  534. {
  535.   if (registers_pid != inferior_pid)
  536.     {
  537.       registers_changed ();
  538.       registers_pid = inferior_pid;
  539.     }
  540.  
  541.   if (!register_valid[regno])
  542.     target_fetch_registers (regno);
  543.   memcpy (myaddr, ®isters[REGISTER_BYTE (regno)],
  544.       REGISTER_RAW_SIZE (regno));
  545. }
  546.  
  547. /* Copy LEN bytes of consecutive data from memory at MYADDR
  548.    into registers starting with the REGBYTE'th byte of register data.  */
  549.  
  550. void
  551. write_register_bytes (regbyte, myaddr, len)
  552.      int regbyte;
  553.      char *myaddr;
  554.      int len;
  555. {
  556.   if (registers_pid != inferior_pid)
  557.     {
  558.       registers_changed ();
  559.       registers_pid = inferior_pid;
  560.     }
  561.  
  562.   /* Make sure the entire registers array is valid.  */
  563.   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
  564.   memcpy (®isters[regbyte], myaddr, len);
  565.   target_store_registers (-1);
  566. }
  567.  
  568. /* Return the raw contents of register REGNO, regarding it as an integer.  */
  569. /* This probably should be returning LONGEST rather than CORE_ADDR.  */
  570.  
  571. CORE_ADDR
  572. read_register (regno)
  573.      int regno;
  574. {
  575.   if (registers_pid != inferior_pid)
  576.     {
  577.       registers_changed ();
  578.       registers_pid = inferior_pid;
  579.     }
  580.  
  581.   if (!register_valid[regno])
  582.     target_fetch_registers (regno);
  583.  
  584.   return extract_address (®isters[REGISTER_BYTE (regno)],
  585.               REGISTER_RAW_SIZE(regno));
  586. }
  587.  
  588. CORE_ADDR
  589. read_register_pid (regno, pid)
  590.      int regno, pid;
  591. {
  592.   int save_pid;
  593.   CORE_ADDR retval;
  594.  
  595.   if (pid == inferior_pid)
  596.     return read_register (regno);
  597.  
  598.   save_pid = inferior_pid;
  599.  
  600.   inferior_pid = pid;
  601.  
  602.   retval = read_register (regno);
  603.  
  604.   inferior_pid = save_pid;
  605.  
  606.   return retval;
  607. }
  608.  
  609. /* Registers we shouldn't try to store.  */
  610. #if !defined (CANNOT_STORE_REGISTER)
  611. #define CANNOT_STORE_REGISTER(regno) 0
  612. #endif
  613.  
  614. /* Store VALUE, into the raw contents of register number REGNO.  */
  615. /* FIXME: The val arg should probably be a LONGEST.  */
  616.  
  617. void
  618. write_register (regno, val)
  619.      int regno;
  620.      LONGEST val;
  621. {
  622.   PTR buf;
  623.   int size;
  624.  
  625.   /* On the sparc, writing %g0 is a no-op, so we don't even want to change
  626.      the registers array if something writes to this register.  */
  627.   if (CANNOT_STORE_REGISTER (regno))
  628.     return;
  629.  
  630.   if (registers_pid != inferior_pid)
  631.     {
  632.       registers_changed ();
  633.       registers_pid = inferior_pid;
  634.     }
  635.  
  636.   size = REGISTER_RAW_SIZE(regno);
  637.   buf = alloca (size);
  638.   store_signed_integer (buf, size, (LONGEST) val);
  639.  
  640.   /* If we have a valid copy of the register, and new value == old value,
  641.      then don't bother doing the actual store. */
  642.  
  643.   if (register_valid [regno]
  644.       && memcmp (®isters[REGISTER_BYTE (regno)], buf, size) == 0)
  645.     return;
  646.   
  647.   target_prepare_to_store ();
  648.  
  649.   memcpy (®isters[REGISTER_BYTE (regno)], buf, size);
  650.  
  651.   register_valid [regno] = 1;
  652.  
  653.   target_store_registers (regno);
  654. }
  655.  
  656. void
  657. write_register_pid (regno, val, pid)
  658.      int regno;
  659.      LONGEST val;
  660.      int pid;
  661. {
  662.   int save_pid;
  663.  
  664.   if (pid == inferior_pid)
  665.     {
  666.       write_register (regno, val);
  667.       return;
  668.     }
  669.  
  670.   save_pid = inferior_pid;
  671.  
  672.   inferior_pid = pid;
  673.  
  674.   write_register (regno, val);
  675.  
  676.   inferior_pid = save_pid;
  677. }
  678.  
  679. /* Record that register REGNO contains VAL.
  680.    This is used when the value is obtained from the inferior or core dump,
  681.    so there is no need to store the value there.  */
  682.  
  683. void
  684. supply_register (regno, val)
  685.      int regno;
  686.      char *val;
  687. {
  688.   if (registers_pid != inferior_pid)
  689.     {
  690.       registers_changed ();
  691.       registers_pid = inferior_pid;
  692.     }
  693.  
  694.   register_valid[regno] = 1;
  695.   memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
  696.  
  697.   /* On some architectures, e.g. HPPA, there are a few stray bits in some
  698.      registers, that the rest of the code would like to ignore.  */
  699. #ifdef CLEAN_UP_REGISTER_VALUE
  700.   CLEAN_UP_REGISTER_VALUE(regno, ®isters[REGISTER_BYTE(regno)]);
  701. #endif
  702. }
  703.  
  704.  
  705. /* This routine is getting awfully cluttered with #if's.  It's probably
  706.    time to turn this into READ_PC and define it in the tm.h file.
  707.    Ditto for write_pc.  */
  708.  
  709. CORE_ADDR
  710. read_pc ()
  711. {
  712. #ifdef TARGET_READ_PC
  713.   return TARGET_READ_PC (inferior_pid);
  714. #else
  715.   return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, inferior_pid));
  716. #endif
  717. }
  718.  
  719. CORE_ADDR
  720. read_pc_pid (pid)
  721.      int pid;
  722. {
  723. #ifdef TARGET_READ_PC
  724.   return TARGET_READ_PC (pid);
  725. #else
  726.   return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
  727. #endif
  728. }
  729.  
  730. void
  731. write_pc (val)
  732.      CORE_ADDR val;
  733. {
  734. #ifdef TARGET_WRITE_PC
  735.   TARGET_WRITE_PC (val, inferior_pid);
  736. #else
  737.   write_register_pid (PC_REGNUM, (long) val, inferior_pid);
  738. #ifdef NPC_REGNUM
  739.   write_register_pid (NPC_REGNUM, (long) val + 4, inferior_pid);
  740. #ifdef NNPC_REGNUM
  741.   write_register_pid (NNPC_REGNUM, (long) val + 8, inferior_pid);
  742. #endif
  743. #endif
  744. #endif
  745. }
  746.  
  747. void
  748. write_pc_pid (val, pid)
  749.      CORE_ADDR val;
  750.      int pid;
  751. {
  752. #ifdef TARGET_WRITE_PC
  753.   TARGET_WRITE_PC (val, pid);
  754. #else
  755.   write_register_pid (PC_REGNUM, (long) val, pid);
  756. #ifdef NPC_REGNUM
  757.   write_register_pid (NPC_REGNUM, (long) val + 4, pid);
  758. #ifdef NNPC_REGNUM
  759.   write_register_pid (NNPC_REGNUM, (long) val + 8, pid);
  760. #endif
  761. #endif
  762. #endif
  763. }
  764.  
  765. /* Cope with strage ways of getting to the stack and frame pointers */
  766.  
  767. CORE_ADDR
  768. read_sp ()
  769. {
  770. #ifdef TARGET_READ_SP
  771.   return TARGET_READ_SP ();
  772. #else
  773.   return read_register (SP_REGNUM);
  774. #endif
  775. }
  776.  
  777. void
  778. write_sp (val)
  779.      CORE_ADDR val;
  780. {
  781. #ifdef TARGET_WRITE_SP
  782.   TARGET_WRITE_SP (val);
  783. #else
  784.   write_register (SP_REGNUM, val);
  785. #endif
  786. }
  787.  
  788. CORE_ADDR
  789. read_fp ()
  790. {
  791. #ifdef TARGET_READ_FP
  792.   return TARGET_READ_FP ();
  793. #else
  794.   return read_register (FP_REGNUM);
  795. #endif
  796. }
  797.  
  798. void
  799. write_fp (val)
  800.      CORE_ADDR val;
  801. {
  802. #ifdef TARGET_WRITE_FP
  803.   TARGET_WRITE_FP (val);
  804. #else
  805.   write_register (FP_REGNUM, val);
  806. #endif
  807. }
  808.  
  809. /* Will calling read_var_value or locate_var_value on SYM end
  810.    up caring what frame it is being evaluated relative to?  SYM must
  811.    be non-NULL.  */
  812. int
  813. symbol_read_needs_frame (sym)
  814.      struct symbol *sym;
  815. {
  816.   switch (SYMBOL_CLASS (sym))
  817.     {
  818.       /* All cases listed explicitly so that gcc -Wall will detect it if
  819.      we failed to consider one.  */
  820.     case LOC_REGISTER:
  821.     case LOC_ARG:
  822.     case LOC_REF_ARG:
  823.     case LOC_REGPARM:
  824.     case LOC_REGPARM_ADDR:
  825.     case LOC_LOCAL:
  826.     case LOC_LOCAL_ARG:
  827.     case LOC_BASEREG:
  828.     case LOC_BASEREG_ARG:
  829.       return 1;
  830.  
  831.     case LOC_UNDEF:
  832.     case LOC_CONST:
  833.     case LOC_STATIC:
  834.     case LOC_TYPEDEF:
  835.  
  836.     case LOC_LABEL:
  837.       /* Getting the address of a label can be done independently of the block,
  838.      even if some *uses* of that address wouldn't work so well without
  839.      the right frame.  */
  840.  
  841.     case LOC_BLOCK:
  842.     case LOC_CONST_BYTES:
  843.     case LOC_OPTIMIZED_OUT:
  844.       return 0;
  845.     }
  846.   return 1;
  847. }
  848.  
  849. /* Given a struct symbol for a variable,
  850.    and a stack frame id, read the value of the variable
  851.    and return a (pointer to a) struct value containing the value. 
  852.    If the variable cannot be found, return a zero pointer.
  853.    If FRAME is NULL, use the selected_frame.  */
  854.  
  855. value_ptr
  856. read_var_value (var, frame)
  857.      register struct symbol *var;
  858.      FRAME frame;
  859. {
  860.   register value_ptr v;
  861.   struct frame_info *fi;
  862.   struct type *type = SYMBOL_TYPE (var);
  863.   CORE_ADDR addr;
  864.   register int len;
  865.  
  866.   v = allocate_value (type);
  867.   VALUE_LVAL (v) = lval_memory;    /* The most likely possibility.  */
  868.   len = TYPE_LENGTH (type);
  869.  
  870.   if (frame == 0) frame = selected_frame;
  871.  
  872.   switch (SYMBOL_CLASS (var))
  873.     {
  874.     case LOC_CONST:
  875.       /* Put the constant back in target format.  */
  876.       store_signed_integer (VALUE_CONTENTS_RAW (v), len,
  877.                 (LONGEST) SYMBOL_VALUE (var));
  878.       VALUE_LVAL (v) = not_lval;
  879.       return v;
  880.  
  881.     case LOC_LABEL:
  882.       /* Put the constant back in target format.  */
  883.       store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
  884.       VALUE_LVAL (v) = not_lval;
  885.       return v;
  886.  
  887.     case LOC_CONST_BYTES:
  888.       {
  889.     char *bytes_addr;
  890.     bytes_addr = SYMBOL_VALUE_BYTES (var);
  891.     memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
  892.     VALUE_LVAL (v) = not_lval;
  893.     return v;
  894.       }
  895.  
  896.     case LOC_STATIC:
  897.       addr = SYMBOL_VALUE_ADDRESS (var);
  898.       break;
  899.  
  900.     case LOC_ARG:
  901.       fi = get_frame_info (frame);
  902.       if (fi == NULL)
  903.     return 0;
  904.       addr = FRAME_ARGS_ADDRESS (fi);
  905.       if (!addr)
  906.     {
  907.       return 0;
  908.     }
  909.       addr += SYMBOL_VALUE (var);
  910.       break;
  911.  
  912.     case LOC_REF_ARG:
  913.       fi = get_frame_info (frame);
  914.       if (fi == NULL)
  915.     return 0;
  916.       addr = FRAME_ARGS_ADDRESS (fi);
  917.       if (!addr)
  918.     {
  919.       return 0;
  920.     }
  921.       addr += SYMBOL_VALUE (var);
  922.       addr = read_memory_unsigned_integer
  923.     (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  924.       break;
  925.  
  926.     case LOC_LOCAL:
  927.     case LOC_LOCAL_ARG:
  928.       fi = get_frame_info (frame);
  929.       if (fi == NULL)
  930.     return 0;
  931.       addr = FRAME_LOCALS_ADDRESS (fi);
  932.       addr += SYMBOL_VALUE (var);
  933.       break;
  934.  
  935.     case LOC_BASEREG:
  936.     case LOC_BASEREG_ARG:
  937.       {
  938.     char buf[MAX_REGISTER_RAW_SIZE];
  939.     get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
  940.                 NULL);
  941.     addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
  942.     addr += SYMBOL_VALUE (var);
  943.     break;
  944.       }
  945.                 
  946.     case LOC_TYPEDEF:
  947.       error ("Cannot look up value of a typedef");
  948.       break;
  949.  
  950.     case LOC_BLOCK:
  951.       VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
  952.       return v;
  953.  
  954.     case LOC_REGISTER:
  955.     case LOC_REGPARM:
  956.     case LOC_REGPARM_ADDR:
  957.       {
  958.     struct block *b;
  959.  
  960.     if (frame == NULL)
  961.       return 0;
  962.     b = get_frame_block (frame);
  963.     
  964.  
  965.     if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
  966.       {
  967.         addr =
  968.           value_as_pointer (value_from_register (lookup_pointer_type (type),
  969.                              SYMBOL_VALUE (var),
  970.                              frame));
  971.         VALUE_LVAL (v) = lval_memory;
  972.       }
  973.     else
  974.       return value_from_register (type, SYMBOL_VALUE (var), frame);
  975.       }
  976.       break;
  977.  
  978.     case LOC_OPTIMIZED_OUT:
  979.       VALUE_LVAL (v) = not_lval;
  980.       VALUE_OPTIMIZED_OUT (v) = 1;
  981.       return v;
  982.  
  983.     default:
  984.       error ("Cannot look up value of a botched symbol.");
  985.       break;
  986.     }
  987.  
  988.   VALUE_ADDRESS (v) = addr;
  989.   VALUE_LAZY (v) = 1;
  990.   return v;
  991. }
  992.  
  993. /* Return a value of type TYPE, stored in register REGNUM, in frame
  994.    FRAME. */
  995.  
  996. value_ptr
  997. value_from_register (type, regnum, frame)
  998.      struct type *type;
  999.      int regnum;
  1000.      FRAME frame;
  1001. {
  1002.   char raw_buffer [MAX_REGISTER_RAW_SIZE];
  1003.   CORE_ADDR addr;
  1004.   int optim;
  1005.   value_ptr v = allocate_value (type);
  1006.   int len = TYPE_LENGTH (type);
  1007.   char *value_bytes = 0;
  1008.   int value_bytes_copied = 0;
  1009.   int num_storage_locs;
  1010.   enum lval_type lval;
  1011.  
  1012.   VALUE_REGNO (v) = regnum;
  1013.  
  1014.   num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
  1015.               ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
  1016.               1);
  1017.  
  1018.   if (num_storage_locs > 1
  1019. #ifdef GDB_TARGET_IS_H8500
  1020.       || TYPE_CODE (type) == TYPE_CODE_PTR
  1021. #endif
  1022.       )
  1023.     {
  1024.       /* Value spread across multiple storage locations.  */
  1025.       
  1026.       int local_regnum;
  1027.       int mem_stor = 0, reg_stor = 0;
  1028.       int mem_tracking = 1;
  1029.       CORE_ADDR last_addr = 0;
  1030.       CORE_ADDR first_addr = 0;
  1031.  
  1032.       value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
  1033.  
  1034.       /* Copy all of the data out, whereever it may be.  */
  1035.  
  1036. #ifdef GDB_TARGET_IS_H8500
  1037. /* This piece of hideosity is required because the H8500 treats registers
  1038.    differently depending upon whether they are used as pointers or not.  As a
  1039.    pointer, a register needs to have a page register tacked onto the front.
  1040.    An alternate way to do this would be to have gcc output different register
  1041.    numbers for the pointer & non-pointer form of the register.  But, it
  1042.    doesn't, so we're stuck with this.  */
  1043.  
  1044.       if (TYPE_CODE (type) == TYPE_CODE_PTR
  1045.       && len > 2)
  1046.     {
  1047.       int page_regnum;
  1048.  
  1049.       switch (regnum)
  1050.         {
  1051.         case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
  1052.           page_regnum = SEG_D_REGNUM;
  1053.           break;
  1054.         case R4_REGNUM: case R5_REGNUM:
  1055.           page_regnum = SEG_E_REGNUM;
  1056.           break;
  1057.         case R6_REGNUM: case R7_REGNUM:
  1058.           page_regnum = SEG_T_REGNUM;
  1059.           break;
  1060.         }
  1061.  
  1062.       value_bytes[0] = 0;
  1063.       get_saved_register (value_bytes + 1,
  1064.                   &optim,
  1065.                   &addr,
  1066.                   frame,
  1067.                   page_regnum,
  1068.                   &lval);
  1069.  
  1070.       if (lval == lval_register)
  1071.         reg_stor++;
  1072.       else
  1073.         mem_stor++;
  1074.       first_addr = addr;
  1075.       last_addr = addr;
  1076.  
  1077.       get_saved_register (value_bytes + 2,
  1078.                   &optim,
  1079.                   &addr,
  1080.                   frame,
  1081.                   regnum,
  1082.                   &lval);
  1083.  
  1084.       if (lval == lval_register)
  1085.         reg_stor++;
  1086.       else
  1087.         {
  1088.           mem_stor++;
  1089.           mem_tracking = mem_tracking && (addr == last_addr);
  1090.         }
  1091.       last_addr = addr;
  1092.     }
  1093.       else
  1094. #endif                /* GDB_TARGET_IS_H8500 */
  1095.     for (local_regnum = regnum;
  1096.          value_bytes_copied < len;
  1097.          (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
  1098.           ++local_regnum))
  1099.       {
  1100.         get_saved_register (value_bytes + value_bytes_copied,
  1101.                 &optim,
  1102.                 &addr,
  1103.                 frame,
  1104.                 local_regnum,
  1105.                 &lval);
  1106.  
  1107.         if (regnum == local_regnum)
  1108.           first_addr = addr;
  1109.         if (lval == lval_register)
  1110.           reg_stor++;
  1111.         else
  1112.           {
  1113.         mem_stor++;
  1114.           
  1115.         mem_tracking =
  1116.           (mem_tracking
  1117.            && (regnum == local_regnum
  1118.                || addr == last_addr));
  1119.           }
  1120.         last_addr = addr;
  1121.       }
  1122.  
  1123.       if ((reg_stor && mem_stor)
  1124.       || (mem_stor && !mem_tracking))
  1125.     /* Mixed storage; all of the hassle we just went through was
  1126.        for some good purpose.  */
  1127.     {
  1128.       VALUE_LVAL (v) = lval_reg_frame_relative;
  1129.       VALUE_FRAME (v) = FRAME_FP (frame);
  1130.       VALUE_FRAME_REGNUM (v) = regnum;
  1131.     }
  1132.       else if (mem_stor)
  1133.     {
  1134.       VALUE_LVAL (v) = lval_memory;
  1135.       VALUE_ADDRESS (v) = first_addr;
  1136.     }
  1137.       else if (reg_stor)
  1138.     {
  1139.       VALUE_LVAL (v) = lval_register;
  1140.       VALUE_ADDRESS (v) = first_addr;
  1141.     }
  1142.       else
  1143.     fatal ("value_from_register: Value not stored anywhere!");
  1144.  
  1145.       VALUE_OPTIMIZED_OUT (v) = optim;
  1146.  
  1147.       /* Any structure stored in more than one register will always be
  1148.      an integral number of registers.  Otherwise, you'd need to do
  1149.      some fiddling with the last register copied here for little
  1150.      endian machines.  */
  1151.  
  1152.       /* Copy into the contents section of the value.  */
  1153.       memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
  1154.  
  1155.       /* Finally do any conversion necessary when extracting this
  1156.          type from more than one register.  */
  1157. #ifdef REGISTER_CONVERT_TO_TYPE
  1158.       REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
  1159. #endif
  1160.       return v;
  1161.     }
  1162.  
  1163.   /* Data is completely contained within a single register.  Locate the
  1164.      register's contents in a real register or in core;
  1165.      read the data in raw format.  */
  1166.  
  1167.   get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
  1168.   VALUE_OPTIMIZED_OUT (v) = optim;
  1169.   VALUE_LVAL (v) = lval;
  1170.   VALUE_ADDRESS (v) = addr;
  1171.  
  1172.   /* Convert raw data to virtual format if necessary.  */
  1173.   
  1174. #ifdef REGISTER_CONVERTIBLE
  1175.   if (REGISTER_CONVERTIBLE (regnum))
  1176.     {
  1177.       REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
  1178.                    raw_buffer, VALUE_CONTENTS_RAW (v));
  1179.     }
  1180.   else
  1181. #endif
  1182.     {
  1183.       /* Raw and virtual formats are the same for this register.  */
  1184.  
  1185. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  1186.       if (len < REGISTER_RAW_SIZE (regnum))
  1187.     {
  1188.         /* Big-endian, and we want less than full size.  */
  1189.       VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
  1190.     }
  1191. #endif
  1192.  
  1193.       memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
  1194.     }
  1195.   
  1196.   return v;
  1197. }
  1198.  
  1199. /* Given a struct symbol for a variable or function,
  1200.    and a stack frame id, 
  1201.    return a (pointer to a) struct value containing the properly typed
  1202.    address.  */
  1203.  
  1204. value_ptr
  1205. locate_var_value (var, frame)
  1206.      register struct symbol *var;
  1207.      FRAME frame;
  1208. {
  1209.   CORE_ADDR addr = 0;
  1210.   struct type *type = SYMBOL_TYPE (var);
  1211.   value_ptr lazy_value;
  1212.  
  1213.   /* Evaluate it first; if the result is a memory address, we're fine.
  1214.      Lazy evaluation pays off here. */
  1215.  
  1216.   lazy_value = read_var_value (var, frame);
  1217.   if (lazy_value == 0)
  1218.     error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
  1219.  
  1220.   if (VALUE_LAZY (lazy_value)
  1221.       || TYPE_CODE (type) == TYPE_CODE_FUNC)
  1222.     {
  1223.       addr = VALUE_ADDRESS (lazy_value);
  1224.       return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
  1225.     }
  1226.  
  1227.   /* Not a memory address; check what the problem was.  */
  1228.   switch (VALUE_LVAL (lazy_value)) 
  1229.     {
  1230.     case lval_register:
  1231.     case lval_reg_frame_relative:
  1232.       error ("Address requested for identifier \"%s\" which is in a register.",
  1233.          SYMBOL_SOURCE_NAME (var));
  1234.       break;
  1235.  
  1236.     default:
  1237.       error ("Can't take address of \"%s\" which isn't an lvalue.",
  1238.          SYMBOL_SOURCE_NAME (var));
  1239.       break;
  1240.     }
  1241.   return 0;  /* For lint -- never reached */
  1242. }
  1243.