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