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 "extract-store-integer.h"
30 #include "user-regs.h"
35 /* Basic byte-swapping routines. All 'extract' functions return a
36 host-format integer from a target-format integer at ADDR which is
39 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
40 /* 8 bit characters are a pretty safe assumption these days, so we
41 assume it throughout all these swapping routines. If we had to deal with
42 9 bit characters, we would need to make len be in bits and would have
43 to re-write these routines... */
50 value_of_register (int regnum
, const frame_info_ptr
&next_frame
)
52 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
54 /* User registers lie completely outside of the range of normal
55 registers. Catch them early so that the target never sees them. */
56 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
57 return value_of_user_reg (regnum
, get_prev_frame_always (next_frame
));
59 value
*reg_val
= value_of_register_lazy (next_frame
, regnum
);
60 reg_val
->fetch_lazy ();
67 value_of_register_lazy (const frame_info_ptr
&next_frame
, int regnum
)
69 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
71 gdb_assert (regnum
< gdbarch_num_cooked_regs (gdbarch
));
72 gdb_assert (next_frame
!= nullptr);
74 return value::allocate_register_lazy (next_frame
, regnum
);
77 /* Given a pointer of type TYPE in target form in BUF, return the
78 address it represents. */
80 unsigned_pointer_to_address (struct gdbarch
*gdbarch
,
81 struct type
*type
, const gdb_byte
*buf
)
83 enum bfd_endian byte_order
= type_byte_order (type
);
85 return extract_unsigned_integer (buf
, type
->length (), byte_order
);
89 signed_pointer_to_address (struct gdbarch
*gdbarch
,
90 struct type
*type
, const gdb_byte
*buf
)
92 enum bfd_endian byte_order
= type_byte_order (type
);
94 return extract_signed_integer (buf
, type
->length (), byte_order
);
97 /* Given an address, store it as a pointer of type TYPE in target
100 unsigned_address_to_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
101 gdb_byte
*buf
, CORE_ADDR addr
)
103 enum bfd_endian byte_order
= type_byte_order (type
);
105 store_unsigned_integer (buf
, type
->length (), byte_order
, addr
);
109 address_to_signed_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
110 gdb_byte
*buf
, CORE_ADDR addr
)
112 enum bfd_endian byte_order
= type_byte_order (type
);
114 store_signed_integer (buf
, type
->length (), byte_order
, addr
);
119 enum symbol_needs_kind
120 symbol_read_needs (struct symbol
*sym
)
122 if (const symbol_computed_ops
*computed_ops
= sym
->computed_ops ();
123 computed_ops
!= nullptr)
124 return computed_ops
->get_symbol_read_needs (sym
);
126 switch (sym
->aclass ())
128 /* All cases listed explicitly so that gcc -Wall will detect it if
129 we failed to consider one. */
131 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
136 case LOC_REGPARM_ADDR
:
138 return SYMBOL_NEEDS_FRAME
;
146 /* Getting the address of a label can be done independently of the block,
147 even if some *uses* of that address wouldn't work so well without
151 case LOC_CONST_BYTES
:
153 case LOC_OPTIMIZED_OUT
:
154 return SYMBOL_NEEDS_NONE
;
156 return SYMBOL_NEEDS_FRAME
;
162 symbol_read_needs_frame (struct symbol
*sym
)
164 return symbol_read_needs (sym
) == SYMBOL_NEEDS_FRAME
;
167 /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
168 rules, look for the frame that is actually hosting VAR and return it. If,
169 for some reason, we found no such frame, return NULL.
171 This kind of computation is necessary to correctly handle lexically nested
174 Note that in some cases, we know what scope VAR comes from but we cannot
175 reach the specific frame that hosts the instance of VAR we are looking for.
176 For backward compatibility purposes (with old compilers), we then look for
177 the first frame that can host it. */
179 static frame_info_ptr
180 get_hosting_frame (struct symbol
*var
, const struct block
*var_block
,
181 const frame_info_ptr
&initial_frame
)
183 const struct block
*frame_block
= NULL
;
185 if (!symbol_read_needs_frame (var
))
188 /* Some symbols for local variables have no block: this happens when they are
189 not produced by a debug information reader, for instance when GDB creates
190 synthetic symbols. Without block information, we must assume they are
191 local to FRAME. In this case, there is nothing to do. */
192 else if (var_block
== NULL
)
193 return initial_frame
;
195 /* We currently assume that all symbols with a location list need a frame.
196 This is true in practice because selecting the location description
197 requires to compute the CFA, hence requires a frame. However we have
198 tests that embed global/static symbols with null location lists.
199 We want to get <optimized out> instead of <frame required> when evaluating
200 them so return a frame instead of raising an error. */
201 else if (var_block
->is_global_block () || var_block
->is_static_block ())
202 return initial_frame
;
204 /* We have to handle the "my_func::my_local_var" notation. This requires us
205 to look for upper frames when we find no block for the current frame: here
206 and below, handle when frame_block == NULL. */
207 if (initial_frame
!= nullptr)
208 frame_block
= get_frame_block (initial_frame
, NULL
);
210 /* Climb up the call stack until reaching the frame we are looking for. */
211 frame_info_ptr frame
= initial_frame
;
212 while (frame
!= NULL
&& frame_block
!= var_block
)
214 /* Stacks can be quite deep: give the user a chance to stop this. */
217 if (frame_block
== NULL
)
219 frame
= get_prev_frame (frame
);
222 frame_block
= get_frame_block (frame
, NULL
);
225 /* If we failed to find the proper frame, fallback to the heuristic
227 else if (frame_block
->is_global_block ())
233 /* Assuming we have a block for this frame: if we are at the function
234 level, the immediate upper lexical block is in an outer function:
235 follow the static link. */
236 else if (frame_block
->function () != nullptr)
238 frame
= frame_follow_static_link (frame
);
239 if (frame
!= nullptr)
241 frame_block
= get_frame_block (frame
, nullptr);
242 if (frame_block
== nullptr)
248 /* We must be in some function nested lexical block. Just get the
249 outer block: both must share the same frame. */
250 frame_block
= frame_block
->superblock ();
253 /* Old compilers may not provide a static link, or they may provide an
254 invalid one. For such cases, fallback on the old way to evaluate
255 non-local references: just climb up the call stack and pick the first
256 frame that contains the variable we are looking for. */
259 frame
= block_innermost_frame (var_block
);
262 if (var_block
->function ()
263 && !var_block
->inlined_p ()
264 && var_block
->function ()->print_name ())
265 error (_("No frame is currently executing in block %s."),
266 var_block
->function ()->print_name ());
268 error (_("No frame is currently executing in specified"
276 /* See language.h. */
279 language_defn::read_var_value (struct symbol
*var
,
280 const struct block
*var_block
,
281 const frame_info_ptr
&frame_param
) const
284 struct type
*type
= var
->type ();
286 enum symbol_needs_kind sym_need
;
287 frame_info_ptr frame
= frame_param
;
289 /* Call check_typedef on our type to make sure that, if TYPE is
290 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
291 instead of zero. However, we do not replace the typedef type by the
292 target type, because we want to keep the typedef in order to be able to
293 set the returned value type description correctly. */
294 check_typedef (type
);
296 sym_need
= symbol_read_needs (var
);
297 if (sym_need
== SYMBOL_NEEDS_FRAME
)
298 gdb_assert (frame
!= NULL
);
299 else if (sym_need
== SYMBOL_NEEDS_REGISTERS
&& !target_has_registers ())
300 error (_("Cannot read `%s' without registers"), var
->print_name ());
303 frame
= get_hosting_frame (var
, var_block
, frame
);
305 if (const symbol_computed_ops
*computed_ops
= var
->computed_ops ())
306 return computed_ops
->read_variable (var
, frame
);
308 switch (var
->aclass ())
311 if (is_dynamic_type (type
))
313 gdb_byte bytes
[sizeof (LONGEST
)];
315 size_t len
= std::min (sizeof (LONGEST
), (size_t) type
->length ());
316 store_unsigned_integer (bytes
, len
,
317 type_byte_order (type
),
318 var
->value_longest ());
319 gdb::array_view
<const gdb_byte
> view (bytes
, len
);
321 /* Value is a constant byte-sequence. */
322 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
324 /* Put the constant back in target format. */
325 v
= value::allocate (type
);
326 store_signed_integer (v
->contents_raw ().data (), type
->length (),
327 type_byte_order (type
), var
->value_longest ());
328 v
->set_lval (not_lval
);
333 /* Put the constant back in target format. */
334 if (overlay_debugging
)
336 struct objfile
*var_objfile
= var
->objfile ();
337 addr
= symbol_overlayed_address (var
->value_address (),
338 var
->obj_section (var_objfile
));
341 addr
= var
->value_address ();
343 /* First convert the CORE_ADDR to a function pointer type, this
344 ensures the gdbarch knows what type of pointer we are
345 manipulating when value_from_pointer is called. */
346 type
= builtin_type (var
->arch ())->builtin_func_ptr
;
347 v
= value_from_pointer (type
, addr
);
349 /* But we want to present the value as 'void *', so cast it to the
350 required type now, this will not change the values bit
352 struct type
*void_ptr_type
353 = builtin_type (var
->arch ())->builtin_data_ptr
;
354 v
= value_cast_pointers (void_ptr_type
, v
, 0);
355 v
->set_lval (not_lval
);
359 case LOC_CONST_BYTES
:
360 if (is_dynamic_type (type
))
362 gdb::array_view
<const gdb_byte
> view (var
->value_bytes (),
365 /* Value is a constant byte-sequence. */
366 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
368 v
= value::allocate (type
);
369 memcpy (v
->contents_raw ().data (), var
->value_bytes (),
371 v
->set_lval (not_lval
);
375 if (overlay_debugging
)
377 = symbol_overlayed_address (var
->value_address (),
378 var
->obj_section (var
->objfile ()));
380 addr
= var
->value_address ();
384 addr
= get_frame_args_address (frame
);
386 error (_("Unknown argument list address for `%s'."),
388 addr
+= var
->value_longest ();
396 argref
= get_frame_args_address (frame
);
398 error (_("Unknown argument list address for `%s'."),
400 argref
+= var
->value_longest ();
401 ref
= value_at (lookup_pointer_type (type
), argref
);
402 addr
= value_as_address (ref
);
407 addr
= get_frame_locals_address (frame
);
408 addr
+= var
->value_longest ();
412 error (_("Cannot look up value of a typedef `%s'."),
417 if (overlay_debugging
)
418 addr
= symbol_overlayed_address
419 (var
->value_block ()->entry_pc (),
420 var
->obj_section (var
->objfile ()));
422 addr
= var
->value_block ()->entry_pc ();
426 case LOC_REGPARM_ADDR
:
428 const symbol_register_ops
*reg_ops
= var
->register_ops ();
429 int regno
= reg_ops
->register_number (var
, get_frame_arch (frame
));
431 if (var
->aclass () == LOC_REGPARM_ADDR
)
432 addr
= value_as_address
433 (value_from_register (lookup_pointer_type (type
), regno
, frame
));
435 return value_from_register (type
, regno
, frame
);
440 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
444 struct obj_section
*obj_section
;
445 bound_minimal_symbol bmsym
;
447 gdbarch_iterate_over_objfiles_in_search_order
449 [var
, &bmsym
] (objfile
*objfile
)
451 bmsym
= lookup_minimal_symbol (var
->linkage_name (), nullptr,
454 /* Stop if a match is found. */
455 return bmsym
.minsym
!= nullptr;
459 /* If we can't find the minsym there's a problem in the symbol info.
460 The symbol exists in the debug info, but it's missing in the minsym
462 if (bmsym
.minsym
== nullptr)
464 const char *flavour_name
465 = objfile_flavour_name (var
->objfile ());
467 /* We can't get here unless we've opened the file, so flavour_name
469 gdb_assert (flavour_name
!= NULL
);
470 error (_("Missing %s symbol \"%s\"."),
471 flavour_name
, var
->linkage_name ());
474 obj_section
= bmsym
.minsym
->obj_section (bmsym
.objfile
);
475 /* Relocate address, unless there is no section or the variable is
477 if (obj_section
== NULL
478 || (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
479 addr
= CORE_ADDR (bmsym
.minsym
->unrelocated_address ());
481 addr
= bmsym
.value_address ();
482 if (overlay_debugging
)
483 addr
= symbol_overlayed_address (addr
, obj_section
);
484 /* Determine address of TLS variable. */
486 && (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
487 addr
= target_translate_tls_address (obj_section
->objfile
, addr
);
491 case LOC_OPTIMIZED_OUT
:
492 if (is_dynamic_type (type
))
493 type
= resolve_dynamic_type (type
, {}, /* Unused address. */ 0);
494 return value::allocate_optimized_out (type
);
497 error (_("Cannot look up value of a botched symbol `%s'."),
502 v
= value_at_lazy (type
, addr
);
506 /* Calls VAR's language read_var_value hook with the given arguments. */
509 read_var_value (struct symbol
*var
, const struct block
*var_block
,
510 const frame_info_ptr
&frame
)
512 const struct language_defn
*lang
= language_def (var
->language ());
514 gdb_assert (lang
!= NULL
);
516 return lang
->read_var_value (var
, var_block
, frame
);
519 /* Install default attributes for register values. */
522 default_value_from_register (gdbarch
*gdbarch
, type
*type
, int regnum
,
523 const frame_info_ptr
&this_frame
)
526 = value::allocate_register (get_next_frame_sentinel_okay (this_frame
),
529 /* Any structure stored in more than one register will always be
530 an integral number of registers. Otherwise, you need to do
531 some fiddling with the last register copied here for little
533 if (type_byte_order (type
) == BFD_ENDIAN_BIG
534 && type
->length () < register_size (gdbarch
, regnum
))
535 /* Big-endian, and we want less than full size. */
536 value
->set_offset (register_size (gdbarch
, regnum
) - type
->length ());
538 value
->set_offset (0);
543 /* VALUE must be an lval_register value. If regnum is the value's
544 associated register number, and len the length of the value's type,
545 read one or more registers in VALUE's frame, starting with register REGNUM,
546 until we've read LEN bytes.
548 If any of the registers we try to read are optimized out, then mark the
549 complete resulting value as optimized out. */
552 read_frame_register_value (value
*value
)
554 gdb_assert (value
->lval () == lval_register
);
556 frame_info_ptr next_frame
= frame_find_by_id (value
->next_frame_id ());
557 gdb_assert (next_frame
!= nullptr);
559 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
561 LONGEST reg_offset
= value
->offset ();
562 int regnum
= value
->regnum ();
563 int len
= type_length_units (check_typedef (value
->type ()));
565 /* Skip registers wholly inside of REG_OFFSET. */
566 while (reg_offset
>= register_size (gdbarch
, regnum
))
568 reg_offset
-= register_size (gdbarch
, regnum
);
575 struct value
*regval
= frame_unwind_register_value (next_frame
, regnum
);
576 int reg_len
= type_length_units (regval
->type ()) - reg_offset
;
578 /* If the register length is larger than the number of bytes
579 remaining to copy, then only copy the appropriate bytes. */
583 regval
->contents_copy (value
, offset
, reg_offset
, reg_len
);
592 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
595 value_from_register (struct type
*type
, int regnum
, const frame_info_ptr
&frame
)
597 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
598 struct type
*type1
= check_typedef (type
);
601 if (gdbarch_convert_register_p (gdbarch
, regnum
, type1
))
603 int optim
, unavail
, ok
;
605 /* The ISA/ABI need to something weird when obtaining the
606 specified value from this register. It might need to
607 re-order non-adjacent, starting with REGNUM (see MIPS and
608 i386). It might need to convert the [float] register into
609 the corresponding [integer] type (see Alpha). The assumption
610 is that gdbarch_register_to_value populates the entire value
611 including the location. */
612 v
= value::allocate_register (get_next_frame_sentinel_okay (frame
),
614 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type1
,
615 v
->contents_raw ().data (), &optim
,
621 v
->mark_bytes_optimized_out (0, type
->length ());
623 v
->mark_bytes_unavailable (0, type
->length ());
628 /* Construct the value. */
629 v
= gdbarch_value_from_register (gdbarch
, type
, regnum
, frame
);
632 read_frame_register_value (v
);
638 /* Return contents of register REGNUM in frame FRAME as address.
639 Will abort if register value is not available. */
642 address_from_register (int regnum
, const frame_info_ptr
&frame
)
644 type
*type
= builtin_type (get_frame_arch (frame
))->builtin_data_ptr
;
645 value_ref_ptr v
= release_value (value_from_register (type
, regnum
, frame
));
647 if (v
->optimized_out ())
649 /* This function is used while computing a location expression.
650 Complain about the value being optimized out, rather than
651 letting value_as_address complain about some random register
652 the expression depends on not being saved. */
653 error_value_optimized_out ();
656 return value_as_address (v
.get ());