1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 87, 89, 91, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 #include "gdb_string.h"
31 #include "floatformat.h"
32 #include "symfile.h" /* for overlay functions */
34 /* This is used to indicate that we don't know the format of the floating point
35 number. Typically, this is useful for native ports, where the actual format
36 is irrelevant, since no conversions will be taking place. */
38 const struct floatformat floatformat_unknown;
40 /* Registers we shouldn't try to store. */
41 #if !defined (CANNOT_STORE_REGISTER)
42 #define CANNOT_STORE_REGISTER(regno) 0
45 static void write_register_gen PARAMS ((int, char *));
47 static int read_relative_register_raw_bytes_for_frame PARAMS ((int regnum, char *myaddr, struct frame_info * frame));
49 /* Basic byte-swapping routines. GDB has needed these for a long time...
50 All extract a target-format integer at ADDR which is LEN bytes long. */
52 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
53 /* 8 bit characters are a pretty safe assumption these days, so we
54 assume it throughout all these swapping routines. If we had to deal with
55 9 bit characters, we would need to make len be in bits and would have
56 to re-write these routines... */
61 extract_signed_integer (void *addr, int len)
65 unsigned char *startaddr = (unsigned char *) addr;
66 unsigned char *endaddr = startaddr + len;
68 if (len > (int) sizeof (LONGEST))
70 That operation is not available on integers of more than %d bytes.",
73 /* Start at the most significant end of the integer, and work towards
74 the least significant. */
75 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
78 /* Do the sign extension once at the start. */
79 retval = ((LONGEST) * p ^ 0x80) - 0x80;
80 for (++p; p < endaddr; ++p)
81 retval = (retval << 8) | *p;
86 /* Do the sign extension once at the start. */
87 retval = ((LONGEST) * p ^ 0x80) - 0x80;
88 for (--p; p >= startaddr; --p)
89 retval = (retval << 8) | *p;
95 extract_unsigned_integer (void *addr, int len)
99 unsigned char *startaddr = (unsigned char *) addr;
100 unsigned char *endaddr = startaddr + len;
102 if (len > (int) sizeof (ULONGEST))
104 That operation is not available on integers of more than %d bytes.",
107 /* Start at the most significant end of the integer, and work towards
108 the least significant. */
110 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
112 for (p = startaddr; p < endaddr; ++p)
113 retval = (retval << 8) | *p;
117 for (p = endaddr - 1; p >= startaddr; --p)
118 retval = (retval << 8) | *p;
123 /* Sometimes a long long unsigned integer can be extracted as a
124 LONGEST value. This is done so that we can print these values
125 better. If this integer can be converted to a LONGEST, this
126 function returns 1 and sets *PVAL. Otherwise it returns 0. */
129 extract_long_unsigned_integer (void *addr, int orig_len, LONGEST *pval)
131 char *p, *first_addr;
135 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
137 for (p = (char *) addr;
138 len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
150 first_addr = (char *) addr;
151 for (p = (char *) addr + orig_len - 1;
152 len > (int) sizeof (LONGEST) && p >= (char *) addr;
162 if (len <= (int) sizeof (LONGEST))
164 *pval = (LONGEST) extract_unsigned_integer (first_addr,
173 /* Treat the LEN bytes at ADDR as a target-format address, and return
174 that address. ADDR is a buffer in the GDB process, not in the
177 This function should only be used by target-specific code. It
178 assumes that a pointer has the same representation as that thing's
179 address represented as an integer. Some machines use word
180 addresses, or similarly munged things, for certain types of
181 pointers, so that assumption doesn't hold everywhere.
183 Common code should use extract_typed_address instead, or something
184 else based on POINTER_TO_ADDRESS. */
187 extract_address (void *addr, int len)
189 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
190 whether we want this to be true eventually. */
191 return (CORE_ADDR) extract_unsigned_integer (addr, len);
195 #ifndef POINTER_TO_ADDRESS
196 #define POINTER_TO_ADDRESS generic_pointer_to_address
199 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
200 address it represents. */
202 extract_typed_address (void *buf, struct type *type)
204 if (TYPE_CODE (type) != TYPE_CODE_PTR
205 && TYPE_CODE (type) != TYPE_CODE_REF)
206 internal_error ("findvar.c (extract_typed_address): "
207 "type is not a pointer or reference");
209 return POINTER_TO_ADDRESS (type, buf);
214 store_signed_integer (void *addr, int len, LONGEST val)
217 unsigned char *startaddr = (unsigned char *) addr;
218 unsigned char *endaddr = startaddr + len;
220 /* Start at the least significant end of the integer, and work towards
221 the most significant. */
222 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
224 for (p = endaddr - 1; p >= startaddr; --p)
232 for (p = startaddr; p < endaddr; ++p)
241 store_unsigned_integer (void *addr, int len, ULONGEST val)
244 unsigned char *startaddr = (unsigned char *) addr;
245 unsigned char *endaddr = startaddr + len;
247 /* Start at the least significant end of the integer, and work towards
248 the most significant. */
249 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
251 for (p = endaddr - 1; p >= startaddr; --p)
259 for (p = startaddr; p < endaddr; ++p)
267 /* Store the address VAL as a LEN-byte value in target byte order at
268 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
270 This function should only be used by target-specific code. It
271 assumes that a pointer has the same representation as that thing's
272 address represented as an integer. Some machines use word
273 addresses, or similarly munged things, for certain types of
274 pointers, so that assumption doesn't hold everywhere.
276 Common code should use store_typed_address instead, or something else
277 based on ADDRESS_TO_POINTER. */
279 store_address (void *addr, int len, LONGEST val)
281 store_unsigned_integer (addr, len, val);
285 #ifndef ADDRESS_TO_POINTER
286 #define ADDRESS_TO_POINTER generic_address_to_pointer
289 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
292 store_typed_address (void *buf, struct type *type, CORE_ADDR addr)
294 if (TYPE_CODE (type) != TYPE_CODE_PTR
295 && TYPE_CODE (type) != TYPE_CODE_REF)
296 internal_error ("findvar.c (store_typed_address): "
297 "type is not a pointer or reference");
299 ADDRESS_TO_POINTER (type, buf, addr);
305 /* Extract a floating-point number from a target-order byte-stream at ADDR.
306 Returns the value as type DOUBLEST.
308 If the host and target formats agree, we just copy the raw data into the
309 appropriate type of variable and return, letting the host increase precision
310 as necessary. Otherwise, we call the conversion routine and let it do the
314 extract_floating (void *addr, int len)
318 if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
320 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
324 memcpy (&retval, addr, sizeof (retval));
328 floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
330 else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
332 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
336 memcpy (&retval, addr, sizeof (retval));
340 floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
342 else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
344 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
348 memcpy (&retval, addr, sizeof (retval));
352 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
356 error ("Can't deal with a floating point number of %d bytes.", len);
363 store_floating (void *addr, int len, DOUBLEST val)
365 if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
367 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
369 float floatval = val;
371 memcpy (addr, &floatval, sizeof (floatval));
374 floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
376 else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
378 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
380 double doubleval = val;
382 memcpy (addr, &doubleval, sizeof (doubleval));
385 floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
387 else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
389 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
390 memcpy (addr, &val, sizeof (val));
392 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
396 error ("Can't deal with a floating point number of %d bytes.", len);
401 /* Return the address in which frame FRAME's value of register REGNUM
402 has been saved in memory. Or return zero if it has not been saved.
403 If REGNUM specifies the SP, the value we return is actually
404 the SP value, not an address where it was saved. */
407 find_saved_register (frame, regnum)
408 struct frame_info *frame;
411 register struct frame_info *frame1 = NULL;
412 register CORE_ADDR addr = 0;
414 if (frame == NULL) /* No regs saved if want current frame */
417 #ifdef HAVE_REGISTER_WINDOWS
418 /* We assume that a register in a register window will only be saved
419 in one place (since the name changes and/or disappears as you go
420 towards inner frames), so we only call get_frame_saved_regs on
421 the current frame. This is directly in contradiction to the
422 usage below, which assumes that registers used in a frame must be
423 saved in a lower (more interior) frame. This change is a result
424 of working on a register window machine; get_frame_saved_regs
425 always returns the registers saved within a frame, within the
426 context (register namespace) of that frame. */
428 /* However, note that we don't want this to return anything if
429 nothing is saved (if there's a frame inside of this one). Also,
430 callers to this routine asking for the stack pointer want the
431 stack pointer saved for *this* frame; this is returned from the
434 if (REGISTER_IN_WINDOW_P (regnum))
436 frame1 = get_next_frame (frame);
438 return 0; /* Registers of this frame are active. */
440 /* Get the SP from the next frame in; it will be this
442 if (regnum != SP_REGNUM)
445 FRAME_INIT_SAVED_REGS (frame1);
446 return frame1->saved_regs[regnum]; /* ... which might be zero */
448 #endif /* HAVE_REGISTER_WINDOWS */
450 /* Note that this next routine assumes that registers used in
451 frame x will be saved only in the frame that x calls and
452 frames interior to it. This is not true on the sparc, but the
453 above macro takes care of it, so we should be all right. */
457 frame1 = get_prev_frame (frame1);
458 if (frame1 == 0 || frame1 == frame)
460 FRAME_INIT_SAVED_REGS (frame1);
461 if (frame1->saved_regs[regnum])
462 addr = frame1->saved_regs[regnum];
468 /* Find register number REGNUM relative to FRAME and put its (raw,
469 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
470 variable was optimized out (and thus can't be fetched). Set *LVAL
471 to lval_memory, lval_register, or not_lval, depending on whether
472 the value was fetched from memory, from a register, or in a strange
473 and non-modifiable way (e.g. a frame pointer which was calculated
474 rather than fetched). Set *ADDRP to the address, either in memory
475 on as a REGISTER_BYTE offset into the registers array.
477 Note that this implementation never sets *LVAL to not_lval. But
478 it can be replaced by defining GET_SAVED_REGISTER and supplying
481 The argument RAW_BUFFER must point to aligned memory. */
484 default_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
488 struct frame_info *frame;
490 enum lval_type *lval;
494 if (!target_has_registers)
495 error ("No registers.");
497 /* Normal systems don't optimize out things with register numbers. */
498 if (optimized != NULL)
500 addr = find_saved_register (frame, regnum);
505 if (regnum == SP_REGNUM)
507 if (raw_buffer != NULL)
509 /* Put it back in target format. */
510 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
517 if (raw_buffer != NULL)
518 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
523 *lval = lval_register;
524 addr = REGISTER_BYTE (regnum);
525 if (raw_buffer != NULL)
526 read_register_gen (regnum, raw_buffer);
532 #if !defined (GET_SAVED_REGISTER)
533 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
534 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
537 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
541 struct frame_info *frame;
543 enum lval_type *lval;
545 GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
548 /* Copy the bytes of register REGNUM, relative to the input stack frame,
549 into our memory at MYADDR, in target byte order.
550 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
552 Returns 1 if could not be read, 0 if could. */
555 read_relative_register_raw_bytes_for_frame (regnum, myaddr, frame)
558 struct frame_info *frame;
561 if (regnum == FP_REGNUM && frame)
563 /* Put it back in target format. */
564 store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
565 (LONGEST) FRAME_FP (frame));
570 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
571 regnum, (enum lval_type *) NULL);
573 if (register_valid[regnum] < 0)
574 return 1; /* register value not available */
579 /* Copy the bytes of register REGNUM, relative to the current stack frame,
580 into our memory at MYADDR, in target byte order.
581 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
583 Returns 1 if could not be read, 0 if could. */
586 read_relative_register_raw_bytes (regnum, myaddr)
590 return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
594 /* Return a `value' with the contents of register REGNUM
595 in its virtual format, with the type specified by
596 REGISTER_VIRTUAL_TYPE.
598 NOTE: returns NULL if register value is not available.
599 Caller will check return value or die! */
602 value_of_register (regnum)
607 register value_ptr reg_val;
608 char raw_buffer[MAX_REGISTER_RAW_SIZE];
611 get_saved_register (raw_buffer, &optim, &addr,
612 selected_frame, regnum, &lval);
614 if (register_valid[regnum] < 0)
615 return NULL; /* register value not available */
617 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
619 /* Convert raw data to virtual format if necessary. */
621 if (REGISTER_CONVERTIBLE (regnum))
623 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
624 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
626 else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
627 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
628 REGISTER_RAW_SIZE (regnum));
630 internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
631 REGISTER_NAME (regnum),
633 REGISTER_RAW_SIZE (regnum),
634 REGISTER_VIRTUAL_SIZE (regnum));
635 VALUE_LVAL (reg_val) = lval;
636 VALUE_ADDRESS (reg_val) = addr;
637 VALUE_REGNO (reg_val) = regnum;
638 VALUE_OPTIMIZED_OUT (reg_val) = optim;
642 /* Low level examining and depositing of registers.
644 The caller is responsible for making
645 sure that the inferior is stopped before calling the fetching routines,
646 or it will get garbage. (a change from GDB version 3, in which
647 the caller got the value from the last stop). */
649 /* Contents and state of the registers (in target byte order). */
653 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
654 register value was not available. */
656 signed char *register_valid;
658 /* The thread/process associated with the current set of registers. For now,
659 -1 is special, and means `no current process'. */
660 int registers_pid = -1;
662 /* Indicate that registers may have changed, so invalidate the cache. */
668 int numregs = ARCH_NUM_REGS;
672 /* Force cleanup of any alloca areas if using C alloca instead of
673 a builtin alloca. This particular call is used to clean up
674 areas allocated by low level target code which may build up
675 during lengthy interactions between gdb and the target before
676 gdb gives control to the user (ie watchpoints). */
679 for (i = 0; i < numregs; i++)
680 register_valid[i] = 0;
682 if (registers_changed_hook)
683 registers_changed_hook ();
686 /* Indicate that all registers have been fetched, so mark them all valid. */
691 int numregs = ARCH_NUM_REGS;
692 for (i = 0; i < numregs; i++)
693 register_valid[i] = 1;
696 /* read_register_bytes and write_register_bytes are generally a *BAD*
697 idea. They are inefficient because they need to check for partial
698 updates, which can only be done by scanning through all of the
699 registers and seeing if the bytes that are being read/written fall
700 inside of an invalid register. [The main reason this is necessary
701 is that register sizes can vary, so a simple index won't suffice.]
702 It is far better to call read_register_gen and write_register_gen
703 if you want to get at the raw register contents, as it only takes a
704 regno as an argument, and therefore can't do a partial register
707 Prior to the recent fixes to check for partial updates, both read
708 and write_register_bytes always checked to see if any registers
709 were stale, and then called target_fetch_registers (-1) to update
710 the whole set. This caused really slowed things down for remote
713 /* Copy INLEN bytes of consecutive data from registers
714 starting with the INREGBYTE'th byte of register data
715 into memory at MYADDR. */
718 read_register_bytes (inregbyte, myaddr, inlen)
723 int inregend = inregbyte + inlen;
726 if (registers_pid != inferior_pid)
728 registers_changed ();
729 registers_pid = inferior_pid;
732 /* See if we are trying to read bytes from out-of-date registers. If so,
733 update just those registers. */
735 for (regno = 0; regno < NUM_REGS; regno++)
737 int regstart, regend;
739 if (register_valid[regno])
742 if (REGISTER_NAME (regno) == NULL || *REGISTER_NAME (regno) == '\0')
745 regstart = REGISTER_BYTE (regno);
746 regend = regstart + REGISTER_RAW_SIZE (regno);
748 if (regend <= inregbyte || inregend <= regstart)
749 /* The range the user wants to read doesn't overlap with regno. */
752 /* We've found an invalid register where at least one byte will be read.
753 Update it from the target. */
754 target_fetch_registers (regno);
756 if (!register_valid[regno])
757 error ("read_register_bytes: Couldn't update register %d.", regno);
761 memcpy (myaddr, ®isters[inregbyte], inlen);
764 /* Read register REGNO into memory at MYADDR, which must be large enough
765 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
766 If the register is known to be the size of a CORE_ADDR or smaller,
767 read_register can be used instead. */
769 read_register_gen (regno, myaddr)
773 if (registers_pid != inferior_pid)
775 registers_changed ();
776 registers_pid = inferior_pid;
779 if (!register_valid[regno])
780 target_fetch_registers (regno);
781 memcpy (myaddr, ®isters[REGISTER_BYTE (regno)],
782 REGISTER_RAW_SIZE (regno));
785 /* Write register REGNO at MYADDR to the target. MYADDR points at
786 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
789 write_register_gen (regno, myaddr)
795 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
796 the registers array if something writes to this register. */
797 if (CANNOT_STORE_REGISTER (regno))
800 if (registers_pid != inferior_pid)
802 registers_changed ();
803 registers_pid = inferior_pid;
806 size = REGISTER_RAW_SIZE (regno);
808 /* If we have a valid copy of the register, and new value == old value,
809 then don't bother doing the actual store. */
811 if (register_valid[regno]
812 && memcmp (®isters[REGISTER_BYTE (regno)], myaddr, size) == 0)
815 target_prepare_to_store ();
817 memcpy (®isters[REGISTER_BYTE (regno)], myaddr, size);
819 register_valid[regno] = 1;
821 target_store_registers (regno);
824 /* Copy INLEN bytes of consecutive data from memory at MYADDR
825 into registers starting with the MYREGSTART'th byte of register data. */
828 write_register_bytes (myregstart, myaddr, inlen)
833 int myregend = myregstart + inlen;
836 target_prepare_to_store ();
838 /* Scan through the registers updating any that are covered by the range
839 myregstart<=>myregend using write_register_gen, which does nice things
840 like handling threads, and avoiding updates when the new and old contents
843 for (regno = 0; regno < NUM_REGS; regno++)
845 int regstart, regend;
847 regstart = REGISTER_BYTE (regno);
848 regend = regstart + REGISTER_RAW_SIZE (regno);
850 /* Is this register completely outside the range the user is writing? */
851 if (myregend <= regstart || regend <= myregstart)
854 /* Is this register completely within the range the user is writing? */
855 else if (myregstart <= regstart && regend <= myregend)
856 write_register_gen (regno, myaddr + (regstart - myregstart));
858 /* The register partially overlaps the range being written. */
861 char regbuf[MAX_REGISTER_RAW_SIZE];
862 /* What's the overlap between this register's bytes and
863 those the caller wants to write? */
864 int overlapstart = max (regstart, myregstart);
865 int overlapend = min (regend, myregend);
867 /* We may be doing a partial update of an invalid register.
868 Update it from the target before scribbling on it. */
869 read_register_gen (regno, regbuf);
871 memcpy (registers + overlapstart,
872 myaddr + (overlapstart - myregstart),
873 overlapend - overlapstart);
875 target_store_registers (regno);
881 /* Return the raw contents of register REGNO, regarding it as an integer. */
882 /* This probably should be returning LONGEST rather than CORE_ADDR. */
885 read_register (regno)
888 if (registers_pid != inferior_pid)
890 registers_changed ();
891 registers_pid = inferior_pid;
894 if (!register_valid[regno])
895 target_fetch_registers (regno);
898 extract_unsigned_integer (®isters[REGISTER_BYTE (regno)],
899 REGISTER_RAW_SIZE (regno)));
903 read_register_pid (regno, pid)
909 if (pid == inferior_pid)
910 return read_register (regno);
912 save_pid = inferior_pid;
916 retval = read_register (regno);
918 inferior_pid = save_pid;
923 /* Store VALUE, into the raw contents of register number REGNO.
924 This should probably write a LONGEST rather than a CORE_ADDR */
927 write_register (regno, val)
934 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
935 the registers array if something writes to this register. */
936 if (CANNOT_STORE_REGISTER (regno))
939 if (registers_pid != inferior_pid)
941 registers_changed ();
942 registers_pid = inferior_pid;
945 size = REGISTER_RAW_SIZE (regno);
947 store_signed_integer (buf, size, (LONGEST) val);
949 /* If we have a valid copy of the register, and new value == old value,
950 then don't bother doing the actual store. */
952 if (register_valid[regno]
953 && memcmp (®isters[REGISTER_BYTE (regno)], buf, size) == 0)
956 target_prepare_to_store ();
958 memcpy (®isters[REGISTER_BYTE (regno)], buf, size);
960 register_valid[regno] = 1;
962 target_store_registers (regno);
966 write_register_pid (regno, val, pid)
973 if (pid == inferior_pid)
975 write_register (regno, val);
979 save_pid = inferior_pid;
983 write_register (regno, val);
985 inferior_pid = save_pid;
988 /* Record that register REGNO contains VAL.
989 This is used when the value is obtained from the inferior or core dump,
990 so there is no need to store the value there.
992 If VAL is a NULL pointer, then it's probably an unsupported register. We
993 just set it's value to all zeros. We might want to record this fact, and
994 report it to the users of read_register and friends.
998 supply_register (regno, val)
1003 if (registers_pid != inferior_pid)
1005 registers_changed ();
1006 registers_pid = inferior_pid;
1010 register_valid[regno] = 1;
1012 memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
1014 memset (®isters[REGISTER_BYTE (regno)], '\000', REGISTER_RAW_SIZE (regno));
1016 /* On some architectures, e.g. HPPA, there are a few stray bits in some
1017 registers, that the rest of the code would like to ignore. */
1018 #ifdef CLEAN_UP_REGISTER_VALUE
1019 CLEAN_UP_REGISTER_VALUE (regno, ®isters[REGISTER_BYTE (regno)]);
1024 /* This routine is getting awfully cluttered with #if's. It's probably
1025 time to turn this into READ_PC and define it in the tm.h file.
1028 1999-06-08: The following were re-written so that it assumes the
1029 existance of a TARGET_READ_PC et.al. macro. A default generic
1030 version of that macro is made available where needed.
1032 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1033 by the multi-arch framework, it will eventually be possible to
1034 eliminate the intermediate read_pc_pid(). The client would call
1035 TARGET_READ_PC directly. (cagney). */
1037 #ifndef TARGET_READ_PC
1038 #define TARGET_READ_PC generic_target_read_pc
1042 generic_target_read_pc (int pid)
1047 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
1051 internal_error ("generic_target_read_pc");
1059 int saved_inferior_pid;
1062 /* In case pid != inferior_pid. */
1063 saved_inferior_pid = inferior_pid;
1066 pc_val = TARGET_READ_PC (pid);
1068 inferior_pid = saved_inferior_pid;
1075 return read_pc_pid (inferior_pid);
1078 #ifndef TARGET_WRITE_PC
1079 #define TARGET_WRITE_PC generic_target_write_pc
1083 generic_target_write_pc (pc, pid)
1089 write_register_pid (PC_REGNUM, pc, pid);
1091 if (NPC_REGNUM >= 0)
1092 write_register_pid (NPC_REGNUM, pc + 4, pid);
1094 if (NNPC_REGNUM >= 0)
1095 write_register_pid (NNPC_REGNUM, pc + 8, pid);
1099 internal_error ("generic_target_write_pc");
1104 write_pc_pid (pc, pid)
1108 int saved_inferior_pid;
1110 /* In case pid != inferior_pid. */
1111 saved_inferior_pid = inferior_pid;
1114 TARGET_WRITE_PC (pc, pid);
1116 inferior_pid = saved_inferior_pid;
1123 write_pc_pid (pc, inferior_pid);
1126 /* Cope with strage ways of getting to the stack and frame pointers */
1128 #ifndef TARGET_READ_SP
1129 #define TARGET_READ_SP generic_target_read_sp
1133 generic_target_read_sp ()
1137 return read_register (SP_REGNUM);
1139 internal_error ("generic_target_read_sp");
1145 return TARGET_READ_SP ();
1148 #ifndef TARGET_WRITE_SP
1149 #define TARGET_WRITE_SP generic_target_write_sp
1153 generic_target_write_sp (val)
1159 write_register (SP_REGNUM, val);
1163 internal_error ("generic_target_write_sp");
1170 TARGET_WRITE_SP (val);
1173 #ifndef TARGET_READ_FP
1174 #define TARGET_READ_FP generic_target_read_fp
1178 generic_target_read_fp ()
1182 return read_register (FP_REGNUM);
1184 internal_error ("generic_target_read_fp");
1190 return TARGET_READ_FP ();
1193 #ifndef TARGET_WRITE_FP
1194 #define TARGET_WRITE_FP generic_target_write_fp
1198 generic_target_write_fp (val)
1204 write_register (FP_REGNUM, val);
1208 internal_error ("generic_target_write_fp");
1215 TARGET_WRITE_FP (val);
1219 /* Given a pointer of type TYPE in target form in BUF, return the
1220 address it represents. */
1222 generic_pointer_to_address (struct type *type, char *buf)
1224 return extract_address (buf, TYPE_LENGTH (type));
1228 /* Given an address, store it as a pointer of type TYPE in target
1231 generic_address_to_pointer (struct type *type, char *buf, CORE_ADDR addr)
1233 store_address (buf, TYPE_LENGTH (type), addr);
1237 /* Will calling read_var_value or locate_var_value on SYM end
1238 up caring what frame it is being evaluated relative to? SYM must
1241 symbol_read_needs_frame (sym)
1244 switch (SYMBOL_CLASS (sym))
1246 /* All cases listed explicitly so that gcc -Wall will detect it if
1247 we failed to consider one. */
1252 case LOC_REGPARM_ADDR:
1256 case LOC_BASEREG_ARG:
1257 case LOC_THREAD_LOCAL_STATIC:
1267 /* Getting the address of a label can be done independently of the block,
1268 even if some *uses* of that address wouldn't work so well without
1272 case LOC_CONST_BYTES:
1273 case LOC_UNRESOLVED:
1274 case LOC_OPTIMIZED_OUT:
1280 /* Given a struct symbol for a variable,
1281 and a stack frame id, read the value of the variable
1282 and return a (pointer to a) struct value containing the value.
1283 If the variable cannot be found, return a zero pointer.
1284 If FRAME is NULL, use the selected_frame. */
1287 read_var_value (var, frame)
1288 register struct symbol *var;
1289 struct frame_info *frame;
1291 register value_ptr v;
1292 struct type *type = SYMBOL_TYPE (var);
1296 v = allocate_value (type);
1297 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
1298 VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
1300 len = TYPE_LENGTH (type);
1303 frame = selected_frame;
1305 switch (SYMBOL_CLASS (var))
1308 /* Put the constant back in target format. */
1309 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
1310 (LONGEST) SYMBOL_VALUE (var));
1311 VALUE_LVAL (v) = not_lval;
1315 /* Put the constant back in target format. */
1316 if (overlay_debugging)
1319 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1320 SYMBOL_BFD_SECTION (var));
1321 store_typed_address (VALUE_CONTENTS_RAW (v), type, addr);
1324 store_typed_address (VALUE_CONTENTS_RAW (v), type,
1325 SYMBOL_VALUE_ADDRESS (var));
1326 VALUE_LVAL (v) = not_lval;
1329 case LOC_CONST_BYTES:
1332 bytes_addr = SYMBOL_VALUE_BYTES (var);
1333 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
1334 VALUE_LVAL (v) = not_lval;
1339 if (overlay_debugging)
1340 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1341 SYMBOL_BFD_SECTION (var));
1343 addr = SYMBOL_VALUE_ADDRESS (var);
1347 /* The import slot does not have a real address in it from the
1348 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1349 execution yet, so check for that. */
1350 if (!target_has_execution)
1352 Attempt to access variable defined in different shared object or load module when\n\
1353 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1355 addr = SYMBOL_VALUE_ADDRESS (var);
1356 addr = read_memory_unsigned_integer
1357 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1363 addr = FRAME_ARGS_ADDRESS (frame);
1366 addr += SYMBOL_VALUE (var);
1372 addr = FRAME_ARGS_ADDRESS (frame);
1375 addr += SYMBOL_VALUE (var);
1376 addr = read_memory_unsigned_integer
1377 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1384 addr = FRAME_LOCALS_ADDRESS (frame);
1385 addr += SYMBOL_VALUE (var);
1389 case LOC_BASEREG_ARG:
1391 char buf[MAX_REGISTER_RAW_SIZE];
1392 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1394 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1395 addr += SYMBOL_VALUE (var);
1399 case LOC_THREAD_LOCAL_STATIC:
1401 char buf[MAX_REGISTER_RAW_SIZE];
1403 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1405 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1406 addr += SYMBOL_VALUE (var);
1411 error ("Cannot look up value of a typedef");
1415 if (overlay_debugging)
1416 VALUE_ADDRESS (v) = symbol_overlayed_address
1417 (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
1419 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
1424 case LOC_REGPARM_ADDR:
1427 int regno = SYMBOL_VALUE (var);
1432 b = get_frame_block (frame);
1434 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
1436 regval = value_from_register (lookup_pointer_type (type),
1441 error ("Value of register variable not available.");
1443 addr = value_as_pointer (regval);
1444 VALUE_LVAL (v) = lval_memory;
1448 regval = value_from_register (type, regno, frame);
1451 error ("Value of register variable not available.");
1457 case LOC_UNRESOLVED:
1459 struct minimal_symbol *msym;
1461 msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
1464 if (overlay_debugging)
1465 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
1466 SYMBOL_BFD_SECTION (msym));
1468 addr = SYMBOL_VALUE_ADDRESS (msym);
1472 case LOC_OPTIMIZED_OUT:
1473 VALUE_LVAL (v) = not_lval;
1474 VALUE_OPTIMIZED_OUT (v) = 1;
1478 error ("Cannot look up value of a botched symbol.");
1482 VALUE_ADDRESS (v) = addr;
1487 /* Return a value of type TYPE, stored in register REGNUM, in frame
1490 NOTE: returns NULL if register value is not available.
1491 Caller will check return value or die! */
1494 value_from_register (type, regnum, frame)
1497 struct frame_info *frame;
1499 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1502 value_ptr v = allocate_value (type);
1503 char *value_bytes = 0;
1504 int value_bytes_copied = 0;
1505 int num_storage_locs;
1506 enum lval_type lval;
1509 CHECK_TYPEDEF (type);
1510 len = TYPE_LENGTH (type);
1512 /* Pointers on D10V are really only 16 bits, but we lie to gdb elsewhere... */
1513 if (GDB_TARGET_IS_D10V && TYPE_CODE (type) == TYPE_CODE_PTR)
1516 VALUE_REGNO (v) = regnum;
1518 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
1519 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
1522 if (num_storage_locs > 1
1523 #ifdef GDB_TARGET_IS_H8500
1524 || TYPE_CODE (type) == TYPE_CODE_PTR
1528 /* Value spread across multiple storage locations. */
1531 int mem_stor = 0, reg_stor = 0;
1532 int mem_tracking = 1;
1533 CORE_ADDR last_addr = 0;
1534 CORE_ADDR first_addr = 0;
1536 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1538 /* Copy all of the data out, whereever it may be. */
1540 #ifdef GDB_TARGET_IS_H8500
1541 /* This piece of hideosity is required because the H8500 treats registers
1542 differently depending upon whether they are used as pointers or not. As a
1543 pointer, a register needs to have a page register tacked onto the front.
1544 An alternate way to do this would be to have gcc output different register
1545 numbers for the pointer & non-pointer form of the register. But, it
1546 doesn't, so we're stuck with this. */
1548 if (TYPE_CODE (type) == TYPE_CODE_PTR
1559 page_regnum = SEG_D_REGNUM;
1563 page_regnum = SEG_E_REGNUM;
1567 page_regnum = SEG_T_REGNUM;
1572 get_saved_register (value_bytes + 1,
1579 if (register_valid[page_regnum] == -1)
1580 return NULL; /* register value not available */
1582 if (lval == lval_register)
1589 get_saved_register (value_bytes + 2,
1596 if (register_valid[regnum] == -1)
1597 return NULL; /* register value not available */
1599 if (lval == lval_register)
1604 mem_tracking = mem_tracking && (addr == last_addr);
1609 #endif /* GDB_TARGET_IS_H8500 */
1610 for (local_regnum = regnum;
1611 value_bytes_copied < len;
1612 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
1615 get_saved_register (value_bytes + value_bytes_copied,
1622 if (register_valid[local_regnum] == -1)
1623 return NULL; /* register value not available */
1625 if (regnum == local_regnum)
1627 if (lval == lval_register)
1635 && (regnum == local_regnum
1636 || addr == last_addr));
1641 if ((reg_stor && mem_stor)
1642 || (mem_stor && !mem_tracking))
1643 /* Mixed storage; all of the hassle we just went through was
1644 for some good purpose. */
1646 VALUE_LVAL (v) = lval_reg_frame_relative;
1647 VALUE_FRAME (v) = FRAME_FP (frame);
1648 VALUE_FRAME_REGNUM (v) = regnum;
1652 VALUE_LVAL (v) = lval_memory;
1653 VALUE_ADDRESS (v) = first_addr;
1657 VALUE_LVAL (v) = lval_register;
1658 VALUE_ADDRESS (v) = first_addr;
1661 internal_error ("value_from_register: Value not stored anywhere!");
1663 VALUE_OPTIMIZED_OUT (v) = optim;
1665 /* Any structure stored in more than one register will always be
1666 an integral number of registers. Otherwise, you'd need to do
1667 some fiddling with the last register copied here for little
1670 /* Copy into the contents section of the value. */
1671 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
1673 /* Finally do any conversion necessary when extracting this
1674 type from more than one register. */
1675 #ifdef REGISTER_CONVERT_TO_TYPE
1676 REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v));
1681 /* Data is completely contained within a single register. Locate the
1682 register's contents in a real register or in core;
1683 read the data in raw format. */
1685 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
1687 if (register_valid[regnum] == -1)
1688 return NULL; /* register value not available */
1690 VALUE_OPTIMIZED_OUT (v) = optim;
1691 VALUE_LVAL (v) = lval;
1692 VALUE_ADDRESS (v) = addr;
1694 /* Convert raw data to virtual format if necessary. */
1696 if (REGISTER_CONVERTIBLE (regnum))
1698 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1699 raw_buffer, VALUE_CONTENTS_RAW (v));
1703 /* Raw and virtual formats are the same for this register. */
1705 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
1707 /* Big-endian, and we want less than full size. */
1708 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1711 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
1714 if (GDB_TARGET_IS_D10V
1715 && TYPE_CODE (type) == TYPE_CODE_PTR
1716 && TYPE_TARGET_TYPE (type)
1717 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
1719 /* pointer to function */
1721 unsigned short snum;
1722 snum = (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v), 2);
1723 num = D10V_MAKE_IADDR (snum);
1724 store_address (VALUE_CONTENTS_RAW (v), 4, num);
1726 else if (GDB_TARGET_IS_D10V
1727 && TYPE_CODE (type) == TYPE_CODE_PTR)
1729 /* pointer to data */
1731 unsigned short snum;
1732 snum = (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v), 2);
1733 num = D10V_MAKE_DADDR (snum);
1734 store_address (VALUE_CONTENTS_RAW (v), 4, num);
1740 /* Given a struct symbol for a variable or function,
1741 and a stack frame id,
1742 return a (pointer to a) struct value containing the properly typed
1746 locate_var_value (var, frame)
1747 register struct symbol *var;
1748 struct frame_info *frame;
1751 struct type *type = SYMBOL_TYPE (var);
1752 value_ptr lazy_value;
1754 /* Evaluate it first; if the result is a memory address, we're fine.
1755 Lazy evaluation pays off here. */
1757 lazy_value = read_var_value (var, frame);
1758 if (lazy_value == 0)
1759 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1761 if (VALUE_LAZY (lazy_value)
1762 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1766 addr = VALUE_ADDRESS (lazy_value);
1767 val = value_from_pointer (lookup_pointer_type (type), addr);
1768 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
1772 /* Not a memory address; check what the problem was. */
1773 switch (VALUE_LVAL (lazy_value))
1776 case lval_reg_frame_relative:
1777 error ("Address requested for identifier \"%s\" which is in a register.",
1778 SYMBOL_SOURCE_NAME (var));
1782 error ("Can't take address of \"%s\" which isn't an lvalue.",
1783 SYMBOL_SOURCE_NAME (var));
1786 return 0; /* For lint -- never reached */
1790 static void build_findvar PARAMS ((void));
1794 /* We allocate some extra slop since we do a lot of memcpy's around
1795 `registers', and failing-soft is better than failing hard. */
1796 int sizeof_registers = REGISTER_BYTES + /* SLOP */ 256;
1797 int sizeof_register_valid = NUM_REGS * sizeof (*register_valid);
1798 registers = xmalloc (sizeof_registers);
1799 memset (registers, 0, sizeof_registers);
1800 register_valid = xmalloc (sizeof_register_valid);
1801 memset (register_valid, 0, sizeof_register_valid);
1804 void _initialize_findvar PARAMS ((void));
1806 _initialize_findvar ()
1810 register_gdbarch_swap (®isters, sizeof (registers), NULL);
1811 register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL);
1812 register_gdbarch_swap (NULL, 0, build_findvar);