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