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