1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
31 #include "gdb_string.h"
32 #include "gdb_assert.h"
33 #include "floatformat.h"
34 #include "symfile.h" /* for overlay functions */
36 #include "builtin-regs.h"
38 /* Basic byte-swapping routines. GDB has needed these for a long time...
39 All extract a target-format integer at ADDR which is LEN bytes long. */
41 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
42 /* 8 bit characters are a pretty safe assumption these days, so we
43 assume it throughout all these swapping routines. If we had to deal with
44 9 bit characters, we would need to make len be in bits and would have
45 to re-write these routines... */
50 extract_signed_integer (const void *addr
, int len
)
53 const unsigned char *p
;
54 const unsigned char *startaddr
= addr
;
55 const unsigned char *endaddr
= startaddr
+ len
;
57 if (len
> (int) sizeof (LONGEST
))
59 That operation is not available on integers of more than %d bytes.",
60 (int) sizeof (LONGEST
));
62 /* Start at the most significant end of the integer, and work towards
63 the least significant. */
64 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
67 /* Do the sign extension once at the start. */
68 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
69 for (++p
; p
< endaddr
; ++p
)
70 retval
= (retval
<< 8) | *p
;
75 /* Do the sign extension once at the start. */
76 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
77 for (--p
; p
>= startaddr
; --p
)
78 retval
= (retval
<< 8) | *p
;
84 extract_unsigned_integer (const void *addr
, int len
)
87 const unsigned char *p
;
88 const unsigned char *startaddr
= addr
;
89 const unsigned char *endaddr
= startaddr
+ len
;
91 if (len
> (int) sizeof (ULONGEST
))
93 That operation is not available on integers of more than %d bytes.",
94 (int) sizeof (ULONGEST
));
96 /* Start at the most significant end of the integer, and work towards
97 the least significant. */
99 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
101 for (p
= startaddr
; p
< endaddr
; ++p
)
102 retval
= (retval
<< 8) | *p
;
106 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
107 retval
= (retval
<< 8) | *p
;
112 /* Sometimes a long long unsigned integer can be extracted as a
113 LONGEST value. This is done so that we can print these values
114 better. If this integer can be converted to a LONGEST, this
115 function returns 1 and sets *PVAL. Otherwise it returns 0. */
118 extract_long_unsigned_integer (void *addr
, int orig_len
, LONGEST
*pval
)
120 char *p
, *first_addr
;
124 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
126 for (p
= (char *) addr
;
127 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
139 first_addr
= (char *) addr
;
140 for (p
= (char *) addr
+ orig_len
- 1;
141 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
151 if (len
<= (int) sizeof (LONGEST
))
153 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
162 /* Treat the LEN bytes at ADDR as a target-format address, and return
163 that address. ADDR is a buffer in the GDB process, not in the
166 This function should only be used by target-specific code. It
167 assumes that a pointer has the same representation as that thing's
168 address represented as an integer. Some machines use word
169 addresses, or similarly munged things, for certain types of
170 pointers, so that assumption doesn't hold everywhere.
172 Common code should use extract_typed_address instead, or something
173 else based on POINTER_TO_ADDRESS. */
176 extract_address (void *addr
, int len
)
178 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
179 whether we want this to be true eventually. */
180 return (CORE_ADDR
) extract_unsigned_integer (addr
, len
);
184 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
185 address it represents. */
187 extract_typed_address (void *buf
, struct type
*type
)
189 if (TYPE_CODE (type
) != TYPE_CODE_PTR
190 && TYPE_CODE (type
) != TYPE_CODE_REF
)
191 internal_error (__FILE__
, __LINE__
,
192 "extract_typed_address: "
193 "type is not a pointer or reference");
195 return POINTER_TO_ADDRESS (type
, buf
);
200 store_signed_integer (void *addr
, int len
, LONGEST val
)
203 unsigned char *startaddr
= (unsigned char *) addr
;
204 unsigned char *endaddr
= startaddr
+ len
;
206 /* Start at the least significant end of the integer, and work towards
207 the most significant. */
208 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
210 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
218 for (p
= startaddr
; p
< endaddr
; ++p
)
227 store_unsigned_integer (void *addr
, int len
, ULONGEST val
)
230 unsigned char *startaddr
= (unsigned char *) addr
;
231 unsigned char *endaddr
= startaddr
+ len
;
233 /* Start at the least significant end of the integer, and work towards
234 the most significant. */
235 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
237 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
245 for (p
= startaddr
; p
< endaddr
; ++p
)
253 /* Store the address VAL as a LEN-byte value in target byte order at
254 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
256 This function should only be used by target-specific code. It
257 assumes that a pointer has the same representation as that thing's
258 address represented as an integer. Some machines use word
259 addresses, or similarly munged things, for certain types of
260 pointers, so that assumption doesn't hold everywhere.
262 Common code should use store_typed_address instead, or something else
263 based on ADDRESS_TO_POINTER. */
265 store_address (void *addr
, int len
, LONGEST val
)
267 store_unsigned_integer (addr
, len
, val
);
271 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
274 store_typed_address (void *buf
, struct type
*type
, CORE_ADDR addr
)
276 if (TYPE_CODE (type
) != TYPE_CODE_PTR
277 && TYPE_CODE (type
) != TYPE_CODE_REF
)
278 internal_error (__FILE__
, __LINE__
,
279 "store_typed_address: "
280 "type is not a pointer or reference");
282 ADDRESS_TO_POINTER (type
, buf
, addr
);
287 /* Return a `value' with the contents of (virtual or cooked) register
288 REGNUM as found in the specified FRAME. The register's type is
289 determined by REGISTER_VIRTUAL_TYPE.
291 NOTE: returns NULL if register value is not available. Caller will
292 check return value or die! */
295 value_of_register (int regnum
, struct frame_info
*frame
)
299 struct value
*reg_val
;
300 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
303 /* Builtin registers lie completly outside of the range of normal
304 registers. Catch them early so that the target never sees them. */
305 if (regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
306 return value_of_builtin_reg (regnum
, selected_frame
);
308 get_saved_register (raw_buffer
, &optim
, &addr
,
309 frame
, regnum
, &lval
);
311 /* FIXME: cagney/2002-05-15: This test is just bogus.
313 It indicates that the target failed to supply a value for a
314 register because it was "not available" at this time. Problem
315 is, the target still has the register and so get saved_register()
316 may be returning a value saved on the stack. */
318 if (register_cached (regnum
) < 0)
319 return NULL
; /* register value not available */
321 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
323 /* Convert raw data to virtual format if necessary. */
325 if (REGISTER_CONVERTIBLE (regnum
))
327 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
328 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
330 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
331 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
332 REGISTER_RAW_SIZE (regnum
));
334 internal_error (__FILE__
, __LINE__
,
335 "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
336 REGISTER_NAME (regnum
),
338 REGISTER_RAW_SIZE (regnum
),
339 REGISTER_VIRTUAL_SIZE (regnum
));
340 VALUE_LVAL (reg_val
) = lval
;
341 VALUE_ADDRESS (reg_val
) = addr
;
342 VALUE_REGNO (reg_val
) = regnum
;
343 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
347 /* Given a pointer of type TYPE in target form in BUF, return the
348 address it represents. */
350 unsigned_pointer_to_address (struct type
*type
, void *buf
)
352 return extract_address (buf
, TYPE_LENGTH (type
));
356 signed_pointer_to_address (struct type
*type
, void *buf
)
358 return extract_signed_integer (buf
, TYPE_LENGTH (type
));
361 /* Given an address, store it as a pointer of type TYPE in target
364 unsigned_address_to_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
366 store_address (buf
, TYPE_LENGTH (type
), addr
);
370 address_to_signed_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
372 store_signed_integer (buf
, TYPE_LENGTH (type
), addr
);
375 /* Will calling read_var_value or locate_var_value on SYM end
376 up caring what frame it is being evaluated relative to? SYM must
379 symbol_read_needs_frame (struct symbol
*sym
)
381 switch (SYMBOL_CLASS (sym
))
383 /* All cases listed explicitly so that gcc -Wall will detect it if
384 we failed to consider one. */
389 case LOC_REGPARM_ADDR
:
393 case LOC_BASEREG_ARG
:
394 case LOC_HP_THREAD_LOCAL_STATIC
:
404 /* Getting the address of a label can be done independently of the block,
405 even if some *uses* of that address wouldn't work so well without
409 case LOC_CONST_BYTES
:
411 case LOC_OPTIMIZED_OUT
:
417 /* Given a struct symbol for a variable,
418 and a stack frame id, read the value of the variable
419 and return a (pointer to a) struct value containing the value.
420 If the variable cannot be found, return a zero pointer.
421 If FRAME is NULL, use the selected_frame. */
424 read_var_value (register struct symbol
*var
, struct frame_info
*frame
)
426 register struct value
*v
;
427 struct type
*type
= SYMBOL_TYPE (var
);
431 v
= allocate_value (type
);
432 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
433 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
435 len
= TYPE_LENGTH (type
);
438 frame
= selected_frame
;
440 switch (SYMBOL_CLASS (var
))
443 /* Put the constant back in target format. */
444 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
445 (LONGEST
) SYMBOL_VALUE (var
));
446 VALUE_LVAL (v
) = not_lval
;
450 /* Put the constant back in target format. */
451 if (overlay_debugging
)
454 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
455 SYMBOL_BFD_SECTION (var
));
456 store_typed_address (VALUE_CONTENTS_RAW (v
), type
, addr
);
459 store_typed_address (VALUE_CONTENTS_RAW (v
), type
,
460 SYMBOL_VALUE_ADDRESS (var
));
461 VALUE_LVAL (v
) = not_lval
;
464 case LOC_CONST_BYTES
:
467 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
468 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
469 VALUE_LVAL (v
) = not_lval
;
474 if (overlay_debugging
)
475 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
476 SYMBOL_BFD_SECTION (var
));
478 addr
= SYMBOL_VALUE_ADDRESS (var
);
483 /* The import slot does not have a real address in it from the
484 dynamic loader (dld.sl on HP-UX), if the target hasn't
485 begun execution yet, so check for that. */
488 if (!target_has_execution
)
490 Attempt to access variable defined in different shared object or load module when\n\
491 addresses have not been bound by the dynamic loader. Try again when executable is running.");
493 locaddr
= SYMBOL_VALUE_ADDRESS (var
);
494 loc
= value_at (lookup_pointer_type (type
), locaddr
, NULL
);
495 addr
= value_as_address (loc
);
501 addr
= FRAME_ARGS_ADDRESS (frame
);
504 addr
+= SYMBOL_VALUE (var
);
513 argref
= FRAME_ARGS_ADDRESS (frame
);
516 argref
+= SYMBOL_VALUE (var
);
517 ref
= value_at (lookup_pointer_type (type
), argref
, NULL
);
518 addr
= value_as_address (ref
);
526 addr
= FRAME_LOCALS_ADDRESS (frame
);
527 addr
+= SYMBOL_VALUE (var
);
531 case LOC_BASEREG_ARG
:
532 case LOC_HP_THREAD_LOCAL_STATIC
:
534 struct value
*regval
;
536 regval
= value_from_register (lookup_pointer_type (type
),
537 SYMBOL_BASEREG (var
), frame
);
539 error ("Value of base register not available.");
540 addr
= value_as_address (regval
);
541 addr
+= SYMBOL_VALUE (var
);
545 case LOC_THREAD_LOCAL_STATIC
:
547 /* We want to let the target / ABI-specific code construct
548 this value for us, so we need to dispose of the value
549 allocated for us above. */
550 if (target_get_thread_local_address_p ())
551 addr
= target_get_thread_local_address (inferior_ptid
,
552 SYMBOL_OBJFILE (var
),
553 SYMBOL_VALUE_ADDRESS (var
));
554 /* It wouldn't be wrong here to try a gdbarch method, too;
555 finding TLS is an ABI-specific thing. But we don't do that
558 error ("Cannot find thread-local variables on this target");
563 error ("Cannot look up value of a typedef");
567 if (overlay_debugging
)
568 VALUE_ADDRESS (v
) = symbol_overlayed_address
569 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
571 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
576 case LOC_REGPARM_ADDR
:
579 int regno
= SYMBOL_VALUE (var
);
580 struct value
*regval
;
584 b
= get_frame_block (frame
, 0);
586 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
588 regval
= value_from_register (lookup_pointer_type (type
),
593 error ("Value of register variable not available.");
595 addr
= value_as_address (regval
);
596 VALUE_LVAL (v
) = lval_memory
;
600 regval
= value_from_register (type
, regno
, frame
);
603 error ("Value of register variable not available.");
611 struct minimal_symbol
*msym
;
613 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
616 if (overlay_debugging
)
617 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
618 SYMBOL_BFD_SECTION (msym
));
620 addr
= SYMBOL_VALUE_ADDRESS (msym
);
624 case LOC_OPTIMIZED_OUT
:
625 VALUE_LVAL (v
) = not_lval
;
626 VALUE_OPTIMIZED_OUT (v
) = 1;
630 error ("Cannot look up value of a botched symbol.");
634 VALUE_ADDRESS (v
) = addr
;
639 /* Return a value of type TYPE, stored in register REGNUM, in frame
642 NOTE: returns NULL if register value is not available.
643 Caller will check return value or die! */
646 value_from_register (struct type
*type
, int regnum
, struct frame_info
*frame
)
648 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
651 struct value
*v
= allocate_value (type
);
652 char *value_bytes
= 0;
653 int value_bytes_copied
= 0;
654 int num_storage_locs
;
658 CHECK_TYPEDEF (type
);
659 len
= TYPE_LENGTH (type
);
661 VALUE_REGNO (v
) = regnum
;
663 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
664 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
667 if (num_storage_locs
> 1
668 #ifdef GDB_TARGET_IS_H8500
669 || TYPE_CODE (type
) == TYPE_CODE_PTR
673 /* Value spread across multiple storage locations. */
676 int mem_stor
= 0, reg_stor
= 0;
677 int mem_tracking
= 1;
678 CORE_ADDR last_addr
= 0;
679 CORE_ADDR first_addr
= 0;
681 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
683 /* Copy all of the data out, whereever it may be. */
685 #ifdef GDB_TARGET_IS_H8500
686 /* This piece of hideosity is required because the H8500 treats registers
687 differently depending upon whether they are used as pointers or not. As a
688 pointer, a register needs to have a page register tacked onto the front.
689 An alternate way to do this would be to have gcc output different register
690 numbers for the pointer & non-pointer form of the register. But, it
691 doesn't, so we're stuck with this. */
693 if (TYPE_CODE (type
) == TYPE_CODE_PTR
704 page_regnum
= SEG_D_REGNUM
;
708 page_regnum
= SEG_E_REGNUM
;
712 page_regnum
= SEG_T_REGNUM
;
717 get_saved_register (value_bytes
+ 1,
724 if (register_cached (page_regnum
) == -1)
725 return NULL
; /* register value not available */
727 if (lval
== lval_register
)
734 get_saved_register (value_bytes
+ 2,
741 if (register_cached (regnum
) == -1)
742 return NULL
; /* register value not available */
744 if (lval
== lval_register
)
749 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
754 #endif /* GDB_TARGET_IS_H8500 */
755 for (local_regnum
= regnum
;
756 value_bytes_copied
< len
;
757 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
760 get_saved_register (value_bytes
+ value_bytes_copied
,
767 if (register_cached (local_regnum
) == -1)
768 return NULL
; /* register value not available */
770 if (regnum
== local_regnum
)
772 if (lval
== lval_register
)
780 && (regnum
== local_regnum
781 || addr
== last_addr
));
786 if ((reg_stor
&& mem_stor
)
787 || (mem_stor
&& !mem_tracking
))
788 /* Mixed storage; all of the hassle we just went through was
789 for some good purpose. */
791 VALUE_LVAL (v
) = lval_reg_frame_relative
;
792 VALUE_FRAME (v
) = FRAME_FP (frame
);
793 VALUE_FRAME_REGNUM (v
) = regnum
;
797 VALUE_LVAL (v
) = lval_memory
;
798 VALUE_ADDRESS (v
) = first_addr
;
802 VALUE_LVAL (v
) = lval_register
;
803 VALUE_ADDRESS (v
) = first_addr
;
806 internal_error (__FILE__
, __LINE__
,
807 "value_from_register: Value not stored anywhere!");
809 VALUE_OPTIMIZED_OUT (v
) = optim
;
811 /* Any structure stored in more than one register will always be
812 an integral number of registers. Otherwise, you'd need to do
813 some fiddling with the last register copied here for little
816 /* Copy into the contents section of the value. */
817 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
819 /* Finally do any conversion necessary when extracting this
820 type from more than one register. */
821 #ifdef REGISTER_CONVERT_TO_TYPE
822 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
827 /* Data is completely contained within a single register. Locate the
828 register's contents in a real register or in core;
829 read the data in raw format. */
831 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
833 if (register_cached (regnum
) == -1)
834 return NULL
; /* register value not available */
836 VALUE_OPTIMIZED_OUT (v
) = optim
;
837 VALUE_LVAL (v
) = lval
;
838 VALUE_ADDRESS (v
) = addr
;
840 /* Convert the raw register to the corresponding data value's memory
841 format, if necessary. */
843 if (CONVERT_REGISTER_P (regnum
))
845 REGISTER_TO_VALUE (regnum
, type
, raw_buffer
, VALUE_CONTENTS_RAW (v
));
849 /* Raw and virtual formats are the same for this register. */
851 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
&& len
< REGISTER_RAW_SIZE (regnum
))
853 /* Big-endian, and we want less than full size. */
854 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
857 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
863 /* Given a struct symbol for a variable or function,
864 and a stack frame id,
865 return a (pointer to a) struct value containing the properly typed
869 locate_var_value (register struct symbol
*var
, struct frame_info
*frame
)
872 struct type
*type
= SYMBOL_TYPE (var
);
873 struct value
*lazy_value
;
875 /* Evaluate it first; if the result is a memory address, we're fine.
876 Lazy evaluation pays off here. */
878 lazy_value
= read_var_value (var
, frame
);
880 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
882 if (VALUE_LAZY (lazy_value
)
883 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
887 addr
= VALUE_ADDRESS (lazy_value
);
888 val
= value_from_pointer (lookup_pointer_type (type
), addr
);
889 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
893 /* Not a memory address; check what the problem was. */
894 switch (VALUE_LVAL (lazy_value
))
897 gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value
)) != NULL
898 && *REGISTER_NAME (VALUE_REGNO (lazy_value
)) != '\0');
899 error("Address requested for identifier "
900 "\"%s\" which is in register $%s",
901 SYMBOL_SOURCE_NAME (var
),
902 REGISTER_NAME (VALUE_REGNO (lazy_value
)));
905 case lval_reg_frame_relative
:
906 gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != NULL
907 && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != '\0');
908 error("Address requested for identifier "
909 "\"%s\" which is in frame register $%s",
910 SYMBOL_SOURCE_NAME (var
),
911 REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)));
915 error ("Can't take address of \"%s\" which isn't an lvalue.",
916 SYMBOL_SOURCE_NAME (var
));
919 return 0; /* For lint -- never reached */