*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / value.c
1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
2
3 Copyright (C) 1986-2000, 2002-2012 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 "gdb_string.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29 #include "target.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "doublest.h"
33 #include "gdb_assert.h"
34 #include "regcache.h"
35 #include "block.h"
36 #include "dfp.h"
37 #include "objfiles.h"
38 #include "valprint.h"
39 #include "cli/cli-decode.h"
40 #include "exceptions.h"
41 #include "python/python.h"
42 #include <ctype.h>
43 #include "tracepoint.h"
44
45 /* Prototypes for exported functions. */
46
47 void _initialize_values (void);
48
49 /* Definition of a user function. */
50 struct internal_function
51 {
52 /* The name of the function. It is a bit odd to have this in the
53 function itself -- the user might use a differently-named
54 convenience variable to hold the function. */
55 char *name;
56
57 /* The handler. */
58 internal_function_fn handler;
59
60 /* User data for the handler. */
61 void *cookie;
62 };
63
64 /* Defines an [OFFSET, OFFSET + LENGTH) range. */
65
66 struct range
67 {
68 /* Lowest offset in the range. */
69 int offset;
70
71 /* Length of the range. */
72 int length;
73 };
74
75 typedef struct range range_s;
76
77 DEF_VEC_O(range_s);
78
79 /* Returns true if the ranges defined by [offset1, offset1+len1) and
80 [offset2, offset2+len2) overlap. */
81
82 static int
83 ranges_overlap (int offset1, int len1,
84 int offset2, int len2)
85 {
86 ULONGEST h, l;
87
88 l = max (offset1, offset2);
89 h = min (offset1 + len1, offset2 + len2);
90 return (l < h);
91 }
92
93 /* Returns true if the first argument is strictly less than the
94 second, useful for VEC_lower_bound. We keep ranges sorted by
95 offset and coalesce overlapping and contiguous ranges, so this just
96 compares the starting offset. */
97
98 static int
99 range_lessthan (const range_s *r1, const range_s *r2)
100 {
101 return r1->offset < r2->offset;
102 }
103
104 /* Returns true if RANGES contains any range that overlaps [OFFSET,
105 OFFSET+LENGTH). */
106
107 static int
108 ranges_contain (VEC(range_s) *ranges, int offset, int length)
109 {
110 range_s what;
111 int i;
112
113 what.offset = offset;
114 what.length = length;
115
116 /* We keep ranges sorted by offset and coalesce overlapping and
117 contiguous ranges, so to check if a range list contains a given
118 range, we can do a binary search for the position the given range
119 would be inserted if we only considered the starting OFFSET of
120 ranges. We call that position I. Since we also have LENGTH to
121 care for (this is a range afterall), we need to check if the
122 _previous_ range overlaps the I range. E.g.,
123
124 R
125 |---|
126 |---| |---| |------| ... |--|
127 0 1 2 N
128
129 I=1
130
131 In the case above, the binary search would return `I=1', meaning,
132 this OFFSET should be inserted at position 1, and the current
133 position 1 should be pushed further (and before 2). But, `0'
134 overlaps with R.
135
136 Then we need to check if the I range overlaps the I range itself.
137 E.g.,
138
139 R
140 |---|
141 |---| |---| |-------| ... |--|
142 0 1 2 N
143
144 I=1
145 */
146
147 i = VEC_lower_bound (range_s, ranges, &what, range_lessthan);
148
149 if (i > 0)
150 {
151 struct range *bef = VEC_index (range_s, ranges, i - 1);
152
153 if (ranges_overlap (bef->offset, bef->length, offset, length))
154 return 1;
155 }
156
157 if (i < VEC_length (range_s, ranges))
158 {
159 struct range *r = VEC_index (range_s, ranges, i);
160
161 if (ranges_overlap (r->offset, r->length, offset, length))
162 return 1;
163 }
164
165 return 0;
166 }
167
168 static struct cmd_list_element *functionlist;
169
170 /* Note that the fields in this structure are arranged to save a bit
171 of memory. */
172
173 struct value
174 {
175 /* Type of value; either not an lval, or one of the various
176 different possible kinds of lval. */
177 enum lval_type lval;
178
179 /* Is it modifiable? Only relevant if lval != not_lval. */
180 unsigned int modifiable : 1;
181
182 /* If zero, contents of this value are in the contents field. If
183 nonzero, contents are in inferior. If the lval field is lval_memory,
184 the contents are in inferior memory at location.address plus offset.
185 The lval field may also be lval_register.
186
187 WARNING: This field is used by the code which handles watchpoints
188 (see breakpoint.c) to decide whether a particular value can be
189 watched by hardware watchpoints. If the lazy flag is set for
190 some member of a value chain, it is assumed that this member of
191 the chain doesn't need to be watched as part of watching the
192 value itself. This is how GDB avoids watching the entire struct
193 or array when the user wants to watch a single struct member or
194 array element. If you ever change the way lazy flag is set and
195 reset, be sure to consider this use as well! */
196 unsigned int lazy : 1;
197
198 /* If nonzero, this is the value of a variable which does not
199 actually exist in the program. */
200 unsigned int optimized_out : 1;
201
202 /* If value is a variable, is it initialized or not. */
203 unsigned int initialized : 1;
204
205 /* If value is from the stack. If this is set, read_stack will be
206 used instead of read_memory to enable extra caching. */
207 unsigned int stack : 1;
208
209 /* If the value has been released. */
210 unsigned int released : 1;
211
212 /* Location of value (if lval). */
213 union
214 {
215 /* If lval == lval_memory, this is the address in the inferior.
216 If lval == lval_register, this is the byte offset into the
217 registers structure. */
218 CORE_ADDR address;
219
220 /* Pointer to internal variable. */
221 struct internalvar *internalvar;
222
223 /* If lval == lval_computed, this is a set of function pointers
224 to use to access and describe the value, and a closure pointer
225 for them to use. */
226 struct
227 {
228 /* Functions to call. */
229 const struct lval_funcs *funcs;
230
231 /* Closure for those functions to use. */
232 void *closure;
233 } computed;
234 } location;
235
236 /* Describes offset of a value within lval of a structure in bytes.
237 If lval == lval_memory, this is an offset to the address. If
238 lval == lval_register, this is a further offset from
239 location.address within the registers structure. Note also the
240 member embedded_offset below. */
241 int offset;
242
243 /* Only used for bitfields; number of bits contained in them. */
244 int bitsize;
245
246 /* Only used for bitfields; position of start of field. For
247 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
248 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
249 int bitpos;
250
251 /* The number of references to this value. When a value is created,
252 the value chain holds a reference, so REFERENCE_COUNT is 1. If
253 release_value is called, this value is removed from the chain but
254 the caller of release_value now has a reference to this value.
255 The caller must arrange for a call to value_free later. */
256 int reference_count;
257
258 /* Only used for bitfields; the containing value. This allows a
259 single read from the target when displaying multiple
260 bitfields. */
261 struct value *parent;
262
263 /* Frame register value is relative to. This will be described in
264 the lval enum above as "lval_register". */
265 struct frame_id frame_id;
266
267 /* Type of the value. */
268 struct type *type;
269
270 /* If a value represents a C++ object, then the `type' field gives
271 the object's compile-time type. If the object actually belongs
272 to some class derived from `type', perhaps with other base
273 classes and additional members, then `type' is just a subobject
274 of the real thing, and the full object is probably larger than
275 `type' would suggest.
276
277 If `type' is a dynamic class (i.e. one with a vtable), then GDB
278 can actually determine the object's run-time type by looking at
279 the run-time type information in the vtable. When this
280 information is available, we may elect to read in the entire
281 object, for several reasons:
282
283 - When printing the value, the user would probably rather see the
284 full object, not just the limited portion apparent from the
285 compile-time type.
286
287 - If `type' has virtual base classes, then even printing `type'
288 alone may require reaching outside the `type' portion of the
289 object to wherever the virtual base class has been stored.
290
291 When we store the entire object, `enclosing_type' is the run-time
292 type -- the complete object -- and `embedded_offset' is the
293 offset of `type' within that larger type, in bytes. The
294 value_contents() macro takes `embedded_offset' into account, so
295 most GDB code continues to see the `type' portion of the value,
296 just as the inferior would.
297
298 If `type' is a pointer to an object, then `enclosing_type' is a
299 pointer to the object's run-time type, and `pointed_to_offset' is
300 the offset in bytes from the full object to the pointed-to object
301 -- that is, the value `embedded_offset' would have if we followed
302 the pointer and fetched the complete object. (I don't really see
303 the point. Why not just determine the run-time type when you
304 indirect, and avoid the special case? The contents don't matter
305 until you indirect anyway.)
306
307 If we're not doing anything fancy, `enclosing_type' is equal to
308 `type', and `embedded_offset' is zero, so everything works
309 normally. */
310 struct type *enclosing_type;
311 int embedded_offset;
312 int pointed_to_offset;
313
314 /* Values are stored in a chain, so that they can be deleted easily
315 over calls to the inferior. Values assigned to internal
316 variables, put into the value history or exposed to Python are
317 taken off this list. */
318 struct value *next;
319
320 /* Register number if the value is from a register. */
321 short regnum;
322
323 /* Actual contents of the value. Target byte-order. NULL or not
324 valid if lazy is nonzero. */
325 gdb_byte *contents;
326
327 /* Unavailable ranges in CONTENTS. We mark unavailable ranges,
328 rather than available, since the common and default case is for a
329 value to be available. This is filled in at value read time. */
330 VEC(range_s) *unavailable;
331 };
332
333 int
334 value_bytes_available (const struct value *value, int offset, int length)
335 {
336 gdb_assert (!value->lazy);
337
338 return !ranges_contain (value->unavailable, offset, length);
339 }
340
341 int
342 value_entirely_available (struct value *value)
343 {
344 /* We can only tell whether the whole value is available when we try
345 to read it. */
346 if (value->lazy)
347 value_fetch_lazy (value);
348
349 if (VEC_empty (range_s, value->unavailable))
350 return 1;
351 return 0;
352 }
353
354 void
355 mark_value_bytes_unavailable (struct value *value, int offset, int length)
356 {
357 range_s newr;
358 int i;
359
360 /* Insert the range sorted. If there's overlap or the new range
361 would be contiguous with an existing range, merge. */
362
363 newr.offset = offset;
364 newr.length = length;
365
366 /* Do a binary search for the position the given range would be
367 inserted if we only considered the starting OFFSET of ranges.
368 Call that position I. Since we also have LENGTH to care for
369 (this is a range afterall), we need to check if the _previous_
370 range overlaps the I range. E.g., calling R the new range:
371
372 #1 - overlaps with previous
373
374 R
375 |-...-|
376 |---| |---| |------| ... |--|
377 0 1 2 N
378
379 I=1
380
381 In the case #1 above, the binary search would return `I=1',
382 meaning, this OFFSET should be inserted at position 1, and the
383 current position 1 should be pushed further (and become 2). But,
384 note that `0' overlaps with R, so we want to merge them.
385
386 A similar consideration needs to be taken if the new range would
387 be contiguous with the previous range:
388
389 #2 - contiguous with previous
390
391 R
392 |-...-|
393 |--| |---| |------| ... |--|
394 0 1 2 N
395
396 I=1
397
398 If there's no overlap with the previous range, as in:
399
400 #3 - not overlapping and not contiguous
401
402 R
403 |-...-|
404 |--| |---| |------| ... |--|
405 0 1 2 N
406
407 I=1
408
409 or if I is 0:
410
411 #4 - R is the range with lowest offset
412
413 R
414 |-...-|
415 |--| |---| |------| ... |--|
416 0 1 2 N
417
418 I=0
419
420 ... we just push the new range to I.
421
422 All the 4 cases above need to consider that the new range may
423 also overlap several of the ranges that follow, or that R may be
424 contiguous with the following range, and merge. E.g.,
425
426 #5 - overlapping following ranges
427
428 R
429 |------------------------|
430 |--| |---| |------| ... |--|
431 0 1 2 N
432
433 I=0
434
435 or:
436
437 R
438 |-------|
439 |--| |---| |------| ... |--|
440 0 1 2 N
441
442 I=1
443
444 */
445
446 i = VEC_lower_bound (range_s, value->unavailable, &newr, range_lessthan);
447 if (i > 0)
448 {
449 struct range *bef = VEC_index (range_s, value->unavailable, i - 1);
450
451 if (ranges_overlap (bef->offset, bef->length, offset, length))
452 {
453 /* #1 */
454 ULONGEST l = min (bef->offset, offset);
455 ULONGEST h = max (bef->offset + bef->length, offset + length);
456
457 bef->offset = l;
458 bef->length = h - l;
459 i--;
460 }
461 else if (offset == bef->offset + bef->length)
462 {
463 /* #2 */
464 bef->length += length;
465 i--;
466 }
467 else
468 {
469 /* #3 */
470 VEC_safe_insert (range_s, value->unavailable, i, &newr);
471 }
472 }
473 else
474 {
475 /* #4 */
476 VEC_safe_insert (range_s, value->unavailable, i, &newr);
477 }
478
479 /* Check whether the ranges following the one we've just added or
480 touched can be folded in (#5 above). */
481 if (i + 1 < VEC_length (range_s, value->unavailable))
482 {
483 struct range *t;
484 struct range *r;
485 int removed = 0;
486 int next = i + 1;
487
488 /* Get the range we just touched. */
489 t = VEC_index (range_s, value->unavailable, i);
490 removed = 0;
491
492 i = next;
493 for (; VEC_iterate (range_s, value->unavailable, i, r); i++)
494 if (r->offset <= t->offset + t->length)
495 {
496 ULONGEST l, h;
497
498 l = min (t->offset, r->offset);
499 h = max (t->offset + t->length, r->offset + r->length);
500
501 t->offset = l;
502 t->length = h - l;
503
504 removed++;
505 }
506 else
507 {
508 /* If we couldn't merge this one, we won't be able to
509 merge following ones either, since the ranges are
510 always sorted by OFFSET. */
511 break;
512 }
513
514 if (removed != 0)
515 VEC_block_remove (range_s, value->unavailable, next, removed);
516 }
517 }
518
519 /* Find the first range in RANGES that overlaps the range defined by
520 OFFSET and LENGTH, starting at element POS in the RANGES vector,
521 Returns the index into RANGES where such overlapping range was
522 found, or -1 if none was found. */
523
524 static int
525 find_first_range_overlap (VEC(range_s) *ranges, int pos,
526 int offset, int length)
527 {
528 range_s *r;
529 int i;
530
531 for (i = pos; VEC_iterate (range_s, ranges, i, r); i++)
532 if (ranges_overlap (r->offset, r->length, offset, length))
533 return i;
534
535 return -1;
536 }
537
538 int
539 value_available_contents_eq (const struct value *val1, int offset1,
540 const struct value *val2, int offset2,
541 int length)
542 {
543 int idx1 = 0, idx2 = 0;
544
545 /* This routine is used by printing routines, where we should
546 already have read the value. Note that we only know whether a
547 value chunk is available if we've tried to read it. */
548 gdb_assert (!val1->lazy && !val2->lazy);
549
550 while (length > 0)
551 {
552 range_s *r1, *r2;
553 ULONGEST l1, h1;
554 ULONGEST l2, h2;
555
556 idx1 = find_first_range_overlap (val1->unavailable, idx1,
557 offset1, length);
558 idx2 = find_first_range_overlap (val2->unavailable, idx2,
559 offset2, length);
560
561 /* The usual case is for both values to be completely available. */
562 if (idx1 == -1 && idx2 == -1)
563 return (memcmp (val1->contents + offset1,
564 val2->contents + offset2,
565 length) == 0);
566 /* The contents only match equal if the available set matches as
567 well. */
568 else if (idx1 == -1 || idx2 == -1)
569 return 0;
570
571 gdb_assert (idx1 != -1 && idx2 != -1);
572
573 r1 = VEC_index (range_s, val1->unavailable, idx1);
574 r2 = VEC_index (range_s, val2->unavailable, idx2);
575
576 /* Get the unavailable windows intersected by the incoming
577 ranges. The first and last ranges that overlap the argument
578 range may be wider than said incoming arguments ranges. */
579 l1 = max (offset1, r1->offset);
580 h1 = min (offset1 + length, r1->offset + r1->length);
581
582 l2 = max (offset2, r2->offset);
583 h2 = min (offset2 + length, r2->offset + r2->length);
584
585 /* Make them relative to the respective start offsets, so we can
586 compare them for equality. */
587 l1 -= offset1;
588 h1 -= offset1;
589
590 l2 -= offset2;
591 h2 -= offset2;
592
593 /* Different availability, no match. */
594 if (l1 != l2 || h1 != h2)
595 return 0;
596
597 /* Compare the _available_ contents. */
598 if (memcmp (val1->contents + offset1,
599 val2->contents + offset2,
600 l1) != 0)
601 return 0;
602
603 length -= h1;
604 offset1 += h1;
605 offset2 += h1;
606 }
607
608 return 1;
609 }
610
611 /* Prototypes for local functions. */
612
613 static void show_values (char *, int);
614
615 static void show_convenience (char *, int);
616
617
618 /* The value-history records all the values printed
619 by print commands during this session. Each chunk
620 records 60 consecutive values. The first chunk on
621 the chain records the most recent values.
622 The total number of values is in value_history_count. */
623
624 #define VALUE_HISTORY_CHUNK 60
625
626 struct value_history_chunk
627 {
628 struct value_history_chunk *next;
629 struct value *values[VALUE_HISTORY_CHUNK];
630 };
631
632 /* Chain of chunks now in use. */
633
634 static struct value_history_chunk *value_history_chain;
635
636 static int value_history_count; /* Abs number of last entry stored. */
637
638 \f
639 /* List of all value objects currently allocated
640 (except for those released by calls to release_value)
641 This is so they can be freed after each command. */
642
643 static struct value *all_values;
644
645 /* Allocate a lazy value for type TYPE. Its actual content is
646 "lazily" allocated too: the content field of the return value is
647 NULL; it will be allocated when it is fetched from the target. */
648
649 struct value *
650 allocate_value_lazy (struct type *type)
651 {
652 struct value *val;
653
654 /* Call check_typedef on our type to make sure that, if TYPE
655 is a TYPE_CODE_TYPEDEF, its length is set to the length
656 of the target type instead of zero. However, we do not
657 replace the typedef type by the target type, because we want
658 to keep the typedef in order to be able to set the VAL's type
659 description correctly. */
660 check_typedef (type);
661
662 val = (struct value *) xzalloc (sizeof (struct value));
663 val->contents = NULL;
664 val->next = all_values;
665 all_values = val;
666 val->type = type;
667 val->enclosing_type = type;
668 VALUE_LVAL (val) = not_lval;
669 val->location.address = 0;
670 VALUE_FRAME_ID (val) = null_frame_id;
671 val->offset = 0;
672 val->bitpos = 0;
673 val->bitsize = 0;
674 VALUE_REGNUM (val) = -1;
675 val->lazy = 1;
676 val->optimized_out = 0;
677 val->embedded_offset = 0;
678 val->pointed_to_offset = 0;
679 val->modifiable = 1;
680 val->initialized = 1; /* Default to initialized. */
681
682 /* Values start out on the all_values chain. */
683 val->reference_count = 1;
684
685 return val;
686 }
687
688 /* Allocate the contents of VAL if it has not been allocated yet. */
689
690 void
691 allocate_value_contents (struct value *val)
692 {
693 if (!val->contents)
694 val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
695 }
696
697 /* Allocate a value and its contents for type TYPE. */
698
699 struct value *
700 allocate_value (struct type *type)
701 {
702 struct value *val = allocate_value_lazy (type);
703
704 allocate_value_contents (val);
705 val->lazy = 0;
706 return val;
707 }
708
709 /* Allocate a value that has the correct length
710 for COUNT repetitions of type TYPE. */
711
712 struct value *
713 allocate_repeat_value (struct type *type, int count)
714 {
715 int low_bound = current_language->string_lower_bound; /* ??? */
716 /* FIXME-type-allocation: need a way to free this type when we are
717 done with it. */
718 struct type *array_type
719 = lookup_array_range_type (type, low_bound, count + low_bound - 1);
720
721 return allocate_value (array_type);
722 }
723
724 struct value *
725 allocate_computed_value (struct type *type,
726 const struct lval_funcs *funcs,
727 void *closure)
728 {
729 struct value *v = allocate_value_lazy (type);
730
731 VALUE_LVAL (v) = lval_computed;
732 v->location.computed.funcs = funcs;
733 v->location.computed.closure = closure;
734
735 return v;
736 }
737
738 /* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */
739
740 struct value *
741 allocate_optimized_out_value (struct type *type)
742 {
743 struct value *retval = allocate_value_lazy (type);
744
745 set_value_optimized_out (retval, 1);
746
747 return retval;
748 }
749
750 /* Accessor methods. */
751
752 struct value *
753 value_next (struct value *value)
754 {
755 return value->next;
756 }
757
758 struct type *
759 value_type (const struct value *value)
760 {
761 return value->type;
762 }
763 void
764 deprecated_set_value_type (struct value *value, struct type *type)
765 {
766 value->type = type;
767 }
768
769 int
770 value_offset (const struct value *value)
771 {
772 return value->offset;
773 }
774 void
775 set_value_offset (struct value *value, int offset)
776 {
777 value->offset = offset;
778 }
779
780 int
781 value_bitpos (const struct value *value)
782 {
783 return value->bitpos;
784 }
785 void
786 set_value_bitpos (struct value *value, int bit)
787 {
788 value->bitpos = bit;
789 }
790
791 int
792 value_bitsize (const struct value *value)
793 {
794 return value->bitsize;
795 }
796 void
797 set_value_bitsize (struct value *value, int bit)
798 {
799 value->bitsize = bit;
800 }
801
802 struct value *
803 value_parent (struct value *value)
804 {
805 return value->parent;
806 }
807
808 gdb_byte *
809 value_contents_raw (struct value *value)
810 {
811 allocate_value_contents (value);
812 return value->contents + value->embedded_offset;
813 }
814
815 gdb_byte *
816 value_contents_all_raw (struct value *value)
817 {
818 allocate_value_contents (value);
819 return value->contents;
820 }
821
822 struct type *
823 value_enclosing_type (struct value *value)
824 {
825 return value->enclosing_type;
826 }
827
828 static void
829 require_not_optimized_out (const struct value *value)
830 {
831 if (value->optimized_out)
832 error (_("value has been optimized out"));
833 }
834
835 static void
836 require_available (const struct value *value)
837 {
838 if (!VEC_empty (range_s, value->unavailable))
839 throw_error (NOT_AVAILABLE_ERROR, _("value is not available"));
840 }
841
842 const gdb_byte *
843 value_contents_for_printing (struct value *value)
844 {
845 if (value->lazy)
846 value_fetch_lazy (value);
847 return value->contents;
848 }
849
850 const gdb_byte *
851 value_contents_for_printing_const (const struct value *value)
852 {
853 gdb_assert (!value->lazy);
854 return value->contents;
855 }
856
857 const gdb_byte *
858 value_contents_all (struct value *value)
859 {
860 const gdb_byte *result = value_contents_for_printing (value);
861 require_not_optimized_out (value);
862 require_available (value);
863 return result;
864 }
865
866 /* Copy LENGTH bytes of SRC value's (all) contents
867 (value_contents_all) starting at SRC_OFFSET, into DST value's (all)
868 contents, starting at DST_OFFSET. If unavailable contents are
869 being copied from SRC, the corresponding DST contents are marked
870 unavailable accordingly. Neither DST nor SRC may be lazy
871 values.
872
873 It is assumed the contents of DST in the [DST_OFFSET,
874 DST_OFFSET+LENGTH) range are wholly available. */
875
876 void
877 value_contents_copy_raw (struct value *dst, int dst_offset,
878 struct value *src, int src_offset, int length)
879 {
880 range_s *r;
881 int i;
882
883 /* A lazy DST would make that this copy operation useless, since as
884 soon as DST's contents were un-lazied (by a later value_contents
885 call, say), the contents would be overwritten. A lazy SRC would
886 mean we'd be copying garbage. */
887 gdb_assert (!dst->lazy && !src->lazy);
888
889 /* The overwritten DST range gets unavailability ORed in, not
890 replaced. Make sure to remember to implement replacing if it
891 turns out actually necessary. */
892 gdb_assert (value_bytes_available (dst, dst_offset, length));
893
894 /* Copy the data. */
895 memcpy (value_contents_all_raw (dst) + dst_offset,
896 value_contents_all_raw (src) + src_offset,
897 length);
898
899 /* Copy the meta-data, adjusted. */
900 for (i = 0; VEC_iterate (range_s, src->unavailable, i, r); i++)
901 {
902 ULONGEST h, l;
903
904 l = max (r->offset, src_offset);
905 h = min (r->offset + r->length, src_offset + length);
906
907 if (l < h)
908 mark_value_bytes_unavailable (dst,
909 dst_offset + (l - src_offset),
910 h - l);
911 }
912 }
913
914 /* Copy LENGTH bytes of SRC value's (all) contents
915 (value_contents_all) starting at SRC_OFFSET byte, into DST value's
916 (all) contents, starting at DST_OFFSET. If unavailable contents
917 are being copied from SRC, the corresponding DST contents are
918 marked unavailable accordingly. DST must not be lazy. If SRC is
919 lazy, it will be fetched now. If SRC is not valid (is optimized
920 out), an error is thrown.
921
922 It is assumed the contents of DST in the [DST_OFFSET,
923 DST_OFFSET+LENGTH) range are wholly available. */
924
925 void
926 value_contents_copy (struct value *dst, int dst_offset,
927 struct value *src, int src_offset, int length)
928 {
929 require_not_optimized_out (src);
930
931 if (src->lazy)
932 value_fetch_lazy (src);
933
934 value_contents_copy_raw (dst, dst_offset, src, src_offset, length);
935 }
936
937 int
938 value_lazy (struct value *value)
939 {
940 return value->lazy;
941 }
942
943 void
944 set_value_lazy (struct value *value, int val)
945 {
946 value->lazy = val;
947 }
948
949 int
950 value_stack (struct value *value)
951 {
952 return value->stack;
953 }
954
955 void
956 set_value_stack (struct value *value, int val)
957 {
958 value->stack = val;
959 }
960
961 const gdb_byte *
962 value_contents (struct value *value)
963 {
964 const gdb_byte *result = value_contents_writeable (value);
965 require_not_optimized_out (value);
966 require_available (value);
967 return result;
968 }
969
970 gdb_byte *
971 value_contents_writeable (struct value *value)
972 {
973 if (value->lazy)
974 value_fetch_lazy (value);
975 return value_contents_raw (value);
976 }
977
978 /* Return non-zero if VAL1 and VAL2 have the same contents. Note that
979 this function is different from value_equal; in C the operator ==
980 can return 0 even if the two values being compared are equal. */
981
982 int
983 value_contents_equal (struct value *val1, struct value *val2)
984 {
985 struct type *type1;
986 struct type *type2;
987 int len;
988
989 type1 = check_typedef (value_type (val1));
990 type2 = check_typedef (value_type (val2));
991 len = TYPE_LENGTH (type1);
992 if (len != TYPE_LENGTH (type2))
993 return 0;
994
995 return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
996 }
997
998 int
999 value_optimized_out (struct value *value)
1000 {
1001 return value->optimized_out;
1002 }
1003
1004 void
1005 set_value_optimized_out (struct value *value, int val)
1006 {
1007 value->optimized_out = val;
1008 }
1009
1010 int
1011 value_entirely_optimized_out (const struct value *value)
1012 {
1013 if (!value->optimized_out)
1014 return 0;
1015 if (value->lval != lval_computed
1016 || !value->location.computed.funcs->check_any_valid)
1017 return 1;
1018 return !value->location.computed.funcs->check_any_valid (value);
1019 }
1020
1021 int
1022 value_bits_valid (const struct value *value, int offset, int length)
1023 {
1024 if (!value->optimized_out)
1025 return 1;
1026 if (value->lval != lval_computed
1027 || !value->location.computed.funcs->check_validity)
1028 return 0;
1029 return value->location.computed.funcs->check_validity (value, offset,
1030 length);
1031 }
1032
1033 int
1034 value_bits_synthetic_pointer (const struct value *value,
1035 int offset, int length)
1036 {
1037 if (value->lval != lval_computed
1038 || !value->location.computed.funcs->check_synthetic_pointer)
1039 return 0;
1040 return value->location.computed.funcs->check_synthetic_pointer (value,
1041 offset,
1042 length);
1043 }
1044
1045 int
1046 value_embedded_offset (struct value *value)
1047 {
1048 return value->embedded_offset;
1049 }
1050
1051 void
1052 set_value_embedded_offset (struct value *value, int val)
1053 {
1054 value->embedded_offset = val;
1055 }
1056
1057 int
1058 value_pointed_to_offset (struct value *value)
1059 {
1060 return value->pointed_to_offset;
1061 }
1062
1063 void
1064 set_value_pointed_to_offset (struct value *value, int val)
1065 {
1066 value->pointed_to_offset = val;
1067 }
1068
1069 const struct lval_funcs *
1070 value_computed_funcs (const struct value *v)
1071 {
1072 gdb_assert (value_lval_const (v) == lval_computed);
1073
1074 return v->location.computed.funcs;
1075 }
1076
1077 void *
1078 value_computed_closure (const struct value *v)
1079 {
1080 gdb_assert (v->lval == lval_computed);
1081
1082 return v->location.computed.closure;
1083 }
1084
1085 enum lval_type *
1086 deprecated_value_lval_hack (struct value *value)
1087 {
1088 return &value->lval;
1089 }
1090
1091 enum lval_type
1092 value_lval_const (const struct value *value)
1093 {
1094 return value->lval;
1095 }
1096
1097 CORE_ADDR
1098 value_address (const struct value *value)
1099 {
1100 if (value->lval == lval_internalvar
1101 || value->lval == lval_internalvar_component)
1102 return 0;
1103 return value->location.address + value->offset;
1104 }
1105
1106 CORE_ADDR
1107 value_raw_address (struct value *value)
1108 {
1109 if (value->lval == lval_internalvar
1110 || value->lval == lval_internalvar_component)
1111 return 0;
1112 return value->location.address;
1113 }
1114
1115 void
1116 set_value_address (struct value *value, CORE_ADDR addr)
1117 {
1118 gdb_assert (value->lval != lval_internalvar
1119 && value->lval != lval_internalvar_component);
1120 value->location.address = addr;
1121 }
1122
1123 struct internalvar **
1124 deprecated_value_internalvar_hack (struct value *value)
1125 {
1126 return &value->location.internalvar;
1127 }
1128
1129 struct frame_id *
1130 deprecated_value_frame_id_hack (struct value *value)
1131 {
1132 return &value->frame_id;
1133 }
1134
1135 short *
1136 deprecated_value_regnum_hack (struct value *value)
1137 {
1138 return &value->regnum;
1139 }
1140
1141 int
1142 deprecated_value_modifiable (struct value *value)
1143 {
1144 return value->modifiable;
1145 }
1146 void
1147 deprecated_set_value_modifiable (struct value *value, int modifiable)
1148 {
1149 value->modifiable = modifiable;
1150 }
1151 \f
1152 /* Return a mark in the value chain. All values allocated after the
1153 mark is obtained (except for those released) are subject to being freed
1154 if a subsequent value_free_to_mark is passed the mark. */
1155 struct value *
1156 value_mark (void)
1157 {
1158 return all_values;
1159 }
1160
1161 /* Take a reference to VAL. VAL will not be deallocated until all
1162 references are released. */
1163
1164 void
1165 value_incref (struct value *val)
1166 {
1167 val->reference_count++;
1168 }
1169
1170 /* Release a reference to VAL, which was acquired with value_incref.
1171 This function is also called to deallocate values from the value
1172 chain. */
1173
1174 void
1175 value_free (struct value *val)
1176 {
1177 if (val)
1178 {
1179 gdb_assert (val->reference_count > 0);
1180 val->reference_count--;
1181 if (val->reference_count > 0)
1182 return;
1183
1184 /* If there's an associated parent value, drop our reference to
1185 it. */
1186 if (val->parent != NULL)
1187 value_free (val->parent);
1188
1189 if (VALUE_LVAL (val) == lval_computed)
1190 {
1191 const struct lval_funcs *funcs = val->location.computed.funcs;
1192
1193 if (funcs->free_closure)
1194 funcs->free_closure (val);
1195 }
1196
1197 xfree (val->contents);
1198 VEC_free (range_s, val->unavailable);
1199 }
1200 xfree (val);
1201 }
1202
1203 /* Free all values allocated since MARK was obtained by value_mark
1204 (except for those released). */
1205 void
1206 value_free_to_mark (struct value *mark)
1207 {
1208 struct value *val;
1209 struct value *next;
1210
1211 for (val = all_values; val && val != mark; val = next)
1212 {
1213 next = val->next;
1214 val->released = 1;
1215 value_free (val);
1216 }
1217 all_values = val;
1218 }
1219
1220 /* Free all the values that have been allocated (except for those released).
1221 Call after each command, successful or not.
1222 In practice this is called before each command, which is sufficient. */
1223
1224 void
1225 free_all_values (void)
1226 {
1227 struct value *val;
1228 struct value *next;
1229
1230 for (val = all_values; val; val = next)
1231 {
1232 next = val->next;
1233 val->released = 1;
1234 value_free (val);
1235 }
1236
1237 all_values = 0;
1238 }
1239
1240 /* Frees all the elements in a chain of values. */
1241
1242 void
1243 free_value_chain (struct value *v)
1244 {
1245 struct value *next;
1246
1247 for (; v; v = next)
1248 {
1249 next = value_next (v);
1250 value_free (v);
1251 }
1252 }
1253
1254 /* Remove VAL from the chain all_values
1255 so it will not be freed automatically. */
1256
1257 void
1258 release_value (struct value *val)
1259 {
1260 struct value *v;
1261
1262 if (all_values == val)
1263 {
1264 all_values = val->next;
1265 val->next = NULL;
1266 val->released = 1;
1267 return;
1268 }
1269
1270 for (v = all_values; v; v = v->next)
1271 {
1272 if (v->next == val)
1273 {
1274 v->next = val->next;
1275 val->next = NULL;
1276 val->released = 1;
1277 break;
1278 }
1279 }
1280 }
1281
1282 /* If the value is not already released, release it.
1283 If the value is already released, increment its reference count.
1284 That is, this function ensures that the value is released from the
1285 value chain and that the caller owns a reference to it. */
1286
1287 void
1288 release_value_or_incref (struct value *val)
1289 {
1290 if (val->released)
1291 value_incref (val);
1292 else
1293 release_value (val);
1294 }
1295
1296 /* Release all values up to mark */
1297 struct value *
1298 value_release_to_mark (struct value *mark)
1299 {
1300 struct value *val;
1301 struct value *next;
1302
1303 for (val = next = all_values; next; next = next->next)
1304 {
1305 if (next->next == mark)
1306 {
1307 all_values = next->next;
1308 next->next = NULL;
1309 return val;
1310 }
1311 next->released = 1;
1312 }
1313 all_values = 0;
1314 return val;
1315 }
1316
1317 /* Return a copy of the value ARG.
1318 It contains the same contents, for same memory address,
1319 but it's a different block of storage. */
1320
1321 struct value *
1322 value_copy (struct value *arg)
1323 {
1324 struct type *encl_type = value_enclosing_type (arg);
1325 struct value *val;
1326
1327 if (value_lazy (arg))
1328 val = allocate_value_lazy (encl_type);
1329 else
1330 val = allocate_value (encl_type);
1331 val->type = arg->type;
1332 VALUE_LVAL (val) = VALUE_LVAL (arg);
1333 val->location = arg->location;
1334 val->offset = arg->offset;
1335 val->bitpos = arg->bitpos;
1336 val->bitsize = arg->bitsize;
1337 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
1338 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
1339 val->lazy = arg->lazy;
1340 val->optimized_out = arg->optimized_out;
1341 val->embedded_offset = value_embedded_offset (arg);
1342 val->pointed_to_offset = arg->pointed_to_offset;
1343 val->modifiable = arg->modifiable;
1344 if (!value_lazy (val))
1345 {
1346 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
1347 TYPE_LENGTH (value_enclosing_type (arg)));
1348
1349 }
1350 val->unavailable = VEC_copy (range_s, arg->unavailable);
1351 val->parent = arg->parent;
1352 if (val->parent)
1353 value_incref (val->parent);
1354 if (VALUE_LVAL (val) == lval_computed)
1355 {
1356 const struct lval_funcs *funcs = val->location.computed.funcs;
1357
1358 if (funcs->copy_closure)
1359 val->location.computed.closure = funcs->copy_closure (val);
1360 }
1361 return val;
1362 }
1363
1364 /* Return a version of ARG that is non-lvalue. */
1365
1366 struct value *
1367 value_non_lval (struct value *arg)
1368 {
1369 if (VALUE_LVAL (arg) != not_lval)
1370 {
1371 struct type *enc_type = value_enclosing_type (arg);
1372 struct value *val = allocate_value (enc_type);
1373
1374 memcpy (value_contents_all_raw (val), value_contents_all (arg),
1375 TYPE_LENGTH (enc_type));
1376 val->type = arg->type;
1377 set_value_embedded_offset (val, value_embedded_offset (arg));
1378 set_value_pointed_to_offset (val, value_pointed_to_offset (arg));
1379 return val;
1380 }
1381 return arg;
1382 }
1383
1384 void
1385 set_value_component_location (struct value *component,
1386 const struct value *whole)
1387 {
1388 if (whole->lval == lval_internalvar)
1389 VALUE_LVAL (component) = lval_internalvar_component;
1390 else
1391 VALUE_LVAL (component) = whole->lval;
1392
1393 component->location = whole->location;
1394 if (whole->lval == lval_computed)
1395 {
1396 const struct lval_funcs *funcs = whole->location.computed.funcs;
1397
1398 if (funcs->copy_closure)
1399 component->location.computed.closure = funcs->copy_closure (whole);
1400 }
1401 }
1402
1403 \f
1404 /* Access to the value history. */
1405
1406 /* Record a new value in the value history.
1407 Returns the absolute history index of the entry.
1408 Result of -1 indicates the value was not saved; otherwise it is the
1409 value history index of this new item. */
1410
1411 int
1412 record_latest_value (struct value *val)
1413 {
1414 int i;
1415
1416 /* We don't want this value to have anything to do with the inferior anymore.
1417 In particular, "set $1 = 50" should not affect the variable from which
1418 the value was taken, and fast watchpoints should be able to assume that
1419 a value on the value history never changes. */
1420 if (value_lazy (val))
1421 value_fetch_lazy (val);
1422 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
1423 from. This is a bit dubious, because then *&$1 does not just return $1
1424 but the current contents of that location. c'est la vie... */
1425 val->modifiable = 0;
1426 release_value (val);
1427
1428 /* Here we treat value_history_count as origin-zero
1429 and applying to the value being stored now. */
1430
1431 i = value_history_count % VALUE_HISTORY_CHUNK;
1432 if (i == 0)
1433 {
1434 struct value_history_chunk *new
1435 = (struct value_history_chunk *)
1436
1437 xmalloc (sizeof (struct value_history_chunk));
1438 memset (new->values, 0, sizeof new->values);
1439 new->next = value_history_chain;
1440 value_history_chain = new;
1441 }
1442
1443 value_history_chain->values[i] = val;
1444
1445 /* Now we regard value_history_count as origin-one
1446 and applying to the value just stored. */
1447
1448 return ++value_history_count;
1449 }
1450
1451 /* Return a copy of the value in the history with sequence number NUM. */
1452
1453 struct value *
1454 access_value_history (int num)
1455 {
1456 struct value_history_chunk *chunk;
1457 int i;
1458 int absnum = num;
1459
1460 if (absnum <= 0)
1461 absnum += value_history_count;
1462
1463 if (absnum <= 0)
1464 {
1465 if (num == 0)
1466 error (_("The history is empty."));
1467 else if (num == 1)
1468 error (_("There is only one value in the history."));
1469 else
1470 error (_("History does not go back to $$%d."), -num);
1471 }
1472 if (absnum > value_history_count)
1473 error (_("History has not yet reached $%d."), absnum);
1474
1475 absnum--;
1476
1477 /* Now absnum is always absolute and origin zero. */
1478
1479 chunk = value_history_chain;
1480 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK
1481 - absnum / VALUE_HISTORY_CHUNK;
1482 i > 0; i--)
1483 chunk = chunk->next;
1484
1485 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
1486 }
1487
1488 static void
1489 show_values (char *num_exp, int from_tty)
1490 {
1491 int i;
1492 struct value *val;
1493 static int num = 1;
1494
1495 if (num_exp)
1496 {
1497 /* "show values +" should print from the stored position.
1498 "show values <exp>" should print around value number <exp>. */
1499 if (num_exp[0] != '+' || num_exp[1] != '\0')
1500 num = parse_and_eval_long (num_exp) - 5;
1501 }
1502 else
1503 {
1504 /* "show values" means print the last 10 values. */
1505 num = value_history_count - 9;
1506 }
1507
1508 if (num <= 0)
1509 num = 1;
1510
1511 for (i = num; i < num + 10 && i <= value_history_count; i++)
1512 {
1513 struct value_print_options opts;
1514
1515 val = access_value_history (i);
1516 printf_filtered (("$%d = "), i);
1517 get_user_print_options (&opts);
1518 value_print (val, gdb_stdout, &opts);
1519 printf_filtered (("\n"));
1520 }
1521
1522 /* The next "show values +" should start after what we just printed. */
1523 num += 10;
1524
1525 /* Hitting just return after this command should do the same thing as
1526 "show values +". If num_exp is null, this is unnecessary, since
1527 "show values +" is not useful after "show values". */
1528 if (from_tty && num_exp)
1529 {
1530 num_exp[0] = '+';
1531 num_exp[1] = '\0';
1532 }
1533 }
1534 \f
1535 /* Internal variables. These are variables within the debugger
1536 that hold values assigned by debugger commands.
1537 The user refers to them with a '$' prefix
1538 that does not appear in the variable names stored internally. */
1539
1540 struct internalvar
1541 {
1542 struct internalvar *next;
1543 char *name;
1544
1545 /* We support various different kinds of content of an internal variable.
1546 enum internalvar_kind specifies the kind, and union internalvar_data
1547 provides the data associated with this particular kind. */
1548
1549 enum internalvar_kind
1550 {
1551 /* The internal variable is empty. */
1552 INTERNALVAR_VOID,
1553
1554 /* The value of the internal variable is provided directly as
1555 a GDB value object. */
1556 INTERNALVAR_VALUE,
1557
1558 /* A fresh value is computed via a call-back routine on every
1559 access to the internal variable. */
1560 INTERNALVAR_MAKE_VALUE,
1561
1562 /* The internal variable holds a GDB internal convenience function. */
1563 INTERNALVAR_FUNCTION,
1564
1565 /* The variable holds an integer value. */
1566 INTERNALVAR_INTEGER,
1567
1568 /* The variable holds a GDB-provided string. */
1569 INTERNALVAR_STRING,
1570
1571 } kind;
1572
1573 union internalvar_data
1574 {
1575 /* A value object used with INTERNALVAR_VALUE. */
1576 struct value *value;
1577
1578 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
1579 internalvar_make_value make_value;
1580
1581 /* The internal function used with INTERNALVAR_FUNCTION. */
1582 struct
1583 {
1584 struct internal_function *function;
1585 /* True if this is the canonical name for the function. */
1586 int canonical;
1587 } fn;
1588
1589 /* An integer value used with INTERNALVAR_INTEGER. */
1590 struct
1591 {
1592 /* If type is non-NULL, it will be used as the type to generate
1593 a value for this internal variable. If type is NULL, a default
1594 integer type for the architecture is used. */
1595 struct type *type;
1596 LONGEST val;
1597 } integer;
1598
1599 /* A string value used with INTERNALVAR_STRING. */
1600 char *string;
1601 } u;
1602 };
1603
1604 static struct internalvar *internalvars;
1605
1606 /* If the variable does not already exist create it and give it the
1607 value given. If no value is given then the default is zero. */
1608 static void
1609 init_if_undefined_command (char* args, int from_tty)
1610 {
1611 struct internalvar* intvar;
1612
1613 /* Parse the expression - this is taken from set_command(). */
1614 struct expression *expr = parse_expression (args);
1615 register struct cleanup *old_chain =
1616 make_cleanup (free_current_contents, &expr);
1617
1618 /* Validate the expression.
1619 Was the expression an assignment?
1620 Or even an expression at all? */
1621 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
1622 error (_("Init-if-undefined requires an assignment expression."));
1623
1624 /* Extract the variable from the parsed expression.
1625 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
1626 if (expr->elts[1].opcode != OP_INTERNALVAR)
1627 error (_("The first parameter to init-if-undefined "
1628 "should be a GDB variable."));
1629 intvar = expr->elts[2].internalvar;
1630
1631 /* Only evaluate the expression if the lvalue is void.
1632 This may still fail if the expresssion is invalid. */
1633 if (intvar->kind == INTERNALVAR_VOID)
1634 evaluate_expression (expr);
1635
1636 do_cleanups (old_chain);
1637 }
1638
1639
1640 /* Look up an internal variable with name NAME. NAME should not
1641 normally include a dollar sign.
1642
1643 If the specified internal variable does not exist,
1644 the return value is NULL. */
1645
1646 struct internalvar *
1647 lookup_only_internalvar (const char *name)
1648 {
1649 struct internalvar *var;
1650
1651 for (var = internalvars; var; var = var->next)
1652 if (strcmp (var->name, name) == 0)
1653 return var;
1654
1655 return NULL;
1656 }
1657
1658
1659 /* Create an internal variable with name NAME and with a void value.
1660 NAME should not normally include a dollar sign. */
1661
1662 struct internalvar *
1663 create_internalvar (const char *name)
1664 {
1665 struct internalvar *var;
1666
1667 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
1668 var->name = concat (name, (char *)NULL);
1669 var->kind = INTERNALVAR_VOID;
1670 var->next = internalvars;
1671 internalvars = var;
1672 return var;
1673 }
1674
1675 /* Create an internal variable with name NAME and register FUN as the
1676 function that value_of_internalvar uses to create a value whenever
1677 this variable is referenced. NAME should not normally include a
1678 dollar sign. */
1679
1680 struct internalvar *
1681 create_internalvar_type_lazy (char *name, internalvar_make_value fun)
1682 {
1683 struct internalvar *var = create_internalvar (name);
1684
1685 var->kind = INTERNALVAR_MAKE_VALUE;
1686 var->u.make_value = fun;
1687 return var;
1688 }
1689
1690 /* Look up an internal variable with name NAME. NAME should not
1691 normally include a dollar sign.
1692
1693 If the specified internal variable does not exist,
1694 one is created, with a void value. */
1695
1696 struct internalvar *
1697 lookup_internalvar (const char *name)
1698 {
1699 struct internalvar *var;
1700
1701 var = lookup_only_internalvar (name);
1702 if (var)
1703 return var;
1704
1705 return create_internalvar (name);
1706 }
1707
1708 /* Return current value of internal variable VAR. For variables that
1709 are not inherently typed, use a value type appropriate for GDBARCH. */
1710
1711 struct value *
1712 value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
1713 {
1714 struct value *val;
1715 struct trace_state_variable *tsv;
1716
1717 /* If there is a trace state variable of the same name, assume that
1718 is what we really want to see. */
1719 tsv = find_trace_state_variable (var->name);
1720 if (tsv)
1721 {
1722 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
1723 &(tsv->value));
1724 if (tsv->value_known)
1725 val = value_from_longest (builtin_type (gdbarch)->builtin_int64,
1726 tsv->value);
1727 else
1728 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1729 return val;
1730 }
1731
1732 switch (var->kind)
1733 {
1734 case INTERNALVAR_VOID:
1735 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1736 break;
1737
1738 case INTERNALVAR_FUNCTION:
1739 val = allocate_value (builtin_type (gdbarch)->internal_fn);
1740 break;
1741
1742 case INTERNALVAR_INTEGER:
1743 if (!var->u.integer.type)
1744 val = value_from_longest (builtin_type (gdbarch)->builtin_int,
1745 var->u.integer.val);
1746 else
1747 val = value_from_longest (var->u.integer.type, var->u.integer.val);
1748 break;
1749
1750 case INTERNALVAR_STRING:
1751 val = value_cstring (var->u.string, strlen (var->u.string),
1752 builtin_type (gdbarch)->builtin_char);
1753 break;
1754
1755 case INTERNALVAR_VALUE:
1756 val = value_copy (var->u.value);
1757 if (value_lazy (val))
1758 value_fetch_lazy (val);
1759 break;
1760
1761 case INTERNALVAR_MAKE_VALUE:
1762 val = (*var->u.make_value) (gdbarch, var);
1763 break;
1764
1765 default:
1766 internal_error (__FILE__, __LINE__, _("bad kind"));
1767 }
1768
1769 /* Change the VALUE_LVAL to lval_internalvar so that future operations
1770 on this value go back to affect the original internal variable.
1771
1772 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
1773 no underlying modifyable state in the internal variable.
1774
1775 Likewise, if the variable's value is a computed lvalue, we want
1776 references to it to produce another computed lvalue, where
1777 references and assignments actually operate through the
1778 computed value's functions.
1779
1780 This means that internal variables with computed values
1781 behave a little differently from other internal variables:
1782 assignments to them don't just replace the previous value
1783 altogether. At the moment, this seems like the behavior we
1784 want. */
1785
1786 if (var->kind != INTERNALVAR_MAKE_VALUE
1787 && val->lval != lval_computed)
1788 {
1789 VALUE_LVAL (val) = lval_internalvar;
1790 VALUE_INTERNALVAR (val) = var;
1791 }
1792
1793 return val;
1794 }
1795
1796 int
1797 get_internalvar_integer (struct internalvar *var, LONGEST *result)
1798 {
1799 if (var->kind == INTERNALVAR_INTEGER)
1800 {
1801 *result = var->u.integer.val;
1802 return 1;
1803 }
1804
1805 if (var->kind == INTERNALVAR_VALUE)
1806 {
1807 struct type *type = check_typedef (value_type (var->u.value));
1808
1809 if (TYPE_CODE (type) == TYPE_CODE_INT)
1810 {
1811 *result = value_as_long (var->u.value);
1812 return 1;
1813 }
1814 }
1815
1816 return 0;
1817 }
1818
1819 static int
1820 get_internalvar_function (struct internalvar *var,
1821 struct internal_function **result)
1822 {
1823 switch (var->kind)
1824 {
1825 case INTERNALVAR_FUNCTION:
1826 *result = var->u.fn.function;
1827 return 1;
1828
1829 default:
1830 return 0;
1831 }
1832 }
1833
1834 void
1835 set_internalvar_component (struct internalvar *var, int offset, int bitpos,
1836 int bitsize, struct value *newval)
1837 {
1838 gdb_byte *addr;
1839
1840 switch (var->kind)
1841 {
1842 case INTERNALVAR_VALUE:
1843 addr = value_contents_writeable (var->u.value);
1844
1845 if (bitsize)
1846 modify_field (value_type (var->u.value), addr + offset,
1847 value_as_long (newval), bitpos, bitsize);
1848 else
1849 memcpy (addr + offset, value_contents (newval),
1850 TYPE_LENGTH (value_type (newval)));
1851 break;
1852
1853 default:
1854 /* We can never get a component of any other kind. */
1855 internal_error (__FILE__, __LINE__, _("set_internalvar_component"));
1856 }
1857 }
1858
1859 void
1860 set_internalvar (struct internalvar *var, struct value *val)
1861 {
1862 enum internalvar_kind new_kind;
1863 union internalvar_data new_data = { 0 };
1864
1865 if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
1866 error (_("Cannot overwrite convenience function %s"), var->name);
1867
1868 /* Prepare new contents. */
1869 switch (TYPE_CODE (check_typedef (value_type (val))))
1870 {
1871 case TYPE_CODE_VOID:
1872 new_kind = INTERNALVAR_VOID;
1873 break;
1874
1875 case TYPE_CODE_INTERNAL_FUNCTION:
1876 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
1877 new_kind = INTERNALVAR_FUNCTION;
1878 get_internalvar_function (VALUE_INTERNALVAR (val),
1879 &new_data.fn.function);
1880 /* Copies created here are never canonical. */
1881 break;
1882
1883 default:
1884 new_kind = INTERNALVAR_VALUE;
1885 new_data.value = value_copy (val);
1886 new_data.value->modifiable = 1;
1887
1888 /* Force the value to be fetched from the target now, to avoid problems
1889 later when this internalvar is referenced and the target is gone or
1890 has changed. */
1891 if (value_lazy (new_data.value))
1892 value_fetch_lazy (new_data.value);
1893
1894 /* Release the value from the value chain to prevent it from being
1895 deleted by free_all_values. From here on this function should not
1896 call error () until new_data is installed into the var->u to avoid
1897 leaking memory. */
1898 release_value (new_data.value);
1899 break;
1900 }
1901
1902 /* Clean up old contents. */
1903 clear_internalvar (var);
1904
1905 /* Switch over. */
1906 var->kind = new_kind;
1907 var->u = new_data;
1908 /* End code which must not call error(). */
1909 }
1910
1911 void
1912 set_internalvar_integer (struct internalvar *var, LONGEST l)
1913 {
1914 /* Clean up old contents. */
1915 clear_internalvar (var);
1916
1917 var->kind = INTERNALVAR_INTEGER;
1918 var->u.integer.type = NULL;
1919 var->u.integer.val = l;
1920 }
1921
1922 void
1923 set_internalvar_string (struct internalvar *var, const char *string)
1924 {
1925 /* Clean up old contents. */
1926 clear_internalvar (var);
1927
1928 var->kind = INTERNALVAR_STRING;
1929 var->u.string = xstrdup (string);
1930 }
1931
1932 static void
1933 set_internalvar_function (struct internalvar *var, struct internal_function *f)
1934 {
1935 /* Clean up old contents. */
1936 clear_internalvar (var);
1937
1938 var->kind = INTERNALVAR_FUNCTION;
1939 var->u.fn.function = f;
1940 var->u.fn.canonical = 1;
1941 /* Variables installed here are always the canonical version. */
1942 }
1943
1944 void
1945 clear_internalvar (struct internalvar *var)
1946 {
1947 /* Clean up old contents. */
1948 switch (var->kind)
1949 {
1950 case INTERNALVAR_VALUE:
1951 value_free (var->u.value);
1952 break;
1953
1954 case INTERNALVAR_STRING:
1955 xfree (var->u.string);
1956 break;
1957
1958 default:
1959 break;
1960 }
1961
1962 /* Reset to void kind. */
1963 var->kind = INTERNALVAR_VOID;
1964 }
1965
1966 char *
1967 internalvar_name (struct internalvar *var)
1968 {
1969 return var->name;
1970 }
1971
1972 static struct internal_function *
1973 create_internal_function (const char *name,
1974 internal_function_fn handler, void *cookie)
1975 {
1976 struct internal_function *ifn = XNEW (struct internal_function);
1977
1978 ifn->name = xstrdup (name);
1979 ifn->handler = handler;
1980 ifn->cookie = cookie;
1981 return ifn;
1982 }
1983
1984 char *
1985 value_internal_function_name (struct value *val)
1986 {
1987 struct internal_function *ifn;
1988 int result;
1989
1990 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
1991 result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
1992 gdb_assert (result);
1993
1994 return ifn->name;
1995 }
1996
1997 struct value *
1998 call_internal_function (struct gdbarch *gdbarch,
1999 const struct language_defn *language,
2000 struct value *func, int argc, struct value **argv)
2001 {
2002 struct internal_function *ifn;
2003 int result;
2004
2005 gdb_assert (VALUE_LVAL (func) == lval_internalvar);
2006 result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
2007 gdb_assert (result);
2008
2009 return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
2010 }
2011
2012 /* The 'function' command. This does nothing -- it is just a
2013 placeholder to let "help function NAME" work. This is also used as
2014 the implementation of the sub-command that is created when
2015 registering an internal function. */
2016 static void
2017 function_command (char *command, int from_tty)
2018 {
2019 /* Do nothing. */
2020 }
2021
2022 /* Clean up if an internal function's command is destroyed. */
2023 static void
2024 function_destroyer (struct cmd_list_element *self, void *ignore)
2025 {
2026 xfree (self->name);
2027 xfree (self->doc);
2028 }
2029
2030 /* Add a new internal function. NAME is the name of the function; DOC
2031 is a documentation string describing the function. HANDLER is
2032 called when the function is invoked. COOKIE is an arbitrary
2033 pointer which is passed to HANDLER and is intended for "user
2034 data". */
2035 void
2036 add_internal_function (const char *name, const char *doc,
2037 internal_function_fn handler, void *cookie)
2038 {
2039 struct cmd_list_element *cmd;
2040 struct internal_function *ifn;
2041 struct internalvar *var = lookup_internalvar (name);
2042
2043 ifn = create_internal_function (name, handler, cookie);
2044 set_internalvar_function (var, ifn);
2045
2046 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
2047 &functionlist);
2048 cmd->destroyer = function_destroyer;
2049 }
2050
2051 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
2052 prevent cycles / duplicates. */
2053
2054 void
2055 preserve_one_value (struct value *value, struct objfile *objfile,
2056 htab_t copied_types)
2057 {
2058 if (TYPE_OBJFILE (value->type) == objfile)
2059 value->type = copy_type_recursive (objfile, value->type, copied_types);
2060
2061 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
2062 value->enclosing_type = copy_type_recursive (objfile,
2063 value->enclosing_type,
2064 copied_types);
2065 }
2066
2067 /* Likewise for internal variable VAR. */
2068
2069 static void
2070 preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
2071 htab_t copied_types)
2072 {
2073 switch (var->kind)
2074 {
2075 case INTERNALVAR_INTEGER:
2076 if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
2077 var->u.integer.type
2078 = copy_type_recursive (objfile, var->u.integer.type, copied_types);
2079 break;
2080
2081 case INTERNALVAR_VALUE:
2082 preserve_one_value (var->u.value, objfile, copied_types);
2083 break;
2084 }
2085 }
2086
2087 /* Update the internal variables and value history when OBJFILE is
2088 discarded; we must copy the types out of the objfile. New global types
2089 will be created for every convenience variable which currently points to
2090 this objfile's types, and the convenience variables will be adjusted to
2091 use the new global types. */
2092
2093 void
2094 preserve_values (struct objfile *objfile)
2095 {
2096 htab_t copied_types;
2097 struct value_history_chunk *cur;
2098 struct internalvar *var;
2099 int i;
2100
2101 /* Create the hash table. We allocate on the objfile's obstack, since
2102 it is soon to be deleted. */
2103 copied_types = create_copied_types_hash (objfile);
2104
2105 for (cur = value_history_chain; cur; cur = cur->next)
2106 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
2107 if (cur->values[i])
2108 preserve_one_value (cur->values[i], objfile, copied_types);
2109
2110 for (var = internalvars; var; var = var->next)
2111 preserve_one_internalvar (var, objfile, copied_types);
2112
2113 preserve_python_values (objfile, copied_types);
2114
2115 htab_delete (copied_types);
2116 }
2117
2118 static void
2119 show_convenience (char *ignore, int from_tty)
2120 {
2121 struct gdbarch *gdbarch = get_current_arch ();
2122 struct internalvar *var;
2123 int varseen = 0;
2124 struct value_print_options opts;
2125
2126 get_user_print_options (&opts);
2127 for (var = internalvars; var; var = var->next)
2128 {
2129 volatile struct gdb_exception ex;
2130
2131 if (!varseen)
2132 {
2133 varseen = 1;
2134 }
2135 printf_filtered (("$%s = "), var->name);
2136
2137 TRY_CATCH (ex, RETURN_MASK_ERROR)
2138 {
2139 struct value *val;
2140
2141 val = value_of_internalvar (gdbarch, var);
2142 value_print (val, gdb_stdout, &opts);
2143 }
2144 if (ex.reason < 0)
2145 fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
2146 printf_filtered (("\n"));
2147 }
2148 if (!varseen)
2149 printf_unfiltered (_("No debugger convenience variables now defined.\n"
2150 "Convenience variables have "
2151 "names starting with \"$\";\n"
2152 "use \"set\" as in \"set "
2153 "$foo = 5\" to define them.\n"));
2154 }
2155 \f
2156 /* Extract a value as a C number (either long or double).
2157 Knows how to convert fixed values to double, or
2158 floating values to long.
2159 Does not deallocate the value. */
2160
2161 LONGEST
2162 value_as_long (struct value *val)
2163 {
2164 /* This coerces arrays and functions, which is necessary (e.g.
2165 in disassemble_command). It also dereferences references, which
2166 I suspect is the most logical thing to do. */
2167 val = coerce_array (val);
2168 return unpack_long (value_type (val), value_contents (val));
2169 }
2170
2171 DOUBLEST
2172 value_as_double (struct value *val)
2173 {
2174 DOUBLEST foo;
2175 int inv;
2176
2177 foo = unpack_double (value_type (val), value_contents (val), &inv);
2178 if (inv)
2179 error (_("Invalid floating value found in program."));
2180 return foo;
2181 }
2182
2183 /* Extract a value as a C pointer. Does not deallocate the value.
2184 Note that val's type may not actually be a pointer; value_as_long
2185 handles all the cases. */
2186 CORE_ADDR
2187 value_as_address (struct value *val)
2188 {
2189 struct gdbarch *gdbarch = get_type_arch (value_type (val));
2190
2191 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2192 whether we want this to be true eventually. */
2193 #if 0
2194 /* gdbarch_addr_bits_remove is wrong if we are being called for a
2195 non-address (e.g. argument to "signal", "info break", etc.), or
2196 for pointers to char, in which the low bits *are* significant. */
2197 return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
2198 #else
2199
2200 /* There are several targets (IA-64, PowerPC, and others) which
2201 don't represent pointers to functions as simply the address of
2202 the function's entry point. For example, on the IA-64, a
2203 function pointer points to a two-word descriptor, generated by
2204 the linker, which contains the function's entry point, and the
2205 value the IA-64 "global pointer" register should have --- to
2206 support position-independent code. The linker generates
2207 descriptors only for those functions whose addresses are taken.
2208
2209 On such targets, it's difficult for GDB to convert an arbitrary
2210 function address into a function pointer; it has to either find
2211 an existing descriptor for that function, or call malloc and
2212 build its own. On some targets, it is impossible for GDB to
2213 build a descriptor at all: the descriptor must contain a jump
2214 instruction; data memory cannot be executed; and code memory
2215 cannot be modified.
2216
2217 Upon entry to this function, if VAL is a value of type `function'
2218 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
2219 value_address (val) is the address of the function. This is what
2220 you'll get if you evaluate an expression like `main'. The call
2221 to COERCE_ARRAY below actually does all the usual unary
2222 conversions, which includes converting values of type `function'
2223 to `pointer to function'. This is the challenging conversion
2224 discussed above. Then, `unpack_long' will convert that pointer
2225 back into an address.
2226
2227 So, suppose the user types `disassemble foo' on an architecture
2228 with a strange function pointer representation, on which GDB
2229 cannot build its own descriptors, and suppose further that `foo'
2230 has no linker-built descriptor. The address->pointer conversion
2231 will signal an error and prevent the command from running, even
2232 though the next step would have been to convert the pointer
2233 directly back into the same address.
2234
2235 The following shortcut avoids this whole mess. If VAL is a
2236 function, just return its address directly. */
2237 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
2238 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
2239 return value_address (val);
2240
2241 val = coerce_array (val);
2242
2243 /* Some architectures (e.g. Harvard), map instruction and data
2244 addresses onto a single large unified address space. For
2245 instance: An architecture may consider a large integer in the
2246 range 0x10000000 .. 0x1000ffff to already represent a data
2247 addresses (hence not need a pointer to address conversion) while
2248 a small integer would still need to be converted integer to
2249 pointer to address. Just assume such architectures handle all
2250 integer conversions in a single function. */
2251
2252 /* JimB writes:
2253
2254 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
2255 must admonish GDB hackers to make sure its behavior matches the
2256 compiler's, whenever possible.
2257
2258 In general, I think GDB should evaluate expressions the same way
2259 the compiler does. When the user copies an expression out of
2260 their source code and hands it to a `print' command, they should
2261 get the same value the compiler would have computed. Any
2262 deviation from this rule can cause major confusion and annoyance,
2263 and needs to be justified carefully. In other words, GDB doesn't
2264 really have the freedom to do these conversions in clever and
2265 useful ways.
2266
2267 AndrewC pointed out that users aren't complaining about how GDB
2268 casts integers to pointers; they are complaining that they can't
2269 take an address from a disassembly listing and give it to `x/i'.
2270 This is certainly important.
2271
2272 Adding an architecture method like integer_to_address() certainly
2273 makes it possible for GDB to "get it right" in all circumstances
2274 --- the target has complete control over how things get done, so
2275 people can Do The Right Thing for their target without breaking
2276 anyone else. The standard doesn't specify how integers get
2277 converted to pointers; usually, the ABI doesn't either, but
2278 ABI-specific code is a more reasonable place to handle it. */
2279
2280 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
2281 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
2282 && gdbarch_integer_to_address_p (gdbarch))
2283 return gdbarch_integer_to_address (gdbarch, value_type (val),
2284 value_contents (val));
2285
2286 return unpack_long (value_type (val), value_contents (val));
2287 #endif
2288 }
2289 \f
2290 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2291 as a long, or as a double, assuming the raw data is described
2292 by type TYPE. Knows how to convert different sizes of values
2293 and can convert between fixed and floating point. We don't assume
2294 any alignment for the raw data. Return value is in host byte order.
2295
2296 If you want functions and arrays to be coerced to pointers, and
2297 references to be dereferenced, call value_as_long() instead.
2298
2299 C++: It is assumed that the front-end has taken care of
2300 all matters concerning pointers to members. A pointer
2301 to member which reaches here is considered to be equivalent
2302 to an INT (or some size). After all, it is only an offset. */
2303
2304 LONGEST
2305 unpack_long (struct type *type, const gdb_byte *valaddr)
2306 {
2307 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2308 enum type_code code = TYPE_CODE (type);
2309 int len = TYPE_LENGTH (type);
2310 int nosign = TYPE_UNSIGNED (type);
2311
2312 switch (code)
2313 {
2314 case TYPE_CODE_TYPEDEF:
2315 return unpack_long (check_typedef (type), valaddr);
2316 case TYPE_CODE_ENUM:
2317 case TYPE_CODE_FLAGS:
2318 case TYPE_CODE_BOOL:
2319 case TYPE_CODE_INT:
2320 case TYPE_CODE_CHAR:
2321 case TYPE_CODE_RANGE:
2322 case TYPE_CODE_MEMBERPTR:
2323 if (nosign)
2324 return extract_unsigned_integer (valaddr, len, byte_order);
2325 else
2326 return extract_signed_integer (valaddr, len, byte_order);
2327
2328 case TYPE_CODE_FLT:
2329 return extract_typed_floating (valaddr, type);
2330
2331 case TYPE_CODE_DECFLOAT:
2332 /* libdecnumber has a function to convert from decimal to integer, but
2333 it doesn't work when the decimal number has a fractional part. */
2334 return decimal_to_doublest (valaddr, len, byte_order);
2335
2336 case TYPE_CODE_PTR:
2337 case TYPE_CODE_REF:
2338 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2339 whether we want this to be true eventually. */
2340 return extract_typed_address (valaddr, type);
2341
2342 default:
2343 error (_("Value can't be converted to integer."));
2344 }
2345 return 0; /* Placate lint. */
2346 }
2347
2348 /* Return a double value from the specified type and address.
2349 INVP points to an int which is set to 0 for valid value,
2350 1 for invalid value (bad float format). In either case,
2351 the returned double is OK to use. Argument is in target
2352 format, result is in host format. */
2353
2354 DOUBLEST
2355 unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
2356 {
2357 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2358 enum type_code code;
2359 int len;
2360 int nosign;
2361
2362 *invp = 0; /* Assume valid. */
2363 CHECK_TYPEDEF (type);
2364 code = TYPE_CODE (type);
2365 len = TYPE_LENGTH (type);
2366 nosign = TYPE_UNSIGNED (type);
2367 if (code == TYPE_CODE_FLT)
2368 {
2369 /* NOTE: cagney/2002-02-19: There was a test here to see if the
2370 floating-point value was valid (using the macro
2371 INVALID_FLOAT). That test/macro have been removed.
2372
2373 It turns out that only the VAX defined this macro and then
2374 only in a non-portable way. Fixing the portability problem
2375 wouldn't help since the VAX floating-point code is also badly
2376 bit-rotten. The target needs to add definitions for the
2377 methods gdbarch_float_format and gdbarch_double_format - these
2378 exactly describe the target floating-point format. The
2379 problem here is that the corresponding floatformat_vax_f and
2380 floatformat_vax_d values these methods should be set to are
2381 also not defined either. Oops!
2382
2383 Hopefully someone will add both the missing floatformat
2384 definitions and the new cases for floatformat_is_valid (). */
2385
2386 if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
2387 {
2388 *invp = 1;
2389 return 0.0;
2390 }
2391
2392 return extract_typed_floating (valaddr, type);
2393 }
2394 else if (code == TYPE_CODE_DECFLOAT)
2395 return decimal_to_doublest (valaddr, len, byte_order);
2396 else if (nosign)
2397 {
2398 /* Unsigned -- be sure we compensate for signed LONGEST. */
2399 return (ULONGEST) unpack_long (type, valaddr);
2400 }
2401 else
2402 {
2403 /* Signed -- we are OK with unpack_long. */
2404 return unpack_long (type, valaddr);
2405 }
2406 }
2407
2408 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
2409 as a CORE_ADDR, assuming the raw data is described by type TYPE.
2410 We don't assume any alignment for the raw data. Return value is in
2411 host byte order.
2412
2413 If you want functions and arrays to be coerced to pointers, and
2414 references to be dereferenced, call value_as_address() instead.
2415
2416 C++: It is assumed that the front-end has taken care of
2417 all matters concerning pointers to members. A pointer
2418 to member which reaches here is considered to be equivalent
2419 to an INT (or some size). After all, it is only an offset. */
2420
2421 CORE_ADDR
2422 unpack_pointer (struct type *type, const gdb_byte *valaddr)
2423 {
2424 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
2425 whether we want this to be true eventually. */
2426 return unpack_long (type, valaddr);
2427 }
2428
2429 \f
2430 /* Get the value of the FIELDNO'th field (which must be static) of
2431 TYPE. Return NULL if the field doesn't exist or has been
2432 optimized out. */
2433
2434 struct value *
2435 value_static_field (struct type *type, int fieldno)
2436 {
2437 struct value *retval;
2438
2439 switch (TYPE_FIELD_LOC_KIND (type, fieldno))
2440 {
2441 case FIELD_LOC_KIND_PHYSADDR:
2442 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2443 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
2444 break;
2445 case FIELD_LOC_KIND_PHYSNAME:
2446 {
2447 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
2448 /* TYPE_FIELD_NAME (type, fieldno); */
2449 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
2450
2451 if (sym == NULL)
2452 {
2453 /* With some compilers, e.g. HP aCC, static data members are
2454 reported as non-debuggable symbols. */
2455 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name,
2456 NULL, NULL);
2457
2458 if (!msym)
2459 return NULL;
2460 else
2461 {
2462 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2463 SYMBOL_VALUE_ADDRESS (msym));
2464 }
2465 }
2466 else
2467 retval = value_of_variable (sym, NULL);
2468 break;
2469 }
2470 default:
2471 gdb_assert_not_reached ("unexpected field location kind");
2472 }
2473
2474 return retval;
2475 }
2476
2477 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
2478 You have to be careful here, since the size of the data area for the value
2479 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
2480 than the old enclosing type, you have to allocate more space for the
2481 data. */
2482
2483 void
2484 set_value_enclosing_type (struct value *val, struct type *new_encl_type)
2485 {
2486 if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
2487 val->contents =
2488 (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
2489
2490 val->enclosing_type = new_encl_type;
2491 }
2492
2493 /* Given a value ARG1 (offset by OFFSET bytes)
2494 of a struct or union type ARG_TYPE,
2495 extract and return the value of one of its (non-static) fields.
2496 FIELDNO says which field. */
2497
2498 struct value *
2499 value_primitive_field (struct value *arg1, int offset,
2500 int fieldno, struct type *arg_type)
2501 {
2502 struct value *v;
2503 struct type *type;
2504
2505 CHECK_TYPEDEF (arg_type);
2506 type = TYPE_FIELD_TYPE (arg_type, fieldno);
2507
2508 /* Call check_typedef on our type to make sure that, if TYPE
2509 is a TYPE_CODE_TYPEDEF, its length is set to the length
2510 of the target type instead of zero. However, we do not
2511 replace the typedef type by the target type, because we want
2512 to keep the typedef in order to be able to print the type
2513 description correctly. */
2514 check_typedef (type);
2515
2516 if (value_optimized_out (arg1))
2517 v = allocate_optimized_out_value (type);
2518 else if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
2519 {
2520 /* Handle packed fields.
2521
2522 Create a new value for the bitfield, with bitpos and bitsize
2523 set. If possible, arrange offset and bitpos so that we can
2524 do a single aligned read of the size of the containing type.
2525 Otherwise, adjust offset to the byte containing the first
2526 bit. Assume that the address, offset, and embedded offset
2527 are sufficiently aligned. */
2528
2529 int bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
2530 int container_bitsize = TYPE_LENGTH (type) * 8;
2531
2532 v = allocate_value_lazy (type);
2533 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
2534 if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
2535 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST))
2536 v->bitpos = bitpos % container_bitsize;
2537 else
2538 v->bitpos = bitpos % 8;
2539 v->offset = (value_embedded_offset (arg1)
2540 + offset
2541 + (bitpos - v->bitpos) / 8);
2542 v->parent = arg1;
2543 value_incref (v->parent);
2544 if (!value_lazy (arg1))
2545 value_fetch_lazy (v);
2546 }
2547 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
2548 {
2549 /* This field is actually a base subobject, so preserve the
2550 entire object's contents for later references to virtual
2551 bases, etc. */
2552
2553 /* Lazy register values with offsets are not supported. */
2554 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
2555 value_fetch_lazy (arg1);
2556
2557 if (value_lazy (arg1))
2558 v = allocate_value_lazy (value_enclosing_type (arg1));
2559 else
2560 {
2561 v = allocate_value (value_enclosing_type (arg1));
2562 value_contents_copy_raw (v, 0, arg1, 0,
2563 TYPE_LENGTH (value_enclosing_type (arg1)));
2564 }
2565 v->type = type;
2566 v->offset = value_offset (arg1);
2567 v->embedded_offset = (offset + value_embedded_offset (arg1)
2568 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
2569 }
2570 else
2571 {
2572 /* Plain old data member */
2573 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
2574
2575 /* Lazy register values with offsets are not supported. */
2576 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
2577 value_fetch_lazy (arg1);
2578
2579 if (value_lazy (arg1))
2580 v = allocate_value_lazy (type);
2581 else
2582 {
2583 v = allocate_value (type);
2584 value_contents_copy_raw (v, value_embedded_offset (v),
2585 arg1, value_embedded_offset (arg1) + offset,
2586 TYPE_LENGTH (type));
2587 }
2588 v->offset = (value_offset (arg1) + offset
2589 + value_embedded_offset (arg1));
2590 }
2591 set_value_component_location (v, arg1);
2592 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
2593 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
2594 return v;
2595 }
2596
2597 /* Given a value ARG1 of a struct or union type,
2598 extract and return the value of one of its (non-static) fields.
2599 FIELDNO says which field. */
2600
2601 struct value *
2602 value_field (struct value *arg1, int fieldno)
2603 {
2604 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
2605 }
2606
2607 /* Return a non-virtual function as a value.
2608 F is the list of member functions which contains the desired method.
2609 J is an index into F which provides the desired method.
2610
2611 We only use the symbol for its address, so be happy with either a
2612 full symbol or a minimal symbol. */
2613
2614 struct value *
2615 value_fn_field (struct value **arg1p, struct fn_field *f,
2616 int j, struct type *type,
2617 int offset)
2618 {
2619 struct value *v;
2620 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
2621 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
2622 struct symbol *sym;
2623 struct minimal_symbol *msym;
2624
2625 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
2626 if (sym != NULL)
2627 {
2628 msym = NULL;
2629 }
2630 else
2631 {
2632 gdb_assert (sym == NULL);
2633 msym = lookup_minimal_symbol (physname, NULL, NULL);
2634 if (msym == NULL)
2635 return NULL;
2636 }
2637
2638 v = allocate_value (ftype);
2639 if (sym)
2640 {
2641 set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
2642 }
2643 else
2644 {
2645 /* The minimal symbol might point to a function descriptor;
2646 resolve it to the actual code address instead. */
2647 struct objfile *objfile = msymbol_objfile (msym);
2648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2649
2650 set_value_address (v,
2651 gdbarch_convert_from_func_ptr_addr
2652 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), &current_target));
2653 }
2654
2655 if (arg1p)
2656 {
2657 if (type != value_type (*arg1p))
2658 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
2659 value_addr (*arg1p)));
2660
2661 /* Move the `this' pointer according to the offset.
2662 VALUE_OFFSET (*arg1p) += offset; */
2663 }
2664
2665 return v;
2666 }
2667
2668 \f
2669
2670 /* Helper function for both unpack_value_bits_as_long and
2671 unpack_bits_as_long. See those functions for more details on the
2672 interface; the only difference is that this function accepts either
2673 a NULL or a non-NULL ORIGINAL_VALUE. */
2674
2675 static int
2676 unpack_value_bits_as_long_1 (struct type *field_type, const gdb_byte *valaddr,
2677 int embedded_offset, int bitpos, int bitsize,
2678 const struct value *original_value,
2679 LONGEST *result)
2680 {
2681 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
2682 ULONGEST val;
2683 ULONGEST valmask;
2684 int lsbcount;
2685 int bytes_read;
2686 int read_offset;
2687
2688 /* Read the minimum number of bytes required; there may not be
2689 enough bytes to read an entire ULONGEST. */
2690 CHECK_TYPEDEF (field_type);
2691 if (bitsize)
2692 bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
2693 else
2694 bytes_read = TYPE_LENGTH (field_type);
2695
2696 read_offset = bitpos / 8;
2697
2698 if (original_value != NULL
2699 && !value_bytes_available (original_value, embedded_offset + read_offset,
2700 bytes_read))
2701 return 0;
2702
2703 val = extract_unsigned_integer (valaddr + embedded_offset + read_offset,
2704 bytes_read, byte_order);
2705
2706 /* Extract bits. See comment above. */
2707
2708 if (gdbarch_bits_big_endian (get_type_arch (field_type)))
2709 lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
2710 else
2711 lsbcount = (bitpos % 8);
2712 val >>= lsbcount;
2713
2714 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
2715 If the field is signed, and is negative, then sign extend. */
2716
2717 if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
2718 {
2719 valmask = (((ULONGEST) 1) << bitsize) - 1;
2720 val &= valmask;
2721 if (!TYPE_UNSIGNED (field_type))
2722 {
2723 if (val & (valmask ^ (valmask >> 1)))
2724 {
2725 val |= ~valmask;
2726 }
2727 }
2728 }
2729
2730 *result = val;
2731 return 1;
2732 }
2733
2734 /* Unpack a bitfield of the specified FIELD_TYPE, from the object at
2735 VALADDR + EMBEDDED_OFFSET, and store the result in *RESULT.
2736 VALADDR points to the contents of ORIGINAL_VALUE, which must not be
2737 NULL. The bitfield starts at BITPOS bits and contains BITSIZE
2738 bits.
2739
2740 Returns false if the value contents are unavailable, otherwise
2741 returns true, indicating a valid value has been stored in *RESULT.
2742
2743 Extracting bits depends on endianness of the machine. Compute the
2744 number of least significant bits to discard. For big endian machines,
2745 we compute the total number of bits in the anonymous object, subtract
2746 off the bit count from the MSB of the object to the MSB of the
2747 bitfield, then the size of the bitfield, which leaves the LSB discard
2748 count. For little endian machines, the discard count is simply the
2749 number of bits from the LSB of the anonymous object to the LSB of the
2750 bitfield.
2751
2752 If the field is signed, we also do sign extension. */
2753
2754 int
2755 unpack_value_bits_as_long (struct type *field_type, const gdb_byte *valaddr,
2756 int embedded_offset, int bitpos, int bitsize,
2757 const struct value *original_value,
2758 LONGEST *result)
2759 {
2760 gdb_assert (original_value != NULL);
2761
2762 return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2763 bitpos, bitsize, original_value, result);
2764
2765 }
2766
2767 /* Unpack a field FIELDNO of the specified TYPE, from the object at
2768 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
2769 ORIGINAL_VALUE. See unpack_value_bits_as_long for more
2770 details. */
2771
2772 static int
2773 unpack_value_field_as_long_1 (struct type *type, const gdb_byte *valaddr,
2774 int embedded_offset, int fieldno,
2775 const struct value *val, LONGEST *result)
2776 {
2777 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
2778 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
2779 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
2780
2781 return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2782 bitpos, bitsize, val,
2783 result);
2784 }
2785
2786 /* Unpack a field FIELDNO of the specified TYPE, from the object at
2787 VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of
2788 ORIGINAL_VALUE, which must not be NULL. See
2789 unpack_value_bits_as_long for more details. */
2790
2791 int
2792 unpack_value_field_as_long (struct type *type, const gdb_byte *valaddr,
2793 int embedded_offset, int fieldno,
2794 const struct value *val, LONGEST *result)
2795 {
2796 gdb_assert (val != NULL);
2797
2798 return unpack_value_field_as_long_1 (type, valaddr, embedded_offset,
2799 fieldno, val, result);
2800 }
2801
2802 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous
2803 object at VALADDR. See unpack_value_bits_as_long for more details.
2804 This function differs from unpack_value_field_as_long in that it
2805 operates without a struct value object. */
2806
2807 LONGEST
2808 unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
2809 {
2810 LONGEST result;
2811
2812 unpack_value_field_as_long_1 (type, valaddr, 0, fieldno, NULL, &result);
2813 return result;
2814 }
2815
2816 /* Return a new value with type TYPE, which is FIELDNO field of the
2817 object at VALADDR + EMBEDDEDOFFSET. VALADDR points to the contents
2818 of VAL. If the VAL's contents required to extract the bitfield
2819 from are unavailable, the new value is correspondingly marked as
2820 unavailable. */
2821
2822 struct value *
2823 value_field_bitfield (struct type *type, int fieldno,
2824 const gdb_byte *valaddr,
2825 int embedded_offset, const struct value *val)
2826 {
2827 LONGEST l;
2828
2829 if (!unpack_value_field_as_long (type, valaddr, embedded_offset, fieldno,
2830 val, &l))
2831 {
2832 struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
2833 struct value *retval = allocate_value (field_type);
2834 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (field_type));
2835 return retval;
2836 }
2837 else
2838 {
2839 return value_from_longest (TYPE_FIELD_TYPE (type, fieldno), l);
2840 }
2841 }
2842
2843 /* Modify the value of a bitfield. ADDR points to a block of memory in
2844 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
2845 is the desired value of the field, in host byte order. BITPOS and BITSIZE
2846 indicate which bits (in target bit order) comprise the bitfield.
2847 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
2848 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
2849
2850 void
2851 modify_field (struct type *type, gdb_byte *addr,
2852 LONGEST fieldval, int bitpos, int bitsize)
2853 {
2854 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2855 ULONGEST oword;
2856 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
2857 int bytesize;
2858
2859 /* Normalize BITPOS. */
2860 addr += bitpos / 8;
2861 bitpos %= 8;
2862
2863 /* If a negative fieldval fits in the field in question, chop
2864 off the sign extension bits. */
2865 if ((~fieldval & ~(mask >> 1)) == 0)
2866 fieldval &= mask;
2867
2868 /* Warn if value is too big to fit in the field in question. */
2869 if (0 != (fieldval & ~mask))
2870 {
2871 /* FIXME: would like to include fieldval in the message, but
2872 we don't have a sprintf_longest. */
2873 warning (_("Value does not fit in %d bits."), bitsize);
2874
2875 /* Truncate it, otherwise adjoining fields may be corrupted. */
2876 fieldval &= mask;
2877 }
2878
2879 /* Ensure no bytes outside of the modified ones get accessed as it may cause
2880 false valgrind reports. */
2881
2882 bytesize = (bitpos + bitsize + 7) / 8;
2883 oword = extract_unsigned_integer (addr, bytesize, byte_order);
2884
2885 /* Shifting for bit field depends on endianness of the target machine. */
2886 if (gdbarch_bits_big_endian (get_type_arch (type)))
2887 bitpos = bytesize * 8 - bitpos - bitsize;
2888
2889 oword &= ~(mask << bitpos);
2890 oword |= fieldval << bitpos;
2891
2892 store_unsigned_integer (addr, bytesize, byte_order, oword);
2893 }
2894 \f
2895 /* Pack NUM into BUF using a target format of TYPE. */
2896
2897 void
2898 pack_long (gdb_byte *buf, struct type *type, LONGEST num)
2899 {
2900 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2901 int len;
2902
2903 type = check_typedef (type);
2904 len = TYPE_LENGTH (type);
2905
2906 switch (TYPE_CODE (type))
2907 {
2908 case TYPE_CODE_INT:
2909 case TYPE_CODE_CHAR:
2910 case TYPE_CODE_ENUM:
2911 case TYPE_CODE_FLAGS:
2912 case TYPE_CODE_BOOL:
2913 case TYPE_CODE_RANGE:
2914 case TYPE_CODE_MEMBERPTR:
2915 store_signed_integer (buf, len, byte_order, num);
2916 break;
2917
2918 case TYPE_CODE_REF:
2919 case TYPE_CODE_PTR:
2920 store_typed_address (buf, type, (CORE_ADDR) num);
2921 break;
2922
2923 default:
2924 error (_("Unexpected type (%d) encountered for integer constant."),
2925 TYPE_CODE (type));
2926 }
2927 }
2928
2929
2930 /* Pack NUM into BUF using a target format of TYPE. */
2931
2932 void
2933 pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
2934 {
2935 int len;
2936 enum bfd_endian byte_order;
2937
2938 type = check_typedef (type);
2939 len = TYPE_LENGTH (type);
2940 byte_order = gdbarch_byte_order (get_type_arch (type));
2941
2942 switch (TYPE_CODE (type))
2943 {
2944 case TYPE_CODE_INT:
2945 case TYPE_CODE_CHAR:
2946 case TYPE_CODE_ENUM:
2947 case TYPE_CODE_FLAGS:
2948 case TYPE_CODE_BOOL:
2949 case TYPE_CODE_RANGE:
2950 case TYPE_CODE_MEMBERPTR:
2951 store_unsigned_integer (buf, len, byte_order, num);
2952 break;
2953
2954 case TYPE_CODE_REF:
2955 case TYPE_CODE_PTR:
2956 store_typed_address (buf, type, (CORE_ADDR) num);
2957 break;
2958
2959 default:
2960 error (_("Unexpected type (%d) encountered "
2961 "for unsigned integer constant."),
2962 TYPE_CODE (type));
2963 }
2964 }
2965
2966
2967 /* Convert C numbers into newly allocated values. */
2968
2969 struct value *
2970 value_from_longest (struct type *type, LONGEST num)
2971 {
2972 struct value *val = allocate_value (type);
2973
2974 pack_long (value_contents_raw (val), type, num);
2975 return val;
2976 }
2977
2978
2979 /* Convert C unsigned numbers into newly allocated values. */
2980
2981 struct value *
2982 value_from_ulongest (struct type *type, ULONGEST num)
2983 {
2984 struct value *val = allocate_value (type);
2985
2986 pack_unsigned_long (value_contents_raw (val), type, num);
2987
2988 return val;
2989 }
2990
2991
2992 /* Create a value representing a pointer of type TYPE to the address
2993 ADDR. */
2994 struct value *
2995 value_from_pointer (struct type *type, CORE_ADDR addr)
2996 {
2997 struct value *val = allocate_value (type);
2998
2999 store_typed_address (value_contents_raw (val), check_typedef (type), addr);
3000 return val;
3001 }
3002
3003
3004 /* Create a value of type TYPE whose contents come from VALADDR, if it
3005 is non-null, and whose memory address (in the inferior) is
3006 ADDRESS. */
3007
3008 struct value *
3009 value_from_contents_and_address (struct type *type,
3010 const gdb_byte *valaddr,
3011 CORE_ADDR address)
3012 {
3013 struct value *v;
3014
3015 if (valaddr == NULL)
3016 v = allocate_value_lazy (type);
3017 else
3018 {
3019 v = allocate_value (type);
3020 memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
3021 }
3022 set_value_address (v, address);
3023 VALUE_LVAL (v) = lval_memory;
3024 return v;
3025 }
3026
3027 /* Create a value of type TYPE holding the contents CONTENTS.
3028 The new value is `not_lval'. */
3029
3030 struct value *
3031 value_from_contents (struct type *type, const gdb_byte *contents)
3032 {
3033 struct value *result;
3034
3035 result = allocate_value (type);
3036 memcpy (value_contents_raw (result), contents, TYPE_LENGTH (type));
3037 return result;
3038 }
3039
3040 struct value *
3041 value_from_double (struct type *type, DOUBLEST num)
3042 {
3043 struct value *val = allocate_value (type);
3044 struct type *base_type = check_typedef (type);
3045 enum type_code code = TYPE_CODE (base_type);
3046
3047 if (code == TYPE_CODE_FLT)
3048 {
3049 store_typed_floating (value_contents_raw (val), base_type, num);
3050 }
3051 else
3052 error (_("Unexpected type encountered for floating constant."));
3053
3054 return val;
3055 }
3056
3057 struct value *
3058 value_from_decfloat (struct type *type, const gdb_byte *dec)
3059 {
3060 struct value *val = allocate_value (type);
3061
3062 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
3063 return val;
3064 }
3065
3066 /* Extract a value from the history file. Input will be of the form
3067 $digits or $$digits. See block comment above 'write_dollar_variable'
3068 for details. */
3069
3070 struct value *
3071 value_from_history_ref (char *h, char **endp)
3072 {
3073 int index, len;
3074
3075 if (h[0] == '$')
3076 len = 1;
3077 else
3078 return NULL;
3079
3080 if (h[1] == '$')
3081 len = 2;
3082
3083 /* Find length of numeral string. */
3084 for (; isdigit (h[len]); len++)
3085 ;
3086
3087 /* Make sure numeral string is not part of an identifier. */
3088 if (h[len] == '_' || isalpha (h[len]))
3089 return NULL;
3090
3091 /* Now collect the index value. */
3092 if (h[1] == '$')
3093 {
3094 if (len == 2)
3095 {
3096 /* For some bizarre reason, "$$" is equivalent to "$$1",
3097 rather than to "$$0" as it ought to be! */
3098 index = -1;
3099 *endp += len;
3100 }
3101 else
3102 index = -strtol (&h[2], endp, 10);
3103 }
3104 else
3105 {
3106 if (len == 1)
3107 {
3108 /* "$" is equivalent to "$0". */
3109 index = 0;
3110 *endp += len;
3111 }
3112 else
3113 index = strtol (&h[1], endp, 10);
3114 }
3115
3116 return access_value_history (index);
3117 }
3118
3119 struct value *
3120 coerce_ref_if_computed (const struct value *arg)
3121 {
3122 const struct lval_funcs *funcs;
3123
3124 if (TYPE_CODE (check_typedef (value_type (arg))) != TYPE_CODE_REF)
3125 return NULL;
3126
3127 if (value_lval_const (arg) != lval_computed)
3128 return NULL;
3129
3130 funcs = value_computed_funcs (arg);
3131 if (funcs->coerce_ref == NULL)
3132 return NULL;
3133
3134 return funcs->coerce_ref (arg);
3135 }
3136
3137 /* Look at value.h for description. */
3138
3139 struct value *
3140 readjust_indirect_value_type (struct value *value, struct type *enc_type,
3141 struct type *original_type,
3142 struct value *original_value)
3143 {
3144 /* Re-adjust type. */
3145 deprecated_set_value_type (value, TYPE_TARGET_TYPE (original_type));
3146
3147 /* Add embedding info. */
3148 set_value_enclosing_type (value, enc_type);
3149 set_value_embedded_offset (value, value_pointed_to_offset (original_value));
3150
3151 /* We may be pointing to an object of some derived type. */
3152 return value_full_object (value, NULL, 0, 0, 0);
3153 }
3154
3155 struct value *
3156 coerce_ref (struct value *arg)
3157 {
3158 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
3159 struct value *retval;
3160 struct type *enc_type;
3161
3162 retval = coerce_ref_if_computed (arg);
3163 if (retval)
3164 return retval;
3165
3166 if (TYPE_CODE (value_type_arg_tmp) != TYPE_CODE_REF)
3167 return arg;
3168
3169 enc_type = check_typedef (value_enclosing_type (arg));
3170 enc_type = TYPE_TARGET_TYPE (enc_type);
3171
3172 retval = value_at_lazy (enc_type,
3173 unpack_pointer (value_type (arg),
3174 value_contents (arg)));
3175 return readjust_indirect_value_type (retval, enc_type,
3176 value_type_arg_tmp, arg);
3177 }
3178
3179 struct value *
3180 coerce_array (struct value *arg)
3181 {
3182 struct type *type;
3183
3184 arg = coerce_ref (arg);
3185 type = check_typedef (value_type (arg));
3186
3187 switch (TYPE_CODE (type))
3188 {
3189 case TYPE_CODE_ARRAY:
3190 if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
3191 arg = value_coerce_array (arg);
3192 break;
3193 case TYPE_CODE_FUNC:
3194 arg = value_coerce_function (arg);
3195 break;
3196 }
3197 return arg;
3198 }
3199 \f
3200
3201 /* Return true if the function returning the specified type is using
3202 the convention of returning structures in memory (passing in the
3203 address as a hidden first parameter). */
3204
3205 int
3206 using_struct_return (struct gdbarch *gdbarch,
3207 struct type *func_type, struct type *value_type)
3208 {
3209 enum type_code code = TYPE_CODE (value_type);
3210
3211 if (code == TYPE_CODE_ERROR)
3212 error (_("Function return type unknown."));
3213
3214 if (code == TYPE_CODE_VOID)
3215 /* A void return value is never in memory. See also corresponding
3216 code in "print_return_value". */
3217 return 0;
3218
3219 /* Probe the architecture for the return-value convention. */
3220 return (gdbarch_return_value (gdbarch, func_type, value_type,
3221 NULL, NULL, NULL)
3222 != RETURN_VALUE_REGISTER_CONVENTION);
3223 }
3224
3225 /* Set the initialized field in a value struct. */
3226
3227 void
3228 set_value_initialized (struct value *val, int status)
3229 {
3230 val->initialized = status;
3231 }
3232
3233 /* Return the initialized field in a value struct. */
3234
3235 int
3236 value_initialized (struct value *val)
3237 {
3238 return val->initialized;
3239 }
3240
3241 void
3242 _initialize_values (void)
3243 {
3244 add_cmd ("convenience", no_class, show_convenience, _("\
3245 Debugger convenience (\"$foo\") variables.\n\
3246 These variables are created when you assign them values;\n\
3247 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
3248 \n\
3249 A few convenience variables are given values automatically:\n\
3250 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
3251 \"$__\" holds the contents of the last address examined with \"x\"."),
3252 &showlist);
3253
3254 add_cmd ("values", no_set_class, show_values, _("\
3255 Elements of value history around item number IDX (or last ten)."),
3256 &showlist);
3257
3258 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
3259 Initialize a convenience variable if necessary.\n\
3260 init-if-undefined VARIABLE = EXPRESSION\n\
3261 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
3262 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
3263 VARIABLE is already initialized."));
3264
3265 add_prefix_cmd ("function", no_class, function_command, _("\
3266 Placeholder command for showing help on convenience functions."),
3267 &functionlist, "function ", 0, &cmdlist);
3268 }
This page took 0.177377 seconds and 4 git commands to generate.