]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/value.c
Use new and delete for values
[thirdparty/binutils-gdb.git] / gdb / value.c
CommitLineData
c906108c 1/* Low level packing and unpacking of values for GDB, the GNU Debugger.
1bac305b 2
e2882c85 3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
e17c207e 21#include "arch-utils.h"
c906108c
SS
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "value.h"
25#include "gdbcore.h"
c906108c
SS
26#include "command.h"
27#include "gdbcmd.h"
28#include "target.h"
29#include "language.h"
c906108c 30#include "demangle.h"
36160dc4 31#include "regcache.h"
fe898f56 32#include "block.h"
70100014 33#include "target-float.h"
bccdca4a 34#include "objfiles.h"
79a45b7d 35#include "valprint.h"
bc3b79fd 36#include "cli/cli-decode.h"
6dddc817 37#include "extension.h"
3bd0f5ef 38#include <ctype.h>
0914bcdb 39#include "tracepoint.h"
be335936 40#include "cp-abi.h"
a58e2656 41#include "user-regs.h"
325fac50 42#include <algorithm>
eb3ff9a5 43#include "completer.h"
0914bcdb 44
bc3b79fd
TJB
45/* Definition of a user function. */
46struct internal_function
47{
48 /* The name of the function. It is a bit odd to have this in the
49 function itself -- the user might use a differently-named
50 convenience variable to hold the function. */
51 char *name;
52
53 /* The handler. */
54 internal_function_fn handler;
55
56 /* User data for the handler. */
57 void *cookie;
58};
59
4e07d55f
PA
60/* Defines an [OFFSET, OFFSET + LENGTH) range. */
61
62struct range
63{
64 /* Lowest offset in the range. */
6b850546 65 LONGEST offset;
4e07d55f
PA
66
67 /* Length of the range. */
6b850546 68 LONGEST length;
4e07d55f
PA
69};
70
71typedef struct range range_s;
72
73DEF_VEC_O(range_s);
74
75/* Returns true if the ranges defined by [offset1, offset1+len1) and
76 [offset2, offset2+len2) overlap. */
77
78static int
6b850546
DT
79ranges_overlap (LONGEST offset1, LONGEST len1,
80 LONGEST offset2, LONGEST len2)
4e07d55f
PA
81{
82 ULONGEST h, l;
83
325fac50
PA
84 l = std::max (offset1, offset2);
85 h = std::min (offset1 + len1, offset2 + len2);
4e07d55f
PA
86 return (l < h);
87}
88
89/* Returns true if the first argument is strictly less than the
90 second, useful for VEC_lower_bound. We keep ranges sorted by
91 offset and coalesce overlapping and contiguous ranges, so this just
92 compares the starting offset. */
93
94static int
95range_lessthan (const range_s *r1, const range_s *r2)
96{
97 return r1->offset < r2->offset;
98}
99
100/* Returns true if RANGES contains any range that overlaps [OFFSET,
101 OFFSET+LENGTH). */
102
103static int
6b850546 104ranges_contain (VEC(range_s) *ranges, LONGEST offset, LONGEST length)
4e07d55f
PA
105{
106 range_s what;
6b850546 107 LONGEST i;
4e07d55f
PA
108
109 what.offset = offset;
110 what.length = length;
111
112 /* We keep ranges sorted by offset and coalesce overlapping and
113 contiguous ranges, so to check if a range list contains a given
114 range, we can do a binary search for the position the given range
115 would be inserted if we only considered the starting OFFSET of
116 ranges. We call that position I. Since we also have LENGTH to
117 care for (this is a range afterall), we need to check if the
118 _previous_ range overlaps the I range. E.g.,
119
120 R
121 |---|
122 |---| |---| |------| ... |--|
123 0 1 2 N
124
125 I=1
126
127 In the case above, the binary search would return `I=1', meaning,
128 this OFFSET should be inserted at position 1, and the current
129 position 1 should be pushed further (and before 2). But, `0'
130 overlaps with R.
131
132 Then we need to check if the I range overlaps the I range itself.
133 E.g.,
134
135 R
136 |---|
137 |---| |---| |-------| ... |--|
138 0 1 2 N
139
140 I=1
141 */
142
143 i = VEC_lower_bound (range_s, ranges, &what, range_lessthan);
144
145 if (i > 0)
146 {
147 struct range *bef = VEC_index (range_s, ranges, i - 1);
148
149 if (ranges_overlap (bef->offset, bef->length, offset, length))
150 return 1;
151 }
152
153 if (i < VEC_length (range_s, ranges))
154 {
155 struct range *r = VEC_index (range_s, ranges, i);
156
157 if (ranges_overlap (r->offset, r->length, offset, length))
158 return 1;
159 }
160
161 return 0;
162}
163
bc3b79fd
TJB
164static struct cmd_list_element *functionlist;
165
87784a47
TT
166/* Note that the fields in this structure are arranged to save a bit
167 of memory. */
168
91294c83
AC
169struct value
170{
466ce3ae
TT
171 explicit value (struct type *type_)
172 : modifiable (1),
173 lazy (1),
174 initialized (1),
175 stack (0),
176 type (type_),
177 enclosing_type (type_)
178 {
179 location.address = 0;
180 }
181
182 ~value ()
183 {
184 /* If there's an associated parent value, drop our reference to
185 it. */
186 if (parent != NULL)
187 value_decref (parent);
188
189 if (VALUE_LVAL (this) == lval_computed)
190 {
191 const struct lval_funcs *funcs = location.computed.funcs;
192
193 if (funcs->free_closure)
194 funcs->free_closure (this);
195 }
196 else if (VALUE_LVAL (this) == lval_xcallable)
197 delete location.xm_worker;
198
199 xfree (contents);
200 VEC_free (range_s, unavailable);
201 VEC_free (range_s, optimized_out);
202 }
203
204 DISABLE_COPY_AND_ASSIGN (value);
205
91294c83
AC
206 /* Type of value; either not an lval, or one of the various
207 different possible kinds of lval. */
466ce3ae 208 enum lval_type lval = not_lval;
91294c83
AC
209
210 /* Is it modifiable? Only relevant if lval != not_lval. */
87784a47
TT
211 unsigned int modifiable : 1;
212
213 /* If zero, contents of this value are in the contents field. If
214 nonzero, contents are in inferior. If the lval field is lval_memory,
215 the contents are in inferior memory at location.address plus offset.
216 The lval field may also be lval_register.
217
218 WARNING: This field is used by the code which handles watchpoints
219 (see breakpoint.c) to decide whether a particular value can be
220 watched by hardware watchpoints. If the lazy flag is set for
221 some member of a value chain, it is assumed that this member of
222 the chain doesn't need to be watched as part of watching the
223 value itself. This is how GDB avoids watching the entire struct
224 or array when the user wants to watch a single struct member or
225 array element. If you ever change the way lazy flag is set and
226 reset, be sure to consider this use as well! */
227 unsigned int lazy : 1;
228
87784a47
TT
229 /* If value is a variable, is it initialized or not. */
230 unsigned int initialized : 1;
231
232 /* If value is from the stack. If this is set, read_stack will be
233 used instead of read_memory to enable extra caching. */
234 unsigned int stack : 1;
91294c83
AC
235
236 /* Location of value (if lval). */
237 union
238 {
7dc54575 239 /* If lval == lval_memory, this is the address in the inferior */
91294c83
AC
240 CORE_ADDR address;
241
7dc54575
YQ
242 /*If lval == lval_register, the value is from a register. */
243 struct
244 {
245 /* Register number. */
246 int regnum;
247 /* Frame ID of "next" frame to which a register value is relative.
248 If the register value is found relative to frame F, then the
249 frame id of F->next will be stored in next_frame_id. */
250 struct frame_id next_frame_id;
251 } reg;
252
91294c83
AC
253 /* Pointer to internal variable. */
254 struct internalvar *internalvar;
5f5233d4 255
e81e7f5e
SC
256 /* Pointer to xmethod worker. */
257 struct xmethod_worker *xm_worker;
258
5f5233d4
PA
259 /* If lval == lval_computed, this is a set of function pointers
260 to use to access and describe the value, and a closure pointer
261 for them to use. */
262 struct
263 {
c8f2448a
JK
264 /* Functions to call. */
265 const struct lval_funcs *funcs;
266
267 /* Closure for those functions to use. */
268 void *closure;
5f5233d4 269 } computed;
91294c83
AC
270 } location;
271
3723fda8 272 /* Describes offset of a value within lval of a structure in target
7dc54575
YQ
273 addressable memory units. Note also the member embedded_offset
274 below. */
466ce3ae 275 LONGEST offset = 0;
91294c83
AC
276
277 /* Only used for bitfields; number of bits contained in them. */
466ce3ae 278 LONGEST bitsize = 0;
91294c83
AC
279
280 /* Only used for bitfields; position of start of field. For
32c9a795 281 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
581e13c1 282 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
466ce3ae 283 LONGEST bitpos = 0;
91294c83 284
87784a47
TT
285 /* The number of references to this value. When a value is created,
286 the value chain holds a reference, so REFERENCE_COUNT is 1. If
287 release_value is called, this value is removed from the chain but
288 the caller of release_value now has a reference to this value.
289 The caller must arrange for a call to value_free later. */
466ce3ae 290 int reference_count = 1;
87784a47 291
4ea48cc1
DJ
292 /* Only used for bitfields; the containing value. This allows a
293 single read from the target when displaying multiple
294 bitfields. */
466ce3ae 295 struct value *parent = nullptr;
4ea48cc1 296
91294c83
AC
297 /* Type of the value. */
298 struct type *type;
299
300 /* If a value represents a C++ object, then the `type' field gives
301 the object's compile-time type. If the object actually belongs
302 to some class derived from `type', perhaps with other base
303 classes and additional members, then `type' is just a subobject
304 of the real thing, and the full object is probably larger than
305 `type' would suggest.
306
307 If `type' is a dynamic class (i.e. one with a vtable), then GDB
308 can actually determine the object's run-time type by looking at
309 the run-time type information in the vtable. When this
310 information is available, we may elect to read in the entire
311 object, for several reasons:
312
313 - When printing the value, the user would probably rather see the
314 full object, not just the limited portion apparent from the
315 compile-time type.
316
317 - If `type' has virtual base classes, then even printing `type'
318 alone may require reaching outside the `type' portion of the
319 object to wherever the virtual base class has been stored.
320
321 When we store the entire object, `enclosing_type' is the run-time
322 type -- the complete object -- and `embedded_offset' is the
3723fda8
SM
323 offset of `type' within that larger type, in target addressable memory
324 units. The value_contents() macro takes `embedded_offset' into account,
325 so most GDB code continues to see the `type' portion of the value, just
326 as the inferior would.
91294c83
AC
327
328 If `type' is a pointer to an object, then `enclosing_type' is a
329 pointer to the object's run-time type, and `pointed_to_offset' is
3723fda8
SM
330 the offset in target addressable memory units from the full object
331 to the pointed-to object -- that is, the value `embedded_offset' would
332 have if we followed the pointer and fetched the complete object.
333 (I don't really see the point. Why not just determine the
334 run-time type when you indirect, and avoid the special case? The
335 contents don't matter until you indirect anyway.)
91294c83
AC
336
337 If we're not doing anything fancy, `enclosing_type' is equal to
338 `type', and `embedded_offset' is zero, so everything works
339 normally. */
340 struct type *enclosing_type;
466ce3ae
TT
341 LONGEST embedded_offset = 0;
342 LONGEST pointed_to_offset = 0;
91294c83 343
3e3d7139
JG
344 /* Actual contents of the value. Target byte-order. NULL or not
345 valid if lazy is nonzero. */
466ce3ae 346 gdb_byte *contents = nullptr;
828d3400 347
4e07d55f
PA
348 /* Unavailable ranges in CONTENTS. We mark unavailable ranges,
349 rather than available, since the common and default case is for a
9a0dc9e3
PA
350 value to be available. This is filled in at value read time.
351 The unavailable ranges are tracked in bits. Note that a contents
352 bit that has been optimized out doesn't really exist in the
353 program, so it can't be marked unavailable either. */
466ce3ae 354 VEC(range_s) *unavailable = nullptr;
9a0dc9e3
PA
355
356 /* Likewise, but for optimized out contents (a chunk of the value of
357 a variable that does not actually exist in the program). If LVAL
358 is lval_register, this is a register ($pc, $sp, etc., never a
359 program variable) that has not been saved in the frame. Not
360 saved registers and optimized-out program variables values are
361 treated pretty much the same, except not-saved registers have a
362 different string representation and related error strings. */
466ce3ae 363 VEC(range_s) *optimized_out = nullptr;
91294c83
AC
364};
365
e512cdbd
SM
366/* See value.h. */
367
368struct gdbarch *
369get_value_arch (const struct value *value)
370{
371 return get_type_arch (value_type (value));
372}
373
4e07d55f 374int
6b850546 375value_bits_available (const struct value *value, LONGEST offset, LONGEST length)
4e07d55f
PA
376{
377 gdb_assert (!value->lazy);
378
379 return !ranges_contain (value->unavailable, offset, length);
380}
381
bdf22206 382int
6b850546
DT
383value_bytes_available (const struct value *value,
384 LONGEST offset, LONGEST length)
bdf22206
AB
385{
386 return value_bits_available (value,
387 offset * TARGET_CHAR_BIT,
388 length * TARGET_CHAR_BIT);
389}
390
9a0dc9e3
PA
391int
392value_bits_any_optimized_out (const struct value *value, int bit_offset, int bit_length)
393{
394 gdb_assert (!value->lazy);
395
396 return ranges_contain (value->optimized_out, bit_offset, bit_length);
397}
398
ec0a52e1
PA
399int
400value_entirely_available (struct value *value)
401{
402 /* We can only tell whether the whole value is available when we try
403 to read it. */
404 if (value->lazy)
405 value_fetch_lazy (value);
406
407 if (VEC_empty (range_s, value->unavailable))
408 return 1;
409 return 0;
410}
411
9a0dc9e3
PA
412/* Returns true if VALUE is entirely covered by RANGES. If the value
413 is lazy, it'll be read now. Note that RANGE is a pointer to
414 pointer because reading the value might change *RANGE. */
415
416static int
417value_entirely_covered_by_range_vector (struct value *value,
418 VEC(range_s) **ranges)
6211c335 419{
9a0dc9e3
PA
420 /* We can only tell whether the whole value is optimized out /
421 unavailable when we try to read it. */
6211c335
YQ
422 if (value->lazy)
423 value_fetch_lazy (value);
424
9a0dc9e3 425 if (VEC_length (range_s, *ranges) == 1)
6211c335 426 {
9a0dc9e3 427 struct range *t = VEC_index (range_s, *ranges, 0);
6211c335
YQ
428
429 if (t->offset == 0
64c46ce4
JB
430 && t->length == (TARGET_CHAR_BIT
431 * TYPE_LENGTH (value_enclosing_type (value))))
6211c335
YQ
432 return 1;
433 }
434
435 return 0;
436}
437
9a0dc9e3
PA
438int
439value_entirely_unavailable (struct value *value)
440{
441 return value_entirely_covered_by_range_vector (value, &value->unavailable);
442}
443
444int
445value_entirely_optimized_out (struct value *value)
446{
447 return value_entirely_covered_by_range_vector (value, &value->optimized_out);
448}
449
450/* Insert into the vector pointed to by VECTORP the bit range starting of
451 OFFSET bits, and extending for the next LENGTH bits. */
452
453static void
6b850546
DT
454insert_into_bit_range_vector (VEC(range_s) **vectorp,
455 LONGEST offset, LONGEST length)
4e07d55f
PA
456{
457 range_s newr;
458 int i;
459
460 /* Insert the range sorted. If there's overlap or the new range
461 would be contiguous with an existing range, merge. */
462
463 newr.offset = offset;
464 newr.length = length;
465
466 /* Do a binary search for the position the given range would be
467 inserted if we only considered the starting OFFSET of ranges.
468 Call that position I. Since we also have LENGTH to care for
469 (this is a range afterall), we need to check if the _previous_
470 range overlaps the I range. E.g., calling R the new range:
471
472 #1 - overlaps with previous
473
474 R
475 |-...-|
476 |---| |---| |------| ... |--|
477 0 1 2 N
478
479 I=1
480
481 In the case #1 above, the binary search would return `I=1',
482 meaning, this OFFSET should be inserted at position 1, and the
483 current position 1 should be pushed further (and become 2). But,
484 note that `0' overlaps with R, so we want to merge them.
485
486 A similar consideration needs to be taken if the new range would
487 be contiguous with the previous range:
488
489 #2 - contiguous with previous
490
491 R
492 |-...-|
493 |--| |---| |------| ... |--|
494 0 1 2 N
495
496 I=1
497
498 If there's no overlap with the previous range, as in:
499
500 #3 - not overlapping and not contiguous
501
502 R
503 |-...-|
504 |--| |---| |------| ... |--|
505 0 1 2 N
506
507 I=1
508
509 or if I is 0:
510
511 #4 - R is the range with lowest offset
512
513 R
514 |-...-|
515 |--| |---| |------| ... |--|
516 0 1 2 N
517
518 I=0
519
520 ... we just push the new range to I.
521
522 All the 4 cases above need to consider that the new range may
523 also overlap several of the ranges that follow, or that R may be
524 contiguous with the following range, and merge. E.g.,
525
526 #5 - overlapping following ranges
527
528 R
529 |------------------------|
530 |--| |---| |------| ... |--|
531 0 1 2 N
532
533 I=0
534
535 or:
536
537 R
538 |-------|
539 |--| |---| |------| ... |--|
540 0 1 2 N
541
542 I=1
543
544 */
545
9a0dc9e3 546 i = VEC_lower_bound (range_s, *vectorp, &newr, range_lessthan);
4e07d55f
PA
547 if (i > 0)
548 {
9a0dc9e3 549 struct range *bef = VEC_index (range_s, *vectorp, i - 1);
4e07d55f
PA
550
551 if (ranges_overlap (bef->offset, bef->length, offset, length))
552 {
553 /* #1 */
325fac50
PA
554 ULONGEST l = std::min (bef->offset, offset);
555 ULONGEST h = std::max (bef->offset + bef->length, offset + length);
4e07d55f
PA
556
557 bef->offset = l;
558 bef->length = h - l;
559 i--;
560 }
561 else if (offset == bef->offset + bef->length)
562 {
563 /* #2 */
564 bef->length += length;
565 i--;
566 }
567 else
568 {
569 /* #3 */
9a0dc9e3 570 VEC_safe_insert (range_s, *vectorp, i, &newr);
4e07d55f
PA
571 }
572 }
573 else
574 {
575 /* #4 */
9a0dc9e3 576 VEC_safe_insert (range_s, *vectorp, i, &newr);
4e07d55f
PA
577 }
578
579 /* Check whether the ranges following the one we've just added or
580 touched can be folded in (#5 above). */
9a0dc9e3 581 if (i + 1 < VEC_length (range_s, *vectorp))
4e07d55f
PA
582 {
583 struct range *t;
584 struct range *r;
585 int removed = 0;
586 int next = i + 1;
587
588 /* Get the range we just touched. */
9a0dc9e3 589 t = VEC_index (range_s, *vectorp, i);
4e07d55f
PA
590 removed = 0;
591
592 i = next;
9a0dc9e3 593 for (; VEC_iterate (range_s, *vectorp, i, r); i++)
4e07d55f
PA
594 if (r->offset <= t->offset + t->length)
595 {
596 ULONGEST l, h;
597
325fac50
PA
598 l = std::min (t->offset, r->offset);
599 h = std::max (t->offset + t->length, r->offset + r->length);
4e07d55f
PA
600
601 t->offset = l;
602 t->length = h - l;
603
604 removed++;
605 }
606 else
607 {
608 /* If we couldn't merge this one, we won't be able to
609 merge following ones either, since the ranges are
610 always sorted by OFFSET. */
611 break;
612 }
613
614 if (removed != 0)
9a0dc9e3 615 VEC_block_remove (range_s, *vectorp, next, removed);
4e07d55f
PA
616 }
617}
618
9a0dc9e3 619void
6b850546
DT
620mark_value_bits_unavailable (struct value *value,
621 LONGEST offset, LONGEST length)
9a0dc9e3
PA
622{
623 insert_into_bit_range_vector (&value->unavailable, offset, length);
624}
625
bdf22206 626void
6b850546
DT
627mark_value_bytes_unavailable (struct value *value,
628 LONGEST offset, LONGEST length)
bdf22206
AB
629{
630 mark_value_bits_unavailable (value,
631 offset * TARGET_CHAR_BIT,
632 length * TARGET_CHAR_BIT);
633}
634
c8c1c22f
PA
635/* Find the first range in RANGES that overlaps the range defined by
636 OFFSET and LENGTH, starting at element POS in the RANGES vector,
637 Returns the index into RANGES where such overlapping range was
638 found, or -1 if none was found. */
639
640static int
641find_first_range_overlap (VEC(range_s) *ranges, int pos,
6b850546 642 LONGEST offset, LONGEST length)
c8c1c22f
PA
643{
644 range_s *r;
645 int i;
646
647 for (i = pos; VEC_iterate (range_s, ranges, i, r); i++)
648 if (ranges_overlap (r->offset, r->length, offset, length))
649 return i;
650
651 return -1;
652}
653
bdf22206
AB
654/* Compare LENGTH_BITS of memory at PTR1 + OFFSET1_BITS with the memory at
655 PTR2 + OFFSET2_BITS. Return 0 if the memory is the same, otherwise
656 return non-zero.
657
658 It must always be the case that:
659 OFFSET1_BITS % TARGET_CHAR_BIT == OFFSET2_BITS % TARGET_CHAR_BIT
660
661 It is assumed that memory can be accessed from:
662 PTR + (OFFSET_BITS / TARGET_CHAR_BIT)
663 to:
664 PTR + ((OFFSET_BITS + LENGTH_BITS + TARGET_CHAR_BIT - 1)
665 / TARGET_CHAR_BIT) */
666static int
667memcmp_with_bit_offsets (const gdb_byte *ptr1, size_t offset1_bits,
668 const gdb_byte *ptr2, size_t offset2_bits,
669 size_t length_bits)
670{
671 gdb_assert (offset1_bits % TARGET_CHAR_BIT
672 == offset2_bits % TARGET_CHAR_BIT);
673
674 if (offset1_bits % TARGET_CHAR_BIT != 0)
675 {
676 size_t bits;
677 gdb_byte mask, b1, b2;
678
679 /* The offset from the base pointers PTR1 and PTR2 is not a complete
680 number of bytes. A number of bits up to either the next exact
681 byte boundary, or LENGTH_BITS (which ever is sooner) will be
682 compared. */
683 bits = TARGET_CHAR_BIT - offset1_bits % TARGET_CHAR_BIT;
684 gdb_assert (bits < sizeof (mask) * TARGET_CHAR_BIT);
685 mask = (1 << bits) - 1;
686
687 if (length_bits < bits)
688 {
689 mask &= ~(gdb_byte) ((1 << (bits - length_bits)) - 1);
690 bits = length_bits;
691 }
692
693 /* Now load the two bytes and mask off the bits we care about. */
694 b1 = *(ptr1 + offset1_bits / TARGET_CHAR_BIT) & mask;
695 b2 = *(ptr2 + offset2_bits / TARGET_CHAR_BIT) & mask;
696
697 if (b1 != b2)
698 return 1;
699
700 /* Now update the length and offsets to take account of the bits
701 we've just compared. */
702 length_bits -= bits;
703 offset1_bits += bits;
704 offset2_bits += bits;
705 }
706
707 if (length_bits % TARGET_CHAR_BIT != 0)
708 {
709 size_t bits;
710 size_t o1, o2;
711 gdb_byte mask, b1, b2;
712
713 /* The length is not an exact number of bytes. After the previous
714 IF.. block then the offsets are byte aligned, or the
715 length is zero (in which case this code is not reached). Compare
716 a number of bits at the end of the region, starting from an exact
717 byte boundary. */
718 bits = length_bits % TARGET_CHAR_BIT;
719 o1 = offset1_bits + length_bits - bits;
720 o2 = offset2_bits + length_bits - bits;
721
722 gdb_assert (bits < sizeof (mask) * TARGET_CHAR_BIT);
723 mask = ((1 << bits) - 1) << (TARGET_CHAR_BIT - bits);
724
725 gdb_assert (o1 % TARGET_CHAR_BIT == 0);
726 gdb_assert (o2 % TARGET_CHAR_BIT == 0);
727
728 b1 = *(ptr1 + o1 / TARGET_CHAR_BIT) & mask;
729 b2 = *(ptr2 + o2 / TARGET_CHAR_BIT) & mask;
730
731 if (b1 != b2)
732 return 1;
733
734 length_bits -= bits;
735 }
736
737 if (length_bits > 0)
738 {
739 /* We've now taken care of any stray "bits" at the start, or end of
740 the region to compare, the remainder can be covered with a simple
741 memcmp. */
742 gdb_assert (offset1_bits % TARGET_CHAR_BIT == 0);
743 gdb_assert (offset2_bits % TARGET_CHAR_BIT == 0);
744 gdb_assert (length_bits % TARGET_CHAR_BIT == 0);
745
746 return memcmp (ptr1 + offset1_bits / TARGET_CHAR_BIT,
747 ptr2 + offset2_bits / TARGET_CHAR_BIT,
748 length_bits / TARGET_CHAR_BIT);
749 }
750
751 /* Length is zero, regions match. */
752 return 0;
753}
754
9a0dc9e3
PA
755/* Helper struct for find_first_range_overlap_and_match and
756 value_contents_bits_eq. Keep track of which slot of a given ranges
757 vector have we last looked at. */
bdf22206 758
9a0dc9e3
PA
759struct ranges_and_idx
760{
761 /* The ranges. */
762 VEC(range_s) *ranges;
763
764 /* The range we've last found in RANGES. Given ranges are sorted,
765 we can start the next lookup here. */
766 int idx;
767};
768
769/* Helper function for value_contents_bits_eq. Compare LENGTH bits of
770 RP1's ranges starting at OFFSET1 bits with LENGTH bits of RP2's
771 ranges starting at OFFSET2 bits. Return true if the ranges match
772 and fill in *L and *H with the overlapping window relative to
773 (both) OFFSET1 or OFFSET2. */
bdf22206
AB
774
775static int
9a0dc9e3
PA
776find_first_range_overlap_and_match (struct ranges_and_idx *rp1,
777 struct ranges_and_idx *rp2,
6b850546
DT
778 LONGEST offset1, LONGEST offset2,
779 LONGEST length, ULONGEST *l, ULONGEST *h)
c8c1c22f 780{
9a0dc9e3
PA
781 rp1->idx = find_first_range_overlap (rp1->ranges, rp1->idx,
782 offset1, length);
783 rp2->idx = find_first_range_overlap (rp2->ranges, rp2->idx,
784 offset2, length);
c8c1c22f 785
9a0dc9e3
PA
786 if (rp1->idx == -1 && rp2->idx == -1)
787 {
788 *l = length;
789 *h = length;
790 return 1;
791 }
792 else if (rp1->idx == -1 || rp2->idx == -1)
793 return 0;
794 else
c8c1c22f
PA
795 {
796 range_s *r1, *r2;
797 ULONGEST l1, h1;
798 ULONGEST l2, h2;
799
9a0dc9e3
PA
800 r1 = VEC_index (range_s, rp1->ranges, rp1->idx);
801 r2 = VEC_index (range_s, rp2->ranges, rp2->idx);
c8c1c22f
PA
802
803 /* Get the unavailable windows intersected by the incoming
804 ranges. The first and last ranges that overlap the argument
805 range may be wider than said incoming arguments ranges. */
325fac50
PA
806 l1 = std::max (offset1, r1->offset);
807 h1 = std::min (offset1 + length, r1->offset + r1->length);
c8c1c22f 808
325fac50
PA
809 l2 = std::max (offset2, r2->offset);
810 h2 = std::min (offset2 + length, offset2 + r2->length);
c8c1c22f
PA
811
812 /* Make them relative to the respective start offsets, so we can
813 compare them for equality. */
814 l1 -= offset1;
815 h1 -= offset1;
816
817 l2 -= offset2;
818 h2 -= offset2;
819
9a0dc9e3 820 /* Different ranges, no match. */
c8c1c22f
PA
821 if (l1 != l2 || h1 != h2)
822 return 0;
823
9a0dc9e3
PA
824 *h = h1;
825 *l = l1;
826 return 1;
827 }
828}
829
830/* Helper function for value_contents_eq. The only difference is that
831 this function is bit rather than byte based.
832
833 Compare LENGTH bits of VAL1's contents starting at OFFSET1 bits
834 with LENGTH bits of VAL2's contents starting at OFFSET2 bits.
835 Return true if the available bits match. */
836
98ead37e 837static bool
9a0dc9e3
PA
838value_contents_bits_eq (const struct value *val1, int offset1,
839 const struct value *val2, int offset2,
840 int length)
841{
842 /* Each array element corresponds to a ranges source (unavailable,
843 optimized out). '1' is for VAL1, '2' for VAL2. */
844 struct ranges_and_idx rp1[2], rp2[2];
845
846 /* See function description in value.h. */
847 gdb_assert (!val1->lazy && !val2->lazy);
848
849 /* We shouldn't be trying to compare past the end of the values. */
850 gdb_assert (offset1 + length
851 <= TYPE_LENGTH (val1->enclosing_type) * TARGET_CHAR_BIT);
852 gdb_assert (offset2 + length
853 <= TYPE_LENGTH (val2->enclosing_type) * TARGET_CHAR_BIT);
854
855 memset (&rp1, 0, sizeof (rp1));
856 memset (&rp2, 0, sizeof (rp2));
857 rp1[0].ranges = val1->unavailable;
858 rp2[0].ranges = val2->unavailable;
859 rp1[1].ranges = val1->optimized_out;
860 rp2[1].ranges = val2->optimized_out;
861
862 while (length > 0)
863 {
000339af 864 ULONGEST l = 0, h = 0; /* init for gcc -Wall */
9a0dc9e3
PA
865 int i;
866
867 for (i = 0; i < 2; i++)
868 {
869 ULONGEST l_tmp, h_tmp;
870
871 /* The contents only match equal if the invalid/unavailable
872 contents ranges match as well. */
873 if (!find_first_range_overlap_and_match (&rp1[i], &rp2[i],
874 offset1, offset2, length,
875 &l_tmp, &h_tmp))
98ead37e 876 return false;
9a0dc9e3
PA
877
878 /* We're interested in the lowest/first range found. */
879 if (i == 0 || l_tmp < l)
880 {
881 l = l_tmp;
882 h = h_tmp;
883 }
884 }
885
886 /* Compare the available/valid contents. */
bdf22206 887 if (memcmp_with_bit_offsets (val1->contents, offset1,
9a0dc9e3 888 val2->contents, offset2, l) != 0)
98ead37e 889 return false;
c8c1c22f 890
9a0dc9e3
PA
891 length -= h;
892 offset1 += h;
893 offset2 += h;
c8c1c22f
PA
894 }
895
98ead37e 896 return true;
c8c1c22f
PA
897}
898
98ead37e 899bool
6b850546
DT
900value_contents_eq (const struct value *val1, LONGEST offset1,
901 const struct value *val2, LONGEST offset2,
902 LONGEST length)
bdf22206 903{
9a0dc9e3
PA
904 return value_contents_bits_eq (val1, offset1 * TARGET_CHAR_BIT,
905 val2, offset2 * TARGET_CHAR_BIT,
906 length * TARGET_CHAR_BIT);
bdf22206
AB
907}
908
c906108c 909
4d0266a0
TT
910/* The value-history records all the values printed by print commands
911 during this session. */
c906108c 912
4d0266a0 913static std::vector<value_ref_ptr> value_history;
bc3b79fd 914
c906108c
SS
915\f
916/* List of all value objects currently allocated
917 (except for those released by calls to release_value)
918 This is so they can be freed after each command. */
919
062d818d 920static std::vector<value_ref_ptr> all_values;
c906108c 921
3e3d7139
JG
922/* Allocate a lazy value for type TYPE. Its actual content is
923 "lazily" allocated too: the content field of the return value is
924 NULL; it will be allocated when it is fetched from the target. */
c906108c 925
f23631e4 926struct value *
3e3d7139 927allocate_value_lazy (struct type *type)
c906108c 928{
f23631e4 929 struct value *val;
c54eabfa
JK
930
931 /* Call check_typedef on our type to make sure that, if TYPE
932 is a TYPE_CODE_TYPEDEF, its length is set to the length
933 of the target type instead of zero. However, we do not
934 replace the typedef type by the target type, because we want
935 to keep the typedef in order to be able to set the VAL's type
936 description correctly. */
937 check_typedef (type);
c906108c 938
466ce3ae 939 val = new struct value (type);
828d3400
DJ
940
941 /* Values start out on the all_values chain. */
062d818d 942 all_values.emplace_back (val);
828d3400 943
c906108c
SS
944 return val;
945}
946
5fdf6324
AB
947/* The maximum size, in bytes, that GDB will try to allocate for a value.
948 The initial value of 64k was not selected for any specific reason, it is
949 just a reasonable starting point. */
950
951static int max_value_size = 65536; /* 64k bytes */
952
953/* It is critical that the MAX_VALUE_SIZE is at least as big as the size of
954 LONGEST, otherwise GDB will not be able to parse integer values from the
955 CLI; for example if the MAX_VALUE_SIZE could be set to 1 then GDB would
956 be unable to parse "set max-value-size 2".
957
958 As we want a consistent GDB experience across hosts with different sizes
959 of LONGEST, this arbitrary minimum value was selected, so long as this
960 is bigger than LONGEST on all GDB supported hosts we're fine. */
961
962#define MIN_VALUE_FOR_MAX_VALUE_SIZE 16
963gdb_static_assert (sizeof (LONGEST) <= MIN_VALUE_FOR_MAX_VALUE_SIZE);
964
965/* Implement the "set max-value-size" command. */
966
967static void
eb4c3f4a 968set_max_value_size (const char *args, int from_tty,
5fdf6324
AB
969 struct cmd_list_element *c)
970{
971 gdb_assert (max_value_size == -1 || max_value_size >= 0);
972
973 if (max_value_size > -1 && max_value_size < MIN_VALUE_FOR_MAX_VALUE_SIZE)
974 {
975 max_value_size = MIN_VALUE_FOR_MAX_VALUE_SIZE;
976 error (_("max-value-size set too low, increasing to %d bytes"),
977 max_value_size);
978 }
979}
980
981/* Implement the "show max-value-size" command. */
982
983static void
984show_max_value_size (struct ui_file *file, int from_tty,
985 struct cmd_list_element *c, const char *value)
986{
987 if (max_value_size == -1)
988 fprintf_filtered (file, _("Maximum value size is unlimited.\n"));
989 else
990 fprintf_filtered (file, _("Maximum value size is %d bytes.\n"),
991 max_value_size);
992}
993
994/* Called before we attempt to allocate or reallocate a buffer for the
995 contents of a value. TYPE is the type of the value for which we are
996 allocating the buffer. If the buffer is too large (based on the user
997 controllable setting) then throw an error. If this function returns
998 then we should attempt to allocate the buffer. */
999
1000static void
1001check_type_length_before_alloc (const struct type *type)
1002{
1003 unsigned int length = TYPE_LENGTH (type);
1004
1005 if (max_value_size > -1 && length > max_value_size)
1006 {
1007 if (TYPE_NAME (type) != NULL)
1008 error (_("value of type `%s' requires %u bytes, which is more "
1009 "than max-value-size"), TYPE_NAME (type), length);
1010 else
1011 error (_("value requires %u bytes, which is more than "
1012 "max-value-size"), length);
1013 }
1014}
1015
3e3d7139
JG
1016/* Allocate the contents of VAL if it has not been allocated yet. */
1017
548b762d 1018static void
3e3d7139
JG
1019allocate_value_contents (struct value *val)
1020{
1021 if (!val->contents)
5fdf6324
AB
1022 {
1023 check_type_length_before_alloc (val->enclosing_type);
1024 val->contents
1025 = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
1026 }
3e3d7139
JG
1027}
1028
1029/* Allocate a value and its contents for type TYPE. */
1030
1031struct value *
1032allocate_value (struct type *type)
1033{
1034 struct value *val = allocate_value_lazy (type);
a109c7c1 1035
3e3d7139
JG
1036 allocate_value_contents (val);
1037 val->lazy = 0;
1038 return val;
1039}
1040
c906108c 1041/* Allocate a value that has the correct length
938f5214 1042 for COUNT repetitions of type TYPE. */
c906108c 1043
f23631e4 1044struct value *
fba45db2 1045allocate_repeat_value (struct type *type, int count)
c906108c 1046{
c5aa993b 1047 int low_bound = current_language->string_lower_bound; /* ??? */
c906108c
SS
1048 /* FIXME-type-allocation: need a way to free this type when we are
1049 done with it. */
e3506a9f
UW
1050 struct type *array_type
1051 = lookup_array_range_type (type, low_bound, count + low_bound - 1);
a109c7c1 1052
e3506a9f 1053 return allocate_value (array_type);
c906108c
SS
1054}
1055
5f5233d4
PA
1056struct value *
1057allocate_computed_value (struct type *type,
c8f2448a 1058 const struct lval_funcs *funcs,
5f5233d4
PA
1059 void *closure)
1060{
41e8491f 1061 struct value *v = allocate_value_lazy (type);
a109c7c1 1062
5f5233d4
PA
1063 VALUE_LVAL (v) = lval_computed;
1064 v->location.computed.funcs = funcs;
1065 v->location.computed.closure = closure;
5f5233d4
PA
1066
1067 return v;
1068}
1069
a7035dbb
JK
1070/* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */
1071
1072struct value *
1073allocate_optimized_out_value (struct type *type)
1074{
1075 struct value *retval = allocate_value_lazy (type);
1076
9a0dc9e3
PA
1077 mark_value_bytes_optimized_out (retval, 0, TYPE_LENGTH (type));
1078 set_value_lazy (retval, 0);
a7035dbb
JK
1079 return retval;
1080}
1081
df407dfe
AC
1082/* Accessor methods. */
1083
1084struct type *
0e03807e 1085value_type (const struct value *value)
df407dfe
AC
1086{
1087 return value->type;
1088}
04624583
AC
1089void
1090deprecated_set_value_type (struct value *value, struct type *type)
1091{
1092 value->type = type;
1093}
df407dfe 1094
6b850546 1095LONGEST
0e03807e 1096value_offset (const struct value *value)
df407dfe
AC
1097{
1098 return value->offset;
1099}
f5cf64a7 1100void
6b850546 1101set_value_offset (struct value *value, LONGEST offset)
f5cf64a7
AC
1102{
1103 value->offset = offset;
1104}
df407dfe 1105
6b850546 1106LONGEST
0e03807e 1107value_bitpos (const struct value *value)
df407dfe
AC
1108{
1109 return value->bitpos;
1110}
9bbda503 1111void
6b850546 1112set_value_bitpos (struct value *value, LONGEST bit)
9bbda503
AC
1113{
1114 value->bitpos = bit;
1115}
df407dfe 1116
6b850546 1117LONGEST
0e03807e 1118value_bitsize (const struct value *value)
df407dfe
AC
1119{
1120 return value->bitsize;
1121}
9bbda503 1122void
6b850546 1123set_value_bitsize (struct value *value, LONGEST bit)
9bbda503
AC
1124{
1125 value->bitsize = bit;
1126}
df407dfe 1127
4ea48cc1 1128struct value *
4bf7b526 1129value_parent (const struct value *value)
4ea48cc1
DJ
1130{
1131 return value->parent;
1132}
1133
53ba8333
JB
1134/* See value.h. */
1135
1136void
1137set_value_parent (struct value *value, struct value *parent)
1138{
40501e00
TT
1139 struct value *old = value->parent;
1140
53ba8333 1141 value->parent = parent;
40501e00
TT
1142 if (parent != NULL)
1143 value_incref (parent);
22bc8444 1144 value_decref (old);
53ba8333
JB
1145}
1146
fc1a4b47 1147gdb_byte *
990a07ab
AC
1148value_contents_raw (struct value *value)
1149{
3ae385af
SM
1150 struct gdbarch *arch = get_value_arch (value);
1151 int unit_size = gdbarch_addressable_memory_unit_size (arch);
1152
3e3d7139 1153 allocate_value_contents (value);
3ae385af 1154 return value->contents + value->embedded_offset * unit_size;
990a07ab
AC
1155}
1156
fc1a4b47 1157gdb_byte *
990a07ab
AC
1158value_contents_all_raw (struct value *value)
1159{
3e3d7139
JG
1160 allocate_value_contents (value);
1161 return value->contents;
990a07ab
AC
1162}
1163
4754a64e 1164struct type *
4bf7b526 1165value_enclosing_type (const struct value *value)
4754a64e
AC
1166{
1167 return value->enclosing_type;
1168}
1169
8264ba82
AG
1170/* Look at value.h for description. */
1171
1172struct type *
1173value_actual_type (struct value *value, int resolve_simple_types,
1174 int *real_type_found)
1175{
1176 struct value_print_options opts;
8264ba82
AG
1177 struct type *result;
1178
1179 get_user_print_options (&opts);
1180
1181 if (real_type_found)
1182 *real_type_found = 0;
1183 result = value_type (value);
1184 if (opts.objectprint)
1185 {
5e34c6c3
LM
1186 /* If result's target type is TYPE_CODE_STRUCT, proceed to
1187 fetch its rtti type. */
aa006118 1188 if ((TYPE_CODE (result) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (result))
5e34c6c3 1189 && TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (result)))
ecf2e90c
DB
1190 == TYPE_CODE_STRUCT
1191 && !value_optimized_out (value))
8264ba82
AG
1192 {
1193 struct type *real_type;
1194
1195 real_type = value_rtti_indirect_type (value, NULL, NULL, NULL);
1196 if (real_type)
1197 {
1198 if (real_type_found)
1199 *real_type_found = 1;
1200 result = real_type;
1201 }
1202 }
1203 else if (resolve_simple_types)
1204 {
1205 if (real_type_found)
1206 *real_type_found = 1;
1207 result = value_enclosing_type (value);
1208 }
1209 }
1210
1211 return result;
1212}
1213
901461f8
PA
1214void
1215error_value_optimized_out (void)
1216{
1217 error (_("value has been optimized out"));
1218}
1219
0e03807e 1220static void
4e07d55f 1221require_not_optimized_out (const struct value *value)
0e03807e 1222{
9a0dc9e3 1223 if (!VEC_empty (range_s, value->optimized_out))
901461f8
PA
1224 {
1225 if (value->lval == lval_register)
1226 error (_("register has not been saved in frame"));
1227 else
1228 error_value_optimized_out ();
1229 }
0e03807e
TT
1230}
1231
4e07d55f
PA
1232static void
1233require_available (const struct value *value)
1234{
1235 if (!VEC_empty (range_s, value->unavailable))
8af8e3bc 1236 throw_error (NOT_AVAILABLE_ERROR, _("value is not available"));
4e07d55f
PA
1237}
1238
fc1a4b47 1239const gdb_byte *
0e03807e 1240value_contents_for_printing (struct value *value)
46615f07
AC
1241{
1242 if (value->lazy)
1243 value_fetch_lazy (value);
3e3d7139 1244 return value->contents;
46615f07
AC
1245}
1246
de4127a3
PA
1247const gdb_byte *
1248value_contents_for_printing_const (const struct value *value)
1249{
1250 gdb_assert (!value->lazy);
1251 return value->contents;
1252}
1253
0e03807e
TT
1254const gdb_byte *
1255value_contents_all (struct value *value)
1256{
1257 const gdb_byte *result = value_contents_for_printing (value);
1258 require_not_optimized_out (value);
4e07d55f 1259 require_available (value);
0e03807e
TT
1260 return result;
1261}
1262
9a0dc9e3
PA
1263/* Copy ranges in SRC_RANGE that overlap [SRC_BIT_OFFSET,
1264 SRC_BIT_OFFSET+BIT_LENGTH) ranges into *DST_RANGE, adjusted. */
1265
1266static void
1267ranges_copy_adjusted (VEC (range_s) **dst_range, int dst_bit_offset,
1268 VEC (range_s) *src_range, int src_bit_offset,
1269 int bit_length)
1270{
1271 range_s *r;
1272 int i;
1273
1274 for (i = 0; VEC_iterate (range_s, src_range, i, r); i++)
1275 {
1276 ULONGEST h, l;
1277
325fac50
PA
1278 l = std::max (r->offset, (LONGEST) src_bit_offset);
1279 h = std::min (r->offset + r->length,
1280 (LONGEST) src_bit_offset + bit_length);
9a0dc9e3
PA
1281
1282 if (l < h)
1283 insert_into_bit_range_vector (dst_range,
1284 dst_bit_offset + (l - src_bit_offset),
1285 h - l);
1286 }
1287}
1288
4875ffdb
PA
1289/* Copy the ranges metadata in SRC that overlaps [SRC_BIT_OFFSET,
1290 SRC_BIT_OFFSET+BIT_LENGTH) into DST, adjusted. */
1291
1292static void
1293value_ranges_copy_adjusted (struct value *dst, int dst_bit_offset,
1294 const struct value *src, int src_bit_offset,
1295 int bit_length)
1296{
1297 ranges_copy_adjusted (&dst->unavailable, dst_bit_offset,
1298 src->unavailable, src_bit_offset,
1299 bit_length);
1300 ranges_copy_adjusted (&dst->optimized_out, dst_bit_offset,
1301 src->optimized_out, src_bit_offset,
1302 bit_length);
1303}
1304
3ae385af 1305/* Copy LENGTH target addressable memory units of SRC value's (all) contents
29976f3f
PA
1306 (value_contents_all) starting at SRC_OFFSET, into DST value's (all)
1307 contents, starting at DST_OFFSET. If unavailable contents are
1308 being copied from SRC, the corresponding DST contents are marked
1309 unavailable accordingly. Neither DST nor SRC may be lazy
1310 values.
1311
1312 It is assumed the contents of DST in the [DST_OFFSET,
1313 DST_OFFSET+LENGTH) range are wholly available. */
39d37385
PA
1314
1315void
6b850546
DT
1316value_contents_copy_raw (struct value *dst, LONGEST dst_offset,
1317 struct value *src, LONGEST src_offset, LONGEST length)
39d37385 1318{
6b850546 1319 LONGEST src_bit_offset, dst_bit_offset, bit_length;
3ae385af
SM
1320 struct gdbarch *arch = get_value_arch (src);
1321 int unit_size = gdbarch_addressable_memory_unit_size (arch);
39d37385
PA
1322
1323 /* A lazy DST would make that this copy operation useless, since as
1324 soon as DST's contents were un-lazied (by a later value_contents
1325 call, say), the contents would be overwritten. A lazy SRC would
1326 mean we'd be copying garbage. */
1327 gdb_assert (!dst->lazy && !src->lazy);
1328
29976f3f
PA
1329 /* The overwritten DST range gets unavailability ORed in, not
1330 replaced. Make sure to remember to implement replacing if it
1331 turns out actually necessary. */
1332 gdb_assert (value_bytes_available (dst, dst_offset, length));
9a0dc9e3
PA
1333 gdb_assert (!value_bits_any_optimized_out (dst,
1334 TARGET_CHAR_BIT * dst_offset,
1335 TARGET_CHAR_BIT * length));
29976f3f 1336
39d37385 1337 /* Copy the data. */
3ae385af
SM
1338 memcpy (value_contents_all_raw (dst) + dst_offset * unit_size,
1339 value_contents_all_raw (src) + src_offset * unit_size,
1340 length * unit_size);
39d37385
PA
1341
1342 /* Copy the meta-data, adjusted. */
3ae385af
SM
1343 src_bit_offset = src_offset * unit_size * HOST_CHAR_BIT;
1344 dst_bit_offset = dst_offset * unit_size * HOST_CHAR_BIT;
1345 bit_length = length * unit_size * HOST_CHAR_BIT;
39d37385 1346
4875ffdb
PA
1347 value_ranges_copy_adjusted (dst, dst_bit_offset,
1348 src, src_bit_offset,
1349 bit_length);
39d37385
PA
1350}
1351
29976f3f
PA
1352/* Copy LENGTH bytes of SRC value's (all) contents
1353 (value_contents_all) starting at SRC_OFFSET byte, into DST value's
1354 (all) contents, starting at DST_OFFSET. If unavailable contents
1355 are being copied from SRC, the corresponding DST contents are
1356 marked unavailable accordingly. DST must not be lazy. If SRC is
9a0dc9e3 1357 lazy, it will be fetched now.
29976f3f
PA
1358
1359 It is assumed the contents of DST in the [DST_OFFSET,
1360 DST_OFFSET+LENGTH) range are wholly available. */
39d37385
PA
1361
1362void
6b850546
DT
1363value_contents_copy (struct value *dst, LONGEST dst_offset,
1364 struct value *src, LONGEST src_offset, LONGEST length)
39d37385 1365{
39d37385
PA
1366 if (src->lazy)
1367 value_fetch_lazy (src);
1368
1369 value_contents_copy_raw (dst, dst_offset, src, src_offset, length);
1370}
1371
d69fe07e 1372int
4bf7b526 1373value_lazy (const struct value *value)
d69fe07e
AC
1374{
1375 return value->lazy;
1376}
1377
dfa52d88
AC
1378void
1379set_value_lazy (struct value *value, int val)
1380{
1381 value->lazy = val;
1382}
1383
4e5d721f 1384int
4bf7b526 1385value_stack (const struct value *value)
4e5d721f
DE
1386{
1387 return value->stack;
1388}
1389
1390void
1391set_value_stack (struct value *value, int val)
1392{
1393 value->stack = val;
1394}
1395
fc1a4b47 1396const gdb_byte *
0fd88904
AC
1397value_contents (struct value *value)
1398{
0e03807e
TT
1399 const gdb_byte *result = value_contents_writeable (value);
1400 require_not_optimized_out (value);
4e07d55f 1401 require_available (value);
0e03807e 1402 return result;
0fd88904
AC
1403}
1404
fc1a4b47 1405gdb_byte *
0fd88904
AC
1406value_contents_writeable (struct value *value)
1407{
1408 if (value->lazy)
1409 value_fetch_lazy (value);
fc0c53a0 1410 return value_contents_raw (value);
0fd88904
AC
1411}
1412
feb13ab0
AC
1413int
1414value_optimized_out (struct value *value)
1415{
691a26f5
AB
1416 /* We can only know if a value is optimized out once we have tried to
1417 fetch it. */
9a0dc9e3 1418 if (VEC_empty (range_s, value->optimized_out) && value->lazy)
ecf2e90c
DB
1419 {
1420 TRY
1421 {
1422 value_fetch_lazy (value);
1423 }
1424 CATCH (ex, RETURN_MASK_ERROR)
1425 {
1426 /* Fall back to checking value->optimized_out. */
1427 }
1428 END_CATCH
1429 }
691a26f5 1430
9a0dc9e3 1431 return !VEC_empty (range_s, value->optimized_out);
feb13ab0
AC
1432}
1433
9a0dc9e3
PA
1434/* Mark contents of VALUE as optimized out, starting at OFFSET bytes, and
1435 the following LENGTH bytes. */
eca07816 1436
feb13ab0 1437void
9a0dc9e3 1438mark_value_bytes_optimized_out (struct value *value, int offset, int length)
feb13ab0 1439{
9a0dc9e3
PA
1440 mark_value_bits_optimized_out (value,
1441 offset * TARGET_CHAR_BIT,
1442 length * TARGET_CHAR_BIT);
feb13ab0 1443}
13c3b5f5 1444
9a0dc9e3 1445/* See value.h. */
0e03807e 1446
9a0dc9e3 1447void
6b850546
DT
1448mark_value_bits_optimized_out (struct value *value,
1449 LONGEST offset, LONGEST length)
0e03807e 1450{
9a0dc9e3 1451 insert_into_bit_range_vector (&value->optimized_out, offset, length);
0e03807e
TT
1452}
1453
8cf6f0b1
TT
1454int
1455value_bits_synthetic_pointer (const struct value *value,
6b850546 1456 LONGEST offset, LONGEST length)
8cf6f0b1 1457{
e7303042 1458 if (value->lval != lval_computed
8cf6f0b1
TT
1459 || !value->location.computed.funcs->check_synthetic_pointer)
1460 return 0;
1461 return value->location.computed.funcs->check_synthetic_pointer (value,
1462 offset,
1463 length);
1464}
1465
6b850546 1466LONGEST
4bf7b526 1467value_embedded_offset (const struct value *value)
13c3b5f5
AC
1468{
1469 return value->embedded_offset;
1470}
1471
1472void
6b850546 1473set_value_embedded_offset (struct value *value, LONGEST val)
13c3b5f5
AC
1474{
1475 value->embedded_offset = val;
1476}
b44d461b 1477
6b850546 1478LONGEST
4bf7b526 1479value_pointed_to_offset (const struct value *value)
b44d461b
AC
1480{
1481 return value->pointed_to_offset;
1482}
1483
1484void
6b850546 1485set_value_pointed_to_offset (struct value *value, LONGEST val)
b44d461b
AC
1486{
1487 value->pointed_to_offset = val;
1488}
13bb5560 1489
c8f2448a 1490const struct lval_funcs *
a471c594 1491value_computed_funcs (const struct value *v)
5f5233d4 1492{
a471c594 1493 gdb_assert (value_lval_const (v) == lval_computed);
5f5233d4
PA
1494
1495 return v->location.computed.funcs;
1496}
1497
1498void *
0e03807e 1499value_computed_closure (const struct value *v)
5f5233d4 1500{
0e03807e 1501 gdb_assert (v->lval == lval_computed);
5f5233d4
PA
1502
1503 return v->location.computed.closure;
1504}
1505
13bb5560
AC
1506enum lval_type *
1507deprecated_value_lval_hack (struct value *value)
1508{
1509 return &value->lval;
1510}
1511
a471c594
JK
1512enum lval_type
1513value_lval_const (const struct value *value)
1514{
1515 return value->lval;
1516}
1517
42ae5230 1518CORE_ADDR
de4127a3 1519value_address (const struct value *value)
42ae5230 1520{
1a088441 1521 if (value->lval != lval_memory)
42ae5230 1522 return 0;
53ba8333
JB
1523 if (value->parent != NULL)
1524 return value_address (value->parent) + value->offset;
9920b434
BH
1525 if (NULL != TYPE_DATA_LOCATION (value_type (value)))
1526 {
1527 gdb_assert (PROP_CONST == TYPE_DATA_LOCATION_KIND (value_type (value)));
1528 return TYPE_DATA_LOCATION_ADDR (value_type (value));
1529 }
1530
1531 return value->location.address + value->offset;
42ae5230
TT
1532}
1533
1534CORE_ADDR
4bf7b526 1535value_raw_address (const struct value *value)
42ae5230 1536{
1a088441 1537 if (value->lval != lval_memory)
42ae5230
TT
1538 return 0;
1539 return value->location.address;
1540}
1541
1542void
1543set_value_address (struct value *value, CORE_ADDR addr)
13bb5560 1544{
1a088441 1545 gdb_assert (value->lval == lval_memory);
42ae5230 1546 value->location.address = addr;
13bb5560
AC
1547}
1548
1549struct internalvar **
1550deprecated_value_internalvar_hack (struct value *value)
1551{
1552 return &value->location.internalvar;
1553}
1554
1555struct frame_id *
41b56feb 1556deprecated_value_next_frame_id_hack (struct value *value)
13bb5560 1557{
7c2ba67e 1558 gdb_assert (value->lval == lval_register);
7dc54575 1559 return &value->location.reg.next_frame_id;
13bb5560
AC
1560}
1561
7dc54575 1562int *
13bb5560
AC
1563deprecated_value_regnum_hack (struct value *value)
1564{
7c2ba67e 1565 gdb_assert (value->lval == lval_register);
7dc54575 1566 return &value->location.reg.regnum;
13bb5560 1567}
88e3b34b
AC
1568
1569int
4bf7b526 1570deprecated_value_modifiable (const struct value *value)
88e3b34b
AC
1571{
1572 return value->modifiable;
1573}
990a07ab 1574\f
c906108c
SS
1575/* Return a mark in the value chain. All values allocated after the
1576 mark is obtained (except for those released) are subject to being freed
1577 if a subsequent value_free_to_mark is passed the mark. */
f23631e4 1578struct value *
fba45db2 1579value_mark (void)
c906108c 1580{
062d818d
TT
1581 if (all_values.empty ())
1582 return nullptr;
1583 return all_values.back ().get ();
c906108c
SS
1584}
1585
828d3400
DJ
1586/* Take a reference to VAL. VAL will not be deallocated until all
1587 references are released. */
1588
22bc8444 1589struct value *
828d3400
DJ
1590value_incref (struct value *val)
1591{
1592 val->reference_count++;
22bc8444 1593 return val;
828d3400
DJ
1594}
1595
1596/* Release a reference to VAL, which was acquired with value_incref.
1597 This function is also called to deallocate values from the value
1598 chain. */
1599
3e3d7139 1600void
22bc8444 1601value_decref (struct value *val)
3e3d7139 1602{
466ce3ae 1603 if (val != nullptr)
5f5233d4 1604 {
828d3400
DJ
1605 gdb_assert (val->reference_count > 0);
1606 val->reference_count--;
466ce3ae
TT
1607 if (val->reference_count == 0)
1608 delete val;
5f5233d4 1609 }
3e3d7139
JG
1610}
1611
c906108c
SS
1612/* Free all values allocated since MARK was obtained by value_mark
1613 (except for those released). */
1614void
4bf7b526 1615value_free_to_mark (const struct value *mark)
c906108c 1616{
062d818d
TT
1617 auto iter = std::find (all_values.begin (), all_values.end (), mark);
1618 if (iter == all_values.end ())
1619 all_values.clear ();
1620 else
1621 all_values.erase (iter + 1, all_values.end ());
c906108c
SS
1622}
1623
c906108c
SS
1624/* Remove VAL from the chain all_values
1625 so it will not be freed automatically. */
1626
22bc8444 1627value_ref_ptr
f23631e4 1628release_value (struct value *val)
c906108c 1629{
f23631e4 1630 struct value *v;
c906108c 1631
850645cf
TT
1632 if (val == nullptr)
1633 return value_ref_ptr ();
1634
062d818d
TT
1635 std::vector<value_ref_ptr>::reverse_iterator iter;
1636 for (iter = all_values.rbegin (); iter != all_values.rend (); ++iter)
c906108c 1637 {
062d818d 1638 if (*iter == val)
c906108c 1639 {
062d818d
TT
1640 value_ref_ptr result = *iter;
1641 all_values.erase (iter.base () - 1);
1642 return result;
c906108c
SS
1643 }
1644 }
c906108c 1645
062d818d
TT
1646 /* We must always return an owned reference. Normally this happens
1647 because we transfer the reference from the value chain, but in
1648 this case the value was not on the chain. */
1649 return value_ref_ptr (value_incref (val));
e848a8a5
TT
1650}
1651
a6535de1
TT
1652/* See value.h. */
1653
1654std::vector<value_ref_ptr>
4bf7b526 1655value_release_to_mark (const struct value *mark)
c906108c 1656{
a6535de1 1657 std::vector<value_ref_ptr> result;
c906108c 1658
062d818d
TT
1659 auto iter = std::find (all_values.begin (), all_values.end (), mark);
1660 if (iter == all_values.end ())
1661 std::swap (result, all_values);
1662 else
e848a8a5 1663 {
062d818d
TT
1664 std::move (iter + 1, all_values.end (), std::back_inserter (result));
1665 all_values.erase (iter + 1, all_values.end ());
e848a8a5 1666 }
062d818d 1667 std::reverse (result.begin (), result.end ());
a6535de1 1668 return result;
c906108c
SS
1669}
1670
1671/* Return a copy of the value ARG.
1672 It contains the same contents, for same memory address,
1673 but it's a different block of storage. */
1674
f23631e4
AC
1675struct value *
1676value_copy (struct value *arg)
c906108c 1677{
4754a64e 1678 struct type *encl_type = value_enclosing_type (arg);
3e3d7139
JG
1679 struct value *val;
1680
1681 if (value_lazy (arg))
1682 val = allocate_value_lazy (encl_type);
1683 else
1684 val = allocate_value (encl_type);
df407dfe 1685 val->type = arg->type;
c906108c 1686 VALUE_LVAL (val) = VALUE_LVAL (arg);
6f7c8fc2 1687 val->location = arg->location;
df407dfe
AC
1688 val->offset = arg->offset;
1689 val->bitpos = arg->bitpos;
1690 val->bitsize = arg->bitsize;
d69fe07e 1691 val->lazy = arg->lazy;
13c3b5f5 1692 val->embedded_offset = value_embedded_offset (arg);
b44d461b 1693 val->pointed_to_offset = arg->pointed_to_offset;
c906108c 1694 val->modifiable = arg->modifiable;
d69fe07e 1695 if (!value_lazy (val))
c906108c 1696 {
990a07ab 1697 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
4754a64e 1698 TYPE_LENGTH (value_enclosing_type (arg)));
c906108c
SS
1699
1700 }
4e07d55f 1701 val->unavailable = VEC_copy (range_s, arg->unavailable);
9a0dc9e3 1702 val->optimized_out = VEC_copy (range_s, arg->optimized_out);
40501e00 1703 set_value_parent (val, arg->parent);
5f5233d4
PA
1704 if (VALUE_LVAL (val) == lval_computed)
1705 {
c8f2448a 1706 const struct lval_funcs *funcs = val->location.computed.funcs;
5f5233d4
PA
1707
1708 if (funcs->copy_closure)
1709 val->location.computed.closure = funcs->copy_closure (val);
1710 }
c906108c
SS
1711 return val;
1712}
74bcbdf3 1713
4c082a81
SC
1714/* Return a "const" and/or "volatile" qualified version of the value V.
1715 If CNST is true, then the returned value will be qualified with
1716 "const".
1717 if VOLTL is true, then the returned value will be qualified with
1718 "volatile". */
1719
1720struct value *
1721make_cv_value (int cnst, int voltl, struct value *v)
1722{
1723 struct type *val_type = value_type (v);
1724 struct type *enclosing_type = value_enclosing_type (v);
1725 struct value *cv_val = value_copy (v);
1726
1727 deprecated_set_value_type (cv_val,
1728 make_cv_type (cnst, voltl, val_type, NULL));
1729 set_value_enclosing_type (cv_val,
1730 make_cv_type (cnst, voltl, enclosing_type, NULL));
1731
1732 return cv_val;
1733}
1734
c37f7098
KW
1735/* Return a version of ARG that is non-lvalue. */
1736
1737struct value *
1738value_non_lval (struct value *arg)
1739{
1740 if (VALUE_LVAL (arg) != not_lval)
1741 {
1742 struct type *enc_type = value_enclosing_type (arg);
1743 struct value *val = allocate_value (enc_type);
1744
1745 memcpy (value_contents_all_raw (val), value_contents_all (arg),
1746 TYPE_LENGTH (enc_type));
1747 val->type = arg->type;
1748 set_value_embedded_offset (val, value_embedded_offset (arg));
1749 set_value_pointed_to_offset (val, value_pointed_to_offset (arg));
1750 return val;
1751 }
1752 return arg;
1753}
1754
6c659fc2
SC
1755/* Write contents of V at ADDR and set its lval type to be LVAL_MEMORY. */
1756
1757void
1758value_force_lval (struct value *v, CORE_ADDR addr)
1759{
1760 gdb_assert (VALUE_LVAL (v) == not_lval);
1761
1762 write_memory (addr, value_contents_raw (v), TYPE_LENGTH (value_type (v)));
1763 v->lval = lval_memory;
1764 v->location.address = addr;
1765}
1766
74bcbdf3 1767void
0e03807e
TT
1768set_value_component_location (struct value *component,
1769 const struct value *whole)
74bcbdf3 1770{
9920b434
BH
1771 struct type *type;
1772
e81e7f5e
SC
1773 gdb_assert (whole->lval != lval_xcallable);
1774
0e03807e 1775 if (whole->lval == lval_internalvar)
74bcbdf3
PA
1776 VALUE_LVAL (component) = lval_internalvar_component;
1777 else
0e03807e 1778 VALUE_LVAL (component) = whole->lval;
5f5233d4 1779
74bcbdf3 1780 component->location = whole->location;
0e03807e 1781 if (whole->lval == lval_computed)
5f5233d4 1782 {
c8f2448a 1783 const struct lval_funcs *funcs = whole->location.computed.funcs;
5f5233d4
PA
1784
1785 if (funcs->copy_closure)
1786 component->location.computed.closure = funcs->copy_closure (whole);
1787 }
9920b434
BH
1788
1789 /* If type has a dynamic resolved location property
1790 update it's value address. */
1791 type = value_type (whole);
1792 if (NULL != TYPE_DATA_LOCATION (type)
1793 && TYPE_DATA_LOCATION_KIND (type) == PROP_CONST)
1794 set_value_address (component, TYPE_DATA_LOCATION_ADDR (type));
74bcbdf3
PA
1795}
1796
c906108c
SS
1797/* Access to the value history. */
1798
1799/* Record a new value in the value history.
eddf0bae 1800 Returns the absolute history index of the entry. */
c906108c
SS
1801
1802int
f23631e4 1803record_latest_value (struct value *val)
c906108c
SS
1804{
1805 int i;
1806
1807 /* We don't want this value to have anything to do with the inferior anymore.
1808 In particular, "set $1 = 50" should not affect the variable from which
1809 the value was taken, and fast watchpoints should be able to assume that
1810 a value on the value history never changes. */
d69fe07e 1811 if (value_lazy (val))
c906108c
SS
1812 value_fetch_lazy (val);
1813 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
1814 from. This is a bit dubious, because then *&$1 does not just return $1
1815 but the current contents of that location. c'est la vie... */
1816 val->modifiable = 0;
350e1a76 1817
4d0266a0 1818 value_history.push_back (release_value (val));
a109c7c1 1819
4d0266a0 1820 return value_history.size ();
c906108c
SS
1821}
1822
1823/* Return a copy of the value in the history with sequence number NUM. */
1824
f23631e4 1825struct value *
fba45db2 1826access_value_history (int num)
c906108c 1827{
52f0bd74
AC
1828 int i;
1829 int absnum = num;
c906108c
SS
1830
1831 if (absnum <= 0)
4d0266a0 1832 absnum += value_history.size ();
c906108c
SS
1833
1834 if (absnum <= 0)
1835 {
1836 if (num == 0)
8a3fe4f8 1837 error (_("The history is empty."));
c906108c 1838 else if (num == 1)
8a3fe4f8 1839 error (_("There is only one value in the history."));
c906108c 1840 else
8a3fe4f8 1841 error (_("History does not go back to $$%d."), -num);
c906108c 1842 }
4d0266a0 1843 if (absnum > value_history.size ())
8a3fe4f8 1844 error (_("History has not yet reached $%d."), absnum);
c906108c
SS
1845
1846 absnum--;
1847
4d0266a0 1848 return value_copy (value_history[absnum].get ());
c906108c
SS
1849}
1850
c906108c 1851static void
5fed81ff 1852show_values (const char *num_exp, int from_tty)
c906108c 1853{
52f0bd74 1854 int i;
f23631e4 1855 struct value *val;
c906108c
SS
1856 static int num = 1;
1857
1858 if (num_exp)
1859 {
f132ba9d
TJB
1860 /* "show values +" should print from the stored position.
1861 "show values <exp>" should print around value number <exp>. */
c906108c 1862 if (num_exp[0] != '+' || num_exp[1] != '\0')
bb518678 1863 num = parse_and_eval_long (num_exp) - 5;
c906108c
SS
1864 }
1865 else
1866 {
f132ba9d 1867 /* "show values" means print the last 10 values. */
4d0266a0 1868 num = value_history.size () - 9;
c906108c
SS
1869 }
1870
1871 if (num <= 0)
1872 num = 1;
1873
4d0266a0 1874 for (i = num; i < num + 10 && i <= value_history.size (); i++)
c906108c 1875 {
79a45b7d 1876 struct value_print_options opts;
a109c7c1 1877
c906108c 1878 val = access_value_history (i);
a3f17187 1879 printf_filtered (("$%d = "), i);
79a45b7d
TT
1880 get_user_print_options (&opts);
1881 value_print (val, gdb_stdout, &opts);
a3f17187 1882 printf_filtered (("\n"));
c906108c
SS
1883 }
1884
f132ba9d 1885 /* The next "show values +" should start after what we just printed. */
c906108c
SS
1886 num += 10;
1887
1888 /* Hitting just return after this command should do the same thing as
f132ba9d
TJB
1889 "show values +". If num_exp is null, this is unnecessary, since
1890 "show values +" is not useful after "show values". */
c906108c 1891 if (from_tty && num_exp)
85c4be7c 1892 set_repeat_arguments ("+");
c906108c
SS
1893}
1894\f
52059ffd
TT
1895enum internalvar_kind
1896{
1897 /* The internal variable is empty. */
1898 INTERNALVAR_VOID,
1899
1900 /* The value of the internal variable is provided directly as
1901 a GDB value object. */
1902 INTERNALVAR_VALUE,
1903
1904 /* A fresh value is computed via a call-back routine on every
1905 access to the internal variable. */
1906 INTERNALVAR_MAKE_VALUE,
1907
1908 /* The internal variable holds a GDB internal convenience function. */
1909 INTERNALVAR_FUNCTION,
1910
1911 /* The variable holds an integer value. */
1912 INTERNALVAR_INTEGER,
1913
1914 /* The variable holds a GDB-provided string. */
1915 INTERNALVAR_STRING,
1916};
1917
1918union internalvar_data
1919{
1920 /* A value object used with INTERNALVAR_VALUE. */
1921 struct value *value;
1922
1923 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
1924 struct
1925 {
1926 /* The functions to call. */
1927 const struct internalvar_funcs *functions;
1928
1929 /* The function's user-data. */
1930 void *data;
1931 } make_value;
1932
1933 /* The internal function used with INTERNALVAR_FUNCTION. */
1934 struct
1935 {
1936 struct internal_function *function;
1937 /* True if this is the canonical name for the function. */
1938 int canonical;
1939 } fn;
1940
1941 /* An integer value used with INTERNALVAR_INTEGER. */
1942 struct
1943 {
1944 /* If type is non-NULL, it will be used as the type to generate
1945 a value for this internal variable. If type is NULL, a default
1946 integer type for the architecture is used. */
1947 struct type *type;
1948 LONGEST val;
1949 } integer;
1950
1951 /* A string value used with INTERNALVAR_STRING. */
1952 char *string;
1953};
1954
c906108c
SS
1955/* Internal variables. These are variables within the debugger
1956 that hold values assigned by debugger commands.
1957 The user refers to them with a '$' prefix
1958 that does not appear in the variable names stored internally. */
1959
4fa62494
UW
1960struct internalvar
1961{
1962 struct internalvar *next;
1963 char *name;
4fa62494 1964
78267919
UW
1965 /* We support various different kinds of content of an internal variable.
1966 enum internalvar_kind specifies the kind, and union internalvar_data
1967 provides the data associated with this particular kind. */
1968
52059ffd 1969 enum internalvar_kind kind;
4fa62494 1970
52059ffd 1971 union internalvar_data u;
4fa62494
UW
1972};
1973
c906108c
SS
1974static struct internalvar *internalvars;
1975
3e43a32a
MS
1976/* If the variable does not already exist create it and give it the
1977 value given. If no value is given then the default is zero. */
53e5f3cf 1978static void
0b39b52e 1979init_if_undefined_command (const char* args, int from_tty)
53e5f3cf
AS
1980{
1981 struct internalvar* intvar;
1982
1983 /* Parse the expression - this is taken from set_command(). */
4d01a485 1984 expression_up expr = parse_expression (args);
53e5f3cf
AS
1985
1986 /* Validate the expression.
1987 Was the expression an assignment?
1988 Or even an expression at all? */
1989 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
1990 error (_("Init-if-undefined requires an assignment expression."));
1991
1992 /* Extract the variable from the parsed expression.
1993 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
1994 if (expr->elts[1].opcode != OP_INTERNALVAR)
3e43a32a
MS
1995 error (_("The first parameter to init-if-undefined "
1996 "should be a GDB variable."));
53e5f3cf
AS
1997 intvar = expr->elts[2].internalvar;
1998
1999 /* Only evaluate the expression if the lvalue is void.
2000 This may still fail if the expresssion is invalid. */
78267919 2001 if (intvar->kind == INTERNALVAR_VOID)
4d01a485 2002 evaluate_expression (expr.get ());
53e5f3cf
AS
2003}
2004
2005
c906108c
SS
2006/* Look up an internal variable with name NAME. NAME should not
2007 normally include a dollar sign.
2008
2009 If the specified internal variable does not exist,
c4a3d09a 2010 the return value is NULL. */
c906108c
SS
2011
2012struct internalvar *
bc3b79fd 2013lookup_only_internalvar (const char *name)
c906108c 2014{
52f0bd74 2015 struct internalvar *var;
c906108c
SS
2016
2017 for (var = internalvars; var; var = var->next)
5cb316ef 2018 if (strcmp (var->name, name) == 0)
c906108c
SS
2019 return var;
2020
c4a3d09a
MF
2021 return NULL;
2022}
2023
eb3ff9a5
PA
2024/* Complete NAME by comparing it to the names of internal
2025 variables. */
d55637df 2026
eb3ff9a5
PA
2027void
2028complete_internalvar (completion_tracker &tracker, const char *name)
d55637df 2029{
d55637df
TT
2030 struct internalvar *var;
2031 int len;
2032
2033 len = strlen (name);
2034
2035 for (var = internalvars; var; var = var->next)
2036 if (strncmp (var->name, name, len) == 0)
2037 {
eb3ff9a5 2038 gdb::unique_xmalloc_ptr<char> copy (xstrdup (var->name));
d55637df 2039
eb3ff9a5 2040 tracker.add_completion (std::move (copy));
d55637df 2041 }
d55637df 2042}
c4a3d09a
MF
2043
2044/* Create an internal variable with name NAME and with a void value.
2045 NAME should not normally include a dollar sign. */
2046
2047struct internalvar *
bc3b79fd 2048create_internalvar (const char *name)
c4a3d09a 2049{
8d749320 2050 struct internalvar *var = XNEW (struct internalvar);
a109c7c1 2051
1754f103 2052 var->name = concat (name, (char *)NULL);
78267919 2053 var->kind = INTERNALVAR_VOID;
c906108c
SS
2054 var->next = internalvars;
2055 internalvars = var;
2056 return var;
2057}
2058
4aa995e1
PA
2059/* Create an internal variable with name NAME and register FUN as the
2060 function that value_of_internalvar uses to create a value whenever
2061 this variable is referenced. NAME should not normally include a
22d2b532
SDJ
2062 dollar sign. DATA is passed uninterpreted to FUN when it is
2063 called. CLEANUP, if not NULL, is called when the internal variable
2064 is destroyed. It is passed DATA as its only argument. */
4aa995e1
PA
2065
2066struct internalvar *
22d2b532
SDJ
2067create_internalvar_type_lazy (const char *name,
2068 const struct internalvar_funcs *funcs,
2069 void *data)
4aa995e1 2070{
4fa62494 2071 struct internalvar *var = create_internalvar (name);
a109c7c1 2072
78267919 2073 var->kind = INTERNALVAR_MAKE_VALUE;
22d2b532
SDJ
2074 var->u.make_value.functions = funcs;
2075 var->u.make_value.data = data;
4aa995e1
PA
2076 return var;
2077}
c4a3d09a 2078
22d2b532
SDJ
2079/* See documentation in value.h. */
2080
2081int
2082compile_internalvar_to_ax (struct internalvar *var,
2083 struct agent_expr *expr,
2084 struct axs_value *value)
2085{
2086 if (var->kind != INTERNALVAR_MAKE_VALUE
2087 || var->u.make_value.functions->compile_to_ax == NULL)
2088 return 0;
2089
2090 var->u.make_value.functions->compile_to_ax (var, expr, value,
2091 var->u.make_value.data);
2092 return 1;
2093}
2094
c4a3d09a
MF
2095/* Look up an internal variable with name NAME. NAME should not
2096 normally include a dollar sign.
2097
2098 If the specified internal variable does not exist,
2099 one is created, with a void value. */
2100
2101struct internalvar *
bc3b79fd 2102lookup_internalvar (const char *name)
c4a3d09a
MF
2103{
2104 struct internalvar *var;
2105
2106 var = lookup_only_internalvar (name);
2107 if (var)
2108 return var;
2109
2110 return create_internalvar (name);
2111}
2112
78267919
UW
2113/* Return current value of internal variable VAR. For variables that
2114 are not inherently typed, use a value type appropriate for GDBARCH. */
2115
f23631e4 2116struct value *
78267919 2117value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
c906108c 2118{
f23631e4 2119 struct value *val;
0914bcdb
SS
2120 struct trace_state_variable *tsv;
2121
2122 /* If there is a trace state variable of the same name, assume that
2123 is what we really want to see. */
2124 tsv = find_trace_state_variable (var->name);
2125 if (tsv)
2126 {
2127 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
2128 &(tsv->value));
2129 if (tsv->value_known)
2130 val = value_from_longest (builtin_type (gdbarch)->builtin_int64,
2131 tsv->value);
2132 else
2133 val = allocate_value (builtin_type (gdbarch)->builtin_void);
2134 return val;
2135 }
c906108c 2136
78267919 2137 switch (var->kind)
5f5233d4 2138 {
78267919
UW
2139 case INTERNALVAR_VOID:
2140 val = allocate_value (builtin_type (gdbarch)->builtin_void);
2141 break;
4fa62494 2142
78267919
UW
2143 case INTERNALVAR_FUNCTION:
2144 val = allocate_value (builtin_type (gdbarch)->internal_fn);
2145 break;
4fa62494 2146
cab0c772
UW
2147 case INTERNALVAR_INTEGER:
2148 if (!var->u.integer.type)
78267919 2149 val = value_from_longest (builtin_type (gdbarch)->builtin_int,
cab0c772 2150 var->u.integer.val);
78267919 2151 else
cab0c772
UW
2152 val = value_from_longest (var->u.integer.type, var->u.integer.val);
2153 break;
2154
78267919
UW
2155 case INTERNALVAR_STRING:
2156 val = value_cstring (var->u.string, strlen (var->u.string),
2157 builtin_type (gdbarch)->builtin_char);
2158 break;
4fa62494 2159
78267919
UW
2160 case INTERNALVAR_VALUE:
2161 val = value_copy (var->u.value);
4aa995e1
PA
2162 if (value_lazy (val))
2163 value_fetch_lazy (val);
78267919 2164 break;
4aa995e1 2165
78267919 2166 case INTERNALVAR_MAKE_VALUE:
22d2b532
SDJ
2167 val = (*var->u.make_value.functions->make_value) (gdbarch, var,
2168 var->u.make_value.data);
78267919
UW
2169 break;
2170
2171 default:
9b20d036 2172 internal_error (__FILE__, __LINE__, _("bad kind"));
78267919
UW
2173 }
2174
2175 /* Change the VALUE_LVAL to lval_internalvar so that future operations
2176 on this value go back to affect the original internal variable.
2177
2178 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
2179 no underlying modifyable state in the internal variable.
2180
2181 Likewise, if the variable's value is a computed lvalue, we want
2182 references to it to produce another computed lvalue, where
2183 references and assignments actually operate through the
2184 computed value's functions.
2185
2186 This means that internal variables with computed values
2187 behave a little differently from other internal variables:
2188 assignments to them don't just replace the previous value
2189 altogether. At the moment, this seems like the behavior we
2190 want. */
2191
2192 if (var->kind != INTERNALVAR_MAKE_VALUE
2193 && val->lval != lval_computed)
2194 {
2195 VALUE_LVAL (val) = lval_internalvar;
2196 VALUE_INTERNALVAR (val) = var;
5f5233d4 2197 }
d3c139e9 2198
4fa62494
UW
2199 return val;
2200}
d3c139e9 2201
4fa62494
UW
2202int
2203get_internalvar_integer (struct internalvar *var, LONGEST *result)
2204{
3158c6ed 2205 if (var->kind == INTERNALVAR_INTEGER)
4fa62494 2206 {
cab0c772
UW
2207 *result = var->u.integer.val;
2208 return 1;
3158c6ed 2209 }
d3c139e9 2210
3158c6ed
PA
2211 if (var->kind == INTERNALVAR_VALUE)
2212 {
2213 struct type *type = check_typedef (value_type (var->u.value));
2214
2215 if (TYPE_CODE (type) == TYPE_CODE_INT)
2216 {
2217 *result = value_as_long (var->u.value);
2218 return 1;
2219 }
4fa62494 2220 }
3158c6ed
PA
2221
2222 return 0;
4fa62494 2223}
d3c139e9 2224
4fa62494
UW
2225static int
2226get_internalvar_function (struct internalvar *var,
2227 struct internal_function **result)
2228{
78267919 2229 switch (var->kind)
d3c139e9 2230 {
78267919
UW
2231 case INTERNALVAR_FUNCTION:
2232 *result = var->u.fn.function;
4fa62494 2233 return 1;
d3c139e9 2234
4fa62494
UW
2235 default:
2236 return 0;
2237 }
c906108c
SS
2238}
2239
2240void
6b850546
DT
2241set_internalvar_component (struct internalvar *var,
2242 LONGEST offset, LONGEST bitpos,
2243 LONGEST bitsize, struct value *newval)
c906108c 2244{
4fa62494 2245 gdb_byte *addr;
3ae385af
SM
2246 struct gdbarch *arch;
2247 int unit_size;
c906108c 2248
78267919 2249 switch (var->kind)
4fa62494 2250 {
78267919
UW
2251 case INTERNALVAR_VALUE:
2252 addr = value_contents_writeable (var->u.value);
3ae385af
SM
2253 arch = get_value_arch (var->u.value);
2254 unit_size = gdbarch_addressable_memory_unit_size (arch);
4fa62494
UW
2255
2256 if (bitsize)
50810684 2257 modify_field (value_type (var->u.value), addr + offset,
4fa62494
UW
2258 value_as_long (newval), bitpos, bitsize);
2259 else
3ae385af 2260 memcpy (addr + offset * unit_size, value_contents (newval),
4fa62494
UW
2261 TYPE_LENGTH (value_type (newval)));
2262 break;
78267919
UW
2263
2264 default:
2265 /* We can never get a component of any other kind. */
9b20d036 2266 internal_error (__FILE__, __LINE__, _("set_internalvar_component"));
4fa62494 2267 }
c906108c
SS
2268}
2269
2270void
f23631e4 2271set_internalvar (struct internalvar *var, struct value *val)
c906108c 2272{
78267919 2273 enum internalvar_kind new_kind;
4fa62494 2274 union internalvar_data new_data = { 0 };
c906108c 2275
78267919 2276 if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
bc3b79fd
TJB
2277 error (_("Cannot overwrite convenience function %s"), var->name);
2278
4fa62494 2279 /* Prepare new contents. */
78267919 2280 switch (TYPE_CODE (check_typedef (value_type (val))))
4fa62494
UW
2281 {
2282 case TYPE_CODE_VOID:
78267919 2283 new_kind = INTERNALVAR_VOID;
4fa62494
UW
2284 break;
2285
2286 case TYPE_CODE_INTERNAL_FUNCTION:
2287 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
78267919
UW
2288 new_kind = INTERNALVAR_FUNCTION;
2289 get_internalvar_function (VALUE_INTERNALVAR (val),
2290 &new_data.fn.function);
2291 /* Copies created here are never canonical. */
4fa62494
UW
2292 break;
2293
4fa62494 2294 default:
78267919
UW
2295 new_kind = INTERNALVAR_VALUE;
2296 new_data.value = value_copy (val);
2297 new_data.value->modifiable = 1;
4fa62494
UW
2298
2299 /* Force the value to be fetched from the target now, to avoid problems
2300 later when this internalvar is referenced and the target is gone or
2301 has changed. */
78267919
UW
2302 if (value_lazy (new_data.value))
2303 value_fetch_lazy (new_data.value);
4fa62494
UW
2304
2305 /* Release the value from the value chain to prevent it from being
2306 deleted by free_all_values. From here on this function should not
2307 call error () until new_data is installed into the var->u to avoid
2308 leaking memory. */
22bc8444 2309 release_value (new_data.value).release ();
9920b434
BH
2310
2311 /* Internal variables which are created from values with a dynamic
2312 location don't need the location property of the origin anymore.
2313 The resolved dynamic location is used prior then any other address
2314 when accessing the value.
2315 If we keep it, we would still refer to the origin value.
2316 Remove the location property in case it exist. */
2317 remove_dyn_prop (DYN_PROP_DATA_LOCATION, value_type (new_data.value));
2318
4fa62494
UW
2319 break;
2320 }
2321
2322 /* Clean up old contents. */
2323 clear_internalvar (var);
2324
2325 /* Switch over. */
78267919 2326 var->kind = new_kind;
4fa62494 2327 var->u = new_data;
c906108c
SS
2328 /* End code which must not call error(). */
2329}
2330
4fa62494
UW
2331void
2332set_internalvar_integer (struct internalvar *var, LONGEST l)
2333{
2334 /* Clean up old contents. */
2335 clear_internalvar (var);
2336
cab0c772
UW
2337 var->kind = INTERNALVAR_INTEGER;
2338 var->u.integer.type = NULL;
2339 var->u.integer.val = l;
78267919
UW
2340}
2341
2342void
2343set_internalvar_string (struct internalvar *var, const char *string)
2344{
2345 /* Clean up old contents. */
2346 clear_internalvar (var);
2347
2348 var->kind = INTERNALVAR_STRING;
2349 var->u.string = xstrdup (string);
4fa62494
UW
2350}
2351
2352static void
2353set_internalvar_function (struct internalvar *var, struct internal_function *f)
2354{
2355 /* Clean up old contents. */
2356 clear_internalvar (var);
2357
78267919
UW
2358 var->kind = INTERNALVAR_FUNCTION;
2359 var->u.fn.function = f;
2360 var->u.fn.canonical = 1;
2361 /* Variables installed here are always the canonical version. */
4fa62494
UW
2362}
2363
2364void
2365clear_internalvar (struct internalvar *var)
2366{
2367 /* Clean up old contents. */
78267919 2368 switch (var->kind)
4fa62494 2369 {
78267919 2370 case INTERNALVAR_VALUE:
22bc8444 2371 value_decref (var->u.value);
78267919
UW
2372 break;
2373
2374 case INTERNALVAR_STRING:
2375 xfree (var->u.string);
4fa62494
UW
2376 break;
2377
22d2b532
SDJ
2378 case INTERNALVAR_MAKE_VALUE:
2379 if (var->u.make_value.functions->destroy != NULL)
2380 var->u.make_value.functions->destroy (var->u.make_value.data);
2381 break;
2382
4fa62494 2383 default:
4fa62494
UW
2384 break;
2385 }
2386
78267919
UW
2387 /* Reset to void kind. */
2388 var->kind = INTERNALVAR_VOID;
4fa62494
UW
2389}
2390
c906108c 2391char *
4bf7b526 2392internalvar_name (const struct internalvar *var)
c906108c
SS
2393{
2394 return var->name;
2395}
2396
4fa62494
UW
2397static struct internal_function *
2398create_internal_function (const char *name,
2399 internal_function_fn handler, void *cookie)
bc3b79fd 2400{
bc3b79fd 2401 struct internal_function *ifn = XNEW (struct internal_function);
a109c7c1 2402
bc3b79fd
TJB
2403 ifn->name = xstrdup (name);
2404 ifn->handler = handler;
2405 ifn->cookie = cookie;
4fa62494 2406 return ifn;
bc3b79fd
TJB
2407}
2408
2409char *
2410value_internal_function_name (struct value *val)
2411{
4fa62494
UW
2412 struct internal_function *ifn;
2413 int result;
2414
2415 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
2416 result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
2417 gdb_assert (result);
2418
bc3b79fd
TJB
2419 return ifn->name;
2420}
2421
2422struct value *
d452c4bc
UW
2423call_internal_function (struct gdbarch *gdbarch,
2424 const struct language_defn *language,
2425 struct value *func, int argc, struct value **argv)
bc3b79fd 2426{
4fa62494
UW
2427 struct internal_function *ifn;
2428 int result;
2429
2430 gdb_assert (VALUE_LVAL (func) == lval_internalvar);
2431 result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
2432 gdb_assert (result);
2433
d452c4bc 2434 return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
bc3b79fd
TJB
2435}
2436
2437/* The 'function' command. This does nothing -- it is just a
2438 placeholder to let "help function NAME" work. This is also used as
2439 the implementation of the sub-command that is created when
2440 registering an internal function. */
2441static void
981a3fb3 2442function_command (const char *command, int from_tty)
bc3b79fd
TJB
2443{
2444 /* Do nothing. */
2445}
2446
2447/* Clean up if an internal function's command is destroyed. */
2448static void
2449function_destroyer (struct cmd_list_element *self, void *ignore)
2450{
6f937416 2451 xfree ((char *) self->name);
1947513d 2452 xfree ((char *) self->doc);
bc3b79fd
TJB
2453}
2454
2455/* Add a new internal function. NAME is the name of the function; DOC
2456 is a documentation string describing the function. HANDLER is
2457 called when the function is invoked. COOKIE is an arbitrary
2458 pointer which is passed to HANDLER and is intended for "user
2459 data". */
2460void
2461add_internal_function (const char *name, const char *doc,
2462 internal_function_fn handler, void *cookie)
2463{
2464 struct cmd_list_element *cmd;
4fa62494 2465 struct internal_function *ifn;
bc3b79fd 2466 struct internalvar *var = lookup_internalvar (name);
4fa62494
UW
2467
2468 ifn = create_internal_function (name, handler, cookie);
2469 set_internalvar_function (var, ifn);
bc3b79fd
TJB
2470
2471 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
2472 &functionlist);
2473 cmd->destroyer = function_destroyer;
2474}
2475
ae5a43e0
DJ
2476/* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
2477 prevent cycles / duplicates. */
2478
4e7a5ef5 2479void
ae5a43e0
DJ
2480preserve_one_value (struct value *value, struct objfile *objfile,
2481 htab_t copied_types)
2482{
2483 if (TYPE_OBJFILE (value->type) == objfile)
2484 value->type = copy_type_recursive (objfile, value->type, copied_types);
2485
2486 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
2487 value->enclosing_type = copy_type_recursive (objfile,
2488 value->enclosing_type,
2489 copied_types);
2490}
2491
78267919
UW
2492/* Likewise for internal variable VAR. */
2493
2494static void
2495preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
2496 htab_t copied_types)
2497{
2498 switch (var->kind)
2499 {
cab0c772
UW
2500 case INTERNALVAR_INTEGER:
2501 if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
2502 var->u.integer.type
2503 = copy_type_recursive (objfile, var->u.integer.type, copied_types);
2504 break;
2505
78267919
UW
2506 case INTERNALVAR_VALUE:
2507 preserve_one_value (var->u.value, objfile, copied_types);
2508 break;
2509 }
2510}
2511
ae5a43e0
DJ
2512/* Update the internal variables and value history when OBJFILE is
2513 discarded; we must copy the types out of the objfile. New global types
2514 will be created for every convenience variable which currently points to
2515 this objfile's types, and the convenience variables will be adjusted to
2516 use the new global types. */
c906108c
SS
2517
2518void
ae5a43e0 2519preserve_values (struct objfile *objfile)
c906108c 2520{
ae5a43e0 2521 htab_t copied_types;
52f0bd74 2522 struct internalvar *var;
ae5a43e0 2523 int i;
c906108c 2524
ae5a43e0
DJ
2525 /* Create the hash table. We allocate on the objfile's obstack, since
2526 it is soon to be deleted. */
2527 copied_types = create_copied_types_hash (objfile);
2528
4d0266a0
TT
2529 for (const value_ref_ptr &item : value_history)
2530 preserve_one_value (item.get (), objfile, copied_types);
ae5a43e0
DJ
2531
2532 for (var = internalvars; var; var = var->next)
78267919 2533 preserve_one_internalvar (var, objfile, copied_types);
ae5a43e0 2534
6dddc817 2535 preserve_ext_lang_values (objfile, copied_types);
a08702d6 2536
ae5a43e0 2537 htab_delete (copied_types);
c906108c
SS
2538}
2539
2540static void
ad25e423 2541show_convenience (const char *ignore, int from_tty)
c906108c 2542{
e17c207e 2543 struct gdbarch *gdbarch = get_current_arch ();
52f0bd74 2544 struct internalvar *var;
c906108c 2545 int varseen = 0;
79a45b7d 2546 struct value_print_options opts;
c906108c 2547
79a45b7d 2548 get_user_print_options (&opts);
c906108c
SS
2549 for (var = internalvars; var; var = var->next)
2550 {
c709acd1 2551
c906108c
SS
2552 if (!varseen)
2553 {
2554 varseen = 1;
2555 }
a3f17187 2556 printf_filtered (("$%s = "), var->name);
c709acd1 2557
492d29ea 2558 TRY
c709acd1
PA
2559 {
2560 struct value *val;
2561
2562 val = value_of_internalvar (gdbarch, var);
2563 value_print (val, gdb_stdout, &opts);
2564 }
492d29ea
PA
2565 CATCH (ex, RETURN_MASK_ERROR)
2566 {
2567 fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
2568 }
2569 END_CATCH
2570
a3f17187 2571 printf_filtered (("\n"));
c906108c
SS
2572 }
2573 if (!varseen)
f47f77df
DE
2574 {
2575 /* This text does not mention convenience functions on purpose.
2576 The user can't create them except via Python, and if Python support
2577 is installed this message will never be printed ($_streq will
2578 exist). */
2579 printf_unfiltered (_("No debugger convenience variables now defined.\n"
2580 "Convenience variables have "
2581 "names starting with \"$\";\n"
2582 "use \"set\" as in \"set "
2583 "$foo = 5\" to define them.\n"));
2584 }
c906108c
SS
2585}
2586\f
ba18742c
SM
2587
2588/* See value.h. */
e81e7f5e
SC
2589
2590struct value *
ba18742c 2591value_from_xmethod (xmethod_worker_up &&worker)
e81e7f5e 2592{
ba18742c 2593 struct value *v;
e81e7f5e 2594
ba18742c
SM
2595 v = allocate_value (builtin_type (target_gdbarch ())->xmethod);
2596 v->lval = lval_xcallable;
2597 v->location.xm_worker = worker.release ();
2598 v->modifiable = 0;
e81e7f5e 2599
ba18742c 2600 return v;
e81e7f5e
SC
2601}
2602
2ce1cdbf
DE
2603/* Return the type of the result of TYPE_CODE_XMETHOD value METHOD. */
2604
2605struct type *
2606result_type_of_xmethod (struct value *method, int argc, struct value **argv)
2607{
2608 gdb_assert (TYPE_CODE (value_type (method)) == TYPE_CODE_XMETHOD
2609 && method->lval == lval_xcallable && argc > 0);
2610
ba18742c
SM
2611 return method->location.xm_worker->get_result_type
2612 (argv[0], argv + 1, argc - 1);
2ce1cdbf
DE
2613}
2614
e81e7f5e
SC
2615/* Call the xmethod corresponding to the TYPE_CODE_XMETHOD value METHOD. */
2616
2617struct value *
2618call_xmethod (struct value *method, int argc, struct value **argv)
2619{
2620 gdb_assert (TYPE_CODE (value_type (method)) == TYPE_CODE_XMETHOD
2621 && method->lval == lval_xcallable && argc > 0);
2622
ba18742c 2623 return method->location.xm_worker->invoke (argv[0], argv + 1, argc - 1);
e81e7f5e
SC
2624}
2625\f
c906108c
SS
2626/* Extract a value as a C number (either long or double).
2627 Knows how to convert fixed values to double, or
2628 floating values to long.
2629 Does not deallocate the value. */
2630
2631LONGEST
f23631e4 2632value_as_long (struct value *val)
c906108c
SS
2633{
2634 /* This coerces arrays and functions, which is necessary (e.g.
2635 in disassemble_command). It also dereferences references, which
2636 I suspect is the most logical thing to do. */
994b9211 2637 val = coerce_array (val);
0fd88904 2638 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
2639}
2640
581e13c1 2641/* Extract a value as a C pointer. Does not deallocate the value.
4478b372
JB
2642 Note that val's type may not actually be a pointer; value_as_long
2643 handles all the cases. */
c906108c 2644CORE_ADDR
f23631e4 2645value_as_address (struct value *val)
c906108c 2646{
50810684
UW
2647 struct gdbarch *gdbarch = get_type_arch (value_type (val));
2648
c906108c
SS
2649 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2650 whether we want this to be true eventually. */
2651#if 0
bf6ae464 2652 /* gdbarch_addr_bits_remove is wrong if we are being called for a
c906108c
SS
2653 non-address (e.g. argument to "signal", "info break", etc.), or
2654 for pointers to char, in which the low bits *are* significant. */
50810684 2655 return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
c906108c 2656#else
f312f057
JB
2657
2658 /* There are several targets (IA-64, PowerPC, and others) which
2659 don't represent pointers to functions as simply the address of
2660 the function's entry point. For example, on the IA-64, a
2661 function pointer points to a two-word descriptor, generated by
2662 the linker, which contains the function's entry point, and the
2663 value the IA-64 "global pointer" register should have --- to
2664 support position-independent code. The linker generates
2665 descriptors only for those functions whose addresses are taken.
2666
2667 On such targets, it's difficult for GDB to convert an arbitrary
2668 function address into a function pointer; it has to either find
2669 an existing descriptor for that function, or call malloc and
2670 build its own. On some targets, it is impossible for GDB to
2671 build a descriptor at all: the descriptor must contain a jump
2672 instruction; data memory cannot be executed; and code memory
2673 cannot be modified.
2674
2675 Upon entry to this function, if VAL is a value of type `function'
2676 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
42ae5230 2677 value_address (val) is the address of the function. This is what
f312f057
JB
2678 you'll get if you evaluate an expression like `main'. The call
2679 to COERCE_ARRAY below actually does all the usual unary
2680 conversions, which includes converting values of type `function'
2681 to `pointer to function'. This is the challenging conversion
2682 discussed above. Then, `unpack_long' will convert that pointer
2683 back into an address.
2684
2685 So, suppose the user types `disassemble foo' on an architecture
2686 with a strange function pointer representation, on which GDB
2687 cannot build its own descriptors, and suppose further that `foo'
2688 has no linker-built descriptor. The address->pointer conversion
2689 will signal an error and prevent the command from running, even
2690 though the next step would have been to convert the pointer
2691 directly back into the same address.
2692
2693 The following shortcut avoids this whole mess. If VAL is a
2694 function, just return its address directly. */
df407dfe
AC
2695 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
2696 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
42ae5230 2697 return value_address (val);
f312f057 2698
994b9211 2699 val = coerce_array (val);
fc0c74b1
AC
2700
2701 /* Some architectures (e.g. Harvard), map instruction and data
2702 addresses onto a single large unified address space. For
2703 instance: An architecture may consider a large integer in the
2704 range 0x10000000 .. 0x1000ffff to already represent a data
2705 addresses (hence not need a pointer to address conversion) while
2706 a small integer would still need to be converted integer to
2707 pointer to address. Just assume such architectures handle all
2708 integer conversions in a single function. */
2709
2710 /* JimB writes:
2711
2712 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
2713 must admonish GDB hackers to make sure its behavior matches the
2714 compiler's, whenever possible.
2715
2716 In general, I think GDB should evaluate expressions the same way
2717 the compiler does. When the user copies an expression out of
2718 their source code and hands it to a `print' command, they should
2719 get the same value the compiler would have computed. Any
2720 deviation from this rule can cause major confusion and annoyance,
2721 and needs to be justified carefully. In other words, GDB doesn't
2722 really have the freedom to do these conversions in clever and
2723 useful ways.
2724
2725 AndrewC pointed out that users aren't complaining about how GDB
2726 casts integers to pointers; they are complaining that they can't
2727 take an address from a disassembly listing and give it to `x/i'.
2728 This is certainly important.
2729
79dd2d24 2730 Adding an architecture method like integer_to_address() certainly
fc0c74b1
AC
2731 makes it possible for GDB to "get it right" in all circumstances
2732 --- the target has complete control over how things get done, so
2733 people can Do The Right Thing for their target without breaking
2734 anyone else. The standard doesn't specify how integers get
2735 converted to pointers; usually, the ABI doesn't either, but
2736 ABI-specific code is a more reasonable place to handle it. */
2737
df407dfe 2738 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
aa006118 2739 && !TYPE_IS_REFERENCE (value_type (val))
50810684
UW
2740 && gdbarch_integer_to_address_p (gdbarch))
2741 return gdbarch_integer_to_address (gdbarch, value_type (val),
0fd88904 2742 value_contents (val));
fc0c74b1 2743
0fd88904 2744 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
2745#endif
2746}
2747\f
2748/* Unpack raw data (copied from debugee, target byte order) at VALADDR
2749 as a long, or as a double, assuming the raw data is described
2750 by type TYPE. Knows how to convert different sizes of values
2751 and can convert between fixed and floating point. We don't assume
2752 any alignment for the raw data. Return value is in host byte order.
2753
2754 If you want functions and arrays to be coerced to pointers, and
2755 references to be dereferenced, call value_as_long() instead.
2756
2757 C++: It is assumed that the front-end has taken care of
2758 all matters concerning pointers to members. A pointer
2759 to member which reaches here is considered to be equivalent
2760 to an INT (or some size). After all, it is only an offset. */
2761
2762LONGEST
fc1a4b47 2763unpack_long (struct type *type, const gdb_byte *valaddr)
c906108c 2764{
e17a4113 2765 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
52f0bd74
AC
2766 enum type_code code = TYPE_CODE (type);
2767 int len = TYPE_LENGTH (type);
2768 int nosign = TYPE_UNSIGNED (type);
c906108c 2769
c906108c
SS
2770 switch (code)
2771 {
2772 case TYPE_CODE_TYPEDEF:
2773 return unpack_long (check_typedef (type), valaddr);
2774 case TYPE_CODE_ENUM:
4f2aea11 2775 case TYPE_CODE_FLAGS:
c906108c
SS
2776 case TYPE_CODE_BOOL:
2777 case TYPE_CODE_INT:
2778 case TYPE_CODE_CHAR:
2779 case TYPE_CODE_RANGE:
0d5de010 2780 case TYPE_CODE_MEMBERPTR:
c906108c 2781 if (nosign)
e17a4113 2782 return extract_unsigned_integer (valaddr, len, byte_order);
c906108c 2783 else
e17a4113 2784 return extract_signed_integer (valaddr, len, byte_order);
c906108c
SS
2785
2786 case TYPE_CODE_FLT:
4ef30785 2787 case TYPE_CODE_DECFLOAT:
50637b26 2788 return target_float_to_longest (valaddr, type);
4ef30785 2789
c906108c
SS
2790 case TYPE_CODE_PTR:
2791 case TYPE_CODE_REF:
aa006118 2792 case TYPE_CODE_RVALUE_REF:
c906108c 2793 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
c5aa993b 2794 whether we want this to be true eventually. */
4478b372 2795 return extract_typed_address (valaddr, type);
c906108c 2796
c906108c 2797 default:
8a3fe4f8 2798 error (_("Value can't be converted to integer."));
c906108c 2799 }
c5aa993b 2800 return 0; /* Placate lint. */
c906108c
SS
2801}
2802
c906108c
SS
2803/* Unpack raw data (copied from debugee, target byte order) at VALADDR
2804 as a CORE_ADDR, assuming the raw data is described by type TYPE.
2805 We don't assume any alignment for the raw data. Return value is in
2806 host byte order.
2807
2808 If you want functions and arrays to be coerced to pointers, and
1aa20aa8 2809 references to be dereferenced, call value_as_address() instead.
c906108c
SS
2810
2811 C++: It is assumed that the front-end has taken care of
2812 all matters concerning pointers to members. A pointer
2813 to member which reaches here is considered to be equivalent
2814 to an INT (or some size). After all, it is only an offset. */
2815
2816CORE_ADDR
fc1a4b47 2817unpack_pointer (struct type *type, const gdb_byte *valaddr)
c906108c
SS
2818{
2819 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2820 whether we want this to be true eventually. */
2821 return unpack_long (type, valaddr);
2822}
4478b372 2823
70100014
UW
2824bool
2825is_floating_value (struct value *val)
2826{
2827 struct type *type = check_typedef (value_type (val));
2828
2829 if (is_floating_type (type))
2830 {
2831 if (!target_float_is_valid (value_contents (val), type))
2832 error (_("Invalid floating value found in program."));
2833 return true;
2834 }
2835
2836 return false;
2837}
2838
c906108c 2839\f
1596cb5d 2840/* Get the value of the FIELDNO'th field (which must be static) of
686d4def 2841 TYPE. */
c906108c 2842
f23631e4 2843struct value *
fba45db2 2844value_static_field (struct type *type, int fieldno)
c906108c 2845{
948e66d9
DJ
2846 struct value *retval;
2847
1596cb5d 2848 switch (TYPE_FIELD_LOC_KIND (type, fieldno))
c906108c 2849 {
1596cb5d 2850 case FIELD_LOC_KIND_PHYSADDR:
52e9fde8
SS
2851 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2852 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1596cb5d
DE
2853 break;
2854 case FIELD_LOC_KIND_PHYSNAME:
c906108c 2855 {
ff355380 2856 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
581e13c1 2857 /* TYPE_FIELD_NAME (type, fieldno); */
d12307c1 2858 struct block_symbol sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
94af9270 2859
d12307c1 2860 if (sym.symbol == NULL)
c906108c 2861 {
a109c7c1 2862 /* With some compilers, e.g. HP aCC, static data members are
581e13c1 2863 reported as non-debuggable symbols. */
3b7344d5
TT
2864 struct bound_minimal_symbol msym
2865 = lookup_minimal_symbol (phys_name, NULL, NULL);
c2e0e465 2866 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
a109c7c1 2867
3b7344d5 2868 if (!msym.minsym)
c2e0e465 2869 retval = allocate_optimized_out_value (field_type);
c906108c 2870 else
c2e0e465 2871 retval = value_at_lazy (field_type, BMSYMBOL_VALUE_ADDRESS (msym));
c906108c
SS
2872 }
2873 else
d12307c1 2874 retval = value_of_variable (sym.symbol, sym.block);
1596cb5d 2875 break;
c906108c 2876 }
1596cb5d 2877 default:
f3574227 2878 gdb_assert_not_reached ("unexpected field location kind");
1596cb5d
DE
2879 }
2880
948e66d9 2881 return retval;
c906108c
SS
2882}
2883
4dfea560
DE
2884/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
2885 You have to be careful here, since the size of the data area for the value
2886 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
2887 than the old enclosing type, you have to allocate more space for the
2888 data. */
2b127877 2889
4dfea560
DE
2890void
2891set_value_enclosing_type (struct value *val, struct type *new_encl_type)
2b127877 2892{
5fdf6324
AB
2893 if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
2894 {
2895 check_type_length_before_alloc (new_encl_type);
2896 val->contents
2897 = (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
2898 }
3e3d7139
JG
2899
2900 val->enclosing_type = new_encl_type;
2b127877
DB
2901}
2902
c906108c
SS
2903/* Given a value ARG1 (offset by OFFSET bytes)
2904 of a struct or union type ARG_TYPE,
2905 extract and return the value of one of its (non-static) fields.
581e13c1 2906 FIELDNO says which field. */
c906108c 2907
f23631e4 2908struct value *
6b850546 2909value_primitive_field (struct value *arg1, LONGEST offset,
aa1ee363 2910 int fieldno, struct type *arg_type)
c906108c 2911{
f23631e4 2912 struct value *v;
52f0bd74 2913 struct type *type;
3ae385af
SM
2914 struct gdbarch *arch = get_value_arch (arg1);
2915 int unit_size = gdbarch_addressable_memory_unit_size (arch);
c906108c 2916
f168693b 2917 arg_type = check_typedef (arg_type);
c906108c 2918 type = TYPE_FIELD_TYPE (arg_type, fieldno);
c54eabfa
JK
2919
2920 /* Call check_typedef on our type to make sure that, if TYPE
2921 is a TYPE_CODE_TYPEDEF, its length is set to the length
2922 of the target type instead of zero. However, we do not
2923 replace the typedef type by the target type, because we want
2924 to keep the typedef in order to be able to print the type
2925 description correctly. */
2926 check_typedef (type);
c906108c 2927
691a26f5 2928 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
c906108c 2929 {
22c05d8a
JK
2930 /* Handle packed fields.
2931
2932 Create a new value for the bitfield, with bitpos and bitsize
4ea48cc1
DJ
2933 set. If possible, arrange offset and bitpos so that we can
2934 do a single aligned read of the size of the containing type.
2935 Otherwise, adjust offset to the byte containing the first
2936 bit. Assume that the address, offset, and embedded offset
2937 are sufficiently aligned. */
22c05d8a 2938
6b850546
DT
2939 LONGEST bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
2940 LONGEST container_bitsize = TYPE_LENGTH (type) * 8;
4ea48cc1 2941
9a0dc9e3
PA
2942 v = allocate_value_lazy (type);
2943 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
2944 if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
2945 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST))
2946 v->bitpos = bitpos % container_bitsize;
4ea48cc1 2947 else
9a0dc9e3
PA
2948 v->bitpos = bitpos % 8;
2949 v->offset = (value_embedded_offset (arg1)
2950 + offset
2951 + (bitpos - v->bitpos) / 8);
2952 set_value_parent (v, arg1);
2953 if (!value_lazy (arg1))
2954 value_fetch_lazy (v);
c906108c
SS
2955 }
2956 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
2957 {
2958 /* This field is actually a base subobject, so preserve the
39d37385
PA
2959 entire object's contents for later references to virtual
2960 bases, etc. */
6b850546 2961 LONGEST boffset;
a4e2ee12
DJ
2962
2963 /* Lazy register values with offsets are not supported. */
2964 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
2965 value_fetch_lazy (arg1);
2966
9a0dc9e3
PA
2967 /* We special case virtual inheritance here because this
2968 requires access to the contents, which we would rather avoid
2969 for references to ordinary fields of unavailable values. */
2970 if (BASETYPE_VIA_VIRTUAL (arg_type, fieldno))
2971 boffset = baseclass_offset (arg_type, fieldno,
2972 value_contents (arg1),
2973 value_embedded_offset (arg1),
2974 value_address (arg1),
2975 arg1);
c906108c 2976 else
9a0dc9e3 2977 boffset = TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
691a26f5 2978
9a0dc9e3
PA
2979 if (value_lazy (arg1))
2980 v = allocate_value_lazy (value_enclosing_type (arg1));
2981 else
2982 {
2983 v = allocate_value (value_enclosing_type (arg1));
2984 value_contents_copy_raw (v, 0, arg1, 0,
2985 TYPE_LENGTH (value_enclosing_type (arg1)));
3e3d7139 2986 }
9a0dc9e3
PA
2987 v->type = type;
2988 v->offset = value_offset (arg1);
2989 v->embedded_offset = offset + value_embedded_offset (arg1) + boffset;
c906108c 2990 }
9920b434
BH
2991 else if (NULL != TYPE_DATA_LOCATION (type))
2992 {
2993 /* Field is a dynamic data member. */
2994
2995 gdb_assert (0 == offset);
2996 /* We expect an already resolved data location. */
2997 gdb_assert (PROP_CONST == TYPE_DATA_LOCATION_KIND (type));
2998 /* For dynamic data types defer memory allocation
2999 until we actual access the value. */
3000 v = allocate_value_lazy (type);
3001 }
c906108c
SS
3002 else
3003 {
3004 /* Plain old data member */
3ae385af
SM
3005 offset += (TYPE_FIELD_BITPOS (arg_type, fieldno)
3006 / (HOST_CHAR_BIT * unit_size));
a4e2ee12
DJ
3007
3008 /* Lazy register values with offsets are not supported. */
3009 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
3010 value_fetch_lazy (arg1);
3011
9a0dc9e3 3012 if (value_lazy (arg1))
3e3d7139 3013 v = allocate_value_lazy (type);
c906108c 3014 else
3e3d7139
JG
3015 {
3016 v = allocate_value (type);
39d37385
PA
3017 value_contents_copy_raw (v, value_embedded_offset (v),
3018 arg1, value_embedded_offset (arg1) + offset,
3ae385af 3019 type_length_units (type));
3e3d7139 3020 }
df407dfe 3021 v->offset = (value_offset (arg1) + offset
13c3b5f5 3022 + value_embedded_offset (arg1));
c906108c 3023 }
74bcbdf3 3024 set_value_component_location (v, arg1);
c906108c
SS
3025 return v;
3026}
3027
3028/* Given a value ARG1 of a struct or union type,
3029 extract and return the value of one of its (non-static) fields.
581e13c1 3030 FIELDNO says which field. */
c906108c 3031
f23631e4 3032struct value *
aa1ee363 3033value_field (struct value *arg1, int fieldno)
c906108c 3034{
df407dfe 3035 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
c906108c
SS
3036}
3037
3038/* Return a non-virtual function as a value.
3039 F is the list of member functions which contains the desired method.
0478d61c
FF
3040 J is an index into F which provides the desired method.
3041
3042 We only use the symbol for its address, so be happy with either a
581e13c1 3043 full symbol or a minimal symbol. */
c906108c 3044
f23631e4 3045struct value *
3e43a32a
MS
3046value_fn_field (struct value **arg1p, struct fn_field *f,
3047 int j, struct type *type,
6b850546 3048 LONGEST offset)
c906108c 3049{
f23631e4 3050 struct value *v;
52f0bd74 3051 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1d06ead6 3052 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c906108c 3053 struct symbol *sym;
7c7b6655 3054 struct bound_minimal_symbol msym;
c906108c 3055
d12307c1 3056 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0).symbol;
5ae326fa 3057 if (sym != NULL)
0478d61c 3058 {
7c7b6655 3059 memset (&msym, 0, sizeof (msym));
5ae326fa
AC
3060 }
3061 else
3062 {
3063 gdb_assert (sym == NULL);
7c7b6655
TT
3064 msym = lookup_bound_minimal_symbol (physname);
3065 if (msym.minsym == NULL)
5ae326fa 3066 return NULL;
0478d61c
FF
3067 }
3068
c906108c 3069 v = allocate_value (ftype);
1a088441 3070 VALUE_LVAL (v) = lval_memory;
0478d61c
FF
3071 if (sym)
3072 {
42ae5230 3073 set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
0478d61c
FF
3074 }
3075 else
3076 {
bccdca4a
UW
3077 /* The minimal symbol might point to a function descriptor;
3078 resolve it to the actual code address instead. */
7c7b6655 3079 struct objfile *objfile = msym.objfile;
bccdca4a
UW
3080 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3081
42ae5230
TT
3082 set_value_address (v,
3083 gdbarch_convert_from_func_ptr_addr
77e371c0 3084 (gdbarch, BMSYMBOL_VALUE_ADDRESS (msym), &current_target));
0478d61c 3085 }
c906108c
SS
3086
3087 if (arg1p)
c5aa993b 3088 {
df407dfe 3089 if (type != value_type (*arg1p))
c5aa993b
JM
3090 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
3091 value_addr (*arg1p)));
3092
070ad9f0 3093 /* Move the `this' pointer according to the offset.
581e13c1 3094 VALUE_OFFSET (*arg1p) += offset; */
c906108c
SS
3095 }
3096
3097 return v;
3098}
3099
c906108c 3100\f
c906108c 3101
4875ffdb
PA
3102/* Unpack a bitfield of the specified FIELD_TYPE, from the object at
3103 VALADDR, and store the result in *RESULT.
15ce8941
TT
3104 The bitfield starts at BITPOS bits and contains BITSIZE bits; if
3105 BITSIZE is zero, then the length is taken from FIELD_TYPE.
c906108c 3106
4875ffdb
PA
3107 Extracting bits depends on endianness of the machine. Compute the
3108 number of least significant bits to discard. For big endian machines,
3109 we compute the total number of bits in the anonymous object, subtract
3110 off the bit count from the MSB of the object to the MSB of the
3111 bitfield, then the size of the bitfield, which leaves the LSB discard
3112 count. For little endian machines, the discard count is simply the
3113 number of bits from the LSB of the anonymous object to the LSB of the
3114 bitfield.
3115
3116 If the field is signed, we also do sign extension. */
3117
3118static LONGEST
3119unpack_bits_as_long (struct type *field_type, const gdb_byte *valaddr,
6b850546 3120 LONGEST bitpos, LONGEST bitsize)
c906108c 3121{
4ea48cc1 3122 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
c906108c
SS
3123 ULONGEST val;
3124 ULONGEST valmask;
c906108c 3125 int lsbcount;
6b850546
DT
3126 LONGEST bytes_read;
3127 LONGEST read_offset;
c906108c 3128
4a76eae5
DJ
3129 /* Read the minimum number of bytes required; there may not be
3130 enough bytes to read an entire ULONGEST. */
f168693b 3131 field_type = check_typedef (field_type);
4a76eae5
DJ
3132 if (bitsize)
3133 bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
3134 else
15ce8941
TT
3135 {
3136 bytes_read = TYPE_LENGTH (field_type);
3137 bitsize = 8 * bytes_read;
3138 }
4a76eae5 3139
5467c6c8
PA
3140 read_offset = bitpos / 8;
3141
4875ffdb 3142 val = extract_unsigned_integer (valaddr + read_offset,
4a76eae5 3143 bytes_read, byte_order);
c906108c 3144
581e13c1 3145 /* Extract bits. See comment above. */
c906108c 3146
4ea48cc1 3147 if (gdbarch_bits_big_endian (get_type_arch (field_type)))
4a76eae5 3148 lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
c906108c
SS
3149 else
3150 lsbcount = (bitpos % 8);
3151 val >>= lsbcount;
3152
3153 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
581e13c1 3154 If the field is signed, and is negative, then sign extend. */
c906108c 3155
15ce8941 3156 if (bitsize < 8 * (int) sizeof (val))
c906108c
SS
3157 {
3158 valmask = (((ULONGEST) 1) << bitsize) - 1;
3159 val &= valmask;
3160 if (!TYPE_UNSIGNED (field_type))
3161 {
3162 if (val & (valmask ^ (valmask >> 1)))
3163 {
3164 val |= ~valmask;
3165 }
3166 }
3167 }
5467c6c8 3168
4875ffdb 3169 return val;
5467c6c8
PA
3170}
3171
3172/* Unpack a field FIELDNO of the specified TYPE, from the object at
3173 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
3174 ORIGINAL_VALUE, which must not be NULL. See
3175 unpack_value_bits_as_long for more details. */
3176
3177int
3178unpack_value_field_as_long (struct type *type, const gdb_byte *valaddr,
6b850546 3179 LONGEST embedded_offset, int fieldno,
5467c6c8
PA
3180 const struct value *val, LONGEST *result)
3181{
4875ffdb
PA
3182 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
3183 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
3184 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
3185 int bit_offset;
3186
5467c6c8
PA
3187 gdb_assert (val != NULL);
3188
4875ffdb
PA
3189 bit_offset = embedded_offset * TARGET_CHAR_BIT + bitpos;
3190 if (value_bits_any_optimized_out (val, bit_offset, bitsize)
3191 || !value_bits_available (val, bit_offset, bitsize))
3192 return 0;
3193
3194 *result = unpack_bits_as_long (field_type, valaddr + embedded_offset,
3195 bitpos, bitsize);
3196 return 1;
5467c6c8
PA
3197}
3198
3199/* Unpack a field FIELDNO of the specified TYPE, from the anonymous
4875ffdb 3200 object at VALADDR. See unpack_bits_as_long for more details. */
5467c6c8
PA
3201
3202LONGEST
3203unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
3204{
4875ffdb
PA
3205 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
3206 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
3207 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
5467c6c8 3208
4875ffdb
PA
3209 return unpack_bits_as_long (field_type, valaddr, bitpos, bitsize);
3210}
3211
3212/* Unpack a bitfield of BITSIZE bits found at BITPOS in the object at
3213 VALADDR + EMBEDDEDOFFSET that has the type of DEST_VAL and store
3214 the contents in DEST_VAL, zero or sign extending if the type of
3215 DEST_VAL is wider than BITSIZE. VALADDR points to the contents of
3216 VAL. If the VAL's contents required to extract the bitfield from
3217 are unavailable/optimized out, DEST_VAL is correspondingly
3218 marked unavailable/optimized out. */
3219
bb9d5f81 3220void
4875ffdb 3221unpack_value_bitfield (struct value *dest_val,
6b850546
DT
3222 LONGEST bitpos, LONGEST bitsize,
3223 const gdb_byte *valaddr, LONGEST embedded_offset,
4875ffdb
PA
3224 const struct value *val)
3225{
3226 enum bfd_endian byte_order;
3227 int src_bit_offset;
3228 int dst_bit_offset;
4875ffdb
PA
3229 struct type *field_type = value_type (dest_val);
3230
4875ffdb 3231 byte_order = gdbarch_byte_order (get_type_arch (field_type));
e5ca03b4
PA
3232
3233 /* First, unpack and sign extend the bitfield as if it was wholly
3234 valid. Optimized out/unavailable bits are read as zero, but
3235 that's OK, as they'll end up marked below. If the VAL is
3236 wholly-invalid we may have skipped allocating its contents,
3237 though. See allocate_optimized_out_value. */
3238 if (valaddr != NULL)
3239 {
3240 LONGEST num;
3241
3242 num = unpack_bits_as_long (field_type, valaddr + embedded_offset,
3243 bitpos, bitsize);
3244 store_signed_integer (value_contents_raw (dest_val),
3245 TYPE_LENGTH (field_type), byte_order, num);
3246 }
4875ffdb
PA
3247
3248 /* Now copy the optimized out / unavailability ranges to the right
3249 bits. */
3250 src_bit_offset = embedded_offset * TARGET_CHAR_BIT + bitpos;
3251 if (byte_order == BFD_ENDIAN_BIG)
3252 dst_bit_offset = TYPE_LENGTH (field_type) * TARGET_CHAR_BIT - bitsize;
3253 else
3254 dst_bit_offset = 0;
3255 value_ranges_copy_adjusted (dest_val, dst_bit_offset,
3256 val, src_bit_offset, bitsize);
5467c6c8
PA
3257}
3258
3259/* Return a new value with type TYPE, which is FIELDNO field of the
3260 object at VALADDR + EMBEDDEDOFFSET. VALADDR points to the contents
3261 of VAL. If the VAL's contents required to extract the bitfield
4875ffdb
PA
3262 from are unavailable/optimized out, the new value is
3263 correspondingly marked unavailable/optimized out. */
5467c6c8
PA
3264
3265struct value *
3266value_field_bitfield (struct type *type, int fieldno,
3267 const gdb_byte *valaddr,
6b850546 3268 LONGEST embedded_offset, const struct value *val)
5467c6c8 3269{
4875ffdb
PA
3270 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
3271 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
3272 struct value *res_val = allocate_value (TYPE_FIELD_TYPE (type, fieldno));
5467c6c8 3273
4875ffdb
PA
3274 unpack_value_bitfield (res_val, bitpos, bitsize,
3275 valaddr, embedded_offset, val);
3276
3277 return res_val;
4ea48cc1
DJ
3278}
3279
c906108c
SS
3280/* Modify the value of a bitfield. ADDR points to a block of memory in
3281 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
3282 is the desired value of the field, in host byte order. BITPOS and BITSIZE
581e13c1 3283 indicate which bits (in target bit order) comprise the bitfield.
19f220c3 3284 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
f4e88c8e 3285 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
c906108c
SS
3286
3287void
50810684 3288modify_field (struct type *type, gdb_byte *addr,
6b850546 3289 LONGEST fieldval, LONGEST bitpos, LONGEST bitsize)
c906108c 3290{
e17a4113 3291 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
f4e88c8e
PH
3292 ULONGEST oword;
3293 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
6b850546 3294 LONGEST bytesize;
19f220c3
JK
3295
3296 /* Normalize BITPOS. */
3297 addr += bitpos / 8;
3298 bitpos %= 8;
c906108c
SS
3299
3300 /* If a negative fieldval fits in the field in question, chop
3301 off the sign extension bits. */
f4e88c8e
PH
3302 if ((~fieldval & ~(mask >> 1)) == 0)
3303 fieldval &= mask;
c906108c
SS
3304
3305 /* Warn if value is too big to fit in the field in question. */
f4e88c8e 3306 if (0 != (fieldval & ~mask))
c906108c
SS
3307 {
3308 /* FIXME: would like to include fieldval in the message, but
c5aa993b 3309 we don't have a sprintf_longest. */
6b850546 3310 warning (_("Value does not fit in %s bits."), plongest (bitsize));
c906108c
SS
3311
3312 /* Truncate it, otherwise adjoining fields may be corrupted. */
f4e88c8e 3313 fieldval &= mask;
c906108c
SS
3314 }
3315
19f220c3
JK
3316 /* Ensure no bytes outside of the modified ones get accessed as it may cause
3317 false valgrind reports. */
3318
3319 bytesize = (bitpos + bitsize + 7) / 8;
3320 oword = extract_unsigned_integer (addr, bytesize, byte_order);
c906108c
SS
3321
3322 /* Shifting for bit field depends on endianness of the target machine. */
50810684 3323 if (gdbarch_bits_big_endian (get_type_arch (type)))
19f220c3 3324 bitpos = bytesize * 8 - bitpos - bitsize;
c906108c 3325
f4e88c8e 3326 oword &= ~(mask << bitpos);
c906108c
SS
3327 oword |= fieldval << bitpos;
3328
19f220c3 3329 store_unsigned_integer (addr, bytesize, byte_order, oword);
c906108c
SS
3330}
3331\f
14d06750 3332/* Pack NUM into BUF using a target format of TYPE. */
c906108c 3333
14d06750
DJ
3334void
3335pack_long (gdb_byte *buf, struct type *type, LONGEST num)
c906108c 3336{
e17a4113 3337 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
6b850546 3338 LONGEST len;
14d06750
DJ
3339
3340 type = check_typedef (type);
c906108c
SS
3341 len = TYPE_LENGTH (type);
3342
14d06750 3343 switch (TYPE_CODE (type))
c906108c 3344 {
c906108c
SS
3345 case TYPE_CODE_INT:
3346 case TYPE_CODE_CHAR:
3347 case TYPE_CODE_ENUM:
4f2aea11 3348 case TYPE_CODE_FLAGS:
c906108c
SS
3349 case TYPE_CODE_BOOL:
3350 case TYPE_CODE_RANGE:
0d5de010 3351 case TYPE_CODE_MEMBERPTR:
e17a4113 3352 store_signed_integer (buf, len, byte_order, num);
c906108c 3353 break;
c5aa993b 3354
c906108c 3355 case TYPE_CODE_REF:
aa006118 3356 case TYPE_CODE_RVALUE_REF:
c906108c 3357 case TYPE_CODE_PTR:
14d06750 3358 store_typed_address (buf, type, (CORE_ADDR) num);
c906108c 3359 break;
c5aa993b 3360
50637b26
UW
3361 case TYPE_CODE_FLT:
3362 case TYPE_CODE_DECFLOAT:
3363 target_float_from_longest (buf, type, num);
3364 break;
3365
c906108c 3366 default:
14d06750
DJ
3367 error (_("Unexpected type (%d) encountered for integer constant."),
3368 TYPE_CODE (type));
c906108c 3369 }
14d06750
DJ
3370}
3371
3372
595939de
PM
3373/* Pack NUM into BUF using a target format of TYPE. */
3374
70221824 3375static void
595939de
PM
3376pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
3377{
6b850546 3378 LONGEST len;
595939de
PM
3379 enum bfd_endian byte_order;
3380
3381 type = check_typedef (type);
3382 len = TYPE_LENGTH (type);
3383 byte_order = gdbarch_byte_order (get_type_arch (type));
3384
3385 switch (TYPE_CODE (type))
3386 {
3387 case TYPE_CODE_INT:
3388 case TYPE_CODE_CHAR:
3389 case TYPE_CODE_ENUM:
3390 case TYPE_CODE_FLAGS:
3391 case TYPE_CODE_BOOL:
3392 case TYPE_CODE_RANGE:
3393 case TYPE_CODE_MEMBERPTR:
3394 store_unsigned_integer (buf, len, byte_order, num);
3395 break;
3396
3397 case TYPE_CODE_REF:
aa006118 3398 case TYPE_CODE_RVALUE_REF:
595939de
PM
3399 case TYPE_CODE_PTR:
3400 store_typed_address (buf, type, (CORE_ADDR) num);
3401 break;
3402
50637b26
UW
3403 case TYPE_CODE_FLT:
3404 case TYPE_CODE_DECFLOAT:
3405 target_float_from_ulongest (buf, type, num);
3406 break;
3407
595939de 3408 default:
3e43a32a
MS
3409 error (_("Unexpected type (%d) encountered "
3410 "for unsigned integer constant."),
595939de
PM
3411 TYPE_CODE (type));
3412 }
3413}
3414
3415
14d06750
DJ
3416/* Convert C numbers into newly allocated values. */
3417
3418struct value *
3419value_from_longest (struct type *type, LONGEST num)
3420{
3421 struct value *val = allocate_value (type);
3422
3423 pack_long (value_contents_raw (val), type, num);
c906108c
SS
3424 return val;
3425}
3426
4478b372 3427
595939de
PM
3428/* Convert C unsigned numbers into newly allocated values. */
3429
3430struct value *
3431value_from_ulongest (struct type *type, ULONGEST num)
3432{
3433 struct value *val = allocate_value (type);
3434
3435 pack_unsigned_long (value_contents_raw (val), type, num);
3436
3437 return val;
3438}
3439
3440
4478b372 3441/* Create a value representing a pointer of type TYPE to the address
cb417230 3442 ADDR. */
80180f79 3443
f23631e4 3444struct value *
4478b372
JB
3445value_from_pointer (struct type *type, CORE_ADDR addr)
3446{
cb417230 3447 struct value *val = allocate_value (type);
a109c7c1 3448
80180f79 3449 store_typed_address (value_contents_raw (val),
cb417230 3450 check_typedef (type), addr);
4478b372
JB
3451 return val;
3452}
3453
3454
012370f6
TT
3455/* Create a value of type TYPE whose contents come from VALADDR, if it
3456 is non-null, and whose memory address (in the inferior) is
3457 ADDRESS. The type of the created value may differ from the passed
3458 type TYPE. Make sure to retrieve values new type after this call.
3459 Note that TYPE is not passed through resolve_dynamic_type; this is
3460 a special API intended for use only by Ada. */
3461
3462struct value *
3463value_from_contents_and_address_unresolved (struct type *type,
3464 const gdb_byte *valaddr,
3465 CORE_ADDR address)
3466{
3467 struct value *v;
3468
3469 if (valaddr == NULL)
3470 v = allocate_value_lazy (type);
3471 else
3472 v = value_from_contents (type, valaddr);
012370f6 3473 VALUE_LVAL (v) = lval_memory;
1a088441 3474 set_value_address (v, address);
012370f6
TT
3475 return v;
3476}
3477
8acb6b92
TT
3478/* Create a value of type TYPE whose contents come from VALADDR, if it
3479 is non-null, and whose memory address (in the inferior) is
80180f79
SA
3480 ADDRESS. The type of the created value may differ from the passed
3481 type TYPE. Make sure to retrieve values new type after this call. */
8acb6b92
TT
3482
3483struct value *
3484value_from_contents_and_address (struct type *type,
3485 const gdb_byte *valaddr,
3486 CORE_ADDR address)
3487{
c3345124 3488 struct type *resolved_type = resolve_dynamic_type (type, valaddr, address);
d36430db 3489 struct type *resolved_type_no_typedef = check_typedef (resolved_type);
41e8491f 3490 struct value *v;
a109c7c1 3491
8acb6b92 3492 if (valaddr == NULL)
80180f79 3493 v = allocate_value_lazy (resolved_type);
8acb6b92 3494 else
80180f79 3495 v = value_from_contents (resolved_type, valaddr);
d36430db
JB
3496 if (TYPE_DATA_LOCATION (resolved_type_no_typedef) != NULL
3497 && TYPE_DATA_LOCATION_KIND (resolved_type_no_typedef) == PROP_CONST)
3498 address = TYPE_DATA_LOCATION_ADDR (resolved_type_no_typedef);
33d502b4 3499 VALUE_LVAL (v) = lval_memory;
1a088441 3500 set_value_address (v, address);
8acb6b92
TT
3501 return v;
3502}
3503
8a9b8146
TT
3504/* Create a value of type TYPE holding the contents CONTENTS.
3505 The new value is `not_lval'. */
3506
3507struct value *
3508value_from_contents (struct type *type, const gdb_byte *contents)
3509{
3510 struct value *result;
3511
3512 result = allocate_value (type);
3513 memcpy (value_contents_raw (result), contents, TYPE_LENGTH (type));
3514 return result;
3515}
3516
3bd0f5ef
MS
3517/* Extract a value from the history file. Input will be of the form
3518 $digits or $$digits. See block comment above 'write_dollar_variable'
3519 for details. */
3520
3521struct value *
e799154c 3522value_from_history_ref (const char *h, const char **endp)
3bd0f5ef
MS
3523{
3524 int index, len;
3525
3526 if (h[0] == '$')
3527 len = 1;
3528 else
3529 return NULL;
3530
3531 if (h[1] == '$')
3532 len = 2;
3533
3534 /* Find length of numeral string. */
3535 for (; isdigit (h[len]); len++)
3536 ;
3537
3538 /* Make sure numeral string is not part of an identifier. */
3539 if (h[len] == '_' || isalpha (h[len]))
3540 return NULL;
3541
3542 /* Now collect the index value. */
3543 if (h[1] == '$')
3544 {
3545 if (len == 2)
3546 {
3547 /* For some bizarre reason, "$$" is equivalent to "$$1",
3548 rather than to "$$0" as it ought to be! */
3549 index = -1;
3550 *endp += len;
3551 }
3552 else
e799154c
TT
3553 {
3554 char *local_end;
3555
3556 index = -strtol (&h[2], &local_end, 10);
3557 *endp = local_end;
3558 }
3bd0f5ef
MS
3559 }
3560 else
3561 {
3562 if (len == 1)
3563 {
3564 /* "$" is equivalent to "$0". */
3565 index = 0;
3566 *endp += len;
3567 }
3568 else
e799154c
TT
3569 {
3570 char *local_end;
3571
3572 index = strtol (&h[1], &local_end, 10);
3573 *endp = local_end;
3574 }
3bd0f5ef
MS
3575 }
3576
3577 return access_value_history (index);
3578}
3579
3fff9862
YQ
3580/* Get the component value (offset by OFFSET bytes) of a struct or
3581 union WHOLE. Component's type is TYPE. */
3582
3583struct value *
3584value_from_component (struct value *whole, struct type *type, LONGEST offset)
3585{
3586 struct value *v;
3587
3588 if (VALUE_LVAL (whole) == lval_memory && value_lazy (whole))
3589 v = allocate_value_lazy (type);
3590 else
3591 {
3592 v = allocate_value (type);
3593 value_contents_copy (v, value_embedded_offset (v),
3594 whole, value_embedded_offset (whole) + offset,
3595 type_length_units (type));
3596 }
3597 v->offset = value_offset (whole) + offset + value_embedded_offset (whole);
3598 set_value_component_location (v, whole);
3fff9862
YQ
3599
3600 return v;
3601}
3602
a471c594
JK
3603struct value *
3604coerce_ref_if_computed (const struct value *arg)
3605{
3606 const struct lval_funcs *funcs;
3607
aa006118 3608 if (!TYPE_IS_REFERENCE (check_typedef (value_type (arg))))
a471c594
JK
3609 return NULL;
3610
3611 if (value_lval_const (arg) != lval_computed)
3612 return NULL;
3613
3614 funcs = value_computed_funcs (arg);
3615 if (funcs->coerce_ref == NULL)
3616 return NULL;
3617
3618 return funcs->coerce_ref (arg);
3619}
3620
dfcee124
AG
3621/* Look at value.h for description. */
3622
3623struct value *
3624readjust_indirect_value_type (struct value *value, struct type *enc_type,
4bf7b526
MG
3625 const struct type *original_type,
3626 const struct value *original_value)
dfcee124
AG
3627{
3628 /* Re-adjust type. */
3629 deprecated_set_value_type (value, TYPE_TARGET_TYPE (original_type));
3630
3631 /* Add embedding info. */
3632 set_value_enclosing_type (value, enc_type);
3633 set_value_embedded_offset (value, value_pointed_to_offset (original_value));
3634
3635 /* We may be pointing to an object of some derived type. */
3636 return value_full_object (value, NULL, 0, 0, 0);
3637}
3638
994b9211
AC
3639struct value *
3640coerce_ref (struct value *arg)
3641{
df407dfe 3642 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
a471c594 3643 struct value *retval;
dfcee124 3644 struct type *enc_type;
a109c7c1 3645
a471c594
JK
3646 retval = coerce_ref_if_computed (arg);
3647 if (retval)
3648 return retval;
3649
aa006118 3650 if (!TYPE_IS_REFERENCE (value_type_arg_tmp))
a471c594
JK
3651 return arg;
3652
dfcee124
AG
3653 enc_type = check_typedef (value_enclosing_type (arg));
3654 enc_type = TYPE_TARGET_TYPE (enc_type);
3655
3656 retval = value_at_lazy (enc_type,
3657 unpack_pointer (value_type (arg),
3658 value_contents (arg)));
9f1f738a 3659 enc_type = value_type (retval);
dfcee124
AG
3660 return readjust_indirect_value_type (retval, enc_type,
3661 value_type_arg_tmp, arg);
994b9211
AC
3662}
3663
3664struct value *
3665coerce_array (struct value *arg)
3666{
f3134b88
TT
3667 struct type *type;
3668
994b9211 3669 arg = coerce_ref (arg);
f3134b88
TT
3670 type = check_typedef (value_type (arg));
3671
3672 switch (TYPE_CODE (type))
3673 {
3674 case TYPE_CODE_ARRAY:
7346b668 3675 if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
f3134b88
TT
3676 arg = value_coerce_array (arg);
3677 break;
3678 case TYPE_CODE_FUNC:
3679 arg = value_coerce_function (arg);
3680 break;
3681 }
994b9211
AC
3682 return arg;
3683}
c906108c 3684\f
c906108c 3685
bbfdfe1c
DM
3686/* Return the return value convention that will be used for the
3687 specified type. */
3688
3689enum return_value_convention
3690struct_return_convention (struct gdbarch *gdbarch,
3691 struct value *function, struct type *value_type)
3692{
3693 enum type_code code = TYPE_CODE (value_type);
3694
3695 if (code == TYPE_CODE_ERROR)
3696 error (_("Function return type unknown."));
3697
3698 /* Probe the architecture for the return-value convention. */
3699 return gdbarch_return_value (gdbarch, function, value_type,
3700 NULL, NULL, NULL);
3701}
3702
48436ce6
AC
3703/* Return true if the function returning the specified type is using
3704 the convention of returning structures in memory (passing in the
82585c72 3705 address as a hidden first parameter). */
c906108c
SS
3706
3707int
d80b854b 3708using_struct_return (struct gdbarch *gdbarch,
6a3a010b 3709 struct value *function, struct type *value_type)
c906108c 3710{
bbfdfe1c 3711 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
667e784f 3712 /* A void return value is never in memory. See also corresponding
44e5158b 3713 code in "print_return_value". */
667e784f
AC
3714 return 0;
3715
bbfdfe1c 3716 return (struct_return_convention (gdbarch, function, value_type)
31db7b6c 3717 != RETURN_VALUE_REGISTER_CONVENTION);
c906108c
SS
3718}
3719
42be36b3
CT
3720/* Set the initialized field in a value struct. */
3721
3722void
3723set_value_initialized (struct value *val, int status)
3724{
3725 val->initialized = status;
3726}
3727
3728/* Return the initialized field in a value struct. */
3729
3730int
4bf7b526 3731value_initialized (const struct value *val)
42be36b3
CT
3732{
3733 return val->initialized;
3734}
3735
a844296a
SM
3736/* Load the actual content of a lazy value. Fetch the data from the
3737 user's process and clear the lazy flag to indicate that the data in
3738 the buffer is valid.
a58e2656
AB
3739
3740 If the value is zero-length, we avoid calling read_memory, which
3741 would abort. We mark the value as fetched anyway -- all 0 bytes of
a844296a 3742 it. */
a58e2656 3743
a844296a 3744void
a58e2656
AB
3745value_fetch_lazy (struct value *val)
3746{
3747 gdb_assert (value_lazy (val));
3748 allocate_value_contents (val);
9a0dc9e3
PA
3749 /* A value is either lazy, or fully fetched. The
3750 availability/validity is only established as we try to fetch a
3751 value. */
3752 gdb_assert (VEC_empty (range_s, val->optimized_out));
3753 gdb_assert (VEC_empty (range_s, val->unavailable));
a58e2656
AB
3754 if (value_bitsize (val))
3755 {
3756 /* To read a lazy bitfield, read the entire enclosing value. This
3757 prevents reading the same block of (possibly volatile) memory once
3758 per bitfield. It would be even better to read only the containing
3759 word, but we have no way to record that just specific bits of a
3760 value have been fetched. */
3761 struct type *type = check_typedef (value_type (val));
a58e2656 3762 struct value *parent = value_parent (val);
a58e2656 3763
b0c54aa5
AB
3764 if (value_lazy (parent))
3765 value_fetch_lazy (parent);
3766
4875ffdb
PA
3767 unpack_value_bitfield (val,
3768 value_bitpos (val), value_bitsize (val),
3769 value_contents_for_printing (parent),
3770 value_offset (val), parent);
a58e2656
AB
3771 }
3772 else if (VALUE_LVAL (val) == lval_memory)
3773 {
3774 CORE_ADDR addr = value_address (val);
3775 struct type *type = check_typedef (value_enclosing_type (val));
3776
3777 if (TYPE_LENGTH (type))
3778 read_value_memory (val, 0, value_stack (val),
3779 addr, value_contents_all_raw (val),
3ae385af 3780 type_length_units (type));
a58e2656
AB
3781 }
3782 else if (VALUE_LVAL (val) == lval_register)
3783 {
41b56feb 3784 struct frame_info *next_frame;
a58e2656
AB
3785 int regnum;
3786 struct type *type = check_typedef (value_type (val));
3787 struct value *new_val = val, *mark = value_mark ();
3788
3789 /* Offsets are not supported here; lazy register values must
3790 refer to the entire register. */
3791 gdb_assert (value_offset (val) == 0);
3792
3793 while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
3794 {
41b56feb 3795 struct frame_id next_frame_id = VALUE_NEXT_FRAME_ID (new_val);
6eeee81c 3796
41b56feb 3797 next_frame = frame_find_by_id (next_frame_id);
a58e2656
AB
3798 regnum = VALUE_REGNUM (new_val);
3799
41b56feb 3800 gdb_assert (next_frame != NULL);
a58e2656
AB
3801
3802 /* Convertible register routines are used for multi-register
3803 values and for interpretation in different types
3804 (e.g. float or int from a double register). Lazy
3805 register values should have the register's natural type,
3806 so they do not apply. */
41b56feb 3807 gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame),
a58e2656
AB
3808 regnum, type));
3809
41b56feb
KB
3810 /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID.
3811 Since a "->next" operation was performed when setting
3812 this field, we do not need to perform a "next" operation
3813 again when unwinding the register. That's why
3814 frame_unwind_register_value() is called here instead of
3815 get_frame_register_value(). */
3816 new_val = frame_unwind_register_value (next_frame, regnum);
6eeee81c
TT
3817
3818 /* If we get another lazy lval_register value, it means the
41b56feb
KB
3819 register is found by reading it from NEXT_FRAME's next frame.
3820 frame_unwind_register_value should never return a value with
3821 the frame id pointing to NEXT_FRAME. If it does, it means we
6eeee81c
TT
3822 either have two consecutive frames with the same frame id
3823 in the frame chain, or some code is trying to unwind
3824 behind get_prev_frame's back (e.g., a frame unwind
3825 sniffer trying to unwind), bypassing its validations. In
3826 any case, it should always be an internal error to end up
3827 in this situation. */
3828 if (VALUE_LVAL (new_val) == lval_register
3829 && value_lazy (new_val)
41b56feb 3830 && frame_id_eq (VALUE_NEXT_FRAME_ID (new_val), next_frame_id))
6eeee81c
TT
3831 internal_error (__FILE__, __LINE__,
3832 _("infinite loop while fetching a register"));
a58e2656
AB
3833 }
3834
3835 /* If it's still lazy (for instance, a saved register on the
3836 stack), fetch it. */
3837 if (value_lazy (new_val))
3838 value_fetch_lazy (new_val);
3839
9a0dc9e3
PA
3840 /* Copy the contents and the unavailability/optimized-out
3841 meta-data from NEW_VAL to VAL. */
3842 set_value_lazy (val, 0);
3843 value_contents_copy (val, value_embedded_offset (val),
3844 new_val, value_embedded_offset (new_val),
3ae385af 3845 type_length_units (type));
a58e2656
AB
3846
3847 if (frame_debug)
3848 {
3849 struct gdbarch *gdbarch;
41b56feb
KB
3850 struct frame_info *frame;
3851 /* VALUE_FRAME_ID is used here, instead of VALUE_NEXT_FRAME_ID,
3852 so that the frame level will be shown correctly. */
a58e2656
AB
3853 frame = frame_find_by_id (VALUE_FRAME_ID (val));
3854 regnum = VALUE_REGNUM (val);
3855 gdbarch = get_frame_arch (frame);
3856
3857 fprintf_unfiltered (gdb_stdlog,
3858 "{ value_fetch_lazy "
3859 "(frame=%d,regnum=%d(%s),...) ",
3860 frame_relative_level (frame), regnum,
3861 user_reg_map_regnum_to_name (gdbarch, regnum));
3862
3863 fprintf_unfiltered (gdb_stdlog, "->");
3864 if (value_optimized_out (new_val))
f6c01fc5
AB
3865 {
3866 fprintf_unfiltered (gdb_stdlog, " ");
3867 val_print_optimized_out (new_val, gdb_stdlog);
3868 }
a58e2656
AB
3869 else
3870 {
3871 int i;
3872 const gdb_byte *buf = value_contents (new_val);
3873
3874 if (VALUE_LVAL (new_val) == lval_register)
3875 fprintf_unfiltered (gdb_stdlog, " register=%d",
3876 VALUE_REGNUM (new_val));
3877 else if (VALUE_LVAL (new_val) == lval_memory)
3878 fprintf_unfiltered (gdb_stdlog, " address=%s",
3879 paddress (gdbarch,
3880 value_address (new_val)));
3881 else
3882 fprintf_unfiltered (gdb_stdlog, " computed");
3883
3884 fprintf_unfiltered (gdb_stdlog, " bytes=");
3885 fprintf_unfiltered (gdb_stdlog, "[");
3886 for (i = 0; i < register_size (gdbarch, regnum); i++)
3887 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3888 fprintf_unfiltered (gdb_stdlog, "]");
3889 }
3890
3891 fprintf_unfiltered (gdb_stdlog, " }\n");
3892 }
3893
3894 /* Dispose of the intermediate values. This prevents
3895 watchpoints from trying to watch the saved frame pointer. */
3896 value_free_to_mark (mark);
3897 }
3898 else if (VALUE_LVAL (val) == lval_computed
3899 && value_computed_funcs (val)->read != NULL)
3900 value_computed_funcs (val)->read (val);
a58e2656
AB
3901 else
3902 internal_error (__FILE__, __LINE__, _("Unexpected lazy value type."));
3903
3904 set_value_lazy (val, 0);
a58e2656
AB
3905}
3906
a280dbd1
SDJ
3907/* Implementation of the convenience function $_isvoid. */
3908
3909static struct value *
3910isvoid_internal_fn (struct gdbarch *gdbarch,
3911 const struct language_defn *language,
3912 void *cookie, int argc, struct value **argv)
3913{
3914 int ret;
3915
3916 if (argc != 1)
6bc305f5 3917 error (_("You must provide one argument for $_isvoid."));
a280dbd1
SDJ
3918
3919 ret = TYPE_CODE (value_type (argv[0])) == TYPE_CODE_VOID;
3920
3921 return value_from_longest (builtin_type (gdbarch)->builtin_int, ret);
3922}
3923
c906108c 3924void
fba45db2 3925_initialize_values (void)
c906108c 3926{
1a966eab 3927 add_cmd ("convenience", no_class, show_convenience, _("\
f47f77df
DE
3928Debugger convenience (\"$foo\") variables and functions.\n\
3929Convenience variables are created when you assign them values;\n\
3930thus, \"set $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
1a966eab 3931\n\
c906108c
SS
3932A few convenience variables are given values automatically:\n\
3933\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
f47f77df
DE
3934\"$__\" holds the contents of the last address examined with \"x\"."
3935#ifdef HAVE_PYTHON
3936"\n\n\
3937Convenience functions are defined via the Python API."
3938#endif
3939 ), &showlist);
7e20dfcd 3940 add_alias_cmd ("conv", "convenience", no_class, 1, &showlist);
c906108c 3941
db5f229b 3942 add_cmd ("values", no_set_class, show_values, _("\
3e43a32a 3943Elements of value history around item number IDX (or last ten)."),
c906108c 3944 &showlist);
53e5f3cf
AS
3945
3946 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
3947Initialize a convenience variable if necessary.\n\
3948init-if-undefined VARIABLE = EXPRESSION\n\
3949Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
3950exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
3951VARIABLE is already initialized."));
bc3b79fd
TJB
3952
3953 add_prefix_cmd ("function", no_class, function_command, _("\
3954Placeholder command for showing help on convenience functions."),
3955 &functionlist, "function ", 0, &cmdlist);
a280dbd1
SDJ
3956
3957 add_internal_function ("_isvoid", _("\
3958Check whether an expression is void.\n\
3959Usage: $_isvoid (expression)\n\
3960Return 1 if the expression is void, zero otherwise."),
3961 isvoid_internal_fn, NULL);
5fdf6324
AB
3962
3963 add_setshow_zuinteger_unlimited_cmd ("max-value-size",
3964 class_support, &max_value_size, _("\
3965Set maximum sized value gdb will load from the inferior."), _("\
3966Show maximum sized value gdb will load from the inferior."), _("\
3967Use this to control the maximum size, in bytes, of a value that gdb\n\
3968will load from the inferior. Setting this value to 'unlimited'\n\
3969disables checking.\n\
3970Setting this does not invalidate already allocated values, it only\n\
3971prevents future values, larger than this size, from being allocated."),
3972 set_max_value_size,
3973 show_max_value_size,
3974 &setlist, &showlist);
c906108c 3975}