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 */
37 /* This is used to indicate that we don't know the format of the floating point
38 number. Typically, this is useful for native ports, where the actual format
39 is irrelevant, since no conversions will be taking place. */
41 const struct floatformat floatformat_unknown
;
43 /* Basic byte-swapping routines. GDB has needed these for a long time...
44 All extract a target-format integer at ADDR which is LEN bytes long. */
46 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
47 /* 8 bit characters are a pretty safe assumption these days, so we
48 assume it throughout all these swapping routines. If we had to deal with
49 9 bit characters, we would need to make len be in bits and would have
50 to re-write these routines... */
55 extract_signed_integer (void *addr
, int len
)
59 unsigned char *startaddr
= (unsigned char *) addr
;
60 unsigned char *endaddr
= startaddr
+ len
;
62 if (len
> (int) sizeof (LONGEST
))
64 That operation is not available on integers of more than %d bytes.",
67 /* Start at the most significant end of the integer, and work towards
68 the least significant. */
69 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
72 /* Do the sign extension once at the start. */
73 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
74 for (++p
; p
< endaddr
; ++p
)
75 retval
= (retval
<< 8) | *p
;
80 /* Do the sign extension once at the start. */
81 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
82 for (--p
; p
>= startaddr
; --p
)
83 retval
= (retval
<< 8) | *p
;
89 extract_unsigned_integer (void *addr
, int len
)
93 unsigned char *startaddr
= (unsigned char *) addr
;
94 unsigned char *endaddr
= startaddr
+ len
;
96 if (len
> (int) sizeof (ULONGEST
))
98 That operation is not available on integers of more than %d bytes.",
101 /* Start at the most significant end of the integer, and work towards
102 the least significant. */
104 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
106 for (p
= startaddr
; p
< endaddr
; ++p
)
107 retval
= (retval
<< 8) | *p
;
111 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
112 retval
= (retval
<< 8) | *p
;
117 /* Sometimes a long long unsigned integer can be extracted as a
118 LONGEST value. This is done so that we can print these values
119 better. If this integer can be converted to a LONGEST, this
120 function returns 1 and sets *PVAL. Otherwise it returns 0. */
123 extract_long_unsigned_integer (void *addr
, int orig_len
, LONGEST
*pval
)
125 char *p
, *first_addr
;
129 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
131 for (p
= (char *) addr
;
132 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
144 first_addr
= (char *) addr
;
145 for (p
= (char *) addr
+ orig_len
- 1;
146 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
156 if (len
<= (int) sizeof (LONGEST
))
158 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
167 /* Treat the LEN bytes at ADDR as a target-format address, and return
168 that address. ADDR is a buffer in the GDB process, not in the
171 This function should only be used by target-specific code. It
172 assumes that a pointer has the same representation as that thing's
173 address represented as an integer. Some machines use word
174 addresses, or similarly munged things, for certain types of
175 pointers, so that assumption doesn't hold everywhere.
177 Common code should use extract_typed_address instead, or something
178 else based on POINTER_TO_ADDRESS. */
181 extract_address (void *addr
, int 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 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
190 address it represents. */
192 extract_typed_address (void *buf
, struct type
*type
)
194 if (TYPE_CODE (type
) != TYPE_CODE_PTR
195 && TYPE_CODE (type
) != TYPE_CODE_REF
)
196 internal_error (__FILE__
, __LINE__
,
197 "extract_typed_address: "
198 "type is not a pointer or reference");
200 return POINTER_TO_ADDRESS (type
, buf
);
205 store_signed_integer (void *addr
, int len
, LONGEST val
)
208 unsigned char *startaddr
= (unsigned char *) addr
;
209 unsigned char *endaddr
= startaddr
+ len
;
211 /* Start at the least significant end of the integer, and work towards
212 the most significant. */
213 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
215 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
223 for (p
= startaddr
; p
< endaddr
; ++p
)
232 store_unsigned_integer (void *addr
, int len
, ULONGEST val
)
235 unsigned char *startaddr
= (unsigned char *) addr
;
236 unsigned char *endaddr
= startaddr
+ len
;
238 /* Start at the least significant end of the integer, and work towards
239 the most significant. */
240 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
242 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
250 for (p
= startaddr
; p
< endaddr
; ++p
)
258 /* Store the address VAL as a LEN-byte value in target byte order at
259 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
261 This function should only be used by target-specific code. It
262 assumes that a pointer has the same representation as that thing's
263 address represented as an integer. Some machines use word
264 addresses, or similarly munged things, for certain types of
265 pointers, so that assumption doesn't hold everywhere.
267 Common code should use store_typed_address instead, or something else
268 based on ADDRESS_TO_POINTER. */
270 store_address (void *addr
, int len
, LONGEST val
)
272 store_unsigned_integer (addr
, len
, val
);
276 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
279 store_typed_address (void *buf
, struct type
*type
, CORE_ADDR addr
)
281 if (TYPE_CODE (type
) != TYPE_CODE_PTR
282 && TYPE_CODE (type
) != TYPE_CODE_REF
)
283 internal_error (__FILE__
, __LINE__
,
284 "store_typed_address: "
285 "type is not a pointer or reference");
287 ADDRESS_TO_POINTER (type
, buf
, addr
);
292 /* Return a `value' with the contents of register REGNUM
293 in its virtual format, with the type specified by
294 REGISTER_VIRTUAL_TYPE.
296 NOTE: returns NULL if register value is not available.
297 Caller will check return value or die! */
300 value_of_register (int regnum
)
304 struct value
*reg_val
;
305 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
308 get_saved_register (raw_buffer
, &optim
, &addr
,
309 selected_frame
, regnum
, &lval
);
311 if (register_cached (regnum
) < 0)
312 return NULL
; /* register value not available */
314 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
316 /* Convert raw data to virtual format if necessary. */
318 if (REGISTER_CONVERTIBLE (regnum
))
320 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
321 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
323 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
324 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
325 REGISTER_RAW_SIZE (regnum
));
327 internal_error (__FILE__
, __LINE__
,
328 "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
329 REGISTER_NAME (regnum
),
331 REGISTER_RAW_SIZE (regnum
),
332 REGISTER_VIRTUAL_SIZE (regnum
));
333 VALUE_LVAL (reg_val
) = lval
;
334 VALUE_ADDRESS (reg_val
) = addr
;
335 VALUE_REGNO (reg_val
) = regnum
;
336 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
340 /* Given a pointer of type TYPE in target form in BUF, return the
341 address it represents. */
343 unsigned_pointer_to_address (struct type
*type
, void *buf
)
345 return extract_address (buf
, TYPE_LENGTH (type
));
349 signed_pointer_to_address (struct type
*type
, void *buf
)
351 return extract_signed_integer (buf
, TYPE_LENGTH (type
));
354 /* Given an address, store it as a pointer of type TYPE in target
357 unsigned_address_to_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
359 store_address (buf
, TYPE_LENGTH (type
), addr
);
363 address_to_signed_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
365 store_signed_integer (buf
, TYPE_LENGTH (type
), addr
);
368 /* Will calling read_var_value or locate_var_value on SYM end
369 up caring what frame it is being evaluated relative to? SYM must
372 symbol_read_needs_frame (struct symbol
*sym
)
374 switch (SYMBOL_CLASS (sym
))
376 /* All cases listed explicitly so that gcc -Wall will detect it if
377 we failed to consider one. */
382 case LOC_REGPARM_ADDR
:
386 case LOC_BASEREG_ARG
:
387 case LOC_THREAD_LOCAL_STATIC
:
397 /* Getting the address of a label can be done independently of the block,
398 even if some *uses* of that address wouldn't work so well without
402 case LOC_CONST_BYTES
:
404 case LOC_OPTIMIZED_OUT
:
410 /* Given a struct symbol for a variable,
411 and a stack frame id, read the value of the variable
412 and return a (pointer to a) struct value containing the value.
413 If the variable cannot be found, return a zero pointer.
414 If FRAME is NULL, use the selected_frame. */
417 read_var_value (register struct symbol
*var
, struct frame_info
*frame
)
419 register struct value
*v
;
420 struct type
*type
= SYMBOL_TYPE (var
);
424 v
= allocate_value (type
);
425 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
426 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
428 len
= TYPE_LENGTH (type
);
431 frame
= selected_frame
;
433 switch (SYMBOL_CLASS (var
))
436 /* Put the constant back in target format. */
437 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
438 (LONGEST
) SYMBOL_VALUE (var
));
439 VALUE_LVAL (v
) = not_lval
;
443 /* Put the constant back in target format. */
444 if (overlay_debugging
)
447 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
448 SYMBOL_BFD_SECTION (var
));
449 store_typed_address (VALUE_CONTENTS_RAW (v
), type
, addr
);
452 store_typed_address (VALUE_CONTENTS_RAW (v
), type
,
453 SYMBOL_VALUE_ADDRESS (var
));
454 VALUE_LVAL (v
) = not_lval
;
457 case LOC_CONST_BYTES
:
460 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
461 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
462 VALUE_LVAL (v
) = not_lval
;
467 if (overlay_debugging
)
468 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
469 SYMBOL_BFD_SECTION (var
));
471 addr
= SYMBOL_VALUE_ADDRESS (var
);
476 /* The import slot does not have a real address in it from the
477 dynamic loader (dld.sl on HP-UX), if the target hasn't
478 begun execution yet, so check for that. */
481 if (!target_has_execution
)
483 Attempt to access variable defined in different shared object or load module when\n\
484 addresses have not been bound by the dynamic loader. Try again when executable is running.");
486 locaddr
= SYMBOL_VALUE_ADDRESS (var
);
487 loc
= value_at (lookup_pointer_type (type
), locaddr
, NULL
);
488 addr
= value_as_address (loc
);
494 addr
= FRAME_ARGS_ADDRESS (frame
);
497 addr
+= SYMBOL_VALUE (var
);
506 argref
= FRAME_ARGS_ADDRESS (frame
);
509 argref
+= SYMBOL_VALUE (var
);
510 ref
= value_at (lookup_pointer_type (type
), argref
, NULL
);
511 addr
= value_as_address (ref
);
519 addr
= FRAME_LOCALS_ADDRESS (frame
);
520 addr
+= SYMBOL_VALUE (var
);
524 case LOC_BASEREG_ARG
:
525 case LOC_THREAD_LOCAL_STATIC
:
527 struct value
*regval
;
529 regval
= value_from_register (lookup_pointer_type (type
),
530 SYMBOL_BASEREG (var
), frame
);
532 error ("Value of base register not available.");
533 addr
= value_as_address (regval
);
534 addr
+= SYMBOL_VALUE (var
);
539 error ("Cannot look up value of a typedef");
543 if (overlay_debugging
)
544 VALUE_ADDRESS (v
) = symbol_overlayed_address
545 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
547 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
552 case LOC_REGPARM_ADDR
:
555 int regno
= SYMBOL_VALUE (var
);
556 struct value
*regval
;
560 b
= get_frame_block (frame
);
562 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
564 regval
= value_from_register (lookup_pointer_type (type
),
569 error ("Value of register variable not available.");
571 addr
= value_as_address (regval
);
572 VALUE_LVAL (v
) = lval_memory
;
576 regval
= value_from_register (type
, regno
, frame
);
579 error ("Value of register variable not available.");
587 struct minimal_symbol
*msym
;
589 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
592 if (overlay_debugging
)
593 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
594 SYMBOL_BFD_SECTION (msym
));
596 addr
= SYMBOL_VALUE_ADDRESS (msym
);
600 case LOC_OPTIMIZED_OUT
:
601 VALUE_LVAL (v
) = not_lval
;
602 VALUE_OPTIMIZED_OUT (v
) = 1;
606 error ("Cannot look up value of a botched symbol.");
610 VALUE_ADDRESS (v
) = addr
;
615 /* Return a value of type TYPE, stored in register REGNUM, in frame
618 NOTE: returns NULL if register value is not available.
619 Caller will check return value or die! */
622 value_from_register (struct type
*type
, int regnum
, struct frame_info
*frame
)
624 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
627 struct value
*v
= allocate_value (type
);
628 char *value_bytes
= 0;
629 int value_bytes_copied
= 0;
630 int num_storage_locs
;
634 CHECK_TYPEDEF (type
);
635 len
= TYPE_LENGTH (type
);
637 VALUE_REGNO (v
) = regnum
;
639 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
640 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
643 if (num_storage_locs
> 1
644 #ifdef GDB_TARGET_IS_H8500
645 || TYPE_CODE (type
) == TYPE_CODE_PTR
649 /* Value spread across multiple storage locations. */
652 int mem_stor
= 0, reg_stor
= 0;
653 int mem_tracking
= 1;
654 CORE_ADDR last_addr
= 0;
655 CORE_ADDR first_addr
= 0;
657 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
659 /* Copy all of the data out, whereever it may be. */
661 #ifdef GDB_TARGET_IS_H8500
662 /* This piece of hideosity is required because the H8500 treats registers
663 differently depending upon whether they are used as pointers or not. As a
664 pointer, a register needs to have a page register tacked onto the front.
665 An alternate way to do this would be to have gcc output different register
666 numbers for the pointer & non-pointer form of the register. But, it
667 doesn't, so we're stuck with this. */
669 if (TYPE_CODE (type
) == TYPE_CODE_PTR
680 page_regnum
= SEG_D_REGNUM
;
684 page_regnum
= SEG_E_REGNUM
;
688 page_regnum
= SEG_T_REGNUM
;
693 get_saved_register (value_bytes
+ 1,
700 if (register_cached (page_regnum
) == -1)
701 return NULL
; /* register value not available */
703 if (lval
== lval_register
)
710 get_saved_register (value_bytes
+ 2,
717 if (register_cached (regnum
) == -1)
718 return NULL
; /* register value not available */
720 if (lval
== lval_register
)
725 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
730 #endif /* GDB_TARGET_IS_H8500 */
731 for (local_regnum
= regnum
;
732 value_bytes_copied
< len
;
733 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
736 get_saved_register (value_bytes
+ value_bytes_copied
,
743 if (register_cached (local_regnum
) == -1)
744 return NULL
; /* register value not available */
746 if (regnum
== local_regnum
)
748 if (lval
== lval_register
)
756 && (regnum
== local_regnum
757 || addr
== last_addr
));
762 if ((reg_stor
&& mem_stor
)
763 || (mem_stor
&& !mem_tracking
))
764 /* Mixed storage; all of the hassle we just went through was
765 for some good purpose. */
767 VALUE_LVAL (v
) = lval_reg_frame_relative
;
768 VALUE_FRAME (v
) = FRAME_FP (frame
);
769 VALUE_FRAME_REGNUM (v
) = regnum
;
773 VALUE_LVAL (v
) = lval_memory
;
774 VALUE_ADDRESS (v
) = first_addr
;
778 VALUE_LVAL (v
) = lval_register
;
779 VALUE_ADDRESS (v
) = first_addr
;
782 internal_error (__FILE__
, __LINE__
,
783 "value_from_register: Value not stored anywhere!");
785 VALUE_OPTIMIZED_OUT (v
) = optim
;
787 /* Any structure stored in more than one register will always be
788 an integral number of registers. Otherwise, you'd need to do
789 some fiddling with the last register copied here for little
792 /* Copy into the contents section of the value. */
793 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
795 /* Finally do any conversion necessary when extracting this
796 type from more than one register. */
797 #ifdef REGISTER_CONVERT_TO_TYPE
798 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
803 /* Data is completely contained within a single register. Locate the
804 register's contents in a real register or in core;
805 read the data in raw format. */
807 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
809 if (register_cached (regnum
) == -1)
810 return NULL
; /* register value not available */
812 VALUE_OPTIMIZED_OUT (v
) = optim
;
813 VALUE_LVAL (v
) = lval
;
814 VALUE_ADDRESS (v
) = addr
;
816 /* Convert raw data to virtual format if necessary. */
818 if (REGISTER_CONVERTIBLE (regnum
))
820 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
821 raw_buffer
, VALUE_CONTENTS_RAW (v
));
825 /* Raw and virtual formats are the same for this register. */
827 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
&& len
< REGISTER_RAW_SIZE (regnum
))
829 /* Big-endian, and we want less than full size. */
830 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
833 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
839 /* Given a struct symbol for a variable or function,
840 and a stack frame id,
841 return a (pointer to a) struct value containing the properly typed
845 locate_var_value (register struct symbol
*var
, struct frame_info
*frame
)
848 struct type
*type
= SYMBOL_TYPE (var
);
849 struct value
*lazy_value
;
851 /* Evaluate it first; if the result is a memory address, we're fine.
852 Lazy evaluation pays off here. */
854 lazy_value
= read_var_value (var
, frame
);
856 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
858 if (VALUE_LAZY (lazy_value
)
859 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
863 addr
= VALUE_ADDRESS (lazy_value
);
864 val
= value_from_pointer (lookup_pointer_type (type
), addr
);
865 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
869 /* Not a memory address; check what the problem was. */
870 switch (VALUE_LVAL (lazy_value
))
873 gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value
)) != NULL
874 && *REGISTER_NAME (VALUE_REGNO (lazy_value
)) != '\0');
875 error("Address requested for identifier "
876 "\"%s\" which is in register $%s",
877 SYMBOL_SOURCE_NAME (var
),
878 REGISTER_NAME (VALUE_REGNO (lazy_value
)));
881 case lval_reg_frame_relative
:
882 gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != NULL
883 && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != '\0');
884 error("Address requested for identifier "
885 "\"%s\" which is in frame register $%s",
886 SYMBOL_SOURCE_NAME (var
),
887 REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)));
891 error ("Can't take address of \"%s\" which isn't an lvalue.",
892 SYMBOL_SOURCE_NAME (var
));
895 return 0; /* For lint -- never reached */