/* Cache and manage the values of registers for GDB, the GNU debugger.
- Copyright 1986, 87, 89, 91, 94, 95, 96, 1998, 2000, 2001
+ Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001
Free Software Foundation, Inc.
This file is part of GDB.
Boston, MA 02111-1307, USA. */
#include "defs.h"
-#include "frame.h"
#include "inferior.h"
#include "target.h"
#include "gdbarch.h"
#include "gdbcmd.h"
+#include "regcache.h"
+#include "gdb_assert.h"
/*
* DATA STRUCTURE
STORE_PSEUDO_REGISTER (regnum);
}
-/* FIND_SAVED_REGISTER ()
-
- Return the address in which frame FRAME's value of register REGNUM
- has been saved in memory. Or return zero if it has not been saved.
- If REGNUM specifies the SP, the value we return is actually
- the SP value, not an address where it was saved. */
-
-CORE_ADDR
-find_saved_register (struct frame_info *frame, int regnum)
-{
- register struct frame_info *frame1 = NULL;
- register CORE_ADDR addr = 0;
-
- if (frame == NULL) /* No regs saved if want current frame */
- return 0;
-
-#ifdef HAVE_REGISTER_WINDOWS
- /* We assume that a register in a register window will only be saved
- in one place (since the name changes and/or disappears as you go
- towards inner frames), so we only call get_frame_saved_regs on
- the current frame. This is directly in contradiction to the
- usage below, which assumes that registers used in a frame must be
- saved in a lower (more interior) frame. This change is a result
- of working on a register window machine; get_frame_saved_regs
- always returns the registers saved within a frame, within the
- context (register namespace) of that frame. */
-
- /* However, note that we don't want this to return anything if
- nothing is saved (if there's a frame inside of this one). Also,
- callers to this routine asking for the stack pointer want the
- stack pointer saved for *this* frame; this is returned from the
- next frame. */
-
- if (REGISTER_IN_WINDOW_P (regnum))
- {
- frame1 = get_next_frame (frame);
- if (!frame1)
- return 0; /* Registers of this frame are active. */
-
- /* Get the SP from the next frame in; it will be this
- current frame. */
- if (regnum != SP_REGNUM)
- frame1 = frame;
-
- FRAME_INIT_SAVED_REGS (frame1);
- return frame1->saved_regs[regnum]; /* ... which might be zero */
- }
-#endif /* HAVE_REGISTER_WINDOWS */
-
- /* Note that this next routine assumes that registers used in
- frame x will be saved only in the frame that x calls and
- frames interior to it. This is not true on the sparc, but the
- above macro takes care of it, so we should be all right. */
- while (1)
- {
- QUIT;
- frame1 = get_prev_frame (frame1);
- if (frame1 == 0 || frame1 == frame)
- break;
- FRAME_INIT_SAVED_REGS (frame1);
- if (frame1->saved_regs[regnum])
- addr = frame1->saved_regs[regnum];
- }
-
- return addr;
-}
-
-/* DEFAULT_GET_SAVED_REGISTER ()
-
- Find register number REGNUM relative to FRAME and put its (raw,
- target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
- variable was optimized out (and thus can't be fetched). Set *LVAL
- to lval_memory, lval_register, or not_lval, depending on whether
- the value was fetched from memory, from a register, or in a strange
- and non-modifiable way (e.g. a frame pointer which was calculated
- rather than fetched). Set *ADDRP to the address, either in memory
- on as a REGISTER_BYTE offset into the registers array.
-
- Note that this implementation never sets *LVAL to not_lval. But
- it can be replaced by defining GET_SAVED_REGISTER and supplying
- your own.
-
- The argument RAW_BUFFER must point to aligned memory. */
-
-static void
-default_get_saved_register (char *raw_buffer,
- int *optimized,
- CORE_ADDR *addrp,
- struct frame_info *frame,
- int regnum,
- enum lval_type *lval)
-{
- CORE_ADDR addr;
-
- if (!target_has_registers)
- error ("No registers.");
-
- /* Normal systems don't optimize out things with register numbers. */
- if (optimized != NULL)
- *optimized = 0;
- addr = find_saved_register (frame, regnum);
- if (addr != 0)
- {
- if (lval != NULL)
- *lval = lval_memory;
- if (regnum == SP_REGNUM)
- {
- if (raw_buffer != NULL)
- {
- /* Put it back in target format. */
- store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
- (LONGEST) addr);
- }
- if (addrp != NULL)
- *addrp = 0;
- return;
- }
- if (raw_buffer != NULL)
- target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
- }
- else
- {
- if (lval != NULL)
- *lval = lval_register;
- addr = REGISTER_BYTE (regnum);
- if (raw_buffer != NULL)
- read_register_gen (regnum, raw_buffer);
- }
- if (addrp != NULL)
- *addrp = addr;
-}
-
-#if !defined (GET_SAVED_REGISTER)
-#define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
- default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
-#endif
-
-void
-get_saved_register (char *raw_buffer,
- int *optimized,
- CORE_ADDR *addrp,
- struct frame_info *frame,
- int regnum,
- enum lval_type *lval)
-{
- GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
-}
-
-/* READ_RELATIVE_REGISTER_RAW_BYTES_FOR_FRAME
-
- Copy the bytes of register REGNUM, relative to the input stack frame,
- into our memory at MYADDR, in target byte order.
- The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
-
- Returns 1 if could not be read, 0 if could. */
-
-/* FIXME: This function increases the confusion between FP_REGNUM
- and the virtual/pseudo-frame pointer. */
-
-static int
-read_relative_register_raw_bytes_for_frame (int regnum,
- char *myaddr,
- struct frame_info *frame)
-{
- int optim;
- if (regnum == FP_REGNUM && frame)
- {
- /* Put it back in target format. */
- store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
- (LONGEST) FRAME_FP (frame));
-
- return 0;
- }
-
- get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
- regnum, (enum lval_type *) NULL);
-
- if (register_cached (regnum) < 0)
- return 1; /* register value not available */
-
- return optim;
-}
-
-/* READ_RELATIVE_REGISTER_RAW_BYTES
-
- Copy the bytes of register REGNUM, relative to the current stack
- frame, into our memory at MYADDR, in target byte order.
- The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
-
- Returns 1 if could not be read, 0 if could. */
-
-int
-read_relative_register_raw_bytes (int regnum, char *myaddr)
-{
- return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
- selected_frame);
-}
-
-
/* Low level examining and depositing of registers.
The caller is responsible for making sure that the inferior is
gdb gives control to the user (ie watchpoints). */
alloca (0);
- for (i = 0; i < ARCH_NUM_REGS; i++)
+ for (i = 0; i < NUM_REGS; i++)
set_register_cached (i, 0);
/* Assume that if all the hardware regs have changed,
{
int i;
- for (i = 0; i < ARCH_NUM_REGS; i++)
+ for (i = 0; i < NUM_REGS; i++)
set_register_cached (i, 1);
/* Do not assume that the pseudo-regs have also been fetched.
Fetching all real regs might not account for all pseudo-regs. */
into memory at MYADDR. */
void
-read_register_bytes (int inregbyte, char *myaddr, int inlen)
+read_register_bytes (int in_start, char *in_buf, int in_len)
{
- int inregend = inregbyte + inlen;
+ int in_end = in_start + in_len;
int regnum;
-
- if (registers_pid != inferior_pid)
- {
- registers_changed ();
- registers_pid = inferior_pid;
- }
+ char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
/* See if we are trying to read bytes from out-of-date registers. If so,
update just those registers. */
for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
{
- int regstart, regend;
-
- if (register_cached (regnum))
- continue;
+ int reg_start;
+ int reg_end;
+ int reg_len;
+ int start;
+ int end;
+ int byte;
if (REGISTER_NAME (regnum) == NULL || *REGISTER_NAME (regnum) == '\0')
continue;
- regstart = REGISTER_BYTE (regnum);
- regend = regstart + REGISTER_RAW_SIZE (regnum);
+ reg_start = REGISTER_BYTE (regnum);
+ reg_len = REGISTER_RAW_SIZE (regnum);
+ reg_end = reg_start + reg_len;
- if (regend <= inregbyte || inregend <= regstart)
+ if (reg_end <= in_start || in_end <= reg_start)
/* The range the user wants to read doesn't overlap with regnum. */
continue;
- /* We've found an uncached register where at least one byte will be read.
- Update it from the target. */
- fetch_register (regnum);
+ /* Force the cache to fetch the entire register. */
+ read_register_gen (regnum, reg_buf);
- if (!register_cached (regnum))
+ /* Legacy note: This function, for some reason, allows a NULL
+ input buffer. If the buffer is NULL, the registers are still
+ fetched, just the final transfer is skipped. */
+ if (in_buf == NULL)
+ continue;
+
+ /* start = max (reg_start, in_start) */
+ if (reg_start > in_start)
+ start = reg_start;
+ else
+ start = in_start;
+
+ /* end = min (reg_end, in_end) */
+ if (reg_end < in_end)
+ end = reg_end;
+ else
+ end = in_end;
+
+ /* Transfer just the bytes common to both IN_BUF and REG_BUF */
+ for (byte = start; byte < end; byte++)
{
- /* Sometimes pseudoregs are never marked valid, so that they
- will be fetched every time (it can be complicated to know
- if a pseudoreg is valid, while "fetching" them can be cheap).
- */
- if (regnum < NUM_REGS)
- error ("read_register_bytes: Couldn't update register %d.", regnum);
+ in_buf[byte - in_start] = reg_buf[byte - reg_start];
}
}
-
- if (myaddr != NULL)
- memcpy (myaddr, register_buffer (-1) + inregbyte, inlen);
}
/* Read register REGNUM into memory at MYADDR, which must be large
register is known to be the size of a CORE_ADDR or smaller,
read_register can be used instead. */
-void
-read_register_gen (int regnum, char *myaddr)
+static void
+legacy_read_register_gen (int regnum, char *myaddr)
{
+ gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
if (registers_pid != inferior_pid)
{
registers_changed ();
REGISTER_RAW_SIZE (regnum));
}
+void
+regcache_read (int rawnum, char *buf)
+{
+ gdb_assert (rawnum >= 0 && rawnum < NUM_REGS);
+ /* For moment, just use underlying legacy code. Ulgh!!! */
+ legacy_read_register_gen (rawnum, buf);
+}
+
+void
+read_register_gen (int regnum, char *buf)
+{
+ if (! gdbarch_register_read_p (current_gdbarch))
+ {
+ legacy_read_register_gen (regnum, buf);
+ return;
+ }
+ gdbarch_register_read (current_gdbarch, regnum, buf);
+}
+
+
/* Write register REGNUM at MYADDR to the target. MYADDR points at
REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
#define CANNOT_STORE_REGISTER(regnum) 0
#endif
-void
-write_register_gen (int regnum, char *myaddr)
+static void
+legacy_write_register_gen (int regnum, char *myaddr)
{
int size;
+ gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
/* On the sparc, writing %g0 is a no-op, so we don't even want to
change the registers array if something writes to this register. */
store_register (regnum);
}
+void
+regcache_write (int rawnum, char *buf)
+{
+ gdb_assert (rawnum >= 0 && rawnum < NUM_REGS);
+ /* For moment, just use underlying legacy code. Ulgh!!! */
+ legacy_write_register_gen (rawnum, buf);
+}
+
+void
+write_register_gen (int regnum, char *buf)
+{
+ if (! gdbarch_register_write_p (current_gdbarch))
+ {
+ legacy_write_register_gen (regnum, buf);
+ return;
+ }
+ gdbarch_register_write (current_gdbarch, regnum, buf);
+}
+
/* Copy INLEN bytes of consecutive data from memory at MYADDR
into registers starting with the MYREGSTART'th byte of register data. */
ULONGEST
read_register (int regnum)
{
- if (registers_pid != inferior_pid)
- {
- registers_changed ();
- registers_pid = inferior_pid;
- }
-
- if (!register_cached (regnum))
- fetch_register (regnum);
-
- return (extract_unsigned_integer (register_buffer (regnum),
- REGISTER_RAW_SIZE (regnum)));
+ char *buf = alloca (REGISTER_RAW_SIZE (regnum));
+ read_register_gen (regnum, buf);
+ return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
}
ULONGEST
LONGEST
read_signed_register (int regnum)
{
- if (registers_pid != inferior_pid)
- {
- registers_changed ();
- registers_pid = inferior_pid;
- }
-
- if (!register_cached (regnum))
- fetch_register (regnum);
-
- return (extract_signed_integer (register_buffer (regnum),
- REGISTER_RAW_SIZE (regnum)));
+ void *buf = alloca (REGISTER_RAW_SIZE (regnum));
+ read_register_gen (regnum, buf);
+ return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
}
LONGEST
void
write_register (int regnum, LONGEST val)
{
- PTR buf;
+ void *buf;
int size;
-
- /* On the sparc, writing %g0 is a no-op, so we don't even want to
- change the registers array if something writes to this register. */
- if (CANNOT_STORE_REGISTER (regnum))
- return;
-
- if (registers_pid != inferior_pid)
- {
- registers_changed ();
- registers_pid = inferior_pid;
- }
-
size = REGISTER_RAW_SIZE (regnum);
buf = alloca (size);
store_signed_integer (buf, size, (LONGEST) val);
-
- /* If we have a valid copy of the register, and new value == old value,
- then don't bother doing the actual store. */
-
- if (register_cached (regnum)
- && memcmp (register_buffer (regnum), buf, size) == 0)
- return;
-
- if (real_register (regnum))
- target_prepare_to_store ();
-
- memcpy (register_buffer (regnum), buf, size);
-
- set_register_cached (regnum, 1);
- store_register (regnum);
+ write_register_gen (regnum, buf);
}
void
/* On some architectures, e.g. HPPA, there are a few stray bits in
some registers, that the rest of the code would like to ignore. */
+ /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
+ going to be deprecated. Instead architectures will leave the raw
+ register value as is and instead clean things up as they pass
+ through the method gdbarch_register_read() clean up the
+ values. */
+
#ifdef CLEAN_UP_REGISTER_VALUE
CLEAN_UP_REGISTER_VALUE (regnum, register_buffer (regnum));
#endif
/* read_pc, write_pc, read_sp, write_sp, read_fp, write_fp, etc.
Special handling for registers PC, SP, and FP. */
+/* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
+ read_pc_pid(), read_pc(), generic_target_write_pc(),
+ write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
+ generic_target_write_sp(), write_sp(), generic_target_read_fp(),
+ read_fp(), generic_target_write_fp(), write_fp will eventually be
+ moved out of the reg-cache into either frame.[hc] or to the
+ multi-arch framework. The are not part of the raw register cache. */
+
/* This routine is getting awfully cluttered with #if's. It's probably
time to turn this into READ_PC and define it in the tm.h file.
Ditto for write_pc.