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