1 /* Find a variable's value in memory, for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
20 #include "event-top.h"
21 #include "extract-store-integer.h"
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... */
51 value_of_register (int regnum
, const frame_info_ptr
&next_frame
)
53 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
55 /* User registers lie completely outside of the range of normal
56 registers. Catch them early so that the target never sees them. */
57 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
58 return value_of_user_reg (regnum
, get_prev_frame_always (next_frame
));
60 value
*reg_val
= value_of_register_lazy (next_frame
, regnum
);
61 reg_val
->fetch_lazy ();
68 value_of_register_lazy (const frame_info_ptr
&next_frame
, int regnum
)
70 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
72 gdb_assert (regnum
< gdbarch_num_cooked_regs (gdbarch
));
73 gdb_assert (next_frame
!= nullptr);
75 return value::allocate_register_lazy (next_frame
, regnum
);
78 /* Given a pointer of type TYPE in target form in BUF, return the
79 address it represents. */
81 unsigned_pointer_to_address (struct gdbarch
*gdbarch
,
82 struct type
*type
, const gdb_byte
*buf
)
84 enum bfd_endian byte_order
= type_byte_order (type
);
86 return extract_unsigned_integer (buf
, type
->length (), byte_order
);
90 signed_pointer_to_address (struct gdbarch
*gdbarch
,
91 struct type
*type
, const gdb_byte
*buf
)
93 enum bfd_endian byte_order
= type_byte_order (type
);
95 return extract_signed_integer (buf
, type
->length (), byte_order
);
98 /* Given an address, store it as a pointer of type TYPE in target
101 unsigned_address_to_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
102 gdb_byte
*buf
, CORE_ADDR addr
)
104 enum bfd_endian byte_order
= type_byte_order (type
);
106 store_unsigned_integer (buf
, type
->length (), byte_order
, addr
);
110 address_to_signed_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
111 gdb_byte
*buf
, CORE_ADDR addr
)
113 enum bfd_endian byte_order
= type_byte_order (type
);
115 store_signed_integer (buf
, type
->length (), byte_order
, addr
);
120 enum symbol_needs_kind
121 symbol_read_needs (struct symbol
*sym
)
123 if (const symbol_computed_ops
*computed_ops
= sym
->computed_ops ();
124 computed_ops
!= nullptr)
125 return computed_ops
->get_symbol_read_needs (sym
);
127 switch (sym
->aclass ())
129 /* All cases listed explicitly so that gcc -Wall will detect it if
130 we failed to consider one. */
132 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
137 case LOC_REGPARM_ADDR
:
139 return SYMBOL_NEEDS_FRAME
;
147 /* Getting the address of a label can be done independently of the block,
148 even if some *uses* of that address wouldn't work so well without
152 case LOC_CONST_BYTES
:
154 case LOC_OPTIMIZED_OUT
:
155 return SYMBOL_NEEDS_NONE
;
157 return SYMBOL_NEEDS_FRAME
;
163 symbol_read_needs_frame (struct symbol
*sym
)
165 return symbol_read_needs (sym
) == SYMBOL_NEEDS_FRAME
;
168 /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
169 rules, look for the frame that is actually hosting VAR and return it. If,
170 for some reason, we found no such frame, return NULL.
172 This kind of computation is necessary to correctly handle lexically nested
175 Note that in some cases, we know what scope VAR comes from but we cannot
176 reach the specific frame that hosts the instance of VAR we are looking for.
177 For backward compatibility purposes (with old compilers), we then look for
178 the first frame that can host it. */
180 static frame_info_ptr
181 get_hosting_frame (struct symbol
*var
, const struct block
*var_block
,
182 const frame_info_ptr
&initial_frame
)
184 const struct block
*frame_block
= NULL
;
186 if (!symbol_read_needs_frame (var
))
189 /* Some symbols for local variables have no block: this happens when they are
190 not produced by a debug information reader, for instance when GDB creates
191 synthetic symbols. Without block information, we must assume they are
192 local to FRAME. In this case, there is nothing to do. */
193 else if (var_block
== NULL
)
194 return initial_frame
;
196 /* We currently assume that all symbols with a location list need a frame.
197 This is true in practice because selecting the location description
198 requires to compute the CFA, hence requires a frame. However we have
199 tests that embed global/static symbols with null location lists.
200 We want to get <optimized out> instead of <frame required> when evaluating
201 them so return a frame instead of raising an error. */
202 else if (var_block
->is_global_block () || var_block
->is_static_block ())
203 return initial_frame
;
205 /* We have to handle the "my_func::my_local_var" notation. This requires us
206 to look for upper frames when we find no block for the current frame: here
207 and below, handle when frame_block == NULL. */
208 if (initial_frame
!= nullptr)
209 frame_block
= get_frame_block (initial_frame
, NULL
);
211 /* Climb up the call stack until reaching the frame we are looking for. */
212 frame_info_ptr frame
= initial_frame
;
213 while (frame
!= NULL
&& frame_block
!= var_block
)
215 /* Stacks can be quite deep: give the user a chance to stop this. */
218 if (frame_block
== NULL
)
220 frame
= get_prev_frame (frame
);
223 frame_block
= get_frame_block (frame
, NULL
);
226 /* If we failed to find the proper frame, fallback to the heuristic
228 else if (frame_block
->is_global_block ())
234 /* Assuming we have a block for this frame: if we are at the function
235 level, the immediate upper lexical block is in an outer function:
236 follow the static link. */
237 else if (frame_block
->function () != nullptr)
239 frame
= frame_follow_static_link (frame
);
240 if (frame
!= nullptr)
242 frame_block
= get_frame_block (frame
, nullptr);
243 if (frame_block
== nullptr)
249 /* We must be in some function nested lexical block. Just get the
250 outer block: both must share the same frame. */
251 frame_block
= frame_block
->superblock ();
254 /* Old compilers may not provide a static link, or they may provide an
255 invalid one. For such cases, fallback on the old way to evaluate
256 non-local references: just climb up the call stack and pick the first
257 frame that contains the variable we are looking for. */
260 frame
= block_innermost_frame (var_block
);
263 if (var_block
->function ()
264 && !var_block
->inlined_p ()
265 && var_block
->function ()->print_name ())
266 error (_("No frame is currently executing in block %s."),
267 var_block
->function ()->print_name ());
269 error (_("No frame is currently executing in specified"
277 /* See language.h. */
280 language_defn::read_var_value (struct symbol
*var
,
281 const struct block
*var_block
,
282 const frame_info_ptr
&frame_param
) const
285 struct type
*type
= var
->type ();
287 enum symbol_needs_kind sym_need
;
288 frame_info_ptr frame
= frame_param
;
290 /* Call check_typedef on our type to make sure that, if TYPE is
291 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
292 instead of zero. However, we do not replace the typedef type by the
293 target type, because we want to keep the typedef in order to be able to
294 set the returned value type description correctly. */
295 check_typedef (type
);
297 sym_need
= symbol_read_needs (var
);
298 if (sym_need
== SYMBOL_NEEDS_FRAME
)
299 gdb_assert (frame
!= NULL
);
300 else if (sym_need
== SYMBOL_NEEDS_REGISTERS
&& !target_has_registers ())
301 error (_("Cannot read `%s' without registers"), var
->print_name ());
304 frame
= get_hosting_frame (var
, var_block
, frame
);
306 if (const symbol_computed_ops
*computed_ops
= var
->computed_ops ())
307 return computed_ops
->read_variable (var
, frame
);
309 switch (var
->aclass ())
312 if (is_dynamic_type (type
))
314 gdb_byte bytes
[sizeof (LONGEST
)];
316 size_t len
= std::min (sizeof (LONGEST
), (size_t) type
->length ());
317 store_unsigned_integer (bytes
, len
,
318 type_byte_order (type
),
319 var
->value_longest ());
320 gdb::array_view
<const gdb_byte
> view (bytes
, len
);
322 /* Value is a constant byte-sequence. */
323 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
325 /* Put the constant back in target format. */
326 v
= value::allocate (type
);
327 store_signed_integer (v
->contents_raw ().data (), type
->length (),
328 type_byte_order (type
), var
->value_longest ());
329 v
->set_lval (not_lval
);
334 /* Put the constant back in target format. */
335 if (overlay_debugging
)
337 struct objfile
*var_objfile
= var
->objfile ();
338 addr
= symbol_overlayed_address (var
->value_address (),
339 var
->obj_section (var_objfile
));
342 addr
= var
->value_address ();
344 /* First convert the CORE_ADDR to a function pointer type, this
345 ensures the gdbarch knows what type of pointer we are
346 manipulating when value_from_pointer is called. */
347 type
= builtin_type (var
->arch ())->builtin_func_ptr
;
348 v
= value_from_pointer (type
, addr
);
350 /* But we want to present the value as 'void *', so cast it to the
351 required type now, this will not change the values bit
353 struct type
*void_ptr_type
354 = builtin_type (var
->arch ())->builtin_data_ptr
;
355 v
= value_cast_pointers (void_ptr_type
, v
, 0);
356 v
->set_lval (not_lval
);
360 case LOC_CONST_BYTES
:
361 if (is_dynamic_type (type
))
363 gdb::array_view
<const gdb_byte
> view (var
->value_bytes (),
366 /* Value is a constant byte-sequence. */
367 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
369 v
= value::allocate (type
);
370 memcpy (v
->contents_raw ().data (), var
->value_bytes (),
372 v
->set_lval (not_lval
);
376 if (overlay_debugging
)
378 = symbol_overlayed_address (var
->value_address (),
379 var
->obj_section (var
->objfile ()));
381 addr
= var
->value_address ();
385 addr
= get_frame_args_address (frame
);
387 error (_("Unknown argument list address for `%s'."),
389 addr
+= var
->value_longest ();
397 argref
= get_frame_args_address (frame
);
399 error (_("Unknown argument list address for `%s'."),
401 argref
+= var
->value_longest ();
402 ref
= value_at (lookup_pointer_type (type
), argref
);
403 addr
= value_as_address (ref
);
408 addr
= get_frame_locals_address (frame
);
409 addr
+= var
->value_longest ();
413 error (_("Cannot look up value of a typedef `%s'."),
418 if (overlay_debugging
)
419 addr
= symbol_overlayed_address
420 (var
->value_block ()->entry_pc (),
421 var
->obj_section (var
->objfile ()));
423 addr
= var
->value_block ()->entry_pc ();
427 case LOC_REGPARM_ADDR
:
429 const symbol_register_ops
*reg_ops
= var
->register_ops ();
430 int regno
= reg_ops
->register_number (var
, get_frame_arch (frame
));
432 if (var
->aclass () == LOC_REGPARM_ADDR
)
433 addr
= value_as_address
434 (value_from_register (lookup_pointer_type (type
), regno
, frame
));
436 return value_from_register (type
, regno
, frame
);
441 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
445 struct obj_section
*obj_section
;
446 bound_minimal_symbol bmsym
;
448 gdbarch_iterate_over_objfiles_in_search_order
450 [var
, &bmsym
] (objfile
*objfile
)
452 bmsym
= lookup_minimal_symbol (var
->linkage_name (), nullptr,
455 /* Stop if a match is found. */
456 return bmsym
.minsym
!= nullptr;
460 /* If we can't find the minsym there's a problem in the symbol info.
461 The symbol exists in the debug info, but it's missing in the minsym
463 if (bmsym
.minsym
== nullptr)
465 const char *flavour_name
466 = objfile_flavour_name (var
->objfile ());
468 /* We can't get here unless we've opened the file, so flavour_name
470 gdb_assert (flavour_name
!= NULL
);
471 error (_("Missing %s symbol \"%s\"."),
472 flavour_name
, var
->linkage_name ());
475 obj_section
= bmsym
.minsym
->obj_section (bmsym
.objfile
);
476 /* Relocate address, unless there is no section or the variable is
478 if (obj_section
== NULL
479 || (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
480 addr
= CORE_ADDR (bmsym
.minsym
->unrelocated_address ());
482 addr
= bmsym
.value_address ();
483 if (overlay_debugging
)
484 addr
= symbol_overlayed_address (addr
, obj_section
);
485 /* Determine address of TLS variable. */
487 && (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
488 addr
= target_translate_tls_address (obj_section
->objfile
, addr
);
492 case LOC_OPTIMIZED_OUT
:
493 if (is_dynamic_type (type
))
494 type
= resolve_dynamic_type (type
, {}, /* Unused address. */ 0);
495 return value::allocate_optimized_out (type
);
498 error (_("Cannot look up value of a botched symbol `%s'."),
503 v
= value_at_lazy (type
, addr
);
507 /* Calls VAR's language read_var_value hook with the given arguments. */
510 read_var_value (struct symbol
*var
, const struct block
*var_block
,
511 const frame_info_ptr
&frame
)
513 const struct language_defn
*lang
= language_def (var
->language ());
515 gdb_assert (lang
!= NULL
);
517 return lang
->read_var_value (var
, var_block
, frame
);
520 /* Install default attributes for register values. */
523 default_value_from_register (gdbarch
*gdbarch
, type
*type
, int regnum
,
524 const frame_info_ptr
&this_frame
)
527 = value::allocate_register (get_next_frame_sentinel_okay (this_frame
),
530 /* Any structure stored in more than one register will always be
531 an integral number of registers. Otherwise, you need to do
532 some fiddling with the last register copied here for little
534 if (type_byte_order (type
) == BFD_ENDIAN_BIG
535 && type
->length () < register_size (gdbarch
, regnum
))
536 /* Big-endian, and we want less than full size. */
537 value
->set_offset (register_size (gdbarch
, regnum
) - type
->length ());
539 value
->set_offset (0);
544 /* VALUE must be an lval_register value. If regnum is the value's
545 associated register number, and len the length of the value's type,
546 read one or more registers in VALUE's frame, starting with register REGNUM,
547 until we've read LEN bytes.
549 If any of the registers we try to read are optimized out, then mark the
550 complete resulting value as optimized out. */
553 read_frame_register_value (value
*value
)
555 gdb_assert (value
->lval () == lval_register
);
557 frame_info_ptr next_frame
= frame_find_by_id (value
->next_frame_id ());
558 gdb_assert (next_frame
!= nullptr);
560 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
562 LONGEST reg_offset
= value
->offset ();
563 int regnum
= value
->regnum ();
564 int len
= type_length_units (check_typedef (value
->type ()));
566 /* Skip registers wholly inside of REG_OFFSET. */
567 while (reg_offset
>= register_size (gdbarch
, regnum
))
569 reg_offset
-= register_size (gdbarch
, regnum
);
576 struct value
*regval
= frame_unwind_register_value (next_frame
, regnum
);
577 int reg_len
= type_length_units (regval
->type ()) - reg_offset
;
579 /* If the register length is larger than the number of bytes
580 remaining to copy, then only copy the appropriate bytes. */
584 regval
->contents_copy (value
, offset
, reg_offset
, reg_len
);
593 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
596 value_from_register (struct type
*type
, int regnum
, const frame_info_ptr
&frame
)
598 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
599 struct type
*type1
= check_typedef (type
);
602 if (gdbarch_convert_register_p (gdbarch
, regnum
, type1
))
604 int optim
, unavail
, ok
;
606 /* The ISA/ABI need to something weird when obtaining the
607 specified value from this register. It might need to
608 re-order non-adjacent, starting with REGNUM (see MIPS and
609 i386). It might need to convert the [float] register into
610 the corresponding [integer] type (see Alpha). The assumption
611 is that gdbarch_register_to_value populates the entire value
612 including the location. */
613 v
= value::allocate_register (get_next_frame_sentinel_okay (frame
),
615 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type1
,
616 v
->contents_raw ().data (), &optim
,
622 v
->mark_bytes_optimized_out (0, type
->length ());
624 v
->mark_bytes_unavailable (0, type
->length ());
629 /* Construct the value. */
630 v
= gdbarch_value_from_register (gdbarch
, type
, regnum
, frame
);
633 read_frame_register_value (v
);
639 /* Return contents of register REGNUM in frame FRAME as address.
640 Will abort if register value is not available. */
643 address_from_register (int regnum
, const frame_info_ptr
&frame
)
645 type
*type
= builtin_type (get_frame_arch (frame
))->builtin_data_ptr
;
646 value_ref_ptr v
= release_value (value_from_register (type
, regnum
, frame
));
648 if (v
->optimized_out ())
650 /* This function is used while computing a location expression.
651 Complain about the value being optimized out, rather than
652 letting value_as_address complain about some random register
653 the expression depends on not being saved. */
654 error_value_optimized_out ();
657 return value_as_address (v
.get ());