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