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, Boston, MA 02111-1307, USA. */
29 #include "gdb_string.h"
30 #include "floatformat.h"
31 #include "symfile.h" /* for overlay functions */
33 /* This is used to indicate that we don't know the format of the floating point
34 number. Typically, this is useful for native ports, where the actual format
35 is irrelevant, since no conversions will be taking place. */
37 const struct floatformat floatformat_unknown
;
39 /* Registers we shouldn't try to store. */
40 #if !defined (CANNOT_STORE_REGISTER)
41 #define CANNOT_STORE_REGISTER(regno) 0
44 static void write_register_gen
PARAMS ((int, char *));
46 static int read_relative_register_raw_bytes_for_frame
PARAMS ((int regnum
, char *myaddr
, struct frame_info
*frame
));
48 /* Basic byte-swapping routines. GDB has needed these for a long time...
49 All extract a target-format integer at ADDR which is LEN bytes long. */
51 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
52 /* 8 bit characters are a pretty safe assumption these days, so we
53 assume it throughout all these swapping routines. If we had to deal with
54 9 bit characters, we would need to make len be in bits and would have
55 to re-write these routines... */
60 extract_signed_integer (addr
, len
)
66 unsigned char *startaddr
= (unsigned char *)addr
;
67 unsigned char *endaddr
= startaddr
+ len
;
69 if (len
> (int) sizeof (LONGEST
))
71 That operation is not available on integers of more than %d bytes.",
74 /* Start at the most significant end of the integer, and work towards
75 the least significant. */
76 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
79 /* Do the sign extension once at the start. */
80 retval
= ((LONGEST
)*p
^ 0x80) - 0x80;
81 for (++p
; p
< endaddr
; ++p
)
82 retval
= (retval
<< 8) | *p
;
87 /* Do the sign extension once at the start. */
88 retval
= ((LONGEST
)*p
^ 0x80) - 0x80;
89 for (--p
; p
>= startaddr
; --p
)
90 retval
= (retval
<< 8) | *p
;
96 extract_unsigned_integer (addr
, len
)
102 unsigned char *startaddr
= (unsigned char *)addr
;
103 unsigned char *endaddr
= startaddr
+ len
;
105 if (len
> (int) sizeof (ULONGEST
))
107 That operation is not available on integers of more than %d bytes.",
110 /* Start at the most significant end of the integer, and work towards
111 the least significant. */
113 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
115 for (p
= startaddr
; p
< endaddr
; ++p
)
116 retval
= (retval
<< 8) | *p
;
120 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
121 retval
= (retval
<< 8) | *p
;
126 /* Sometimes a long long unsigned integer can be extracted as a
127 LONGEST value. This is done so that we can print these values
128 better. If this integer can be converted to a LONGEST, this
129 function returns 1 and sets *PVAL. Otherwise it returns 0. */
132 extract_long_unsigned_integer (addr
, orig_len
, pval
)
137 char *p
, *first_addr
;
141 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
143 for (p
= (char *) addr
;
144 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
156 first_addr
= (char *) addr
;
157 for (p
= (char *) addr
+ orig_len
- 1;
158 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
168 if (len
<= (int) sizeof (LONGEST
))
170 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
179 extract_address (addr
, len
)
183 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
184 whether we want this to be true eventually. */
185 return (CORE_ADDR
)extract_unsigned_integer (addr
, len
);
189 store_signed_integer (addr
, len
, val
)
195 unsigned char *startaddr
= (unsigned char *)addr
;
196 unsigned char *endaddr
= startaddr
+ len
;
198 /* Start at the least significant end of the integer, and work towards
199 the most significant. */
200 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
202 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
210 for (p
= startaddr
; p
< endaddr
; ++p
)
219 store_unsigned_integer (addr
, len
, val
)
225 unsigned char *startaddr
= (unsigned char *)addr
;
226 unsigned char *endaddr
= startaddr
+ len
;
228 /* Start at the least significant end of the integer, and work towards
229 the most significant. */
230 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
232 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
240 for (p
= startaddr
; p
< endaddr
; ++p
)
248 /* Store the literal address "val" into
249 gdb-local memory pointed to by "addr"
252 store_address (addr
, len
, val
)
257 store_unsigned_integer (addr
, len
, val
);
260 /* Swap LEN bytes at BUFFER between target and host byte-order. */
261 #define SWAP_FLOATING(buffer,len) \
264 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
267 char *p = (char *)(buffer); \
268 char *q = ((char *)(buffer)) + len - 1; \
269 for (; p < q; p++, q--) \
279 /* Extract a floating-point number from a target-order byte-stream at ADDR.
280 Returns the value as type DOUBLEST.
282 If the host and target formats agree, we just copy the raw data into the
283 appropriate type of variable and return, letting the host increase precision
284 as necessary. Otherwise, we call the conversion routine and let it do the
288 extract_floating (addr
, len
)
294 if (len
== sizeof (float))
296 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
300 memcpy (&retval
, addr
, sizeof (retval
));
304 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
306 else if (len
== sizeof (double))
308 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
312 memcpy (&retval
, addr
, sizeof (retval
));
316 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
318 else if (len
== sizeof (DOUBLEST
))
320 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
324 memcpy (&retval
, addr
, sizeof (retval
));
328 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
332 error ("Can't deal with a floating point number of %d bytes.", len
);
339 store_floating (addr
, len
, val
)
344 if (len
== sizeof (float))
346 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
348 float floatval
= val
;
350 memcpy (addr
, &floatval
, sizeof (floatval
));
353 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
355 else if (len
== sizeof (double))
357 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
359 double doubleval
= val
;
361 memcpy (addr
, &doubleval
, sizeof (doubleval
));
364 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
366 else if (len
== sizeof (DOUBLEST
))
368 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
369 memcpy (addr
, &val
, sizeof (val
));
371 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
375 error ("Can't deal with a floating point number of %d bytes.", len
);
380 /* Return the address in which frame FRAME's value of register REGNUM
381 has been saved in memory. Or return zero if it has not been saved.
382 If REGNUM specifies the SP, the value we return is actually
383 the SP value, not an address where it was saved. */
386 find_saved_register (frame
, regnum
)
387 struct frame_info
*frame
;
390 register struct frame_info
*frame1
= NULL
;
391 register CORE_ADDR addr
= 0;
393 if (frame
== NULL
) /* No regs saved if want current frame */
396 #ifdef HAVE_REGISTER_WINDOWS
397 /* We assume that a register in a register window will only be saved
398 in one place (since the name changes and/or disappears as you go
399 towards inner frames), so we only call get_frame_saved_regs on
400 the current frame. This is directly in contradiction to the
401 usage below, which assumes that registers used in a frame must be
402 saved in a lower (more interior) frame. This change is a result
403 of working on a register window machine; get_frame_saved_regs
404 always returns the registers saved within a frame, within the
405 context (register namespace) of that frame. */
407 /* However, note that we don't want this to return anything if
408 nothing is saved (if there's a frame inside of this one). Also,
409 callers to this routine asking for the stack pointer want the
410 stack pointer saved for *this* frame; this is returned from the
413 if (REGISTER_IN_WINDOW_P(regnum
))
415 frame1
= get_next_frame (frame
);
416 if (!frame1
) return 0; /* Registers of this frame are active. */
418 /* Get the SP from the next frame in; it will be this
420 if (regnum
!= SP_REGNUM
)
423 FRAME_INIT_SAVED_REGS (frame1
);
424 return frame1
->saved_regs
[regnum
]; /* ... which might be zero */
426 #endif /* HAVE_REGISTER_WINDOWS */
428 /* Note that this next routine assumes that registers used in
429 frame x will be saved only in the frame that x calls and
430 frames interior to it. This is not true on the sparc, but the
431 above macro takes care of it, so we should be all right. */
435 frame1
= get_prev_frame (frame1
);
436 if (frame1
== 0 || frame1
== frame
)
438 FRAME_INIT_SAVED_REGS (frame1
);
439 if (frame1
->saved_regs
[regnum
])
440 addr
= frame1
->saved_regs
[regnum
];
446 /* Find register number REGNUM relative to FRAME and put its (raw,
447 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
448 variable was optimized out (and thus can't be fetched). Set *LVAL
449 to lval_memory, lval_register, or not_lval, depending on whether
450 the value was fetched from memory, from a register, or in a strange
451 and non-modifiable way (e.g. a frame pointer which was calculated
452 rather than fetched). Set *ADDRP to the address, either in memory
453 on as a REGISTER_BYTE offset into the registers array.
455 Note that this implementation never sets *LVAL to not_lval. But
456 it can be replaced by defining GET_SAVED_REGISTER and supplying
459 The argument RAW_BUFFER must point to aligned memory. */
462 default_get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
466 struct frame_info
*frame
;
468 enum lval_type
*lval
;
472 if (!target_has_registers
)
473 error ("No registers.");
475 /* Normal systems don't optimize out things with register numbers. */
476 if (optimized
!= NULL
)
478 addr
= find_saved_register (frame
, regnum
);
483 if (regnum
== SP_REGNUM
)
485 if (raw_buffer
!= NULL
)
487 /* Put it back in target format. */
488 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
), (LONGEST
)addr
);
494 if (raw_buffer
!= NULL
)
495 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
500 *lval
= lval_register
;
501 addr
= REGISTER_BYTE (regnum
);
502 if (raw_buffer
!= NULL
)
503 read_register_gen (regnum
, raw_buffer
);
509 #if !defined (GET_SAVED_REGISTER)
510 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
511 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
514 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
518 struct frame_info
*frame
;
520 enum lval_type
*lval
;
522 GET_SAVED_REGISTER (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
);
525 /* Copy the bytes of register REGNUM, relative to the input stack frame,
526 into our memory at MYADDR, in target byte order.
527 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
529 Returns 1 if could not be read, 0 if could. */
532 read_relative_register_raw_bytes_for_frame (regnum
, myaddr
, frame
)
535 struct frame_info
*frame
;
538 if (regnum
== FP_REGNUM
&& frame
)
540 /* Put it back in target format. */
541 store_address (myaddr
, REGISTER_RAW_SIZE(FP_REGNUM
),
542 (LONGEST
)FRAME_FP(frame
));
547 get_saved_register (myaddr
, &optim
, (CORE_ADDR
*) NULL
, frame
,
548 regnum
, (enum lval_type
*)NULL
);
550 if (register_valid
[regnum
] < 0)
551 return 1; /* register value not available */
556 /* Copy the bytes of register REGNUM, relative to the current stack frame,
557 into our memory at MYADDR, in target byte order.
558 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
560 Returns 1 if could not be read, 0 if could. */
563 read_relative_register_raw_bytes (regnum
, myaddr
)
567 return read_relative_register_raw_bytes_for_frame (regnum
, myaddr
,
571 /* Return a `value' with the contents of register REGNUM
572 in its virtual format, with the type specified by
573 REGISTER_VIRTUAL_TYPE.
575 NOTE: returns NULL if register value is not available.
576 Caller will check return value or die! */
579 value_of_register (regnum
)
584 register value_ptr reg_val
;
585 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
588 get_saved_register (raw_buffer
, &optim
, &addr
,
589 selected_frame
, regnum
, &lval
);
591 if (register_valid
[regnum
] < 0)
592 return NULL
; /* register value not available */
594 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
596 /* Convert raw data to virtual format if necessary. */
598 if (REGISTER_CONVERTIBLE (regnum
))
600 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
601 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
603 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
604 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
605 REGISTER_RAW_SIZE (regnum
));
607 fatal ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
608 REGISTER_NAME (regnum
), regnum
,
609 REGISTER_RAW_SIZE (regnum
), REGISTER_VIRTUAL_SIZE (regnum
));
610 VALUE_LVAL (reg_val
) = lval
;
611 VALUE_ADDRESS (reg_val
) = addr
;
612 VALUE_REGNO (reg_val
) = regnum
;
613 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
617 /* Low level examining and depositing of registers.
619 The caller is responsible for making
620 sure that the inferior is stopped before calling the fetching routines,
621 or it will get garbage. (a change from GDB version 3, in which
622 the caller got the value from the last stop). */
624 /* Contents and state of the registers (in target byte order). */
628 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
629 register value was not available. */
631 signed char *register_valid
;
633 /* The thread/process associated with the current set of registers. For now,
634 -1 is special, and means `no current process'. */
635 int registers_pid
= -1;
637 /* Indicate that registers may have changed, so invalidate the cache. */
643 int numregs
= ARCH_NUM_REGS
;
647 /* Force cleanup of any alloca areas if using C alloca instead of
648 a builtin alloca. This particular call is used to clean up
649 areas allocated by low level target code which may build up
650 during lengthy interactions between gdb and the target before
651 gdb gives control to the user (ie watchpoints). */
654 for (i
= 0; i
< numregs
; i
++)
655 register_valid
[i
] = 0;
657 if (registers_changed_hook
)
658 registers_changed_hook ();
661 /* Indicate that all registers have been fetched, so mark them all valid. */
666 int numregs
= ARCH_NUM_REGS
;
667 for (i
= 0; i
< numregs
; i
++)
668 register_valid
[i
] = 1;
671 /* read_register_bytes and write_register_bytes are generally a *BAD* idea.
672 They are inefficient because they need to check for partial updates, which
673 can only be done by scanning through all of the registers and seeing if the
674 bytes that are being read/written fall inside of an invalid register. [The
675 main reason this is necessary is that register sizes can vary, so a simple
676 index won't suffice.] It is far better to call read_register_gen if you
677 want to get at the raw register contents, as it only takes a regno as an
678 argument, and therefore can't do a partial register update. It would also
679 be good to have a write_register_gen for similar reasons.
681 Prior to the recent fixes to check for partial updates, both read and
682 write_register_bytes always checked to see if any registers were stale, and
683 then called target_fetch_registers (-1) to update the whole set. This
684 caused really slowed things down for remote targets. */
686 /* Copy INLEN bytes of consecutive data from registers
687 starting with the INREGBYTE'th byte of register data
688 into memory at MYADDR. */
691 read_register_bytes (inregbyte
, myaddr
, inlen
)
696 int inregend
= inregbyte
+ inlen
;
699 if (registers_pid
!= inferior_pid
)
701 registers_changed ();
702 registers_pid
= inferior_pid
;
705 /* See if we are trying to read bytes from out-of-date registers. If so,
706 update just those registers. */
708 for (regno
= 0; regno
< NUM_REGS
; regno
++)
710 int regstart
, regend
;
713 if (register_valid
[regno
])
716 if (REGISTER_NAME (regno
) == NULL
|| *REGISTER_NAME (regno
) == '\0')
719 regstart
= REGISTER_BYTE (regno
);
720 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
722 startin
= regstart
>= inregbyte
&& regstart
< inregend
;
723 endin
= regend
> inregbyte
&& regend
<= inregend
;
725 if (!startin
&& !endin
)
728 /* We've found an invalid register where at least one byte will be read.
729 Update it from the target. */
731 target_fetch_registers (regno
);
733 if (!register_valid
[regno
])
734 error ("read_register_bytes: Couldn't update register %d.", regno
);
738 memcpy (myaddr
, ®isters
[inregbyte
], inlen
);
741 /* Read register REGNO into memory at MYADDR, which must be large enough
742 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
743 If the register is known to be the size of a CORE_ADDR or smaller,
744 read_register can be used instead. */
746 read_register_gen (regno
, myaddr
)
750 if (registers_pid
!= inferior_pid
)
752 registers_changed ();
753 registers_pid
= inferior_pid
;
756 if (!register_valid
[regno
])
757 target_fetch_registers (regno
);
758 memcpy (myaddr
, ®isters
[REGISTER_BYTE (regno
)],
759 REGISTER_RAW_SIZE (regno
));
762 /* Write register REGNO at MYADDR to the target. MYADDR points at
763 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
766 write_register_gen (regno
, myaddr
)
772 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
773 the registers array if something writes to this register. */
774 if (CANNOT_STORE_REGISTER (regno
))
777 if (registers_pid
!= inferior_pid
)
779 registers_changed ();
780 registers_pid
= inferior_pid
;
783 size
= REGISTER_RAW_SIZE(regno
);
785 /* If we have a valid copy of the register, and new value == old value,
786 then don't bother doing the actual store. */
788 if (register_valid
[regno
]
789 && memcmp (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
) == 0)
792 target_prepare_to_store ();
794 memcpy (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
);
796 register_valid
[regno
] = 1;
798 target_store_registers (regno
);
801 /* Copy INLEN bytes of consecutive data from memory at MYADDR
802 into registers starting with the MYREGSTART'th byte of register data. */
805 write_register_bytes (myregstart
, myaddr
, inlen
)
810 int myregend
= myregstart
+ inlen
;
813 target_prepare_to_store ();
815 /* Scan through the registers updating any that are covered by the range
816 myregstart<=>myregend using write_register_gen, which does nice things
817 like handling threads, and avoiding updates when the new and old contents
820 for (regno
= 0; regno
< NUM_REGS
; regno
++)
822 int regstart
, regend
;
824 char regbuf
[MAX_REGISTER_RAW_SIZE
];
826 regstart
= REGISTER_BYTE (regno
);
827 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
829 startin
= regstart
>= myregstart
&& regstart
< myregend
;
830 endin
= regend
> myregstart
&& regend
<= myregend
;
832 if (!startin
&& !endin
)
833 continue; /* Register is completely out of range */
835 if (startin
&& endin
) /* register is completely in range */
837 write_register_gen (regno
, myaddr
+ (regstart
- myregstart
));
841 /* We may be doing a partial update of an invalid register. Update it
842 from the target before scribbling on it. */
843 read_register_gen (regno
, regbuf
);
846 memcpy (registers
+ regstart
,
847 myaddr
+ regstart
- myregstart
,
848 myregend
- regstart
);
850 memcpy (registers
+ myregstart
,
852 regend
- myregstart
);
853 target_store_registers (regno
);
857 /* Return the raw contents of register REGNO, regarding it as an integer. */
858 /* This probably should be returning LONGEST rather than CORE_ADDR. */
861 read_register (regno
)
864 if (registers_pid
!= inferior_pid
)
866 registers_changed ();
867 registers_pid
= inferior_pid
;
870 if (!register_valid
[regno
])
871 target_fetch_registers (regno
);
873 return (CORE_ADDR
)extract_address (®isters
[REGISTER_BYTE (regno
)],
874 REGISTER_RAW_SIZE(regno
));
878 read_register_pid (regno
, pid
)
884 if (pid
== inferior_pid
)
885 return read_register (regno
);
887 save_pid
= inferior_pid
;
891 retval
= read_register (regno
);
893 inferior_pid
= save_pid
;
898 /* Store VALUE, into the raw contents of register number REGNO.
899 This should probably write a LONGEST rather than a CORE_ADDR */
902 write_register (regno
, val
)
909 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
910 the registers array if something writes to this register. */
911 if (CANNOT_STORE_REGISTER (regno
))
914 if (registers_pid
!= inferior_pid
)
916 registers_changed ();
917 registers_pid
= inferior_pid
;
920 size
= REGISTER_RAW_SIZE(regno
);
922 store_signed_integer (buf
, size
, (LONGEST
)val
);
924 /* If we have a valid copy of the register, and new value == old value,
925 then don't bother doing the actual store. */
927 if (register_valid
[regno
]
928 && memcmp (®isters
[REGISTER_BYTE (regno
)], buf
, size
) == 0)
931 target_prepare_to_store ();
933 memcpy (®isters
[REGISTER_BYTE (regno
)], buf
, size
);
935 register_valid
[regno
] = 1;
937 target_store_registers (regno
);
941 write_register_pid (regno
, val
, pid
)
948 if (pid
== inferior_pid
)
950 write_register (regno
, val
);
954 save_pid
= inferior_pid
;
958 write_register (regno
, val
);
960 inferior_pid
= save_pid
;
963 /* Record that register REGNO contains VAL.
964 This is used when the value is obtained from the inferior or core dump,
965 so there is no need to store the value there.
967 If VAL is a NULL pointer, then it's probably an unsupported register. We
968 just set it's value to all zeros. We might want to record this fact, and
969 report it to the users of read_register and friends.
973 supply_register (regno
, val
)
978 if (registers_pid
!= inferior_pid
)
980 registers_changed ();
981 registers_pid
= inferior_pid
;
985 register_valid
[regno
] = 1;
987 memcpy (®isters
[REGISTER_BYTE (regno
)], val
, REGISTER_RAW_SIZE (regno
));
989 memset (®isters
[REGISTER_BYTE (regno
)], '\000', REGISTER_RAW_SIZE (regno
));
991 /* On some architectures, e.g. HPPA, there are a few stray bits in some
992 registers, that the rest of the code would like to ignore. */
993 #ifdef CLEAN_UP_REGISTER_VALUE
994 CLEAN_UP_REGISTER_VALUE(regno
, ®isters
[REGISTER_BYTE(regno
)]);
999 /* This routine is getting awfully cluttered with #if's. It's probably
1000 time to turn this into READ_PC and define it in the tm.h file.
1003 1999-06-08: The following were re-written so that it assumes the
1004 existance of a TARGET_READ_PC et.al. macro. A default generic
1005 version of that macro is made available where needed.
1007 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1008 by the multi-arch framework, it will eventually be possible to
1009 eliminate the intermediate read_pc_pid(). The client would call
1010 TARGET_READ_PC directly. (cagney). */
1012 #ifndef TARGET_READ_PC
1013 #define TARGET_READ_PC generic_target_read_pc
1017 generic_target_read_pc (pid
)
1022 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, pid
));
1026 fatal ("generic_target_read_pc");
1034 int saved_inferior_pid
;
1037 /* In case pid != inferior_pid. */
1038 saved_inferior_pid
= inferior_pid
;
1041 pc_val
= TARGET_READ_PC (pid
);
1043 inferior_pid
= saved_inferior_pid
;
1050 return read_pc_pid (inferior_pid
);
1053 #ifndef TARGET_WRITE_PC
1054 #define TARGET_WRITE_PC generic_target_write_pc
1058 generic_target_write_pc (pc
, pid
)
1064 write_register_pid (PC_REGNUM
, pc
, pid
);
1066 if (NPC_REGNUM
>= 0)
1067 write_register_pid (NPC_REGNUM
, pc
+ 4, pid
);
1069 if (NNPC_REGNUM
>= 0)
1070 write_register_pid (NNPC_REGNUM
, pc
+ 8, pid
);
1074 fatal ("generic_target_write_pc");
1079 write_pc_pid (pc
, pid
)
1083 int saved_inferior_pid
;
1085 /* In case pid != inferior_pid. */
1086 saved_inferior_pid
= inferior_pid
;
1089 TARGET_WRITE_PC (pc
, pid
);
1091 inferior_pid
= saved_inferior_pid
;
1098 write_pc_pid (pc
, inferior_pid
);
1101 /* Cope with strage ways of getting to the stack and frame pointers */
1103 #ifndef TARGET_READ_SP
1104 #define TARGET_READ_SP generic_target_read_sp
1108 generic_target_read_sp ()
1112 return read_register (SP_REGNUM
);
1114 fatal ("generic_target_read_sp");
1120 return TARGET_READ_SP ();
1123 #ifndef TARGET_WRITE_SP
1124 #define TARGET_WRITE_SP generic_target_write_sp
1128 generic_target_write_sp (val
)
1134 write_register (SP_REGNUM
, val
);
1138 fatal ("generic_target_write_sp");
1145 TARGET_WRITE_SP (val
);
1148 #ifndef TARGET_READ_FP
1149 #define TARGET_READ_FP generic_target_read_fp
1153 generic_target_read_fp ()
1157 return read_register (FP_REGNUM
);
1159 fatal ("generic_target_read_fp");
1165 return TARGET_READ_FP ();
1168 #ifndef TARGET_WRITE_FP
1169 #define TARGET_WRITE_FP generic_target_write_fp
1173 generic_target_write_fp (val
)
1179 write_register (FP_REGNUM
, val
);
1183 fatal ("generic_target_write_fp");
1190 TARGET_WRITE_FP (val
);
1193 /* Will calling read_var_value or locate_var_value on SYM end
1194 up caring what frame it is being evaluated relative to? SYM must
1197 symbol_read_needs_frame (sym
)
1200 switch (SYMBOL_CLASS (sym
))
1202 /* All cases listed explicitly so that gcc -Wall will detect it if
1203 we failed to consider one. */
1208 case LOC_REGPARM_ADDR
:
1212 case LOC_BASEREG_ARG
:
1213 case LOC_THREAD_LOCAL_STATIC
:
1223 /* Getting the address of a label can be done independently of the block,
1224 even if some *uses* of that address wouldn't work so well without
1228 case LOC_CONST_BYTES
:
1229 case LOC_UNRESOLVED
:
1230 case LOC_OPTIMIZED_OUT
:
1236 /* Given a struct symbol for a variable,
1237 and a stack frame id, read the value of the variable
1238 and return a (pointer to a) struct value containing the value.
1239 If the variable cannot be found, return a zero pointer.
1240 If FRAME is NULL, use the selected_frame. */
1243 read_var_value (var
, frame
)
1244 register struct symbol
*var
;
1245 struct frame_info
*frame
;
1247 register value_ptr v
;
1248 struct type
*type
= SYMBOL_TYPE (var
);
1252 v
= allocate_value (type
);
1253 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
1254 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
1256 len
= TYPE_LENGTH (type
);
1258 if (frame
== NULL
) frame
= selected_frame
;
1260 switch (SYMBOL_CLASS (var
))
1263 /* Put the constant back in target format. */
1264 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
1265 (LONGEST
) SYMBOL_VALUE (var
));
1266 VALUE_LVAL (v
) = not_lval
;
1270 /* Put the constant back in target format. */
1271 if (overlay_debugging
)
1272 store_address (VALUE_CONTENTS_RAW (v
), len
,
1273 (LONGEST
)symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1274 SYMBOL_BFD_SECTION (var
)));
1276 store_address (VALUE_CONTENTS_RAW (v
), len
,
1277 (LONGEST
)SYMBOL_VALUE_ADDRESS (var
));
1278 VALUE_LVAL (v
) = not_lval
;
1281 case LOC_CONST_BYTES
:
1284 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
1285 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
1286 VALUE_LVAL (v
) = not_lval
;
1291 if (overlay_debugging
)
1292 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1293 SYMBOL_BFD_SECTION (var
));
1295 addr
= SYMBOL_VALUE_ADDRESS (var
);
1299 /* The import slot does not have a real address in it from the
1300 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1301 execution yet, so check for that. */
1302 if (!target_has_execution
)
1304 Attempt to access variable defined in different shared object or load module when\n\
1305 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1307 addr
= SYMBOL_VALUE_ADDRESS (var
);
1308 addr
= read_memory_unsigned_integer
1309 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1315 addr
= FRAME_ARGS_ADDRESS (frame
);
1318 addr
+= SYMBOL_VALUE (var
);
1324 addr
= FRAME_ARGS_ADDRESS (frame
);
1327 addr
+= SYMBOL_VALUE (var
);
1328 addr
= read_memory_unsigned_integer
1329 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1336 addr
= FRAME_LOCALS_ADDRESS (frame
);
1337 addr
+= SYMBOL_VALUE (var
);
1341 case LOC_BASEREG_ARG
:
1343 char buf
[MAX_REGISTER_RAW_SIZE
];
1344 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1346 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1347 addr
+= SYMBOL_VALUE (var
);
1351 case LOC_THREAD_LOCAL_STATIC
:
1353 char buf
[MAX_REGISTER_RAW_SIZE
];
1355 get_saved_register(buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1357 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1358 addr
+= SYMBOL_VALUE (var
);
1363 error ("Cannot look up value of a typedef");
1367 if (overlay_debugging
)
1368 VALUE_ADDRESS (v
) = symbol_overlayed_address
1369 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
1371 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
1376 case LOC_REGPARM_ADDR
:
1379 int regno
= SYMBOL_VALUE (var
);
1384 b
= get_frame_block (frame
);
1386 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
1388 regval
= value_from_register (lookup_pointer_type (type
),
1393 error ("Value of register variable not available.");
1395 addr
= value_as_pointer (regval
);
1396 VALUE_LVAL (v
) = lval_memory
;
1400 regval
= value_from_register (type
, regno
, frame
);
1403 error ("Value of register variable not available.");
1409 case LOC_UNRESOLVED
:
1411 struct minimal_symbol
*msym
;
1413 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
1416 if (overlay_debugging
)
1417 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
1418 SYMBOL_BFD_SECTION (msym
));
1420 addr
= SYMBOL_VALUE_ADDRESS (msym
);
1424 case LOC_OPTIMIZED_OUT
:
1425 VALUE_LVAL (v
) = not_lval
;
1426 VALUE_OPTIMIZED_OUT (v
) = 1;
1430 error ("Cannot look up value of a botched symbol.");
1434 VALUE_ADDRESS (v
) = addr
;
1439 /* Return a value of type TYPE, stored in register REGNUM, in frame
1442 NOTE: returns NULL if register value is not available.
1443 Caller will check return value or die! */
1446 value_from_register (type
, regnum
, frame
)
1449 struct frame_info
*frame
;
1451 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
1454 value_ptr v
= allocate_value (type
);
1455 char *value_bytes
= 0;
1456 int value_bytes_copied
= 0;
1457 int num_storage_locs
;
1458 enum lval_type lval
;
1461 CHECK_TYPEDEF (type
);
1462 len
= TYPE_LENGTH (type
);
1464 VALUE_REGNO (v
) = regnum
;
1466 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
1467 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
1470 if (num_storage_locs
> 1
1471 #ifdef GDB_TARGET_IS_H8500
1472 || TYPE_CODE (type
) == TYPE_CODE_PTR
1476 /* Value spread across multiple storage locations. */
1479 int mem_stor
= 0, reg_stor
= 0;
1480 int mem_tracking
= 1;
1481 CORE_ADDR last_addr
= 0;
1482 CORE_ADDR first_addr
= 0;
1484 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
1486 /* Copy all of the data out, whereever it may be. */
1488 #ifdef GDB_TARGET_IS_H8500
1489 /* This piece of hideosity is required because the H8500 treats registers
1490 differently depending upon whether they are used as pointers or not. As a
1491 pointer, a register needs to have a page register tacked onto the front.
1492 An alternate way to do this would be to have gcc output different register
1493 numbers for the pointer & non-pointer form of the register. But, it
1494 doesn't, so we're stuck with this. */
1496 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1503 case R0_REGNUM
: case R1_REGNUM
: case R2_REGNUM
: case R3_REGNUM
:
1504 page_regnum
= SEG_D_REGNUM
;
1506 case R4_REGNUM
: case R5_REGNUM
:
1507 page_regnum
= SEG_E_REGNUM
;
1509 case R6_REGNUM
: case R7_REGNUM
:
1510 page_regnum
= SEG_T_REGNUM
;
1515 get_saved_register (value_bytes
+ 1,
1522 if (register_valid
[page_regnum
] == -1)
1523 return NULL
; /* register value not available */
1525 if (lval
== lval_register
)
1532 get_saved_register (value_bytes
+ 2,
1539 if (register_valid
[regnum
] == -1)
1540 return NULL
; /* register value not available */
1542 if (lval
== lval_register
)
1547 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
1552 #endif /* GDB_TARGET_IS_H8500 */
1553 for (local_regnum
= regnum
;
1554 value_bytes_copied
< len
;
1555 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
1558 get_saved_register (value_bytes
+ value_bytes_copied
,
1565 if (register_valid
[local_regnum
] == -1)
1566 return NULL
; /* register value not available */
1568 if (regnum
== local_regnum
)
1570 if (lval
== lval_register
)
1578 && (regnum
== local_regnum
1579 || addr
== last_addr
));
1584 if ((reg_stor
&& mem_stor
)
1585 || (mem_stor
&& !mem_tracking
))
1586 /* Mixed storage; all of the hassle we just went through was
1587 for some good purpose. */
1589 VALUE_LVAL (v
) = lval_reg_frame_relative
;
1590 VALUE_FRAME (v
) = FRAME_FP (frame
);
1591 VALUE_FRAME_REGNUM (v
) = regnum
;
1595 VALUE_LVAL (v
) = lval_memory
;
1596 VALUE_ADDRESS (v
) = first_addr
;
1600 VALUE_LVAL (v
) = lval_register
;
1601 VALUE_ADDRESS (v
) = first_addr
;
1604 fatal ("value_from_register: Value not stored anywhere!");
1606 VALUE_OPTIMIZED_OUT (v
) = optim
;
1608 /* Any structure stored in more than one register will always be
1609 an integral number of registers. Otherwise, you'd need to do
1610 some fiddling with the last register copied here for little
1613 /* Copy into the contents section of the value. */
1614 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
1616 /* Finally do any conversion necessary when extracting this
1617 type from more than one register. */
1618 #ifdef REGISTER_CONVERT_TO_TYPE
1619 REGISTER_CONVERT_TO_TYPE(regnum
, type
, VALUE_CONTENTS_RAW(v
));
1624 /* Data is completely contained within a single register. Locate the
1625 register's contents in a real register or in core;
1626 read the data in raw format. */
1628 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
1630 if (register_valid
[regnum
] == -1)
1631 return NULL
; /* register value not available */
1633 VALUE_OPTIMIZED_OUT (v
) = optim
;
1634 VALUE_LVAL (v
) = lval
;
1635 VALUE_ADDRESS (v
) = addr
;
1637 /* Convert raw data to virtual format if necessary. */
1639 if (REGISTER_CONVERTIBLE (regnum
))
1641 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
1642 raw_buffer
, VALUE_CONTENTS_RAW (v
));
1646 /* Raw and virtual formats are the same for this register. */
1648 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
&& len
< REGISTER_RAW_SIZE (regnum
))
1650 /* Big-endian, and we want less than full size. */
1651 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
1654 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
1660 /* Given a struct symbol for a variable or function,
1661 and a stack frame id,
1662 return a (pointer to a) struct value containing the properly typed
1666 locate_var_value (var
, frame
)
1667 register struct symbol
*var
;
1668 struct frame_info
*frame
;
1671 struct type
*type
= SYMBOL_TYPE (var
);
1672 value_ptr lazy_value
;
1674 /* Evaluate it first; if the result is a memory address, we're fine.
1675 Lazy evaluation pays off here. */
1677 lazy_value
= read_var_value (var
, frame
);
1678 if (lazy_value
== 0)
1679 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
1681 if (VALUE_LAZY (lazy_value
)
1682 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1686 addr
= VALUE_ADDRESS (lazy_value
);
1687 val
= value_from_longest (lookup_pointer_type (type
), (LONGEST
) addr
);
1688 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
1692 /* Not a memory address; check what the problem was. */
1693 switch (VALUE_LVAL (lazy_value
))
1696 case lval_reg_frame_relative
:
1697 error ("Address requested for identifier \"%s\" which is in a register.",
1698 SYMBOL_SOURCE_NAME (var
));
1702 error ("Can't take address of \"%s\" which isn't an lvalue.",
1703 SYMBOL_SOURCE_NAME (var
));
1706 return 0; /* For lint -- never reached */
1710 static void build_findvar
PARAMS ((void));
1714 /* We allocate some extra slop since we do a lot of memcpy's around
1715 `registers', and failing-soft is better than failing hard. */
1716 int sizeof_registers
= REGISTER_BYTES
+ /* SLOP */ 256;
1717 int sizeof_register_valid
= NUM_REGS
* sizeof (*register_valid
);
1718 registers
= xmalloc (sizeof_registers
);
1719 memset (registers
, 0, sizeof_registers
);
1720 register_valid
= xmalloc (sizeof_register_valid
);
1721 memset (register_valid
, 0, sizeof_register_valid
);
1724 void _initialize_findvar
PARAMS ((void));
1726 _initialize_findvar ()
1730 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1731 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1732 register_gdbarch_swap (NULL
, 0, build_findvar
);