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