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