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 /* Basic byte-swapping routines. GDB has needed these for a long time...
38 All extract a target-format integer at ADDR which is LEN bytes long. */
40 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
41 /* 8 bit characters are a pretty safe assumption these days, so we
42 assume it throughout all these swapping routines. If we had to deal with
43 9 bit characters, we would need to make len be in bits and would have
44 to re-write these routines... */
49 extract_signed_integer (void *addr
, int len
)
53 unsigned char *startaddr
= (unsigned char *) addr
;
54 unsigned char *endaddr
= startaddr
+ len
;
56 if (len
> (int) sizeof (LONGEST
))
58 That operation is not available on integers of more than %d bytes.",
61 /* Start at the most significant end of the integer, and work towards
62 the least significant. */
63 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
66 /* Do the sign extension once at the start. */
67 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
68 for (++p
; p
< endaddr
; ++p
)
69 retval
= (retval
<< 8) | *p
;
74 /* Do the sign extension once at the start. */
75 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
76 for (--p
; p
>= startaddr
; --p
)
77 retval
= (retval
<< 8) | *p
;
83 extract_unsigned_integer (void *addr
, int len
)
87 unsigned char *startaddr
= (unsigned char *) addr
;
88 unsigned char *endaddr
= startaddr
+ len
;
90 if (len
> (int) sizeof (ULONGEST
))
92 That operation is not available on integers of more than %d bytes.",
95 /* Start at the most significant end of the integer, and work towards
96 the least significant. */
98 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
100 for (p
= startaddr
; p
< endaddr
; ++p
)
101 retval
= (retval
<< 8) | *p
;
105 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
106 retval
= (retval
<< 8) | *p
;
111 /* Sometimes a long long unsigned integer can be extracted as a
112 LONGEST value. This is done so that we can print these values
113 better. If this integer can be converted to a LONGEST, this
114 function returns 1 and sets *PVAL. Otherwise it returns 0. */
117 extract_long_unsigned_integer (void *addr
, int orig_len
, LONGEST
*pval
)
119 char *p
, *first_addr
;
123 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
125 for (p
= (char *) addr
;
126 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
138 first_addr
= (char *) addr
;
139 for (p
= (char *) addr
+ orig_len
- 1;
140 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
150 if (len
<= (int) sizeof (LONGEST
))
152 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
161 /* Treat the LEN bytes at ADDR as a target-format address, and return
162 that address. ADDR is a buffer in the GDB process, not in the
165 This function should only be used by target-specific code. It
166 assumes that a pointer has the same representation as that thing's
167 address represented as an integer. Some machines use word
168 addresses, or similarly munged things, for certain types of
169 pointers, so that assumption doesn't hold everywhere.
171 Common code should use extract_typed_address instead, or something
172 else based on POINTER_TO_ADDRESS. */
175 extract_address (void *addr
, int len
)
177 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
178 whether we want this to be true eventually. */
179 return (CORE_ADDR
) extract_unsigned_integer (addr
, len
);
183 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
184 address it represents. */
186 extract_typed_address (void *buf
, struct type
*type
)
188 if (TYPE_CODE (type
) != TYPE_CODE_PTR
189 && TYPE_CODE (type
) != TYPE_CODE_REF
)
190 internal_error (__FILE__
, __LINE__
,
191 "extract_typed_address: "
192 "type is not a pointer or reference");
194 return POINTER_TO_ADDRESS (type
, buf
);
199 store_signed_integer (void *addr
, int len
, LONGEST val
)
202 unsigned char *startaddr
= (unsigned char *) addr
;
203 unsigned char *endaddr
= startaddr
+ len
;
205 /* Start at the least significant end of the integer, and work towards
206 the most significant. */
207 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
209 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
217 for (p
= startaddr
; p
< endaddr
; ++p
)
226 store_unsigned_integer (void *addr
, int len
, ULONGEST val
)
229 unsigned char *startaddr
= (unsigned char *) addr
;
230 unsigned char *endaddr
= startaddr
+ len
;
232 /* Start at the least significant end of the integer, and work towards
233 the most significant. */
234 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
236 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
244 for (p
= startaddr
; p
< endaddr
; ++p
)
252 /* Store the address VAL as a LEN-byte value in target byte order at
253 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
255 This function should only be used by target-specific code. It
256 assumes that a pointer has the same representation as that thing's
257 address represented as an integer. Some machines use word
258 addresses, or similarly munged things, for certain types of
259 pointers, so that assumption doesn't hold everywhere.
261 Common code should use store_typed_address instead, or something else
262 based on ADDRESS_TO_POINTER. */
264 store_address (void *addr
, int len
, LONGEST val
)
266 store_unsigned_integer (addr
, len
, val
);
270 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
273 store_typed_address (void *buf
, struct type
*type
, CORE_ADDR addr
)
275 if (TYPE_CODE (type
) != TYPE_CODE_PTR
276 && TYPE_CODE (type
) != TYPE_CODE_REF
)
277 internal_error (__FILE__
, __LINE__
,
278 "store_typed_address: "
279 "type is not a pointer or reference");
281 ADDRESS_TO_POINTER (type
, buf
, addr
);
286 /* Return a `value' with the contents of (virtual or cooked) register
287 REGNUM as found in the specified FRAME. The register's type is
288 determined by REGISTER_VIRTUAL_TYPE.
290 NOTE: returns NULL if register value is not available. Caller will
291 check return value or die! */
294 value_of_register (int regnum
, struct frame_info
*frame
)
298 struct value
*reg_val
;
299 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
302 get_saved_register (raw_buffer
, &optim
, &addr
,
303 frame
, regnum
, &lval
);
305 if (register_cached (regnum
) < 0)
306 return NULL
; /* register value not available */
308 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
310 /* Convert raw data to virtual format if necessary. */
312 if (REGISTER_CONVERTIBLE (regnum
))
314 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
315 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
317 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
318 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
319 REGISTER_RAW_SIZE (regnum
));
321 internal_error (__FILE__
, __LINE__
,
322 "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
323 REGISTER_NAME (regnum
),
325 REGISTER_RAW_SIZE (regnum
),
326 REGISTER_VIRTUAL_SIZE (regnum
));
327 VALUE_LVAL (reg_val
) = lval
;
328 VALUE_ADDRESS (reg_val
) = addr
;
329 VALUE_REGNO (reg_val
) = regnum
;
330 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
334 /* Given a pointer of type TYPE in target form in BUF, return the
335 address it represents. */
337 unsigned_pointer_to_address (struct type
*type
, void *buf
)
339 return extract_address (buf
, TYPE_LENGTH (type
));
343 signed_pointer_to_address (struct type
*type
, void *buf
)
345 return extract_signed_integer (buf
, TYPE_LENGTH (type
));
348 /* Given an address, store it as a pointer of type TYPE in target
351 unsigned_address_to_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
353 store_address (buf
, TYPE_LENGTH (type
), addr
);
357 address_to_signed_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
359 store_signed_integer (buf
, TYPE_LENGTH (type
), addr
);
362 /* Will calling read_var_value or locate_var_value on SYM end
363 up caring what frame it is being evaluated relative to? SYM must
366 symbol_read_needs_frame (struct symbol
*sym
)
368 switch (SYMBOL_CLASS (sym
))
370 /* All cases listed explicitly so that gcc -Wall will detect it if
371 we failed to consider one. */
376 case LOC_REGPARM_ADDR
:
380 case LOC_BASEREG_ARG
:
381 case LOC_THREAD_LOCAL_STATIC
:
391 /* Getting the address of a label can be done independently of the block,
392 even if some *uses* of that address wouldn't work so well without
396 case LOC_CONST_BYTES
:
398 case LOC_OPTIMIZED_OUT
:
404 /* Given a struct symbol for a variable,
405 and a stack frame id, read the value of the variable
406 and return a (pointer to a) struct value containing the value.
407 If the variable cannot be found, return a zero pointer.
408 If FRAME is NULL, use the selected_frame. */
411 read_var_value (register struct symbol
*var
, struct frame_info
*frame
)
413 register struct value
*v
;
414 struct type
*type
= SYMBOL_TYPE (var
);
418 v
= allocate_value (type
);
419 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
420 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
422 len
= TYPE_LENGTH (type
);
425 frame
= selected_frame
;
427 switch (SYMBOL_CLASS (var
))
430 /* Put the constant back in target format. */
431 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
432 (LONGEST
) SYMBOL_VALUE (var
));
433 VALUE_LVAL (v
) = not_lval
;
437 /* Put the constant back in target format. */
438 if (overlay_debugging
)
441 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
442 SYMBOL_BFD_SECTION (var
));
443 store_typed_address (VALUE_CONTENTS_RAW (v
), type
, addr
);
446 store_typed_address (VALUE_CONTENTS_RAW (v
), type
,
447 SYMBOL_VALUE_ADDRESS (var
));
448 VALUE_LVAL (v
) = not_lval
;
451 case LOC_CONST_BYTES
:
454 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
455 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
456 VALUE_LVAL (v
) = not_lval
;
461 if (overlay_debugging
)
462 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
463 SYMBOL_BFD_SECTION (var
));
465 addr
= SYMBOL_VALUE_ADDRESS (var
);
470 /* The import slot does not have a real address in it from the
471 dynamic loader (dld.sl on HP-UX), if the target hasn't
472 begun execution yet, so check for that. */
475 if (!target_has_execution
)
477 Attempt to access variable defined in different shared object or load module when\n\
478 addresses have not been bound by the dynamic loader. Try again when executable is running.");
480 locaddr
= SYMBOL_VALUE_ADDRESS (var
);
481 loc
= value_at (lookup_pointer_type (type
), locaddr
, NULL
);
482 addr
= value_as_address (loc
);
488 addr
= FRAME_ARGS_ADDRESS (frame
);
491 addr
+= SYMBOL_VALUE (var
);
500 argref
= FRAME_ARGS_ADDRESS (frame
);
503 argref
+= SYMBOL_VALUE (var
);
504 ref
= value_at (lookup_pointer_type (type
), argref
, NULL
);
505 addr
= value_as_address (ref
);
513 addr
= FRAME_LOCALS_ADDRESS (frame
);
514 addr
+= SYMBOL_VALUE (var
);
518 case LOC_BASEREG_ARG
:
519 case LOC_THREAD_LOCAL_STATIC
:
521 struct value
*regval
;
523 regval
= value_from_register (lookup_pointer_type (type
),
524 SYMBOL_BASEREG (var
), frame
);
526 error ("Value of base register not available.");
527 addr
= value_as_address (regval
);
528 addr
+= SYMBOL_VALUE (var
);
533 error ("Cannot look up value of a typedef");
537 if (overlay_debugging
)
538 VALUE_ADDRESS (v
) = symbol_overlayed_address
539 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
541 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
546 case LOC_REGPARM_ADDR
:
549 int regno
= SYMBOL_VALUE (var
);
550 struct value
*regval
;
554 b
= get_frame_block (frame
);
556 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
558 regval
= value_from_register (lookup_pointer_type (type
),
563 error ("Value of register variable not available.");
565 addr
= value_as_address (regval
);
566 VALUE_LVAL (v
) = lval_memory
;
570 regval
= value_from_register (type
, regno
, frame
);
573 error ("Value of register variable not available.");
581 struct minimal_symbol
*msym
;
583 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
586 if (overlay_debugging
)
587 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
588 SYMBOL_BFD_SECTION (msym
));
590 addr
= SYMBOL_VALUE_ADDRESS (msym
);
594 case LOC_OPTIMIZED_OUT
:
595 VALUE_LVAL (v
) = not_lval
;
596 VALUE_OPTIMIZED_OUT (v
) = 1;
600 error ("Cannot look up value of a botched symbol.");
604 VALUE_ADDRESS (v
) = addr
;
609 /* Return a value of type TYPE, stored in register REGNUM, in frame
612 NOTE: returns NULL if register value is not available.
613 Caller will check return value or die! */
616 value_from_register (struct type
*type
, int regnum
, struct frame_info
*frame
)
618 char *raw_buffer
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
621 struct value
*v
= allocate_value (type
);
622 char *value_bytes
= 0;
623 int value_bytes_copied
= 0;
624 int num_storage_locs
;
628 CHECK_TYPEDEF (type
);
629 len
= TYPE_LENGTH (type
);
631 VALUE_REGNO (v
) = regnum
;
633 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
634 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
637 if (num_storage_locs
> 1
638 #ifdef GDB_TARGET_IS_H8500
639 || TYPE_CODE (type
) == TYPE_CODE_PTR
643 /* Value spread across multiple storage locations. */
646 int mem_stor
= 0, reg_stor
= 0;
647 int mem_tracking
= 1;
648 CORE_ADDR last_addr
= 0;
649 CORE_ADDR first_addr
= 0;
651 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
653 /* Copy all of the data out, whereever it may be. */
655 #ifdef GDB_TARGET_IS_H8500
656 /* This piece of hideosity is required because the H8500 treats registers
657 differently depending upon whether they are used as pointers or not. As a
658 pointer, a register needs to have a page register tacked onto the front.
659 An alternate way to do this would be to have gcc output different register
660 numbers for the pointer & non-pointer form of the register. But, it
661 doesn't, so we're stuck with this. */
663 if (TYPE_CODE (type
) == TYPE_CODE_PTR
674 page_regnum
= SEG_D_REGNUM
;
678 page_regnum
= SEG_E_REGNUM
;
682 page_regnum
= SEG_T_REGNUM
;
687 get_saved_register (value_bytes
+ 1,
694 if (register_cached (page_regnum
) == -1)
695 return NULL
; /* register value not available */
697 if (lval
== lval_register
)
704 get_saved_register (value_bytes
+ 2,
711 if (register_cached (regnum
) == -1)
712 return NULL
; /* register value not available */
714 if (lval
== lval_register
)
719 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
724 #endif /* GDB_TARGET_IS_H8500 */
725 for (local_regnum
= regnum
;
726 value_bytes_copied
< len
;
727 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
730 get_saved_register (value_bytes
+ value_bytes_copied
,
737 if (register_cached (local_regnum
) == -1)
738 return NULL
; /* register value not available */
740 if (regnum
== local_regnum
)
742 if (lval
== lval_register
)
750 && (regnum
== local_regnum
751 || addr
== last_addr
));
756 if ((reg_stor
&& mem_stor
)
757 || (mem_stor
&& !mem_tracking
))
758 /* Mixed storage; all of the hassle we just went through was
759 for some good purpose. */
761 VALUE_LVAL (v
) = lval_reg_frame_relative
;
762 VALUE_FRAME (v
) = FRAME_FP (frame
);
763 VALUE_FRAME_REGNUM (v
) = regnum
;
767 VALUE_LVAL (v
) = lval_memory
;
768 VALUE_ADDRESS (v
) = first_addr
;
772 VALUE_LVAL (v
) = lval_register
;
773 VALUE_ADDRESS (v
) = first_addr
;
776 internal_error (__FILE__
, __LINE__
,
777 "value_from_register: Value not stored anywhere!");
779 VALUE_OPTIMIZED_OUT (v
) = optim
;
781 /* Any structure stored in more than one register will always be
782 an integral number of registers. Otherwise, you'd need to do
783 some fiddling with the last register copied here for little
786 /* Copy into the contents section of the value. */
787 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
789 /* Finally do any conversion necessary when extracting this
790 type from more than one register. */
791 #ifdef REGISTER_CONVERT_TO_TYPE
792 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
797 /* Data is completely contained within a single register. Locate the
798 register's contents in a real register or in core;
799 read the data in raw format. */
801 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
803 if (register_cached (regnum
) == -1)
804 return NULL
; /* register value not available */
806 VALUE_OPTIMIZED_OUT (v
) = optim
;
807 VALUE_LVAL (v
) = lval
;
808 VALUE_ADDRESS (v
) = addr
;
810 /* Convert raw data to virtual format if necessary. */
812 if (REGISTER_CONVERTIBLE (regnum
))
814 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
815 raw_buffer
, VALUE_CONTENTS_RAW (v
));
819 /* Raw and virtual formats are the same for this register. */
821 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
&& len
< REGISTER_RAW_SIZE (regnum
))
823 /* Big-endian, and we want less than full size. */
824 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
827 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
833 /* Given a struct symbol for a variable or function,
834 and a stack frame id,
835 return a (pointer to a) struct value containing the properly typed
839 locate_var_value (register struct symbol
*var
, struct frame_info
*frame
)
842 struct type
*type
= SYMBOL_TYPE (var
);
843 struct value
*lazy_value
;
845 /* Evaluate it first; if the result is a memory address, we're fine.
846 Lazy evaluation pays off here. */
848 lazy_value
= read_var_value (var
, frame
);
850 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
852 if (VALUE_LAZY (lazy_value
)
853 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
857 addr
= VALUE_ADDRESS (lazy_value
);
858 val
= value_from_pointer (lookup_pointer_type (type
), addr
);
859 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
863 /* Not a memory address; check what the problem was. */
864 switch (VALUE_LVAL (lazy_value
))
867 gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value
)) != NULL
868 && *REGISTER_NAME (VALUE_REGNO (lazy_value
)) != '\0');
869 error("Address requested for identifier "
870 "\"%s\" which is in register $%s",
871 SYMBOL_SOURCE_NAME (var
),
872 REGISTER_NAME (VALUE_REGNO (lazy_value
)));
875 case lval_reg_frame_relative
:
876 gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != NULL
877 && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)) != '\0');
878 error("Address requested for identifier "
879 "\"%s\" which is in frame register $%s",
880 SYMBOL_SOURCE_NAME (var
),
881 REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value
)));
885 error ("Can't take address of \"%s\" which isn't an lvalue.",
886 SYMBOL_SOURCE_NAME (var
));
889 return 0; /* For lint -- never reached */
This page took 0.047567 seconds and 4 git commands to generate.