1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
3 Copyright (C) 1986-2021 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/>. */
21 #include "arch-utils.h"
33 #include "target-float.h"
36 #include "cli/cli-decode.h"
37 #include "extension.h"
39 #include "tracepoint.h"
41 #include "user-regs.h"
43 #include "completer.h"
44 #include "gdbsupport/selftest.h"
45 #include "gdbsupport/array-view.h"
46 #include "cli/cli-style.h"
49 /* Definition of a user function. */
50 struct internal_function
52 /* The name of the function. It is a bit odd to have this in the
53 function itself -- the user might use a differently-named
54 convenience variable to hold the function. */
58 internal_function_fn handler
;
60 /* User data for the handler. */
64 /* Defines an [OFFSET, OFFSET + LENGTH) range. */
68 /* Lowest offset in the range. */
71 /* Length of the range. */
74 /* Returns true if THIS is strictly less than OTHER, useful for
75 searching. We keep ranges sorted by offset and coalesce
76 overlapping and contiguous ranges, so this just compares the
79 bool operator< (const range
&other
) const
81 return offset
< other
.offset
;
84 /* Returns true if THIS is equal to OTHER. */
85 bool operator== (const range
&other
) const
87 return offset
== other
.offset
&& length
== other
.length
;
91 /* Returns true if the ranges defined by [offset1, offset1+len1) and
92 [offset2, offset2+len2) overlap. */
95 ranges_overlap (LONGEST offset1
, LONGEST len1
,
96 LONGEST offset2
, LONGEST len2
)
100 l
= std::max (offset1
, offset2
);
101 h
= std::min (offset1
+ len1
, offset2
+ len2
);
105 /* Returns true if RANGES contains any range that overlaps [OFFSET,
109 ranges_contain (const std::vector
<range
> &ranges
, LONGEST offset
,
114 what
.offset
= offset
;
115 what
.length
= length
;
117 /* We keep ranges sorted by offset and coalesce overlapping and
118 contiguous ranges, so to check if a range list contains a given
119 range, we can do a binary search for the position the given range
120 would be inserted if we only considered the starting OFFSET of
121 ranges. We call that position I. Since we also have LENGTH to
122 care for (this is a range afterall), we need to check if the
123 _previous_ range overlaps the I range. E.g.,
127 |---| |---| |------| ... |--|
132 In the case above, the binary search would return `I=1', meaning,
133 this OFFSET should be inserted at position 1, and the current
134 position 1 should be pushed further (and before 2). But, `0'
137 Then we need to check if the I range overlaps the I range itself.
142 |---| |---| |-------| ... |--|
149 auto i
= std::lower_bound (ranges
.begin (), ranges
.end (), what
);
151 if (i
> ranges
.begin ())
153 const struct range
&bef
= *(i
- 1);
155 if (ranges_overlap (bef
.offset
, bef
.length
, offset
, length
))
159 if (i
< ranges
.end ())
161 const struct range
&r
= *i
;
163 if (ranges_overlap (r
.offset
, r
.length
, offset
, length
))
170 static struct cmd_list_element
*functionlist
;
172 /* Note that the fields in this structure are arranged to save a bit
177 explicit value (struct type
*type_
)
183 enclosing_type (type_
)
189 if (VALUE_LVAL (this) == lval_computed
)
191 const struct lval_funcs
*funcs
= location
.computed
.funcs
;
193 if (funcs
->free_closure
)
194 funcs
->free_closure (this);
196 else if (VALUE_LVAL (this) == lval_xcallable
)
197 delete location
.xm_worker
;
200 DISABLE_COPY_AND_ASSIGN (value
);
202 /* Type of value; either not an lval, or one of the various
203 different possible kinds of lval. */
204 enum lval_type lval
= not_lval
;
206 /* Is it modifiable? Only relevant if lval != not_lval. */
207 unsigned int modifiable
: 1;
209 /* If zero, contents of this value are in the contents field. If
210 nonzero, contents are in inferior. If the lval field is lval_memory,
211 the contents are in inferior memory at location.address plus offset.
212 The lval field may also be lval_register.
214 WARNING: This field is used by the code which handles watchpoints
215 (see breakpoint.c) to decide whether a particular value can be
216 watched by hardware watchpoints. If the lazy flag is set for
217 some member of a value chain, it is assumed that this member of
218 the chain doesn't need to be watched as part of watching the
219 value itself. This is how GDB avoids watching the entire struct
220 or array when the user wants to watch a single struct member or
221 array element. If you ever change the way lazy flag is set and
222 reset, be sure to consider this use as well! */
223 unsigned int lazy
: 1;
225 /* If value is a variable, is it initialized or not. */
226 unsigned int initialized
: 1;
228 /* If value is from the stack. If this is set, read_stack will be
229 used instead of read_memory to enable extra caching. */
230 unsigned int stack
: 1;
232 /* Location of value (if lval). */
235 /* If lval == lval_memory, this is the address in the inferior */
238 /*If lval == lval_register, the value is from a register. */
241 /* Register number. */
243 /* Frame ID of "next" frame to which a register value is relative.
244 If the register value is found relative to frame F, then the
245 frame id of F->next will be stored in next_frame_id. */
246 struct frame_id next_frame_id
;
249 /* Pointer to internal variable. */
250 struct internalvar
*internalvar
;
252 /* Pointer to xmethod worker. */
253 struct xmethod_worker
*xm_worker
;
255 /* If lval == lval_computed, this is a set of function pointers
256 to use to access and describe the value, and a closure pointer
260 /* Functions to call. */
261 const struct lval_funcs
*funcs
;
263 /* Closure for those functions to use. */
268 /* Describes offset of a value within lval of a structure in target
269 addressable memory units. Note also the member embedded_offset
273 /* Only used for bitfields; number of bits contained in them. */
276 /* Only used for bitfields; position of start of field. For
277 little-endian targets, it is the position of the LSB. For
278 big-endian targets, it is the position of the MSB. */
281 /* The number of references to this value. When a value is created,
282 the value chain holds a reference, so REFERENCE_COUNT is 1. If
283 release_value is called, this value is removed from the chain but
284 the caller of release_value now has a reference to this value.
285 The caller must arrange for a call to value_free later. */
286 int reference_count
= 1;
288 /* Only used for bitfields; the containing value. This allows a
289 single read from the target when displaying multiple
291 value_ref_ptr parent
;
293 /* Type of the value. */
296 /* If a value represents a C++ object, then the `type' field gives
297 the object's compile-time type. If the object actually belongs
298 to some class derived from `type', perhaps with other base
299 classes and additional members, then `type' is just a subobject
300 of the real thing, and the full object is probably larger than
301 `type' would suggest.
303 If `type' is a dynamic class (i.e. one with a vtable), then GDB
304 can actually determine the object's run-time type by looking at
305 the run-time type information in the vtable. When this
306 information is available, we may elect to read in the entire
307 object, for several reasons:
309 - When printing the value, the user would probably rather see the
310 full object, not just the limited portion apparent from the
313 - If `type' has virtual base classes, then even printing `type'
314 alone may require reaching outside the `type' portion of the
315 object to wherever the virtual base class has been stored.
317 When we store the entire object, `enclosing_type' is the run-time
318 type -- the complete object -- and `embedded_offset' is the
319 offset of `type' within that larger type, in target addressable memory
320 units. The value_contents() macro takes `embedded_offset' into account,
321 so most GDB code continues to see the `type' portion of the value, just
322 as the inferior would.
324 If `type' is a pointer to an object, then `enclosing_type' is a
325 pointer to the object's run-time type, and `pointed_to_offset' is
326 the offset in target addressable memory units from the full object
327 to the pointed-to object -- that is, the value `embedded_offset' would
328 have if we followed the pointer and fetched the complete object.
329 (I don't really see the point. Why not just determine the
330 run-time type when you indirect, and avoid the special case? The
331 contents don't matter until you indirect anyway.)
333 If we're not doing anything fancy, `enclosing_type' is equal to
334 `type', and `embedded_offset' is zero, so everything works
336 struct type
*enclosing_type
;
337 LONGEST embedded_offset
= 0;
338 LONGEST pointed_to_offset
= 0;
340 /* Actual contents of the value. Target byte-order. NULL or not
341 valid if lazy is nonzero. */
342 gdb::unique_xmalloc_ptr
<gdb_byte
> contents
;
344 /* Unavailable ranges in CONTENTS. We mark unavailable ranges,
345 rather than available, since the common and default case is for a
346 value to be available. This is filled in at value read time.
347 The unavailable ranges are tracked in bits. Note that a contents
348 bit that has been optimized out doesn't really exist in the
349 program, so it can't be marked unavailable either. */
350 std::vector
<range
> unavailable
;
352 /* Likewise, but for optimized out contents (a chunk of the value of
353 a variable that does not actually exist in the program). If LVAL
354 is lval_register, this is a register ($pc, $sp, etc., never a
355 program variable) that has not been saved in the frame. Not
356 saved registers and optimized-out program variables values are
357 treated pretty much the same, except not-saved registers have a
358 different string representation and related error strings. */
359 std::vector
<range
> optimized_out
;
365 get_value_arch (const struct value
*value
)
367 return value_type (value
)->arch ();
371 value_bits_available (const struct value
*value
, LONGEST offset
, LONGEST length
)
373 gdb_assert (!value
->lazy
);
375 return !ranges_contain (value
->unavailable
, offset
, length
);
379 value_bytes_available (const struct value
*value
,
380 LONGEST offset
, LONGEST length
)
382 return value_bits_available (value
,
383 offset
* TARGET_CHAR_BIT
,
384 length
* TARGET_CHAR_BIT
);
388 value_bits_any_optimized_out (const struct value
*value
, int bit_offset
, int bit_length
)
390 gdb_assert (!value
->lazy
);
392 return ranges_contain (value
->optimized_out
, bit_offset
, bit_length
);
396 value_entirely_available (struct value
*value
)
398 /* We can only tell whether the whole value is available when we try
401 value_fetch_lazy (value
);
403 if (value
->unavailable
.empty ())
408 /* Returns true if VALUE is entirely covered by RANGES. If the value
409 is lazy, it'll be read now. Note that RANGE is a pointer to
410 pointer because reading the value might change *RANGE. */
413 value_entirely_covered_by_range_vector (struct value
*value
,
414 const std::vector
<range
> &ranges
)
416 /* We can only tell whether the whole value is optimized out /
417 unavailable when we try to read it. */
419 value_fetch_lazy (value
);
421 if (ranges
.size () == 1)
423 const struct range
&t
= ranges
[0];
426 && t
.length
== (TARGET_CHAR_BIT
427 * TYPE_LENGTH (value_enclosing_type (value
))))
435 value_entirely_unavailable (struct value
*value
)
437 return value_entirely_covered_by_range_vector (value
, value
->unavailable
);
441 value_entirely_optimized_out (struct value
*value
)
443 return value_entirely_covered_by_range_vector (value
, value
->optimized_out
);
446 /* Insert into the vector pointed to by VECTORP the bit range starting of
447 OFFSET bits, and extending for the next LENGTH bits. */
450 insert_into_bit_range_vector (std::vector
<range
> *vectorp
,
451 LONGEST offset
, LONGEST length
)
455 /* Insert the range sorted. If there's overlap or the new range
456 would be contiguous with an existing range, merge. */
458 newr
.offset
= offset
;
459 newr
.length
= length
;
461 /* Do a binary search for the position the given range would be
462 inserted if we only considered the starting OFFSET of ranges.
463 Call that position I. Since we also have LENGTH to care for
464 (this is a range afterall), we need to check if the _previous_
465 range overlaps the I range. E.g., calling R the new range:
467 #1 - overlaps with previous
471 |---| |---| |------| ... |--|
476 In the case #1 above, the binary search would return `I=1',
477 meaning, this OFFSET should be inserted at position 1, and the
478 current position 1 should be pushed further (and become 2). But,
479 note that `0' overlaps with R, so we want to merge them.
481 A similar consideration needs to be taken if the new range would
482 be contiguous with the previous range:
484 #2 - contiguous with previous
488 |--| |---| |------| ... |--|
493 If there's no overlap with the previous range, as in:
495 #3 - not overlapping and not contiguous
499 |--| |---| |------| ... |--|
506 #4 - R is the range with lowest offset
510 |--| |---| |------| ... |--|
515 ... we just push the new range to I.
517 All the 4 cases above need to consider that the new range may
518 also overlap several of the ranges that follow, or that R may be
519 contiguous with the following range, and merge. E.g.,
521 #5 - overlapping following ranges
524 |------------------------|
525 |--| |---| |------| ... |--|
534 |--| |---| |------| ... |--|
541 auto i
= std::lower_bound (vectorp
->begin (), vectorp
->end (), newr
);
542 if (i
> vectorp
->begin ())
544 struct range
&bef
= *(i
- 1);
546 if (ranges_overlap (bef
.offset
, bef
.length
, offset
, length
))
549 ULONGEST l
= std::min (bef
.offset
, offset
);
550 ULONGEST h
= std::max (bef
.offset
+ bef
.length
, offset
+ length
);
556 else if (offset
== bef
.offset
+ bef
.length
)
559 bef
.length
+= length
;
565 i
= vectorp
->insert (i
, newr
);
571 i
= vectorp
->insert (i
, newr
);
574 /* Check whether the ranges following the one we've just added or
575 touched can be folded in (#5 above). */
576 if (i
!= vectorp
->end () && i
+ 1 < vectorp
->end ())
581 /* Get the range we just touched. */
582 struct range
&t
= *i
;
586 for (; i
< vectorp
->end (); i
++)
588 struct range
&r
= *i
;
589 if (r
.offset
<= t
.offset
+ t
.length
)
593 l
= std::min (t
.offset
, r
.offset
);
594 h
= std::max (t
.offset
+ t
.length
, r
.offset
+ r
.length
);
603 /* If we couldn't merge this one, we won't be able to
604 merge following ones either, since the ranges are
605 always sorted by OFFSET. */
611 vectorp
->erase (next
, next
+ removed
);
616 mark_value_bits_unavailable (struct value
*value
,
617 LONGEST offset
, LONGEST length
)
619 insert_into_bit_range_vector (&value
->unavailable
, offset
, length
);
623 mark_value_bytes_unavailable (struct value
*value
,
624 LONGEST offset
, LONGEST length
)
626 mark_value_bits_unavailable (value
,
627 offset
* TARGET_CHAR_BIT
,
628 length
* TARGET_CHAR_BIT
);
631 /* Find the first range in RANGES that overlaps the range defined by
632 OFFSET and LENGTH, starting at element POS in the RANGES vector,
633 Returns the index into RANGES where such overlapping range was
634 found, or -1 if none was found. */
637 find_first_range_overlap (const std::vector
<range
> *ranges
, int pos
,
638 LONGEST offset
, LONGEST length
)
642 for (i
= pos
; i
< ranges
->size (); i
++)
644 const range
&r
= (*ranges
)[i
];
645 if (ranges_overlap (r
.offset
, r
.length
, offset
, length
))
652 /* Compare LENGTH_BITS of memory at PTR1 + OFFSET1_BITS with the memory at
653 PTR2 + OFFSET2_BITS. Return 0 if the memory is the same, otherwise
656 It must always be the case that:
657 OFFSET1_BITS % TARGET_CHAR_BIT == OFFSET2_BITS % TARGET_CHAR_BIT
659 It is assumed that memory can be accessed from:
660 PTR + (OFFSET_BITS / TARGET_CHAR_BIT)
662 PTR + ((OFFSET_BITS + LENGTH_BITS + TARGET_CHAR_BIT - 1)
663 / TARGET_CHAR_BIT) */
665 memcmp_with_bit_offsets (const gdb_byte
*ptr1
, size_t offset1_bits
,
666 const gdb_byte
*ptr2
, size_t offset2_bits
,
669 gdb_assert (offset1_bits
% TARGET_CHAR_BIT
670 == offset2_bits
% TARGET_CHAR_BIT
);
672 if (offset1_bits
% TARGET_CHAR_BIT
!= 0)
675 gdb_byte mask
, b1
, b2
;
677 /* The offset from the base pointers PTR1 and PTR2 is not a complete
678 number of bytes. A number of bits up to either the next exact
679 byte boundary, or LENGTH_BITS (which ever is sooner) will be
681 bits
= TARGET_CHAR_BIT
- offset1_bits
% TARGET_CHAR_BIT
;
682 gdb_assert (bits
< sizeof (mask
) * TARGET_CHAR_BIT
);
683 mask
= (1 << bits
) - 1;
685 if (length_bits
< bits
)
687 mask
&= ~(gdb_byte
) ((1 << (bits
- length_bits
)) - 1);
691 /* Now load the two bytes and mask off the bits we care about. */
692 b1
= *(ptr1
+ offset1_bits
/ TARGET_CHAR_BIT
) & mask
;
693 b2
= *(ptr2
+ offset2_bits
/ TARGET_CHAR_BIT
) & mask
;
698 /* Now update the length and offsets to take account of the bits
699 we've just compared. */
701 offset1_bits
+= bits
;
702 offset2_bits
+= bits
;
705 if (length_bits
% TARGET_CHAR_BIT
!= 0)
709 gdb_byte mask
, b1
, b2
;
711 /* The length is not an exact number of bytes. After the previous
712 IF.. block then the offsets are byte aligned, or the
713 length is zero (in which case this code is not reached). Compare
714 a number of bits at the end of the region, starting from an exact
716 bits
= length_bits
% TARGET_CHAR_BIT
;
717 o1
= offset1_bits
+ length_bits
- bits
;
718 o2
= offset2_bits
+ length_bits
- bits
;
720 gdb_assert (bits
< sizeof (mask
) * TARGET_CHAR_BIT
);
721 mask
= ((1 << bits
) - 1) << (TARGET_CHAR_BIT
- bits
);
723 gdb_assert (o1
% TARGET_CHAR_BIT
== 0);
724 gdb_assert (o2
% TARGET_CHAR_BIT
== 0);
726 b1
= *(ptr1
+ o1
/ TARGET_CHAR_BIT
) & mask
;
727 b2
= *(ptr2
+ o2
/ TARGET_CHAR_BIT
) & mask
;
737 /* We've now taken care of any stray "bits" at the start, or end of
738 the region to compare, the remainder can be covered with a simple
740 gdb_assert (offset1_bits
% TARGET_CHAR_BIT
== 0);
741 gdb_assert (offset2_bits
% TARGET_CHAR_BIT
== 0);
742 gdb_assert (length_bits
% TARGET_CHAR_BIT
== 0);
744 return memcmp (ptr1
+ offset1_bits
/ TARGET_CHAR_BIT
,
745 ptr2
+ offset2_bits
/ TARGET_CHAR_BIT
,
746 length_bits
/ TARGET_CHAR_BIT
);
749 /* Length is zero, regions match. */
753 /* Helper struct for find_first_range_overlap_and_match and
754 value_contents_bits_eq. Keep track of which slot of a given ranges
755 vector have we last looked at. */
757 struct ranges_and_idx
760 const std::vector
<range
> *ranges
;
762 /* The range we've last found in RANGES. Given ranges are sorted,
763 we can start the next lookup here. */
767 /* Helper function for value_contents_bits_eq. Compare LENGTH bits of
768 RP1's ranges starting at OFFSET1 bits with LENGTH bits of RP2's
769 ranges starting at OFFSET2 bits. Return true if the ranges match
770 and fill in *L and *H with the overlapping window relative to
771 (both) OFFSET1 or OFFSET2. */
774 find_first_range_overlap_and_match (struct ranges_and_idx
*rp1
,
775 struct ranges_and_idx
*rp2
,
776 LONGEST offset1
, LONGEST offset2
,
777 LONGEST length
, ULONGEST
*l
, ULONGEST
*h
)
779 rp1
->idx
= find_first_range_overlap (rp1
->ranges
, rp1
->idx
,
781 rp2
->idx
= find_first_range_overlap (rp2
->ranges
, rp2
->idx
,
784 if (rp1
->idx
== -1 && rp2
->idx
== -1)
790 else if (rp1
->idx
== -1 || rp2
->idx
== -1)
794 const range
*r1
, *r2
;
798 r1
= &(*rp1
->ranges
)[rp1
->idx
];
799 r2
= &(*rp2
->ranges
)[rp2
->idx
];
801 /* Get the unavailable windows intersected by the incoming
802 ranges. The first and last ranges that overlap the argument
803 range may be wider than said incoming arguments ranges. */
804 l1
= std::max (offset1
, r1
->offset
);
805 h1
= std::min (offset1
+ length
, r1
->offset
+ r1
->length
);
807 l2
= std::max (offset2
, r2
->offset
);
808 h2
= std::min (offset2
+ length
, offset2
+ r2
->length
);
810 /* Make them relative to the respective start offsets, so we can
811 compare them for equality. */
818 /* Different ranges, no match. */
819 if (l1
!= l2
|| h1
!= h2
)
828 /* Helper function for value_contents_eq. The only difference is that
829 this function is bit rather than byte based.
831 Compare LENGTH bits of VAL1's contents starting at OFFSET1 bits
832 with LENGTH bits of VAL2's contents starting at OFFSET2 bits.
833 Return true if the available bits match. */
836 value_contents_bits_eq (const struct value
*val1
, int offset1
,
837 const struct value
*val2
, int offset2
,
840 /* Each array element corresponds to a ranges source (unavailable,
841 optimized out). '1' is for VAL1, '2' for VAL2. */
842 struct ranges_and_idx rp1
[2], rp2
[2];
844 /* See function description in value.h. */
845 gdb_assert (!val1
->lazy
&& !val2
->lazy
);
847 /* We shouldn't be trying to compare past the end of the values. */
848 gdb_assert (offset1
+ length
849 <= TYPE_LENGTH (val1
->enclosing_type
) * TARGET_CHAR_BIT
);
850 gdb_assert (offset2
+ length
851 <= TYPE_LENGTH (val2
->enclosing_type
) * TARGET_CHAR_BIT
);
853 memset (&rp1
, 0, sizeof (rp1
));
854 memset (&rp2
, 0, sizeof (rp2
));
855 rp1
[0].ranges
= &val1
->unavailable
;
856 rp2
[0].ranges
= &val2
->unavailable
;
857 rp1
[1].ranges
= &val1
->optimized_out
;
858 rp2
[1].ranges
= &val2
->optimized_out
;
862 ULONGEST l
= 0, h
= 0; /* init for gcc -Wall */
865 for (i
= 0; i
< 2; i
++)
867 ULONGEST l_tmp
, h_tmp
;
869 /* The contents only match equal if the invalid/unavailable
870 contents ranges match as well. */
871 if (!find_first_range_overlap_and_match (&rp1
[i
], &rp2
[i
],
872 offset1
, offset2
, length
,
876 /* We're interested in the lowest/first range found. */
877 if (i
== 0 || l_tmp
< l
)
884 /* Compare the available/valid contents. */
885 if (memcmp_with_bit_offsets (val1
->contents
.get (), offset1
,
886 val2
->contents
.get (), offset2
, l
) != 0)
898 value_contents_eq (const struct value
*val1
, LONGEST offset1
,
899 const struct value
*val2
, LONGEST offset2
,
902 return value_contents_bits_eq (val1
, offset1
* TARGET_CHAR_BIT
,
903 val2
, offset2
* TARGET_CHAR_BIT
,
904 length
* TARGET_CHAR_BIT
);
908 /* The value-history records all the values printed by print commands
909 during this session. */
911 static std::vector
<value_ref_ptr
> value_history
;
914 /* List of all value objects currently allocated
915 (except for those released by calls to release_value)
916 This is so they can be freed after each command. */
918 static std::vector
<value_ref_ptr
> all_values
;
920 /* Allocate a lazy value for type TYPE. Its actual content is
921 "lazily" allocated too: the content field of the return value is
922 NULL; it will be allocated when it is fetched from the target. */
925 allocate_value_lazy (struct type
*type
)
929 /* Call check_typedef on our type to make sure that, if TYPE
930 is a TYPE_CODE_TYPEDEF, its length is set to the length
931 of the target type instead of zero. However, we do not
932 replace the typedef type by the target type, because we want
933 to keep the typedef in order to be able to set the VAL's type
934 description correctly. */
935 check_typedef (type
);
937 val
= new struct value (type
);
939 /* Values start out on the all_values chain. */
940 all_values
.emplace_back (val
);
945 /* The maximum size, in bytes, that GDB will try to allocate for a value.
946 The initial value of 64k was not selected for any specific reason, it is
947 just a reasonable starting point. */
949 static int max_value_size
= 65536; /* 64k bytes */
951 /* It is critical that the MAX_VALUE_SIZE is at least as big as the size of
952 LONGEST, otherwise GDB will not be able to parse integer values from the
953 CLI; for example if the MAX_VALUE_SIZE could be set to 1 then GDB would
954 be unable to parse "set max-value-size 2".
956 As we want a consistent GDB experience across hosts with different sizes
957 of LONGEST, this arbitrary minimum value was selected, so long as this
958 is bigger than LONGEST on all GDB supported hosts we're fine. */
960 #define MIN_VALUE_FOR_MAX_VALUE_SIZE 16
961 gdb_static_assert (sizeof (LONGEST
) <= MIN_VALUE_FOR_MAX_VALUE_SIZE
);
963 /* Implement the "set max-value-size" command. */
966 set_max_value_size (const char *args
, int from_tty
,
967 struct cmd_list_element
*c
)
969 gdb_assert (max_value_size
== -1 || max_value_size
>= 0);
971 if (max_value_size
> -1 && max_value_size
< MIN_VALUE_FOR_MAX_VALUE_SIZE
)
973 max_value_size
= MIN_VALUE_FOR_MAX_VALUE_SIZE
;
974 error (_("max-value-size set too low, increasing to %d bytes"),
979 /* Implement the "show max-value-size" command. */
982 show_max_value_size (struct ui_file
*file
, int from_tty
,
983 struct cmd_list_element
*c
, const char *value
)
985 if (max_value_size
== -1)
986 fprintf_filtered (file
, _("Maximum value size is unlimited.\n"));
988 fprintf_filtered (file
, _("Maximum value size is %d bytes.\n"),
992 /* Called before we attempt to allocate or reallocate a buffer for the
993 contents of a value. TYPE is the type of the value for which we are
994 allocating the buffer. If the buffer is too large (based on the user
995 controllable setting) then throw an error. If this function returns
996 then we should attempt to allocate the buffer. */
999 check_type_length_before_alloc (const struct type
*type
)
1001 ULONGEST length
= TYPE_LENGTH (type
);
1003 if (max_value_size
> -1 && length
> max_value_size
)
1005 if (type
->name () != NULL
)
1006 error (_("value of type `%s' requires %s bytes, which is more "
1007 "than max-value-size"), type
->name (), pulongest (length
));
1009 error (_("value requires %s bytes, which is more than "
1010 "max-value-size"), pulongest (length
));
1014 /* Allocate the contents of VAL if it has not been allocated yet. */
1017 allocate_value_contents (struct value
*val
)
1021 check_type_length_before_alloc (val
->enclosing_type
);
1023 ((gdb_byte
*) xzalloc (TYPE_LENGTH (val
->enclosing_type
)));
1027 /* Allocate a value and its contents for type TYPE. */
1030 allocate_value (struct type
*type
)
1032 struct value
*val
= allocate_value_lazy (type
);
1034 allocate_value_contents (val
);
1039 /* Allocate a value that has the correct length
1040 for COUNT repetitions of type TYPE. */
1043 allocate_repeat_value (struct type
*type
, int count
)
1045 /* Despite the fact that we are really creating an array of TYPE here, we
1046 use the string lower bound as the array lower bound. This seems to
1047 work fine for now. */
1048 int low_bound
= current_language
->string_lower_bound ();
1049 /* FIXME-type-allocation: need a way to free this type when we are
1051 struct type
*array_type
1052 = lookup_array_range_type (type
, low_bound
, count
+ low_bound
- 1);
1054 return allocate_value (array_type
);
1058 allocate_computed_value (struct type
*type
,
1059 const struct lval_funcs
*funcs
,
1062 struct value
*v
= allocate_value_lazy (type
);
1064 VALUE_LVAL (v
) = lval_computed
;
1065 v
->location
.computed
.funcs
= funcs
;
1066 v
->location
.computed
.closure
= closure
;
1071 /* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */
1074 allocate_optimized_out_value (struct type
*type
)
1076 struct value
*retval
= allocate_value_lazy (type
);
1078 mark_value_bytes_optimized_out (retval
, 0, TYPE_LENGTH (type
));
1079 set_value_lazy (retval
, 0);
1083 /* Accessor methods. */
1086 value_type (const struct value
*value
)
1091 deprecated_set_value_type (struct value
*value
, struct type
*type
)
1097 value_offset (const struct value
*value
)
1099 return value
->offset
;
1102 set_value_offset (struct value
*value
, LONGEST offset
)
1104 value
->offset
= offset
;
1108 value_bitpos (const struct value
*value
)
1110 return value
->bitpos
;
1113 set_value_bitpos (struct value
*value
, LONGEST bit
)
1115 value
->bitpos
= bit
;
1119 value_bitsize (const struct value
*value
)
1121 return value
->bitsize
;
1124 set_value_bitsize (struct value
*value
, LONGEST bit
)
1126 value
->bitsize
= bit
;
1130 value_parent (const struct value
*value
)
1132 return value
->parent
.get ();
1138 set_value_parent (struct value
*value
, struct value
*parent
)
1140 value
->parent
= value_ref_ptr::new_reference (parent
);
1144 value_contents_raw (struct value
*value
)
1146 struct gdbarch
*arch
= get_value_arch (value
);
1147 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1149 allocate_value_contents (value
);
1150 return value
->contents
.get () + value
->embedded_offset
* unit_size
;
1154 value_contents_all_raw (struct value
*value
)
1156 allocate_value_contents (value
);
1157 return value
->contents
.get ();
1161 value_enclosing_type (const struct value
*value
)
1163 return value
->enclosing_type
;
1166 /* Look at value.h for description. */
1169 value_actual_type (struct value
*value
, int resolve_simple_types
,
1170 int *real_type_found
)
1172 struct value_print_options opts
;
1173 struct type
*result
;
1175 get_user_print_options (&opts
);
1177 if (real_type_found
)
1178 *real_type_found
= 0;
1179 result
= value_type (value
);
1180 if (opts
.objectprint
)
1182 /* If result's target type is TYPE_CODE_STRUCT, proceed to
1183 fetch its rtti type. */
1184 if ((result
->code () == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (result
))
1185 && (check_typedef (TYPE_TARGET_TYPE (result
))->code ()
1186 == TYPE_CODE_STRUCT
)
1187 && !value_optimized_out (value
))
1189 struct type
*real_type
;
1191 real_type
= value_rtti_indirect_type (value
, NULL
, NULL
, NULL
);
1194 if (real_type_found
)
1195 *real_type_found
= 1;
1199 else if (resolve_simple_types
)
1201 if (real_type_found
)
1202 *real_type_found
= 1;
1203 result
= value_enclosing_type (value
);
1211 error_value_optimized_out (void)
1213 error (_("value has been optimized out"));
1217 require_not_optimized_out (const struct value
*value
)
1219 if (!value
->optimized_out
.empty ())
1221 if (value
->lval
== lval_register
)
1222 error (_("register has not been saved in frame"));
1224 error_value_optimized_out ();
1229 require_available (const struct value
*value
)
1231 if (!value
->unavailable
.empty ())
1232 throw_error (NOT_AVAILABLE_ERROR
, _("value is not available"));
1236 value_contents_for_printing (struct value
*value
)
1239 value_fetch_lazy (value
);
1240 return value
->contents
.get ();
1244 value_contents_for_printing_const (const struct value
*value
)
1246 gdb_assert (!value
->lazy
);
1247 return value
->contents
.get ();
1251 value_contents_all (struct value
*value
)
1253 const gdb_byte
*result
= value_contents_for_printing (value
);
1254 require_not_optimized_out (value
);
1255 require_available (value
);
1259 /* Copy ranges in SRC_RANGE that overlap [SRC_BIT_OFFSET,
1260 SRC_BIT_OFFSET+BIT_LENGTH) ranges into *DST_RANGE, adjusted. */
1263 ranges_copy_adjusted (std::vector
<range
> *dst_range
, int dst_bit_offset
,
1264 const std::vector
<range
> &src_range
, int src_bit_offset
,
1267 for (const range
&r
: src_range
)
1271 l
= std::max (r
.offset
, (LONGEST
) src_bit_offset
);
1272 h
= std::min (r
.offset
+ r
.length
,
1273 (LONGEST
) src_bit_offset
+ bit_length
);
1276 insert_into_bit_range_vector (dst_range
,
1277 dst_bit_offset
+ (l
- src_bit_offset
),
1282 /* Copy the ranges metadata in SRC that overlaps [SRC_BIT_OFFSET,
1283 SRC_BIT_OFFSET+BIT_LENGTH) into DST, adjusted. */
1286 value_ranges_copy_adjusted (struct value
*dst
, int dst_bit_offset
,
1287 const struct value
*src
, int src_bit_offset
,
1290 ranges_copy_adjusted (&dst
->unavailable
, dst_bit_offset
,
1291 src
->unavailable
, src_bit_offset
,
1293 ranges_copy_adjusted (&dst
->optimized_out
, dst_bit_offset
,
1294 src
->optimized_out
, src_bit_offset
,
1298 /* Copy LENGTH target addressable memory units of SRC value's (all) contents
1299 (value_contents_all) starting at SRC_OFFSET, into DST value's (all)
1300 contents, starting at DST_OFFSET. If unavailable contents are
1301 being copied from SRC, the corresponding DST contents are marked
1302 unavailable accordingly. Neither DST nor SRC may be lazy
1305 It is assumed the contents of DST in the [DST_OFFSET,
1306 DST_OFFSET+LENGTH) range are wholly available. */
1309 value_contents_copy_raw (struct value
*dst
, LONGEST dst_offset
,
1310 struct value
*src
, LONGEST src_offset
, LONGEST length
)
1312 LONGEST src_bit_offset
, dst_bit_offset
, bit_length
;
1313 struct gdbarch
*arch
= get_value_arch (src
);
1314 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1316 /* A lazy DST would make that this copy operation useless, since as
1317 soon as DST's contents were un-lazied (by a later value_contents
1318 call, say), the contents would be overwritten. A lazy SRC would
1319 mean we'd be copying garbage. */
1320 gdb_assert (!dst
->lazy
&& !src
->lazy
);
1322 /* The overwritten DST range gets unavailability ORed in, not
1323 replaced. Make sure to remember to implement replacing if it
1324 turns out actually necessary. */
1325 gdb_assert (value_bytes_available (dst
, dst_offset
, length
));
1326 gdb_assert (!value_bits_any_optimized_out (dst
,
1327 TARGET_CHAR_BIT
* dst_offset
,
1328 TARGET_CHAR_BIT
* length
));
1330 /* Copy the data. */
1331 memcpy (value_contents_all_raw (dst
) + dst_offset
* unit_size
,
1332 value_contents_all_raw (src
) + src_offset
* unit_size
,
1333 length
* unit_size
);
1335 /* Copy the meta-data, adjusted. */
1336 src_bit_offset
= src_offset
* unit_size
* HOST_CHAR_BIT
;
1337 dst_bit_offset
= dst_offset
* unit_size
* HOST_CHAR_BIT
;
1338 bit_length
= length
* unit_size
* HOST_CHAR_BIT
;
1340 value_ranges_copy_adjusted (dst
, dst_bit_offset
,
1341 src
, src_bit_offset
,
1345 /* Copy LENGTH bytes of SRC value's (all) contents
1346 (value_contents_all) starting at SRC_OFFSET byte, into DST value's
1347 (all) contents, starting at DST_OFFSET. If unavailable contents
1348 are being copied from SRC, the corresponding DST contents are
1349 marked unavailable accordingly. DST must not be lazy. If SRC is
1350 lazy, it will be fetched now.
1352 It is assumed the contents of DST in the [DST_OFFSET,
1353 DST_OFFSET+LENGTH) range are wholly available. */
1356 value_contents_copy (struct value
*dst
, LONGEST dst_offset
,
1357 struct value
*src
, LONGEST src_offset
, LONGEST length
)
1360 value_fetch_lazy (src
);
1362 value_contents_copy_raw (dst
, dst_offset
, src
, src_offset
, length
);
1366 value_lazy (const struct value
*value
)
1372 set_value_lazy (struct value
*value
, int val
)
1378 value_stack (const struct value
*value
)
1380 return value
->stack
;
1384 set_value_stack (struct value
*value
, int val
)
1390 value_contents (struct value
*value
)
1392 const gdb_byte
*result
= value_contents_writeable (value
);
1393 require_not_optimized_out (value
);
1394 require_available (value
);
1399 value_contents_writeable (struct value
*value
)
1402 value_fetch_lazy (value
);
1403 return value_contents_raw (value
);
1407 value_optimized_out (struct value
*value
)
1409 /* We can only know if a value is optimized out once we have tried to
1411 if (value
->optimized_out
.empty () && value
->lazy
)
1415 value_fetch_lazy (value
);
1417 catch (const gdb_exception_error
&ex
)
1422 case OPTIMIZED_OUT_ERROR
:
1423 case NOT_AVAILABLE_ERROR
:
1424 /* These can normally happen when we try to access an
1425 optimized out or unavailable register, either in a
1426 physical register or spilled to memory. */
1434 return !value
->optimized_out
.empty ();
1437 /* Mark contents of VALUE as optimized out, starting at OFFSET bytes, and
1438 the following LENGTH bytes. */
1441 mark_value_bytes_optimized_out (struct value
*value
, int offset
, int length
)
1443 mark_value_bits_optimized_out (value
,
1444 offset
* TARGET_CHAR_BIT
,
1445 length
* TARGET_CHAR_BIT
);
1451 mark_value_bits_optimized_out (struct value
*value
,
1452 LONGEST offset
, LONGEST length
)
1454 insert_into_bit_range_vector (&value
->optimized_out
, offset
, length
);
1458 value_bits_synthetic_pointer (const struct value
*value
,
1459 LONGEST offset
, LONGEST length
)
1461 if (value
->lval
!= lval_computed
1462 || !value
->location
.computed
.funcs
->check_synthetic_pointer
)
1464 return value
->location
.computed
.funcs
->check_synthetic_pointer (value
,
1470 value_embedded_offset (const struct value
*value
)
1472 return value
->embedded_offset
;
1476 set_value_embedded_offset (struct value
*value
, LONGEST val
)
1478 value
->embedded_offset
= val
;
1482 value_pointed_to_offset (const struct value
*value
)
1484 return value
->pointed_to_offset
;
1488 set_value_pointed_to_offset (struct value
*value
, LONGEST val
)
1490 value
->pointed_to_offset
= val
;
1493 const struct lval_funcs
*
1494 value_computed_funcs (const struct value
*v
)
1496 gdb_assert (value_lval_const (v
) == lval_computed
);
1498 return v
->location
.computed
.funcs
;
1502 value_computed_closure (const struct value
*v
)
1504 gdb_assert (v
->lval
== lval_computed
);
1506 return v
->location
.computed
.closure
;
1510 deprecated_value_lval_hack (struct value
*value
)
1512 return &value
->lval
;
1516 value_lval_const (const struct value
*value
)
1522 value_address (const struct value
*value
)
1524 if (value
->lval
!= lval_memory
)
1526 if (value
->parent
!= NULL
)
1527 return value_address (value
->parent
.get ()) + value
->offset
;
1528 if (NULL
!= TYPE_DATA_LOCATION (value_type (value
)))
1530 gdb_assert (PROP_CONST
== TYPE_DATA_LOCATION_KIND (value_type (value
)));
1531 return TYPE_DATA_LOCATION_ADDR (value_type (value
));
1534 return value
->location
.address
+ value
->offset
;
1538 value_raw_address (const struct value
*value
)
1540 if (value
->lval
!= lval_memory
)
1542 return value
->location
.address
;
1546 set_value_address (struct value
*value
, CORE_ADDR addr
)
1548 gdb_assert (value
->lval
== lval_memory
);
1549 value
->location
.address
= addr
;
1552 struct internalvar
**
1553 deprecated_value_internalvar_hack (struct value
*value
)
1555 return &value
->location
.internalvar
;
1559 deprecated_value_next_frame_id_hack (struct value
*value
)
1561 gdb_assert (value
->lval
== lval_register
);
1562 return &value
->location
.reg
.next_frame_id
;
1566 deprecated_value_regnum_hack (struct value
*value
)
1568 gdb_assert (value
->lval
== lval_register
);
1569 return &value
->location
.reg
.regnum
;
1573 deprecated_value_modifiable (const struct value
*value
)
1575 return value
->modifiable
;
1578 /* Return a mark in the value chain. All values allocated after the
1579 mark is obtained (except for those released) are subject to being freed
1580 if a subsequent value_free_to_mark is passed the mark. */
1584 if (all_values
.empty ())
1586 return all_values
.back ().get ();
1592 value_incref (struct value
*val
)
1594 val
->reference_count
++;
1597 /* Release a reference to VAL, which was acquired with value_incref.
1598 This function is also called to deallocate values from the value
1602 value_decref (struct value
*val
)
1606 gdb_assert (val
->reference_count
> 0);
1607 val
->reference_count
--;
1608 if (val
->reference_count
== 0)
1613 /* Free all values allocated since MARK was obtained by value_mark
1614 (except for those released). */
1616 value_free_to_mark (const struct value
*mark
)
1618 auto iter
= std::find (all_values
.begin (), all_values
.end (), mark
);
1619 if (iter
== all_values
.end ())
1620 all_values
.clear ();
1622 all_values
.erase (iter
+ 1, all_values
.end ());
1625 /* Remove VAL from the chain all_values
1626 so it will not be freed automatically. */
1629 release_value (struct value
*val
)
1632 return value_ref_ptr ();
1634 std::vector
<value_ref_ptr
>::reverse_iterator iter
;
1635 for (iter
= all_values
.rbegin (); iter
!= all_values
.rend (); ++iter
)
1639 value_ref_ptr result
= *iter
;
1640 all_values
.erase (iter
.base () - 1);
1645 /* We must always return an owned reference. Normally this happens
1646 because we transfer the reference from the value chain, but in
1647 this case the value was not on the chain. */
1648 return value_ref_ptr::new_reference (val
);
1653 std::vector
<value_ref_ptr
>
1654 value_release_to_mark (const struct value
*mark
)
1656 std::vector
<value_ref_ptr
> result
;
1658 auto iter
= std::find (all_values
.begin (), all_values
.end (), mark
);
1659 if (iter
== all_values
.end ())
1660 std::swap (result
, all_values
);
1663 std::move (iter
+ 1, all_values
.end (), std::back_inserter (result
));
1664 all_values
.erase (iter
+ 1, all_values
.end ());
1666 std::reverse (result
.begin (), result
.end ());
1670 /* Return a copy of the value ARG.
1671 It contains the same contents, for same memory address,
1672 but it's a different block of storage. */
1675 value_copy (struct value
*arg
)
1677 struct type
*encl_type
= value_enclosing_type (arg
);
1680 if (value_lazy (arg
))
1681 val
= allocate_value_lazy (encl_type
);
1683 val
= allocate_value (encl_type
);
1684 val
->type
= arg
->type
;
1685 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
1686 val
->location
= arg
->location
;
1687 val
->offset
= arg
->offset
;
1688 val
->bitpos
= arg
->bitpos
;
1689 val
->bitsize
= arg
->bitsize
;
1690 val
->lazy
= arg
->lazy
;
1691 val
->embedded_offset
= value_embedded_offset (arg
);
1692 val
->pointed_to_offset
= arg
->pointed_to_offset
;
1693 val
->modifiable
= arg
->modifiable
;
1694 if (!value_lazy (val
))
1696 memcpy (value_contents_all_raw (val
), value_contents_all_raw (arg
),
1697 TYPE_LENGTH (value_enclosing_type (arg
)));
1700 val
->unavailable
= arg
->unavailable
;
1701 val
->optimized_out
= arg
->optimized_out
;
1702 val
->parent
= arg
->parent
;
1703 if (VALUE_LVAL (val
) == lval_computed
)
1705 const struct lval_funcs
*funcs
= val
->location
.computed
.funcs
;
1707 if (funcs
->copy_closure
)
1708 val
->location
.computed
.closure
= funcs
->copy_closure (val
);
1713 /* Return a "const" and/or "volatile" qualified version of the value V.
1714 If CNST is true, then the returned value will be qualified with
1716 if VOLTL is true, then the returned value will be qualified with
1720 make_cv_value (int cnst
, int voltl
, struct value
*v
)
1722 struct type
*val_type
= value_type (v
);
1723 struct type
*enclosing_type
= value_enclosing_type (v
);
1724 struct value
*cv_val
= value_copy (v
);
1726 deprecated_set_value_type (cv_val
,
1727 make_cv_type (cnst
, voltl
, val_type
, NULL
));
1728 set_value_enclosing_type (cv_val
,
1729 make_cv_type (cnst
, voltl
, enclosing_type
, NULL
));
1734 /* Return a version of ARG that is non-lvalue. */
1737 value_non_lval (struct value
*arg
)
1739 if (VALUE_LVAL (arg
) != not_lval
)
1741 struct type
*enc_type
= value_enclosing_type (arg
);
1742 struct value
*val
= allocate_value (enc_type
);
1744 memcpy (value_contents_all_raw (val
), value_contents_all (arg
),
1745 TYPE_LENGTH (enc_type
));
1746 val
->type
= arg
->type
;
1747 set_value_embedded_offset (val
, value_embedded_offset (arg
));
1748 set_value_pointed_to_offset (val
, value_pointed_to_offset (arg
));
1754 /* Write contents of V at ADDR and set its lval type to be LVAL_MEMORY. */
1757 value_force_lval (struct value
*v
, CORE_ADDR addr
)
1759 gdb_assert (VALUE_LVAL (v
) == not_lval
);
1761 write_memory (addr
, value_contents_raw (v
), TYPE_LENGTH (value_type (v
)));
1762 v
->lval
= lval_memory
;
1763 v
->location
.address
= addr
;
1767 set_value_component_location (struct value
*component
,
1768 const struct value
*whole
)
1772 gdb_assert (whole
->lval
!= lval_xcallable
);
1774 if (whole
->lval
== lval_internalvar
)
1775 VALUE_LVAL (component
) = lval_internalvar_component
;
1777 VALUE_LVAL (component
) = whole
->lval
;
1779 component
->location
= whole
->location
;
1780 if (whole
->lval
== lval_computed
)
1782 const struct lval_funcs
*funcs
= whole
->location
.computed
.funcs
;
1784 if (funcs
->copy_closure
)
1785 component
->location
.computed
.closure
= funcs
->copy_closure (whole
);
1788 /* If the WHOLE value has a dynamically resolved location property then
1789 update the address of the COMPONENT. */
1790 type
= value_type (whole
);
1791 if (NULL
!= TYPE_DATA_LOCATION (type
)
1792 && TYPE_DATA_LOCATION_KIND (type
) == PROP_CONST
)
1793 set_value_address (component
, TYPE_DATA_LOCATION_ADDR (type
));
1795 /* Similarly, if the COMPONENT value has a dynamically resolved location
1796 property then update its address. */
1797 type
= value_type (component
);
1798 if (NULL
!= TYPE_DATA_LOCATION (type
)
1799 && TYPE_DATA_LOCATION_KIND (type
) == PROP_CONST
)
1801 /* If the COMPONENT has a dynamic location, and is an
1802 lval_internalvar_component, then we change it to a lval_memory.
1804 Usually a component of an internalvar is created non-lazy, and has
1805 its content immediately copied from the parent internalvar.
1806 However, for components with a dynamic location, the content of
1807 the component is not contained within the parent, but is instead
1808 accessed indirectly. Further, the component will be created as a
1811 By changing the type of the component to lval_memory we ensure
1812 that value_fetch_lazy can successfully load the component.
1814 This solution isn't ideal, but a real fix would require values to
1815 carry around both the parent value contents, and the contents of
1816 any dynamic fields within the parent. This is a substantial
1817 change to how values work in GDB. */
1818 if (VALUE_LVAL (component
) == lval_internalvar_component
)
1820 gdb_assert (value_lazy (component
));
1821 VALUE_LVAL (component
) = lval_memory
;
1824 gdb_assert (VALUE_LVAL (component
) == lval_memory
);
1825 set_value_address (component
, TYPE_DATA_LOCATION_ADDR (type
));
1829 /* Access to the value history. */
1831 /* Record a new value in the value history.
1832 Returns the absolute history index of the entry. */
1835 record_latest_value (struct value
*val
)
1837 /* We don't want this value to have anything to do with the inferior anymore.
1838 In particular, "set $1 = 50" should not affect the variable from which
1839 the value was taken, and fast watchpoints should be able to assume that
1840 a value on the value history never changes. */
1841 if (value_lazy (val
))
1842 value_fetch_lazy (val
);
1843 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
1844 from. This is a bit dubious, because then *&$1 does not just return $1
1845 but the current contents of that location. c'est la vie... */
1846 val
->modifiable
= 0;
1848 value_history
.push_back (release_value (val
));
1850 return value_history
.size ();
1853 /* Return a copy of the value in the history with sequence number NUM. */
1856 access_value_history (int num
)
1861 absnum
+= value_history
.size ();
1866 error (_("The history is empty."));
1868 error (_("There is only one value in the history."));
1870 error (_("History does not go back to $$%d."), -num
);
1872 if (absnum
> value_history
.size ())
1873 error (_("History has not yet reached $%d."), absnum
);
1877 return value_copy (value_history
[absnum
].get ());
1881 show_values (const char *num_exp
, int from_tty
)
1889 /* "show values +" should print from the stored position.
1890 "show values <exp>" should print around value number <exp>. */
1891 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
1892 num
= parse_and_eval_long (num_exp
) - 5;
1896 /* "show values" means print the last 10 values. */
1897 num
= value_history
.size () - 9;
1903 for (i
= num
; i
< num
+ 10 && i
<= value_history
.size (); i
++)
1905 struct value_print_options opts
;
1907 val
= access_value_history (i
);
1908 printf_filtered (("$%d = "), i
);
1909 get_user_print_options (&opts
);
1910 value_print (val
, gdb_stdout
, &opts
);
1911 printf_filtered (("\n"));
1914 /* The next "show values +" should start after what we just printed. */
1917 /* Hitting just return after this command should do the same thing as
1918 "show values +". If num_exp is null, this is unnecessary, since
1919 "show values +" is not useful after "show values". */
1920 if (from_tty
&& num_exp
)
1921 set_repeat_arguments ("+");
1924 enum internalvar_kind
1926 /* The internal variable is empty. */
1929 /* The value of the internal variable is provided directly as
1930 a GDB value object. */
1933 /* A fresh value is computed via a call-back routine on every
1934 access to the internal variable. */
1935 INTERNALVAR_MAKE_VALUE
,
1937 /* The internal variable holds a GDB internal convenience function. */
1938 INTERNALVAR_FUNCTION
,
1940 /* The variable holds an integer value. */
1941 INTERNALVAR_INTEGER
,
1943 /* The variable holds a GDB-provided string. */
1947 union internalvar_data
1949 /* A value object used with INTERNALVAR_VALUE. */
1950 struct value
*value
;
1952 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
1955 /* The functions to call. */
1956 const struct internalvar_funcs
*functions
;
1958 /* The function's user-data. */
1962 /* The internal function used with INTERNALVAR_FUNCTION. */
1965 struct internal_function
*function
;
1966 /* True if this is the canonical name for the function. */
1970 /* An integer value used with INTERNALVAR_INTEGER. */
1973 /* If type is non-NULL, it will be used as the type to generate
1974 a value for this internal variable. If type is NULL, a default
1975 integer type for the architecture is used. */
1980 /* A string value used with INTERNALVAR_STRING. */
1984 /* Internal variables. These are variables within the debugger
1985 that hold values assigned by debugger commands.
1986 The user refers to them with a '$' prefix
1987 that does not appear in the variable names stored internally. */
1991 struct internalvar
*next
;
1994 /* We support various different kinds of content of an internal variable.
1995 enum internalvar_kind specifies the kind, and union internalvar_data
1996 provides the data associated with this particular kind. */
1998 enum internalvar_kind kind
;
2000 union internalvar_data u
;
2003 static struct internalvar
*internalvars
;
2005 /* If the variable does not already exist create it and give it the
2006 value given. If no value is given then the default is zero. */
2008 init_if_undefined_command (const char* args
, int from_tty
)
2010 struct internalvar
*intvar
= nullptr;
2012 /* Parse the expression - this is taken from set_command(). */
2013 expression_up expr
= parse_expression (args
);
2015 /* Validate the expression.
2016 Was the expression an assignment?
2017 Or even an expression at all? */
2018 if ((expr
->nelts
== 0 && expr
->op
== nullptr)
2019 || expr
->first_opcode () != BINOP_ASSIGN
)
2020 error (_("Init-if-undefined requires an assignment expression."));
2022 /* Extract the variable from the parsed expression.
2023 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
2024 if (expr
->op
== nullptr)
2026 if (expr
->elts
[1].opcode
== OP_INTERNALVAR
)
2027 intvar
= expr
->elts
[2].internalvar
;
2031 expr::assign_operation
*assign
2032 = dynamic_cast<expr::assign_operation
*> (expr
->op
.get ());
2033 if (assign
!= nullptr)
2035 expr::operation
*lhs
= assign
->get_lhs ();
2036 expr::internalvar_operation
*ivarop
2037 = dynamic_cast<expr::internalvar_operation
*> (lhs
);
2038 if (ivarop
!= nullptr)
2039 intvar
= ivarop
->get_internalvar ();
2043 if (intvar
== nullptr)
2044 error (_("The first parameter to init-if-undefined "
2045 "should be a GDB variable."));
2047 /* Only evaluate the expression if the lvalue is void.
2048 This may still fail if the expression is invalid. */
2049 if (intvar
->kind
== INTERNALVAR_VOID
)
2050 evaluate_expression (expr
.get ());
2054 /* Look up an internal variable with name NAME. NAME should not
2055 normally include a dollar sign.
2057 If the specified internal variable does not exist,
2058 the return value is NULL. */
2060 struct internalvar
*
2061 lookup_only_internalvar (const char *name
)
2063 struct internalvar
*var
;
2065 for (var
= internalvars
; var
; var
= var
->next
)
2066 if (strcmp (var
->name
, name
) == 0)
2072 /* Complete NAME by comparing it to the names of internal
2076 complete_internalvar (completion_tracker
&tracker
, const char *name
)
2078 struct internalvar
*var
;
2081 len
= strlen (name
);
2083 for (var
= internalvars
; var
; var
= var
->next
)
2084 if (strncmp (var
->name
, name
, len
) == 0)
2085 tracker
.add_completion (make_unique_xstrdup (var
->name
));
2088 /* Create an internal variable with name NAME and with a void value.
2089 NAME should not normally include a dollar sign. */
2091 struct internalvar
*
2092 create_internalvar (const char *name
)
2094 struct internalvar
*var
= XNEW (struct internalvar
);
2096 var
->name
= xstrdup (name
);
2097 var
->kind
= INTERNALVAR_VOID
;
2098 var
->next
= internalvars
;
2103 /* Create an internal variable with name NAME and register FUN as the
2104 function that value_of_internalvar uses to create a value whenever
2105 this variable is referenced. NAME should not normally include a
2106 dollar sign. DATA is passed uninterpreted to FUN when it is
2107 called. CLEANUP, if not NULL, is called when the internal variable
2108 is destroyed. It is passed DATA as its only argument. */
2110 struct internalvar
*
2111 create_internalvar_type_lazy (const char *name
,
2112 const struct internalvar_funcs
*funcs
,
2115 struct internalvar
*var
= create_internalvar (name
);
2117 var
->kind
= INTERNALVAR_MAKE_VALUE
;
2118 var
->u
.make_value
.functions
= funcs
;
2119 var
->u
.make_value
.data
= data
;
2123 /* See documentation in value.h. */
2126 compile_internalvar_to_ax (struct internalvar
*var
,
2127 struct agent_expr
*expr
,
2128 struct axs_value
*value
)
2130 if (var
->kind
!= INTERNALVAR_MAKE_VALUE
2131 || var
->u
.make_value
.functions
->compile_to_ax
== NULL
)
2134 var
->u
.make_value
.functions
->compile_to_ax (var
, expr
, value
,
2135 var
->u
.make_value
.data
);
2139 /* Look up an internal variable with name NAME. NAME should not
2140 normally include a dollar sign.
2142 If the specified internal variable does not exist,
2143 one is created, with a void value. */
2145 struct internalvar
*
2146 lookup_internalvar (const char *name
)
2148 struct internalvar
*var
;
2150 var
= lookup_only_internalvar (name
);
2154 return create_internalvar (name
);
2157 /* Return current value of internal variable VAR. For variables that
2158 are not inherently typed, use a value type appropriate for GDBARCH. */
2161 value_of_internalvar (struct gdbarch
*gdbarch
, struct internalvar
*var
)
2164 struct trace_state_variable
*tsv
;
2166 /* If there is a trace state variable of the same name, assume that
2167 is what we really want to see. */
2168 tsv
= find_trace_state_variable (var
->name
);
2171 tsv
->value_known
= target_get_trace_state_variable_value (tsv
->number
,
2173 if (tsv
->value_known
)
2174 val
= value_from_longest (builtin_type (gdbarch
)->builtin_int64
,
2177 val
= allocate_value (builtin_type (gdbarch
)->builtin_void
);
2183 case INTERNALVAR_VOID
:
2184 val
= allocate_value (builtin_type (gdbarch
)->builtin_void
);
2187 case INTERNALVAR_FUNCTION
:
2188 val
= allocate_value (builtin_type (gdbarch
)->internal_fn
);
2191 case INTERNALVAR_INTEGER
:
2192 if (!var
->u
.integer
.type
)
2193 val
= value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2194 var
->u
.integer
.val
);
2196 val
= value_from_longest (var
->u
.integer
.type
, var
->u
.integer
.val
);
2199 case INTERNALVAR_STRING
:
2200 val
= value_cstring (var
->u
.string
, strlen (var
->u
.string
),
2201 builtin_type (gdbarch
)->builtin_char
);
2204 case INTERNALVAR_VALUE
:
2205 val
= value_copy (var
->u
.value
);
2206 if (value_lazy (val
))
2207 value_fetch_lazy (val
);
2210 case INTERNALVAR_MAKE_VALUE
:
2211 val
= (*var
->u
.make_value
.functions
->make_value
) (gdbarch
, var
,
2212 var
->u
.make_value
.data
);
2216 internal_error (__FILE__
, __LINE__
, _("bad kind"));
2219 /* Change the VALUE_LVAL to lval_internalvar so that future operations
2220 on this value go back to affect the original internal variable.
2222 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
2223 no underlying modifiable state in the internal variable.
2225 Likewise, if the variable's value is a computed lvalue, we want
2226 references to it to produce another computed lvalue, where
2227 references and assignments actually operate through the
2228 computed value's functions.
2230 This means that internal variables with computed values
2231 behave a little differently from other internal variables:
2232 assignments to them don't just replace the previous value
2233 altogether. At the moment, this seems like the behavior we
2236 if (var
->kind
!= INTERNALVAR_MAKE_VALUE
2237 && val
->lval
!= lval_computed
)
2239 VALUE_LVAL (val
) = lval_internalvar
;
2240 VALUE_INTERNALVAR (val
) = var
;
2247 get_internalvar_integer (struct internalvar
*var
, LONGEST
*result
)
2249 if (var
->kind
== INTERNALVAR_INTEGER
)
2251 *result
= var
->u
.integer
.val
;
2255 if (var
->kind
== INTERNALVAR_VALUE
)
2257 struct type
*type
= check_typedef (value_type (var
->u
.value
));
2259 if (type
->code () == TYPE_CODE_INT
)
2261 *result
= value_as_long (var
->u
.value
);
2270 get_internalvar_function (struct internalvar
*var
,
2271 struct internal_function
**result
)
2275 case INTERNALVAR_FUNCTION
:
2276 *result
= var
->u
.fn
.function
;
2285 set_internalvar_component (struct internalvar
*var
,
2286 LONGEST offset
, LONGEST bitpos
,
2287 LONGEST bitsize
, struct value
*newval
)
2290 struct gdbarch
*arch
;
2295 case INTERNALVAR_VALUE
:
2296 addr
= value_contents_writeable (var
->u
.value
);
2297 arch
= get_value_arch (var
->u
.value
);
2298 unit_size
= gdbarch_addressable_memory_unit_size (arch
);
2301 modify_field (value_type (var
->u
.value
), addr
+ offset
,
2302 value_as_long (newval
), bitpos
, bitsize
);
2304 memcpy (addr
+ offset
* unit_size
, value_contents (newval
),
2305 TYPE_LENGTH (value_type (newval
)));
2309 /* We can never get a component of any other kind. */
2310 internal_error (__FILE__
, __LINE__
, _("set_internalvar_component"));
2315 set_internalvar (struct internalvar
*var
, struct value
*val
)
2317 enum internalvar_kind new_kind
;
2318 union internalvar_data new_data
= { 0 };
2320 if (var
->kind
== INTERNALVAR_FUNCTION
&& var
->u
.fn
.canonical
)
2321 error (_("Cannot overwrite convenience function %s"), var
->name
);
2323 /* Prepare new contents. */
2324 switch (check_typedef (value_type (val
))->code ())
2326 case TYPE_CODE_VOID
:
2327 new_kind
= INTERNALVAR_VOID
;
2330 case TYPE_CODE_INTERNAL_FUNCTION
:
2331 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
2332 new_kind
= INTERNALVAR_FUNCTION
;
2333 get_internalvar_function (VALUE_INTERNALVAR (val
),
2334 &new_data
.fn
.function
);
2335 /* Copies created here are never canonical. */
2339 new_kind
= INTERNALVAR_VALUE
;
2340 struct value
*copy
= value_copy (val
);
2341 copy
->modifiable
= 1;
2343 /* Force the value to be fetched from the target now, to avoid problems
2344 later when this internalvar is referenced and the target is gone or
2346 if (value_lazy (copy
))
2347 value_fetch_lazy (copy
);
2349 /* Release the value from the value chain to prevent it from being
2350 deleted by free_all_values. From here on this function should not
2351 call error () until new_data is installed into the var->u to avoid
2353 new_data
.value
= release_value (copy
).release ();
2355 /* Internal variables which are created from values with a dynamic
2356 location don't need the location property of the origin anymore.
2357 The resolved dynamic location is used prior then any other address
2358 when accessing the value.
2359 If we keep it, we would still refer to the origin value.
2360 Remove the location property in case it exist. */
2361 value_type (new_data
.value
)->remove_dyn_prop (DYN_PROP_DATA_LOCATION
);
2366 /* Clean up old contents. */
2367 clear_internalvar (var
);
2370 var
->kind
= new_kind
;
2372 /* End code which must not call error(). */
2376 set_internalvar_integer (struct internalvar
*var
, LONGEST l
)
2378 /* Clean up old contents. */
2379 clear_internalvar (var
);
2381 var
->kind
= INTERNALVAR_INTEGER
;
2382 var
->u
.integer
.type
= NULL
;
2383 var
->u
.integer
.val
= l
;
2387 set_internalvar_string (struct internalvar
*var
, const char *string
)
2389 /* Clean up old contents. */
2390 clear_internalvar (var
);
2392 var
->kind
= INTERNALVAR_STRING
;
2393 var
->u
.string
= xstrdup (string
);
2397 set_internalvar_function (struct internalvar
*var
, struct internal_function
*f
)
2399 /* Clean up old contents. */
2400 clear_internalvar (var
);
2402 var
->kind
= INTERNALVAR_FUNCTION
;
2403 var
->u
.fn
.function
= f
;
2404 var
->u
.fn
.canonical
= 1;
2405 /* Variables installed here are always the canonical version. */
2409 clear_internalvar (struct internalvar
*var
)
2411 /* Clean up old contents. */
2414 case INTERNALVAR_VALUE
:
2415 value_decref (var
->u
.value
);
2418 case INTERNALVAR_STRING
:
2419 xfree (var
->u
.string
);
2422 case INTERNALVAR_MAKE_VALUE
:
2423 if (var
->u
.make_value
.functions
->destroy
!= NULL
)
2424 var
->u
.make_value
.functions
->destroy (var
->u
.make_value
.data
);
2431 /* Reset to void kind. */
2432 var
->kind
= INTERNALVAR_VOID
;
2436 internalvar_name (const struct internalvar
*var
)
2441 static struct internal_function
*
2442 create_internal_function (const char *name
,
2443 internal_function_fn handler
, void *cookie
)
2445 struct internal_function
*ifn
= XNEW (struct internal_function
);
2447 ifn
->name
= xstrdup (name
);
2448 ifn
->handler
= handler
;
2449 ifn
->cookie
= cookie
;
2454 value_internal_function_name (struct value
*val
)
2456 struct internal_function
*ifn
;
2459 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
2460 result
= get_internalvar_function (VALUE_INTERNALVAR (val
), &ifn
);
2461 gdb_assert (result
);
2467 call_internal_function (struct gdbarch
*gdbarch
,
2468 const struct language_defn
*language
,
2469 struct value
*func
, int argc
, struct value
**argv
)
2471 struct internal_function
*ifn
;
2474 gdb_assert (VALUE_LVAL (func
) == lval_internalvar
);
2475 result
= get_internalvar_function (VALUE_INTERNALVAR (func
), &ifn
);
2476 gdb_assert (result
);
2478 return (*ifn
->handler
) (gdbarch
, language
, ifn
->cookie
, argc
, argv
);
2481 /* The 'function' command. This does nothing -- it is just a
2482 placeholder to let "help function NAME" work. This is also used as
2483 the implementation of the sub-command that is created when
2484 registering an internal function. */
2486 function_command (const char *command
, int from_tty
)
2491 /* Helper function that does the work for add_internal_function. */
2493 static struct cmd_list_element
*
2494 do_add_internal_function (const char *name
, const char *doc
,
2495 internal_function_fn handler
, void *cookie
)
2497 struct internal_function
*ifn
;
2498 struct internalvar
*var
= lookup_internalvar (name
);
2500 ifn
= create_internal_function (name
, handler
, cookie
);
2501 set_internalvar_function (var
, ifn
);
2503 return add_cmd (name
, no_class
, function_command
, doc
, &functionlist
);
2509 add_internal_function (const char *name
, const char *doc
,
2510 internal_function_fn handler
, void *cookie
)
2512 do_add_internal_function (name
, doc
, handler
, cookie
);
2518 add_internal_function (gdb::unique_xmalloc_ptr
<char> &&name
,
2519 gdb::unique_xmalloc_ptr
<char> &&doc
,
2520 internal_function_fn handler
, void *cookie
)
2522 struct cmd_list_element
*cmd
2523 = do_add_internal_function (name
.get (), doc
.get (), handler
, cookie
);
2525 cmd
->doc_allocated
= 1;
2527 cmd
->name_allocated
= 1;
2530 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
2531 prevent cycles / duplicates. */
2534 preserve_one_value (struct value
*value
, struct objfile
*objfile
,
2535 htab_t copied_types
)
2537 if (value
->type
->objfile_owner () == objfile
)
2538 value
->type
= copy_type_recursive (objfile
, value
->type
, copied_types
);
2540 if (value
->enclosing_type
->objfile_owner () == objfile
)
2541 value
->enclosing_type
= copy_type_recursive (objfile
,
2542 value
->enclosing_type
,
2546 /* Likewise for internal variable VAR. */
2549 preserve_one_internalvar (struct internalvar
*var
, struct objfile
*objfile
,
2550 htab_t copied_types
)
2554 case INTERNALVAR_INTEGER
:
2555 if (var
->u
.integer
.type
2556 && var
->u
.integer
.type
->objfile_owner () == objfile
)
2558 = copy_type_recursive (objfile
, var
->u
.integer
.type
, copied_types
);
2561 case INTERNALVAR_VALUE
:
2562 preserve_one_value (var
->u
.value
, objfile
, copied_types
);
2567 /* Update the internal variables and value history when OBJFILE is
2568 discarded; we must copy the types out of the objfile. New global types
2569 will be created for every convenience variable which currently points to
2570 this objfile's types, and the convenience variables will be adjusted to
2571 use the new global types. */
2574 preserve_values (struct objfile
*objfile
)
2576 struct internalvar
*var
;
2578 /* Create the hash table. We allocate on the objfile's obstack, since
2579 it is soon to be deleted. */
2580 htab_up copied_types
= create_copied_types_hash (objfile
);
2582 for (const value_ref_ptr
&item
: value_history
)
2583 preserve_one_value (item
.get (), objfile
, copied_types
.get ());
2585 for (var
= internalvars
; var
; var
= var
->next
)
2586 preserve_one_internalvar (var
, objfile
, copied_types
.get ());
2588 preserve_ext_lang_values (objfile
, copied_types
.get ());
2592 show_convenience (const char *ignore
, int from_tty
)
2594 struct gdbarch
*gdbarch
= get_current_arch ();
2595 struct internalvar
*var
;
2597 struct value_print_options opts
;
2599 get_user_print_options (&opts
);
2600 for (var
= internalvars
; var
; var
= var
->next
)
2607 printf_filtered (("$%s = "), var
->name
);
2613 val
= value_of_internalvar (gdbarch
, var
);
2614 value_print (val
, gdb_stdout
, &opts
);
2616 catch (const gdb_exception_error
&ex
)
2618 fprintf_styled (gdb_stdout
, metadata_style
.style (),
2619 _("<error: %s>"), ex
.what ());
2622 printf_filtered (("\n"));
2626 /* This text does not mention convenience functions on purpose.
2627 The user can't create them except via Python, and if Python support
2628 is installed this message will never be printed ($_streq will
2630 printf_unfiltered (_("No debugger convenience variables now defined.\n"
2631 "Convenience variables have "
2632 "names starting with \"$\";\n"
2633 "use \"set\" as in \"set "
2634 "$foo = 5\" to define them.\n"));
2642 value_from_xmethod (xmethod_worker_up
&&worker
)
2646 v
= allocate_value (builtin_type (target_gdbarch ())->xmethod
);
2647 v
->lval
= lval_xcallable
;
2648 v
->location
.xm_worker
= worker
.release ();
2654 /* Return the type of the result of TYPE_CODE_XMETHOD value METHOD. */
2657 result_type_of_xmethod (struct value
*method
, gdb::array_view
<value
*> argv
)
2659 gdb_assert (value_type (method
)->code () == TYPE_CODE_XMETHOD
2660 && method
->lval
== lval_xcallable
&& !argv
.empty ());
2662 return method
->location
.xm_worker
->get_result_type (argv
[0], argv
.slice (1));
2665 /* Call the xmethod corresponding to the TYPE_CODE_XMETHOD value METHOD. */
2668 call_xmethod (struct value
*method
, gdb::array_view
<value
*> argv
)
2670 gdb_assert (value_type (method
)->code () == TYPE_CODE_XMETHOD
2671 && method
->lval
== lval_xcallable
&& !argv
.empty ());
2673 return method
->location
.xm_worker
->invoke (argv
[0], argv
.slice (1));
2676 /* Extract a value as a C number (either long or double).
2677 Knows how to convert fixed values to double, or
2678 floating values to long.
2679 Does not deallocate the value. */
2682 value_as_long (struct value
*val
)
2684 /* This coerces arrays and functions, which is necessary (e.g.
2685 in disassemble_command). It also dereferences references, which
2686 I suspect is the most logical thing to do. */
2687 val
= coerce_array (val
);
2688 return unpack_long (value_type (val
), value_contents (val
));
2691 /* Extract a value as a C pointer. Does not deallocate the value.
2692 Note that val's type may not actually be a pointer; value_as_long
2693 handles all the cases. */
2695 value_as_address (struct value
*val
)
2697 struct gdbarch
*gdbarch
= value_type (val
)->arch ();
2699 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2700 whether we want this to be true eventually. */
2702 /* gdbarch_addr_bits_remove is wrong if we are being called for a
2703 non-address (e.g. argument to "signal", "info break", etc.), or
2704 for pointers to char, in which the low bits *are* significant. */
2705 return gdbarch_addr_bits_remove (gdbarch
, value_as_long (val
));
2708 /* There are several targets (IA-64, PowerPC, and others) which
2709 don't represent pointers to functions as simply the address of
2710 the function's entry point. For example, on the IA-64, a
2711 function pointer points to a two-word descriptor, generated by
2712 the linker, which contains the function's entry point, and the
2713 value the IA-64 "global pointer" register should have --- to
2714 support position-independent code. The linker generates
2715 descriptors only for those functions whose addresses are taken.
2717 On such targets, it's difficult for GDB to convert an arbitrary
2718 function address into a function pointer; it has to either find
2719 an existing descriptor for that function, or call malloc and
2720 build its own. On some targets, it is impossible for GDB to
2721 build a descriptor at all: the descriptor must contain a jump
2722 instruction; data memory cannot be executed; and code memory
2725 Upon entry to this function, if VAL is a value of type `function'
2726 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
2727 value_address (val) is the address of the function. This is what
2728 you'll get if you evaluate an expression like `main'. The call
2729 to COERCE_ARRAY below actually does all the usual unary
2730 conversions, which includes converting values of type `function'
2731 to `pointer to function'. This is the challenging conversion
2732 discussed above. Then, `unpack_long' will convert that pointer
2733 back into an address.
2735 So, suppose the user types `disassemble foo' on an architecture
2736 with a strange function pointer representation, on which GDB
2737 cannot build its own descriptors, and suppose further that `foo'
2738 has no linker-built descriptor. The address->pointer conversion
2739 will signal an error and prevent the command from running, even
2740 though the next step would have been to convert the pointer
2741 directly back into the same address.
2743 The following shortcut avoids this whole mess. If VAL is a
2744 function, just return its address directly. */
2745 if (value_type (val
)->code () == TYPE_CODE_FUNC
2746 || value_type (val
)->code () == TYPE_CODE_METHOD
)
2747 return value_address (val
);
2749 val
= coerce_array (val
);
2751 /* Some architectures (e.g. Harvard), map instruction and data
2752 addresses onto a single large unified address space. For
2753 instance: An architecture may consider a large integer in the
2754 range 0x10000000 .. 0x1000ffff to already represent a data
2755 addresses (hence not need a pointer to address conversion) while
2756 a small integer would still need to be converted integer to
2757 pointer to address. Just assume such architectures handle all
2758 integer conversions in a single function. */
2762 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
2763 must admonish GDB hackers to make sure its behavior matches the
2764 compiler's, whenever possible.
2766 In general, I think GDB should evaluate expressions the same way
2767 the compiler does. When the user copies an expression out of
2768 their source code and hands it to a `print' command, they should
2769 get the same value the compiler would have computed. Any
2770 deviation from this rule can cause major confusion and annoyance,
2771 and needs to be justified carefully. In other words, GDB doesn't
2772 really have the freedom to do these conversions in clever and
2775 AndrewC pointed out that users aren't complaining about how GDB
2776 casts integers to pointers; they are complaining that they can't
2777 take an address from a disassembly listing and give it to `x/i'.
2778 This is certainly important.
2780 Adding an architecture method like integer_to_address() certainly
2781 makes it possible for GDB to "get it right" in all circumstances
2782 --- the target has complete control over how things get done, so
2783 people can Do The Right Thing for their target without breaking
2784 anyone else. The standard doesn't specify how integers get
2785 converted to pointers; usually, the ABI doesn't either, but
2786 ABI-specific code is a more reasonable place to handle it. */
2788 if (value_type (val
)->code () != TYPE_CODE_PTR
2789 && !TYPE_IS_REFERENCE (value_type (val
))
2790 && gdbarch_integer_to_address_p (gdbarch
))
2791 return gdbarch_integer_to_address (gdbarch
, value_type (val
),
2792 value_contents (val
));
2794 return unpack_long (value_type (val
), value_contents (val
));
2798 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2799 as a long, or as a double, assuming the raw data is described
2800 by type TYPE. Knows how to convert different sizes of values
2801 and can convert between fixed and floating point. We don't assume
2802 any alignment for the raw data. Return value is in host byte order.
2804 If you want functions and arrays to be coerced to pointers, and
2805 references to be dereferenced, call value_as_long() instead.
2807 C++: It is assumed that the front-end has taken care of
2808 all matters concerning pointers to members. A pointer
2809 to member which reaches here is considered to be equivalent
2810 to an INT (or some size). After all, it is only an offset. */
2813 unpack_long (struct type
*type
, const gdb_byte
*valaddr
)
2815 if (is_fixed_point_type (type
))
2816 type
= type
->fixed_point_type_base_type ();
2818 enum bfd_endian byte_order
= type_byte_order (type
);
2819 enum type_code code
= type
->code ();
2820 int len
= TYPE_LENGTH (type
);
2821 int nosign
= type
->is_unsigned ();
2825 case TYPE_CODE_TYPEDEF
:
2826 return unpack_long (check_typedef (type
), valaddr
);
2827 case TYPE_CODE_ENUM
:
2828 case TYPE_CODE_FLAGS
:
2829 case TYPE_CODE_BOOL
:
2831 case TYPE_CODE_CHAR
:
2832 case TYPE_CODE_RANGE
:
2833 case TYPE_CODE_MEMBERPTR
:
2837 if (type
->bit_size_differs_p ())
2839 unsigned bit_off
= type
->bit_offset ();
2840 unsigned bit_size
= type
->bit_size ();
2843 /* unpack_bits_as_long doesn't handle this case the
2844 way we'd like, so handle it here. */
2848 result
= unpack_bits_as_long (type
, valaddr
, bit_off
, bit_size
);
2853 result
= extract_unsigned_integer (valaddr
, len
, byte_order
);
2855 result
= extract_signed_integer (valaddr
, len
, byte_order
);
2857 if (code
== TYPE_CODE_RANGE
)
2858 result
+= type
->bounds ()->bias
;
2863 case TYPE_CODE_DECFLOAT
:
2864 return target_float_to_longest (valaddr
, type
);
2866 case TYPE_CODE_FIXED_POINT
:
2869 vq
.read_fixed_point (gdb::make_array_view (valaddr
, len
),
2871 type
->fixed_point_scaling_factor ());
2874 mpz_tdiv_q (vz
.val
, mpq_numref (vq
.val
), mpq_denref (vq
.val
));
2875 return vz
.as_integer
<LONGEST
> ();
2880 case TYPE_CODE_RVALUE_REF
:
2881 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2882 whether we want this to be true eventually. */
2883 return extract_typed_address (valaddr
, type
);
2886 error (_("Value can't be converted to integer."));
2890 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2891 as a CORE_ADDR, assuming the raw data is described by type TYPE.
2892 We don't assume any alignment for the raw data. Return value is in
2895 If you want functions and arrays to be coerced to pointers, and
2896 references to be dereferenced, call value_as_address() instead.
2898 C++: It is assumed that the front-end has taken care of
2899 all matters concerning pointers to members. A pointer
2900 to member which reaches here is considered to be equivalent
2901 to an INT (or some size). After all, it is only an offset. */
2904 unpack_pointer (struct type
*type
, const gdb_byte
*valaddr
)
2906 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2907 whether we want this to be true eventually. */
2908 return unpack_long (type
, valaddr
);
2912 is_floating_value (struct value
*val
)
2914 struct type
*type
= check_typedef (value_type (val
));
2916 if (is_floating_type (type
))
2918 if (!target_float_is_valid (value_contents (val
), type
))
2919 error (_("Invalid floating value found in program."));
2927 /* Get the value of the FIELDNO'th field (which must be static) of
2931 value_static_field (struct type
*type
, int fieldno
)
2933 struct value
*retval
;
2935 switch (TYPE_FIELD_LOC_KIND (type
, fieldno
))
2937 case FIELD_LOC_KIND_PHYSADDR
:
2938 retval
= value_at_lazy (type
->field (fieldno
).type (),
2939 TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
2941 case FIELD_LOC_KIND_PHYSNAME
:
2943 const char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
2944 /* TYPE_FIELD_NAME (type, fieldno); */
2945 struct block_symbol sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0);
2947 if (sym
.symbol
== NULL
)
2949 /* With some compilers, e.g. HP aCC, static data members are
2950 reported as non-debuggable symbols. */
2951 struct bound_minimal_symbol msym
2952 = lookup_minimal_symbol (phys_name
, NULL
, NULL
);
2953 struct type
*field_type
= type
->field (fieldno
).type ();
2956 retval
= allocate_optimized_out_value (field_type
);
2958 retval
= value_at_lazy (field_type
, BMSYMBOL_VALUE_ADDRESS (msym
));
2961 retval
= value_of_variable (sym
.symbol
, sym
.block
);
2965 gdb_assert_not_reached ("unexpected field location kind");
2971 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
2972 You have to be careful here, since the size of the data area for the value
2973 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
2974 than the old enclosing type, you have to allocate more space for the
2978 set_value_enclosing_type (struct value
*val
, struct type
*new_encl_type
)
2980 if (TYPE_LENGTH (new_encl_type
) > TYPE_LENGTH (value_enclosing_type (val
)))
2982 check_type_length_before_alloc (new_encl_type
);
2984 .reset ((gdb_byte
*) xrealloc (val
->contents
.release (),
2985 TYPE_LENGTH (new_encl_type
)));
2988 val
->enclosing_type
= new_encl_type
;
2991 /* Given a value ARG1 (offset by OFFSET bytes)
2992 of a struct or union type ARG_TYPE,
2993 extract and return the value of one of its (non-static) fields.
2994 FIELDNO says which field. */
2997 value_primitive_field (struct value
*arg1
, LONGEST offset
,
2998 int fieldno
, struct type
*arg_type
)
3002 struct gdbarch
*arch
= get_value_arch (arg1
);
3003 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
3005 arg_type
= check_typedef (arg_type
);
3006 type
= arg_type
->field (fieldno
).type ();
3008 /* Call check_typedef on our type to make sure that, if TYPE
3009 is a TYPE_CODE_TYPEDEF, its length is set to the length
3010 of the target type instead of zero. However, we do not
3011 replace the typedef type by the target type, because we want
3012 to keep the typedef in order to be able to print the type
3013 description correctly. */
3014 check_typedef (type
);
3016 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
3018 /* Handle packed fields.
3020 Create a new value for the bitfield, with bitpos and bitsize
3021 set. If possible, arrange offset and bitpos so that we can
3022 do a single aligned read of the size of the containing type.
3023 Otherwise, adjust offset to the byte containing the first
3024 bit. Assume that the address, offset, and embedded offset
3025 are sufficiently aligned. */
3027 LONGEST bitpos
= TYPE_FIELD_BITPOS (arg_type
, fieldno
);
3028 LONGEST container_bitsize
= TYPE_LENGTH (type
) * 8;
3030 v
= allocate_value_lazy (type
);
3031 v
->bitsize
= TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
3032 if ((bitpos
% container_bitsize
) + v
->bitsize
<= container_bitsize
3033 && TYPE_LENGTH (type
) <= (int) sizeof (LONGEST
))
3034 v
->bitpos
= bitpos
% container_bitsize
;
3036 v
->bitpos
= bitpos
% 8;
3037 v
->offset
= (value_embedded_offset (arg1
)
3039 + (bitpos
- v
->bitpos
) / 8);
3040 set_value_parent (v
, arg1
);
3041 if (!value_lazy (arg1
))
3042 value_fetch_lazy (v
);
3044 else if (fieldno
< TYPE_N_BASECLASSES (arg_type
))
3046 /* This field is actually a base subobject, so preserve the
3047 entire object's contents for later references to virtual
3051 /* Lazy register values with offsets are not supported. */
3052 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
3053 value_fetch_lazy (arg1
);
3055 /* We special case virtual inheritance here because this
3056 requires access to the contents, which we would rather avoid
3057 for references to ordinary fields of unavailable values. */
3058 if (BASETYPE_VIA_VIRTUAL (arg_type
, fieldno
))
3059 boffset
= baseclass_offset (arg_type
, fieldno
,
3060 value_contents (arg1
),
3061 value_embedded_offset (arg1
),
3062 value_address (arg1
),
3065 boffset
= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
3067 if (value_lazy (arg1
))
3068 v
= allocate_value_lazy (value_enclosing_type (arg1
));
3071 v
= allocate_value (value_enclosing_type (arg1
));
3072 value_contents_copy_raw (v
, 0, arg1
, 0,
3073 TYPE_LENGTH (value_enclosing_type (arg1
)));
3076 v
->offset
= value_offset (arg1
);
3077 v
->embedded_offset
= offset
+ value_embedded_offset (arg1
) + boffset
;
3079 else if (NULL
!= TYPE_DATA_LOCATION (type
))
3081 /* Field is a dynamic data member. */
3083 gdb_assert (0 == offset
);
3084 /* We expect an already resolved data location. */
3085 gdb_assert (PROP_CONST
== TYPE_DATA_LOCATION_KIND (type
));
3086 /* For dynamic data types defer memory allocation
3087 until we actual access the value. */
3088 v
= allocate_value_lazy (type
);
3092 /* Plain old data member */
3093 offset
+= (TYPE_FIELD_BITPOS (arg_type
, fieldno
)
3094 / (HOST_CHAR_BIT
* unit_size
));
3096 /* Lazy register values with offsets are not supported. */
3097 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
3098 value_fetch_lazy (arg1
);
3100 if (value_lazy (arg1
))
3101 v
= allocate_value_lazy (type
);
3104 v
= allocate_value (type
);
3105 value_contents_copy_raw (v
, value_embedded_offset (v
),
3106 arg1
, value_embedded_offset (arg1
) + offset
,
3107 type_length_units (type
));
3109 v
->offset
= (value_offset (arg1
) + offset
3110 + value_embedded_offset (arg1
));
3112 set_value_component_location (v
, arg1
);
3116 /* Given a value ARG1 of a struct or union type,
3117 extract and return the value of one of its (non-static) fields.
3118 FIELDNO says which field. */
3121 value_field (struct value
*arg1
, int fieldno
)
3123 return value_primitive_field (arg1
, 0, fieldno
, value_type (arg1
));
3126 /* Return a non-virtual function as a value.
3127 F is the list of member functions which contains the desired method.
3128 J is an index into F which provides the desired method.
3130 We only use the symbol for its address, so be happy with either a
3131 full symbol or a minimal symbol. */
3134 value_fn_field (struct value
**arg1p
, struct fn_field
*f
,
3135 int j
, struct type
*type
,
3139 struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
3140 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
3142 struct bound_minimal_symbol msym
;
3144 sym
= lookup_symbol (physname
, 0, VAR_DOMAIN
, 0).symbol
;
3147 memset (&msym
, 0, sizeof (msym
));
3151 gdb_assert (sym
== NULL
);
3152 msym
= lookup_bound_minimal_symbol (physname
);
3153 if (msym
.minsym
== NULL
)
3157 v
= allocate_value (ftype
);
3158 VALUE_LVAL (v
) = lval_memory
;
3161 set_value_address (v
, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
)));
3165 /* The minimal symbol might point to a function descriptor;
3166 resolve it to the actual code address instead. */
3167 struct objfile
*objfile
= msym
.objfile
;
3168 struct gdbarch
*gdbarch
= objfile
->arch ();
3170 set_value_address (v
,
3171 gdbarch_convert_from_func_ptr_addr
3172 (gdbarch
, BMSYMBOL_VALUE_ADDRESS (msym
), current_top_target ()));
3177 if (type
!= value_type (*arg1p
))
3178 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
3179 value_addr (*arg1p
)));
3181 /* Move the `this' pointer according to the offset.
3182 VALUE_OFFSET (*arg1p) += offset; */
3193 unpack_bits_as_long (struct type
*field_type
, const gdb_byte
*valaddr
,
3194 LONGEST bitpos
, LONGEST bitsize
)
3196 enum bfd_endian byte_order
= type_byte_order (field_type
);
3201 LONGEST read_offset
;
3203 /* Read the minimum number of bytes required; there may not be
3204 enough bytes to read an entire ULONGEST. */
3205 field_type
= check_typedef (field_type
);
3207 bytes_read
= ((bitpos
% 8) + bitsize
+ 7) / 8;
3210 bytes_read
= TYPE_LENGTH (field_type
);
3211 bitsize
= 8 * bytes_read
;
3214 read_offset
= bitpos
/ 8;
3216 val
= extract_unsigned_integer (valaddr
+ read_offset
,
3217 bytes_read
, byte_order
);
3219 /* Extract bits. See comment above. */
3221 if (byte_order
== BFD_ENDIAN_BIG
)
3222 lsbcount
= (bytes_read
* 8 - bitpos
% 8 - bitsize
);
3224 lsbcount
= (bitpos
% 8);
3227 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
3228 If the field is signed, and is negative, then sign extend. */
3230 if (bitsize
< 8 * (int) sizeof (val
))
3232 valmask
= (((ULONGEST
) 1) << bitsize
) - 1;
3234 if (!field_type
->is_unsigned ())
3236 if (val
& (valmask
^ (valmask
>> 1)))
3246 /* Unpack a field FIELDNO of the specified TYPE, from the object at
3247 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
3248 ORIGINAL_VALUE, which must not be NULL. See
3249 unpack_value_bits_as_long for more details. */
3252 unpack_value_field_as_long (struct type
*type
, const gdb_byte
*valaddr
,
3253 LONGEST embedded_offset
, int fieldno
,
3254 const struct value
*val
, LONGEST
*result
)
3256 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
3257 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
3258 struct type
*field_type
= type
->field (fieldno
).type ();
3261 gdb_assert (val
!= NULL
);
3263 bit_offset
= embedded_offset
* TARGET_CHAR_BIT
+ bitpos
;
3264 if (value_bits_any_optimized_out (val
, bit_offset
, bitsize
)
3265 || !value_bits_available (val
, bit_offset
, bitsize
))
3268 *result
= unpack_bits_as_long (field_type
, valaddr
+ embedded_offset
,
3273 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous
3274 object at VALADDR. See unpack_bits_as_long for more details. */
3277 unpack_field_as_long (struct type
*type
, const gdb_byte
*valaddr
, int fieldno
)
3279 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
3280 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
3281 struct type
*field_type
= type
->field (fieldno
).type ();
3283 return unpack_bits_as_long (field_type
, valaddr
, bitpos
, bitsize
);
3286 /* Unpack a bitfield of BITSIZE bits found at BITPOS in the object at
3287 VALADDR + EMBEDDEDOFFSET that has the type of DEST_VAL and store
3288 the contents in DEST_VAL, zero or sign extending if the type of
3289 DEST_VAL is wider than BITSIZE. VALADDR points to the contents of
3290 VAL. If the VAL's contents required to extract the bitfield from
3291 are unavailable/optimized out, DEST_VAL is correspondingly
3292 marked unavailable/optimized out. */
3295 unpack_value_bitfield (struct value
*dest_val
,
3296 LONGEST bitpos
, LONGEST bitsize
,
3297 const gdb_byte
*valaddr
, LONGEST embedded_offset
,
3298 const struct value
*val
)
3300 enum bfd_endian byte_order
;
3303 struct type
*field_type
= value_type (dest_val
);
3305 byte_order
= type_byte_order (field_type
);
3307 /* First, unpack and sign extend the bitfield as if it was wholly
3308 valid. Optimized out/unavailable bits are read as zero, but
3309 that's OK, as they'll end up marked below. If the VAL is
3310 wholly-invalid we may have skipped allocating its contents,
3311 though. See allocate_optimized_out_value. */
3312 if (valaddr
!= NULL
)
3316 num
= unpack_bits_as_long (field_type
, valaddr
+ embedded_offset
,
3318 store_signed_integer (value_contents_raw (dest_val
),
3319 TYPE_LENGTH (field_type
), byte_order
, num
);
3322 /* Now copy the optimized out / unavailability ranges to the right
3324 src_bit_offset
= embedded_offset
* TARGET_CHAR_BIT
+ bitpos
;
3325 if (byte_order
== BFD_ENDIAN_BIG
)
3326 dst_bit_offset
= TYPE_LENGTH (field_type
) * TARGET_CHAR_BIT
- bitsize
;
3329 value_ranges_copy_adjusted (dest_val
, dst_bit_offset
,
3330 val
, src_bit_offset
, bitsize
);
3333 /* Return a new value with type TYPE, which is FIELDNO field of the
3334 object at VALADDR + EMBEDDEDOFFSET. VALADDR points to the contents
3335 of VAL. If the VAL's contents required to extract the bitfield
3336 from are unavailable/optimized out, the new value is
3337 correspondingly marked unavailable/optimized out. */
3340 value_field_bitfield (struct type
*type
, int fieldno
,
3341 const gdb_byte
*valaddr
,
3342 LONGEST embedded_offset
, const struct value
*val
)
3344 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
3345 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
3346 struct value
*res_val
= allocate_value (type
->field (fieldno
).type ());
3348 unpack_value_bitfield (res_val
, bitpos
, bitsize
,
3349 valaddr
, embedded_offset
, val
);
3354 /* Modify the value of a bitfield. ADDR points to a block of memory in
3355 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
3356 is the desired value of the field, in host byte order. BITPOS and BITSIZE
3357 indicate which bits (in target bit order) comprise the bitfield.
3358 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
3359 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
3362 modify_field (struct type
*type
, gdb_byte
*addr
,
3363 LONGEST fieldval
, LONGEST bitpos
, LONGEST bitsize
)
3365 enum bfd_endian byte_order
= type_byte_order (type
);
3367 ULONGEST mask
= (ULONGEST
) -1 >> (8 * sizeof (ULONGEST
) - bitsize
);
3370 /* Normalize BITPOS. */
3374 /* If a negative fieldval fits in the field in question, chop
3375 off the sign extension bits. */
3376 if ((~fieldval
& ~(mask
>> 1)) == 0)
3379 /* Warn if value is too big to fit in the field in question. */
3380 if (0 != (fieldval
& ~mask
))
3382 /* FIXME: would like to include fieldval in the message, but
3383 we don't have a sprintf_longest. */
3384 warning (_("Value does not fit in %s bits."), plongest (bitsize
));
3386 /* Truncate it, otherwise adjoining fields may be corrupted. */
3390 /* Ensure no bytes outside of the modified ones get accessed as it may cause
3391 false valgrind reports. */
3393 bytesize
= (bitpos
+ bitsize
+ 7) / 8;
3394 oword
= extract_unsigned_integer (addr
, bytesize
, byte_order
);
3396 /* Shifting for bit field depends on endianness of the target machine. */
3397 if (byte_order
== BFD_ENDIAN_BIG
)
3398 bitpos
= bytesize
* 8 - bitpos
- bitsize
;
3400 oword
&= ~(mask
<< bitpos
);
3401 oword
|= fieldval
<< bitpos
;
3403 store_unsigned_integer (addr
, bytesize
, byte_order
, oword
);
3406 /* Pack NUM into BUF using a target format of TYPE. */
3409 pack_long (gdb_byte
*buf
, struct type
*type
, LONGEST num
)
3411 enum bfd_endian byte_order
= type_byte_order (type
);
3414 type
= check_typedef (type
);
3415 len
= TYPE_LENGTH (type
);
3417 switch (type
->code ())
3419 case TYPE_CODE_RANGE
:
3420 num
-= type
->bounds ()->bias
;
3423 case TYPE_CODE_CHAR
:
3424 case TYPE_CODE_ENUM
:
3425 case TYPE_CODE_FLAGS
:
3426 case TYPE_CODE_BOOL
:
3427 case TYPE_CODE_MEMBERPTR
:
3428 if (type
->bit_size_differs_p ())
3430 unsigned bit_off
= type
->bit_offset ();
3431 unsigned bit_size
= type
->bit_size ();
3432 num
&= ((ULONGEST
) 1 << bit_size
) - 1;
3435 store_signed_integer (buf
, len
, byte_order
, num
);
3439 case TYPE_CODE_RVALUE_REF
:
3441 store_typed_address (buf
, type
, (CORE_ADDR
) num
);
3445 case TYPE_CODE_DECFLOAT
:
3446 target_float_from_longest (buf
, type
, num
);
3450 error (_("Unexpected type (%d) encountered for integer constant."),
3456 /* Pack NUM into BUF using a target format of TYPE. */
3459 pack_unsigned_long (gdb_byte
*buf
, struct type
*type
, ULONGEST num
)
3462 enum bfd_endian byte_order
;
3464 type
= check_typedef (type
);
3465 len
= TYPE_LENGTH (type
);
3466 byte_order
= type_byte_order (type
);
3468 switch (type
->code ())
3471 case TYPE_CODE_CHAR
:
3472 case TYPE_CODE_ENUM
:
3473 case TYPE_CODE_FLAGS
:
3474 case TYPE_CODE_BOOL
:
3475 case TYPE_CODE_RANGE
:
3476 case TYPE_CODE_MEMBERPTR
:
3477 if (type
->bit_size_differs_p ())
3479 unsigned bit_off
= type
->bit_offset ();
3480 unsigned bit_size
= type
->bit_size ();
3481 num
&= ((ULONGEST
) 1 << bit_size
) - 1;
3484 store_unsigned_integer (buf
, len
, byte_order
, num
);
3488 case TYPE_CODE_RVALUE_REF
:
3490 store_typed_address (buf
, type
, (CORE_ADDR
) num
);
3494 case TYPE_CODE_DECFLOAT
:
3495 target_float_from_ulongest (buf
, type
, num
);
3499 error (_("Unexpected type (%d) encountered "
3500 "for unsigned integer constant."),
3506 /* Convert C numbers into newly allocated values. */
3509 value_from_longest (struct type
*type
, LONGEST num
)
3511 struct value
*val
= allocate_value (type
);
3513 pack_long (value_contents_raw (val
), type
, num
);
3518 /* Convert C unsigned numbers into newly allocated values. */
3521 value_from_ulongest (struct type
*type
, ULONGEST num
)
3523 struct value
*val
= allocate_value (type
);
3525 pack_unsigned_long (value_contents_raw (val
), type
, num
);
3531 /* Create a value representing a pointer of type TYPE to the address
3535 value_from_pointer (struct type
*type
, CORE_ADDR addr
)
3537 struct value
*val
= allocate_value (type
);
3539 store_typed_address (value_contents_raw (val
),
3540 check_typedef (type
), addr
);
3544 /* Create and return a value object of TYPE containing the value D. The
3545 TYPE must be of TYPE_CODE_FLT, and must be large enough to hold D once
3546 it is converted to target format. */
3549 value_from_host_double (struct type
*type
, double d
)
3551 struct value
*value
= allocate_value (type
);
3552 gdb_assert (type
->code () == TYPE_CODE_FLT
);
3553 target_float_from_host_double (value_contents_raw (value
),
3554 value_type (value
), d
);
3558 /* Create a value of type TYPE whose contents come from VALADDR, if it
3559 is non-null, and whose memory address (in the inferior) is
3560 ADDRESS. The type of the created value may differ from the passed
3561 type TYPE. Make sure to retrieve values new type after this call.
3562 Note that TYPE is not passed through resolve_dynamic_type; this is
3563 a special API intended for use only by Ada. */
3566 value_from_contents_and_address_unresolved (struct type
*type
,
3567 const gdb_byte
*valaddr
,
3572 if (valaddr
== NULL
)
3573 v
= allocate_value_lazy (type
);
3575 v
= value_from_contents (type
, valaddr
);
3576 VALUE_LVAL (v
) = lval_memory
;
3577 set_value_address (v
, address
);
3581 /* Create a value of type TYPE whose contents come from VALADDR, if it
3582 is non-null, and whose memory address (in the inferior) is
3583 ADDRESS. The type of the created value may differ from the passed
3584 type TYPE. Make sure to retrieve values new type after this call. */
3587 value_from_contents_and_address (struct type
*type
,
3588 const gdb_byte
*valaddr
,
3591 gdb::array_view
<const gdb_byte
> view
;
3592 if (valaddr
!= nullptr)
3593 view
= gdb::make_array_view (valaddr
, TYPE_LENGTH (type
));
3594 struct type
*resolved_type
= resolve_dynamic_type (type
, view
, address
);
3595 struct type
*resolved_type_no_typedef
= check_typedef (resolved_type
);
3598 if (valaddr
== NULL
)
3599 v
= allocate_value_lazy (resolved_type
);
3601 v
= value_from_contents (resolved_type
, valaddr
);
3602 if (TYPE_DATA_LOCATION (resolved_type_no_typedef
) != NULL
3603 && TYPE_DATA_LOCATION_KIND (resolved_type_no_typedef
) == PROP_CONST
)
3604 address
= TYPE_DATA_LOCATION_ADDR (resolved_type_no_typedef
);
3605 VALUE_LVAL (v
) = lval_memory
;
3606 set_value_address (v
, address
);
3610 /* Create a value of type TYPE holding the contents CONTENTS.
3611 The new value is `not_lval'. */
3614 value_from_contents (struct type
*type
, const gdb_byte
*contents
)
3616 struct value
*result
;
3618 result
= allocate_value (type
);
3619 memcpy (value_contents_raw (result
), contents
, TYPE_LENGTH (type
));
3623 /* Extract a value from the history file. Input will be of the form
3624 $digits or $$digits. See block comment above 'write_dollar_variable'
3628 value_from_history_ref (const char *h
, const char **endp
)
3640 /* Find length of numeral string. */
3641 for (; isdigit (h
[len
]); len
++)
3644 /* Make sure numeral string is not part of an identifier. */
3645 if (h
[len
] == '_' || isalpha (h
[len
]))
3648 /* Now collect the index value. */
3653 /* For some bizarre reason, "$$" is equivalent to "$$1",
3654 rather than to "$$0" as it ought to be! */
3662 index
= -strtol (&h
[2], &local_end
, 10);
3670 /* "$" is equivalent to "$0". */
3678 index
= strtol (&h
[1], &local_end
, 10);
3683 return access_value_history (index
);
3686 /* Get the component value (offset by OFFSET bytes) of a struct or
3687 union WHOLE. Component's type is TYPE. */
3690 value_from_component (struct value
*whole
, struct type
*type
, LONGEST offset
)
3694 if (VALUE_LVAL (whole
) == lval_memory
&& value_lazy (whole
))
3695 v
= allocate_value_lazy (type
);
3698 v
= allocate_value (type
);
3699 value_contents_copy (v
, value_embedded_offset (v
),
3700 whole
, value_embedded_offset (whole
) + offset
,
3701 type_length_units (type
));
3703 v
->offset
= value_offset (whole
) + offset
+ value_embedded_offset (whole
);
3704 set_value_component_location (v
, whole
);
3710 coerce_ref_if_computed (const struct value
*arg
)
3712 const struct lval_funcs
*funcs
;
3714 if (!TYPE_IS_REFERENCE (check_typedef (value_type (arg
))))
3717 if (value_lval_const (arg
) != lval_computed
)
3720 funcs
= value_computed_funcs (arg
);
3721 if (funcs
->coerce_ref
== NULL
)
3724 return funcs
->coerce_ref (arg
);
3727 /* Look at value.h for description. */
3730 readjust_indirect_value_type (struct value
*value
, struct type
*enc_type
,
3731 const struct type
*original_type
,
3732 struct value
*original_value
,
3733 CORE_ADDR original_value_address
)
3735 gdb_assert (original_type
->code () == TYPE_CODE_PTR
3736 || TYPE_IS_REFERENCE (original_type
));
3738 struct type
*original_target_type
= TYPE_TARGET_TYPE (original_type
);
3739 gdb::array_view
<const gdb_byte
> view
;
3740 struct type
*resolved_original_target_type
3741 = resolve_dynamic_type (original_target_type
, view
,
3742 original_value_address
);
3744 /* Re-adjust type. */
3745 deprecated_set_value_type (value
, resolved_original_target_type
);
3747 /* Add embedding info. */
3748 set_value_enclosing_type (value
, enc_type
);
3749 set_value_embedded_offset (value
, value_pointed_to_offset (original_value
));
3751 /* We may be pointing to an object of some derived type. */
3752 return value_full_object (value
, NULL
, 0, 0, 0);
3756 coerce_ref (struct value
*arg
)
3758 struct type
*value_type_arg_tmp
= check_typedef (value_type (arg
));
3759 struct value
*retval
;
3760 struct type
*enc_type
;
3762 retval
= coerce_ref_if_computed (arg
);
3766 if (!TYPE_IS_REFERENCE (value_type_arg_tmp
))
3769 enc_type
= check_typedef (value_enclosing_type (arg
));
3770 enc_type
= TYPE_TARGET_TYPE (enc_type
);
3772 CORE_ADDR addr
= unpack_pointer (value_type (arg
), value_contents (arg
));
3773 retval
= value_at_lazy (enc_type
, addr
);
3774 enc_type
= value_type (retval
);
3775 return readjust_indirect_value_type (retval
, enc_type
, value_type_arg_tmp
,
3780 coerce_array (struct value
*arg
)
3784 arg
= coerce_ref (arg
);
3785 type
= check_typedef (value_type (arg
));
3787 switch (type
->code ())
3789 case TYPE_CODE_ARRAY
:
3790 if (!type
->is_vector () && current_language
->c_style_arrays_p ())
3791 arg
= value_coerce_array (arg
);
3793 case TYPE_CODE_FUNC
:
3794 arg
= value_coerce_function (arg
);
3801 /* Return the return value convention that will be used for the
3804 enum return_value_convention
3805 struct_return_convention (struct gdbarch
*gdbarch
,
3806 struct value
*function
, struct type
*value_type
)
3808 enum type_code code
= value_type
->code ();
3810 if (code
== TYPE_CODE_ERROR
)
3811 error (_("Function return type unknown."));
3813 /* Probe the architecture for the return-value convention. */
3814 return gdbarch_return_value (gdbarch
, function
, value_type
,
3818 /* Return true if the function returning the specified type is using
3819 the convention of returning structures in memory (passing in the
3820 address as a hidden first parameter). */
3823 using_struct_return (struct gdbarch
*gdbarch
,
3824 struct value
*function
, struct type
*value_type
)
3826 if (value_type
->code () == TYPE_CODE_VOID
)
3827 /* A void return value is never in memory. See also corresponding
3828 code in "print_return_value". */
3831 return (struct_return_convention (gdbarch
, function
, value_type
)
3832 != RETURN_VALUE_REGISTER_CONVENTION
);
3835 /* Set the initialized field in a value struct. */
3838 set_value_initialized (struct value
*val
, int status
)
3840 val
->initialized
= status
;
3843 /* Return the initialized field in a value struct. */
3846 value_initialized (const struct value
*val
)
3848 return val
->initialized
;
3851 /* Helper for value_fetch_lazy when the value is a bitfield. */
3854 value_fetch_lazy_bitfield (struct value
*val
)
3856 gdb_assert (value_bitsize (val
) != 0);
3858 /* To read a lazy bitfield, read the entire enclosing value. This
3859 prevents reading the same block of (possibly volatile) memory once
3860 per bitfield. It would be even better to read only the containing
3861 word, but we have no way to record that just specific bits of a
3862 value have been fetched. */
3863 struct value
*parent
= value_parent (val
);
3865 if (value_lazy (parent
))
3866 value_fetch_lazy (parent
);
3868 unpack_value_bitfield (val
, value_bitpos (val
), value_bitsize (val
),
3869 value_contents_for_printing (parent
),
3870 value_offset (val
), parent
);
3873 /* Helper for value_fetch_lazy when the value is in memory. */
3876 value_fetch_lazy_memory (struct value
*val
)
3878 gdb_assert (VALUE_LVAL (val
) == lval_memory
);
3880 CORE_ADDR addr
= value_address (val
);
3881 struct type
*type
= check_typedef (value_enclosing_type (val
));
3883 if (TYPE_LENGTH (type
))
3884 read_value_memory (val
, 0, value_stack (val
),
3885 addr
, value_contents_all_raw (val
),
3886 type_length_units (type
));
3889 /* Helper for value_fetch_lazy when the value is in a register. */
3892 value_fetch_lazy_register (struct value
*val
)
3894 struct frame_info
*next_frame
;
3896 struct type
*type
= check_typedef (value_type (val
));
3897 struct value
*new_val
= val
, *mark
= value_mark ();
3899 /* Offsets are not supported here; lazy register values must
3900 refer to the entire register. */
3901 gdb_assert (value_offset (val
) == 0);
3903 while (VALUE_LVAL (new_val
) == lval_register
&& value_lazy (new_val
))
3905 struct frame_id next_frame_id
= VALUE_NEXT_FRAME_ID (new_val
);
3907 next_frame
= frame_find_by_id (next_frame_id
);
3908 regnum
= VALUE_REGNUM (new_val
);
3910 gdb_assert (next_frame
!= NULL
);
3912 /* Convertible register routines are used for multi-register
3913 values and for interpretation in different types
3914 (e.g. float or int from a double register). Lazy
3915 register values should have the register's natural type,
3916 so they do not apply. */
3917 gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame
),
3920 /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID.
3921 Since a "->next" operation was performed when setting
3922 this field, we do not need to perform a "next" operation
3923 again when unwinding the register. That's why
3924 frame_unwind_register_value() is called here instead of
3925 get_frame_register_value(). */
3926 new_val
= frame_unwind_register_value (next_frame
, regnum
);
3928 /* If we get another lazy lval_register value, it means the
3929 register is found by reading it from NEXT_FRAME's next frame.
3930 frame_unwind_register_value should never return a value with
3931 the frame id pointing to NEXT_FRAME. If it does, it means we
3932 either have two consecutive frames with the same frame id
3933 in the frame chain, or some code is trying to unwind
3934 behind get_prev_frame's back (e.g., a frame unwind
3935 sniffer trying to unwind), bypassing its validations. In
3936 any case, it should always be an internal error to end up
3937 in this situation. */
3938 if (VALUE_LVAL (new_val
) == lval_register
3939 && value_lazy (new_val
)
3940 && frame_id_eq (VALUE_NEXT_FRAME_ID (new_val
), next_frame_id
))
3941 internal_error (__FILE__
, __LINE__
,
3942 _("infinite loop while fetching a register"));
3945 /* If it's still lazy (for instance, a saved register on the
3946 stack), fetch it. */
3947 if (value_lazy (new_val
))
3948 value_fetch_lazy (new_val
);
3950 /* Copy the contents and the unavailability/optimized-out
3951 meta-data from NEW_VAL to VAL. */
3952 set_value_lazy (val
, 0);
3953 value_contents_copy (val
, value_embedded_offset (val
),
3954 new_val
, value_embedded_offset (new_val
),
3955 type_length_units (type
));
3959 struct gdbarch
*gdbarch
;
3960 struct frame_info
*frame
;
3961 /* VALUE_FRAME_ID is used here, instead of VALUE_NEXT_FRAME_ID,
3962 so that the frame level will be shown correctly. */
3963 frame
= frame_find_by_id (VALUE_FRAME_ID (val
));
3964 regnum
= VALUE_REGNUM (val
);
3965 gdbarch
= get_frame_arch (frame
);
3967 fprintf_unfiltered (gdb_stdlog
,
3968 "{ value_fetch_lazy "
3969 "(frame=%d,regnum=%d(%s),...) ",
3970 frame_relative_level (frame
), regnum
,
3971 user_reg_map_regnum_to_name (gdbarch
, regnum
));
3973 fprintf_unfiltered (gdb_stdlog
, "->");
3974 if (value_optimized_out (new_val
))
3976 fprintf_unfiltered (gdb_stdlog
, " ");
3977 val_print_optimized_out (new_val
, gdb_stdlog
);
3982 const gdb_byte
*buf
= value_contents (new_val
);
3984 if (VALUE_LVAL (new_val
) == lval_register
)
3985 fprintf_unfiltered (gdb_stdlog
, " register=%d",
3986 VALUE_REGNUM (new_val
));
3987 else if (VALUE_LVAL (new_val
) == lval_memory
)
3988 fprintf_unfiltered (gdb_stdlog
, " address=%s",
3990 value_address (new_val
)));
3992 fprintf_unfiltered (gdb_stdlog
, " computed");
3994 fprintf_unfiltered (gdb_stdlog
, " bytes=");
3995 fprintf_unfiltered (gdb_stdlog
, "[");
3996 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
3997 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
3998 fprintf_unfiltered (gdb_stdlog
, "]");
4001 fprintf_unfiltered (gdb_stdlog
, " }\n");
4004 /* Dispose of the intermediate values. This prevents
4005 watchpoints from trying to watch the saved frame pointer. */
4006 value_free_to_mark (mark
);
4009 /* Load the actual content of a lazy value. Fetch the data from the
4010 user's process and clear the lazy flag to indicate that the data in
4011 the buffer is valid.
4013 If the value is zero-length, we avoid calling read_memory, which
4014 would abort. We mark the value as fetched anyway -- all 0 bytes of
4018 value_fetch_lazy (struct value
*val
)
4020 gdb_assert (value_lazy (val
));
4021 allocate_value_contents (val
);
4022 /* A value is either lazy, or fully fetched. The
4023 availability/validity is only established as we try to fetch a
4025 gdb_assert (val
->optimized_out
.empty ());
4026 gdb_assert (val
->unavailable
.empty ());
4027 if (value_bitsize (val
))
4028 value_fetch_lazy_bitfield (val
);
4029 else if (VALUE_LVAL (val
) == lval_memory
)
4030 value_fetch_lazy_memory (val
);
4031 else if (VALUE_LVAL (val
) == lval_register
)
4032 value_fetch_lazy_register (val
);
4033 else if (VALUE_LVAL (val
) == lval_computed
4034 && value_computed_funcs (val
)->read
!= NULL
)
4035 value_computed_funcs (val
)->read (val
);
4037 internal_error (__FILE__
, __LINE__
, _("Unexpected lazy value type."));
4039 set_value_lazy (val
, 0);
4042 /* Implementation of the convenience function $_isvoid. */
4044 static struct value
*
4045 isvoid_internal_fn (struct gdbarch
*gdbarch
,
4046 const struct language_defn
*language
,
4047 void *cookie
, int argc
, struct value
**argv
)
4052 error (_("You must provide one argument for $_isvoid."));
4054 ret
= value_type (argv
[0])->code () == TYPE_CODE_VOID
;
4056 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, ret
);
4059 /* Implementation of the convenience function $_creal. Extracts the
4060 real part from a complex number. */
4062 static struct value
*
4063 creal_internal_fn (struct gdbarch
*gdbarch
,
4064 const struct language_defn
*language
,
4065 void *cookie
, int argc
, struct value
**argv
)
4068 error (_("You must provide one argument for $_creal."));
4070 value
*cval
= argv
[0];
4071 type
*ctype
= check_typedef (value_type (cval
));
4072 if (ctype
->code () != TYPE_CODE_COMPLEX
)
4073 error (_("expected a complex number"));
4074 return value_real_part (cval
);
4077 /* Implementation of the convenience function $_cimag. Extracts the
4078 imaginary part from a complex number. */
4080 static struct value
*
4081 cimag_internal_fn (struct gdbarch
*gdbarch
,
4082 const struct language_defn
*language
,
4083 void *cookie
, int argc
,
4084 struct value
**argv
)
4087 error (_("You must provide one argument for $_cimag."));
4089 value
*cval
= argv
[0];
4090 type
*ctype
= check_typedef (value_type (cval
));
4091 if (ctype
->code () != TYPE_CODE_COMPLEX
)
4092 error (_("expected a complex number"));
4093 return value_imaginary_part (cval
);
4100 /* Test the ranges_contain function. */
4103 test_ranges_contain ()
4105 std::vector
<range
> ranges
;
4111 ranges
.push_back (r
);
4116 ranges
.push_back (r
);
4119 SELF_CHECK (!ranges_contain (ranges
, 2, 5));
4121 SELF_CHECK (ranges_contain (ranges
, 9, 5));
4123 SELF_CHECK (ranges_contain (ranges
, 10, 2));
4125 SELF_CHECK (ranges_contain (ranges
, 10, 5));
4127 SELF_CHECK (ranges_contain (ranges
, 13, 6));
4129 SELF_CHECK (ranges_contain (ranges
, 14, 5));
4131 SELF_CHECK (!ranges_contain (ranges
, 15, 4));
4133 SELF_CHECK (!ranges_contain (ranges
, 16, 4));
4135 SELF_CHECK (ranges_contain (ranges
, 16, 6));
4137 SELF_CHECK (ranges_contain (ranges
, 21, 1));
4139 SELF_CHECK (ranges_contain (ranges
, 21, 5));
4141 SELF_CHECK (!ranges_contain (ranges
, 26, 3));
4144 /* Check that RANGES contains the same ranges as EXPECTED. */
4147 check_ranges_vector (gdb::array_view
<const range
> ranges
,
4148 gdb::array_view
<const range
> expected
)
4150 return ranges
== expected
;
4153 /* Test the insert_into_bit_range_vector function. */
4156 test_insert_into_bit_range_vector ()
4158 std::vector
<range
> ranges
;
4162 insert_into_bit_range_vector (&ranges
, 10, 5);
4163 static const range expected
[] = {
4166 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4171 insert_into_bit_range_vector (&ranges
, 11, 4);
4172 static const range expected
= {10, 5};
4173 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4176 /* [10, 14] [20, 24] */
4178 insert_into_bit_range_vector (&ranges
, 20, 5);
4179 static const range expected
[] = {
4183 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4186 /* [10, 14] [17, 24] */
4188 insert_into_bit_range_vector (&ranges
, 17, 5);
4189 static const range expected
[] = {
4193 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4196 /* [2, 8] [10, 14] [17, 24] */
4198 insert_into_bit_range_vector (&ranges
, 2, 7);
4199 static const range expected
[] = {
4204 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4207 /* [2, 14] [17, 24] */
4209 insert_into_bit_range_vector (&ranges
, 9, 1);
4210 static const range expected
[] = {
4214 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4217 /* [2, 14] [17, 24] */
4219 insert_into_bit_range_vector (&ranges
, 9, 1);
4220 static const range expected
[] = {
4224 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4229 insert_into_bit_range_vector (&ranges
, 4, 30);
4230 static const range expected
= {2, 32};
4231 SELF_CHECK (check_ranges_vector (ranges
, expected
));
4235 } /* namespace selftests */
4236 #endif /* GDB_SELF_TEST */
4238 void _initialize_values ();
4240 _initialize_values ()
4242 add_cmd ("convenience", no_class
, show_convenience
, _("\
4243 Debugger convenience (\"$foo\") variables and functions.\n\
4244 Convenience variables are created when you assign them values;\n\
4245 thus, \"set $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
4247 A few convenience variables are given values automatically:\n\
4248 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
4249 \"$__\" holds the contents of the last address examined with \"x\"."
4252 Convenience functions are defined via the Python API."
4255 add_alias_cmd ("conv", "convenience", no_class
, 1, &showlist
);
4257 add_cmd ("values", no_set_class
, show_values
, _("\
4258 Elements of value history around item number IDX (or last ten)."),
4261 add_com ("init-if-undefined", class_vars
, init_if_undefined_command
, _("\
4262 Initialize a convenience variable if necessary.\n\
4263 init-if-undefined VARIABLE = EXPRESSION\n\
4264 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
4265 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
4266 VARIABLE is already initialized."));
4268 add_prefix_cmd ("function", no_class
, function_command
, _("\
4269 Placeholder command for showing help on convenience functions."),
4270 &functionlist
, "function ", 0, &cmdlist
);
4272 add_internal_function ("_isvoid", _("\
4273 Check whether an expression is void.\n\
4274 Usage: $_isvoid (expression)\n\
4275 Return 1 if the expression is void, zero otherwise."),
4276 isvoid_internal_fn
, NULL
);
4278 add_internal_function ("_creal", _("\
4279 Extract the real part of a complex number.\n\
4280 Usage: $_creal (expression)\n\
4281 Return the real part of a complex number, the type depends on the\n\
4282 type of a complex number."),
4283 creal_internal_fn
, NULL
);
4285 add_internal_function ("_cimag", _("\
4286 Extract the imaginary part of a complex number.\n\
4287 Usage: $_cimag (expression)\n\
4288 Return the imaginary part of a complex number, the type depends on the\n\
4289 type of a complex number."),
4290 cimag_internal_fn
, NULL
);
4292 add_setshow_zuinteger_unlimited_cmd ("max-value-size",
4293 class_support
, &max_value_size
, _("\
4294 Set maximum sized value gdb will load from the inferior."), _("\
4295 Show maximum sized value gdb will load from the inferior."), _("\
4296 Use this to control the maximum size, in bytes, of a value that gdb\n\
4297 will load from the inferior. Setting this value to 'unlimited'\n\
4298 disables checking.\n\
4299 Setting this does not invalidate already allocated values, it only\n\
4300 prevents future values, larger than this size, from being allocated."),
4302 show_max_value_size
,
4303 &setlist
, &showlist
);
4305 selftests::register_test ("ranges_contain", selftests::test_ranges_contain
);
4306 selftests::register_test ("insert_into_bit_range_vector",
4307 selftests::test_insert_into_bit_range_vector
);
4316 all_values
.clear ();