[gdb/testsuite] Fix gdb.cp/namespace.exp with read1
[binutils-gdb.git] / gdb / findvar.c
blobd0bb3fd320e7b51529bb944fb19d37c28e136c28
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 "defs.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 #include "gdbsupport/selftest.h"
36 /* Basic byte-swapping routines. All 'extract' functions return a
37 host-format integer from a target-format integer at ADDR which is
38 LEN bytes long. */
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... */
45 you lose
46 #endif
48 template<typename T, typename>
50 extract_integer (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order)
52 typename std::make_unsigned<T>::type retval = 0;
54 if (buf.size () > (int) sizeof (T))
55 error (_("\
56 That operation is not available on integers of more than %d bytes."),
57 (int) sizeof (T));
59 /* Start at the most significant end of the integer, and work towards
60 the least significant. */
61 if (byte_order == BFD_ENDIAN_BIG)
63 size_t i = 0;
65 if (std::is_signed<T>::value)
67 /* Do the sign extension once at the start. */
68 retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
69 ++i;
71 for (; i < buf.size (); ++i)
72 retval = (retval << 8) | buf[i];
74 else
76 ssize_t i = buf.size () - 1;
78 if (std::is_signed<T>::value)
80 /* Do the sign extension once at the start. */
81 retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
82 --i;
84 for (; i >= 0; --i)
85 retval = (retval << 8) | buf[i];
87 return retval;
90 /* Explicit instantiations. */
91 template LONGEST extract_integer<LONGEST> (gdb::array_view<const gdb_byte> buf,
92 enum bfd_endian byte_order);
93 template ULONGEST extract_integer<ULONGEST>
94 (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order);
96 /* Sometimes a long long unsigned integer can be extracted as a
97 LONGEST value. This is done so that we can print these values
98 better. If this integer can be converted to a LONGEST, this
99 function returns 1 and sets *PVAL. Otherwise it returns 0. */
102 extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
103 enum bfd_endian byte_order, LONGEST *pval)
105 const gdb_byte *p;
106 const gdb_byte *first_addr;
107 int len;
109 len = orig_len;
110 if (byte_order == BFD_ENDIAN_BIG)
112 for (p = addr;
113 len > (int) sizeof (LONGEST) && p < addr + orig_len;
114 p++)
116 if (*p == 0)
117 len--;
118 else
119 break;
121 first_addr = p;
123 else
125 first_addr = addr;
126 for (p = addr + orig_len - 1;
127 len > (int) sizeof (LONGEST) && p >= addr;
128 p--)
130 if (*p == 0)
131 len--;
132 else
133 break;
137 if (len <= (int) sizeof (LONGEST))
139 *pval = (LONGEST) extract_unsigned_integer (first_addr,
140 sizeof (LONGEST),
141 byte_order);
142 return 1;
145 return 0;
149 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
150 address it represents. */
151 CORE_ADDR
152 extract_typed_address (const gdb_byte *buf, struct type *type)
154 gdb_assert (type->is_pointer_or_reference ());
155 return gdbarch_pointer_to_address (type->arch (), type, buf);
158 /* All 'store' functions accept a host-format integer and store a
159 target-format integer at ADDR which is LEN bytes long. */
160 template<typename T, typename>
161 void
162 store_integer (gdb::array_view<gdb_byte> dst, enum bfd_endian byte_order,
163 T val)
165 gdb_byte *p;
166 gdb_byte *startaddr = dst.data ();
167 gdb_byte *endaddr = startaddr + dst.size ();
169 /* Start at the least significant end of the integer, and work towards
170 the most significant. */
171 if (byte_order == BFD_ENDIAN_BIG)
173 for (p = endaddr - 1; p >= startaddr; --p)
175 *p = val & 0xff;
176 val >>= 8;
179 else
181 for (p = startaddr; p < endaddr; ++p)
183 *p = val & 0xff;
184 val >>= 8;
189 /* Explicit instantiations. */
190 template void store_integer (gdb::array_view<gdb_byte> dst,
191 bfd_endian byte_order, LONGEST val);
193 template void store_integer (gdb::array_view<gdb_byte> dst,
194 bfd_endian byte_order, ULONGEST val);
196 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
197 form. */
198 void
199 store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
201 gdb_assert (type->is_pointer_or_reference ());
202 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
205 /* Copy a value from SOURCE of size SOURCE_SIZE bytes to DEST of size DEST_SIZE
206 bytes. If SOURCE_SIZE is greater than DEST_SIZE, then truncate the most
207 significant bytes. If SOURCE_SIZE is less than DEST_SIZE then either sign
208 or zero extended according to IS_SIGNED. Values are stored in memory with
209 endianness BYTE_ORDER. */
211 void
212 copy_integer_to_size (gdb_byte *dest, int dest_size, const gdb_byte *source,
213 int source_size, bool is_signed,
214 enum bfd_endian byte_order)
216 signed int size_diff = dest_size - source_size;
218 /* Copy across everything from SOURCE that can fit into DEST. */
220 if (byte_order == BFD_ENDIAN_BIG && size_diff > 0)
221 memcpy (dest + size_diff, source, source_size);
222 else if (byte_order == BFD_ENDIAN_BIG && size_diff < 0)
223 memcpy (dest, source - size_diff, dest_size);
224 else
225 memcpy (dest, source, std::min (source_size, dest_size));
227 /* Fill the remaining space in DEST by either zero extending or sign
228 extending. */
230 if (size_diff > 0)
232 gdb_byte extension = 0;
233 if (is_signed
234 && ((byte_order != BFD_ENDIAN_BIG && source[source_size - 1] & 0x80)
235 || (byte_order == BFD_ENDIAN_BIG && source[0] & 0x80)))
236 extension = 0xff;
238 /* Extend into MSBs of SOURCE. */
239 if (byte_order == BFD_ENDIAN_BIG)
240 memset (dest, extension, size_diff);
241 else
242 memset (dest + source_size, extension, size_diff);
246 /* See value.h. */
248 value *
249 value_of_register (int regnum, frame_info_ptr next_frame)
251 gdbarch *gdbarch = frame_unwind_arch (next_frame);
253 /* User registers lie completely outside of the range of normal
254 registers. Catch them early so that the target never sees them. */
255 if (regnum >= gdbarch_num_cooked_regs (gdbarch))
256 return value_of_user_reg (regnum, get_prev_frame_always (next_frame));
258 value *reg_val = value_of_register_lazy (next_frame, regnum);
259 reg_val->fetch_lazy ();
260 return reg_val;
263 /* See value.h. */
265 value *
266 value_of_register_lazy (frame_info_ptr next_frame, int regnum)
268 gdbarch *gdbarch = frame_unwind_arch (next_frame);
270 gdb_assert (regnum < gdbarch_num_cooked_regs (gdbarch));
271 gdb_assert (next_frame != nullptr);
273 return value::allocate_register_lazy (next_frame, regnum);
276 /* Given a pointer of type TYPE in target form in BUF, return the
277 address it represents. */
278 CORE_ADDR
279 unsigned_pointer_to_address (struct gdbarch *gdbarch,
280 struct type *type, const gdb_byte *buf)
282 enum bfd_endian byte_order = type_byte_order (type);
284 return extract_unsigned_integer (buf, type->length (), byte_order);
287 CORE_ADDR
288 signed_pointer_to_address (struct gdbarch *gdbarch,
289 struct type *type, const gdb_byte *buf)
291 enum bfd_endian byte_order = type_byte_order (type);
293 return extract_signed_integer (buf, type->length (), byte_order);
296 /* Given an address, store it as a pointer of type TYPE in target
297 format in BUF. */
298 void
299 unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
300 gdb_byte *buf, CORE_ADDR addr)
302 enum bfd_endian byte_order = type_byte_order (type);
304 store_unsigned_integer (buf, type->length (), byte_order, addr);
307 void
308 address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
309 gdb_byte *buf, CORE_ADDR addr)
311 enum bfd_endian byte_order = type_byte_order (type);
313 store_signed_integer (buf, type->length (), byte_order, addr);
316 /* See value.h. */
318 enum symbol_needs_kind
319 symbol_read_needs (struct symbol *sym)
321 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
322 return SYMBOL_COMPUTED_OPS (sym)->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 frame_info_ptr 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 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 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 (frame != NULL)
406 frame_block = get_frame_block (frame, NULL);
408 /* Climb up the call stack until reaching the frame we are looking for. */
409 while (frame != NULL && frame_block != var_block)
411 /* Stacks can be quite deep: give the user a chance to stop this. */
412 QUIT;
414 if (frame_block == NULL)
416 frame = get_prev_frame (frame);
417 if (frame == NULL)
418 break;
419 frame_block = get_frame_block (frame, NULL);
422 /* If we failed to find the proper frame, fallback to the heuristic
423 method below. */
424 else if (frame_block->is_global_block ())
426 frame = NULL;
427 break;
430 /* Assuming we have a block for this frame: if we are at the function
431 level, the immediate upper lexical block is in an outer function:
432 follow the static link. */
433 else if (frame_block->function () != nullptr)
435 frame = frame_follow_static_link (frame);
436 if (frame != nullptr)
438 frame_block = get_frame_block (frame, nullptr);
439 if (frame_block == nullptr)
440 frame = nullptr;
444 else
445 /* We must be in some function nested lexical block. Just get the
446 outer block: both must share the same frame. */
447 frame_block = frame_block->superblock ();
450 /* Old compilers may not provide a static link, or they may provide an
451 invalid one. For such cases, fallback on the old way to evaluate
452 non-local references: just climb up the call stack and pick the first
453 frame that contains the variable we are looking for. */
454 if (frame == NULL)
456 frame = block_innermost_frame (var_block);
457 if (frame == NULL)
459 if (var_block->function ()
460 && !var_block->inlined_p ()
461 && var_block->function ()->print_name ())
462 error (_("No frame is currently executing in block %s."),
463 var_block->function ()->print_name ());
464 else
465 error (_("No frame is currently executing in specified"
466 " block"));
470 return frame;
473 /* See language.h. */
475 struct value *
476 language_defn::read_var_value (struct symbol *var,
477 const struct block *var_block,
478 frame_info_ptr frame) const
480 struct value *v;
481 struct type *type = var->type ();
482 CORE_ADDR addr;
483 enum symbol_needs_kind sym_need;
485 /* Call check_typedef on our type to make sure that, if TYPE is
486 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
487 instead of zero. However, we do not replace the typedef type by the
488 target type, because we want to keep the typedef in order to be able to
489 set the returned value type description correctly. */
490 check_typedef (type);
492 sym_need = symbol_read_needs (var);
493 if (sym_need == SYMBOL_NEEDS_FRAME)
494 gdb_assert (frame != NULL);
495 else if (sym_need == SYMBOL_NEEDS_REGISTERS && !target_has_registers ())
496 error (_("Cannot read `%s' without registers"), var->print_name ());
498 if (frame != NULL)
499 frame = get_hosting_frame (var, var_block, frame);
501 if (SYMBOL_COMPUTED_OPS (var) != NULL)
502 return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);
504 switch (var->aclass ())
506 case LOC_CONST:
507 if (is_dynamic_type (type))
509 gdb_byte bytes[sizeof (LONGEST)];
511 size_t len = std::min (sizeof (LONGEST), (size_t) type->length ());
512 store_unsigned_integer (bytes, len,
513 type_byte_order (type),
514 var->value_longest ());
515 gdb::array_view<const gdb_byte> view (bytes, len);
517 /* Value is a constant byte-sequence. */
518 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
520 /* Put the constant back in target format. */
521 v = value::allocate (type);
522 store_signed_integer (v->contents_raw ().data (), type->length (),
523 type_byte_order (type), var->value_longest ());
524 v->set_lval (not_lval);
525 return v;
527 case LOC_LABEL:
529 /* Put the constant back in target format. */
530 if (overlay_debugging)
532 struct objfile *var_objfile = var->objfile ();
533 addr = symbol_overlayed_address (var->value_address (),
534 var->obj_section (var_objfile));
536 else
537 addr = var->value_address ();
539 /* First convert the CORE_ADDR to a function pointer type, this
540 ensures the gdbarch knows what type of pointer we are
541 manipulating when value_from_pointer is called. */
542 type = builtin_type (var->arch ())->builtin_func_ptr;
543 v = value_from_pointer (type, addr);
545 /* But we want to present the value as 'void *', so cast it to the
546 required type now, this will not change the values bit
547 representation. */
548 struct type *void_ptr_type
549 = builtin_type (var->arch ())->builtin_data_ptr;
550 v = value_cast_pointers (void_ptr_type, v, 0);
551 v->set_lval (not_lval);
552 return v;
555 case LOC_CONST_BYTES:
556 if (is_dynamic_type (type))
558 gdb::array_view<const gdb_byte> view (var->value_bytes (),
559 type->length ());
561 /* Value is a constant byte-sequence. */
562 type = resolve_dynamic_type (type, view, /* Unused address. */ 0);
564 v = value::allocate (type);
565 memcpy (v->contents_raw ().data (), var->value_bytes (),
566 type->length ());
567 v->set_lval (not_lval);
568 return v;
570 case LOC_STATIC:
571 if (overlay_debugging)
572 addr
573 = symbol_overlayed_address (var->value_address (),
574 var->obj_section (var->objfile ()));
575 else
576 addr = var->value_address ();
577 break;
579 case LOC_ARG:
580 addr = get_frame_args_address (frame);
581 if (!addr)
582 error (_("Unknown argument list address for `%s'."),
583 var->print_name ());
584 addr += var->value_longest ();
585 break;
587 case LOC_REF_ARG:
589 struct value *ref;
590 CORE_ADDR argref;
592 argref = get_frame_args_address (frame);
593 if (!argref)
594 error (_("Unknown argument list address for `%s'."),
595 var->print_name ());
596 argref += var->value_longest ();
597 ref = value_at (lookup_pointer_type (type), argref);
598 addr = value_as_address (ref);
599 break;
602 case LOC_LOCAL:
603 addr = get_frame_locals_address (frame);
604 addr += var->value_longest ();
605 break;
607 case LOC_TYPEDEF:
608 error (_("Cannot look up value of a typedef `%s'."),
609 var->print_name ());
610 break;
612 case LOC_BLOCK:
613 if (overlay_debugging)
614 addr = symbol_overlayed_address
615 (var->value_block ()->entry_pc (),
616 var->obj_section (var->objfile ()));
617 else
618 addr = var->value_block ()->entry_pc ();
619 break;
621 case LOC_REGISTER:
622 case LOC_REGPARM_ADDR:
624 int regno = SYMBOL_REGISTER_OPS (var)
625 ->register_number (var, get_frame_arch (frame));
626 struct value *regval;
628 if (var->aclass () == LOC_REGPARM_ADDR)
630 regval = value_from_register (lookup_pointer_type (type),
631 regno,
632 frame);
634 if (regval == NULL)
635 error (_("Value of register variable not available for `%s'."),
636 var->print_name ());
638 addr = value_as_address (regval);
640 else
642 regval = value_from_register (type, regno, frame);
644 if (regval == NULL)
645 error (_("Value of register variable not available for `%s'."),
646 var->print_name ());
647 return regval;
650 break;
652 case LOC_COMPUTED:
653 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
655 case LOC_UNRESOLVED:
657 struct obj_section *obj_section;
658 bound_minimal_symbol bmsym;
660 gdbarch_iterate_over_objfiles_in_search_order
661 (var->arch (),
662 [var, &bmsym] (objfile *objfile)
664 bmsym = lookup_minimal_symbol (var->linkage_name (), nullptr,
665 objfile);
667 /* Stop if a match is found. */
668 return bmsym.minsym != nullptr;
670 var->objfile ());
672 /* If we can't find the minsym there's a problem in the symbol info.
673 The symbol exists in the debug info, but it's missing in the minsym
674 table. */
675 if (bmsym.minsym == nullptr)
677 const char *flavour_name
678 = objfile_flavour_name (var->objfile ());
680 /* We can't get here unless we've opened the file, so flavour_name
681 can't be NULL. */
682 gdb_assert (flavour_name != NULL);
683 error (_("Missing %s symbol \"%s\"."),
684 flavour_name, var->linkage_name ());
687 obj_section = bmsym.minsym->obj_section (bmsym.objfile);
688 /* Relocate address, unless there is no section or the variable is
689 a TLS variable. */
690 if (obj_section == NULL
691 || (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
692 addr = CORE_ADDR (bmsym.minsym->unrelocated_address ());
693 else
694 addr = bmsym.value_address ();
695 if (overlay_debugging)
696 addr = symbol_overlayed_address (addr, obj_section);
697 /* Determine address of TLS variable. */
698 if (obj_section
699 && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
700 addr = target_translate_tls_address (obj_section->objfile, addr);
702 break;
704 case LOC_OPTIMIZED_OUT:
705 if (is_dynamic_type (type))
706 type = resolve_dynamic_type (type, {}, /* Unused address. */ 0);
707 return value::allocate_optimized_out (type);
709 default:
710 error (_("Cannot look up value of a botched symbol `%s'."),
711 var->print_name ());
712 break;
715 v = value_at_lazy (type, addr);
716 return v;
719 /* Calls VAR's language read_var_value hook with the given arguments. */
721 struct value *
722 read_var_value (struct symbol *var, const struct block *var_block,
723 frame_info_ptr frame)
725 const struct language_defn *lang = language_def (var->language ());
727 gdb_assert (lang != NULL);
729 return lang->read_var_value (var, var_block, frame);
732 /* Install default attributes for register values. */
734 value *
735 default_value_from_register (gdbarch *gdbarch, type *type, int regnum,
736 const frame_info_ptr &this_frame)
738 value *value
739 = value::allocate_register (get_next_frame_sentinel_okay (this_frame),
740 regnum, type);
742 /* Any structure stored in more than one register will always be
743 an integral number of registers. Otherwise, you need to do
744 some fiddling with the last register copied here for little
745 endian machines. */
746 if (type_byte_order (type) == BFD_ENDIAN_BIG
747 && type->length () < register_size (gdbarch, regnum))
748 /* Big-endian, and we want less than full size. */
749 value->set_offset (register_size (gdbarch, regnum) - type->length ());
750 else
751 value->set_offset (0);
753 return value;
756 /* VALUE must be an lval_register value. If regnum is the value's
757 associated register number, and len the length of the value's type,
758 read one or more registers in VALUE's frame, starting with register REGNUM,
759 until we've read LEN bytes.
761 If any of the registers we try to read are optimized out, then mark the
762 complete resulting value as optimized out. */
764 static void
765 read_frame_register_value (value *value)
767 gdb_assert (value->lval () == lval_register);
769 frame_info_ptr next_frame = frame_find_by_id (value->next_frame_id ());
770 gdb_assert (next_frame != nullptr);
772 gdbarch *gdbarch = frame_unwind_arch (next_frame);
773 LONGEST offset = 0;
774 LONGEST reg_offset = value->offset ();
775 int regnum = value->regnum ();
776 int len = type_length_units (check_typedef (value->type ()));
778 /* Skip registers wholly inside of REG_OFFSET. */
779 while (reg_offset >= register_size (gdbarch, regnum))
781 reg_offset -= register_size (gdbarch, regnum);
782 regnum++;
785 /* Copy the data. */
786 while (len > 0)
788 struct value *regval = frame_unwind_register_value (next_frame, regnum);
789 int reg_len = type_length_units (regval->type ()) - reg_offset;
791 /* If the register length is larger than the number of bytes
792 remaining to copy, then only copy the appropriate bytes. */
793 if (reg_len > len)
794 reg_len = len;
796 regval->contents_copy (value, offset, reg_offset, reg_len);
798 offset += reg_len;
799 len -= reg_len;
800 reg_offset = 0;
801 regnum++;
805 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
807 struct value *
808 value_from_register (struct type *type, int regnum, frame_info_ptr frame)
810 struct gdbarch *gdbarch = get_frame_arch (frame);
811 struct type *type1 = check_typedef (type);
812 struct value *v;
814 if (gdbarch_convert_register_p (gdbarch, regnum, type1))
816 int optim, unavail, ok;
818 /* The ISA/ABI need to something weird when obtaining the
819 specified value from this register. It might need to
820 re-order non-adjacent, starting with REGNUM (see MIPS and
821 i386). It might need to convert the [float] register into
822 the corresponding [integer] type (see Alpha). The assumption
823 is that gdbarch_register_to_value populates the entire value
824 including the location. */
825 v = value::allocate_register (get_next_frame_sentinel_okay (frame),
826 regnum, type);
827 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
828 v->contents_raw ().data (), &optim,
829 &unavail);
831 if (!ok)
833 if (optim)
834 v->mark_bytes_optimized_out (0, type->length ());
835 if (unavail)
836 v->mark_bytes_unavailable (0, type->length ());
839 else
841 /* Construct the value. */
842 v = gdbarch_value_from_register (gdbarch, type, regnum, frame);
844 /* Get the data. */
845 read_frame_register_value (v);
848 return v;
851 /* Return contents of register REGNUM in frame FRAME as address.
852 Will abort if register value is not available. */
854 CORE_ADDR
855 address_from_register (int regnum, frame_info_ptr frame)
857 type *type = builtin_type (get_frame_arch (frame))->builtin_data_ptr;
858 value_ref_ptr v = release_value (value_from_register (type, regnum, frame));
860 if (v->optimized_out ())
862 /* This function is used while computing a location expression.
863 Complain about the value being optimized out, rather than
864 letting value_as_address complain about some random register
865 the expression depends on not being saved. */
866 error_value_optimized_out ();
869 return value_as_address (v.get ());
872 #if GDB_SELF_TEST
873 namespace selftests {
874 namespace findvar_tests {
876 /* Function to test copy_integer_to_size. Store SOURCE_VAL with size
877 SOURCE_SIZE to a buffer, making sure no sign extending happens at this
878 stage. Copy buffer to a new buffer using copy_integer_to_size. Extract
879 copied value and compare to DEST_VALU. Copy again with a signed
880 copy_integer_to_size and compare to DEST_VALS. Do everything for both
881 LITTLE and BIG target endians. Use unsigned values throughout to make
882 sure there are no implicit sign extensions. */
884 static void
885 do_cint_test (ULONGEST dest_valu, ULONGEST dest_vals, int dest_size,
886 ULONGEST src_val, int src_size)
888 for (int i = 0; i < 2 ; i++)
890 gdb_byte srcbuf[sizeof (ULONGEST)] = {};
891 gdb_byte destbuf[sizeof (ULONGEST)] = {};
892 enum bfd_endian byte_order = i ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
894 /* Fill the src buffer (and later the dest buffer) with non-zero junk,
895 to ensure zero extensions aren't hidden. */
896 memset (srcbuf, 0xaa, sizeof (srcbuf));
898 /* Store (and later extract) using unsigned to ensure there are no sign
899 extensions. */
900 store_unsigned_integer (srcbuf, src_size, byte_order, src_val);
902 /* Test unsigned. */
903 memset (destbuf, 0xaa, sizeof (destbuf));
904 copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, false,
905 byte_order);
906 SELF_CHECK (dest_valu == extract_unsigned_integer (destbuf, dest_size,
907 byte_order));
909 /* Test signed. */
910 memset (destbuf, 0xaa, sizeof (destbuf));
911 copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, true,
912 byte_order);
913 SELF_CHECK (dest_vals == extract_unsigned_integer (destbuf, dest_size,
914 byte_order));
918 static void
919 copy_integer_to_size_test ()
921 /* Destination is bigger than the source, which has the signed bit unset. */
922 do_cint_test (0x12345678, 0x12345678, 8, 0x12345678, 4);
923 do_cint_test (0x345678, 0x345678, 8, 0x12345678, 3);
925 /* Destination is bigger than the source, which has the signed bit set. */
926 do_cint_test (0xdeadbeef, 0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
927 do_cint_test (0xadbeef, 0xffffffffffadbeef, 8, 0xdeadbeef, 3);
929 /* Destination is smaller than the source. */
930 do_cint_test (0x5678, 0x5678, 2, 0x12345678, 3);
931 do_cint_test (0xbeef, 0xbeef, 2, 0xdeadbeef, 3);
933 /* Destination and source are the same size. */
934 do_cint_test (0x8765432112345678, 0x8765432112345678, 8, 0x8765432112345678,
936 do_cint_test (0x432112345678, 0x432112345678, 6, 0x8765432112345678, 6);
937 do_cint_test (0xfeedbeaddeadbeef, 0xfeedbeaddeadbeef, 8, 0xfeedbeaddeadbeef,
939 do_cint_test (0xbeaddeadbeef, 0xbeaddeadbeef, 6, 0xfeedbeaddeadbeef, 6);
941 /* Destination is bigger than the source. Source is bigger than 32bits. */
942 do_cint_test (0x3412345678, 0x3412345678, 8, 0x3412345678, 6);
943 do_cint_test (0xff12345678, 0xff12345678, 8, 0xff12345678, 6);
944 do_cint_test (0x432112345678, 0x432112345678, 8, 0x8765432112345678, 6);
945 do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
948 } // namespace findvar_test
949 } // namespace selftests
951 #endif
953 void _initialize_findvar ();
954 void
955 _initialize_findvar ()
957 #if GDB_SELF_TEST
958 selftests::register_test
959 ("copy_integer_to_size",
960 selftests::findvar_tests::copy_integer_to_size_test);
961 #endif