1 /* Find a variable's value in memory, for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 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 3 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, see <http://www.gnu.org/licenses/>. */
28 #include "floatformat.h"
29 #include "symfile.h" /* for overlay functions */
31 #include "user-regs.h"
36 /* Basic byte-swapping routines. All 'extract' functions return a
37 host-format integer from a target-format integer at ADDR which is
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 (const gdb_byte
*addr
, int len
,
50 enum bfd_endian byte_order
)
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 (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 gdb_byte
*addr
, int len
,
85 enum bfd_endian byte_order
)
88 const unsigned char *p
;
89 const unsigned char *startaddr
= addr
;
90 const unsigned char *endaddr
= startaddr
+ len
;
92 if (len
> (int) sizeof (ULONGEST
))
94 That operation is not available on integers of more than %d bytes."),
95 (int) sizeof (ULONGEST
));
97 /* Start at the most significant end of the integer, and work towards
98 the least significant. */
100 if (byte_order
== BFD_ENDIAN_BIG
)
102 for (p
= startaddr
; p
< endaddr
; ++p
)
103 retval
= (retval
<< 8) | *p
;
107 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
108 retval
= (retval
<< 8) | *p
;
113 /* Sometimes a long long unsigned integer can be extracted as a
114 LONGEST value. This is done so that we can print these values
115 better. If this integer can be converted to a LONGEST, this
116 function returns 1 and sets *PVAL. Otherwise it returns 0. */
119 extract_long_unsigned_integer (const gdb_byte
*addr
, int orig_len
,
120 enum bfd_endian byte_order
, LONGEST
*pval
)
123 const gdb_byte
*first_addr
;
127 if (byte_order
== BFD_ENDIAN_BIG
)
130 len
> (int) sizeof (LONGEST
) && p
< addr
+ orig_len
;
143 for (p
= addr
+ orig_len
- 1;
144 len
> (int) sizeof (LONGEST
) && p
>= addr
;
154 if (len
<= (int) sizeof (LONGEST
))
156 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
166 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
167 address it represents. */
169 extract_typed_address (const gdb_byte
*buf
, struct type
*type
)
171 if (TYPE_CODE (type
) != TYPE_CODE_PTR
172 && TYPE_CODE (type
) != TYPE_CODE_REF
)
173 internal_error (__FILE__
, __LINE__
,
174 _("extract_typed_address: "
175 "type is not a pointer or reference"));
177 return gdbarch_pointer_to_address (get_type_arch (type
), type
, buf
);
180 /* All 'store' functions accept a host-format integer and store a
181 target-format integer at ADDR which is LEN bytes long. */
184 store_signed_integer (gdb_byte
*addr
, int len
,
185 enum bfd_endian byte_order
, LONGEST val
)
188 gdb_byte
*startaddr
= addr
;
189 gdb_byte
*endaddr
= startaddr
+ len
;
191 /* Start at the least significant end of the integer, and work towards
192 the most significant. */
193 if (byte_order
== BFD_ENDIAN_BIG
)
195 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
203 for (p
= startaddr
; p
< endaddr
; ++p
)
212 store_unsigned_integer (gdb_byte
*addr
, int len
,
213 enum bfd_endian byte_order
, ULONGEST 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 (byte_order
== BFD_ENDIAN_BIG
)
223 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
231 for (p
= startaddr
; p
< endaddr
; ++p
)
239 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
242 store_typed_address (gdb_byte
*buf
, struct type
*type
, CORE_ADDR addr
)
244 if (TYPE_CODE (type
) != TYPE_CODE_PTR
245 && TYPE_CODE (type
) != TYPE_CODE_REF
)
246 internal_error (__FILE__
, __LINE__
,
247 _("store_typed_address: "
248 "type is not a pointer or reference"));
250 gdbarch_address_to_pointer (get_type_arch (type
), type
, buf
, addr
);
255 /* Return a `value' with the contents of (virtual or cooked) register
256 REGNUM as found in the specified FRAME. The register's type is
257 determined by register_type(). */
260 value_of_register (int regnum
, struct frame_info
*frame
)
262 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
263 struct value
*reg_val
;
265 /* User registers lie completely outside of the range of normal
266 registers. Catch them early so that the target never sees them. */
267 if (regnum
>= gdbarch_num_regs (gdbarch
)
268 + gdbarch_num_pseudo_regs (gdbarch
))
269 return value_of_user_reg (regnum
, frame
);
271 reg_val
= value_of_register_lazy (frame
, regnum
);
272 value_fetch_lazy (reg_val
);
276 /* Return a `value' with the contents of (virtual or cooked) register
277 REGNUM as found in the specified FRAME. The register's type is
278 determined by register_type(). The value is not fetched. */
281 value_of_register_lazy (struct frame_info
*frame
, int regnum
)
283 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
284 struct value
*reg_val
;
286 gdb_assert (regnum
< (gdbarch_num_regs (gdbarch
)
287 + gdbarch_num_pseudo_regs (gdbarch
)));
289 /* We should have a valid (i.e. non-sentinel) frame. */
290 gdb_assert (frame_id_p (get_frame_id (frame
)));
292 reg_val
= allocate_value_lazy (register_type (gdbarch
, regnum
));
293 VALUE_LVAL (reg_val
) = lval_register
;
294 VALUE_REGNUM (reg_val
) = regnum
;
295 VALUE_FRAME_ID (reg_val
) = get_frame_id (frame
);
299 /* Given a pointer of type TYPE in target form in BUF, return the
300 address it represents. */
302 unsigned_pointer_to_address (struct gdbarch
*gdbarch
,
303 struct type
*type
, const gdb_byte
*buf
)
305 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
307 return extract_unsigned_integer (buf
, TYPE_LENGTH (type
), byte_order
);
311 signed_pointer_to_address (struct gdbarch
*gdbarch
,
312 struct type
*type
, const gdb_byte
*buf
)
314 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
316 return extract_signed_integer (buf
, TYPE_LENGTH (type
), byte_order
);
319 /* Given an address, store it as a pointer of type TYPE in target
322 unsigned_address_to_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
323 gdb_byte
*buf
, CORE_ADDR addr
)
325 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
327 store_unsigned_integer (buf
, TYPE_LENGTH (type
), byte_order
, addr
);
331 address_to_signed_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
332 gdb_byte
*buf
, CORE_ADDR addr
)
334 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
336 store_signed_integer (buf
, TYPE_LENGTH (type
), byte_order
, addr
);
339 /* Will calling read_var_value or locate_var_value on SYM end
340 up caring what frame it is being evaluated relative to? SYM must
343 symbol_read_needs_frame (struct symbol
*sym
)
345 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
346 return SYMBOL_COMPUTED_OPS (sym
)->read_needs_frame (sym
);
348 switch (SYMBOL_CLASS (sym
))
350 /* All cases listed explicitly so that gcc -Wall will detect it if
351 we failed to consider one. */
353 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
358 case LOC_REGPARM_ADDR
:
368 /* Getting the address of a label can be done independently of the block,
369 even if some *uses* of that address wouldn't work so well without
373 case LOC_CONST_BYTES
:
375 case LOC_OPTIMIZED_OUT
:
381 /* Private data to be used with minsym_lookup_iterator_cb. */
383 struct minsym_lookup_data
385 /* The name of the minimal symbol we are searching for. */
388 /* The field where the callback should store the minimal symbol
389 if found. It should be initialized to NULL before the search
391 struct bound_minimal_symbol result
;
394 /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
395 It searches by name for a minimal symbol within the given OBJFILE.
396 The arguments are passed via CB_DATA, which in reality is a pointer
397 to struct minsym_lookup_data. */
400 minsym_lookup_iterator_cb (struct objfile
*objfile
, void *cb_data
)
402 struct minsym_lookup_data
*data
= (struct minsym_lookup_data
*) cb_data
;
404 gdb_assert (data
->result
.minsym
== NULL
);
406 data
->result
= lookup_minimal_symbol (data
->name
, NULL
, objfile
);
408 /* The iterator should stop iff a match was found. */
409 return (data
->result
.minsym
!= NULL
);
412 /* A default implementation for the "la_read_var_value" hook in
413 the language vector which should work in most situations. */
416 default_read_var_value (struct symbol
*var
, struct frame_info
*frame
)
419 struct type
*type
= SYMBOL_TYPE (var
);
422 /* Call check_typedef on our type to make sure that, if TYPE is
423 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
424 instead of zero. However, we do not replace the typedef type by the
425 target type, because we want to keep the typedef in order to be able to
426 set the returned value type description correctly. */
427 check_typedef (type
);
429 if (symbol_read_needs_frame (var
))
432 if (SYMBOL_COMPUTED_OPS (var
) != NULL
)
433 return SYMBOL_COMPUTED_OPS (var
)->read_variable (var
, frame
);
435 switch (SYMBOL_CLASS (var
))
438 if (is_dynamic_type (type
))
440 /* Value is a constant byte-sequence and needs no memory access. */
441 type
= resolve_dynamic_type (type
, /* Unused address. */ 0);
443 /* Put the constant back in target format. */
444 v
= allocate_value (type
);
445 store_signed_integer (value_contents_raw (v
), TYPE_LENGTH (type
),
446 gdbarch_byte_order (get_type_arch (type
)),
447 (LONGEST
) SYMBOL_VALUE (var
));
448 VALUE_LVAL (v
) = not_lval
;
452 /* Put the constant back in target format. */
453 v
= allocate_value (type
);
454 if (overlay_debugging
)
457 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
458 SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (var
),
461 store_typed_address (value_contents_raw (v
), type
, addr
);
464 store_typed_address (value_contents_raw (v
), type
,
465 SYMBOL_VALUE_ADDRESS (var
));
466 VALUE_LVAL (v
) = not_lval
;
469 case LOC_CONST_BYTES
:
470 if (is_dynamic_type (type
))
472 /* Value is a constant byte-sequence and needs no memory access. */
473 type
= resolve_dynamic_type (type
, /* Unused address. */ 0);
475 v
= allocate_value (type
);
476 memcpy (value_contents_raw (v
), SYMBOL_VALUE_BYTES (var
),
478 VALUE_LVAL (v
) = not_lval
;
482 if (overlay_debugging
)
483 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
484 SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (var
),
487 addr
= SYMBOL_VALUE_ADDRESS (var
);
491 addr
= get_frame_args_address (frame
);
493 error (_("Unknown argument list address for `%s'."),
494 SYMBOL_PRINT_NAME (var
));
495 addr
+= SYMBOL_VALUE (var
);
503 argref
= get_frame_args_address (frame
);
505 error (_("Unknown argument list address for `%s'."),
506 SYMBOL_PRINT_NAME (var
));
507 argref
+= SYMBOL_VALUE (var
);
508 ref
= value_at (lookup_pointer_type (type
), argref
);
509 addr
= value_as_address (ref
);
514 addr
= get_frame_locals_address (frame
);
515 addr
+= SYMBOL_VALUE (var
);
519 error (_("Cannot look up value of a typedef `%s'."),
520 SYMBOL_PRINT_NAME (var
));
524 if (overlay_debugging
)
525 addr
= symbol_overlayed_address
526 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (var
),
529 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
533 case LOC_REGPARM_ADDR
:
535 int regno
= SYMBOL_REGISTER_OPS (var
)
536 ->register_number (var
, get_frame_arch (frame
));
537 struct value
*regval
;
539 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
541 regval
= value_from_register (lookup_pointer_type (type
),
546 error (_("Value of register variable not available for `%s'."),
547 SYMBOL_PRINT_NAME (var
));
549 addr
= value_as_address (regval
);
553 regval
= value_from_register (type
, regno
, frame
);
556 error (_("Value of register variable not available for `%s'."),
557 SYMBOL_PRINT_NAME (var
));
564 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
568 struct minsym_lookup_data lookup_data
;
569 struct minimal_symbol
*msym
;
570 struct obj_section
*obj_section
;
572 memset (&lookup_data
, 0, sizeof (lookup_data
));
573 lookup_data
.name
= SYMBOL_LINKAGE_NAME (var
);
575 gdbarch_iterate_over_objfiles_in_search_order
576 (get_objfile_arch (SYMBOL_OBJFILE (var
)),
577 minsym_lookup_iterator_cb
, &lookup_data
,
578 SYMBOL_OBJFILE (var
));
579 msym
= lookup_data
.result
.minsym
;
582 error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var
));
583 if (overlay_debugging
)
584 addr
= symbol_overlayed_address (BMSYMBOL_VALUE_ADDRESS (lookup_data
.result
),
585 MSYMBOL_OBJ_SECTION (lookup_data
.result
.objfile
,
588 addr
= BMSYMBOL_VALUE_ADDRESS (lookup_data
.result
);
590 obj_section
= MSYMBOL_OBJ_SECTION (lookup_data
.result
.objfile
, msym
);
592 && (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
593 addr
= target_translate_tls_address (obj_section
->objfile
, addr
);
597 case LOC_OPTIMIZED_OUT
:
598 return allocate_optimized_out_value (type
);
601 error (_("Cannot look up value of a botched symbol `%s'."),
602 SYMBOL_PRINT_NAME (var
));
606 v
= value_at_lazy (type
, addr
);
610 /* Calls VAR's language la_read_var_value hook with the given arguments. */
613 read_var_value (struct symbol
*var
, struct frame_info
*frame
)
615 const struct language_defn
*lang
= language_def (SYMBOL_LANGUAGE (var
));
617 gdb_assert (lang
!= NULL
);
618 gdb_assert (lang
->la_read_var_value
!= NULL
);
620 return lang
->la_read_var_value (var
, frame
);
623 /* Install default attributes for register values. */
626 default_value_from_register (struct gdbarch
*gdbarch
, struct type
*type
,
627 int regnum
, struct frame_id frame_id
)
629 int len
= TYPE_LENGTH (type
);
630 struct value
*value
= allocate_value (type
);
632 VALUE_LVAL (value
) = lval_register
;
633 VALUE_FRAME_ID (value
) = frame_id
;
634 VALUE_REGNUM (value
) = regnum
;
636 /* Any structure stored in more than one register will always be
637 an integral number of registers. Otherwise, you need to do
638 some fiddling with the last register copied here for little
640 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
641 && len
< register_size (gdbarch
, regnum
))
642 /* Big-endian, and we want less than full size. */
643 set_value_offset (value
, register_size (gdbarch
, regnum
) - len
);
645 set_value_offset (value
, 0);
650 /* VALUE must be an lval_register value. If regnum is the value's
651 associated register number, and len the length of the values type,
652 read one or more registers in FRAME, starting with register REGNUM,
653 until we've read LEN bytes.
655 If any of the registers we try to read are optimized out, then mark the
656 complete resulting value as optimized out. */
659 read_frame_register_value (struct value
*value
, struct frame_info
*frame
)
661 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
663 int reg_offset
= value_offset (value
);
664 int regnum
= VALUE_REGNUM (value
);
665 int len
= TYPE_LENGTH (check_typedef (value_type (value
)));
667 gdb_assert (VALUE_LVAL (value
) == lval_register
);
669 /* Skip registers wholly inside of REG_OFFSET. */
670 while (reg_offset
>= register_size (gdbarch
, regnum
))
672 reg_offset
-= register_size (gdbarch
, regnum
);
679 struct value
*regval
= get_frame_register_value (frame
, regnum
);
680 int reg_len
= TYPE_LENGTH (value_type (regval
)) - reg_offset
;
682 /* If the register length is larger than the number of bytes
683 remaining to copy, then only copy the appropriate bytes. */
687 value_contents_copy (value
, offset
, regval
, reg_offset
, reg_len
);
696 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
699 value_from_register (struct type
*type
, int regnum
, struct frame_info
*frame
)
701 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
702 struct type
*type1
= check_typedef (type
);
705 if (gdbarch_convert_register_p (gdbarch
, regnum
, type1
))
707 int optim
, unavail
, ok
;
709 /* The ISA/ABI need to something weird when obtaining the
710 specified value from this register. It might need to
711 re-order non-adjacent, starting with REGNUM (see MIPS and
712 i386). It might need to convert the [float] register into
713 the corresponding [integer] type (see Alpha). The assumption
714 is that gdbarch_register_to_value populates the entire value
715 including the location. */
716 v
= allocate_value (type
);
717 VALUE_LVAL (v
) = lval_register
;
718 VALUE_FRAME_ID (v
) = get_frame_id (frame
);
719 VALUE_REGNUM (v
) = regnum
;
720 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type1
,
721 value_contents_raw (v
), &optim
,
727 mark_value_bytes_optimized_out (v
, 0, TYPE_LENGTH (type
));
729 mark_value_bytes_unavailable (v
, 0, TYPE_LENGTH (type
));
734 /* Construct the value. */
735 v
= gdbarch_value_from_register (gdbarch
, type
,
736 regnum
, get_frame_id (frame
));
739 read_frame_register_value (v
, frame
);
745 /* Return contents of register REGNUM in frame FRAME as address.
746 Will abort if register value is not available. */
749 address_from_register (int regnum
, struct frame_info
*frame
)
751 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
752 struct type
*type
= builtin_type (gdbarch
)->builtin_data_ptr
;
756 /* This routine may be called during early unwinding, at a time
757 where the ID of FRAME is not yet known. Calling value_from_register
758 would therefore abort in get_frame_id. However, since we only need
759 a temporary value that is never used as lvalue, we actually do not
760 really need to set its VALUE_FRAME_ID. Therefore, we re-implement
761 the core of value_from_register, but use the null_frame_id. */
763 /* Some targets require a special conversion routine even for plain
764 pointer types. Avoid constructing a value object in those cases. */
765 if (gdbarch_convert_register_p (gdbarch
, regnum
, type
))
767 gdb_byte
*buf
= alloca (TYPE_LENGTH (type
));
768 int optim
, unavail
, ok
;
770 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type
,
771 buf
, &optim
, &unavail
);
774 /* This function is used while computing a location expression.
775 Complain about the value being optimized out, rather than
776 letting value_as_address complain about some random register
777 the expression depends on not being saved. */
778 error_value_optimized_out ();
781 return unpack_long (type
, buf
);
784 value
= gdbarch_value_from_register (gdbarch
, type
, regnum
, null_frame_id
);
785 read_frame_register_value (value
, frame
);
787 if (value_optimized_out (value
))
789 /* This function is used while computing a location expression.
790 Complain about the value being optimized out, rather than
791 letting value_as_address complain about some random register
792 the expression depends on not being saved. */
793 error_value_optimized_out ();
796 result
= value_as_address (value
);
797 release_value (value
);