Automatic date update in version.in
[binutils-gdb.git] / gdb / findvar.c
blob71bfebe40a7ad51d651a7e2a6f160cec113902ff
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 "symtab.h"
21 #include "gdbtypes.h"
22 #include "frame.h"
23 #include "value.h"
24 #include "gdbcore.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "symfile.h"
28 #include "regcache.h"
29 #include "user-regs.h"
30 #include "block.h"
31 #include "objfiles.h"
32 #include "language.h"
33 #include "gdbsupport/selftest.h"
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. */
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
47 template<typename T, typename>
49 extract_integer (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order)
51 typename std::make_unsigned<T>::type retval = 0;
53 if (buf.size () > (int) sizeof (T))
54 error (_("\
55 That operation is not available on integers of more than %d bytes."),
56 (int) sizeof (T));
58 /* Start at the most significant end of the integer, and work towards
59 the least significant. */
60 if (byte_order == BFD_ENDIAN_BIG)
62 size_t i = 0;
64 if (std::is_signed<T>::value)
66 /* Do the sign extension once at the start. */
67 retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
68 ++i;
70 for (; i < buf.size (); ++i)
71 retval = (retval << 8) | buf[i];
73 else
75 ssize_t i = buf.size () - 1;
77 if (std::is_signed<T>::value)
79 /* Do the sign extension once at the start. */
80 retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
81 --i;
83 for (; i >= 0; --i)
84 retval = (retval << 8) | buf[i];
86 return retval;
89 /* Explicit instantiations. */
90 template LONGEST extract_integer<LONGEST> (gdb::array_view<const gdb_byte> buf,
91 enum bfd_endian byte_order);
92 template ULONGEST extract_integer<ULONGEST>
93 (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order);
95 /* Sometimes a long long unsigned integer can be extracted as a
96 LONGEST value. This is done so that we can print these values
97 better. If this integer can be converted to a LONGEST, this
98 function returns 1 and sets *PVAL. Otherwise it returns 0. */
101 extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
102 enum bfd_endian byte_order, LONGEST *pval)
104 const gdb_byte *p;
105 const gdb_byte *first_addr;
106 int len;
108 len = orig_len;
109 if (byte_order == BFD_ENDIAN_BIG)
111 for (p = addr;
112 len > (int) sizeof (LONGEST) && p < addr + orig_len;
113 p++)
115 if (*p == 0)
116 len--;
117 else
118 break;
120 first_addr = p;
122 else
124 first_addr = addr;
125 for (p = addr + orig_len - 1;
126 len > (int) sizeof (LONGEST) && p >= addr;
127 p--)
129 if (*p == 0)
130 len--;
131 else
132 break;
136 if (len <= (int) sizeof (LONGEST))
138 *pval = (LONGEST) extract_unsigned_integer (first_addr,
139 sizeof (LONGEST),
140 byte_order);
141 return 1;
144 return 0;
148 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
149 address it represents. */
150 CORE_ADDR
151 extract_typed_address (const gdb_byte *buf, struct type *type)
153 gdb_assert (type->is_pointer_or_reference ());
154 return gdbarch_pointer_to_address (type->arch (), type, buf);
157 /* All 'store' functions accept a host-format integer and store a
158 target-format integer at ADDR which is LEN bytes long. */
159 template<typename T, typename>
160 void
161 store_integer (gdb::array_view<gdb_byte> dst, enum bfd_endian byte_order,
162 T val)
164 gdb_byte *p;
165 gdb_byte *startaddr = dst.data ();
166 gdb_byte *endaddr = startaddr + dst.size ();
168 /* Start at the least significant end of the integer, and work towards
169 the most significant. */
170 if (byte_order == BFD_ENDIAN_BIG)
172 for (p = endaddr - 1; p >= startaddr; --p)
174 *p = val & 0xff;
175 val >>= 8;
178 else
180 for (p = startaddr; p < endaddr; ++p)
182 *p = val & 0xff;
183 val >>= 8;
188 /* Explicit instantiations. */
189 template void store_integer (gdb::array_view<gdb_byte> dst,
190 bfd_endian byte_order, LONGEST val);
192 template void store_integer (gdb::array_view<gdb_byte> dst,
193 bfd_endian byte_order, ULONGEST val);
195 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
196 form. */
197 void
198 store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
200 gdb_assert (type->is_pointer_or_reference ());
201 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
204 /* Copy a value from SOURCE of size SOURCE_SIZE bytes to DEST of size DEST_SIZE
205 bytes. If SOURCE_SIZE is greater than DEST_SIZE, then truncate the most
206 significant bytes. If SOURCE_SIZE is less than DEST_SIZE then either sign
207 or zero extended according to IS_SIGNED. Values are stored in memory with
208 endianness BYTE_ORDER. */
210 void
211 copy_integer_to_size (gdb_byte *dest, int dest_size, const gdb_byte *source,
212 int source_size, bool is_signed,
213 enum bfd_endian byte_order)
215 signed int size_diff = dest_size - source_size;
217 /* Copy across everything from SOURCE that can fit into DEST. */
219 if (byte_order == BFD_ENDIAN_BIG && size_diff > 0)
220 memcpy (dest + size_diff, source, source_size);
221 else if (byte_order == BFD_ENDIAN_BIG && size_diff < 0)
222 memcpy (dest, source - size_diff, dest_size);
223 else
224 memcpy (dest, source, std::min (source_size, dest_size));
226 /* Fill the remaining space in DEST by either zero extending or sign
227 extending. */
229 if (size_diff > 0)
231 gdb_byte extension = 0;
232 if (is_signed
233 && ((byte_order != BFD_ENDIAN_BIG && source[source_size - 1] & 0x80)
234 || (byte_order == BFD_ENDIAN_BIG && source[0] & 0x80)))
235 extension = 0xff;
237 /* Extend into MSBs of SOURCE. */
238 if (byte_order == BFD_ENDIAN_BIG)
239 memset (dest, extension, size_diff);
240 else
241 memset (dest + source_size, extension, size_diff);
245 /* See value.h. */
247 value *
248 value_of_register (int regnum, const frame_info_ptr &next_frame)
250 gdbarch *gdbarch = frame_unwind_arch (next_frame);
252 /* User registers lie completely outside of the range of normal
253 registers. Catch them early so that the target never sees them. */
254 if (regnum >= gdbarch_num_cooked_regs (gdbarch))
255 return value_of_user_reg (regnum, get_prev_frame_always (next_frame));
257 value *reg_val = value_of_register_lazy (next_frame, regnum);
258 reg_val->fetch_lazy ();
259 return reg_val;
262 /* See value.h. */
264 value *
265 value_of_register_lazy (const frame_info_ptr &next_frame, int regnum)
267 gdbarch *gdbarch = frame_unwind_arch (next_frame);
269 gdb_assert (regnum < gdbarch_num_cooked_regs (gdbarch));
270 gdb_assert (next_frame != nullptr);
272 return value::allocate_register_lazy (next_frame, regnum);
275 /* Given a pointer of type TYPE in target form in BUF, return the
276 address it represents. */
277 CORE_ADDR
278 unsigned_pointer_to_address (struct gdbarch *gdbarch,
279 struct type *type, const gdb_byte *buf)
281 enum bfd_endian byte_order = type_byte_order (type);
283 return extract_unsigned_integer (buf, type->length (), byte_order);
286 CORE_ADDR
287 signed_pointer_to_address (struct gdbarch *gdbarch,
288 struct type *type, const gdb_byte *buf)
290 enum bfd_endian byte_order = type_byte_order (type);
292 return extract_signed_integer (buf, type->length (), byte_order);
295 /* Given an address, store it as a pointer of type TYPE in target
296 format in BUF. */
297 void
298 unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
299 gdb_byte *buf, CORE_ADDR addr)
301 enum bfd_endian byte_order = type_byte_order (type);
303 store_unsigned_integer (buf, type->length (), byte_order, addr);
306 void
307 address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
308 gdb_byte *buf, CORE_ADDR addr)
310 enum bfd_endian byte_order = type_byte_order (type);
312 store_signed_integer (buf, type->length (), byte_order, addr);
315 /* See value.h. */
317 enum symbol_needs_kind
318 symbol_read_needs (struct symbol *sym)
320 if (const symbol_computed_ops *computed_ops = sym->computed_ops ();
321 computed_ops != nullptr)
322 return computed_ops->get_symbol_read_needs (sym);
324 switch (sym->aclass ())
326 /* All cases listed explicitly so that gcc -Wall will detect it if
327 we failed to consider one. */
328 case LOC_COMPUTED:
329 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
331 case LOC_REGISTER:
332 case LOC_ARG:
333 case LOC_REF_ARG:
334 case LOC_REGPARM_ADDR:
335 case LOC_LOCAL:
336 return SYMBOL_NEEDS_FRAME;
338 case LOC_UNDEF:
339 case LOC_CONST:
340 case LOC_STATIC:
341 case LOC_TYPEDEF:
343 case LOC_LABEL:
344 /* Getting the address of a label can be done independently of the block,
345 even if some *uses* of that address wouldn't work so well without
346 the right frame. */
348 case LOC_BLOCK:
349 case LOC_CONST_BYTES:
350 case LOC_UNRESOLVED:
351 case LOC_OPTIMIZED_OUT:
352 return SYMBOL_NEEDS_NONE;
354 return SYMBOL_NEEDS_FRAME;
357 /* See value.h. */
360 symbol_read_needs_frame (struct symbol *sym)
362 return symbol_read_needs (sym) == SYMBOL_NEEDS_FRAME;
365 /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
366 rules, look for the frame that is actually hosting VAR and return it. If,
367 for some reason, we found no such frame, return NULL.
369 This kind of computation is necessary to correctly handle lexically nested
370 functions.
372 Note that in some cases, we know what scope VAR comes from but we cannot
373 reach the specific frame that hosts the instance of VAR we are looking for.
374 For backward compatibility purposes (with old compilers), we then look for
375 the first frame that can host it. */
377 static frame_info_ptr
378 get_hosting_frame (struct symbol *var, const struct block *var_block,
379 const frame_info_ptr &initial_frame)
381 const struct block *frame_block = NULL;
383 if (!symbol_read_needs_frame (var))
384 return NULL;
386 /* Some symbols for local variables have no block: this happens when they are
387 not produced by a debug information reader, for instance when GDB creates
388 synthetic symbols. Without block information, we must assume they are
389 local to FRAME. In this case, there is nothing to do. */
390 else if (var_block == NULL)
391 return initial_frame;
393 /* We currently assume that all symbols with a location list need a frame.
394 This is true in practice because selecting the location description
395 requires to compute the CFA, hence requires a frame. However we have
396 tests that embed global/static symbols with null location lists.
397 We want to get <optimized out> instead of <frame required> when evaluating
398 them so return a frame instead of raising an error. */
399 else if (var_block->is_global_block () || var_block->is_static_block ())
400 return initial_frame;
402 /* We have to handle the "my_func::my_local_var" notation. This requires us
403 to look for upper frames when we find no block for the current frame: here
404 and below, handle when frame_block == NULL. */
405 if (initial_frame != nullptr)
406 frame_block = get_frame_block (initial_frame, NULL);
408 /* Climb up the call stack until reaching the frame we are looking for. */
409 frame_info_ptr frame = initial_frame;
410 while (frame != NULL && frame_block != var_block)
412 /* Stacks can be quite deep: give the user a chance to stop this. */
413 QUIT;
415 if (frame_block == NULL)
417 frame = get_prev_frame (frame);
418 if (frame == NULL)
419 break;
420 frame_block = get_frame_block (frame, NULL);
423 /* If we failed to find the proper frame, fallback to the heuristic
424 method below. */
425 else if (frame_block->is_global_block ())
427 frame = NULL;
428 break;
431 /* Assuming we have a block for this frame: if we are at the function
432 level, the immediate upper lexical block is in an outer function:
433 follow the static link. */
434 else if (frame_block->function () != nullptr)
436 frame = frame_follow_static_link (frame);
437 if (frame != nullptr)
439 frame_block = get_frame_block (frame, nullptr);
440 if (frame_block == nullptr)
441 frame = nullptr;
445 else
446 /* We must be in some function nested lexical block. Just get the
447 outer block: both must share the same frame. */
448 frame_block = frame_block->superblock ();
451 /* Old compilers may not provide a static link, or they may provide an
452 invalid one. For such cases, fallback on the old way to evaluate
453 non-local references: just climb up the call stack and pick the first
454 frame that contains the variable we are looking for. */
455 if (frame == NULL)
457 frame = block_innermost_frame (var_block);
458 if (frame == NULL)
460 if (var_block->function ()
461 && !var_block->inlined_p ()
462 && var_block->function ()->print_name ())
463 error (_("No frame is currently executing in block %s."),
464 var_block->function ()->print_name ());
465 else
466 error (_("No frame is currently executing in specified"
467 " block"));
471 return frame;
474 /* See language.h. */
476 struct value *
477 language_defn::read_var_value (struct symbol *var,
478 const struct block *var_block,
479 const frame_info_ptr &frame_param) const
481 struct value *v;
482 struct type *type = var->type ();
483 CORE_ADDR addr;
484 enum symbol_needs_kind sym_need;
485 frame_info_ptr frame = frame_param;
487 /* Call check_typedef on our type to make sure that, if TYPE is
488 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
489 instead of zero. However, we do not replace the typedef type by the
490 target type, because we want to keep the typedef in order to be able to
491 set the returned value type description correctly. */
492 check_typedef (type);
494 sym_need = symbol_read_needs (var);
495 if (sym_need == SYMBOL_NEEDS_FRAME)
496 gdb_assert (frame != NULL);
497 else if (sym_need == SYMBOL_NEEDS_REGISTERS && !target_has_registers ())
498 error (_("Cannot read `%s' without registers"), var->print_name ());
500 if (frame != NULL)
501 frame = get_hosting_frame (var, var_block, frame);
503 if (const symbol_computed_ops *computed_ops = var->computed_ops ())
504 return computed_ops->read_variable (var, frame);
506 switch (var->aclass ())
508 case LOC_CONST:
509 if (is_dynamic_type (type))
511 gdb_byte bytes[sizeof (LONGEST)];
513 size_t len = std::min (sizeof (LONGEST), (size_t) type->length ());
514 store_unsigned_integer (bytes, len,
515 type_byte_order (type),
516 var->value_longest ());
517 gdb::array_view<const gdb_byte> view (bytes, len);
519 /* Value is a constant byte-sequence. */
520 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
522 /* Put the constant back in target format. */
523 v = value::allocate (type);
524 store_signed_integer (v->contents_raw ().data (), type->length (),
525 type_byte_order (type), var->value_longest ());
526 v->set_lval (not_lval);
527 return v;
529 case LOC_LABEL:
531 /* Put the constant back in target format. */
532 if (overlay_debugging)
534 struct objfile *var_objfile = var->objfile ();
535 addr = symbol_overlayed_address (var->value_address (),
536 var->obj_section (var_objfile));
538 else
539 addr = var->value_address ();
541 /* First convert the CORE_ADDR to a function pointer type, this
542 ensures the gdbarch knows what type of pointer we are
543 manipulating when value_from_pointer is called. */
544 type = builtin_type (var->arch ())->builtin_func_ptr;
545 v = value_from_pointer (type, addr);
547 /* But we want to present the value as 'void *', so cast it to the
548 required type now, this will not change the values bit
549 representation. */
550 struct type *void_ptr_type
551 = builtin_type (var->arch ())->builtin_data_ptr;
552 v = value_cast_pointers (void_ptr_type, v, 0);
553 v->set_lval (not_lval);
554 return v;
557 case LOC_CONST_BYTES:
558 if (is_dynamic_type (type))
560 gdb::array_view<const gdb_byte> view (var->value_bytes (),
561 type->length ());
563 /* Value is a constant byte-sequence. */
564 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
566 v = value::allocate (type);
567 memcpy (v->contents_raw ().data (), var->value_bytes (),
568 type->length ());
569 v->set_lval (not_lval);
570 return v;
572 case LOC_STATIC:
573 if (overlay_debugging)
574 addr
575 = symbol_overlayed_address (var->value_address (),
576 var->obj_section (var->objfile ()));
577 else
578 addr = var->value_address ();
579 break;
581 case LOC_ARG:
582 addr = get_frame_args_address (frame);
583 if (!addr)
584 error (_("Unknown argument list address for `%s'."),
585 var->print_name ());
586 addr += var->value_longest ();
587 break;
589 case LOC_REF_ARG:
591 struct value *ref;
592 CORE_ADDR argref;
594 argref = get_frame_args_address (frame);
595 if (!argref)
596 error (_("Unknown argument list address for `%s'."),
597 var->print_name ());
598 argref += var->value_longest ();
599 ref = value_at (lookup_pointer_type (type), argref);
600 addr = value_as_address (ref);
601 break;
604 case LOC_LOCAL:
605 addr = get_frame_locals_address (frame);
606 addr += var->value_longest ();
607 break;
609 case LOC_TYPEDEF:
610 error (_("Cannot look up value of a typedef `%s'."),
611 var->print_name ());
612 break;
614 case LOC_BLOCK:
615 if (overlay_debugging)
616 addr = symbol_overlayed_address
617 (var->value_block ()->entry_pc (),
618 var->obj_section (var->objfile ()));
619 else
620 addr = var->value_block ()->entry_pc ();
621 break;
623 case LOC_REGISTER:
624 case LOC_REGPARM_ADDR:
626 const symbol_register_ops *reg_ops = var->register_ops ();
627 int regno = reg_ops->register_number (var, get_frame_arch (frame));
629 if (var->aclass () == LOC_REGPARM_ADDR)
630 addr = value_as_address
631 (value_from_register (lookup_pointer_type (type), regno, frame));
632 else
633 return value_from_register (type, regno, frame);
635 break;
637 case LOC_COMPUTED:
638 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
640 case LOC_UNRESOLVED:
642 struct obj_section *obj_section;
643 bound_minimal_symbol bmsym;
645 gdbarch_iterate_over_objfiles_in_search_order
646 (var->arch (),
647 [var, &bmsym] (objfile *objfile)
649 bmsym = lookup_minimal_symbol (var->linkage_name (), nullptr,
650 objfile);
652 /* Stop if a match is found. */
653 return bmsym.minsym != nullptr;
655 var->objfile ());
657 /* If we can't find the minsym there's a problem in the symbol info.
658 The symbol exists in the debug info, but it's missing in the minsym
659 table. */
660 if (bmsym.minsym == nullptr)
662 const char *flavour_name
663 = objfile_flavour_name (var->objfile ());
665 /* We can't get here unless we've opened the file, so flavour_name
666 can't be NULL. */
667 gdb_assert (flavour_name != NULL);
668 error (_("Missing %s symbol \"%s\"."),
669 flavour_name, var->linkage_name ());
672 obj_section = bmsym.minsym->obj_section (bmsym.objfile);
673 /* Relocate address, unless there is no section or the variable is
674 a TLS variable. */
675 if (obj_section == NULL
676 || (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
677 addr = CORE_ADDR (bmsym.minsym->unrelocated_address ());
678 else
679 addr = bmsym.value_address ();
680 if (overlay_debugging)
681 addr = symbol_overlayed_address (addr, obj_section);
682 /* Determine address of TLS variable. */
683 if (obj_section
684 && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
685 addr = target_translate_tls_address (obj_section->objfile, addr);
687 break;
689 case LOC_OPTIMIZED_OUT:
690 if (is_dynamic_type (type))
691 type = resolve_dynamic_type (type, {}, /* Unused address. */ 0);
692 return value::allocate_optimized_out (type);
694 default:
695 error (_("Cannot look up value of a botched symbol `%s'."),
696 var->print_name ());
697 break;
700 v = value_at_lazy (type, addr);
701 return v;
704 /* Calls VAR's language read_var_value hook with the given arguments. */
706 struct value *
707 read_var_value (struct symbol *var, const struct block *var_block,
708 const frame_info_ptr &frame)
710 const struct language_defn *lang = language_def (var->language ());
712 gdb_assert (lang != NULL);
714 return lang->read_var_value (var, var_block, frame);
717 /* Install default attributes for register values. */
719 value *
720 default_value_from_register (gdbarch *gdbarch, type *type, int regnum,
721 const frame_info_ptr &this_frame)
723 value *value
724 = value::allocate_register (get_next_frame_sentinel_okay (this_frame),
725 regnum, type);
727 /* Any structure stored in more than one register will always be
728 an integral number of registers. Otherwise, you need to do
729 some fiddling with the last register copied here for little
730 endian machines. */
731 if (type_byte_order (type) == BFD_ENDIAN_BIG
732 && type->length () < register_size (gdbarch, regnum))
733 /* Big-endian, and we want less than full size. */
734 value->set_offset (register_size (gdbarch, regnum) - type->length ());
735 else
736 value->set_offset (0);
738 return value;
741 /* VALUE must be an lval_register value. If regnum is the value's
742 associated register number, and len the length of the value's type,
743 read one or more registers in VALUE's frame, starting with register REGNUM,
744 until we've read LEN bytes.
746 If any of the registers we try to read are optimized out, then mark the
747 complete resulting value as optimized out. */
749 static void
750 read_frame_register_value (value *value)
752 gdb_assert (value->lval () == lval_register);
754 frame_info_ptr next_frame = frame_find_by_id (value->next_frame_id ());
755 gdb_assert (next_frame != nullptr);
757 gdbarch *gdbarch = frame_unwind_arch (next_frame);
758 LONGEST offset = 0;
759 LONGEST reg_offset = value->offset ();
760 int regnum = value->regnum ();
761 int len = type_length_units (check_typedef (value->type ()));
763 /* Skip registers wholly inside of REG_OFFSET. */
764 while (reg_offset >= register_size (gdbarch, regnum))
766 reg_offset -= register_size (gdbarch, regnum);
767 regnum++;
770 /* Copy the data. */
771 while (len > 0)
773 struct value *regval = frame_unwind_register_value (next_frame, regnum);
774 int reg_len = type_length_units (regval->type ()) - reg_offset;
776 /* If the register length is larger than the number of bytes
777 remaining to copy, then only copy the appropriate bytes. */
778 if (reg_len > len)
779 reg_len = len;
781 regval->contents_copy (value, offset, reg_offset, reg_len);
783 offset += reg_len;
784 len -= reg_len;
785 reg_offset = 0;
786 regnum++;
790 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
792 struct value *
793 value_from_register (struct type *type, int regnum, const frame_info_ptr &frame)
795 struct gdbarch *gdbarch = get_frame_arch (frame);
796 struct type *type1 = check_typedef (type);
797 struct value *v;
799 if (gdbarch_convert_register_p (gdbarch, regnum, type1))
801 int optim, unavail, ok;
803 /* The ISA/ABI need to something weird when obtaining the
804 specified value from this register. It might need to
805 re-order non-adjacent, starting with REGNUM (see MIPS and
806 i386). It might need to convert the [float] register into
807 the corresponding [integer] type (see Alpha). The assumption
808 is that gdbarch_register_to_value populates the entire value
809 including the location. */
810 v = value::allocate_register (get_next_frame_sentinel_okay (frame),
811 regnum, type);
812 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
813 v->contents_raw ().data (), &optim,
814 &unavail);
816 if (!ok)
818 if (optim)
819 v->mark_bytes_optimized_out (0, type->length ());
820 if (unavail)
821 v->mark_bytes_unavailable (0, type->length ());
824 else
826 /* Construct the value. */
827 v = gdbarch_value_from_register (gdbarch, type, regnum, frame);
829 /* Get the data. */
830 read_frame_register_value (v);
833 return v;
836 /* Return contents of register REGNUM in frame FRAME as address.
837 Will abort if register value is not available. */
839 CORE_ADDR
840 address_from_register (int regnum, const frame_info_ptr &frame)
842 type *type = builtin_type (get_frame_arch (frame))->builtin_data_ptr;
843 value_ref_ptr v = release_value (value_from_register (type, regnum, frame));
845 if (v->optimized_out ())
847 /* This function is used while computing a location expression.
848 Complain about the value being optimized out, rather than
849 letting value_as_address complain about some random register
850 the expression depends on not being saved. */
851 error_value_optimized_out ();
854 return value_as_address (v.get ());
857 #if GDB_SELF_TEST
858 namespace selftests {
859 namespace findvar_tests {
861 /* Function to test copy_integer_to_size. Store SOURCE_VAL with size
862 SOURCE_SIZE to a buffer, making sure no sign extending happens at this
863 stage. Copy buffer to a new buffer using copy_integer_to_size. Extract
864 copied value and compare to DEST_VALU. Copy again with a signed
865 copy_integer_to_size and compare to DEST_VALS. Do everything for both
866 LITTLE and BIG target endians. Use unsigned values throughout to make
867 sure there are no implicit sign extensions. */
869 static void
870 do_cint_test (ULONGEST dest_valu, ULONGEST dest_vals, int dest_size,
871 ULONGEST src_val, int src_size)
873 for (int i = 0; i < 2 ; i++)
875 gdb_byte srcbuf[sizeof (ULONGEST)] = {};
876 gdb_byte destbuf[sizeof (ULONGEST)] = {};
877 enum bfd_endian byte_order = i ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
879 /* Fill the src buffer (and later the dest buffer) with non-zero junk,
880 to ensure zero extensions aren't hidden. */
881 memset (srcbuf, 0xaa, sizeof (srcbuf));
883 /* Store (and later extract) using unsigned to ensure there are no sign
884 extensions. */
885 store_unsigned_integer (srcbuf, src_size, byte_order, src_val);
887 /* Test unsigned. */
888 memset (destbuf, 0xaa, sizeof (destbuf));
889 copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, false,
890 byte_order);
891 SELF_CHECK (dest_valu == extract_unsigned_integer (destbuf, dest_size,
892 byte_order));
894 /* Test signed. */
895 memset (destbuf, 0xaa, sizeof (destbuf));
896 copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, true,
897 byte_order);
898 SELF_CHECK (dest_vals == extract_unsigned_integer (destbuf, dest_size,
899 byte_order));
903 static void
904 copy_integer_to_size_test ()
906 /* Destination is bigger than the source, which has the signed bit unset. */
907 do_cint_test (0x12345678, 0x12345678, 8, 0x12345678, 4);
908 do_cint_test (0x345678, 0x345678, 8, 0x12345678, 3);
910 /* Destination is bigger than the source, which has the signed bit set. */
911 do_cint_test (0xdeadbeef, 0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
912 do_cint_test (0xadbeef, 0xffffffffffadbeef, 8, 0xdeadbeef, 3);
914 /* Destination is smaller than the source. */
915 do_cint_test (0x5678, 0x5678, 2, 0x12345678, 3);
916 do_cint_test (0xbeef, 0xbeef, 2, 0xdeadbeef, 3);
918 /* Destination and source are the same size. */
919 do_cint_test (0x8765432112345678, 0x8765432112345678, 8, 0x8765432112345678,
921 do_cint_test (0x432112345678, 0x432112345678, 6, 0x8765432112345678, 6);
922 do_cint_test (0xfeedbeaddeadbeef, 0xfeedbeaddeadbeef, 8, 0xfeedbeaddeadbeef,
924 do_cint_test (0xbeaddeadbeef, 0xbeaddeadbeef, 6, 0xfeedbeaddeadbeef, 6);
926 /* Destination is bigger than the source. Source is bigger than 32bits. */
927 do_cint_test (0x3412345678, 0x3412345678, 8, 0x3412345678, 6);
928 do_cint_test (0xff12345678, 0xff12345678, 8, 0xff12345678, 6);
929 do_cint_test (0x432112345678, 0x432112345678, 8, 0x8765432112345678, 6);
930 do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
933 } // namespace findvar_test
934 } // namespace selftests
936 #endif
938 void _initialize_findvar ();
939 void
940 _initialize_findvar ()
942 #if GDB_SELF_TEST
943 selftests::register_test
944 ("copy_integer_to_size",
945 selftests::findvar_tests::copy_integer_to_size_test);
946 #endif