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