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 void write_register_gen (int, char *);
48 read_relative_register_raw_bytes_for_frame (int regnum
,
50 struct frame_info
*frame
);
52 /* Basic byte-swapping routines. GDB has needed these for a long time...
53 All extract a target-format integer at ADDR which is LEN bytes long. */
55 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
56 /* 8 bit characters are a pretty safe assumption these days, so we
57 assume it throughout all these swapping routines. If we had to deal with
58 9 bit characters, we would need to make len be in bits and would have
59 to re-write these routines... */
64 extract_signed_integer (void *addr
, int len
)
68 unsigned char *startaddr
= (unsigned char *) addr
;
69 unsigned char *endaddr
= startaddr
+ len
;
71 if (len
> (int) sizeof (LONGEST
))
73 That operation is not available on integers of more than %d bytes.",
76 /* Start at the most significant end of the integer, and work towards
77 the least significant. */
78 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
81 /* Do the sign extension once at the start. */
82 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
83 for (++p
; p
< endaddr
; ++p
)
84 retval
= (retval
<< 8) | *p
;
89 /* Do the sign extension once at the start. */
90 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
91 for (--p
; p
>= startaddr
; --p
)
92 retval
= (retval
<< 8) | *p
;
98 extract_unsigned_integer (void *addr
, int 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 (void *addr
, int orig_len
, LONGEST
*pval
)
134 char *p
, *first_addr
;
138 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
140 for (p
= (char *) addr
;
141 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
153 first_addr
= (char *) addr
;
154 for (p
= (char *) addr
+ orig_len
- 1;
155 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
165 if (len
<= (int) sizeof (LONGEST
))
167 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
176 /* Treat the LEN bytes at ADDR as a target-format address, and return
177 that address. ADDR is a buffer in the GDB process, not in the
180 This function should only be used by target-specific code. It
181 assumes that a pointer has the same representation as that thing's
182 address represented as an integer. Some machines use word
183 addresses, or similarly munged things, for certain types of
184 pointers, so that assumption doesn't hold everywhere.
186 Common code should use extract_typed_address instead, or something
187 else based on POINTER_TO_ADDRESS. */
190 extract_address (void *addr
, int len
)
192 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
193 whether we want this to be true eventually. */
194 return (CORE_ADDR
) extract_unsigned_integer (addr
, len
);
198 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
199 address it represents. */
201 extract_typed_address (void *buf
, struct type
*type
)
203 if (TYPE_CODE (type
) != TYPE_CODE_PTR
204 && TYPE_CODE (type
) != TYPE_CODE_REF
)
205 internal_error ("findvar.c (extract_typed_address): "
206 "type is not a pointer or reference");
208 return POINTER_TO_ADDRESS (type
, buf
);
213 store_signed_integer (void *addr
, int len
, LONGEST val
)
216 unsigned char *startaddr
= (unsigned char *) addr
;
217 unsigned char *endaddr
= startaddr
+ len
;
219 /* Start at the least significant end of the integer, and work towards
220 the most significant. */
221 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
223 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
231 for (p
= startaddr
; p
< endaddr
; ++p
)
240 store_unsigned_integer (void *addr
, int len
, ULONGEST val
)
243 unsigned char *startaddr
= (unsigned char *) addr
;
244 unsigned char *endaddr
= startaddr
+ len
;
246 /* Start at the least significant end of the integer, and work towards
247 the most significant. */
248 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
250 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
258 for (p
= startaddr
; p
< endaddr
; ++p
)
266 /* Store the address VAL as a LEN-byte value in target byte order at
267 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
269 This function should only be used by target-specific code. It
270 assumes that a pointer has the same representation as that thing's
271 address represented as an integer. Some machines use word
272 addresses, or similarly munged things, for certain types of
273 pointers, so that assumption doesn't hold everywhere.
275 Common code should use store_typed_address instead, or something else
276 based on ADDRESS_TO_POINTER. */
278 store_address (void *addr
, int len
, LONGEST val
)
280 store_unsigned_integer (addr
, len
, val
);
284 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
287 store_typed_address (void *buf
, struct type
*type
, CORE_ADDR addr
)
289 if (TYPE_CODE (type
) != TYPE_CODE_PTR
290 && TYPE_CODE (type
) != TYPE_CODE_REF
)
291 internal_error ("findvar.c (store_typed_address): "
292 "type is not a pointer or reference");
294 ADDRESS_TO_POINTER (type
, buf
, addr
);
300 /* Extract a floating-point number from a target-order byte-stream at ADDR.
301 Returns the value as type DOUBLEST.
303 If the host and target formats agree, we just copy the raw data into the
304 appropriate type of variable and return, letting the host increase precision
305 as necessary. Otherwise, we call the conversion routine and let it do the
309 extract_floating (void *addr
, int len
)
313 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
315 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
319 memcpy (&retval
, addr
, sizeof (retval
));
323 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
325 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
327 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
331 memcpy (&retval
, addr
, sizeof (retval
));
335 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
337 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
339 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
343 memcpy (&retval
, addr
, sizeof (retval
));
347 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
351 error ("Can't deal with a floating point number of %d bytes.", len
);
358 store_floating (void *addr
, int len
, DOUBLEST val
)
360 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
362 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
364 float floatval
= val
;
366 memcpy (addr
, &floatval
, sizeof (floatval
));
369 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
371 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
373 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
375 double doubleval
= val
;
377 memcpy (addr
, &doubleval
, sizeof (doubleval
));
380 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
382 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
384 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
385 memcpy (addr
, &val
, sizeof (val
));
387 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
391 error ("Can't deal with a floating point number of %d bytes.", len
);
396 /* Return the address in which frame FRAME's value of register REGNUM
397 has been saved in memory. Or return zero if it has not been saved.
398 If REGNUM specifies the SP, the value we return is actually
399 the SP value, not an address where it was saved. */
402 find_saved_register (frame
, regnum
)
403 struct frame_info
*frame
;
406 register struct frame_info
*frame1
= NULL
;
407 register CORE_ADDR addr
= 0;
409 if (frame
== NULL
) /* No regs saved if want current frame */
412 #ifdef HAVE_REGISTER_WINDOWS
413 /* We assume that a register in a register window will only be saved
414 in one place (since the name changes and/or disappears as you go
415 towards inner frames), so we only call get_frame_saved_regs on
416 the current frame. This is directly in contradiction to the
417 usage below, which assumes that registers used in a frame must be
418 saved in a lower (more interior) frame. This change is a result
419 of working on a register window machine; get_frame_saved_regs
420 always returns the registers saved within a frame, within the
421 context (register namespace) of that frame. */
423 /* However, note that we don't want this to return anything if
424 nothing is saved (if there's a frame inside of this one). Also,
425 callers to this routine asking for the stack pointer want the
426 stack pointer saved for *this* frame; this is returned from the
429 if (REGISTER_IN_WINDOW_P (regnum
))
431 frame1
= get_next_frame (frame
);
433 return 0; /* Registers of this frame are active. */
435 /* Get the SP from the next frame in; it will be this
437 if (regnum
!= SP_REGNUM
)
440 FRAME_INIT_SAVED_REGS (frame1
);
441 return frame1
->saved_regs
[regnum
]; /* ... which might be zero */
443 #endif /* HAVE_REGISTER_WINDOWS */
445 /* Note that this next routine assumes that registers used in
446 frame x will be saved only in the frame that x calls and
447 frames interior to it. This is not true on the sparc, but the
448 above macro takes care of it, so we should be all right. */
452 frame1
= get_prev_frame (frame1
);
453 if (frame1
== 0 || frame1
== frame
)
455 FRAME_INIT_SAVED_REGS (frame1
);
456 if (frame1
->saved_regs
[regnum
])
457 addr
= frame1
->saved_regs
[regnum
];
463 /* Find register number REGNUM relative to FRAME and put its (raw,
464 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
465 variable was optimized out (and thus can't be fetched). Set *LVAL
466 to lval_memory, lval_register, or not_lval, depending on whether
467 the value was fetched from memory, from a register, or in a strange
468 and non-modifiable way (e.g. a frame pointer which was calculated
469 rather than fetched). Set *ADDRP to the address, either in memory
470 on as a REGISTER_BYTE offset into the registers array.
472 Note that this implementation never sets *LVAL to not_lval. But
473 it can be replaced by defining GET_SAVED_REGISTER and supplying
476 The argument RAW_BUFFER must point to aligned memory. */
479 default_get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
483 struct frame_info
*frame
;
485 enum lval_type
*lval
;
489 if (!target_has_registers
)
490 error ("No registers.");
492 /* Normal systems don't optimize out things with register numbers. */
493 if (optimized
!= NULL
)
495 addr
= find_saved_register (frame
, regnum
);
500 if (regnum
== SP_REGNUM
)
502 if (raw_buffer
!= NULL
)
504 /* Put it back in target format. */
505 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
),
512 if (raw_buffer
!= NULL
)
513 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
518 *lval
= lval_register
;
519 addr
= REGISTER_BYTE (regnum
);
520 if (raw_buffer
!= NULL
)
521 read_register_gen (regnum
, raw_buffer
);
527 #if !defined (GET_SAVED_REGISTER)
528 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
529 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
532 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
536 struct frame_info
*frame
;
538 enum lval_type
*lval
;
540 GET_SAVED_REGISTER (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
);
543 /* Copy the bytes of register REGNUM, relative to the input stack frame,
544 into our memory at MYADDR, in target byte order.
545 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
547 Returns 1 if could not be read, 0 if could. */
550 read_relative_register_raw_bytes_for_frame (regnum
, myaddr
, frame
)
553 struct frame_info
*frame
;
556 if (regnum
== FP_REGNUM
&& frame
)
558 /* Put it back in target format. */
559 store_address (myaddr
, REGISTER_RAW_SIZE (FP_REGNUM
),
560 (LONGEST
) FRAME_FP (frame
));
565 get_saved_register (myaddr
, &optim
, (CORE_ADDR
*) NULL
, frame
,
566 regnum
, (enum lval_type
*) NULL
);
568 if (register_valid
[regnum
] < 0)
569 return 1; /* register value not available */
574 /* Copy the bytes of register REGNUM, relative to the current stack frame,
575 into our memory at MYADDR, in target byte order.
576 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
578 Returns 1 if could not be read, 0 if could. */
581 read_relative_register_raw_bytes (regnum
, myaddr
)
585 return read_relative_register_raw_bytes_for_frame (regnum
, myaddr
,
589 /* Return a `value' with the contents of register REGNUM
590 in its virtual format, with the type specified by
591 REGISTER_VIRTUAL_TYPE.
593 NOTE: returns NULL if register value is not available.
594 Caller will check return value or die! */
597 value_of_register (regnum
)
602 register value_ptr reg_val
;
603 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
606 get_saved_register (raw_buffer
, &optim
, &addr
,
607 selected_frame
, regnum
, &lval
);
609 if (register_valid
[regnum
] < 0)
610 return NULL
; /* register value not available */
612 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
614 /* Convert raw data to virtual format if necessary. */
616 if (REGISTER_CONVERTIBLE (regnum
))
618 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
619 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
621 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
622 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
623 REGISTER_RAW_SIZE (regnum
));
625 internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
626 REGISTER_NAME (regnum
),
628 REGISTER_RAW_SIZE (regnum
),
629 REGISTER_VIRTUAL_SIZE (regnum
));
630 VALUE_LVAL (reg_val
) = lval
;
631 VALUE_ADDRESS (reg_val
) = addr
;
632 VALUE_REGNO (reg_val
) = regnum
;
633 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
637 /* Low level examining and depositing of registers.
639 The caller is responsible for making
640 sure that the inferior is stopped before calling the fetching routines,
641 or it will get garbage. (a change from GDB version 3, in which
642 the caller got the value from the last stop). */
644 /* Contents and state of the registers (in target byte order). */
648 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
649 register value was not available. */
651 signed char *register_valid
;
653 /* The thread/process associated with the current set of registers. For now,
654 -1 is special, and means `no current process'. */
655 int registers_pid
= -1;
657 /* Indicate that registers may have changed, so invalidate the cache. */
663 int numregs
= ARCH_NUM_REGS
;
667 /* Force cleanup of any alloca areas if using C alloca instead of
668 a builtin alloca. This particular call is used to clean up
669 areas allocated by low level target code which may build up
670 during lengthy interactions between gdb and the target before
671 gdb gives control to the user (ie watchpoints). */
674 for (i
= 0; i
< numregs
; i
++)
675 register_valid
[i
] = 0;
677 if (registers_changed_hook
)
678 registers_changed_hook ();
681 /* Indicate that all registers have been fetched, so mark them all valid. */
686 int numregs
= ARCH_NUM_REGS
;
687 for (i
= 0; i
< numregs
; i
++)
688 register_valid
[i
] = 1;
691 /* read_register_bytes and write_register_bytes are generally a *BAD*
692 idea. They are inefficient because they need to check for partial
693 updates, which can only be done by scanning through all of the
694 registers and seeing if the bytes that are being read/written fall
695 inside of an invalid register. [The main reason this is necessary
696 is that register sizes can vary, so a simple index won't suffice.]
697 It is far better to call read_register_gen and write_register_gen
698 if you want to get at the raw register contents, as it only takes a
699 regno as an argument, and therefore can't do a partial register
702 Prior to the recent fixes to check for partial updates, both read
703 and write_register_bytes always checked to see if any registers
704 were stale, and then called target_fetch_registers (-1) to update
705 the whole set. This caused really slowed things down for remote
708 /* Copy INLEN bytes of consecutive data from registers
709 starting with the INREGBYTE'th byte of register data
710 into memory at MYADDR. */
713 read_register_bytes (inregbyte
, myaddr
, inlen
)
718 int inregend
= inregbyte
+ inlen
;
721 if (registers_pid
!= inferior_pid
)
723 registers_changed ();
724 registers_pid
= inferior_pid
;
727 /* See if we are trying to read bytes from out-of-date registers. If so,
728 update just those registers. */
730 for (regno
= 0; regno
< NUM_REGS
; regno
++)
732 int regstart
, regend
;
734 if (register_valid
[regno
])
737 if (REGISTER_NAME (regno
) == NULL
|| *REGISTER_NAME (regno
) == '\0')
740 regstart
= REGISTER_BYTE (regno
);
741 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
743 if (regend
<= inregbyte
|| inregend
<= regstart
)
744 /* The range the user wants to read doesn't overlap with regno. */
747 /* We've found an invalid register where at least one byte will be read.
748 Update it from the target. */
749 target_fetch_registers (regno
);
751 if (!register_valid
[regno
])
752 error ("read_register_bytes: Couldn't update register %d.", regno
);
756 memcpy (myaddr
, ®isters
[inregbyte
], inlen
);
759 /* Read register REGNO into memory at MYADDR, which must be large enough
760 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
761 If the register is known to be the size of a CORE_ADDR or smaller,
762 read_register can be used instead. */
764 read_register_gen (regno
, myaddr
)
768 if (registers_pid
!= inferior_pid
)
770 registers_changed ();
771 registers_pid
= inferior_pid
;
774 if (!register_valid
[regno
])
775 target_fetch_registers (regno
);
776 memcpy (myaddr
, ®isters
[REGISTER_BYTE (regno
)],
777 REGISTER_RAW_SIZE (regno
));
780 /* Write register REGNO at MYADDR to the target. MYADDR points at
781 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
784 write_register_gen (regno
, myaddr
)
790 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
791 the registers array if something writes to this register. */
792 if (CANNOT_STORE_REGISTER (regno
))
795 if (registers_pid
!= inferior_pid
)
797 registers_changed ();
798 registers_pid
= inferior_pid
;
801 size
= REGISTER_RAW_SIZE (regno
);
803 /* If we have a valid copy of the register, and new value == old value,
804 then don't bother doing the actual store. */
806 if (register_valid
[regno
]
807 && memcmp (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
) == 0)
810 target_prepare_to_store ();
812 memcpy (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
);
814 register_valid
[regno
] = 1;
816 target_store_registers (regno
);
819 /* Copy INLEN bytes of consecutive data from memory at MYADDR
820 into registers starting with the MYREGSTART'th byte of register data. */
823 write_register_bytes (myregstart
, myaddr
, inlen
)
828 int myregend
= myregstart
+ inlen
;
831 target_prepare_to_store ();
833 /* Scan through the registers updating any that are covered by the range
834 myregstart<=>myregend using write_register_gen, which does nice things
835 like handling threads, and avoiding updates when the new and old contents
838 for (regno
= 0; regno
< NUM_REGS
; regno
++)
840 int regstart
, regend
;
842 regstart
= REGISTER_BYTE (regno
);
843 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
845 /* Is this register completely outside the range the user is writing? */
846 if (myregend
<= regstart
|| regend
<= myregstart
)
849 /* Is this register completely within the range the user is writing? */
850 else if (myregstart
<= regstart
&& regend
<= myregend
)
851 write_register_gen (regno
, myaddr
+ (regstart
- myregstart
));
853 /* The register partially overlaps the range being written. */
856 char regbuf
[MAX_REGISTER_RAW_SIZE
];
857 /* What's the overlap between this register's bytes and
858 those the caller wants to write? */
859 int overlapstart
= max (regstart
, myregstart
);
860 int overlapend
= min (regend
, myregend
);
862 /* We may be doing a partial update of an invalid register.
863 Update it from the target before scribbling on it. */
864 read_register_gen (regno
, regbuf
);
866 memcpy (registers
+ overlapstart
,
867 myaddr
+ (overlapstart
- myregstart
),
868 overlapend
- overlapstart
);
870 target_store_registers (regno
);
876 /* Return the raw contents of register REGNO, regarding it as an integer. */
877 /* This probably should be returning LONGEST rather than CORE_ADDR. */
880 read_register (regno
)
883 if (registers_pid
!= inferior_pid
)
885 registers_changed ();
886 registers_pid
= inferior_pid
;
889 if (!register_valid
[regno
])
890 target_fetch_registers (regno
);
893 extract_unsigned_integer (®isters
[REGISTER_BYTE (regno
)],
894 REGISTER_RAW_SIZE (regno
)));
898 read_register_pid (regno
, pid
)
904 if (pid
== inferior_pid
)
905 return read_register (regno
);
907 save_pid
= inferior_pid
;
911 retval
= read_register (regno
);
913 inferior_pid
= save_pid
;
918 /* Store VALUE, into the raw contents of register number REGNO.
919 This should probably write a LONGEST rather than a CORE_ADDR */
922 write_register (regno
, val
)
929 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
930 the registers array if something writes to this register. */
931 if (CANNOT_STORE_REGISTER (regno
))
934 if (registers_pid
!= inferior_pid
)
936 registers_changed ();
937 registers_pid
= inferior_pid
;
940 size
= REGISTER_RAW_SIZE (regno
);
942 store_signed_integer (buf
, size
, (LONGEST
) val
);
944 /* If we have a valid copy of the register, and new value == old value,
945 then don't bother doing the actual store. */
947 if (register_valid
[regno
]
948 && memcmp (®isters
[REGISTER_BYTE (regno
)], buf
, size
) == 0)
951 target_prepare_to_store ();
953 memcpy (®isters
[REGISTER_BYTE (regno
)], buf
, size
);
955 register_valid
[regno
] = 1;
957 target_store_registers (regno
);
961 write_register_pid (regno
, val
, pid
)
968 if (pid
== inferior_pid
)
970 write_register (regno
, val
);
974 save_pid
= inferior_pid
;
978 write_register (regno
, val
);
980 inferior_pid
= save_pid
;
983 /* Record that register REGNO contains VAL.
984 This is used when the value is obtained from the inferior or core dump,
985 so there is no need to store the value there.
987 If VAL is a NULL pointer, then it's probably an unsupported register. We
988 just set it's value to all zeros. We might want to record this fact, and
989 report it to the users of read_register and friends.
993 supply_register (regno
, val
)
998 if (registers_pid
!= inferior_pid
)
1000 registers_changed ();
1001 registers_pid
= inferior_pid
;
1005 register_valid
[regno
] = 1;
1007 memcpy (®isters
[REGISTER_BYTE (regno
)], val
, REGISTER_RAW_SIZE (regno
));
1009 memset (®isters
[REGISTER_BYTE (regno
)], '\000', REGISTER_RAW_SIZE (regno
));
1011 /* On some architectures, e.g. HPPA, there are a few stray bits in some
1012 registers, that the rest of the code would like to ignore. */
1013 #ifdef CLEAN_UP_REGISTER_VALUE
1014 CLEAN_UP_REGISTER_VALUE (regno
, ®isters
[REGISTER_BYTE (regno
)]);
1019 /* This routine is getting awfully cluttered with #if's. It's probably
1020 time to turn this into READ_PC and define it in the tm.h file.
1023 1999-06-08: The following were re-written so that it assumes the
1024 existance of a TARGET_READ_PC et.al. macro. A default generic
1025 version of that macro is made available where needed.
1027 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1028 by the multi-arch framework, it will eventually be possible to
1029 eliminate the intermediate read_pc_pid(). The client would call
1030 TARGET_READ_PC directly. (cagney). */
1032 #ifndef TARGET_READ_PC
1033 #define TARGET_READ_PC generic_target_read_pc
1037 generic_target_read_pc (int pid
)
1042 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, pid
));
1046 internal_error ("generic_target_read_pc");
1054 int saved_inferior_pid
;
1057 /* In case pid != inferior_pid. */
1058 saved_inferior_pid
= inferior_pid
;
1061 pc_val
= TARGET_READ_PC (pid
);
1063 inferior_pid
= saved_inferior_pid
;
1070 return read_pc_pid (inferior_pid
);
1073 #ifndef TARGET_WRITE_PC
1074 #define TARGET_WRITE_PC generic_target_write_pc
1078 generic_target_write_pc (pc
, pid
)
1084 write_register_pid (PC_REGNUM
, pc
, pid
);
1085 if (NPC_REGNUM
>= 0)
1086 write_register_pid (NPC_REGNUM
, pc
+ 4, pid
);
1087 if (NNPC_REGNUM
>= 0)
1088 write_register_pid (NNPC_REGNUM
, pc
+ 8, pid
);
1090 internal_error ("generic_target_write_pc");
1095 write_pc_pid (pc
, pid
)
1099 int saved_inferior_pid
;
1101 /* In case pid != inferior_pid. */
1102 saved_inferior_pid
= inferior_pid
;
1105 TARGET_WRITE_PC (pc
, pid
);
1107 inferior_pid
= saved_inferior_pid
;
1114 write_pc_pid (pc
, inferior_pid
);
1117 /* Cope with strage ways of getting to the stack and frame pointers */
1119 #ifndef TARGET_READ_SP
1120 #define TARGET_READ_SP generic_target_read_sp
1124 generic_target_read_sp ()
1128 return read_register (SP_REGNUM
);
1130 internal_error ("generic_target_read_sp");
1136 return TARGET_READ_SP ();
1139 #ifndef TARGET_WRITE_SP
1140 #define TARGET_WRITE_SP generic_target_write_sp
1144 generic_target_write_sp (val
)
1150 write_register (SP_REGNUM
, val
);
1154 internal_error ("generic_target_write_sp");
1161 TARGET_WRITE_SP (val
);
1164 #ifndef TARGET_READ_FP
1165 #define TARGET_READ_FP generic_target_read_fp
1169 generic_target_read_fp ()
1173 return read_register (FP_REGNUM
);
1175 internal_error ("generic_target_read_fp");
1181 return TARGET_READ_FP ();
1184 #ifndef TARGET_WRITE_FP
1185 #define TARGET_WRITE_FP generic_target_write_fp
1189 generic_target_write_fp (val
)
1195 write_register (FP_REGNUM
, val
);
1199 internal_error ("generic_target_write_fp");
1206 TARGET_WRITE_FP (val
);
1210 /* Given a pointer of type TYPE in target form in BUF, return the
1211 address it represents. */
1213 unsigned_pointer_to_address (struct type
*type
, void *buf
)
1215 return extract_address (buf
, TYPE_LENGTH (type
));
1219 signed_pointer_to_address (struct type
*type
, void *buf
)
1221 return extract_signed_integer (buf
, TYPE_LENGTH (type
));
1224 /* Given an address, store it as a pointer of type TYPE in target
1227 unsigned_address_to_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
1229 store_address (buf
, TYPE_LENGTH (type
), addr
);
1233 address_to_signed_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
1235 store_signed_integer (buf
, TYPE_LENGTH (type
), addr
);
1238 /* Will calling read_var_value or locate_var_value on SYM end
1239 up caring what frame it is being evaluated relative to? SYM must
1242 symbol_read_needs_frame (sym
)
1245 switch (SYMBOL_CLASS (sym
))
1247 /* All cases listed explicitly so that gcc -Wall will detect it if
1248 we failed to consider one. */
1253 case LOC_REGPARM_ADDR
:
1257 case LOC_BASEREG_ARG
:
1258 case LOC_THREAD_LOCAL_STATIC
:
1268 /* Getting the address of a label can be done independently of the block,
1269 even if some *uses* of that address wouldn't work so well without
1273 case LOC_CONST_BYTES
:
1274 case LOC_UNRESOLVED
:
1275 case LOC_OPTIMIZED_OUT
:
1281 /* Given a struct symbol for a variable,
1282 and a stack frame id, read the value of the variable
1283 and return a (pointer to a) struct value containing the value.
1284 If the variable cannot be found, return a zero pointer.
1285 If FRAME is NULL, use the selected_frame. */
1288 read_var_value (var
, frame
)
1289 register struct symbol
*var
;
1290 struct frame_info
*frame
;
1292 register value_ptr v
;
1293 struct type
*type
= SYMBOL_TYPE (var
);
1297 v
= allocate_value (type
);
1298 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
1299 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
1301 len
= TYPE_LENGTH (type
);
1304 frame
= selected_frame
;
1306 switch (SYMBOL_CLASS (var
))
1309 /* Put the constant back in target format. */
1310 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
1311 (LONGEST
) SYMBOL_VALUE (var
));
1312 VALUE_LVAL (v
) = not_lval
;
1316 /* Put the constant back in target format. */
1317 if (overlay_debugging
)
1320 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1321 SYMBOL_BFD_SECTION (var
));
1322 store_typed_address (VALUE_CONTENTS_RAW (v
), type
, addr
);
1325 store_typed_address (VALUE_CONTENTS_RAW (v
), type
,
1326 SYMBOL_VALUE_ADDRESS (var
));
1327 VALUE_LVAL (v
) = not_lval
;
1330 case LOC_CONST_BYTES
:
1333 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
1334 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
1335 VALUE_LVAL (v
) = not_lval
;
1340 if (overlay_debugging
)
1341 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1342 SYMBOL_BFD_SECTION (var
));
1344 addr
= SYMBOL_VALUE_ADDRESS (var
);
1348 /* The import slot does not have a real address in it from the
1349 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1350 execution yet, so check for that. */
1351 if (!target_has_execution
)
1353 Attempt to access variable defined in different shared object or load module when\n\
1354 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1356 addr
= SYMBOL_VALUE_ADDRESS (var
);
1357 addr
= read_memory_unsigned_integer
1358 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1364 addr
= FRAME_ARGS_ADDRESS (frame
);
1367 addr
+= SYMBOL_VALUE (var
);
1373 addr
= FRAME_ARGS_ADDRESS (frame
);
1376 addr
+= SYMBOL_VALUE (var
);
1377 addr
= read_memory_unsigned_integer
1378 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1385 addr
= FRAME_LOCALS_ADDRESS (frame
);
1386 addr
+= SYMBOL_VALUE (var
);
1390 case LOC_BASEREG_ARG
:
1392 char buf
[MAX_REGISTER_RAW_SIZE
];
1393 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1395 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1396 addr
+= SYMBOL_VALUE (var
);
1400 case LOC_THREAD_LOCAL_STATIC
:
1402 char buf
[MAX_REGISTER_RAW_SIZE
];
1404 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1406 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1407 addr
+= SYMBOL_VALUE (var
);
1412 error ("Cannot look up value of a typedef");
1416 if (overlay_debugging
)
1417 VALUE_ADDRESS (v
) = symbol_overlayed_address
1418 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
1420 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
1425 case LOC_REGPARM_ADDR
:
1428 int regno
= SYMBOL_VALUE (var
);
1433 b
= get_frame_block (frame
);
1435 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
1437 regval
= value_from_register (lookup_pointer_type (type
),
1442 error ("Value of register variable not available.");
1444 addr
= value_as_pointer (regval
);
1445 VALUE_LVAL (v
) = lval_memory
;
1449 regval
= value_from_register (type
, regno
, frame
);
1452 error ("Value of register variable not available.");
1458 case LOC_UNRESOLVED
:
1460 struct minimal_symbol
*msym
;
1462 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
1465 if (overlay_debugging
)
1466 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
1467 SYMBOL_BFD_SECTION (msym
));
1469 addr
= SYMBOL_VALUE_ADDRESS (msym
);
1473 case LOC_OPTIMIZED_OUT
:
1474 VALUE_LVAL (v
) = not_lval
;
1475 VALUE_OPTIMIZED_OUT (v
) = 1;
1479 error ("Cannot look up value of a botched symbol.");
1483 VALUE_ADDRESS (v
) = addr
;
1488 /* Return a value of type TYPE, stored in register REGNUM, in frame
1491 NOTE: returns NULL if register value is not available.
1492 Caller will check return value or die! */
1495 value_from_register (type
, regnum
, frame
)
1498 struct frame_info
*frame
;
1500 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
1503 value_ptr v
= allocate_value (type
);
1504 char *value_bytes
= 0;
1505 int value_bytes_copied
= 0;
1506 int num_storage_locs
;
1507 enum lval_type lval
;
1510 CHECK_TYPEDEF (type
);
1511 len
= TYPE_LENGTH (type
);
1513 /* Pointers on D10V are really only 16 bits,
1514 but we lie to gdb elsewhere... */
1515 if (GDB_TARGET_IS_D10V
&& TYPE_CODE (type
) == TYPE_CODE_PTR
)
1518 VALUE_REGNO (v
) = regnum
;
1520 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
1521 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
1524 if (num_storage_locs
> 1
1525 #ifdef GDB_TARGET_IS_H8500
1526 || TYPE_CODE (type
) == TYPE_CODE_PTR
1530 /* Value spread across multiple storage locations. */
1533 int mem_stor
= 0, reg_stor
= 0;
1534 int mem_tracking
= 1;
1535 CORE_ADDR last_addr
= 0;
1536 CORE_ADDR first_addr
= 0;
1538 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
1540 /* Copy all of the data out, whereever it may be. */
1542 #ifdef GDB_TARGET_IS_H8500
1543 /* This piece of hideosity is required because the H8500 treats registers
1544 differently depending upon whether they are used as pointers or not. As a
1545 pointer, a register needs to have a page register tacked onto the front.
1546 An alternate way to do this would be to have gcc output different register
1547 numbers for the pointer & non-pointer form of the register. But, it
1548 doesn't, so we're stuck with this. */
1550 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1561 page_regnum
= SEG_D_REGNUM
;
1565 page_regnum
= SEG_E_REGNUM
;
1569 page_regnum
= SEG_T_REGNUM
;
1574 get_saved_register (value_bytes
+ 1,
1581 if (register_valid
[page_regnum
] == -1)
1582 return NULL
; /* register value not available */
1584 if (lval
== lval_register
)
1591 get_saved_register (value_bytes
+ 2,
1598 if (register_valid
[regnum
] == -1)
1599 return NULL
; /* register value not available */
1601 if (lval
== lval_register
)
1606 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
1611 #endif /* GDB_TARGET_IS_H8500 */
1612 for (local_regnum
= regnum
;
1613 value_bytes_copied
< len
;
1614 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
1617 get_saved_register (value_bytes
+ value_bytes_copied
,
1624 if (register_valid
[local_regnum
] == -1)
1625 return NULL
; /* register value not available */
1627 if (regnum
== local_regnum
)
1629 if (lval
== lval_register
)
1637 && (regnum
== local_regnum
1638 || addr
== last_addr
));
1643 if ((reg_stor
&& mem_stor
)
1644 || (mem_stor
&& !mem_tracking
))
1645 /* Mixed storage; all of the hassle we just went through was
1646 for some good purpose. */
1648 VALUE_LVAL (v
) = lval_reg_frame_relative
;
1649 VALUE_FRAME (v
) = FRAME_FP (frame
);
1650 VALUE_FRAME_REGNUM (v
) = regnum
;
1654 VALUE_LVAL (v
) = lval_memory
;
1655 VALUE_ADDRESS (v
) = first_addr
;
1659 VALUE_LVAL (v
) = lval_register
;
1660 VALUE_ADDRESS (v
) = first_addr
;
1663 internal_error ("value_from_register: Value not stored anywhere!");
1665 VALUE_OPTIMIZED_OUT (v
) = optim
;
1667 /* Any structure stored in more than one register will always be
1668 an integral number of registers. Otherwise, you'd need to do
1669 some fiddling with the last register copied here for little
1672 /* Copy into the contents section of the value. */
1673 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
1675 /* Finally do any conversion necessary when extracting this
1676 type from more than one register. */
1677 #ifdef REGISTER_CONVERT_TO_TYPE
1678 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
1683 /* Data is completely contained within a single register. Locate the
1684 register's contents in a real register or in core;
1685 read the data in raw format. */
1687 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
1689 if (register_valid
[regnum
] == -1)
1690 return NULL
; /* register value not available */
1692 VALUE_OPTIMIZED_OUT (v
) = optim
;
1693 VALUE_LVAL (v
) = lval
;
1694 VALUE_ADDRESS (v
) = addr
;
1696 /* Convert raw data to virtual format if necessary. */
1698 if (REGISTER_CONVERTIBLE (regnum
))
1700 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
1701 raw_buffer
, VALUE_CONTENTS_RAW (v
));
1705 /* Raw and virtual formats are the same for this register. */
1707 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
&& len
< REGISTER_RAW_SIZE (regnum
))
1709 /* Big-endian, and we want less than full size. */
1710 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
1713 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
1716 if (GDB_TARGET_IS_D10V
1717 && TYPE_CODE (type
) == TYPE_CODE_PTR
)
1720 unsigned short snum
;
1722 snum
= (unsigned short)
1723 extract_unsigned_integer (VALUE_CONTENTS_RAW (v
), 2);
1725 if (TYPE_TARGET_TYPE (type
) /* pointer to function */
1726 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_FUNC
))
1727 num
= D10V_MAKE_IADDR (snum
);
1728 else /* pointer to data */
1729 num
= D10V_MAKE_DADDR (snum
);
1731 store_address (VALUE_CONTENTS_RAW (v
), 4, num
);
1737 /* Given a struct symbol for a variable or function,
1738 and a stack frame id,
1739 return a (pointer to a) struct value containing the properly typed
1743 locate_var_value (var
, frame
)
1744 register struct symbol
*var
;
1745 struct frame_info
*frame
;
1748 struct type
*type
= SYMBOL_TYPE (var
);
1749 value_ptr lazy_value
;
1751 /* Evaluate it first; if the result is a memory address, we're fine.
1752 Lazy evaluation pays off here. */
1754 lazy_value
= read_var_value (var
, frame
);
1755 if (lazy_value
== 0)
1756 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
1758 if (VALUE_LAZY (lazy_value
)
1759 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1763 addr
= VALUE_ADDRESS (lazy_value
);
1764 val
= value_from_pointer (lookup_pointer_type (type
), addr
);
1765 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
1769 /* Not a memory address; check what the problem was. */
1770 switch (VALUE_LVAL (lazy_value
))
1773 case lval_reg_frame_relative
:
1774 error ("Address requested for identifier \"%s\" which is in a register.",
1775 SYMBOL_SOURCE_NAME (var
));
1779 error ("Can't take address of \"%s\" which isn't an lvalue.",
1780 SYMBOL_SOURCE_NAME (var
));
1783 return 0; /* For lint -- never reached */
1787 static void build_findvar (void);
1791 /* We allocate some extra slop since we do a lot of memcpy's around
1792 `registers', and failing-soft is better than failing hard. */
1793 int sizeof_registers
= REGISTER_BYTES
+ /* SLOP */ 256;
1794 int sizeof_register_valid
= NUM_REGS
* sizeof (*register_valid
);
1795 registers
= xmalloc (sizeof_registers
);
1796 memset (registers
, 0, sizeof_registers
);
1797 register_valid
= xmalloc (sizeof_register_valid
);
1798 memset (register_valid
, 0, sizeof_register_valid
);
1801 void _initialize_findvar (void);
1803 _initialize_findvar ()
1807 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1808 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1809 register_gdbarch_swap (NULL
, 0, build_findvar
);