]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/findvar.c
gdb: move store/extract integer functions to extract-store-integer.{c,h}
[thirdparty/binutils-gdb.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "extract-store-integer.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "target.h"
28 #include "symfile.h"
29 #include "regcache.h"
30 #include "user-regs.h"
31 #include "block.h"
32 #include "objfiles.h"
33 #include "language.h"
34
35 /* Basic byte-swapping routines. All 'extract' functions return a
36 host-format integer from a target-format integer at ADDR which is
37 LEN bytes long. */
38
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... */
44 you lose
45 #endif
46
47 /* See value.h. */
48
49 value *
50 value_of_register (int regnum, const frame_info_ptr &next_frame)
51 {
52 gdbarch *gdbarch = frame_unwind_arch (next_frame);
53
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));
58
59 value *reg_val = value_of_register_lazy (next_frame, regnum);
60 reg_val->fetch_lazy ();
61 return reg_val;
62 }
63
64 /* See value.h. */
65
66 value *
67 value_of_register_lazy (const frame_info_ptr &next_frame, int regnum)
68 {
69 gdbarch *gdbarch = frame_unwind_arch (next_frame);
70
71 gdb_assert (regnum < gdbarch_num_cooked_regs (gdbarch));
72 gdb_assert (next_frame != nullptr);
73
74 return value::allocate_register_lazy (next_frame, regnum);
75 }
76
77 /* Given a pointer of type TYPE in target form in BUF, return the
78 address it represents. */
79 CORE_ADDR
80 unsigned_pointer_to_address (struct gdbarch *gdbarch,
81 struct type *type, const gdb_byte *buf)
82 {
83 enum bfd_endian byte_order = type_byte_order (type);
84
85 return extract_unsigned_integer (buf, type->length (), byte_order);
86 }
87
88 CORE_ADDR
89 signed_pointer_to_address (struct gdbarch *gdbarch,
90 struct type *type, const gdb_byte *buf)
91 {
92 enum bfd_endian byte_order = type_byte_order (type);
93
94 return extract_signed_integer (buf, type->length (), byte_order);
95 }
96
97 /* Given an address, store it as a pointer of type TYPE in target
98 format in BUF. */
99 void
100 unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
101 gdb_byte *buf, CORE_ADDR addr)
102 {
103 enum bfd_endian byte_order = type_byte_order (type);
104
105 store_unsigned_integer (buf, type->length (), byte_order, addr);
106 }
107
108 void
109 address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
110 gdb_byte *buf, CORE_ADDR addr)
111 {
112 enum bfd_endian byte_order = type_byte_order (type);
113
114 store_signed_integer (buf, type->length (), byte_order, addr);
115 }
116 \f
117 /* See value.h. */
118
119 enum symbol_needs_kind
120 symbol_read_needs (struct symbol *sym)
121 {
122 if (const symbol_computed_ops *computed_ops = sym->computed_ops ();
123 computed_ops != nullptr)
124 return computed_ops->get_symbol_read_needs (sym);
125
126 switch (sym->aclass ())
127 {
128 /* All cases listed explicitly so that gcc -Wall will detect it if
129 we failed to consider one. */
130 case LOC_COMPUTED:
131 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
132
133 case LOC_REGISTER:
134 case LOC_ARG:
135 case LOC_REF_ARG:
136 case LOC_REGPARM_ADDR:
137 case LOC_LOCAL:
138 return SYMBOL_NEEDS_FRAME;
139
140 case LOC_UNDEF:
141 case LOC_CONST:
142 case LOC_STATIC:
143 case LOC_TYPEDEF:
144
145 case LOC_LABEL:
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
148 the right frame. */
149
150 case LOC_BLOCK:
151 case LOC_CONST_BYTES:
152 case LOC_UNRESOLVED:
153 case LOC_OPTIMIZED_OUT:
154 return SYMBOL_NEEDS_NONE;
155 }
156 return SYMBOL_NEEDS_FRAME;
157 }
158
159 /* See value.h. */
160
161 int
162 symbol_read_needs_frame (struct symbol *sym)
163 {
164 return symbol_read_needs (sym) == SYMBOL_NEEDS_FRAME;
165 }
166
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.
170
171 This kind of computation is necessary to correctly handle lexically nested
172 functions.
173
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. */
178
179 static frame_info_ptr
180 get_hosting_frame (struct symbol *var, const struct block *var_block,
181 const frame_info_ptr &initial_frame)
182 {
183 const struct block *frame_block = NULL;
184
185 if (!symbol_read_needs_frame (var))
186 return NULL;
187
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;
194
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;
203
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);
209
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)
213 {
214 /* Stacks can be quite deep: give the user a chance to stop this. */
215 QUIT;
216
217 if (frame_block == NULL)
218 {
219 frame = get_prev_frame (frame);
220 if (frame == NULL)
221 break;
222 frame_block = get_frame_block (frame, NULL);
223 }
224
225 /* If we failed to find the proper frame, fallback to the heuristic
226 method below. */
227 else if (frame_block->is_global_block ())
228 {
229 frame = NULL;
230 break;
231 }
232
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)
237 {
238 frame = frame_follow_static_link (frame);
239 if (frame != nullptr)
240 {
241 frame_block = get_frame_block (frame, nullptr);
242 if (frame_block == nullptr)
243 frame = nullptr;
244 }
245 }
246
247 else
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 ();
251 }
252
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. */
257 if (frame == NULL)
258 {
259 frame = block_innermost_frame (var_block);
260 if (frame == NULL)
261 {
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 ());
267 else
268 error (_("No frame is currently executing in specified"
269 " block"));
270 }
271 }
272
273 return frame;
274 }
275
276 /* See language.h. */
277
278 struct value *
279 language_defn::read_var_value (struct symbol *var,
280 const struct block *var_block,
281 const frame_info_ptr &frame_param) const
282 {
283 struct value *v;
284 struct type *type = var->type ();
285 CORE_ADDR addr;
286 enum symbol_needs_kind sym_need;
287 frame_info_ptr frame = frame_param;
288
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);
295
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 ());
301
302 if (frame != NULL)
303 frame = get_hosting_frame (var, var_block, frame);
304
305 if (const symbol_computed_ops *computed_ops = var->computed_ops ())
306 return computed_ops->read_variable (var, frame);
307
308 switch (var->aclass ())
309 {
310 case LOC_CONST:
311 if (is_dynamic_type (type))
312 {
313 gdb_byte bytes[sizeof (LONGEST)];
314
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);
320
321 /* Value is a constant byte-sequence. */
322 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
323 }
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);
329 return v;
330
331 case LOC_LABEL:
332 {
333 /* Put the constant back in target format. */
334 if (overlay_debugging)
335 {
336 struct objfile *var_objfile = var->objfile ();
337 addr = symbol_overlayed_address (var->value_address (),
338 var->obj_section (var_objfile));
339 }
340 else
341 addr = var->value_address ();
342
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);
348
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
351 representation. */
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);
356 return v;
357 }
358
359 case LOC_CONST_BYTES:
360 if (is_dynamic_type (type))
361 {
362 gdb::array_view<const gdb_byte> view (var->value_bytes (),
363 type->length ());
364
365 /* Value is a constant byte-sequence. */
366 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
367 }
368 v = value::allocate (type);
369 memcpy (v->contents_raw ().data (), var->value_bytes (),
370 type->length ());
371 v->set_lval (not_lval);
372 return v;
373
374 case LOC_STATIC:
375 if (overlay_debugging)
376 addr
377 = symbol_overlayed_address (var->value_address (),
378 var->obj_section (var->objfile ()));
379 else
380 addr = var->value_address ();
381 break;
382
383 case LOC_ARG:
384 addr = get_frame_args_address (frame);
385 if (!addr)
386 error (_("Unknown argument list address for `%s'."),
387 var->print_name ());
388 addr += var->value_longest ();
389 break;
390
391 case LOC_REF_ARG:
392 {
393 struct value *ref;
394 CORE_ADDR argref;
395
396 argref = get_frame_args_address (frame);
397 if (!argref)
398 error (_("Unknown argument list address for `%s'."),
399 var->print_name ());
400 argref += var->value_longest ();
401 ref = value_at (lookup_pointer_type (type), argref);
402 addr = value_as_address (ref);
403 break;
404 }
405
406 case LOC_LOCAL:
407 addr = get_frame_locals_address (frame);
408 addr += var->value_longest ();
409 break;
410
411 case LOC_TYPEDEF:
412 error (_("Cannot look up value of a typedef `%s'."),
413 var->print_name ());
414 break;
415
416 case LOC_BLOCK:
417 if (overlay_debugging)
418 addr = symbol_overlayed_address
419 (var->value_block ()->entry_pc (),
420 var->obj_section (var->objfile ()));
421 else
422 addr = var->value_block ()->entry_pc ();
423 break;
424
425 case LOC_REGISTER:
426 case LOC_REGPARM_ADDR:
427 {
428 const symbol_register_ops *reg_ops = var->register_ops ();
429 int regno = reg_ops->register_number (var, get_frame_arch (frame));
430
431 if (var->aclass () == LOC_REGPARM_ADDR)
432 addr = value_as_address
433 (value_from_register (lookup_pointer_type (type), regno, frame));
434 else
435 return value_from_register (type, regno, frame);
436 }
437 break;
438
439 case LOC_COMPUTED:
440 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
441
442 case LOC_UNRESOLVED:
443 {
444 struct obj_section *obj_section;
445 bound_minimal_symbol bmsym;
446
447 gdbarch_iterate_over_objfiles_in_search_order
448 (var->arch (),
449 [var, &bmsym] (objfile *objfile)
450 {
451 bmsym = lookup_minimal_symbol (var->linkage_name (), nullptr,
452 objfile);
453
454 /* Stop if a match is found. */
455 return bmsym.minsym != nullptr;
456 },
457 var->objfile ());
458
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
461 table. */
462 if (bmsym.minsym == nullptr)
463 {
464 const char *flavour_name
465 = objfile_flavour_name (var->objfile ());
466
467 /* We can't get here unless we've opened the file, so flavour_name
468 can't be NULL. */
469 gdb_assert (flavour_name != NULL);
470 error (_("Missing %s symbol \"%s\"."),
471 flavour_name, var->linkage_name ());
472 }
473
474 obj_section = bmsym.minsym->obj_section (bmsym.objfile);
475 /* Relocate address, unless there is no section or the variable is
476 a TLS variable. */
477 if (obj_section == NULL
478 || (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
479 addr = CORE_ADDR (bmsym.minsym->unrelocated_address ());
480 else
481 addr = bmsym.value_address ();
482 if (overlay_debugging)
483 addr = symbol_overlayed_address (addr, obj_section);
484 /* Determine address of TLS variable. */
485 if (obj_section
486 && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
487 addr = target_translate_tls_address (obj_section->objfile, addr);
488 }
489 break;
490
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);
495
496 default:
497 error (_("Cannot look up value of a botched symbol `%s'."),
498 var->print_name ());
499 break;
500 }
501
502 v = value_at_lazy (type, addr);
503 return v;
504 }
505
506 /* Calls VAR's language read_var_value hook with the given arguments. */
507
508 struct value *
509 read_var_value (struct symbol *var, const struct block *var_block,
510 const frame_info_ptr &frame)
511 {
512 const struct language_defn *lang = language_def (var->language ());
513
514 gdb_assert (lang != NULL);
515
516 return lang->read_var_value (var, var_block, frame);
517 }
518
519 /* Install default attributes for register values. */
520
521 value *
522 default_value_from_register (gdbarch *gdbarch, type *type, int regnum,
523 const frame_info_ptr &this_frame)
524 {
525 value *value
526 = value::allocate_register (get_next_frame_sentinel_okay (this_frame),
527 regnum, type);
528
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
532 endian machines. */
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 ());
537 else
538 value->set_offset (0);
539
540 return value;
541 }
542
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.
547
548 If any of the registers we try to read are optimized out, then mark the
549 complete resulting value as optimized out. */
550
551 static void
552 read_frame_register_value (value *value)
553 {
554 gdb_assert (value->lval () == lval_register);
555
556 frame_info_ptr next_frame = frame_find_by_id (value->next_frame_id ());
557 gdb_assert (next_frame != nullptr);
558
559 gdbarch *gdbarch = frame_unwind_arch (next_frame);
560 LONGEST offset = 0;
561 LONGEST reg_offset = value->offset ();
562 int regnum = value->regnum ();
563 int len = type_length_units (check_typedef (value->type ()));
564
565 /* Skip registers wholly inside of REG_OFFSET. */
566 while (reg_offset >= register_size (gdbarch, regnum))
567 {
568 reg_offset -= register_size (gdbarch, regnum);
569 regnum++;
570 }
571
572 /* Copy the data. */
573 while (len > 0)
574 {
575 struct value *regval = frame_unwind_register_value (next_frame, regnum);
576 int reg_len = type_length_units (regval->type ()) - reg_offset;
577
578 /* If the register length is larger than the number of bytes
579 remaining to copy, then only copy the appropriate bytes. */
580 if (reg_len > len)
581 reg_len = len;
582
583 regval->contents_copy (value, offset, reg_offset, reg_len);
584
585 offset += reg_len;
586 len -= reg_len;
587 reg_offset = 0;
588 regnum++;
589 }
590 }
591
592 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
593
594 struct value *
595 value_from_register (struct type *type, int regnum, const frame_info_ptr &frame)
596 {
597 struct gdbarch *gdbarch = get_frame_arch (frame);
598 struct type *type1 = check_typedef (type);
599 struct value *v;
600
601 if (gdbarch_convert_register_p (gdbarch, regnum, type1))
602 {
603 int optim, unavail, ok;
604
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),
613 regnum, type);
614 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
615 v->contents_raw ().data (), &optim,
616 &unavail);
617
618 if (!ok)
619 {
620 if (optim)
621 v->mark_bytes_optimized_out (0, type->length ());
622 if (unavail)
623 v->mark_bytes_unavailable (0, type->length ());
624 }
625 }
626 else
627 {
628 /* Construct the value. */
629 v = gdbarch_value_from_register (gdbarch, type, regnum, frame);
630
631 /* Get the data. */
632 read_frame_register_value (v);
633 }
634
635 return v;
636 }
637
638 /* Return contents of register REGNUM in frame FRAME as address.
639 Will abort if register value is not available. */
640
641 CORE_ADDR
642 address_from_register (int regnum, const frame_info_ptr &frame)
643 {
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));
646
647 if (v->optimized_out ())
648 {
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 ();
654 }
655
656 return value_as_address (v.get ());
657 }