* blockframe.c (find_pc_partial_function_gnu_ifunc): Change type of
[deliverable/binutils-gdb.git] / gdb / value.c
CommitLineData
c906108c 1/* Low level packing and unpacking of values for GDB, the GNU Debugger.
1bac305b 2
c5a57081 3 Copyright (C) 1986-2000, 2002-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
e17c207e 21#include "arch-utils.h"
c906108c
SS
22#include "gdb_string.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "value.h"
26#include "gdbcore.h"
c906108c
SS
27#include "command.h"
28#include "gdbcmd.h"
29#include "target.h"
30#include "language.h"
c906108c 31#include "demangle.h"
d16aafd8 32#include "doublest.h"
5ae326fa 33#include "gdb_assert.h"
36160dc4 34#include "regcache.h"
fe898f56 35#include "block.h"
27bc4d80 36#include "dfp.h"
bccdca4a 37#include "objfiles.h"
79a45b7d 38#include "valprint.h"
bc3b79fd 39#include "cli/cli-decode.h"
8af8e3bc 40#include "exceptions.h"
a08702d6 41#include "python/python.h"
3bd0f5ef 42#include <ctype.h>
0914bcdb
SS
43#include "tracepoint.h"
44
581e13c1 45/* Prototypes for exported functions. */
c906108c 46
a14ed312 47void _initialize_values (void);
c906108c 48
bc3b79fd
TJB
49/* Definition of a user function. */
50struct internal_function
51{
52 /* The name of the function. It is a bit odd to have this in the
53 function itself -- the user might use a differently-named
54 convenience variable to hold the function. */
55 char *name;
56
57 /* The handler. */
58 internal_function_fn handler;
59
60 /* User data for the handler. */
61 void *cookie;
62};
63
4e07d55f
PA
64/* Defines an [OFFSET, OFFSET + LENGTH) range. */
65
66struct range
67{
68 /* Lowest offset in the range. */
69 int offset;
70
71 /* Length of the range. */
72 int length;
73};
74
75typedef struct range range_s;
76
77DEF_VEC_O(range_s);
78
79/* Returns true if the ranges defined by [offset1, offset1+len1) and
80 [offset2, offset2+len2) overlap. */
81
82static int
83ranges_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
98static int
99range_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
107static int
108ranges_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
bc3b79fd
TJB
168static struct cmd_list_element *functionlist;
169
87784a47
TT
170/* Note that the fields in this structure are arranged to save a bit
171 of memory. */
172
91294c83
AC
173struct 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. */
87784a47
TT
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;
91294c83 208
e848a8a5
TT
209 /* If the value has been released. */
210 unsigned int released : 1;
211
91294c83
AC
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;
5f5233d4
PA
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 {
c8f2448a
JK
228 /* Functions to call. */
229 const struct lval_funcs *funcs;
230
231 /* Closure for those functions to use. */
232 void *closure;
5f5233d4 233 } computed;
91294c83
AC
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
32c9a795 247 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
581e13c1 248 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
91294c83
AC
249 int bitpos;
250
87784a47
TT
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
4ea48cc1
DJ
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
91294c83
AC
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
a08702d6
TJB
316 variables, put into the value history or exposed to Python are
317 taken off this list. */
91294c83
AC
318 struct value *next;
319
320 /* Register number if the value is from a register. */
321 short regnum;
322
3e3d7139
JG
323 /* Actual contents of the value. Target byte-order. NULL or not
324 valid if lazy is nonzero. */
325 gdb_byte *contents;
828d3400 326
4e07d55f
PA
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;
91294c83
AC
331};
332
4e07d55f
PA
333int
334value_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
ec0a52e1
PA
341int
342value_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
4e07d55f
PA
354void
355mark_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 {
6bfc80c7 449 struct range *bef = VEC_index (range_s, value->unavailable, i - 1);
4e07d55f
PA
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
c8c1c22f
PA
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
524static int
525find_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
538int
539value_available_contents_eq (const struct value *val1, int offset1,
540 const struct value *val2, int offset2,
541 int length)
542{
c8c1c22f 543 int idx1 = 0, idx2 = 0;
c8c1c22f
PA
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
c8c1c22f
PA
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)
cd24cfaa
PA
563 return (memcmp (val1->contents + offset1,
564 val2->contents + offset2,
565 length) == 0);
c8c1c22f
PA
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. */
cd24cfaa
PA
598 if (memcmp (val1->contents + offset1,
599 val2->contents + offset2,
600 l1) != 0)
c8c1c22f
PA
601 return 0;
602
c8c1c22f
PA
603 length -= h1;
604 offset1 += h1;
605 offset2 += h1;
606 }
607
608 return 1;
609}
610
581e13c1 611/* Prototypes for local functions. */
c906108c 612
a14ed312 613static void show_values (char *, int);
c906108c 614
a14ed312 615static void show_convenience (char *, int);
c906108c 616
c906108c
SS
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
626struct value_history_chunk
c5aa993b
JM
627 {
628 struct value_history_chunk *next;
f23631e4 629 struct value *values[VALUE_HISTORY_CHUNK];
c5aa993b 630 };
c906108c
SS
631
632/* Chain of chunks now in use. */
633
634static struct value_history_chunk *value_history_chain;
635
581e13c1 636static int value_history_count; /* Abs number of last entry stored. */
bc3b79fd 637
c906108c
SS
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
f23631e4 643static struct value *all_values;
c906108c 644
3e3d7139
JG
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. */
c906108c 648
f23631e4 649struct value *
3e3d7139 650allocate_value_lazy (struct type *type)
c906108c 651{
f23631e4 652 struct value *val;
c54eabfa
JK
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);
c906108c 661
3e3d7139
JG
662 val = (struct value *) xzalloc (sizeof (struct value));
663 val->contents = NULL;
df407dfe 664 val->next = all_values;
c906108c 665 all_values = val;
df407dfe 666 val->type = type;
4754a64e 667 val->enclosing_type = type;
c906108c 668 VALUE_LVAL (val) = not_lval;
42ae5230 669 val->location.address = 0;
1df6926e 670 VALUE_FRAME_ID (val) = null_frame_id;
df407dfe
AC
671 val->offset = 0;
672 val->bitpos = 0;
673 val->bitsize = 0;
9ee8fc9d 674 VALUE_REGNUM (val) = -1;
3e3d7139 675 val->lazy = 1;
feb13ab0 676 val->optimized_out = 0;
13c3b5f5 677 val->embedded_offset = 0;
b44d461b 678 val->pointed_to_offset = 0;
c906108c 679 val->modifiable = 1;
42be36b3 680 val->initialized = 1; /* Default to initialized. */
828d3400
DJ
681
682 /* Values start out on the all_values chain. */
683 val->reference_count = 1;
684
c906108c
SS
685 return val;
686}
687
3e3d7139
JG
688/* Allocate the contents of VAL if it has not been allocated yet. */
689
690void
691allocate_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
699struct value *
700allocate_value (struct type *type)
701{
702 struct value *val = allocate_value_lazy (type);
a109c7c1 703
3e3d7139
JG
704 allocate_value_contents (val);
705 val->lazy = 0;
706 return val;
707}
708
c906108c 709/* Allocate a value that has the correct length
938f5214 710 for COUNT repetitions of type TYPE. */
c906108c 711
f23631e4 712struct value *
fba45db2 713allocate_repeat_value (struct type *type, int count)
c906108c 714{
c5aa993b 715 int low_bound = current_language->string_lower_bound; /* ??? */
c906108c
SS
716 /* FIXME-type-allocation: need a way to free this type when we are
717 done with it. */
e3506a9f
UW
718 struct type *array_type
719 = lookup_array_range_type (type, low_bound, count + low_bound - 1);
a109c7c1 720
e3506a9f 721 return allocate_value (array_type);
c906108c
SS
722}
723
5f5233d4
PA
724struct value *
725allocate_computed_value (struct type *type,
c8f2448a 726 const struct lval_funcs *funcs,
5f5233d4
PA
727 void *closure)
728{
41e8491f 729 struct value *v = allocate_value_lazy (type);
a109c7c1 730
5f5233d4
PA
731 VALUE_LVAL (v) = lval_computed;
732 v->location.computed.funcs = funcs;
733 v->location.computed.closure = closure;
5f5233d4
PA
734
735 return v;
736}
737
a7035dbb
JK
738/* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */
739
740struct value *
741allocate_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
df407dfe
AC
750/* Accessor methods. */
751
17cf0ecd
AC
752struct value *
753value_next (struct value *value)
754{
755 return value->next;
756}
757
df407dfe 758struct type *
0e03807e 759value_type (const struct value *value)
df407dfe
AC
760{
761 return value->type;
762}
04624583
AC
763void
764deprecated_set_value_type (struct value *value, struct type *type)
765{
766 value->type = type;
767}
df407dfe
AC
768
769int
0e03807e 770value_offset (const struct value *value)
df407dfe
AC
771{
772 return value->offset;
773}
f5cf64a7
AC
774void
775set_value_offset (struct value *value, int offset)
776{
777 value->offset = offset;
778}
df407dfe
AC
779
780int
0e03807e 781value_bitpos (const struct value *value)
df407dfe
AC
782{
783 return value->bitpos;
784}
9bbda503
AC
785void
786set_value_bitpos (struct value *value, int bit)
787{
788 value->bitpos = bit;
789}
df407dfe
AC
790
791int
0e03807e 792value_bitsize (const struct value *value)
df407dfe
AC
793{
794 return value->bitsize;
795}
9bbda503
AC
796void
797set_value_bitsize (struct value *value, int bit)
798{
799 value->bitsize = bit;
800}
df407dfe 801
4ea48cc1
DJ
802struct value *
803value_parent (struct value *value)
804{
805 return value->parent;
806}
807
fc1a4b47 808gdb_byte *
990a07ab
AC
809value_contents_raw (struct value *value)
810{
3e3d7139
JG
811 allocate_value_contents (value);
812 return value->contents + value->embedded_offset;
990a07ab
AC
813}
814
fc1a4b47 815gdb_byte *
990a07ab
AC
816value_contents_all_raw (struct value *value)
817{
3e3d7139
JG
818 allocate_value_contents (value);
819 return value->contents;
990a07ab
AC
820}
821
4754a64e
AC
822struct type *
823value_enclosing_type (struct value *value)
824{
825 return value->enclosing_type;
826}
827
0e03807e 828static void
4e07d55f 829require_not_optimized_out (const struct value *value)
0e03807e
TT
830{
831 if (value->optimized_out)
832 error (_("value has been optimized out"));
833}
834
4e07d55f
PA
835static void
836require_available (const struct value *value)
837{
838 if (!VEC_empty (range_s, value->unavailable))
8af8e3bc 839 throw_error (NOT_AVAILABLE_ERROR, _("value is not available"));
4e07d55f
PA
840}
841
fc1a4b47 842const gdb_byte *
0e03807e 843value_contents_for_printing (struct value *value)
46615f07
AC
844{
845 if (value->lazy)
846 value_fetch_lazy (value);
3e3d7139 847 return value->contents;
46615f07
AC
848}
849
de4127a3
PA
850const gdb_byte *
851value_contents_for_printing_const (const struct value *value)
852{
853 gdb_assert (!value->lazy);
854 return value->contents;
855}
856
0e03807e
TT
857const gdb_byte *
858value_contents_all (struct value *value)
859{
860 const gdb_byte *result = value_contents_for_printing (value);
861 require_not_optimized_out (value);
4e07d55f 862 require_available (value);
0e03807e
TT
863 return result;
864}
865
29976f3f
PA
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. */
39d37385
PA
875
876void
877value_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
29976f3f
PA
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
39d37385
PA
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
29976f3f
PA
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. */
39d37385
PA
924
925void
926value_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
d69fe07e
AC
937int
938value_lazy (struct value *value)
939{
940 return value->lazy;
941}
942
dfa52d88
AC
943void
944set_value_lazy (struct value *value, int val)
945{
946 value->lazy = val;
947}
948
4e5d721f
DE
949int
950value_stack (struct value *value)
951{
952 return value->stack;
953}
954
955void
956set_value_stack (struct value *value, int val)
957{
958 value->stack = val;
959}
960
fc1a4b47 961const gdb_byte *
0fd88904
AC
962value_contents (struct value *value)
963{
0e03807e
TT
964 const gdb_byte *result = value_contents_writeable (value);
965 require_not_optimized_out (value);
4e07d55f 966 require_available (value);
0e03807e 967 return result;
0fd88904
AC
968}
969
fc1a4b47 970gdb_byte *
0fd88904
AC
971value_contents_writeable (struct value *value)
972{
973 if (value->lazy)
974 value_fetch_lazy (value);
fc0c53a0 975 return value_contents_raw (value);
0fd88904
AC
976}
977
a6c442d8
MK
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
982int
983value_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
feb13ab0
AC
998int
999value_optimized_out (struct value *value)
1000{
1001 return value->optimized_out;
1002}
1003
1004void
1005set_value_optimized_out (struct value *value, int val)
1006{
1007 value->optimized_out = val;
1008}
13c3b5f5 1009
0e03807e
TT
1010int
1011value_entirely_optimized_out (const struct value *value)
1012{
1013 if (!value->optimized_out)
1014 return 0;
1015 if (value->lval != lval_computed
ba19bb4d 1016 || !value->location.computed.funcs->check_any_valid)
0e03807e 1017 return 1;
b65c7efe 1018 return !value->location.computed.funcs->check_any_valid (value);
0e03807e
TT
1019}
1020
1021int
1022value_bits_valid (const struct value *value, int offset, int length)
1023{
e7303042 1024 if (!value->optimized_out)
0e03807e
TT
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
8cf6f0b1
TT
1033int
1034value_bits_synthetic_pointer (const struct value *value,
1035 int offset, int length)
1036{
e7303042 1037 if (value->lval != lval_computed
8cf6f0b1
TT
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
13c3b5f5
AC
1045int
1046value_embedded_offset (struct value *value)
1047{
1048 return value->embedded_offset;
1049}
1050
1051void
1052set_value_embedded_offset (struct value *value, int val)
1053{
1054 value->embedded_offset = val;
1055}
b44d461b
AC
1056
1057int
1058value_pointed_to_offset (struct value *value)
1059{
1060 return value->pointed_to_offset;
1061}
1062
1063void
1064set_value_pointed_to_offset (struct value *value, int val)
1065{
1066 value->pointed_to_offset = val;
1067}
13bb5560 1068
c8f2448a 1069const struct lval_funcs *
a471c594 1070value_computed_funcs (const struct value *v)
5f5233d4 1071{
a471c594 1072 gdb_assert (value_lval_const (v) == lval_computed);
5f5233d4
PA
1073
1074 return v->location.computed.funcs;
1075}
1076
1077void *
0e03807e 1078value_computed_closure (const struct value *v)
5f5233d4 1079{
0e03807e 1080 gdb_assert (v->lval == lval_computed);
5f5233d4
PA
1081
1082 return v->location.computed.closure;
1083}
1084
13bb5560
AC
1085enum lval_type *
1086deprecated_value_lval_hack (struct value *value)
1087{
1088 return &value->lval;
1089}
1090
a471c594
JK
1091enum lval_type
1092value_lval_const (const struct value *value)
1093{
1094 return value->lval;
1095}
1096
42ae5230 1097CORE_ADDR
de4127a3 1098value_address (const struct value *value)
42ae5230
TT
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
1106CORE_ADDR
1107value_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
1115void
1116set_value_address (struct value *value, CORE_ADDR addr)
13bb5560 1117{
42ae5230
TT
1118 gdb_assert (value->lval != lval_internalvar
1119 && value->lval != lval_internalvar_component);
1120 value->location.address = addr;
13bb5560
AC
1121}
1122
1123struct internalvar **
1124deprecated_value_internalvar_hack (struct value *value)
1125{
1126 return &value->location.internalvar;
1127}
1128
1129struct frame_id *
1130deprecated_value_frame_id_hack (struct value *value)
1131{
1132 return &value->frame_id;
1133}
1134
1135short *
1136deprecated_value_regnum_hack (struct value *value)
1137{
1138 return &value->regnum;
1139}
88e3b34b
AC
1140
1141int
1142deprecated_value_modifiable (struct value *value)
1143{
1144 return value->modifiable;
1145}
1146void
1147deprecated_set_value_modifiable (struct value *value, int modifiable)
1148{
1149 value->modifiable = modifiable;
1150}
990a07ab 1151\f
c906108c
SS
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. */
f23631e4 1155struct value *
fba45db2 1156value_mark (void)
c906108c
SS
1157{
1158 return all_values;
1159}
1160
828d3400
DJ
1161/* Take a reference to VAL. VAL will not be deallocated until all
1162 references are released. */
1163
1164void
1165value_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
3e3d7139
JG
1174void
1175value_free (struct value *val)
1176{
1177 if (val)
5f5233d4 1178 {
828d3400
DJ
1179 gdb_assert (val->reference_count > 0);
1180 val->reference_count--;
1181 if (val->reference_count > 0)
1182 return;
1183
4ea48cc1
DJ
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
5f5233d4
PA
1189 if (VALUE_LVAL (val) == lval_computed)
1190 {
c8f2448a 1191 const struct lval_funcs *funcs = val->location.computed.funcs;
5f5233d4
PA
1192
1193 if (funcs->free_closure)
1194 funcs->free_closure (val);
1195 }
1196
1197 xfree (val->contents);
4e07d55f 1198 VEC_free (range_s, val->unavailable);
5f5233d4 1199 }
3e3d7139
JG
1200 xfree (val);
1201}
1202
c906108c
SS
1203/* Free all values allocated since MARK was obtained by value_mark
1204 (except for those released). */
1205void
f23631e4 1206value_free_to_mark (struct value *mark)
c906108c 1207{
f23631e4
AC
1208 struct value *val;
1209 struct value *next;
c906108c
SS
1210
1211 for (val = all_values; val && val != mark; val = next)
1212 {
df407dfe 1213 next = val->next;
e848a8a5 1214 val->released = 1;
c906108c
SS
1215 value_free (val);
1216 }
1217 all_values = val;
1218}
1219
1220/* Free all the values that have been allocated (except for those released).
725e88af
DE
1221 Call after each command, successful or not.
1222 In practice this is called before each command, which is sufficient. */
c906108c
SS
1223
1224void
fba45db2 1225free_all_values (void)
c906108c 1226{
f23631e4
AC
1227 struct value *val;
1228 struct value *next;
c906108c
SS
1229
1230 for (val = all_values; val; val = next)
1231 {
df407dfe 1232 next = val->next;
e848a8a5 1233 val->released = 1;
c906108c
SS
1234 value_free (val);
1235 }
1236
1237 all_values = 0;
1238}
1239
0cf6dd15
TJB
1240/* Frees all the elements in a chain of values. */
1241
1242void
1243free_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
c906108c
SS
1254/* Remove VAL from the chain all_values
1255 so it will not be freed automatically. */
1256
1257void
f23631e4 1258release_value (struct value *val)
c906108c 1259{
f23631e4 1260 struct value *v;
c906108c
SS
1261
1262 if (all_values == val)
1263 {
1264 all_values = val->next;
06a64a0b 1265 val->next = NULL;
e848a8a5 1266 val->released = 1;
c906108c
SS
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;
06a64a0b 1275 val->next = NULL;
e848a8a5 1276 val->released = 1;
c906108c
SS
1277 break;
1278 }
1279 }
1280}
1281
e848a8a5
TT
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
1287void
1288release_value_or_incref (struct value *val)
1289{
1290 if (val->released)
1291 value_incref (val);
1292 else
1293 release_value (val);
1294}
1295
c906108c 1296/* Release all values up to mark */
f23631e4
AC
1297struct value *
1298value_release_to_mark (struct value *mark)
c906108c 1299{
f23631e4
AC
1300 struct value *val;
1301 struct value *next;
c906108c 1302
df407dfe 1303 for (val = next = all_values; next; next = next->next)
e848a8a5
TT
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 }
c906108c
SS
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
f23631e4
AC
1321struct value *
1322value_copy (struct value *arg)
c906108c 1323{
4754a64e 1324 struct type *encl_type = value_enclosing_type (arg);
3e3d7139
JG
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);
df407dfe 1331 val->type = arg->type;
c906108c 1332 VALUE_LVAL (val) = VALUE_LVAL (arg);
6f7c8fc2 1333 val->location = arg->location;
df407dfe
AC
1334 val->offset = arg->offset;
1335 val->bitpos = arg->bitpos;
1336 val->bitsize = arg->bitsize;
1df6926e 1337 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
9ee8fc9d 1338 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
d69fe07e 1339 val->lazy = arg->lazy;
feb13ab0 1340 val->optimized_out = arg->optimized_out;
13c3b5f5 1341 val->embedded_offset = value_embedded_offset (arg);
b44d461b 1342 val->pointed_to_offset = arg->pointed_to_offset;
c906108c 1343 val->modifiable = arg->modifiable;
d69fe07e 1344 if (!value_lazy (val))
c906108c 1345 {
990a07ab 1346 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
4754a64e 1347 TYPE_LENGTH (value_enclosing_type (arg)));
c906108c
SS
1348
1349 }
4e07d55f 1350 val->unavailable = VEC_copy (range_s, arg->unavailable);
4ea48cc1
DJ
1351 val->parent = arg->parent;
1352 if (val->parent)
1353 value_incref (val->parent);
5f5233d4
PA
1354 if (VALUE_LVAL (val) == lval_computed)
1355 {
c8f2448a 1356 const struct lval_funcs *funcs = val->location.computed.funcs;
5f5233d4
PA
1357
1358 if (funcs->copy_closure)
1359 val->location.computed.closure = funcs->copy_closure (val);
1360 }
c906108c
SS
1361 return val;
1362}
74bcbdf3 1363
c37f7098
KW
1364/* Return a version of ARG that is non-lvalue. */
1365
1366struct value *
1367value_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
74bcbdf3 1384void
0e03807e
TT
1385set_value_component_location (struct value *component,
1386 const struct value *whole)
74bcbdf3 1387{
0e03807e 1388 if (whole->lval == lval_internalvar)
74bcbdf3
PA
1389 VALUE_LVAL (component) = lval_internalvar_component;
1390 else
0e03807e 1391 VALUE_LVAL (component) = whole->lval;
5f5233d4 1392
74bcbdf3 1393 component->location = whole->location;
0e03807e 1394 if (whole->lval == lval_computed)
5f5233d4 1395 {
c8f2448a 1396 const struct lval_funcs *funcs = whole->location.computed.funcs;
5f5233d4
PA
1397
1398 if (funcs->copy_closure)
1399 component->location.computed.closure = funcs->copy_closure (whole);
1400 }
74bcbdf3
PA
1401}
1402
c906108c
SS
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
1411int
f23631e4 1412record_latest_value (struct value *val)
c906108c
SS
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. */
d69fe07e 1420 if (value_lazy (val))
c906108c
SS
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 {
f23631e4 1434 struct value_history_chunk *new
a109c7c1
MS
1435 = (struct value_history_chunk *)
1436
c5aa993b 1437 xmalloc (sizeof (struct value_history_chunk));
c906108c
SS
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
f23631e4 1453struct value *
fba45db2 1454access_value_history (int num)
c906108c 1455{
f23631e4 1456 struct value_history_chunk *chunk;
52f0bd74
AC
1457 int i;
1458 int absnum = num;
c906108c
SS
1459
1460 if (absnum <= 0)
1461 absnum += value_history_count;
1462
1463 if (absnum <= 0)
1464 {
1465 if (num == 0)
8a3fe4f8 1466 error (_("The history is empty."));
c906108c 1467 else if (num == 1)
8a3fe4f8 1468 error (_("There is only one value in the history."));
c906108c 1469 else
8a3fe4f8 1470 error (_("History does not go back to $$%d."), -num);
c906108c
SS
1471 }
1472 if (absnum > value_history_count)
8a3fe4f8 1473 error (_("History has not yet reached $%d."), absnum);
c906108c
SS
1474
1475 absnum--;
1476
1477 /* Now absnum is always absolute and origin zero. */
1478
1479 chunk = value_history_chain;
3e43a32a
MS
1480 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK
1481 - absnum / VALUE_HISTORY_CHUNK;
c906108c
SS
1482 i > 0; i--)
1483 chunk = chunk->next;
1484
1485 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
1486}
1487
c906108c 1488static void
fba45db2 1489show_values (char *num_exp, int from_tty)
c906108c 1490{
52f0bd74 1491 int i;
f23631e4 1492 struct value *val;
c906108c
SS
1493 static int num = 1;
1494
1495 if (num_exp)
1496 {
f132ba9d
TJB
1497 /* "show values +" should print from the stored position.
1498 "show values <exp>" should print around value number <exp>. */
c906108c 1499 if (num_exp[0] != '+' || num_exp[1] != '\0')
bb518678 1500 num = parse_and_eval_long (num_exp) - 5;
c906108c
SS
1501 }
1502 else
1503 {
f132ba9d 1504 /* "show values" means print the last 10 values. */
c906108c
SS
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 {
79a45b7d 1513 struct value_print_options opts;
a109c7c1 1514
c906108c 1515 val = access_value_history (i);
a3f17187 1516 printf_filtered (("$%d = "), i);
79a45b7d
TT
1517 get_user_print_options (&opts);
1518 value_print (val, gdb_stdout, &opts);
a3f17187 1519 printf_filtered (("\n"));
c906108c
SS
1520 }
1521
f132ba9d 1522 /* The next "show values +" should start after what we just printed. */
c906108c
SS
1523 num += 10;
1524
1525 /* Hitting just return after this command should do the same thing as
f132ba9d
TJB
1526 "show values +". If num_exp is null, this is unnecessary, since
1527 "show values +" is not useful after "show values". */
c906108c
SS
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
4fa62494
UW
1540struct internalvar
1541{
1542 struct internalvar *next;
1543 char *name;
4fa62494 1544
78267919
UW
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,
4fa62494 1561
78267919
UW
1562 /* The internal variable holds a GDB internal convenience function. */
1563 INTERNALVAR_FUNCTION,
1564
cab0c772
UW
1565 /* The variable holds an integer value. */
1566 INTERNALVAR_INTEGER,
1567
78267919
UW
1568 /* The variable holds a GDB-provided string. */
1569 INTERNALVAR_STRING,
1570
1571 } kind;
4fa62494 1572
4fa62494
UW
1573 union internalvar_data
1574 {
78267919
UW
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
cab0c772 1589 /* An integer value used with INTERNALVAR_INTEGER. */
78267919
UW
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;
cab0c772
UW
1596 LONGEST val;
1597 } integer;
1598
78267919
UW
1599 /* A string value used with INTERNALVAR_STRING. */
1600 char *string;
4fa62494
UW
1601 } u;
1602};
1603
c906108c
SS
1604static struct internalvar *internalvars;
1605
3e43a32a
MS
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. */
53e5f3cf
AS
1608static void
1609init_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)
3e43a32a
MS
1627 error (_("The first parameter to init-if-undefined "
1628 "should be a GDB variable."));
53e5f3cf
AS
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. */
78267919 1633 if (intvar->kind == INTERNALVAR_VOID)
53e5f3cf
AS
1634 evaluate_expression (expr);
1635
1636 do_cleanups (old_chain);
1637}
1638
1639
c906108c
SS
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,
c4a3d09a 1644 the return value is NULL. */
c906108c
SS
1645
1646struct internalvar *
bc3b79fd 1647lookup_only_internalvar (const char *name)
c906108c 1648{
52f0bd74 1649 struct internalvar *var;
c906108c
SS
1650
1651 for (var = internalvars; var; var = var->next)
5cb316ef 1652 if (strcmp (var->name, name) == 0)
c906108c
SS
1653 return var;
1654
c4a3d09a
MF
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
1662struct internalvar *
bc3b79fd 1663create_internalvar (const char *name)
c4a3d09a
MF
1664{
1665 struct internalvar *var;
a109c7c1 1666
c906108c 1667 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
1754f103 1668 var->name = concat (name, (char *)NULL);
78267919 1669 var->kind = INTERNALVAR_VOID;
c906108c
SS
1670 var->next = internalvars;
1671 internalvars = var;
1672 return var;
1673}
1674
4aa995e1
PA
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
1680struct internalvar *
1681create_internalvar_type_lazy (char *name, internalvar_make_value fun)
1682{
4fa62494 1683 struct internalvar *var = create_internalvar (name);
a109c7c1 1684
78267919
UW
1685 var->kind = INTERNALVAR_MAKE_VALUE;
1686 var->u.make_value = fun;
4aa995e1
PA
1687 return var;
1688}
c4a3d09a
MF
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
1696struct internalvar *
bc3b79fd 1697lookup_internalvar (const char *name)
c4a3d09a
MF
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
78267919
UW
1708/* Return current value of internal variable VAR. For variables that
1709 are not inherently typed, use a value type appropriate for GDBARCH. */
1710
f23631e4 1711struct value *
78267919 1712value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
c906108c 1713{
f23631e4 1714 struct value *val;
0914bcdb
SS
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 }
c906108c 1731
78267919 1732 switch (var->kind)
5f5233d4 1733 {
78267919
UW
1734 case INTERNALVAR_VOID:
1735 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1736 break;
4fa62494 1737
78267919
UW
1738 case INTERNALVAR_FUNCTION:
1739 val = allocate_value (builtin_type (gdbarch)->internal_fn);
1740 break;
4fa62494 1741
cab0c772
UW
1742 case INTERNALVAR_INTEGER:
1743 if (!var->u.integer.type)
78267919 1744 val = value_from_longest (builtin_type (gdbarch)->builtin_int,
cab0c772 1745 var->u.integer.val);
78267919 1746 else
cab0c772
UW
1747 val = value_from_longest (var->u.integer.type, var->u.integer.val);
1748 break;
1749
78267919
UW
1750 case INTERNALVAR_STRING:
1751 val = value_cstring (var->u.string, strlen (var->u.string),
1752 builtin_type (gdbarch)->builtin_char);
1753 break;
4fa62494 1754
78267919
UW
1755 case INTERNALVAR_VALUE:
1756 val = value_copy (var->u.value);
4aa995e1
PA
1757 if (value_lazy (val))
1758 value_fetch_lazy (val);
78267919 1759 break;
4aa995e1 1760
78267919
UW
1761 case INTERNALVAR_MAKE_VALUE:
1762 val = (*var->u.make_value) (gdbarch, var);
1763 break;
1764
1765 default:
9b20d036 1766 internal_error (__FILE__, __LINE__, _("bad kind"));
78267919
UW
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;
5f5233d4 1791 }
d3c139e9 1792
4fa62494
UW
1793 return val;
1794}
d3c139e9 1795
4fa62494
UW
1796int
1797get_internalvar_integer (struct internalvar *var, LONGEST *result)
1798{
3158c6ed 1799 if (var->kind == INTERNALVAR_INTEGER)
4fa62494 1800 {
cab0c772
UW
1801 *result = var->u.integer.val;
1802 return 1;
3158c6ed 1803 }
d3c139e9 1804
3158c6ed
PA
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 }
4fa62494 1814 }
3158c6ed
PA
1815
1816 return 0;
4fa62494 1817}
d3c139e9 1818
4fa62494
UW
1819static int
1820get_internalvar_function (struct internalvar *var,
1821 struct internal_function **result)
1822{
78267919 1823 switch (var->kind)
d3c139e9 1824 {
78267919
UW
1825 case INTERNALVAR_FUNCTION:
1826 *result = var->u.fn.function;
4fa62494 1827 return 1;
d3c139e9 1828
4fa62494
UW
1829 default:
1830 return 0;
1831 }
c906108c
SS
1832}
1833
1834void
fba45db2 1835set_internalvar_component (struct internalvar *var, int offset, int bitpos,
f23631e4 1836 int bitsize, struct value *newval)
c906108c 1837{
4fa62494 1838 gdb_byte *addr;
c906108c 1839
78267919 1840 switch (var->kind)
4fa62494 1841 {
78267919
UW
1842 case INTERNALVAR_VALUE:
1843 addr = value_contents_writeable (var->u.value);
4fa62494
UW
1844
1845 if (bitsize)
50810684 1846 modify_field (value_type (var->u.value), addr + offset,
4fa62494
UW
1847 value_as_long (newval), bitpos, bitsize);
1848 else
1849 memcpy (addr + offset, value_contents (newval),
1850 TYPE_LENGTH (value_type (newval)));
1851 break;
78267919
UW
1852
1853 default:
1854 /* We can never get a component of any other kind. */
9b20d036 1855 internal_error (__FILE__, __LINE__, _("set_internalvar_component"));
4fa62494 1856 }
c906108c
SS
1857}
1858
1859void
f23631e4 1860set_internalvar (struct internalvar *var, struct value *val)
c906108c 1861{
78267919 1862 enum internalvar_kind new_kind;
4fa62494 1863 union internalvar_data new_data = { 0 };
c906108c 1864
78267919 1865 if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
bc3b79fd
TJB
1866 error (_("Cannot overwrite convenience function %s"), var->name);
1867
4fa62494 1868 /* Prepare new contents. */
78267919 1869 switch (TYPE_CODE (check_typedef (value_type (val))))
4fa62494
UW
1870 {
1871 case TYPE_CODE_VOID:
78267919 1872 new_kind = INTERNALVAR_VOID;
4fa62494
UW
1873 break;
1874
1875 case TYPE_CODE_INTERNAL_FUNCTION:
1876 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
78267919
UW
1877 new_kind = INTERNALVAR_FUNCTION;
1878 get_internalvar_function (VALUE_INTERNALVAR (val),
1879 &new_data.fn.function);
1880 /* Copies created here are never canonical. */
4fa62494
UW
1881 break;
1882
4fa62494 1883 default:
78267919
UW
1884 new_kind = INTERNALVAR_VALUE;
1885 new_data.value = value_copy (val);
1886 new_data.value->modifiable = 1;
4fa62494
UW
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. */
78267919
UW
1891 if (value_lazy (new_data.value))
1892 value_fetch_lazy (new_data.value);
4fa62494
UW
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. */
78267919 1898 release_value (new_data.value);
4fa62494
UW
1899 break;
1900 }
1901
1902 /* Clean up old contents. */
1903 clear_internalvar (var);
1904
1905 /* Switch over. */
78267919 1906 var->kind = new_kind;
4fa62494 1907 var->u = new_data;
c906108c
SS
1908 /* End code which must not call error(). */
1909}
1910
4fa62494
UW
1911void
1912set_internalvar_integer (struct internalvar *var, LONGEST l)
1913{
1914 /* Clean up old contents. */
1915 clear_internalvar (var);
1916
cab0c772
UW
1917 var->kind = INTERNALVAR_INTEGER;
1918 var->u.integer.type = NULL;
1919 var->u.integer.val = l;
78267919
UW
1920}
1921
1922void
1923set_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);
4fa62494
UW
1930}
1931
1932static void
1933set_internalvar_function (struct internalvar *var, struct internal_function *f)
1934{
1935 /* Clean up old contents. */
1936 clear_internalvar (var);
1937
78267919
UW
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. */
4fa62494
UW
1942}
1943
1944void
1945clear_internalvar (struct internalvar *var)
1946{
1947 /* Clean up old contents. */
78267919 1948 switch (var->kind)
4fa62494 1949 {
78267919
UW
1950 case INTERNALVAR_VALUE:
1951 value_free (var->u.value);
1952 break;
1953
1954 case INTERNALVAR_STRING:
1955 xfree (var->u.string);
4fa62494
UW
1956 break;
1957
1958 default:
4fa62494
UW
1959 break;
1960 }
1961
78267919
UW
1962 /* Reset to void kind. */
1963 var->kind = INTERNALVAR_VOID;
4fa62494
UW
1964}
1965
c906108c 1966char *
fba45db2 1967internalvar_name (struct internalvar *var)
c906108c
SS
1968{
1969 return var->name;
1970}
1971
4fa62494
UW
1972static struct internal_function *
1973create_internal_function (const char *name,
1974 internal_function_fn handler, void *cookie)
bc3b79fd 1975{
bc3b79fd 1976 struct internal_function *ifn = XNEW (struct internal_function);
a109c7c1 1977
bc3b79fd
TJB
1978 ifn->name = xstrdup (name);
1979 ifn->handler = handler;
1980 ifn->cookie = cookie;
4fa62494 1981 return ifn;
bc3b79fd
TJB
1982}
1983
1984char *
1985value_internal_function_name (struct value *val)
1986{
4fa62494
UW
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
bc3b79fd
TJB
1994 return ifn->name;
1995}
1996
1997struct value *
d452c4bc
UW
1998call_internal_function (struct gdbarch *gdbarch,
1999 const struct language_defn *language,
2000 struct value *func, int argc, struct value **argv)
bc3b79fd 2001{
4fa62494
UW
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
d452c4bc 2009 return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
bc3b79fd
TJB
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. */
2016static void
2017function_command (char *command, int from_tty)
2018{
2019 /* Do nothing. */
2020}
2021
2022/* Clean up if an internal function's command is destroyed. */
2023static void
2024function_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". */
2035void
2036add_internal_function (const char *name, const char *doc,
2037 internal_function_fn handler, void *cookie)
2038{
2039 struct cmd_list_element *cmd;
4fa62494 2040 struct internal_function *ifn;
bc3b79fd 2041 struct internalvar *var = lookup_internalvar (name);
4fa62494
UW
2042
2043 ifn = create_internal_function (name, handler, cookie);
2044 set_internalvar_function (var, ifn);
bc3b79fd
TJB
2045
2046 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
2047 &functionlist);
2048 cmd->destroyer = function_destroyer;
2049}
2050
ae5a43e0
DJ
2051/* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
2052 prevent cycles / duplicates. */
2053
4e7a5ef5 2054void
ae5a43e0
DJ
2055preserve_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
78267919
UW
2067/* Likewise for internal variable VAR. */
2068
2069static void
2070preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
2071 htab_t copied_types)
2072{
2073 switch (var->kind)
2074 {
cab0c772
UW
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
78267919
UW
2081 case INTERNALVAR_VALUE:
2082 preserve_one_value (var->u.value, objfile, copied_types);
2083 break;
2084 }
2085}
2086
ae5a43e0
DJ
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. */
c906108c
SS
2092
2093void
ae5a43e0 2094preserve_values (struct objfile *objfile)
c906108c 2095{
ae5a43e0
DJ
2096 htab_t copied_types;
2097 struct value_history_chunk *cur;
52f0bd74 2098 struct internalvar *var;
ae5a43e0 2099 int i;
c906108c 2100
ae5a43e0
DJ
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)
78267919 2111 preserve_one_internalvar (var, objfile, copied_types);
ae5a43e0 2112
4e7a5ef5 2113 preserve_python_values (objfile, copied_types);
a08702d6 2114
ae5a43e0 2115 htab_delete (copied_types);
c906108c
SS
2116}
2117
2118static void
fba45db2 2119show_convenience (char *ignore, int from_tty)
c906108c 2120{
e17c207e 2121 struct gdbarch *gdbarch = get_current_arch ();
52f0bd74 2122 struct internalvar *var;
c906108c 2123 int varseen = 0;
79a45b7d 2124 struct value_print_options opts;
c906108c 2125
79a45b7d 2126 get_user_print_options (&opts);
c906108c
SS
2127 for (var = internalvars; var; var = var->next)
2128 {
c709acd1
PA
2129 volatile struct gdb_exception ex;
2130
c906108c
SS
2131 if (!varseen)
2132 {
2133 varseen = 1;
2134 }
a3f17187 2135 printf_filtered (("$%s = "), var->name);
c709acd1
PA
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);
a3f17187 2146 printf_filtered (("\n"));
c906108c
SS
2147 }
2148 if (!varseen)
3e43a32a
MS
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"));
c906108c
SS
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
2161LONGEST
f23631e4 2162value_as_long (struct value *val)
c906108c
SS
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. */
994b9211 2167 val = coerce_array (val);
0fd88904 2168 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
2169}
2170
2171DOUBLEST
f23631e4 2172value_as_double (struct value *val)
c906108c
SS
2173{
2174 DOUBLEST foo;
2175 int inv;
c5aa993b 2176
0fd88904 2177 foo = unpack_double (value_type (val), value_contents (val), &inv);
c906108c 2178 if (inv)
8a3fe4f8 2179 error (_("Invalid floating value found in program."));
c906108c
SS
2180 return foo;
2181}
4ef30785 2182
581e13c1 2183/* Extract a value as a C pointer. Does not deallocate the value.
4478b372
JB
2184 Note that val's type may not actually be a pointer; value_as_long
2185 handles all the cases. */
c906108c 2186CORE_ADDR
f23631e4 2187value_as_address (struct value *val)
c906108c 2188{
50810684
UW
2189 struct gdbarch *gdbarch = get_type_arch (value_type (val));
2190
c906108c
SS
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
bf6ae464 2194 /* gdbarch_addr_bits_remove is wrong if we are being called for a
c906108c
SS
2195 non-address (e.g. argument to "signal", "info break", etc.), or
2196 for pointers to char, in which the low bits *are* significant. */
50810684 2197 return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
c906108c 2198#else
f312f057
JB
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
42ae5230 2219 value_address (val) is the address of the function. This is what
f312f057
JB
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. */
df407dfe
AC
2237 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
2238 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
42ae5230 2239 return value_address (val);
f312f057 2240
994b9211 2241 val = coerce_array (val);
fc0c74b1
AC
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
79dd2d24 2272 Adding an architecture method like integer_to_address() certainly
fc0c74b1
AC
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
df407dfe
AC
2280 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
2281 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
50810684
UW
2282 && gdbarch_integer_to_address_p (gdbarch))
2283 return gdbarch_integer_to_address (gdbarch, value_type (val),
0fd88904 2284 value_contents (val));
fc0c74b1 2285
0fd88904 2286 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
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
2304LONGEST
fc1a4b47 2305unpack_long (struct type *type, const gdb_byte *valaddr)
c906108c 2306{
e17a4113 2307 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
52f0bd74
AC
2308 enum type_code code = TYPE_CODE (type);
2309 int len = TYPE_LENGTH (type);
2310 int nosign = TYPE_UNSIGNED (type);
c906108c 2311
c906108c
SS
2312 switch (code)
2313 {
2314 case TYPE_CODE_TYPEDEF:
2315 return unpack_long (check_typedef (type), valaddr);
2316 case TYPE_CODE_ENUM:
4f2aea11 2317 case TYPE_CODE_FLAGS:
c906108c
SS
2318 case TYPE_CODE_BOOL:
2319 case TYPE_CODE_INT:
2320 case TYPE_CODE_CHAR:
2321 case TYPE_CODE_RANGE:
0d5de010 2322 case TYPE_CODE_MEMBERPTR:
c906108c 2323 if (nosign)
e17a4113 2324 return extract_unsigned_integer (valaddr, len, byte_order);
c906108c 2325 else
e17a4113 2326 return extract_signed_integer (valaddr, len, byte_order);
c906108c
SS
2327
2328 case TYPE_CODE_FLT:
96d2f608 2329 return extract_typed_floating (valaddr, type);
c906108c 2330
4ef30785
TJB
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. */
e17a4113 2334 return decimal_to_doublest (valaddr, len, byte_order);
4ef30785 2335
c906108c
SS
2336 case TYPE_CODE_PTR:
2337 case TYPE_CODE_REF:
2338 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
c5aa993b 2339 whether we want this to be true eventually. */
4478b372 2340 return extract_typed_address (valaddr, type);
c906108c 2341
c906108c 2342 default:
8a3fe4f8 2343 error (_("Value can't be converted to integer."));
c906108c 2344 }
c5aa993b 2345 return 0; /* Placate lint. */
c906108c
SS
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
2354DOUBLEST
fc1a4b47 2355unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
c906108c 2356{
e17a4113 2357 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
c906108c
SS
2358 enum type_code code;
2359 int len;
2360 int nosign;
2361
581e13c1 2362 *invp = 0; /* Assume valid. */
c906108c
SS
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 {
75bc7ddf
AC
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
ea06eb3d 2377 methods gdbarch_float_format and gdbarch_double_format - these
75bc7ddf
AC
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
ac79b88b
DJ
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
96d2f608 2392 return extract_typed_floating (valaddr, type);
c906108c 2393 }
4ef30785 2394 else if (code == TYPE_CODE_DECFLOAT)
e17a4113 2395 return decimal_to_doublest (valaddr, len, byte_order);
c906108c
SS
2396 else if (nosign)
2397 {
2398 /* Unsigned -- be sure we compensate for signed LONGEST. */
c906108c 2399 return (ULONGEST) unpack_long (type, valaddr);
c906108c
SS
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
1aa20aa8 2414 references to be dereferenced, call value_as_address() instead.
c906108c
SS
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
2421CORE_ADDR
fc1a4b47 2422unpack_pointer (struct type *type, const gdb_byte *valaddr)
c906108c
SS
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}
4478b372 2428
c906108c 2429\f
1596cb5d 2430/* Get the value of the FIELDNO'th field (which must be static) of
2c2738a0 2431 TYPE. Return NULL if the field doesn't exist or has been
581e13c1 2432 optimized out. */
c906108c 2433
f23631e4 2434struct value *
fba45db2 2435value_static_field (struct type *type, int fieldno)
c906108c 2436{
948e66d9
DJ
2437 struct value *retval;
2438
1596cb5d 2439 switch (TYPE_FIELD_LOC_KIND (type, fieldno))
c906108c 2440 {
1596cb5d 2441 case FIELD_LOC_KIND_PHYSADDR:
52e9fde8
SS
2442 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2443 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1596cb5d
DE
2444 break;
2445 case FIELD_LOC_KIND_PHYSNAME:
c906108c 2446 {
ff355380 2447 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
581e13c1 2448 /* TYPE_FIELD_NAME (type, fieldno); */
2570f2b7 2449 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
94af9270 2450
948e66d9 2451 if (sym == NULL)
c906108c 2452 {
a109c7c1 2453 /* With some compilers, e.g. HP aCC, static data members are
581e13c1 2454 reported as non-debuggable symbols. */
a109c7c1
MS
2455 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name,
2456 NULL, NULL);
2457
c906108c
SS
2458 if (!msym)
2459 return NULL;
2460 else
c5aa993b 2461 {
52e9fde8
SS
2462 retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
2463 SYMBOL_VALUE_ADDRESS (msym));
c906108c
SS
2464 }
2465 }
2466 else
515ed532 2467 retval = value_of_variable (sym, NULL);
1596cb5d 2468 break;
c906108c 2469 }
1596cb5d 2470 default:
f3574227 2471 gdb_assert_not_reached ("unexpected field location kind");
1596cb5d
DE
2472 }
2473
948e66d9 2474 return retval;
c906108c
SS
2475}
2476
4dfea560
DE
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. */
2b127877 2482
4dfea560
DE
2483void
2484set_value_enclosing_type (struct value *val, struct type *new_encl_type)
2b127877 2485{
3e3d7139
JG
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;
2b127877
DB
2491}
2492
c906108c
SS
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.
581e13c1 2496 FIELDNO says which field. */
c906108c 2497
f23631e4
AC
2498struct value *
2499value_primitive_field (struct value *arg1, int offset,
aa1ee363 2500 int fieldno, struct type *arg_type)
c906108c 2501{
f23631e4 2502 struct value *v;
52f0bd74 2503 struct type *type;
c906108c
SS
2504
2505 CHECK_TYPEDEF (arg_type);
2506 type = TYPE_FIELD_TYPE (arg_type, fieldno);
c54eabfa
JK
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);
c906108c 2515
22c05d8a
JK
2516 if (value_optimized_out (arg1))
2517 v = allocate_optimized_out_value (type);
2518 else if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
c906108c 2519 {
22c05d8a
JK
2520 /* Handle packed fields.
2521
2522 Create a new value for the bitfield, with bitpos and bitsize
4ea48cc1
DJ
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. */
22c05d8a 2528
4ea48cc1
DJ
2529 int bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
2530 int container_bitsize = TYPE_LENGTH (type) * 8;
2531
2532 v = allocate_value_lazy (type);
df407dfe 2533 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
4ea48cc1
DJ
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;
38f12cfc
TT
2539 v->offset = (value_embedded_offset (arg1)
2540 + offset
2541 + (bitpos - v->bitpos) / 8);
4ea48cc1
DJ
2542 v->parent = arg1;
2543 value_incref (v->parent);
2544 if (!value_lazy (arg1))
2545 value_fetch_lazy (v);
c906108c
SS
2546 }
2547 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
2548 {
2549 /* This field is actually a base subobject, so preserve the
39d37385
PA
2550 entire object's contents for later references to virtual
2551 bases, etc. */
a4e2ee12
DJ
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))
3e3d7139 2558 v = allocate_value_lazy (value_enclosing_type (arg1));
c906108c 2559 else
3e3d7139
JG
2560 {
2561 v = allocate_value (value_enclosing_type (arg1));
39d37385
PA
2562 value_contents_copy_raw (v, 0, arg1, 0,
2563 TYPE_LENGTH (value_enclosing_type (arg1)));
3e3d7139
JG
2564 }
2565 v->type = type;
df407dfe 2566 v->offset = value_offset (arg1);
13c3b5f5
AC
2567 v->embedded_offset = (offset + value_embedded_offset (arg1)
2568 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
c906108c
SS
2569 }
2570 else
2571 {
2572 /* Plain old data member */
2573 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
a4e2ee12
DJ
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))
3e3d7139 2580 v = allocate_value_lazy (type);
c906108c 2581 else
3e3d7139
JG
2582 {
2583 v = allocate_value (type);
39d37385
PA
2584 value_contents_copy_raw (v, value_embedded_offset (v),
2585 arg1, value_embedded_offset (arg1) + offset,
2586 TYPE_LENGTH (type));
3e3d7139 2587 }
df407dfe 2588 v->offset = (value_offset (arg1) + offset
13c3b5f5 2589 + value_embedded_offset (arg1));
c906108c 2590 }
74bcbdf3 2591 set_value_component_location (v, arg1);
9ee8fc9d 2592 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
0c16dd26 2593 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
c906108c
SS
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.
581e13c1 2599 FIELDNO says which field. */
c906108c 2600
f23631e4 2601struct value *
aa1ee363 2602value_field (struct value *arg1, int fieldno)
c906108c 2603{
df407dfe 2604 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
c906108c
SS
2605}
2606
2607/* Return a non-virtual function as a value.
2608 F is the list of member functions which contains the desired method.
0478d61c
FF
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
581e13c1 2612 full symbol or a minimal symbol. */
c906108c 2613
f23631e4 2614struct value *
3e43a32a
MS
2615value_fn_field (struct value **arg1p, struct fn_field *f,
2616 int j, struct type *type,
fba45db2 2617 int offset)
c906108c 2618{
f23631e4 2619 struct value *v;
52f0bd74 2620 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1d06ead6 2621 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c906108c 2622 struct symbol *sym;
0478d61c 2623 struct minimal_symbol *msym;
c906108c 2624
2570f2b7 2625 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
5ae326fa 2626 if (sym != NULL)
0478d61c 2627 {
5ae326fa
AC
2628 msym = NULL;
2629 }
2630 else
2631 {
2632 gdb_assert (sym == NULL);
0478d61c 2633 msym = lookup_minimal_symbol (physname, NULL, NULL);
5ae326fa
AC
2634 if (msym == NULL)
2635 return NULL;
0478d61c
FF
2636 }
2637
c906108c 2638 v = allocate_value (ftype);
0478d61c
FF
2639 if (sym)
2640 {
42ae5230 2641 set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
0478d61c
FF
2642 }
2643 else
2644 {
bccdca4a
UW
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
42ae5230
TT
2650 set_value_address (v,
2651 gdbarch_convert_from_func_ptr_addr
2652 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), &current_target));
0478d61c 2653 }
c906108c
SS
2654
2655 if (arg1p)
c5aa993b 2656 {
df407dfe 2657 if (type != value_type (*arg1p))
c5aa993b
JM
2658 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
2659 value_addr (*arg1p)));
2660
070ad9f0 2661 /* Move the `this' pointer according to the offset.
581e13c1 2662 VALUE_OFFSET (*arg1p) += offset; */
c906108c
SS
2663 }
2664
2665 return v;
2666}
2667
c906108c 2668\f
c906108c 2669
5467c6c8
PA
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. */
c906108c 2674
5467c6c8
PA
2675static int
2676unpack_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)
c906108c 2680{
4ea48cc1 2681 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
c906108c
SS
2682 ULONGEST val;
2683 ULONGEST valmask;
c906108c 2684 int lsbcount;
4a76eae5 2685 int bytes_read;
5467c6c8 2686 int read_offset;
c906108c 2687
4a76eae5
DJ
2688 /* Read the minimum number of bytes required; there may not be
2689 enough bytes to read an entire ULONGEST. */
c906108c 2690 CHECK_TYPEDEF (field_type);
4a76eae5
DJ
2691 if (bitsize)
2692 bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
2693 else
2694 bytes_read = TYPE_LENGTH (field_type);
2695
5467c6c8
PA
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,
4a76eae5 2704 bytes_read, byte_order);
c906108c 2705
581e13c1 2706 /* Extract bits. See comment above. */
c906108c 2707
4ea48cc1 2708 if (gdbarch_bits_big_endian (get_type_arch (field_type)))
4a76eae5 2709 lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
c906108c
SS
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.
581e13c1 2715 If the field is signed, and is negative, then sign extend. */
c906108c
SS
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 }
5467c6c8
PA
2729
2730 *result = val;
2731 return 1;
c906108c
SS
2732}
2733
5467c6c8
PA
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.
4ea48cc1 2739
5467c6c8
PA
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
2754int
2755unpack_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
2772static int
2773unpack_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)
4ea48cc1
DJ
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
5467c6c8
PA
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
2791int
2792unpack_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
2807LONGEST
2808unpack_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
2822struct value *
2823value_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 }
4ea48cc1
DJ
2841}
2842
c906108c
SS
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
581e13c1 2846 indicate which bits (in target bit order) comprise the bitfield.
19f220c3 2847 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
f4e88c8e 2848 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
c906108c
SS
2849
2850void
50810684
UW
2851modify_field (struct type *type, gdb_byte *addr,
2852 LONGEST fieldval, int bitpos, int bitsize)
c906108c 2853{
e17a4113 2854 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
f4e88c8e
PH
2855 ULONGEST oword;
2856 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
19f220c3
JK
2857 int bytesize;
2858
2859 /* Normalize BITPOS. */
2860 addr += bitpos / 8;
2861 bitpos %= 8;
c906108c
SS
2862
2863 /* If a negative fieldval fits in the field in question, chop
2864 off the sign extension bits. */
f4e88c8e
PH
2865 if ((~fieldval & ~(mask >> 1)) == 0)
2866 fieldval &= mask;
c906108c
SS
2867
2868 /* Warn if value is too big to fit in the field in question. */
f4e88c8e 2869 if (0 != (fieldval & ~mask))
c906108c
SS
2870 {
2871 /* FIXME: would like to include fieldval in the message, but
c5aa993b 2872 we don't have a sprintf_longest. */
8a3fe4f8 2873 warning (_("Value does not fit in %d bits."), bitsize);
c906108c
SS
2874
2875 /* Truncate it, otherwise adjoining fields may be corrupted. */
f4e88c8e 2876 fieldval &= mask;
c906108c
SS
2877 }
2878
19f220c3
JK
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);
c906108c
SS
2884
2885 /* Shifting for bit field depends on endianness of the target machine. */
50810684 2886 if (gdbarch_bits_big_endian (get_type_arch (type)))
19f220c3 2887 bitpos = bytesize * 8 - bitpos - bitsize;
c906108c 2888
f4e88c8e 2889 oword &= ~(mask << bitpos);
c906108c
SS
2890 oword |= fieldval << bitpos;
2891
19f220c3 2892 store_unsigned_integer (addr, bytesize, byte_order, oword);
c906108c
SS
2893}
2894\f
14d06750 2895/* Pack NUM into BUF using a target format of TYPE. */
c906108c 2896
14d06750
DJ
2897void
2898pack_long (gdb_byte *buf, struct type *type, LONGEST num)
c906108c 2899{
e17a4113 2900 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
52f0bd74 2901 int len;
14d06750
DJ
2902
2903 type = check_typedef (type);
c906108c
SS
2904 len = TYPE_LENGTH (type);
2905
14d06750 2906 switch (TYPE_CODE (type))
c906108c 2907 {
c906108c
SS
2908 case TYPE_CODE_INT:
2909 case TYPE_CODE_CHAR:
2910 case TYPE_CODE_ENUM:
4f2aea11 2911 case TYPE_CODE_FLAGS:
c906108c
SS
2912 case TYPE_CODE_BOOL:
2913 case TYPE_CODE_RANGE:
0d5de010 2914 case TYPE_CODE_MEMBERPTR:
e17a4113 2915 store_signed_integer (buf, len, byte_order, num);
c906108c 2916 break;
c5aa993b 2917
c906108c
SS
2918 case TYPE_CODE_REF:
2919 case TYPE_CODE_PTR:
14d06750 2920 store_typed_address (buf, type, (CORE_ADDR) num);
c906108c 2921 break;
c5aa993b 2922
c906108c 2923 default:
14d06750
DJ
2924 error (_("Unexpected type (%d) encountered for integer constant."),
2925 TYPE_CODE (type));
c906108c 2926 }
14d06750
DJ
2927}
2928
2929
595939de
PM
2930/* Pack NUM into BUF using a target format of TYPE. */
2931
2932void
2933pack_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:
3e43a32a
MS
2960 error (_("Unexpected type (%d) encountered "
2961 "for unsigned integer constant."),
595939de
PM
2962 TYPE_CODE (type));
2963 }
2964}
2965
2966
14d06750
DJ
2967/* Convert C numbers into newly allocated values. */
2968
2969struct value *
2970value_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);
c906108c
SS
2975 return val;
2976}
2977
4478b372 2978
595939de
PM
2979/* Convert C unsigned numbers into newly allocated values. */
2980
2981struct value *
2982value_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
4478b372
JB
2992/* Create a value representing a pointer of type TYPE to the address
2993 ADDR. */
f23631e4 2994struct value *
4478b372
JB
2995value_from_pointer (struct type *type, CORE_ADDR addr)
2996{
f23631e4 2997 struct value *val = allocate_value (type);
a109c7c1 2998
cab0c772 2999 store_typed_address (value_contents_raw (val), check_typedef (type), addr);
4478b372
JB
3000 return val;
3001}
3002
3003
8acb6b92
TT
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
3008struct value *
3009value_from_contents_and_address (struct type *type,
3010 const gdb_byte *valaddr,
3011 CORE_ADDR address)
3012{
41e8491f 3013 struct value *v;
a109c7c1 3014
8acb6b92 3015 if (valaddr == NULL)
41e8491f 3016 v = allocate_value_lazy (type);
8acb6b92 3017 else
41e8491f
JK
3018 {
3019 v = allocate_value (type);
3020 memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
3021 }
42ae5230 3022 set_value_address (v, address);
33d502b4 3023 VALUE_LVAL (v) = lval_memory;
8acb6b92
TT
3024 return v;
3025}
3026
8a9b8146
TT
3027/* Create a value of type TYPE holding the contents CONTENTS.
3028 The new value is `not_lval'. */
3029
3030struct value *
3031value_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
f23631e4 3040struct value *
fba45db2 3041value_from_double (struct type *type, DOUBLEST num)
c906108c 3042{
f23631e4 3043 struct value *val = allocate_value (type);
c906108c 3044 struct type *base_type = check_typedef (type);
52f0bd74 3045 enum type_code code = TYPE_CODE (base_type);
c906108c
SS
3046
3047 if (code == TYPE_CODE_FLT)
3048 {
990a07ab 3049 store_typed_floating (value_contents_raw (val), base_type, num);
c906108c
SS
3050 }
3051 else
8a3fe4f8 3052 error (_("Unexpected type encountered for floating constant."));
c906108c
SS
3053
3054 return val;
3055}
994b9211 3056
27bc4d80 3057struct value *
4ef30785 3058value_from_decfloat (struct type *type, const gdb_byte *dec)
27bc4d80
TJB
3059{
3060 struct value *val = allocate_value (type);
27bc4d80 3061
4ef30785 3062 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
27bc4d80
TJB
3063 return val;
3064}
3065
3bd0f5ef
MS
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
3070struct value *
3071value_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
a471c594
JK
3119struct value *
3120coerce_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
994b9211
AC
3137struct value *
3138coerce_ref (struct value *arg)
3139{
df407dfe 3140 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
a471c594 3141 struct value *retval;
a109c7c1 3142
a471c594
JK
3143 retval = coerce_ref_if_computed (arg);
3144 if (retval)
3145 return retval;
3146
3147 if (TYPE_CODE (value_type_arg_tmp) != TYPE_CODE_REF)
3148 return arg;
3149
3150 return value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
3151 unpack_pointer (value_type (arg),
3152 value_contents (arg)));
994b9211
AC
3153}
3154
3155struct value *
3156coerce_array (struct value *arg)
3157{
f3134b88
TT
3158 struct type *type;
3159
994b9211 3160 arg = coerce_ref (arg);
f3134b88
TT
3161 type = check_typedef (value_type (arg));
3162
3163 switch (TYPE_CODE (type))
3164 {
3165 case TYPE_CODE_ARRAY:
7346b668 3166 if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
f3134b88
TT
3167 arg = value_coerce_array (arg);
3168 break;
3169 case TYPE_CODE_FUNC:
3170 arg = value_coerce_function (arg);
3171 break;
3172 }
994b9211
AC
3173 return arg;
3174}
c906108c 3175\f
c906108c 3176
48436ce6
AC
3177/* Return true if the function returning the specified type is using
3178 the convention of returning structures in memory (passing in the
82585c72 3179 address as a hidden first parameter). */
c906108c
SS
3180
3181int
d80b854b
UW
3182using_struct_return (struct gdbarch *gdbarch,
3183 struct type *func_type, struct type *value_type)
c906108c 3184{
52f0bd74 3185 enum type_code code = TYPE_CODE (value_type);
c906108c
SS
3186
3187 if (code == TYPE_CODE_ERROR)
8a3fe4f8 3188 error (_("Function return type unknown."));
c906108c 3189
667e784f
AC
3190 if (code == TYPE_CODE_VOID)
3191 /* A void return value is never in memory. See also corresponding
44e5158b 3192 code in "print_return_value". */
667e784f
AC
3193 return 0;
3194
92ad9cd9 3195 /* Probe the architecture for the return-value convention. */
d80b854b 3196 return (gdbarch_return_value (gdbarch, func_type, value_type,
92ad9cd9 3197 NULL, NULL, NULL)
31db7b6c 3198 != RETURN_VALUE_REGISTER_CONVENTION);
c906108c
SS
3199}
3200
42be36b3
CT
3201/* Set the initialized field in a value struct. */
3202
3203void
3204set_value_initialized (struct value *val, int status)
3205{
3206 val->initialized = status;
3207}
3208
3209/* Return the initialized field in a value struct. */
3210
3211int
3212value_initialized (struct value *val)
3213{
3214 return val->initialized;
3215}
3216
c906108c 3217void
fba45db2 3218_initialize_values (void)
c906108c 3219{
1a966eab
AC
3220 add_cmd ("convenience", no_class, show_convenience, _("\
3221Debugger convenience (\"$foo\") variables.\n\
c906108c 3222These variables are created when you assign them values;\n\
1a966eab
AC
3223thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
3224\n\
c906108c
SS
3225A few convenience variables are given values automatically:\n\
3226\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1a966eab 3227\"$__\" holds the contents of the last address examined with \"x\"."),
c906108c
SS
3228 &showlist);
3229
db5f229b 3230 add_cmd ("values", no_set_class, show_values, _("\
3e43a32a 3231Elements of value history around item number IDX (or last ten)."),
c906108c 3232 &showlist);
53e5f3cf
AS
3233
3234 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
3235Initialize a convenience variable if necessary.\n\
3236init-if-undefined VARIABLE = EXPRESSION\n\
3237Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
3238exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
3239VARIABLE is already initialized."));
bc3b79fd
TJB
3240
3241 add_prefix_cmd ("function", no_class, function_command, _("\
3242Placeholder command for showing help on convenience functions."),
3243 &functionlist, "function ", 0, &cmdlist);
c906108c 3244}
This page took 1.352047 seconds and 4 git commands to generate.