1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
3 Copyright (C) 1986-2000, 2002-2012 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"
22 #include "gdb_string.h"
33 #include "gdb_assert.h"
39 #include "cli/cli-decode.h"
40 #include "exceptions.h"
41 #include "python/python.h"
43 #include "tracepoint.h"
45 /* Prototypes for exported functions. */
47 void _initialize_values (void);
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. */
75 typedef struct range range_s;
79 /* Returns true if the ranges defined by [offset1, offset1+len1) and
80 [offset2, offset2+len2) overlap. */
83 ranges_overlap (int offset1, int len1,
84 int offset2, int len2)
88 l = max (offset1, offset2);
89 h = min (offset1 + len1, offset2 + len2);
93 /* Returns true if the first argument is strictly less than the
94 second, useful for VEC_lower_bound. We keep ranges sorted by
95 offset and coalesce overlapping and contiguous ranges, so this just
96 compares the starting offset. */
99 range_lessthan (const range_s *r1, const range_s *r2)
101 return r1->offset < r2->offset;
104 /* Returns true if RANGES contains any range that overlaps [OFFSET,
108 ranges_contain (VEC(range_s) *ranges, int offset, int length)
113 what.offset = offset;
114 what.length = length;
116 /* We keep ranges sorted by offset and coalesce overlapping and
117 contiguous ranges, so to check if a range list contains a given
118 range, we can do a binary search for the position the given range
119 would be inserted if we only considered the starting OFFSET of
120 ranges. We call that position I. Since we also have LENGTH to
121 care for (this is a range afterall), we need to check if the
122 _previous_ range overlaps the I range. E.g.,
126 |---| |---| |------| ... |--|
131 In the case above, the binary search would return `I=1', meaning,
132 this OFFSET should be inserted at position 1, and the current
133 position 1 should be pushed further (and before 2). But, `0'
136 Then we need to check if the I range overlaps the I range itself.
141 |---| |---| |-------| ... |--|
147 i = VEC_lower_bound (range_s, ranges, &what, range_lessthan);
151 struct range *bef = VEC_index (range_s, ranges, i - 1);
153 if (ranges_overlap (bef->offset, bef->length, offset, length))
157 if (i < VEC_length (range_s, ranges))
159 struct range *r = VEC_index (range_s, ranges, i);
161 if (ranges_overlap (r->offset, r->length, offset, length))
168 static struct cmd_list_element *functionlist;
170 /* Note that the fields in this structure are arranged to save a bit
175 /* Type of value; either not an lval, or one of the various
176 different possible kinds of lval. */
179 /* Is it modifiable? Only relevant if lval != not_lval. */
180 unsigned int modifiable : 1;
182 /* If zero, contents of this value are in the contents field. If
183 nonzero, contents are in inferior. If the lval field is lval_memory,
184 the contents are in inferior memory at location.address plus offset.
185 The lval field may also be lval_register.
187 WARNING: This field is used by the code which handles watchpoints
188 (see breakpoint.c) to decide whether a particular value can be
189 watched by hardware watchpoints. If the lazy flag is set for
190 some member of a value chain, it is assumed that this member of
191 the chain doesn't need to be watched as part of watching the
192 value itself. This is how GDB avoids watching the entire struct
193 or array when the user wants to watch a single struct member or
194 array element. If you ever change the way lazy flag is set and
195 reset, be sure to consider this use as well! */
196 unsigned int lazy : 1;
198 /* If nonzero, this is the value of a variable which does not
199 actually exist in the program. */
200 unsigned int optimized_out : 1;
202 /* If value is a variable, is it initialized or not. */
203 unsigned int initialized : 1;
205 /* If value is from the stack. If this is set, read_stack will be
206 used instead of read_memory to enable extra caching. */
207 unsigned int stack : 1;
209 /* If the value has been released. */
210 unsigned int released : 1;
212 /* Location of value (if lval). */
215 /* If lval == lval_memory, this is the address in the inferior.
216 If lval == lval_register, this is the byte offset into the
217 registers structure. */
220 /* Pointer to internal variable. */
221 struct internalvar *internalvar;
223 /* If lval == lval_computed, this is a set of function pointers
224 to use to access and describe the value, and a closure pointer
228 /* Functions to call. */
229 const struct lval_funcs *funcs;
231 /* Closure for those functions to use. */
236 /* Describes offset of a value within lval of a structure in bytes.
237 If lval == lval_memory, this is an offset to the address. If
238 lval == lval_register, this is a further offset from
239 location.address within the registers structure. Note also the
240 member embedded_offset below. */
243 /* Only used for bitfields; number of bits contained in them. */
246 /* Only used for bitfields; position of start of field. For
247 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
248 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
251 /* The number of references to this value. When a value is created,
252 the value chain holds a reference, so REFERENCE_COUNT is 1. If
253 release_value is called, this value is removed from the chain but
254 the caller of release_value now has a reference to this value.
255 The caller must arrange for a call to value_free later. */
258 /* Only used for bitfields; the containing value. This allows a
259 single read from the target when displaying multiple
261 struct value *parent;
263 /* Frame register value is relative to. This will be described in
264 the lval enum above as "lval_register". */
265 struct frame_id frame_id;
267 /* Type of the value. */
270 /* If a value represents a C++ object, then the `type' field gives
271 the object's compile-time type. If the object actually belongs
272 to some class derived from `type', perhaps with other base
273 classes and additional members, then `type' is just a subobject
274 of the real thing, and the full object is probably larger than
275 `type' would suggest.
277 If `type' is a dynamic class (i.e. one with a vtable), then GDB
278 can actually determine the object's run-time type by looking at
279 the run-time type information in the vtable. When this
280 information is available, we may elect to read in the entire
281 object, for several reasons:
283 - When printing the value, the user would probably rather see the
284 full object, not just the limited portion apparent from the
287 - If `type' has virtual base classes, then even printing `type'
288 alone may require reaching outside the `type' portion of the
289 object to wherever the virtual base class has been stored.
291 When we store the entire object, `enclosing_type' is the run-time
292 type -- the complete object -- and `embedded_offset' is the
293 offset of `type' within that larger type, in bytes. The
294 value_contents() macro takes `embedded_offset' into account, so
295 most GDB code continues to see the `type' portion of the value,
296 just as the inferior would.
298 If `type' is a pointer to an object, then `enclosing_type' is a
299 pointer to the object's run-time type, and `pointed_to_offset' is
300 the offset in bytes from the full object to the pointed-to object
301 -- that is, the value `embedded_offset' would have if we followed
302 the pointer and fetched the complete object. (I don't really see
303 the point. Why not just determine the run-time type when you
304 indirect, and avoid the special case? The contents don't matter
305 until you indirect anyway.)
307 If we're not doing anything fancy, `enclosing_type' is equal to
308 `type', and `embedded_offset' is zero, so everything works
310 struct type *enclosing_type;
312 int pointed_to_offset;
314 /* Values are stored in a chain, so that they can be deleted easily
315 over calls to the inferior. Values assigned to internal
316 variables, put into the value history or exposed to Python are
317 taken off this list. */
320 /* Register number if the value is from a register. */
323 /* Actual contents of the value. Target byte-order. NULL or not
324 valid if lazy is nonzero. */
327 /* Unavailable ranges in CONTENTS. We mark unavailable ranges,
328 rather than available, since the common and default case is for a
329 value to be available. This is filled in at value read time. */
330 VEC(range_s) *unavailable;
334 value_bytes_available (const struct value *value, int offset, int length)
336 gdb_assert (!value->lazy);
338 return !ranges_contain (value->unavailable, offset, length);
342 value_entirely_available (struct value *value)
344 /* We can only tell whether the whole value is available when we try
347 value_fetch_lazy (value);
349 if (VEC_empty (range_s, value->unavailable))
355 mark_value_bytes_unavailable (struct value *value, int offset, int length)
360 /* Insert the range sorted. If there's overlap or the new range
361 would be contiguous with an existing range, merge. */
363 newr.offset = offset;
364 newr.length = length;
366 /* Do a binary search for the position the given range would be
367 inserted if we only considered the starting OFFSET of ranges.
368 Call that position I. Since we also have LENGTH to care for
369 (this is a range afterall), we need to check if the _previous_
370 range overlaps the I range. E.g., calling R the new range:
372 #1 - overlaps with previous
376 |---| |---| |------| ... |--|
381 In the case #1 above, the binary search would return `I=1',
382 meaning, this OFFSET should be inserted at position 1, and the
383 current position 1 should be pushed further (and become 2). But,
384 note that `0' overlaps with R, so we want to merge them.
386 A similar consideration needs to be taken if the new range would
387 be contiguous with the previous range:
389 #2 - contiguous with previous
393 |--| |---| |------| ... |--|
398 If there's no overlap with the previous range, as in:
400 #3 - not overlapping and not contiguous
404 |--| |---| |------| ... |--|
411 #4 - R is the range with lowest offset
415 |--| |---| |------| ... |--|
420 ... we just push the new range to I.
422 All the 4 cases above need to consider that the new range may
423 also overlap several of the ranges that follow, or that R may be
424 contiguous with the following range, and merge. E.g.,
426 #5 - overlapping following ranges
429 |------------------------|
430 |--| |---| |------| ... |--|
439 |--| |---| |------| ... |--|
446 i = VEC_lower_bound (range_s, value->unavailable, &newr, range_lessthan);
449 struct range *bef = VEC_index (range_s, value->unavailable, i - 1);
451 if (ranges_overlap (bef->offset, bef->length, offset, length))
454 ULONGEST l = min (bef->offset, offset);
455 ULONGEST h = max (bef->offset + bef->length, offset + length);
461 else if (offset == bef->offset + bef->length)
464 bef->length += length;
470 VEC_safe_insert (range_s, value->unavailable, i, &newr);
476 VEC_safe_insert (range_s, value->unavailable, i, &newr);
479 /* Check whether the ranges following the one we've just added or
480 touched can be folded in (#5 above). */
481 if (i + 1 < VEC_length (range_s, value->unavailable))
488 /* Get the range we just touched. */
489 t = VEC_index (range_s, value->unavailable, i);
493 for (; VEC_iterate (range_s, value->unavailable, i, r); i++)
494 if (r->offset <= t->offset + t->length)
498 l = min (t->offset, r->offset);
499 h = max (t->offset + t->length, r->offset + r->length);
508 /* If we couldn't merge this one, we won't be able to
509 merge following ones either, since the ranges are
510 always sorted by OFFSET. */
515 VEC_block_remove (range_s, value->unavailable, next, removed);
519 /* Find the first range in RANGES that overlaps the range defined by
520 OFFSET and LENGTH, starting at element POS in the RANGES vector,
521 Returns the index into RANGES where such overlapping range was
522 found, or -1 if none was found. */
525 find_first_range_overlap (VEC(range_s) *ranges, int pos,
526 int offset, int length)
531 for (i = pos; VEC_iterate (range_s, ranges, i, r); i++)
532 if (ranges_overlap (r->offset, r->length, offset, length))
539 value_available_contents_eq (const struct value *val1, int offset1,
540 const struct value *val2, int offset2,
543 int idx1 = 0, idx2 = 0;
545 /* This routine is used by printing routines, where we should
546 already have read the value. Note that we only know whether a
547 value chunk is available if we've tried to read it. */
548 gdb_assert (!val1->lazy && !val2->lazy);
556 idx1 = find_first_range_overlap (val1->unavailable, idx1,
558 idx2 = find_first_range_overlap (val2->unavailable, idx2,
561 /* The usual case is for both values to be completely available. */
562 if (idx1 == -1 && idx2 == -1)
563 return (memcmp (val1->contents + offset1,
564 val2->contents + offset2,
566 /* The contents only match equal if the available set matches as
568 else if (idx1 == -1 || idx2 == -1)
571 gdb_assert (idx1 != -1 && idx2 != -1);
573 r1 = VEC_index (range_s, val1->unavailable, idx1);
574 r2 = VEC_index (range_s, val2->unavailable, idx2);
576 /* Get the unavailable windows intersected by the incoming
577 ranges. The first and last ranges that overlap the argument
578 range may be wider than said incoming arguments ranges. */
579 l1 = max (offset1, r1->offset);
580 h1 = min (offset1 + length, r1->offset + r1->length);
582 l2 = max (offset2, r2->offset);
583 h2 = min (offset2 + length, r2->offset + r2->length);
585 /* Make them relative to the respective start offsets, so we can
586 compare them for equality. */
593 /* Different availability, no match. */
594 if (l1 != l2 || h1 != h2)
597 /* Compare the _available_ contents. */
598 if (memcmp (val1->contents + offset1,
599 val2->contents + offset2,
611 /* Prototypes for local functions. */
613 static void show_values (char *, int);
615 static void show_convenience (char *, int);
618 /* The value-history records all the values printed
619 by print commands during this session. Each chunk
620 records 60 consecutive values. The first chunk on
621 the chain records the most recent values.
622 The total number of values is in value_history_count. */
624 #define VALUE_HISTORY_CHUNK 60
626 struct value_history_chunk
628 struct value_history_chunk *next;
629 struct value *values[VALUE_HISTORY_CHUNK];
632 /* Chain of chunks now in use. */
634 static struct value_history_chunk *value_history_chain;
636 static int value_history_count; /* Abs number of last entry stored. */
639 /* List of all value objects currently allocated
640 (except for those released by calls to release_value)
641 This is so they can be freed after each command. */
643 static struct value *all_values;
645 /* Allocate a lazy value for type TYPE. Its actual content is
646 "lazily" allocated too: the content field of the return value is
647 NULL; it will be allocated when it is fetched from the target. */
650 allocate_value_lazy (struct type *type)
654 /* Call check_typedef on our type to make sure that, if TYPE
655 is a TYPE_CODE_TYPEDEF, its length is set to the length
656 of the target type instead of zero. However, we do not
657 replace the typedef type by the target type, because we want
658 to keep the typedef in order to be able to set the VAL's type
659 description correctly. */
660 check_typedef (type);
662 val = (struct value *) xzalloc (sizeof (struct value));
663 val->contents = NULL;
664 val->next = all_values;
667 val->enclosing_type = type;
668 VALUE_LVAL (val) = not_lval;
669 val->location.address = 0;
670 VALUE_FRAME_ID (val) = null_frame_id;
674 VALUE_REGNUM (val) = -1;
676 val->optimized_out = 0;
677 val->embedded_offset = 0;
678 val->pointed_to_offset = 0;
680 val->initialized = 1; /* Default to initialized. */
682 /* Values start out on the all_values chain. */
683 val->reference_count = 1;
688 /* Allocate the contents of VAL if it has not been allocated yet. */
691 allocate_value_contents (struct value *val)
694 val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
697 /* Allocate a value and its contents for type TYPE. */
700 allocate_value (struct type *type)
702 struct value *val = allocate_value_lazy (type);
704 allocate_value_contents (val);
709 /* Allocate a value that has the correct length
710 for COUNT repetitions of type TYPE. */
713 allocate_repeat_value (struct type *type, int count)
715 int low_bound = current_language->string_lower_bound; /* ??? */
716 /* FIXME-type-allocation: need a way to free this type when we are
718 struct type *array_type
719 = lookup_array_range_type (type, low_bound, count + low_bound - 1);
721 return allocate_value (array_type);
725 allocate_computed_value (struct type *type,
726 const struct lval_funcs *funcs,
729 struct value *v = allocate_value_lazy (type);
731 VALUE_LVAL (v) = lval_computed;
732 v->location.computed.funcs = funcs;
733 v->location.computed.closure = closure;
738 /* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */
741 allocate_optimized_out_value (struct type *type)
743 struct value *retval = allocate_value_lazy (type);
745 set_value_optimized_out (retval, 1);
750 /* Accessor methods. */
753 value_next (struct value *value)
759 value_type (const struct value *value)
764 deprecated_set_value_type (struct value *value, struct type *type)
770 value_offset (const struct value *value)
772 return value->offset;
775 set_value_offset (struct value *value, int offset)
777 value->offset = offset;
781 value_bitpos (const struct value *value)
783 return value->bitpos;
786 set_value_bitpos (struct value *value, int bit)
792 value_bitsize (const struct value *value)
794 return value->bitsize;
797 set_value_bitsize (struct value *value, int bit)
799 value->bitsize = bit;
803 value_parent (struct value *value)
805 return value->parent;
809 value_contents_raw (struct value *value)
811 allocate_value_contents (value);
812 return value->contents + value->embedded_offset;
816 value_contents_all_raw (struct value *value)
818 allocate_value_contents (value);
819 return value->contents;
823 value_enclosing_type (struct value *value)
825 return value->enclosing_type;
829 require_not_optimized_out (const struct value *value)
831 if (value->optimized_out)
832 error (_("value has been optimized out"));
836 require_available (const struct value *value)
838 if (!VEC_empty (range_s, value->unavailable))
839 throw_error (NOT_AVAILABLE_ERROR, _("value is not available"));
843 value_contents_for_printing (struct value *value)
846 value_fetch_lazy (value);
847 return value->contents;
851 value_contents_for_printing_const (const struct value *value)
853 gdb_assert (!value->lazy);
854 return value->contents;
858 value_contents_all (struct value *value)
860 const gdb_byte *result = value_contents_for_printing (value);
861 require_not_optimized_out (value);
862 require_available (value);
866 /* Copy LENGTH bytes of SRC value's (all) contents
867 (value_contents_all) starting at SRC_OFFSET, into DST value's (all)
868 contents, starting at DST_OFFSET. If unavailable contents are
869 being copied from SRC, the corresponding DST contents are marked
870 unavailable accordingly. Neither DST nor SRC may be lazy
873 It is assumed the contents of DST in the [DST_OFFSET,
874 DST_OFFSET+LENGTH) range are wholly available. */
877 value_contents_copy_raw (struct value *dst, int dst_offset,
878 struct value *src, int src_offset, int length)
883 /* A lazy DST would make that this copy operation useless, since as
884 soon as DST's contents were un-lazied (by a later value_contents
885 call, say), the contents would be overwritten. A lazy SRC would
886 mean we'd be copying garbage. */
887 gdb_assert (!dst->lazy && !src->lazy);
889 /* The overwritten DST range gets unavailability ORed in, not
890 replaced. Make sure to remember to implement replacing if it
891 turns out actually necessary. */
892 gdb_assert (value_bytes_available (dst, dst_offset, length));
895 memcpy (value_contents_all_raw (dst) + dst_offset,
896 value_contents_all_raw (src) + src_offset,
899 /* Copy the meta-data, adjusted. */
900 for (i = 0; VEC_iterate (range_s, src->unavailable, i, r); i++)
904 l = max (r->offset, src_offset);
905 h = min (r->offset + r->length, src_offset + length);
908 mark_value_bytes_unavailable (dst,
909 dst_offset + (l - src_offset),
914 /* Copy LENGTH bytes of SRC value's (all) contents
915 (value_contents_all) starting at SRC_OFFSET byte, into DST value's
916 (all) contents, starting at DST_OFFSET. If unavailable contents
917 are being copied from SRC, the corresponding DST contents are
918 marked unavailable accordingly. DST must not be lazy. If SRC is
919 lazy, it will be fetched now. If SRC is not valid (is optimized
920 out), an error is thrown.
922 It is assumed the contents of DST in the [DST_OFFSET,
923 DST_OFFSET+LENGTH) range are wholly available. */
926 value_contents_copy (struct value *dst, int dst_offset,
927 struct value *src, int src_offset, int length)
929 require_not_optimized_out (src);
932 value_fetch_lazy (src);
934 value_contents_copy_raw (dst, dst_offset, src, src_offset, length);
938 value_lazy (struct value *value)
944 set_value_lazy (struct value *value, int val)
950 value_stack (struct value *value)
956 set_value_stack (struct value *value, int val)
962 value_contents (struct value *value)
964 const gdb_byte *result = value_contents_writeable (value);
965 require_not_optimized_out (value);
966 require_available (value);
971 value_contents_writeable (struct value *value)
974 value_fetch_lazy (value);
975 return value_contents_raw (value);
978 /* Return non-zero if VAL1 and VAL2 have the same contents. Note that
979 this function is different from value_equal; in C the operator ==
980 can return 0 even if the two values being compared are equal. */
983 value_contents_equal (struct value *val1, struct value *val2)
989 type1 = check_typedef (value_type (val1));
990 type2 = check_typedef (value_type (val2));
991 len = TYPE_LENGTH (type1);
992 if (len != TYPE_LENGTH (type2))
995 return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
999 value_optimized_out (struct value *value)
1001 return value->optimized_out;
1005 set_value_optimized_out (struct value *value, int val)
1007 value->optimized_out = val;
1011 value_entirely_optimized_out (const struct value *value)
1013 if (!value->optimized_out)
1015 if (value->lval != lval_computed
1016 || !value->location.computed.funcs->check_any_valid)
1018 return !value->location.computed.funcs->check_any_valid (value);
1022 value_bits_valid (const struct value *value, int offset, int length)
1024 if (!value->optimized_out)
1026 if (value->lval != lval_computed
1027 || !value->location.computed.funcs->check_validity)
1029 return value->location.computed.funcs->check_validity (value, offset,
1034 value_bits_synthetic_pointer (const struct value *value,
1035 int offset, int length)
1037 if (value->lval != lval_computed
1038 || !value->location.computed.funcs->check_synthetic_pointer)
1040 return value->location.computed.funcs->check_synthetic_pointer (value,
1046 value_embedded_offset (struct value *value)
1048 return value->embedded_offset;
1052 set_value_embedded_offset (struct value *value, int val)
1054 value->embedded_offset = val;
1058 value_pointed_to_offset (struct value *value)
1060 return value->pointed_to_offset;
1064 set_value_pointed_to_offset (struct value *value, int val)
1066 value->pointed_to_offset = val;
1069 const struct lval_funcs *
1070 value_computed_funcs (const struct value *v)
1072 gdb_assert (value_lval_const (v) == lval_computed);
1074 return v->location.computed.funcs;
1078 value_computed_closure (const struct value *v)
1080 gdb_assert (v->lval == lval_computed);
1082 return v->location.computed.closure;
1086 deprecated_value_lval_hack (struct value *value)
1088 return &value->lval;
1092 value_lval_const (const struct value *value)
1098 value_address (const struct value *value)
1100 if (value->lval == lval_internalvar
1101 || value->lval == lval_internalvar_component)
1103 return value->location.address + value->offset;
1107 value_raw_address (struct value *value)
1109 if (value->lval == lval_internalvar
1110 || value->lval == lval_internalvar_component)
1112 return value->location.address;
1116 set_value_address (struct value *value, CORE_ADDR addr)
1118 gdb_assert (value->lval != lval_internalvar
1119 && value->lval != lval_internalvar_component);
1120 value->location.address = addr;
1123 struct internalvar **
1124 deprecated_value_internalvar_hack (struct value *value)
1126 return &value->location.internalvar;
1130 deprecated_value_frame_id_hack (struct value *value)
1132 return &value->frame_id;
1136 deprecated_value_regnum_hack (struct value *value)
1138 return &value->regnum;
1142 deprecated_value_modifiable (struct value *value)
1144 return value->modifiable;
1147 deprecated_set_value_modifiable (struct value *value, int modifiable)
1149 value->modifiable = modifiable;
1152 /* Return a mark in the value chain. All values allocated after the
1153 mark is obtained (except for those released) are subject to being freed
1154 if a subsequent value_free_to_mark is passed the mark. */
1161 /* Take a reference to VAL. VAL will not be deallocated until all
1162 references are released. */
1165 value_incref (struct value *val)
1167 val->reference_count++;
1170 /* Release a reference to VAL, which was acquired with value_incref.
1171 This function is also called to deallocate values from the value
1175 value_free (struct value *val)
1179 gdb_assert (val->reference_count > 0);
1180 val->reference_count--;
1181 if (val->reference_count > 0)
1184 /* If there's an associated parent value, drop our reference to
1186 if (val->parent != NULL)
1187 value_free (val->parent);
1189 if (VALUE_LVAL (val) == lval_computed)
1191 const struct lval_funcs *funcs = val->location.computed.funcs;
1193 if (funcs->free_closure)
1194 funcs->free_closure (val);
1197 xfree (val->contents);
1198 VEC_free (range_s, val->unavailable);
1203 /* Free all values allocated since MARK was obtained by value_mark
1204 (except for those released). */
1206 value_free_to_mark (struct value *mark)
1211 for (val = all_values; val && val != mark; val = next)
1220 /* Free all the values that have been allocated (except for those released).
1221 Call after each command, successful or not.
1222 In practice this is called before each command, which is sufficient. */
1225 free_all_values (void)
1230 for (val = all_values; val; val = next)
1240 /* Frees all the elements in a chain of values. */
1243 free_value_chain (struct value *v)
1249 next = value_next (v);
1254 /* Remove VAL from the chain all_values
1255 so it will not be freed automatically. */
1258 release_value (struct value *val)
1262 if (all_values == val)
1264 all_values = val->next;
1270 for (v = all_values; v; v = v->next)
1274 v->next = val->next;
1282 /* If the value is not already released, release it.
1283 If the value is already released, increment its reference count.
1284 That is, this function ensures that the value is released from the
1285 value chain and that the caller owns a reference to it. */
1288 release_value_or_incref (struct value *val)
1293 release_value (val);
1296 /* Release all values up to mark */
1298 value_release_to_mark (struct value *mark)
1303 for (val = next = all_values; next; next = next->next)
1305 if (next->next == mark)
1307 all_values = next->next;
1317 /* Return a copy of the value ARG.
1318 It contains the same contents, for same memory address,
1319 but it's a different block of storage. */
1322 value_copy (struct value *arg)
1324 struct type *encl_type = value_enclosing_type (arg);
1327 if (value_lazy (arg))
1328 val = allocate_value_lazy (encl_type);
1330 val = allocate_value (encl_type);
1331 val->type = arg->type;
1332 VALUE_LVAL (val) = VALUE_LVAL (arg);
1333 val->location = arg->location;
1334 val->offset = arg->offset;
1335 val->bitpos = arg->bitpos;
1336 val->bitsize = arg->bitsize;
1337 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
1338 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
1339 val->lazy = arg->lazy;
1340 val->optimized_out = arg->optimized_out;
1341 val->embedded_offset = value_embedded_offset (arg);
1342 val->pointed_to_offset = arg->pointed_to_offset;
1343 val->modifiable = arg->modifiable;
1344 if (!value_lazy (val))
1346 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
1347 TYPE_LENGTH (value_enclosing_type (arg)));
1350 val->unavailable = VEC_copy (range_s, arg->unavailable);
1351 val->parent = arg->parent;
1353 value_incref (val->parent);
1354 if (VALUE_LVAL (val) == lval_computed)
1356 const struct lval_funcs *funcs = val->location.computed.funcs;
1358 if (funcs->copy_closure)
1359 val->location.computed.closure = funcs->copy_closure (val);
1364 /* Return a version of ARG that is non-lvalue. */
1367 value_non_lval (struct value *arg)
1369 if (VALUE_LVAL (arg) != not_lval)
1371 struct type *enc_type = value_enclosing_type (arg);
1372 struct value *val = allocate_value (enc_type);
1374 memcpy (value_contents_all_raw (val), value_contents_all (arg),
1375 TYPE_LENGTH (enc_type));
1376 val->type = arg->type;
1377 set_value_embedded_offset (val, value_embedded_offset (arg));
1378 set_value_pointed_to_offset (val, value_pointed_to_offset (arg));
1385 set_value_component_location (struct value *component,
1386 const struct value *whole)
1388 if (whole->lval == lval_internalvar)
1389 VALUE_LVAL (component) = lval_internalvar_component;
1391 VALUE_LVAL (component) = whole->lval;
1393 component->location = whole->location;
1394 if (whole->lval == lval_computed)
1396 const struct lval_funcs *funcs = whole->location.computed.funcs;
1398 if (funcs->copy_closure)
1399 component->location.computed.closure = funcs->copy_closure (whole);
1404 /* Access to the value history. */
1406 /* Record a new value in the value history.
1407 Returns the absolute history index of the entry.
1408 Result of -1 indicates the value was not saved; otherwise it is the
1409 value history index of this new item. */
1412 record_latest_value (struct value *val)
1416 /* We don't want this value to have anything to do with the inferior anymore.
1417 In particular, "set $1 = 50" should not affect the variable from which
1418 the value was taken, and fast watchpoints should be able to assume that
1419 a value on the value history never changes. */
1420 if (value_lazy (val))
1421 value_fetch_lazy (val);
1422 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
1423 from. This is a bit dubious, because then *&$1 does not just return $1
1424 but the current contents of that location. c'est la vie... */
1425 val->modifiable = 0;
1426 release_value (val);
1428 /* Here we treat value_history_count as origin-zero
1429 and applying to the value being stored now. */
1431 i = value_history_count % VALUE_HISTORY_CHUNK;
1434 struct value_history_chunk *new
1435 = (struct value_history_chunk *)
1437 xmalloc (sizeof (struct value_history_chunk));
1438 memset (new->values, 0, sizeof new->values);
1439 new->next = value_history_chain;
1440 value_history_chain = new;
1443 value_history_chain->values[i] = val;
1445 /* Now we regard value_history_count as origin-one
1446 and applying to the value just stored. */
1448 return ++value_history_count;
1451 /* Return a copy of the value in the history with sequence number NUM. */
1454 access_value_history (int num)
1456 struct value_history_chunk *chunk;
1461 absnum += value_history_count;
1466 error (_("The history is empty."));
1468 error (_("There is only one value in the history."));
1470 error (_("History does not go back to $$%d."), -num);
1472 if (absnum > value_history_count)
1473 error (_("History has not yet reached $%d."), absnum);
1477 /* Now absnum is always absolute and origin zero. */
1479 chunk = value_history_chain;
1480 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK
1481 - absnum / VALUE_HISTORY_CHUNK;
1483 chunk = chunk->next;
1485 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
1489 show_values (char *num_exp, int from_tty)
1497 /* "show values +" should print from the stored position.
1498 "show values <exp>" should print around value number <exp>. */
1499 if (num_exp[0] != '+' || num_exp[1] != '\0')
1500 num = parse_and_eval_long (num_exp) - 5;
1504 /* "show values" means print the last 10 values. */
1505 num = value_history_count - 9;
1511 for (i = num; i < num + 10 && i <= value_history_count; i++)
1513 struct value_print_options opts;
1515 val = access_value_history (i);
1516 printf_filtered (("$%d = "), i);
1517 get_user_print_options (&opts);
1518 value_print (val, gdb_stdout, &opts);
1519 printf_filtered (("\n"));
1522 /* The next "show values +" should start after what we just printed. */
1525 /* Hitting just return after this command should do the same thing as
1526 "show values +". If num_exp is null, this is unnecessary, since
1527 "show values +" is not useful after "show values". */
1528 if (from_tty && num_exp)
1535 /* Internal variables. These are variables within the debugger
1536 that hold values assigned by debugger commands.
1537 The user refers to them with a '$' prefix
1538 that does not appear in the variable names stored internally. */
1542 struct internalvar *next;
1545 /* We support various different kinds of content of an internal variable.
1546 enum internalvar_kind specifies the kind, and union internalvar_data
1547 provides the data associated with this particular kind. */
1549 enum internalvar_kind
1551 /* The internal variable is empty. */
1554 /* The value of the internal variable is provided directly as
1555 a GDB value object. */
1558 /* A fresh value is computed via a call-back routine on every
1559 access to the internal variable. */
1560 INTERNALVAR_MAKE_VALUE,
1562 /* The internal variable holds a GDB internal convenience function. */
1563 INTERNALVAR_FUNCTION,
1565 /* The variable holds an integer value. */
1566 INTERNALVAR_INTEGER,
1568 /* The variable holds a GDB-provided string. */
1573 union internalvar_data
1575 /* A value object used with INTERNALVAR_VALUE. */
1576 struct value *value;
1578 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
1579 internalvar_make_value make_value;
1581 /* The internal function used with INTERNALVAR_FUNCTION. */
1584 struct internal_function *function;
1585 /* True if this is the canonical name for the function. */
1589 /* An integer value used with INTERNALVAR_INTEGER. */
1592 /* If type is non-NULL, it will be used as the type to generate
1593 a value for this internal variable. If type is NULL, a default
1594 integer type for the architecture is used. */
1599 /* A string value used with INTERNALVAR_STRING. */
1604 static struct internalvar *internalvars;
1606 /* If the variable does not already exist create it and give it the
1607 value given. If no value is given then the default is zero. */
1609 init_if_undefined_command (char* args, int from_tty)
1611 struct internalvar* intvar;
1613 /* Parse the expression - this is taken from set_command(). */
1614 struct expression *expr = parse_expression (args);
1615 register struct cleanup *old_chain =
1616 make_cleanup (free_current_contents, &expr);
1618 /* Validate the expression.
1619 Was the expression an assignment?
1620 Or even an expression at all? */
1621 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
1622 error (_("Init-if-undefined requires an assignment expression."));
1624 /* Extract the variable from the parsed expression.
1625 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
1626 if (expr->elts[1].opcode != OP_INTERNALVAR)
1627 error (_("The first parameter to init-if-undefined "
1628 "should be a GDB variable."));
1629 intvar = expr->elts[2].internalvar;
1631 /* Only evaluate the expression if the lvalue is void.
1632 This may still fail if the expresssion is invalid. */
1633 if (intvar->kind == INTERNALVAR_VOID)
1634 evaluate_expression (expr);
1636 do_cleanups (old_chain);
1640 /* Look up an internal variable with name NAME. NAME should not
1641 normally include a dollar sign.
1643 If the specified internal variable does not exist,
1644 the return value is NULL. */
1646 struct internalvar *
1647 lookup_only_internalvar (const char *name)
1649 struct internalvar *var;
1651 for (var = internalvars; var; var = var->next)
1652 if (strcmp (var->name, name) == 0)
1659 /* Create an internal variable with name NAME and with a void value.
1660 NAME should not normally include a dollar sign. */
1662 struct internalvar *
1663 create_internalvar (const char *name)
1665 struct internalvar *var;
1667 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
1668 var->name = concat (name, (char *)NULL);
1669 var->kind = INTERNALVAR_VOID;
1670 var->next = internalvars;
1675 /* Create an internal variable with name NAME and register FUN as the
1676 function that value_of_internalvar uses to create a value whenever
1677 this variable is referenced. NAME should not normally include a
1680 struct internalvar *
1681 create_internalvar_type_lazy (char *name, internalvar_make_value fun)
1683 struct internalvar *var = create_internalvar (name);
1685 var->kind = INTERNALVAR_MAKE_VALUE;
1686 var->u.make_value = fun;
1690 /* Look up an internal variable with name NAME. NAME should not
1691 normally include a dollar sign.
1693 If the specified internal variable does not exist,
1694 one is created, with a void value. */
1696 struct internalvar *
1697 lookup_internalvar (const char *name)
1699 struct internalvar *var;
1701 var = lookup_only_internalvar (name);
1705 return create_internalvar (name);
1708 /* Return current value of internal variable VAR. For variables that
1709 are not inherently typed, use a value type appropriate for GDBARCH. */
1712 value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
1715 struct trace_state_variable *tsv;
1717 /* If there is a trace state variable of the same name, assume that
1718 is what we really want to see. */
1719 tsv = find_trace_state_variable (var->name);
1722 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
1724 if (tsv->value_known)
1725 val = value_from_longest (builtin_type (gdbarch)->builtin_int64,
1728 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1734 case INTERNALVAR_VOID:
1735 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1738 case INTERNALVAR_FUNCTION:
1739 val = allocate_value (builtin_type (gdbarch)->internal_fn);
1742 case INTERNALVAR_INTEGER:
1743 if (!var->u.integer.type)
1744 val = value_from_longest (builtin_type (gdbarch)->builtin_int,
1745 var->u.integer.val);
1747 val = value_from_longest (var->u.integer.type, var->u.integer.val);
1750 case INTERNALVAR_STRING:
1751 val = value_cstring (var->u.string, strlen (var->u.string),
1752 builtin_type (gdbarch)->builtin_char);
1755 case INTERNALVAR_VALUE:
1756 val = value_copy (var->u.value);
1757 if (value_lazy (val))
1758 value_fetch_lazy (val);
1761 case INTERNALVAR_MAKE_VALUE:
1762 val = (*var->u.make_value) (gdbarch, var);
1766 internal_error (__FILE__, __LINE__, _("bad kind"));
1769 /* Change the VALUE_LVAL to lval_internalvar so that future operations
1770 on this value go back to affect the original internal variable.
1772 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
1773 no underlying modifyable state in the internal variable.
1775 Likewise, if the variable's value is a computed lvalue, we want
1776 references to it to produce another computed lvalue, where
1777 references and assignments actually operate through the
1778 computed value's functions.
1780 This means that internal variables with computed values
1781 behave a little differently from other internal variables:
1782 assignments to them don't just replace the previous value
1783 altogether. At the moment, this seems like the behavior we
1786 if (var->kind != INTERNALVAR_MAKE_VALUE
1787 && val->lval != lval_computed)
1789 VALUE_LVAL (val) = lval_internalvar;
1790 VALUE_INTERNALVAR (val) = var;
1797 get_internalvar_integer (struct internalvar *var, LONGEST *result)
1799 if (var->kind == INTERNALVAR_INTEGER)
1801 *result = var->u.integer.val;
1805 if (var->kind == INTERNALVAR_VALUE)
1807 struct type *type = check_typedef (value_type (var->u.value));
1809 if (TYPE_CODE (type) == TYPE_CODE_INT)
1811 *result = value_as_long (var->u.value);
1820 get_internalvar_function (struct internalvar *var,
1821 struct internal_function **result)
1825 case INTERNALVAR_FUNCTION:
1826 *result = var->u.fn.function;
1835 set_internalvar_component (struct internalvar *var, int offset, int bitpos,
1836 int bitsize, struct value *newval)
1842 case INTERNALVAR_VALUE:
1843 addr = value_contents_writeable (var->u.value);
1846 modify_field (value_type (var->u.value), addr + offset,
1847 value_as_long (newval), bitpos, bitsize);
1849 memcpy (addr + offset, value_contents (newval),
1850 TYPE_LENGTH (value_type (newval)));
1854 /* We can never get a component of any other kind. */
1855 internal_error (__FILE__, __LINE__, _("set_internalvar_component"));
1860 set_internalvar (struct internalvar *var, struct value *val)
1862 enum internalvar_kind new_kind;
1863 union internalvar_data new_data = { 0 };
1865 if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
1866 error (_("Cannot overwrite convenience function %s"), var->name);
1868 /* Prepare new contents. */
1869 switch (TYPE_CODE (check_typedef (value_type (val))))
1871 case TYPE_CODE_VOID:
1872 new_kind = INTERNALVAR_VOID;
1875 case TYPE_CODE_INTERNAL_FUNCTION:
1876 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
1877 new_kind = INTERNALVAR_FUNCTION;
1878 get_internalvar_function (VALUE_INTERNALVAR (val),
1879 &new_data.fn.function);
1880 /* Copies created here are never canonical. */
1884 new_kind = INTERNALVAR_VALUE;
1885 new_data.value = value_copy (val);
1886 new_data.value->modifiable = 1;
1888 /* Force the value to be fetched from the target now, to avoid problems
1889 later when this internalvar is referenced and the target is gone or
1891 if (value_lazy (new_data.value))
1892 value_fetch_lazy (new_data.value);
1894 /* Release the value from the value chain to prevent it from being
1895 deleted by free_all_values. From here on this function should not
1896 call error () until new_data is installed into the var->u to avoid
1898 release_value (new_data.value);
1902 /* Clean up old contents. */
1903 clear_internalvar (var);
1906 var->kind = new_kind;
1908 /* End code which must not call error(). */
1912 set_internalvar_integer (struct internalvar *var, LONGEST l)
1914 /* Clean up old contents. */
1915 clear_internalvar (var);
1917 var->kind = INTERNALVAR_INTEGER;
1918 var->u.integer.type = NULL;
1919 var->u.integer.val = l;
1923 set_internalvar_string (struct internalvar *var, const char *string)
1925 /* Clean up old contents. */
1926 clear_internalvar (var);
1928 var->kind = INTERNALVAR_STRING;
1929 var->u.string = xstrdup (string);
1933 set_internalvar_function (struct internalvar *var, struct internal_function *f)
1935 /* Clean up old contents. */
1936 clear_internalvar (var);
1938 var->kind = INTERNALVAR_FUNCTION;
1939 var->u.fn.function = f;
1940 var->u.fn.canonical = 1;
1941 /* Variables installed here are always the canonical version. */
1945 clear_internalvar (struct internalvar *var)
1947 /* Clean up old contents. */
1950 case INTERNALVAR_VALUE:
1951 value_free (var->u.value);
1954 case INTERNALVAR_STRING:
1955 xfree (var->u.string);
1962 /* Reset to void kind. */
1963 var->kind = INTERNALVAR_VOID;
1967 internalvar_name (struct internalvar *var)
1972 static struct internal_function *
1973 create_internal_function (const char *name,
1974 internal_function_fn handler, void *cookie)
1976 struct internal_function *ifn = XNEW (struct internal_function);
1978 ifn->name = xstrdup (name);
1979 ifn->handler = handler;
1980 ifn->cookie = cookie;
1985 value_internal_function_name (struct value *val)
1987 struct internal_function *ifn;
1990 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
1991 result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
1992 gdb_assert (result);
1998 call_internal_function (struct gdbarch *gdbarch,
1999 const struct language_defn *language,
2000 struct value *func, int argc, struct value **argv)
2002 struct internal_function *ifn;
2005 gdb_assert (VALUE_LVAL (func) == lval_internalvar);
2006 result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
2007 gdb_assert (result);
2009 return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
2012 /* The 'function' command. This does nothing -- it is just a
2013 placeholder to let "help function NAME" work. This is also used as
2014 the implementation of the sub-command that is created when
2015 registering an internal function. */
2017 function_command (char *command, int from_tty)
2022 /* Clean up if an internal function's command is destroyed. */
2024 function_destroyer (struct cmd_list_element *self, void *ignore)
2030 /* Add a new internal function. NAME is the name of the function; DOC
2031 is a documentation string describing the function. HANDLER is
2032 called when the function is invoked. COOKIE is an arbitrary
2033 pointer which is passed to HANDLER and is intended for "user
2036 add_internal_function (const char *name, const char *doc,
2037 internal_function_fn handler, void *cookie)
2039 struct cmd_list_element *cmd;
2040 struct internal_function *ifn;
2041 struct internalvar *var = lookup_internalvar (name);
2043 ifn = create_internal_function (name, handler, cookie);
2044 set_internalvar_function (var, ifn);
2046 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
2048 cmd->destroyer = function_destroyer;
2051 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
2052 prevent cycles / duplicates. */
2055 preserve_one_value (struct value *value, struct objfile *objfile,
2056 htab_t copied_types)
2058 if (TYPE_OBJFILE (value->type) == objfile)
2059 value->type = copy_type_recursive (objfile, value->type, copied_types);
2061 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
2062 value->enclosing_type = copy_type_recursive (objfile,
2063 value->enclosing_type,
2067 /* Likewise for internal variable VAR. */
2070 preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
2071 htab_t copied_types)
2075 case INTERNALVAR_INTEGER:
2076 if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
2078 = copy_type_recursive (objfile, var->u.integer.type, copied_types);
2081 case INTERNALVAR_VALUE:
2082 preserve_one_value (var->u.value, objfile, copied_types);
2087 /* Update the internal variables and value history when OBJFILE is
2088 discarded; we must copy the types out of the objfile. New global types
2089 will be created for every convenience variable which currently points to
2090 this objfile's types, and the convenience variables will be adjusted to
2091 use the new global types. */
2094 preserve_values (struct objfile *objfile)
2096 htab_t copied_types;
2097 struct value_history_chunk *cur;
2098 struct internalvar *var;
2101 /* Create the hash table. We allocate on the objfile's obstack, since
2102 it is soon to be deleted. */
2103 copied_types = create_copied_types_hash (objfile);
2105 for (cur = value_history_chain; cur; cur = cur->next)
2106 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
2108 preserve_one_value (cur->values[i], objfile, copied_types);
2110 for (var = internalvars; var; var = var->next)
2111 preserve_one_internalvar (var, objfile, copied_types);
2113 preserve_python_values (objfile, copied_types);
2115 htab_delete (copied_types);
2119 show_convenience (char *ignore, int from_tty)
2121 struct gdbarch *gdbarch = get_current_arch ();
2122 struct internalvar *var;
2124 struct value_print_options opts;
2126 get_user_print_options (&opts);
2127 for (var = internalvars; var; var = var->next)
2129 volatile struct gdb_exception ex;
2135 printf_filtered (("$%s = "), var->name);
2137 TRY_CATCH (ex, RETURN_MASK_ERROR)
2141 val = value_of_internalvar (gdbarch, var);
2142 value_print (val, gdb_stdout, &opts);
2145 fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
2146 printf_filtered (("\n"));
2149 printf_unfiltered (_("No debugger convenience variables now defined.\n"
2150 "Convenience variables have "
2151 "names starting with \"$\";\n"
2152 "use \"set\" as in \"set "
2153 "$foo = 5\" to define them.\n"));
2156 /* Extract a value as a C number (either long or double).
2157 Knows how to convert fixed values to double, or
2158 floating values to long.
2159 Does not deallocate the value. */
2162 value_as_long (struct value *val)
2164 /* This coerces arrays and functions, which is necessary (e.g.
2165 in disassemble_command). It also dereferences references, which
2166 I suspect is the most logical thing to do. */
2167 val = coerce_array (val);
2168 return unpack_long (value_type (val), value_contents (val));
2172 value_as_double (struct value *val)
2177 foo = unpack_double (value_type (val), value_contents (val), &inv);
2179 error (_("Invalid floating value found in program."));
2183 /* Extract a value as a C pointer. Does not deallocate the value.
2184 Note that val's type may not actually be a pointer; value_as_long
2185 handles all the cases. */
2187 value_as_address (struct value *val)
2189 struct gdbarch *gdbarch = get_type_arch (value_type (val));
2191 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2192 whether we want this to be true eventually. */
2194 /* gdbarch_addr_bits_remove is wrong if we are being called for a
2195 non-address (e.g. argument to "signal", "info break", etc.), or
2196 for pointers to char, in which the low bits *are* significant. */
2197 return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
2200 /* There are several targets (IA-64, PowerPC, and others) which
2201 don't represent pointers to functions as simply the address of
2202 the function's entry point. For example, on the IA-64, a
2203 function pointer points to a two-word descriptor, generated by
2204 the linker, which contains the function's entry point, and the
2205 value the IA-64 "global pointer" register should have --- to
2206 support position-independent code. The linker generates
2207 descriptors only for those functions whose addresses are taken.
2209 On such targets, it's difficult for GDB to convert an arbitrary
2210 function address into a function pointer; it has to either find
2211 an existing descriptor for that function, or call malloc and
2212 build its own. On some targets, it is impossible for GDB to
2213 build a descriptor at all: the descriptor must contain a jump
2214 instruction; data memory cannot be executed; and code memory
2217 Upon entry to this function, if VAL is a value of type `function'
2218 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
2219 value_address (val) is the address of the function. This is what
2220 you'll get if you evaluate an expression like `main'. The call
2221 to COERCE_ARRAY below actually does all the usual unary
2222 conversions, which includes converting values of type `function'
2223 to `pointer to function'. This is the challenging conversion
2224 discussed above. Then, `unpack_long' will convert that pointer
2225 back into an address.
2227 So, suppose the user types `disassemble foo' on an architecture
2228 with a strange function pointer representation, on which GDB
2229 cannot build its own descriptors, and suppose further that `foo'
2230 has no linker-built descriptor. The address->pointer conversion
2231 will signal an error and prevent the command from running, even
2232 though the next step would have been to convert the pointer
2233 directly back into the same address.
2235 The following shortcut avoids this whole mess. If VAL is a
2236 function, just return its address directly. */
2237 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
2238 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
2239 return value_address (val);
2241 val = coerce_array (val);
2243 /* Some architectures (e.g. Harvard), map instruction and data
2244 addresses onto a single large unified address space. For
2245 instance: An architecture may consider a large integer in the
2246 range 0x10000000 .. 0x1000ffff to already represent a data
2247 addresses (hence not need a pointer to address conversion) while
2248 a small integer would still need to be converted integer to
2249 pointer to address. Just assume such architectures handle all
2250 integer conversions in a single function. */
2254 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
2255 must admonish GDB hackers to make sure its behavior matches the
2256 compiler's, whenever possible.
2258 In general, I think GDB should evaluate expressions the same way
2259 the compiler does. When the user copies an expression out of
2260 their source code and hands it to a `print' command, they should
2261 get the same value the compiler would have computed. Any
2262 deviation from this rule can cause major confusion and annoyance,
2263 and needs to be justified carefully. In other words, GDB doesn't
2264 really have the freedom to do these conversions in clever and
2267 AndrewC pointed out that users aren't complaining about how GDB
2268 casts integers to pointers; they are complaining that they can't
2269 take an address from a disassembly listing and give it to `x/i'.
2270 This is certainly important.
2272 Adding an architecture method like integer_to_address() certainly
2273 makes it possible for GDB to "get it right" in all circumstances
2274 --- the target has complete control over how things get done, so
2275 people can Do The Right Thing for their target without breaking
2276 anyone else. The standard doesn't specify how integers get
2277 converted to pointers; usually, the ABI doesn't either, but
2278 ABI-specific code is a more reasonable place to handle it. */
2280 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
2281 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
2282 && gdbarch_integer_to_address_p (gdbarch))
2283 return gdbarch_integer_to_address (gdbarch, value_type (val),
2284 value_contents (val));
2286 return unpack_long (value_type (val), value_contents (val));
2290 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2291 as a long, or as a double, assuming the raw data is described
2292 by type TYPE. Knows how to convert different sizes of values
2293 and can convert between fixed and floating point. We don't assume
2294 any alignment for the raw data. Return value is in host byte order.
2296 If you want functions and arrays to be coerced to pointers, and
2297 references to be dereferenced, call value_as_long() instead.
2299 C++: It is assumed that the front-end has taken care of
2300 all matters concerning pointers to members. A pointer
2301 to member which reaches here is considered to be equivalent
2302 to an INT (or some size). After all, it is only an offset. */
2305 unpack_long (struct type *type, const gdb_byte *valaddr)
2307 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2308 enum type_code code = TYPE_CODE (type);
2309 int len = TYPE_LENGTH (type);
2310 int nosign = TYPE_UNSIGNED (type);
2314 case TYPE_CODE_TYPEDEF:
2315 return unpack_long (check_typedef (type), valaddr);
2316 case TYPE_CODE_ENUM:
2317 case TYPE_CODE_FLAGS:
2318 case TYPE_CODE_BOOL:
2320 case TYPE_CODE_CHAR:
2321 case TYPE_CODE_RANGE:
2322 case TYPE_CODE_MEMBERPTR:
2324 return extract_unsigned_integer (valaddr, len, byte_order);
2326 return extract_signed_integer (valaddr, len, byte_order);
2329 return extract_typed_floating (valaddr, type);
2331 case TYPE_CODE_DECFLOAT:
2332 /* libdecnumber has a function to convert from decimal to integer, but
2333 it doesn't work when the decimal number has a fractional part. */
2334 return decimal_to_doublest (valaddr, len, byte_order);
2338 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2339 whether we want this to be true eventually. */
2340 return extract_typed_address (valaddr, type);
2343 error (_("Value can't be converted to integer."));
2345 return 0; /* Placate lint. */
2348 /* Return a double value from the specified type and address.
2349 INVP points to an int which is set to 0 for valid value,
2350 1 for invalid value (bad float format). In either case,
2351 the returned double is OK to use. Argument is in target
2352 format, result is in host format. */
2355 unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
2357 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2358 enum type_code code;
2362 *invp = 0; /* Assume valid. */
2363 CHECK_TYPEDEF (type);
2364 code = TYPE_CODE (type);
2365 len = TYPE_LENGTH (type);
2366 nosign = TYPE_UNSIGNED (type);
2367 if (code == TYPE_CODE_FLT)
2369 /* NOTE: cagney/2002-02-19: There was a test here to see if the
2370 floating-point value was valid (using the macro
2371 INVALID_FLOAT). That test/macro have been removed.
2373 It turns out that only the VAX defined this macro and then
2374 only in a non-portable way. Fixing the portability problem
2375 wouldn't help since the VAX floating-point code is also badly
2376 bit-rotten. The target needs to add definitions for the
2377 methods gdbarch_float_format and gdbarch_double_format - these
2378 exactly describe the target floating-point format. The
2379 problem here is that the corresponding floatformat_vax_f and
2380 floatformat_vax_d values these methods should be set to are
2381 also not defined either. Oops!
2383 Hopefully someone will add both the missing floatformat
2384 definitions and the new cases for floatformat_is_valid (). */
2386 if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
2392 return extract_typed_floating (valaddr, type);
2394 else if (code == TYPE_CODE_DECFLOAT)
2395 return decimal_to_doublest (valaddr, len, byte_order);
2398 /* Unsigned -- be sure we compensate for signed LONGEST. */
2399 return (ULONGEST) unpack_long (type, valaddr);
2403 /* Signed -- we are OK with unpack_long. */
2404 return unpack_long (type, valaddr);
2408 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2409 as a CORE_ADDR, assuming the raw data is described by type TYPE.
2410 We don't assume any alignment for the raw data. Return value is in
2413 If you want functions and arrays to be coerced to pointers, and
2414 references to be dereferenced, call value_as_address() instead.
2416 C++: It is assumed that the front-end has taken care of
2417 all matters concerning pointers to members. A pointer
2418 to member which reaches here is considered to be equivalent
2419 to an INT (or some size). After all, it is only an offset. */
2422 unpack_pointer (struct type *type, const gdb_byte *valaddr)
2424 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2425 whether we want this to be true eventually. */
2426 return unpack_long (type, valaddr);
2430 /* Get the value of the FIELDNO'th field (which must be static) of
2431 TYPE. Return NULL if the field doesn't exist or has been
2435 value_static_field (struct type *type, int fieldno)
2437 struct value *retval;
2439 switch (TYPE_FIELD_LOC_KIND (type, fieldno))
2441 case FIELD_LOC_KIND_PHYSADDR:
2442 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2443 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
2445 case FIELD_LOC_KIND_PHYSNAME:
2447 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
2448 /* TYPE_FIELD_NAME (type, fieldno); */
2449 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
2453 /* With some compilers, e.g. HP aCC, static data members are
2454 reported as non-debuggable symbols. */
2455 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name,
2462 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2463 SYMBOL_VALUE_ADDRESS (msym));
2467 retval = value_of_variable (sym, NULL);
2471 gdb_assert_not_reached ("unexpected field location kind");
2477 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
2478 You have to be careful here, since the size of the data area for the value
2479 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
2480 than the old enclosing type, you have to allocate more space for the
2484 set_value_enclosing_type (struct value *val, struct type *new_encl_type)
2486 if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
2488 (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
2490 val->enclosing_type = new_encl_type;
2493 /* Given a value ARG1 (offset by OFFSET bytes)
2494 of a struct or union type ARG_TYPE,
2495 extract and return the value of one of its (non-static) fields.
2496 FIELDNO says which field. */
2499 value_primitive_field (struct value *arg1, int offset,
2500 int fieldno, struct type *arg_type)
2505 CHECK_TYPEDEF (arg_type);
2506 type = TYPE_FIELD_TYPE (arg_type, fieldno);
2508 /* Call check_typedef on our type to make sure that, if TYPE
2509 is a TYPE_CODE_TYPEDEF, its length is set to the length
2510 of the target type instead of zero. However, we do not
2511 replace the typedef type by the target type, because we want
2512 to keep the typedef in order to be able to print the type
2513 description correctly. */
2514 check_typedef (type);
2516 if (value_optimized_out (arg1))
2517 v = allocate_optimized_out_value (type);
2518 else if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
2520 /* Handle packed fields.
2522 Create a new value for the bitfield, with bitpos and bitsize
2523 set. If possible, arrange offset and bitpos so that we can
2524 do a single aligned read of the size of the containing type.
2525 Otherwise, adjust offset to the byte containing the first
2526 bit. Assume that the address, offset, and embedded offset
2527 are sufficiently aligned. */
2529 int bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
2530 int container_bitsize = TYPE_LENGTH (type) * 8;
2532 v = allocate_value_lazy (type);
2533 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
2534 if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
2535 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST))
2536 v->bitpos = bitpos % container_bitsize;
2538 v->bitpos = bitpos % 8;
2539 v->offset = (value_embedded_offset (arg1)
2541 + (bitpos - v->bitpos) / 8);
2543 value_incref (v->parent);
2544 if (!value_lazy (arg1))
2545 value_fetch_lazy (v);
2547 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
2549 /* This field is actually a base subobject, so preserve the
2550 entire object's contents for later references to virtual
2553 /* Lazy register values with offsets are not supported. */
2554 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
2555 value_fetch_lazy (arg1);
2557 if (value_lazy (arg1))
2558 v = allocate_value_lazy (value_enclosing_type (arg1));
2561 v = allocate_value (value_enclosing_type (arg1));
2562 value_contents_copy_raw (v, 0, arg1, 0,
2563 TYPE_LENGTH (value_enclosing_type (arg1)));
2566 v->offset = value_offset (arg1);
2567 v->embedded_offset = (offset + value_embedded_offset (arg1)
2568 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
2572 /* Plain old data member */
2573 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
2575 /* Lazy register values with offsets are not supported. */
2576 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
2577 value_fetch_lazy (arg1);
2579 if (value_lazy (arg1))
2580 v = allocate_value_lazy (type);
2583 v = allocate_value (type);
2584 value_contents_copy_raw (v, value_embedded_offset (v),
2585 arg1, value_embedded_offset (arg1) + offset,
2586 TYPE_LENGTH (type));
2588 v->offset = (value_offset (arg1) + offset
2589 + value_embedded_offset (arg1));
2591 set_value_component_location (v, arg1);
2592 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
2593 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
2597 /* Given a value ARG1 of a struct or union type,
2598 extract and return the value of one of its (non-static) fields.
2599 FIELDNO says which field. */
2602 value_field (struct value *arg1, int fieldno)
2604 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
2607 /* Return a non-virtual function as a value.
2608 F is the list of member functions which contains the desired method.
2609 J is an index into F which provides the desired method.
2611 We only use the symbol for its address, so be happy with either a
2612 full symbol or a minimal symbol. */
2615 value_fn_field (struct value **arg1p, struct fn_field *f,
2616 int j, struct type *type,
2620 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
2621 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
2623 struct minimal_symbol *msym;
2625 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
2632 gdb_assert (sym == NULL);
2633 msym = lookup_minimal_symbol (physname, NULL, NULL);
2638 v = allocate_value (ftype);
2641 set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
2645 /* The minimal symbol might point to a function descriptor;
2646 resolve it to the actual code address instead. */
2647 struct objfile *objfile = msymbol_objfile (msym);
2648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2650 set_value_address (v,
2651 gdbarch_convert_from_func_ptr_addr
2652 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), ¤t_target));
2657 if (type != value_type (*arg1p))
2658 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
2659 value_addr (*arg1p)));
2661 /* Move the `this' pointer according to the offset.
2662 VALUE_OFFSET (*arg1p) += offset; */
2670 /* Helper function for both unpack_value_bits_as_long and
2671 unpack_bits_as_long. See those functions for more details on the
2672 interface; the only difference is that this function accepts either
2673 a NULL or a non-NULL ORIGINAL_VALUE. */
2676 unpack_value_bits_as_long_1 (struct type *field_type, const gdb_byte *valaddr,
2677 int embedded_offset, int bitpos, int bitsize,
2678 const struct value *original_value,
2681 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
2688 /* Read the minimum number of bytes required; there may not be
2689 enough bytes to read an entire ULONGEST. */
2690 CHECK_TYPEDEF (field_type);
2692 bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
2694 bytes_read = TYPE_LENGTH (field_type);
2696 read_offset = bitpos / 8;
2698 if (original_value != NULL
2699 && !value_bytes_available (original_value, embedded_offset + read_offset,
2703 val = extract_unsigned_integer (valaddr + embedded_offset + read_offset,
2704 bytes_read, byte_order);
2706 /* Extract bits. See comment above. */
2708 if (gdbarch_bits_big_endian (get_type_arch (field_type)))
2709 lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
2711 lsbcount = (bitpos % 8);
2714 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
2715 If the field is signed, and is negative, then sign extend. */
2717 if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
2719 valmask = (((ULONGEST) 1) << bitsize) - 1;
2721 if (!TYPE_UNSIGNED (field_type))
2723 if (val & (valmask ^ (valmask >> 1)))
2734 /* Unpack a bitfield of the specified FIELD_TYPE, from the object at
2735 VALADDR + EMBEDDED_OFFSET, and store the result in *RESULT.
2736 VALADDR points to the contents of ORIGINAL_VALUE, which must not be
2737 NULL. The bitfield starts at BITPOS bits and contains BITSIZE
2740 Returns false if the value contents are unavailable, otherwise
2741 returns true, indicating a valid value has been stored in *RESULT.
2743 Extracting bits depends on endianness of the machine. Compute the
2744 number of least significant bits to discard. For big endian machines,
2745 we compute the total number of bits in the anonymous object, subtract
2746 off the bit count from the MSB of the object to the MSB of the
2747 bitfield, then the size of the bitfield, which leaves the LSB discard
2748 count. For little endian machines, the discard count is simply the
2749 number of bits from the LSB of the anonymous object to the LSB of the
2752 If the field is signed, we also do sign extension. */
2755 unpack_value_bits_as_long (struct type *field_type, const gdb_byte *valaddr,
2756 int embedded_offset, int bitpos, int bitsize,
2757 const struct value *original_value,
2760 gdb_assert (original_value != NULL);
2762 return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2763 bitpos, bitsize, original_value, result);
2767 /* Unpack a field FIELDNO of the specified TYPE, from the object at
2768 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
2769 ORIGINAL_VALUE. See unpack_value_bits_as_long for more
2773 unpack_value_field_as_long_1 (struct type *type, const gdb_byte *valaddr,
2774 int embedded_offset, int fieldno,
2775 const struct value *val, LONGEST *result)
2777 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
2778 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
2779 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
2781 return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2782 bitpos, bitsize, val,
2786 /* Unpack a field FIELDNO of the specified TYPE, from the object at
2787 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
2788 ORIGINAL_VALUE, which must not be NULL. See
2789 unpack_value_bits_as_long for more details. */
2792 unpack_value_field_as_long (struct type *type, const gdb_byte *valaddr,
2793 int embedded_offset, int fieldno,
2794 const struct value *val, LONGEST *result)
2796 gdb_assert (val != NULL);
2798 return unpack_value_field_as_long_1 (type, valaddr, embedded_offset,
2799 fieldno, val, result);
2802 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous
2803 object at VALADDR. See unpack_value_bits_as_long for more details.
2804 This function differs from unpack_value_field_as_long in that it
2805 operates without a struct value object. */
2808 unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
2812 unpack_value_field_as_long_1 (type, valaddr, 0, fieldno, NULL, &result);
2816 /* Return a new value with type TYPE, which is FIELDNO field of the
2817 object at VALADDR + EMBEDDEDOFFSET. VALADDR points to the contents
2818 of VAL. If the VAL's contents required to extract the bitfield
2819 from are unavailable, the new value is correspondingly marked as
2823 value_field_bitfield (struct type *type, int fieldno,
2824 const gdb_byte *valaddr,
2825 int embedded_offset, const struct value *val)
2829 if (!unpack_value_field_as_long (type, valaddr, embedded_offset, fieldno,
2832 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
2833 struct value *retval = allocate_value (field_type);
2834 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (field_type));
2839 return value_from_longest (TYPE_FIELD_TYPE (type, fieldno), l);
2843 /* Modify the value of a bitfield. ADDR points to a block of memory in
2844 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
2845 is the desired value of the field, in host byte order. BITPOS and BITSIZE
2846 indicate which bits (in target bit order) comprise the bitfield.
2847 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
2848 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
2851 modify_field (struct type *type, gdb_byte *addr,
2852 LONGEST fieldval, int bitpos, int bitsize)
2854 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2856 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
2859 /* Normalize BITPOS. */
2863 /* If a negative fieldval fits in the field in question, chop
2864 off the sign extension bits. */
2865 if ((~fieldval & ~(mask >> 1)) == 0)
2868 /* Warn if value is too big to fit in the field in question. */
2869 if (0 != (fieldval & ~mask))
2871 /* FIXME: would like to include fieldval in the message, but
2872 we don't have a sprintf_longest. */
2873 warning (_("Value does not fit in %d bits."), bitsize);
2875 /* Truncate it, otherwise adjoining fields may be corrupted. */
2879 /* Ensure no bytes outside of the modified ones get accessed as it may cause
2880 false valgrind reports. */
2882 bytesize = (bitpos + bitsize + 7) / 8;
2883 oword = extract_unsigned_integer (addr, bytesize, byte_order);
2885 /* Shifting for bit field depends on endianness of the target machine. */
2886 if (gdbarch_bits_big_endian (get_type_arch (type)))
2887 bitpos = bytesize * 8 - bitpos - bitsize;
2889 oword &= ~(mask << bitpos);
2890 oword |= fieldval << bitpos;
2892 store_unsigned_integer (addr, bytesize, byte_order, oword);
2895 /* Pack NUM into BUF using a target format of TYPE. */
2898 pack_long (gdb_byte *buf, struct type *type, LONGEST num)
2900 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2903 type = check_typedef (type);
2904 len = TYPE_LENGTH (type);
2906 switch (TYPE_CODE (type))
2909 case TYPE_CODE_CHAR:
2910 case TYPE_CODE_ENUM:
2911 case TYPE_CODE_FLAGS:
2912 case TYPE_CODE_BOOL:
2913 case TYPE_CODE_RANGE:
2914 case TYPE_CODE_MEMBERPTR:
2915 store_signed_integer (buf, len, byte_order, num);
2920 store_typed_address (buf, type, (CORE_ADDR) num);
2924 error (_("Unexpected type (%d) encountered for integer constant."),
2930 /* Pack NUM into BUF using a target format of TYPE. */
2933 pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
2936 enum bfd_endian byte_order;
2938 type = check_typedef (type);
2939 len = TYPE_LENGTH (type);
2940 byte_order = gdbarch_byte_order (get_type_arch (type));
2942 switch (TYPE_CODE (type))
2945 case TYPE_CODE_CHAR:
2946 case TYPE_CODE_ENUM:
2947 case TYPE_CODE_FLAGS:
2948 case TYPE_CODE_BOOL:
2949 case TYPE_CODE_RANGE:
2950 case TYPE_CODE_MEMBERPTR:
2951 store_unsigned_integer (buf, len, byte_order, num);
2956 store_typed_address (buf, type, (CORE_ADDR) num);
2960 error (_("Unexpected type (%d) encountered "
2961 "for unsigned integer constant."),
2967 /* Convert C numbers into newly allocated values. */
2970 value_from_longest (struct type *type, LONGEST num)
2972 struct value *val = allocate_value (type);
2974 pack_long (value_contents_raw (val), type, num);
2979 /* Convert C unsigned numbers into newly allocated values. */
2982 value_from_ulongest (struct type *type, ULONGEST num)
2984 struct value *val = allocate_value (type);
2986 pack_unsigned_long (value_contents_raw (val), type, num);
2992 /* Create a value representing a pointer of type TYPE to the address
2995 value_from_pointer (struct type *type, CORE_ADDR addr)
2997 struct value *val = allocate_value (type);
2999 store_typed_address (value_contents_raw (val), check_typedef (type), addr);
3004 /* Create a value of type TYPE whose contents come from VALADDR, if it
3005 is non-null, and whose memory address (in the inferior) is
3009 value_from_contents_and_address (struct type *type,
3010 const gdb_byte *valaddr,
3015 if (valaddr == NULL)
3016 v = allocate_value_lazy (type);
3019 v = allocate_value (type);
3020 memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
3022 set_value_address (v, address);
3023 VALUE_LVAL (v) = lval_memory;
3027 /* Create a value of type TYPE holding the contents CONTENTS.
3028 The new value is `not_lval'. */
3031 value_from_contents (struct type *type, const gdb_byte *contents)
3033 struct value *result;
3035 result = allocate_value (type);
3036 memcpy (value_contents_raw (result), contents, TYPE_LENGTH (type));
3041 value_from_double (struct type *type, DOUBLEST num)
3043 struct value *val = allocate_value (type);
3044 struct type *base_type = check_typedef (type);
3045 enum type_code code = TYPE_CODE (base_type);
3047 if (code == TYPE_CODE_FLT)
3049 store_typed_floating (value_contents_raw (val), base_type, num);
3052 error (_("Unexpected type encountered for floating constant."));
3058 value_from_decfloat (struct type *type, const gdb_byte *dec)
3060 struct value *val = allocate_value (type);
3062 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
3066 /* Extract a value from the history file. Input will be of the form
3067 $digits or $$digits. See block comment above 'write_dollar_variable'
3071 value_from_history_ref (char *h, char **endp)
3083 /* Find length of numeral string. */
3084 for (; isdigit (h[len]); len++)
3087 /* Make sure numeral string is not part of an identifier. */
3088 if (h[len] == '_' || isalpha (h[len]))
3091 /* Now collect the index value. */
3096 /* For some bizarre reason, "$$" is equivalent to "$$1",
3097 rather than to "$$0" as it ought to be! */
3102 index = -strtol (&h[2], endp, 10);
3108 /* "$" is equivalent to "$0". */
3113 index = strtol (&h[1], endp, 10);
3116 return access_value_history (index);
3120 coerce_ref_if_computed (const struct value *arg)
3122 const struct lval_funcs *funcs;
3124 if (TYPE_CODE (check_typedef (value_type (arg))) != TYPE_CODE_REF)
3127 if (value_lval_const (arg) != lval_computed)
3130 funcs = value_computed_funcs (arg);
3131 if (funcs->coerce_ref == NULL)
3134 return funcs->coerce_ref (arg);
3138 coerce_ref (struct value *arg)
3140 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
3141 struct value *retval;
3143 retval = coerce_ref_if_computed (arg);
3147 if (TYPE_CODE (value_type_arg_tmp) != TYPE_CODE_REF)
3150 return value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
3151 unpack_pointer (value_type (arg),
3152 value_contents (arg)));
3156 coerce_array (struct value *arg)
3160 arg = coerce_ref (arg);
3161 type = check_typedef (value_type (arg));
3163 switch (TYPE_CODE (type))
3165 case TYPE_CODE_ARRAY:
3166 if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
3167 arg = value_coerce_array (arg);
3169 case TYPE_CODE_FUNC:
3170 arg = value_coerce_function (arg);
3177 /* Return true if the function returning the specified type is using
3178 the convention of returning structures in memory (passing in the
3179 address as a hidden first parameter). */
3182 using_struct_return (struct gdbarch *gdbarch,
3183 struct type *func_type, struct type *value_type)
3185 enum type_code code = TYPE_CODE (value_type);
3187 if (code == TYPE_CODE_ERROR)
3188 error (_("Function return type unknown."));
3190 if (code == TYPE_CODE_VOID)
3191 /* A void return value is never in memory. See also corresponding
3192 code in "print_return_value". */
3195 /* Probe the architecture for the return-value convention. */
3196 return (gdbarch_return_value (gdbarch, func_type, value_type,
3198 != RETURN_VALUE_REGISTER_CONVENTION);
3201 /* Set the initialized field in a value struct. */
3204 set_value_initialized (struct value *val, int status)
3206 val->initialized = status;
3209 /* Return the initialized field in a value struct. */
3212 value_initialized (struct value *val)
3214 return val->initialized;
3218 _initialize_values (void)
3220 add_cmd ("convenience", no_class, show_convenience, _("\
3221 Debugger convenience (\"$foo\") variables.\n\
3222 These variables are created when you assign them values;\n\
3223 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
3225 A few convenience variables are given values automatically:\n\
3226 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
3227 \"$__\" holds the contents of the last address examined with \"x\"."),
3230 add_cmd ("values", no_set_class, show_values, _("\
3231 Elements of value history around item number IDX (or last ten)."),
3234 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
3235 Initialize a convenience variable if necessary.\n\
3236 init-if-undefined VARIABLE = EXPRESSION\n\
3237 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
3238 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
3239 VARIABLE is already initialized."));
3241 add_prefix_cmd ("function", no_class, function_command, _("\
3242 Placeholder command for showing help on convenience functions."),
3243 &functionlist, "function ", 0, &cmdlist);