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