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