findvar.c revision 46283
119370Spst/* Find a variable's value in memory, for GDB, the GNU debugger.
246283Sdfr   Copyright 1986, 87, 89, 91, 94, 95, 96, 1998
346283Sdfr   Free Software Foundation, Inc.
419370Spst
519370SpstThis file is part of GDB.
619370Spst
719370SpstThis program is free software; you can redistribute it and/or modify
819370Spstit under the terms of the GNU General Public License as published by
919370Spstthe Free Software Foundation; either version 2 of the License, or
1019370Spst(at your option) any later version.
1119370Spst
1219370SpstThis program is distributed in the hope that it will be useful,
1319370Spstbut WITHOUT ANY WARRANTY; without even the implied warranty of
1419370SpstMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1519370SpstGNU General Public License for more details.
1619370Spst
1719370SpstYou should have received a copy of the GNU General Public License
1819370Spstalong with this program; if not, write to the Free Software
1919370SpstFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2019370Spst
2119370Spst#include "defs.h"
2219370Spst#include "symtab.h"
2319370Spst#include "gdbtypes.h"
2419370Spst#include "frame.h"
2519370Spst#include "value.h"
2619370Spst#include "gdbcore.h"
2719370Spst#include "inferior.h"
2819370Spst#include "target.h"
2919370Spst#include "gdb_string.h"
3046283Sdfr#include "floatformat.h"
3146283Sdfr#include "symfile.h"	/* for overlay functions */
3219370Spst
3346283Sdfr/* This is used to indicate that we don't know the format of the floating point
3446283Sdfr   number.  Typically, this is useful for native ports, where the actual format
3546283Sdfr   is irrelevant, since no conversions will be taking place.  */
3646283Sdfr
3746283Sdfrconst struct floatformat floatformat_unknown;
3846283Sdfr
3919370Spst/* Registers we shouldn't try to store.  */
4019370Spst#if !defined (CANNOT_STORE_REGISTER)
4119370Spst#define CANNOT_STORE_REGISTER(regno) 0
4219370Spst#endif
4319370Spst
4446283Sdfrstatic void write_register_gen PARAMS ((int, char *));
4519370Spst
4619370Spst/* Basic byte-swapping routines.  GDB has needed these for a long time...
4719370Spst   All extract a target-format integer at ADDR which is LEN bytes long.  */
4819370Spst
4919370Spst#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
5019370Spst  /* 8 bit characters are a pretty safe assumption these days, so we
5119370Spst     assume it throughout all these swapping routines.  If we had to deal with
5219370Spst     9 bit characters, we would need to make len be in bits and would have
5319370Spst     to re-write these routines...  */
5419370Spst  you lose
5519370Spst#endif
5619370Spst
5719370SpstLONGEST
5819370Spstextract_signed_integer (addr, len)
5919370Spst     PTR addr;
6019370Spst     int len;
6119370Spst{
6219370Spst  LONGEST retval;
6319370Spst  unsigned char *p;
6419370Spst  unsigned char *startaddr = (unsigned char *)addr;
6519370Spst  unsigned char *endaddr = startaddr + len;
6619370Spst
6719370Spst  if (len > (int) sizeof (LONGEST))
6819370Spst    error ("\
6919370SpstThat operation is not available on integers of more than %d bytes.",
7019370Spst	   sizeof (LONGEST));
7119370Spst
7219370Spst  /* Start at the most significant end of the integer, and work towards
7319370Spst     the least significant.  */
7419370Spst  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
7519370Spst    {
7619370Spst      p = startaddr;
7719370Spst      /* Do the sign extension once at the start.  */
7819370Spst      retval = ((LONGEST)*p ^ 0x80) - 0x80;
7919370Spst      for (++p; p < endaddr; ++p)
8019370Spst	retval = (retval << 8) | *p;
8119370Spst    }
8219370Spst  else
8319370Spst    {
8419370Spst      p = endaddr - 1;
8519370Spst      /* Do the sign extension once at the start.  */
8619370Spst      retval = ((LONGEST)*p ^ 0x80) - 0x80;
8719370Spst      for (--p; p >= startaddr; --p)
8819370Spst	retval = (retval << 8) | *p;
8919370Spst    }
9019370Spst  return retval;
9119370Spst}
9219370Spst
9346283SdfrULONGEST
9419370Spstextract_unsigned_integer (addr, len)
9519370Spst     PTR addr;
9619370Spst     int len;
9719370Spst{
9846283Sdfr  ULONGEST retval;
9919370Spst  unsigned char *p;
10019370Spst  unsigned char *startaddr = (unsigned char *)addr;
10119370Spst  unsigned char *endaddr = startaddr + len;
10219370Spst
10346283Sdfr  if (len > (int) sizeof (ULONGEST))
10419370Spst    error ("\
10519370SpstThat operation is not available on integers of more than %d bytes.",
10646283Sdfr	   sizeof (ULONGEST));
10719370Spst
10819370Spst  /* Start at the most significant end of the integer, and work towards
10919370Spst     the least significant.  */
11019370Spst  retval = 0;
11119370Spst  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
11219370Spst    {
11319370Spst      for (p = startaddr; p < endaddr; ++p)
11419370Spst	retval = (retval << 8) | *p;
11519370Spst    }
11619370Spst  else
11719370Spst    {
11819370Spst      for (p = endaddr - 1; p >= startaddr; --p)
11919370Spst	retval = (retval << 8) | *p;
12019370Spst    }
12119370Spst  return retval;
12219370Spst}
12319370Spst
12419370Spst/* Sometimes a long long unsigned integer can be extracted as a
12519370Spst   LONGEST value.  This is done so that we can print these values
12619370Spst   better.  If this integer can be converted to a LONGEST, this
12719370Spst   function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
12819370Spst
12919370Spstint
13019370Spstextract_long_unsigned_integer (addr, orig_len, pval)
13119370Spst     PTR addr;
13219370Spst     int orig_len;
13319370Spst     LONGEST *pval;
13419370Spst{
13519370Spst  char *p, *first_addr;
13619370Spst  int len;
13719370Spst
13819370Spst  len = orig_len;
13919370Spst  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
14019370Spst    {
14119370Spst      for (p = (char *) addr;
14219370Spst	   len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
14319370Spst	   p++)
14419370Spst	{
14519370Spst	  if (*p == 0)
14619370Spst	    len--;
14719370Spst	  else
14819370Spst	    break;
14919370Spst	}
15019370Spst      first_addr = p;
15119370Spst    }
15219370Spst  else
15319370Spst    {
15419370Spst      first_addr = (char *) addr;
15519370Spst      for (p = (char *) addr + orig_len - 1;
15619370Spst	   len > (int) sizeof (LONGEST) && p >= (char *) addr;
15719370Spst	   p--)
15819370Spst	{
15919370Spst	  if (*p == 0)
16019370Spst	    len--;
16119370Spst	  else
16219370Spst	    break;
16319370Spst	}
16419370Spst    }
16519370Spst
16619370Spst  if (len <= (int) sizeof (LONGEST))
16719370Spst    {
16819370Spst      *pval = (LONGEST) extract_unsigned_integer (first_addr,
16919370Spst						  sizeof (LONGEST));
17019370Spst      return 1;
17119370Spst    }
17219370Spst
17319370Spst  return 0;
17419370Spst}
17519370Spst
17619370SpstCORE_ADDR
17719370Spstextract_address (addr, len)
17819370Spst     PTR addr;
17919370Spst     int len;
18019370Spst{
18119370Spst  /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
18219370Spst     whether we want this to be true eventually.  */
18346283Sdfr  return (CORE_ADDR)extract_unsigned_integer (addr, len);
18419370Spst}
18519370Spst
18619370Spstvoid
18719370Spststore_signed_integer (addr, len, val)
18819370Spst     PTR addr;
18919370Spst     int len;
19019370Spst     LONGEST val;
19119370Spst{
19219370Spst  unsigned char *p;
19319370Spst  unsigned char *startaddr = (unsigned char *)addr;
19419370Spst  unsigned char *endaddr = startaddr + len;
19519370Spst
19619370Spst  /* Start at the least significant end of the integer, and work towards
19719370Spst     the most significant.  */
19819370Spst  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
19919370Spst    {
20019370Spst      for (p = endaddr - 1; p >= startaddr; --p)
20119370Spst	{
20219370Spst	  *p = val & 0xff;
20319370Spst	  val >>= 8;
20419370Spst	}
20519370Spst    }
20619370Spst  else
20719370Spst    {
20819370Spst      for (p = startaddr; p < endaddr; ++p)
20919370Spst	{
21019370Spst	  *p = val & 0xff;
21119370Spst	  val >>= 8;
21219370Spst	}
21319370Spst    }
21419370Spst}
21519370Spst
21619370Spstvoid
21719370Spststore_unsigned_integer (addr, len, val)
21819370Spst     PTR addr;
21919370Spst     int len;
22046283Sdfr     ULONGEST val;
22119370Spst{
22219370Spst  unsigned char *p;
22319370Spst  unsigned char *startaddr = (unsigned char *)addr;
22419370Spst  unsigned char *endaddr = startaddr + len;
22519370Spst
22619370Spst  /* Start at the least significant end of the integer, and work towards
22719370Spst     the most significant.  */
22819370Spst  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
22919370Spst    {
23019370Spst      for (p = endaddr - 1; p >= startaddr; --p)
23119370Spst	{
23219370Spst	  *p = val & 0xff;
23319370Spst	  val >>= 8;
23419370Spst	}
23519370Spst    }
23619370Spst  else
23719370Spst    {
23819370Spst      for (p = startaddr; p < endaddr; ++p)
23919370Spst	{
24019370Spst	  *p = val & 0xff;
24119370Spst	  val >>= 8;
24219370Spst	}
24319370Spst    }
24419370Spst}
24519370Spst
24646283Sdfr/* Store the literal address "val" into
24746283Sdfr   gdb-local memory pointed to by "addr"
24846283Sdfr   for "len" bytes. */
24919370Spstvoid
25019370Spststore_address (addr, len, val)
25119370Spst     PTR addr;
25219370Spst     int len;
25346283Sdfr     LONGEST val;
25419370Spst{
25546283Sdfr  if( TARGET_BYTE_ORDER == BIG_ENDIAN
25646283Sdfr      &&  len != sizeof( LONGEST )) {
25746283Sdfr    /* On big-endian machines (e.g., HPPA 2.0, narrow mode)
25846283Sdfr     * just letting this fall through to the call below will
25946283Sdfr     * lead to the wrong bits being stored.
26046283Sdfr     *
26146283Sdfr     * Only the simplest case is fixed here, the others just
26246283Sdfr     * get the old behavior.
26346283Sdfr     */
26446283Sdfr    if( (len == sizeof( CORE_ADDR ))
26546283Sdfr	&&  (sizeof( LONGEST ) == 2 * sizeof( CORE_ADDR ))) {
26646283Sdfr      /* Watch out!  The high bits are garbage! */
26746283Sdfr      CORE_ADDR coerce[2];
26846283Sdfr      *(LONGEST*)&coerce = val;
26946283Sdfr
27046283Sdfr      store_unsigned_integer (addr, len, coerce[1] ); /* BIG_ENDIAN code! */
27146283Sdfr      return;
27246283Sdfr    }
27346283Sdfr  }
27446283Sdfr  store_unsigned_integer (addr, len, val);
27519370Spst}
27619370Spst
27719370Spst/* Swap LEN bytes at BUFFER between target and host byte-order.  */
27819370Spst#define SWAP_FLOATING(buffer,len) \
27919370Spst  do                                                                    \
28019370Spst    {                                                                   \
28119370Spst      if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER)                         \
28219370Spst        {                                                               \
28319370Spst          char tmp;                                                     \
28419370Spst          char *p = (char *)(buffer);                                   \
28519370Spst          char *q = ((char *)(buffer)) + len - 1;                       \
28619370Spst          for (; p < q; p++, q--)                                       \
28719370Spst            {                                                           \
28819370Spst              tmp = *q;                                                 \
28919370Spst              *q = *p;                                                  \
29019370Spst              *p = tmp;                                                 \
29119370Spst            }                                                           \
29219370Spst        }                                                               \
29319370Spst    }                                                                   \
29419370Spst  while (0)
29519370Spst
29646283Sdfr/* Extract a floating-point number from a target-order byte-stream at ADDR.
29746283Sdfr   Returns the value as type DOUBLEST.
29819370Spst
29946283Sdfr   If the host and target formats agree, we just copy the raw data into the
30046283Sdfr   appropriate type of variable and return, letting the host increase precision
30146283Sdfr   as necessary.  Otherwise, we call the conversion routine and let it do the
30246283Sdfr   dirty work.  */
30319370Spst
30419370SpstDOUBLEST
30519370Spstextract_floating (addr, len)
30619370Spst     PTR addr;
30719370Spst     int len;
30819370Spst{
30946283Sdfr  DOUBLEST dretval;
31046283Sdfr
31119370Spst  if (len == sizeof (float))
31219370Spst    {
31346283Sdfr      if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
31446283Sdfr	{
31546283Sdfr	  float retval;
31646283Sdfr
31746283Sdfr	  memcpy (&retval, addr, sizeof (retval));
31846283Sdfr	  return retval;
31946283Sdfr	}
32046283Sdfr      else
32146283Sdfr	floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
32219370Spst    }
32319370Spst  else if (len == sizeof (double))
32419370Spst    {
32546283Sdfr      if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
32646283Sdfr	{
32746283Sdfr	  double retval;
32846283Sdfr
32946283Sdfr	  memcpy (&retval, addr, sizeof (retval));
33046283Sdfr	  return retval;
33146283Sdfr	}
33246283Sdfr      else
33346283Sdfr	floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
33419370Spst    }
33519370Spst  else if (len == sizeof (DOUBLEST))
33619370Spst    {
33746283Sdfr      if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
33846283Sdfr	{
33946283Sdfr	  DOUBLEST retval;
34046283Sdfr
34146283Sdfr	  memcpy (&retval, addr, sizeof (retval));
34246283Sdfr	  return retval;
34346283Sdfr	}
34446283Sdfr      else
34546283Sdfr	floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
34619370Spst    }
34719370Spst  else
34819370Spst    {
34919370Spst      error ("Can't deal with a floating point number of %d bytes.", len);
35019370Spst    }
35146283Sdfr
35246283Sdfr  return dretval;
35319370Spst}
35419370Spst
35519370Spstvoid
35619370Spststore_floating (addr, len, val)
35719370Spst     PTR addr;
35819370Spst     int len;
35919370Spst     DOUBLEST val;
36019370Spst{
36119370Spst  if (len == sizeof (float))
36219370Spst    {
36346283Sdfr      if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
36446283Sdfr	{
36546283Sdfr	  float floatval = val;
36646283Sdfr
36746283Sdfr	  memcpy (addr, &floatval, sizeof (floatval));
36846283Sdfr	}
36946283Sdfr      else
37046283Sdfr	floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
37119370Spst    }
37219370Spst  else if (len == sizeof (double))
37319370Spst    {
37446283Sdfr      if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
37546283Sdfr	{
37646283Sdfr	  double doubleval = val;
37719370Spst
37846283Sdfr	  memcpy (addr, &doubleval, sizeof (doubleval));
37946283Sdfr	}
38046283Sdfr      else
38146283Sdfr	floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
38219370Spst    }
38319370Spst  else if (len == sizeof (DOUBLEST))
38419370Spst    {
38546283Sdfr      if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
38646283Sdfr	memcpy (addr, &val, sizeof (val));
38746283Sdfr      else
38846283Sdfr	floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
38919370Spst    }
39019370Spst  else
39119370Spst    {
39219370Spst      error ("Can't deal with a floating point number of %d bytes.", len);
39319370Spst    }
39419370Spst}
39519370Spst
39619370Spst#if !defined (GET_SAVED_REGISTER)
39719370Spst
39819370Spst/* Return the address in which frame FRAME's value of register REGNUM
39919370Spst   has been saved in memory.  Or return zero if it has not been saved.
40019370Spst   If REGNUM specifies the SP, the value we return is actually
40119370Spst   the SP value, not an address where it was saved.  */
40219370Spst
40319370SpstCORE_ADDR
40419370Spstfind_saved_register (frame, regnum)
40519370Spst     struct frame_info *frame;
40619370Spst     int regnum;
40719370Spst{
40819370Spst  register struct frame_info *frame1 = NULL;
40919370Spst  register CORE_ADDR addr = 0;
41019370Spst
41119370Spst  if (frame == NULL)		/* No regs saved if want current frame */
41219370Spst    return 0;
41319370Spst
41419370Spst#ifdef HAVE_REGISTER_WINDOWS
41519370Spst  /* We assume that a register in a register window will only be saved
41619370Spst     in one place (since the name changes and/or disappears as you go
41719370Spst     towards inner frames), so we only call get_frame_saved_regs on
41819370Spst     the current frame.  This is directly in contradiction to the
41919370Spst     usage below, which assumes that registers used in a frame must be
42019370Spst     saved in a lower (more interior) frame.  This change is a result
42119370Spst     of working on a register window machine; get_frame_saved_regs
42219370Spst     always returns the registers saved within a frame, within the
42319370Spst     context (register namespace) of that frame. */
42419370Spst
42519370Spst  /* However, note that we don't want this to return anything if
42619370Spst     nothing is saved (if there's a frame inside of this one).  Also,
42719370Spst     callers to this routine asking for the stack pointer want the
42819370Spst     stack pointer saved for *this* frame; this is returned from the
42919370Spst     next frame.  */
43019370Spst
43119370Spst  if (REGISTER_IN_WINDOW_P(regnum))
43219370Spst    {
43319370Spst      frame1 = get_next_frame (frame);
43419370Spst      if (!frame1) return 0;	/* Registers of this frame are active.  */
43519370Spst
43619370Spst      /* Get the SP from the next frame in; it will be this
43719370Spst	 current frame.  */
43819370Spst      if (regnum != SP_REGNUM)
43919370Spst	frame1 = frame;
44019370Spst
44146283Sdfr      FRAME_INIT_SAVED_REGS (frame1);
44246283Sdfr      return frame1->saved_regs[regnum];	/* ... which might be zero */
44319370Spst    }
44419370Spst#endif /* HAVE_REGISTER_WINDOWS */
44519370Spst
44619370Spst  /* Note that this next routine assumes that registers used in
44719370Spst     frame x will be saved only in the frame that x calls and
44819370Spst     frames interior to it.  This is not true on the sparc, but the
44919370Spst     above macro takes care of it, so we should be all right. */
45019370Spst  while (1)
45119370Spst    {
45219370Spst      QUIT;
45319370Spst      frame1 = get_prev_frame (frame1);
45419370Spst      if (frame1 == 0 || frame1 == frame)
45519370Spst	break;
45646283Sdfr      FRAME_INIT_SAVED_REGS (frame1);
45746283Sdfr      if (frame1->saved_regs[regnum])
45846283Sdfr	addr = frame1->saved_regs[regnum];
45919370Spst    }
46019370Spst
46119370Spst  return addr;
46219370Spst}
46319370Spst
46419370Spst/* Find register number REGNUM relative to FRAME and put its (raw,
46519370Spst   target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
46619370Spst   variable was optimized out (and thus can't be fetched).  Set *LVAL
46719370Spst   to lval_memory, lval_register, or not_lval, depending on whether
46819370Spst   the value was fetched from memory, from a register, or in a strange
46919370Spst   and non-modifiable way (e.g. a frame pointer which was calculated
47019370Spst   rather than fetched).  Set *ADDRP to the address, either in memory
47119370Spst   on as a REGISTER_BYTE offset into the registers array.
47219370Spst
47319370Spst   Note that this implementation never sets *LVAL to not_lval.  But
47419370Spst   it can be replaced by defining GET_SAVED_REGISTER and supplying
47519370Spst   your own.
47619370Spst
47719370Spst   The argument RAW_BUFFER must point to aligned memory.  */
47819370Spst
47919370Spstvoid
48019370Spstget_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
48119370Spst     char *raw_buffer;
48219370Spst     int *optimized;
48319370Spst     CORE_ADDR *addrp;
48419370Spst     struct frame_info *frame;
48519370Spst     int regnum;
48619370Spst     enum lval_type *lval;
48719370Spst{
48819370Spst  CORE_ADDR addr;
48919370Spst
49019370Spst  if (!target_has_registers)
49119370Spst    error ("No registers.");
49219370Spst
49319370Spst  /* Normal systems don't optimize out things with register numbers.  */
49419370Spst  if (optimized != NULL)
49519370Spst    *optimized = 0;
49619370Spst  addr = find_saved_register (frame, regnum);
49719370Spst  if (addr != 0)
49819370Spst    {
49919370Spst      if (lval != NULL)
50019370Spst	*lval = lval_memory;
50119370Spst      if (regnum == SP_REGNUM)
50219370Spst	{
50319370Spst	  if (raw_buffer != NULL)
50419370Spst	    {
50519370Spst	      /* Put it back in target format.  */
50646283Sdfr	      store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), (LONGEST)addr);
50719370Spst	    }
50819370Spst	  if (addrp != NULL)
50919370Spst	    *addrp = 0;
51019370Spst	  return;
51119370Spst	}
51219370Spst      if (raw_buffer != NULL)
51319370Spst	read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
51419370Spst    }
51519370Spst  else
51619370Spst    {
51719370Spst      if (lval != NULL)
51819370Spst	*lval = lval_register;
51919370Spst      addr = REGISTER_BYTE (regnum);
52019370Spst      if (raw_buffer != NULL)
52119370Spst	read_register_gen (regnum, raw_buffer);
52219370Spst    }
52319370Spst  if (addrp != NULL)
52419370Spst    *addrp = addr;
52519370Spst}
52619370Spst#endif /* GET_SAVED_REGISTER.  */
52719370Spst
52846283Sdfr/* Copy the bytes of register REGNUM, relative to the input stack frame,
52919370Spst   into our memory at MYADDR, in target byte order.
53019370Spst   The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
53119370Spst
53219370Spst   Returns 1 if could not be read, 0 if could.  */
53319370Spst
53419370Spstint
53546283Sdfrread_relative_register_raw_bytes_for_frame (regnum, myaddr, frame)
53619370Spst     int regnum;
53719370Spst     char *myaddr;
53846283Sdfr     struct frame_info *frame;
53919370Spst{
54019370Spst  int optim;
54146283Sdfr  if (regnum == FP_REGNUM && frame)
54219370Spst    {
54346283Sdfr      /* Put it back in target format. */
54419370Spst      store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
54546283Sdfr		     (LONGEST)FRAME_FP(frame));
54646283Sdfr
54719370Spst      return 0;
54819370Spst    }
54919370Spst
55046283Sdfr  get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
55119370Spst                      regnum, (enum lval_type *)NULL);
55246283Sdfr
55346283Sdfr  if (register_valid [regnum] < 0)
55446283Sdfr    return 1;	/* register value not available */
55546283Sdfr
55619370Spst  return optim;
55719370Spst}
55819370Spst
55946283Sdfr/* Copy the bytes of register REGNUM, relative to the current stack frame,
56046283Sdfr   into our memory at MYADDR, in target byte order.
56146283Sdfr   The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
56246283Sdfr
56346283Sdfr   Returns 1 if could not be read, 0 if could.  */
56446283Sdfr
56546283Sdfrint
56646283Sdfrread_relative_register_raw_bytes (regnum, myaddr)
56746283Sdfr     int regnum;
56846283Sdfr     char *myaddr;
56946283Sdfr{
57046283Sdfr  return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
57146283Sdfr						     selected_frame);
57246283Sdfr}
57346283Sdfr
57419370Spst/* Return a `value' with the contents of register REGNUM
57519370Spst   in its virtual format, with the type specified by
57646283Sdfr   REGISTER_VIRTUAL_TYPE.
57719370Spst
57846283Sdfr   NOTE: returns NULL if register value is not available.
57946283Sdfr   Caller will check return value or die!  */
58046283Sdfr
58119370Spstvalue_ptr
58219370Spstvalue_of_register (regnum)
58319370Spst     int regnum;
58419370Spst{
58519370Spst  CORE_ADDR addr;
58619370Spst  int optim;
58719370Spst  register value_ptr reg_val;
58819370Spst  char raw_buffer[MAX_REGISTER_RAW_SIZE];
58919370Spst  enum lval_type lval;
59019370Spst
59119370Spst  get_saved_register (raw_buffer, &optim, &addr,
59219370Spst		      selected_frame, regnum, &lval);
59319370Spst
59446283Sdfr  if (register_valid[regnum] < 0)
59546283Sdfr    return NULL;	/* register value not available */
59646283Sdfr
59719370Spst  reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
59819370Spst
59919370Spst  /* Convert raw data to virtual format if necessary.  */
60019370Spst
60119370Spst#ifdef REGISTER_CONVERTIBLE
60219370Spst  if (REGISTER_CONVERTIBLE (regnum))
60319370Spst    {
60419370Spst      REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
60519370Spst				   raw_buffer, VALUE_CONTENTS_RAW (reg_val));
60619370Spst    }
60719370Spst  else
60819370Spst#endif
60946283Sdfr    if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
61046283Sdfr      memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
61146283Sdfr	      REGISTER_RAW_SIZE (regnum));
61246283Sdfr    else
61346283Sdfr      fatal ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
61446283Sdfr	     REGISTER_NAME (regnum), regnum,
61546283Sdfr	     REGISTER_RAW_SIZE (regnum), REGISTER_VIRTUAL_SIZE (regnum));
61619370Spst  VALUE_LVAL (reg_val) = lval;
61719370Spst  VALUE_ADDRESS (reg_val) = addr;
61819370Spst  VALUE_REGNO (reg_val) = regnum;
61919370Spst  VALUE_OPTIMIZED_OUT (reg_val) = optim;
62019370Spst  return reg_val;
62119370Spst}
62219370Spst
62319370Spst/* Low level examining and depositing of registers.
62419370Spst
62519370Spst   The caller is responsible for making
62619370Spst   sure that the inferior is stopped before calling the fetching routines,
62719370Spst   or it will get garbage.  (a change from GDB version 3, in which
62819370Spst   the caller got the value from the last stop).  */
62919370Spst
63019370Spst/* Contents of the registers in target byte order.
63146283Sdfr   We allocate some extra slop since we do a lot of memcpy's around
63246283Sdfr   `registers', and failing-soft is better than failing hard.  */
63346283Sdfr
63419370Spstchar registers[REGISTER_BYTES + /* SLOP */ 256];
63519370Spst
63646283Sdfr/* Nonzero if that register has been fetched,
63746283Sdfr   -1 if register value not available. */
63846283SdfrSIGNED char register_valid[NUM_REGS];
63919370Spst
64019370Spst/* The thread/process associated with the current set of registers.  For now,
64119370Spst   -1 is special, and means `no current process'.  */
64219370Spstint registers_pid = -1;
64319370Spst
64419370Spst/* Indicate that registers may have changed, so invalidate the cache.  */
64519370Spst
64619370Spstvoid
64719370Spstregisters_changed ()
64819370Spst{
64919370Spst  int i;
65019370Spst  int numregs = ARCH_NUM_REGS;
65119370Spst
65219370Spst  registers_pid = -1;
65319370Spst
65446283Sdfr  /* Force cleanup of any alloca areas if using C alloca instead of
65546283Sdfr     a builtin alloca.  This particular call is used to clean up
65646283Sdfr     areas allocated by low level target code which may build up
65746283Sdfr     during lengthy interactions between gdb and the target before
65846283Sdfr     gdb gives control to the user (ie watchpoints).  */
65946283Sdfr  alloca (0);
66046283Sdfr
66119370Spst  for (i = 0; i < numregs; i++)
66219370Spst    register_valid[i] = 0;
66319370Spst
66419370Spst  if (registers_changed_hook)
66519370Spst    registers_changed_hook ();
66619370Spst}
66719370Spst
66819370Spst/* Indicate that all registers have been fetched, so mark them all valid.  */
66919370Spstvoid
67019370Spstregisters_fetched ()
67119370Spst{
67219370Spst  int i;
67319370Spst  int numregs = ARCH_NUM_REGS;
67419370Spst  for (i = 0; i < numregs; i++)
67519370Spst    register_valid[i] = 1;
67619370Spst}
67719370Spst
67819370Spst/* read_register_bytes and write_register_bytes are generally a *BAD* idea.
67919370Spst   They are inefficient because they need to check for partial updates, which
68019370Spst   can only be done by scanning through all of the registers and seeing if the
68119370Spst   bytes that are being read/written fall inside of an invalid register.  [The
68219370Spst    main reason this is necessary is that register sizes can vary, so a simple
68319370Spst    index won't suffice.]  It is far better to call read_register_gen if you
68419370Spst   want to get at the raw register contents, as it only takes a regno as an
68519370Spst   argument, and therefore can't do a partial register update.  It would also
68619370Spst   be good to have a write_register_gen for similar reasons.
68719370Spst
68819370Spst   Prior to the recent fixes to check for partial updates, both read and
68919370Spst   write_register_bytes always checked to see if any registers were stale, and
69019370Spst   then called target_fetch_registers (-1) to update the whole set.  This
69119370Spst   caused really slowed things down for remote targets.  */
69219370Spst
69319370Spst/* Copy INLEN bytes of consecutive data from registers
69419370Spst   starting with the INREGBYTE'th byte of register data
69519370Spst   into memory at MYADDR.  */
69619370Spst
69719370Spstvoid
69819370Spstread_register_bytes (inregbyte, myaddr, inlen)
69919370Spst     int inregbyte;
70019370Spst     char *myaddr;
70119370Spst     int inlen;
70219370Spst{
70319370Spst  int inregend = inregbyte + inlen;
70419370Spst  int regno;
70519370Spst
70619370Spst  if (registers_pid != inferior_pid)
70719370Spst    {
70819370Spst      registers_changed ();
70919370Spst      registers_pid = inferior_pid;
71019370Spst    }
71119370Spst
71219370Spst  /* See if we are trying to read bytes from out-of-date registers.  If so,
71319370Spst     update just those registers.  */
71419370Spst
71519370Spst  for (regno = 0; regno < NUM_REGS; regno++)
71619370Spst    {
71719370Spst      int regstart, regend;
71819370Spst      int startin, endin;
71919370Spst
72019370Spst      if (register_valid[regno])
72119370Spst	continue;
72219370Spst
72346283Sdfr      if (REGISTER_NAME (regno) == NULL || *REGISTER_NAME (regno) == '\0')
72446283Sdfr	continue;
72546283Sdfr
72619370Spst      regstart = REGISTER_BYTE (regno);
72719370Spst      regend = regstart + REGISTER_RAW_SIZE (regno);
72819370Spst
72919370Spst      startin = regstart >= inregbyte && regstart < inregend;
73019370Spst      endin = regend > inregbyte && regend <= inregend;
73119370Spst
73219370Spst      if (!startin && !endin)
73319370Spst	continue;
73419370Spst
73519370Spst      /* We've found an invalid register where at least one byte will be read.
73619370Spst	 Update it from the target.  */
73719370Spst
73819370Spst      target_fetch_registers (regno);
73919370Spst
74019370Spst      if (!register_valid[regno])
74119370Spst	error ("read_register_bytes:  Couldn't update register %d.", regno);
74219370Spst    }
74319370Spst
74419370Spst  if (myaddr != NULL)
74519370Spst    memcpy (myaddr, &registers[inregbyte], inlen);
74619370Spst}
74719370Spst
74819370Spst/* Read register REGNO into memory at MYADDR, which must be large enough
74919370Spst   for REGISTER_RAW_BYTES (REGNO).  Target byte-order.
75019370Spst   If the register is known to be the size of a CORE_ADDR or smaller,
75119370Spst   read_register can be used instead.  */
75219370Spstvoid
75319370Spstread_register_gen (regno, myaddr)
75419370Spst     int regno;
75519370Spst     char *myaddr;
75619370Spst{
75719370Spst  if (registers_pid != inferior_pid)
75819370Spst    {
75919370Spst      registers_changed ();
76019370Spst      registers_pid = inferior_pid;
76119370Spst    }
76219370Spst
76319370Spst  if (!register_valid[regno])
76419370Spst    target_fetch_registers (regno);
76519370Spst  memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
76619370Spst	  REGISTER_RAW_SIZE (regno));
76719370Spst}
76819370Spst
76919370Spst/* Write register REGNO at MYADDR to the target.  MYADDR points at
77019370Spst   REGISTER_RAW_BYTES(REGNO), which must be in target byte-order.  */
77119370Spst
77246283Sdfrstatic void
77319370Spstwrite_register_gen (regno, myaddr)
77419370Spst     int regno;
77519370Spst     char *myaddr;
77619370Spst{
77719370Spst  int size;
77819370Spst
77919370Spst  /* On the sparc, writing %g0 is a no-op, so we don't even want to change
78019370Spst     the registers array if something writes to this register.  */
78119370Spst  if (CANNOT_STORE_REGISTER (regno))
78219370Spst    return;
78319370Spst
78419370Spst  if (registers_pid != inferior_pid)
78519370Spst    {
78619370Spst      registers_changed ();
78719370Spst      registers_pid = inferior_pid;
78819370Spst    }
78919370Spst
79019370Spst  size = REGISTER_RAW_SIZE(regno);
79119370Spst
79219370Spst  /* If we have a valid copy of the register, and new value == old value,
79319370Spst     then don't bother doing the actual store. */
79419370Spst
79519370Spst  if (register_valid [regno]
79619370Spst      && memcmp (&registers[REGISTER_BYTE (regno)], myaddr, size) == 0)
79719370Spst    return;
79819370Spst
79919370Spst  target_prepare_to_store ();
80019370Spst
80119370Spst  memcpy (&registers[REGISTER_BYTE (regno)], myaddr, size);
80219370Spst
80319370Spst  register_valid [regno] = 1;
80419370Spst
80519370Spst  target_store_registers (regno);
80619370Spst}
80719370Spst
80819370Spst/* Copy INLEN bytes of consecutive data from memory at MYADDR
80919370Spst   into registers starting with the MYREGSTART'th byte of register data.  */
81019370Spst
81119370Spstvoid
81219370Spstwrite_register_bytes (myregstart, myaddr, inlen)
81319370Spst     int myregstart;
81419370Spst     char *myaddr;
81519370Spst     int inlen;
81619370Spst{
81719370Spst  int myregend = myregstart + inlen;
81819370Spst  int regno;
81919370Spst
82019370Spst  target_prepare_to_store ();
82119370Spst
82219370Spst  /* Scan through the registers updating any that are covered by the range
82319370Spst     myregstart<=>myregend using write_register_gen, which does nice things
82419370Spst     like handling threads, and avoiding updates when the new and old contents
82519370Spst     are the same.  */
82619370Spst
82719370Spst  for (regno = 0; regno < NUM_REGS; regno++)
82819370Spst    {
82919370Spst      int regstart, regend;
83019370Spst      int startin, endin;
83119370Spst      char regbuf[MAX_REGISTER_RAW_SIZE];
83219370Spst
83319370Spst      regstart = REGISTER_BYTE (regno);
83419370Spst      regend = regstart + REGISTER_RAW_SIZE (regno);
83519370Spst
83619370Spst      startin = regstart >= myregstart && regstart < myregend;
83719370Spst      endin = regend > myregstart && regend <= myregend;
83819370Spst
83919370Spst      if (!startin && !endin)
84019370Spst	continue;		/* Register is completely out of range */
84119370Spst
84219370Spst      if (startin && endin)	/* register is completely in range */
84319370Spst	{
84419370Spst	  write_register_gen (regno, myaddr + (regstart - myregstart));
84519370Spst	  continue;
84619370Spst	}
84719370Spst
84819370Spst      /* We may be doing a partial update of an invalid register.  Update it
84919370Spst	 from the target before scribbling on it.  */
85019370Spst      read_register_gen (regno, regbuf);
85119370Spst
85219370Spst      if (startin)
85319370Spst	memcpy (registers + regstart,
85419370Spst		myaddr + regstart - myregstart,
85519370Spst		myregend - regstart);
85619370Spst      else			/* endin */
85719370Spst	memcpy (registers + myregstart,
85819370Spst		myaddr,
85919370Spst		regend - myregstart);
86019370Spst      target_store_registers (regno);
86119370Spst    }
86219370Spst}
86319370Spst
86419370Spst/* Return the raw contents of register REGNO, regarding it as an integer.  */
86519370Spst/* This probably should be returning LONGEST rather than CORE_ADDR.  */
86619370Spst
86719370SpstCORE_ADDR
86819370Spstread_register (regno)
86919370Spst     int regno;
87019370Spst{
87119370Spst  if (registers_pid != inferior_pid)
87219370Spst    {
87319370Spst      registers_changed ();
87419370Spst      registers_pid = inferior_pid;
87519370Spst    }
87619370Spst
87719370Spst  if (!register_valid[regno])
87819370Spst    target_fetch_registers (regno);
87919370Spst
88046283Sdfr  return (CORE_ADDR)extract_address (&registers[REGISTER_BYTE (regno)],
88146283Sdfr				     REGISTER_RAW_SIZE(regno));
88219370Spst}
88319370Spst
88419370SpstCORE_ADDR
88519370Spstread_register_pid (regno, pid)
88619370Spst     int regno, pid;
88719370Spst{
88819370Spst  int save_pid;
88919370Spst  CORE_ADDR retval;
89019370Spst
89119370Spst  if (pid == inferior_pid)
89219370Spst    return read_register (regno);
89319370Spst
89419370Spst  save_pid = inferior_pid;
89519370Spst
89619370Spst  inferior_pid = pid;
89719370Spst
89819370Spst  retval = read_register (regno);
89919370Spst
90019370Spst  inferior_pid = save_pid;
90119370Spst
90219370Spst  return retval;
90319370Spst}
90419370Spst
90546283Sdfr/* Store VALUE, into the raw contents of register number REGNO.
90646283Sdfr   This should probably write a LONGEST rather than a CORE_ADDR */
90719370Spst
90819370Spstvoid
90919370Spstwrite_register (regno, val)
91019370Spst     int regno;
91119370Spst     LONGEST val;
91219370Spst{
91319370Spst  PTR buf;
91419370Spst  int size;
91519370Spst
91619370Spst  /* On the sparc, writing %g0 is a no-op, so we don't even want to change
91719370Spst     the registers array if something writes to this register.  */
91819370Spst  if (CANNOT_STORE_REGISTER (regno))
91919370Spst    return;
92019370Spst
92119370Spst  if (registers_pid != inferior_pid)
92219370Spst    {
92319370Spst      registers_changed ();
92419370Spst      registers_pid = inferior_pid;
92519370Spst    }
92619370Spst
92719370Spst  size = REGISTER_RAW_SIZE(regno);
92819370Spst  buf = alloca (size);
92946283Sdfr  store_signed_integer (buf, size, (LONGEST)val);
93019370Spst
93119370Spst  /* If we have a valid copy of the register, and new value == old value,
93219370Spst     then don't bother doing the actual store. */
93319370Spst
93419370Spst  if (register_valid [regno]
93519370Spst      && memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
93619370Spst    return;
93719370Spst
93819370Spst  target_prepare_to_store ();
93919370Spst
94019370Spst  memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
94119370Spst
94219370Spst  register_valid [regno] = 1;
94319370Spst
94419370Spst  target_store_registers (regno);
94519370Spst}
94619370Spst
94746283Sdfrvoid
94819370Spstwrite_register_pid (regno, val, pid)
94919370Spst     int regno;
95046283Sdfr     CORE_ADDR val;
95119370Spst     int pid;
95219370Spst{
95319370Spst  int save_pid;
95419370Spst
95519370Spst  if (pid == inferior_pid)
95619370Spst    {
95719370Spst      write_register (regno, val);
95819370Spst      return;
95919370Spst    }
96019370Spst
96119370Spst  save_pid = inferior_pid;
96219370Spst
96319370Spst  inferior_pid = pid;
96419370Spst
96519370Spst  write_register (regno, val);
96619370Spst
96719370Spst  inferior_pid = save_pid;
96819370Spst}
96919370Spst
97019370Spst/* Record that register REGNO contains VAL.
97119370Spst   This is used when the value is obtained from the inferior or core dump,
97246283Sdfr   so there is no need to store the value there.
97319370Spst
97446283Sdfr   If VAL is a NULL pointer, then it's probably an unsupported register.  We
97546283Sdfr   just set it's value to all zeros.  We might want to record this fact, and
97646283Sdfr   report it to the users of read_register and friends.
97746283Sdfr*/
97846283Sdfr
97919370Spstvoid
98019370Spstsupply_register (regno, val)
98119370Spst     int regno;
98219370Spst     char *val;
98319370Spst{
98446283Sdfr#if 1
98519370Spst  if (registers_pid != inferior_pid)
98619370Spst    {
98719370Spst      registers_changed ();
98819370Spst      registers_pid = inferior_pid;
98919370Spst    }
99046283Sdfr#endif
99119370Spst
99219370Spst  register_valid[regno] = 1;
99346283Sdfr  if (val)
99446283Sdfr    memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
99546283Sdfr  else
99646283Sdfr    memset (&registers[REGISTER_BYTE (regno)], '\000', REGISTER_RAW_SIZE (regno));
99719370Spst
99819370Spst  /* On some architectures, e.g. HPPA, there are a few stray bits in some
99919370Spst     registers, that the rest of the code would like to ignore.  */
100019370Spst#ifdef CLEAN_UP_REGISTER_VALUE
100119370Spst  CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
100219370Spst#endif
100319370Spst}
100419370Spst
100519370Spst
100619370Spst/* This routine is getting awfully cluttered with #if's.  It's probably
100719370Spst   time to turn this into READ_PC and define it in the tm.h file.
100819370Spst   Ditto for write_pc.  */
100919370Spst
101019370SpstCORE_ADDR
101146283Sdfrread_pc_pid (pid)
101246283Sdfr     int pid;
101319370Spst{
101446283Sdfr  int  saved_inferior_pid;
101546283Sdfr  CORE_ADDR  pc_val;
101646283Sdfr
101746283Sdfr  /* In case pid != inferior_pid. */
101846283Sdfr  saved_inferior_pid = inferior_pid;
101946283Sdfr  inferior_pid = pid;
102046283Sdfr
102119370Spst#ifdef TARGET_READ_PC
102246283Sdfr  pc_val = TARGET_READ_PC (pid);
102319370Spst#else
102446283Sdfr  pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
102519370Spst#endif
102646283Sdfr
102746283Sdfr  inferior_pid = saved_inferior_pid;
102846283Sdfr  return pc_val;
102919370Spst}
103019370Spst
103119370SpstCORE_ADDR
103246283Sdfrread_pc ()
103319370Spst{
103446283Sdfr  return read_pc_pid (inferior_pid);
103519370Spst}
103619370Spst
103719370Spstvoid
103846283Sdfrwrite_pc_pid (pc, pid)
103946283Sdfr     CORE_ADDR pc;
104046283Sdfr     int pid;
104119370Spst{
104246283Sdfr  int  saved_inferior_pid;
104346283Sdfr
104446283Sdfr  /* In case pid != inferior_pid. */
104546283Sdfr  saved_inferior_pid = inferior_pid;
104646283Sdfr  inferior_pid = pid;
104746283Sdfr
104819370Spst#ifdef TARGET_WRITE_PC
104946283Sdfr  TARGET_WRITE_PC (pc, pid);
105019370Spst#else
105146283Sdfr  write_register_pid (PC_REGNUM, pc, pid);
105219370Spst#ifdef NPC_REGNUM
105346283Sdfr  write_register_pid (NPC_REGNUM, pc + 4, pid);
105419370Spst#ifdef NNPC_REGNUM
105546283Sdfr  write_register_pid (NNPC_REGNUM, pc + 8, pid);
105619370Spst#endif
105719370Spst#endif
105819370Spst#endif
105946283Sdfr
106046283Sdfr  inferior_pid = saved_inferior_pid;
106119370Spst}
106219370Spst
106319370Spstvoid
106446283Sdfrwrite_pc (pc)
106546283Sdfr     CORE_ADDR pc;
106619370Spst{
106746283Sdfr  write_pc_pid (pc, inferior_pid);
106819370Spst}
106919370Spst
107019370Spst/* Cope with strage ways of getting to the stack and frame pointers */
107119370Spst
107219370SpstCORE_ADDR
107319370Spstread_sp ()
107419370Spst{
107519370Spst#ifdef TARGET_READ_SP
107619370Spst  return TARGET_READ_SP ();
107719370Spst#else
107819370Spst  return read_register (SP_REGNUM);
107919370Spst#endif
108019370Spst}
108119370Spst
108219370Spstvoid
108319370Spstwrite_sp (val)
108419370Spst     CORE_ADDR val;
108519370Spst{
108619370Spst#ifdef TARGET_WRITE_SP
108719370Spst  TARGET_WRITE_SP (val);
108819370Spst#else
108919370Spst  write_register (SP_REGNUM, val);
109019370Spst#endif
109119370Spst}
109219370Spst
109319370SpstCORE_ADDR
109419370Spstread_fp ()
109519370Spst{
109619370Spst#ifdef TARGET_READ_FP
109719370Spst  return TARGET_READ_FP ();
109819370Spst#else
109919370Spst  return read_register (FP_REGNUM);
110019370Spst#endif
110119370Spst}
110219370Spst
110319370Spstvoid
110419370Spstwrite_fp (val)
110519370Spst     CORE_ADDR val;
110619370Spst{
110719370Spst#ifdef TARGET_WRITE_FP
110819370Spst  TARGET_WRITE_FP (val);
110919370Spst#else
111019370Spst  write_register (FP_REGNUM, val);
111119370Spst#endif
111219370Spst}
111319370Spst
111419370Spst/* Will calling read_var_value or locate_var_value on SYM end
111519370Spst   up caring what frame it is being evaluated relative to?  SYM must
111619370Spst   be non-NULL.  */
111719370Spstint
111819370Spstsymbol_read_needs_frame (sym)
111919370Spst     struct symbol *sym;
112019370Spst{
112119370Spst  switch (SYMBOL_CLASS (sym))
112219370Spst    {
112319370Spst      /* All cases listed explicitly so that gcc -Wall will detect it if
112419370Spst	 we failed to consider one.  */
112519370Spst    case LOC_REGISTER:
112619370Spst    case LOC_ARG:
112719370Spst    case LOC_REF_ARG:
112819370Spst    case LOC_REGPARM:
112919370Spst    case LOC_REGPARM_ADDR:
113019370Spst    case LOC_LOCAL:
113119370Spst    case LOC_LOCAL_ARG:
113219370Spst    case LOC_BASEREG:
113319370Spst    case LOC_BASEREG_ARG:
113446283Sdfr    case LOC_THREAD_LOCAL_STATIC:
113519370Spst      return 1;
113619370Spst
113719370Spst    case LOC_UNDEF:
113819370Spst    case LOC_CONST:
113919370Spst    case LOC_STATIC:
114046283Sdfr    case LOC_INDIRECT:
114119370Spst    case LOC_TYPEDEF:
114219370Spst
114319370Spst    case LOC_LABEL:
114419370Spst      /* Getting the address of a label can be done independently of the block,
114519370Spst	 even if some *uses* of that address wouldn't work so well without
114619370Spst	 the right frame.  */
114719370Spst
114819370Spst    case LOC_BLOCK:
114919370Spst    case LOC_CONST_BYTES:
115019370Spst    case LOC_UNRESOLVED:
115119370Spst    case LOC_OPTIMIZED_OUT:
115219370Spst      return 0;
115319370Spst    }
115419370Spst  return 1;
115519370Spst}
115619370Spst
115719370Spst/* Given a struct symbol for a variable,
115819370Spst   and a stack frame id, read the value of the variable
115919370Spst   and return a (pointer to a) struct value containing the value.
116019370Spst   If the variable cannot be found, return a zero pointer.
116119370Spst   If FRAME is NULL, use the selected_frame.  */
116219370Spst
116319370Spstvalue_ptr
116419370Spstread_var_value (var, frame)
116519370Spst     register struct symbol *var;
116619370Spst     struct frame_info *frame;
116719370Spst{
116819370Spst  register value_ptr v;
116919370Spst  struct type *type = SYMBOL_TYPE (var);
117019370Spst  CORE_ADDR addr;
117119370Spst  register int len;
117219370Spst
117319370Spst  v = allocate_value (type);
117419370Spst  VALUE_LVAL (v) = lval_memory;	/* The most likely possibility.  */
117546283Sdfr  VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
117646283Sdfr
117719370Spst  len = TYPE_LENGTH (type);
117819370Spst
117919370Spst  if (frame == NULL) frame = selected_frame;
118019370Spst
118119370Spst  switch (SYMBOL_CLASS (var))
118219370Spst    {
118319370Spst    case LOC_CONST:
118419370Spst      /* Put the constant back in target format.  */
118519370Spst      store_signed_integer (VALUE_CONTENTS_RAW (v), len,
118619370Spst			    (LONGEST) SYMBOL_VALUE (var));
118719370Spst      VALUE_LVAL (v) = not_lval;
118819370Spst      return v;
118919370Spst
119019370Spst    case LOC_LABEL:
119119370Spst      /* Put the constant back in target format.  */
119246283Sdfr      if (overlay_debugging)
119346283Sdfr	store_address (VALUE_CONTENTS_RAW (v), len,
119446283Sdfr		       (LONGEST)symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
119546283Sdfr							   SYMBOL_BFD_SECTION (var)));
119646283Sdfr      else
119746283Sdfr	store_address (VALUE_CONTENTS_RAW (v), len,
119846283Sdfr		       (LONGEST)SYMBOL_VALUE_ADDRESS (var));
119919370Spst      VALUE_LVAL (v) = not_lval;
120019370Spst      return v;
120119370Spst
120219370Spst    case LOC_CONST_BYTES:
120319370Spst      {
120419370Spst	char *bytes_addr;
120519370Spst	bytes_addr = SYMBOL_VALUE_BYTES (var);
120619370Spst	memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
120719370Spst	VALUE_LVAL (v) = not_lval;
120819370Spst	return v;
120919370Spst      }
121019370Spst
121119370Spst    case LOC_STATIC:
121246283Sdfr      if (overlay_debugging)
121346283Sdfr	addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
121446283Sdfr					 SYMBOL_BFD_SECTION (var));
121546283Sdfr      else
121646283Sdfr	addr = SYMBOL_VALUE_ADDRESS (var);
121746283Sdfr      break;
121846283Sdfr
121946283Sdfr    case LOC_INDIRECT:
122046283Sdfr      /* The import slot does not have a real address in it from the
122146283Sdfr         dynamic loader (dld.sl on HP-UX), if the target hasn't begun
122246283Sdfr         execution yet, so check for that. */
122346283Sdfr      if (!target_has_execution)
122446283Sdfr        error ("\
122546283SdfrAttempt to access variable defined in different shared object or load module when\n\
122646283Sdfraddresses have not been bound by the dynamic loader. Try again when executable is running.");
122746283Sdfr
122819370Spst      addr = SYMBOL_VALUE_ADDRESS (var);
122946283Sdfr      addr = read_memory_unsigned_integer
123046283Sdfr	(addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
123119370Spst      break;
123219370Spst
123319370Spst    case LOC_ARG:
123419370Spst      if (frame == NULL)
123519370Spst	return 0;
123619370Spst      addr = FRAME_ARGS_ADDRESS (frame);
123719370Spst      if (!addr)
123819370Spst	return 0;
123919370Spst      addr += SYMBOL_VALUE (var);
124019370Spst      break;
124119370Spst
124219370Spst    case LOC_REF_ARG:
124319370Spst      if (frame == NULL)
124419370Spst	return 0;
124519370Spst      addr = FRAME_ARGS_ADDRESS (frame);
124619370Spst      if (!addr)
124719370Spst	return 0;
124819370Spst      addr += SYMBOL_VALUE (var);
124919370Spst      addr = read_memory_unsigned_integer
125019370Spst	(addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
125119370Spst      break;
125219370Spst
125319370Spst    case LOC_LOCAL:
125419370Spst    case LOC_LOCAL_ARG:
125519370Spst      if (frame == NULL)
125619370Spst	return 0;
125719370Spst      addr = FRAME_LOCALS_ADDRESS (frame);
125819370Spst      addr += SYMBOL_VALUE (var);
125919370Spst      break;
126019370Spst
126119370Spst    case LOC_BASEREG:
126219370Spst    case LOC_BASEREG_ARG:
126319370Spst      {
126419370Spst	char buf[MAX_REGISTER_RAW_SIZE];
126519370Spst	get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
126619370Spst			    NULL);
126719370Spst	addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
126819370Spst	addr += SYMBOL_VALUE (var);
126919370Spst	break;
127019370Spst      }
127119370Spst
127246283Sdfr    case LOC_THREAD_LOCAL_STATIC:
127346283Sdfr      {
127446283Sdfr        char buf[MAX_REGISTER_RAW_SIZE];
127546283Sdfr
127646283Sdfr        get_saved_register(buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
127746283Sdfr			    NULL);
127846283Sdfr        addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
127946283Sdfr        addr += SYMBOL_VALUE (var );
128046283Sdfr        break;
128146283Sdfr      }
128246283Sdfr
128319370Spst    case LOC_TYPEDEF:
128419370Spst      error ("Cannot look up value of a typedef");
128519370Spst      break;
128619370Spst
128719370Spst    case LOC_BLOCK:
128846283Sdfr      if (overlay_debugging)
128946283Sdfr	VALUE_ADDRESS (v) = symbol_overlayed_address
129046283Sdfr	  (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
129146283Sdfr      else
129246283Sdfr	VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
129319370Spst      return v;
129419370Spst
129519370Spst    case LOC_REGISTER:
129619370Spst    case LOC_REGPARM:
129719370Spst    case LOC_REGPARM_ADDR:
129819370Spst      {
129919370Spst	struct block *b;
130046283Sdfr	int regno = SYMBOL_VALUE (var);
130146283Sdfr	value_ptr regval;
130219370Spst
130319370Spst	if (frame == NULL)
130419370Spst	  return 0;
130519370Spst	b = get_frame_block (frame);
130619370Spst
130719370Spst	if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
130819370Spst	  {
130946283Sdfr	    regval = value_from_register (lookup_pointer_type (type),
131046283Sdfr					  regno,
131146283Sdfr					  frame);
131246283Sdfr
131346283Sdfr	    if (regval == NULL)
131446283Sdfr	      error ("Value of register variable not available.");
131546283Sdfr
131646283Sdfr	    addr   = value_as_pointer (regval);
131719370Spst	    VALUE_LVAL (v) = lval_memory;
131819370Spst	  }
131919370Spst	else
132046283Sdfr	  {
132146283Sdfr	    regval = value_from_register (type, regno, frame);
132246283Sdfr
132346283Sdfr	    if (regval == NULL)
132446283Sdfr	      error ("Value of register variable not available.");
132546283Sdfr	    return regval;
132646283Sdfr	  }
132719370Spst      }
132819370Spst      break;
132919370Spst
133019370Spst    case LOC_UNRESOLVED:
133119370Spst      {
133219370Spst	struct minimal_symbol *msym;
133319370Spst
133419370Spst	msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
133519370Spst	if (msym == NULL)
133619370Spst	  return 0;
133746283Sdfr	if (overlay_debugging)
133846283Sdfr	  addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
133946283Sdfr					   SYMBOL_BFD_SECTION (msym));
134046283Sdfr	else
134146283Sdfr	  addr = SYMBOL_VALUE_ADDRESS (msym);
134219370Spst      }
134319370Spst      break;
134419370Spst
134519370Spst    case LOC_OPTIMIZED_OUT:
134619370Spst      VALUE_LVAL (v) = not_lval;
134719370Spst      VALUE_OPTIMIZED_OUT (v) = 1;
134819370Spst      return v;
134919370Spst
135019370Spst    default:
135119370Spst      error ("Cannot look up value of a botched symbol.");
135219370Spst      break;
135319370Spst    }
135419370Spst
135519370Spst  VALUE_ADDRESS (v) = addr;
135619370Spst  VALUE_LAZY (v) = 1;
135719370Spst  return v;
135819370Spst}
135919370Spst
136019370Spst/* Return a value of type TYPE, stored in register REGNUM, in frame
136146283Sdfr   FRAME.
136219370Spst
136346283Sdfr   NOTE: returns NULL if register value is not available.
136446283Sdfr   Caller will check return value or die!  */
136546283Sdfr
136619370Spstvalue_ptr
136719370Spstvalue_from_register (type, regnum, frame)
136819370Spst     struct type *type;
136919370Spst     int regnum;
137019370Spst     struct frame_info *frame;
137119370Spst{
137219370Spst  char raw_buffer [MAX_REGISTER_RAW_SIZE];
137319370Spst  CORE_ADDR addr;
137419370Spst  int optim;
137519370Spst  value_ptr v = allocate_value (type);
137619370Spst  char *value_bytes = 0;
137719370Spst  int value_bytes_copied = 0;
137819370Spst  int num_storage_locs;
137919370Spst  enum lval_type lval;
138019370Spst  int len;
138119370Spst
138219370Spst  CHECK_TYPEDEF (type);
138319370Spst  len = TYPE_LENGTH (type);
138419370Spst
138519370Spst  VALUE_REGNO (v) = regnum;
138619370Spst
138719370Spst  num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
138819370Spst		      ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
138919370Spst		      1);
139019370Spst
139119370Spst  if (num_storage_locs > 1
139219370Spst#ifdef GDB_TARGET_IS_H8500
139319370Spst      || TYPE_CODE (type) == TYPE_CODE_PTR
139419370Spst#endif
139519370Spst      )
139619370Spst    {
139719370Spst      /* Value spread across multiple storage locations.  */
139819370Spst
139919370Spst      int local_regnum;
140019370Spst      int mem_stor = 0, reg_stor = 0;
140119370Spst      int mem_tracking = 1;
140219370Spst      CORE_ADDR last_addr = 0;
140319370Spst      CORE_ADDR first_addr = 0;
140419370Spst
140519370Spst      value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
140619370Spst
140719370Spst      /* Copy all of the data out, whereever it may be.  */
140819370Spst
140919370Spst#ifdef GDB_TARGET_IS_H8500
141019370Spst/* This piece of hideosity is required because the H8500 treats registers
141119370Spst   differently depending upon whether they are used as pointers or not.  As a
141219370Spst   pointer, a register needs to have a page register tacked onto the front.
141319370Spst   An alternate way to do this would be to have gcc output different register
141419370Spst   numbers for the pointer & non-pointer form of the register.  But, it
141519370Spst   doesn't, so we're stuck with this.  */
141619370Spst
141719370Spst      if (TYPE_CODE (type) == TYPE_CODE_PTR
141819370Spst	  && len > 2)
141919370Spst	{
142019370Spst	  int page_regnum;
142119370Spst
142219370Spst	  switch (regnum)
142319370Spst	    {
142419370Spst	    case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
142519370Spst	      page_regnum = SEG_D_REGNUM;
142619370Spst	      break;
142719370Spst	    case R4_REGNUM: case R5_REGNUM:
142819370Spst	      page_regnum = SEG_E_REGNUM;
142919370Spst	      break;
143019370Spst	    case R6_REGNUM: case R7_REGNUM:
143119370Spst	      page_regnum = SEG_T_REGNUM;
143219370Spst	      break;
143319370Spst	    }
143419370Spst
143519370Spst	  value_bytes[0] = 0;
143619370Spst	  get_saved_register (value_bytes + 1,
143719370Spst			      &optim,
143819370Spst			      &addr,
143919370Spst			      frame,
144019370Spst			      page_regnum,
144119370Spst			      &lval);
144219370Spst
144346283Sdfr	  if (register_valid[page_regnum] == -1)
144446283Sdfr	    return NULL;	/* register value not available */
144546283Sdfr
144619370Spst	  if (lval == lval_register)
144719370Spst	    reg_stor++;
144819370Spst	  else
144919370Spst	    mem_stor++;
145019370Spst	  first_addr = addr;
145119370Spst	  last_addr = addr;
145219370Spst
145319370Spst	  get_saved_register (value_bytes + 2,
145419370Spst			      &optim,
145519370Spst			      &addr,
145619370Spst			      frame,
145719370Spst			      regnum,
145819370Spst			      &lval);
145919370Spst
146046283Sdfr	  if (register_valid[regnum] == -1)
146146283Sdfr	    return NULL;	/* register value not available */
146246283Sdfr
146319370Spst	  if (lval == lval_register)
146419370Spst	    reg_stor++;
146519370Spst	  else
146619370Spst	    {
146719370Spst	      mem_stor++;
146819370Spst	      mem_tracking = mem_tracking && (addr == last_addr);
146919370Spst	    }
147019370Spst	  last_addr = addr;
147119370Spst	}
147219370Spst      else
147319370Spst#endif				/* GDB_TARGET_IS_H8500 */
147419370Spst	for (local_regnum = regnum;
147519370Spst	     value_bytes_copied < len;
147619370Spst	     (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
147719370Spst	      ++local_regnum))
147819370Spst	  {
147919370Spst	    get_saved_register (value_bytes + value_bytes_copied,
148019370Spst				&optim,
148119370Spst				&addr,
148219370Spst				frame,
148319370Spst				local_regnum,
148419370Spst				&lval);
148519370Spst
148646283Sdfr	  if (register_valid[local_regnum] == -1)
148746283Sdfr	    return NULL;	/* register value not available */
148846283Sdfr
148919370Spst	    if (regnum == local_regnum)
149019370Spst	      first_addr = addr;
149119370Spst	    if (lval == lval_register)
149219370Spst	      reg_stor++;
149319370Spst	    else
149419370Spst	      {
149519370Spst		mem_stor++;
149619370Spst
149719370Spst		mem_tracking =
149819370Spst		  (mem_tracking
149919370Spst		   && (regnum == local_regnum
150019370Spst		       || addr == last_addr));
150119370Spst	      }
150219370Spst	    last_addr = addr;
150319370Spst	  }
150419370Spst
150519370Spst      if ((reg_stor && mem_stor)
150619370Spst	  || (mem_stor && !mem_tracking))
150719370Spst	/* Mixed storage; all of the hassle we just went through was
150819370Spst	   for some good purpose.  */
150919370Spst	{
151019370Spst	  VALUE_LVAL (v) = lval_reg_frame_relative;
151119370Spst	  VALUE_FRAME (v) = FRAME_FP (frame);
151219370Spst	  VALUE_FRAME_REGNUM (v) = regnum;
151319370Spst	}
151419370Spst      else if (mem_stor)
151519370Spst	{
151619370Spst	  VALUE_LVAL (v) = lval_memory;
151719370Spst	  VALUE_ADDRESS (v) = first_addr;
151819370Spst	}
151919370Spst      else if (reg_stor)
152019370Spst	{
152119370Spst	  VALUE_LVAL (v) = lval_register;
152219370Spst	  VALUE_ADDRESS (v) = first_addr;
152319370Spst	}
152419370Spst      else
152519370Spst	fatal ("value_from_register: Value not stored anywhere!");
152619370Spst
152719370Spst      VALUE_OPTIMIZED_OUT (v) = optim;
152819370Spst
152919370Spst      /* Any structure stored in more than one register will always be
153019370Spst	 an integral number of registers.  Otherwise, you'd need to do
153119370Spst	 some fiddling with the last register copied here for little
153219370Spst	 endian machines.  */
153319370Spst
153419370Spst      /* Copy into the contents section of the value.  */
153519370Spst      memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
153619370Spst
153719370Spst      /* Finally do any conversion necessary when extracting this
153819370Spst         type from more than one register.  */
153919370Spst#ifdef REGISTER_CONVERT_TO_TYPE
154019370Spst      REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
154119370Spst#endif
154219370Spst      return v;
154319370Spst    }
154419370Spst
154519370Spst  /* Data is completely contained within a single register.  Locate the
154619370Spst     register's contents in a real register or in core;
154719370Spst     read the data in raw format.  */
154819370Spst
154919370Spst  get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
155046283Sdfr
155146283Sdfr  if (register_valid[regnum] == -1)
155246283Sdfr    return NULL;	/* register value not available */
155346283Sdfr
155419370Spst  VALUE_OPTIMIZED_OUT (v) = optim;
155519370Spst  VALUE_LVAL (v) = lval;
155619370Spst  VALUE_ADDRESS (v) = addr;
155719370Spst
155819370Spst  /* Convert raw data to virtual format if necessary.  */
155919370Spst
156019370Spst#ifdef REGISTER_CONVERTIBLE
156119370Spst  if (REGISTER_CONVERTIBLE (regnum))
156219370Spst    {
156319370Spst      REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
156419370Spst				   raw_buffer, VALUE_CONTENTS_RAW (v));
156519370Spst    }
156619370Spst  else
156719370Spst#endif
156819370Spst    {
156919370Spst      /* Raw and virtual formats are the same for this register.  */
157019370Spst
157119370Spst      if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
157219370Spst	{
157319370Spst  	  /* Big-endian, and we want less than full size.  */
157419370Spst	  VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
157519370Spst	}
157619370Spst
157719370Spst      memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
157819370Spst    }
157919370Spst
158019370Spst  return v;
158119370Spst}
158219370Spst
158319370Spst/* Given a struct symbol for a variable or function,
158419370Spst   and a stack frame id,
158519370Spst   return a (pointer to a) struct value containing the properly typed
158619370Spst   address.  */
158719370Spst
158819370Spstvalue_ptr
158919370Spstlocate_var_value (var, frame)
159019370Spst     register struct symbol *var;
159119370Spst     struct frame_info *frame;
159219370Spst{
159319370Spst  CORE_ADDR addr = 0;
159419370Spst  struct type *type = SYMBOL_TYPE (var);
159519370Spst  value_ptr lazy_value;
159619370Spst
159719370Spst  /* Evaluate it first; if the result is a memory address, we're fine.
159819370Spst     Lazy evaluation pays off here. */
159919370Spst
160019370Spst  lazy_value = read_var_value (var, frame);
160119370Spst  if (lazy_value == 0)
160219370Spst    error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
160319370Spst
160419370Spst  if (VALUE_LAZY (lazy_value)
160519370Spst      || TYPE_CODE (type) == TYPE_CODE_FUNC)
160619370Spst    {
160746283Sdfr      value_ptr val;
160846283Sdfr
160919370Spst      addr = VALUE_ADDRESS (lazy_value);
161046283Sdfr      val =  value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
161146283Sdfr      VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
161246283Sdfr      return val;
161319370Spst    }
161419370Spst
161519370Spst  /* Not a memory address; check what the problem was.  */
161619370Spst  switch (VALUE_LVAL (lazy_value))
161719370Spst    {
161819370Spst    case lval_register:
161919370Spst    case lval_reg_frame_relative:
162019370Spst      error ("Address requested for identifier \"%s\" which is in a register.",
162119370Spst	     SYMBOL_SOURCE_NAME (var));
162219370Spst      break;
162319370Spst
162419370Spst    default:
162519370Spst      error ("Can't take address of \"%s\" which isn't an lvalue.",
162619370Spst	     SYMBOL_SOURCE_NAME (var));
162719370Spst      break;
162819370Spst    }
162919370Spst  return 0;  /* For lint -- never reached */
163019370Spst}
1631