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