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