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