]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/value.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / value.c
CommitLineData
c906108c 1/* Low level packing and unpacking of values for GDB, the GNU Debugger.
1bac305b 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
0fb0cc75
JB
4 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
e17c207e 23#include "arch-utils.h"
c906108c
SS
24#include "gdb_string.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "value.h"
28#include "gdbcore.h"
c906108c
SS
29#include "command.h"
30#include "gdbcmd.h"
31#include "target.h"
32#include "language.h"
c906108c 33#include "demangle.h"
d16aafd8 34#include "doublest.h"
5ae326fa 35#include "gdb_assert.h"
36160dc4 36#include "regcache.h"
fe898f56 37#include "block.h"
27bc4d80 38#include "dfp.h"
bccdca4a 39#include "objfiles.h"
79a45b7d 40#include "valprint.h"
bc3b79fd 41#include "cli/cli-decode.h"
c906108c 42
a08702d6
TJB
43#include "python/python.h"
44
c906108c
SS
45/* Prototypes for exported functions. */
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
64static struct cmd_list_element *functionlist;
65
91294c83
AC
66struct value
67{
68 /* Type of value; either not an lval, or one of the various
69 different possible kinds of lval. */
70 enum lval_type lval;
71
72 /* Is it modifiable? Only relevant if lval != not_lval. */
73 int modifiable;
74
75 /* Location of value (if lval). */
76 union
77 {
78 /* If lval == lval_memory, this is the address in the inferior.
79 If lval == lval_register, this is the byte offset into the
80 registers structure. */
81 CORE_ADDR address;
82
83 /* Pointer to internal variable. */
84 struct internalvar *internalvar;
5f5233d4
PA
85
86 /* If lval == lval_computed, this is a set of function pointers
87 to use to access and describe the value, and a closure pointer
88 for them to use. */
89 struct
90 {
91 struct lval_funcs *funcs; /* Functions to call. */
92 void *closure; /* Closure for those functions to use. */
93 } computed;
91294c83
AC
94 } location;
95
96 /* Describes offset of a value within lval of a structure in bytes.
97 If lval == lval_memory, this is an offset to the address. If
98 lval == lval_register, this is a further offset from
99 location.address within the registers structure. Note also the
100 member embedded_offset below. */
101 int offset;
102
103 /* Only used for bitfields; number of bits contained in them. */
104 int bitsize;
105
106 /* Only used for bitfields; position of start of field. For
32c9a795
MD
107 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
108 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
91294c83
AC
109 int bitpos;
110
111 /* Frame register value is relative to. This will be described in
112 the lval enum above as "lval_register". */
113 struct frame_id frame_id;
114
115 /* Type of the value. */
116 struct type *type;
117
118 /* If a value represents a C++ object, then the `type' field gives
119 the object's compile-time type. If the object actually belongs
120 to some class derived from `type', perhaps with other base
121 classes and additional members, then `type' is just a subobject
122 of the real thing, and the full object is probably larger than
123 `type' would suggest.
124
125 If `type' is a dynamic class (i.e. one with a vtable), then GDB
126 can actually determine the object's run-time type by looking at
127 the run-time type information in the vtable. When this
128 information is available, we may elect to read in the entire
129 object, for several reasons:
130
131 - When printing the value, the user would probably rather see the
132 full object, not just the limited portion apparent from the
133 compile-time type.
134
135 - If `type' has virtual base classes, then even printing `type'
136 alone may require reaching outside the `type' portion of the
137 object to wherever the virtual base class has been stored.
138
139 When we store the entire object, `enclosing_type' is the run-time
140 type -- the complete object -- and `embedded_offset' is the
141 offset of `type' within that larger type, in bytes. The
142 value_contents() macro takes `embedded_offset' into account, so
143 most GDB code continues to see the `type' portion of the value,
144 just as the inferior would.
145
146 If `type' is a pointer to an object, then `enclosing_type' is a
147 pointer to the object's run-time type, and `pointed_to_offset' is
148 the offset in bytes from the full object to the pointed-to object
149 -- that is, the value `embedded_offset' would have if we followed
150 the pointer and fetched the complete object. (I don't really see
151 the point. Why not just determine the run-time type when you
152 indirect, and avoid the special case? The contents don't matter
153 until you indirect anyway.)
154
155 If we're not doing anything fancy, `enclosing_type' is equal to
156 `type', and `embedded_offset' is zero, so everything works
157 normally. */
158 struct type *enclosing_type;
159 int embedded_offset;
160 int pointed_to_offset;
161
162 /* Values are stored in a chain, so that they can be deleted easily
163 over calls to the inferior. Values assigned to internal
a08702d6
TJB
164 variables, put into the value history or exposed to Python are
165 taken off this list. */
91294c83
AC
166 struct value *next;
167
168 /* Register number if the value is from a register. */
169 short regnum;
170
171 /* If zero, contents of this value are in the contents field. If
9214ee5f
DJ
172 nonzero, contents are in inferior. If the lval field is lval_memory,
173 the contents are in inferior memory at location.address plus offset.
174 The lval field may also be lval_register.
91294c83
AC
175
176 WARNING: This field is used by the code which handles watchpoints
177 (see breakpoint.c) to decide whether a particular value can be
178 watched by hardware watchpoints. If the lazy flag is set for
179 some member of a value chain, it is assumed that this member of
180 the chain doesn't need to be watched as part of watching the
181 value itself. This is how GDB avoids watching the entire struct
182 or array when the user wants to watch a single struct member or
183 array element. If you ever change the way lazy flag is set and
184 reset, be sure to consider this use as well! */
185 char lazy;
186
187 /* If nonzero, this is the value of a variable which does not
188 actually exist in the program. */
189 char optimized_out;
190
42be36b3
CT
191 /* If value is a variable, is it initialized or not. */
192 int initialized;
193
3e3d7139
JG
194 /* Actual contents of the value. Target byte-order. NULL or not
195 valid if lazy is nonzero. */
196 gdb_byte *contents;
828d3400
DJ
197
198 /* The number of references to this value. When a value is created,
199 the value chain holds a reference, so REFERENCE_COUNT is 1. If
200 release_value is called, this value is removed from the chain but
201 the caller of release_value now has a reference to this value.
202 The caller must arrange for a call to value_free later. */
203 int reference_count;
91294c83
AC
204};
205
c906108c
SS
206/* Prototypes for local functions. */
207
a14ed312 208static void show_values (char *, int);
c906108c 209
a14ed312 210static void show_convenience (char *, int);
c906108c 211
c906108c
SS
212
213/* The value-history records all the values printed
214 by print commands during this session. Each chunk
215 records 60 consecutive values. The first chunk on
216 the chain records the most recent values.
217 The total number of values is in value_history_count. */
218
219#define VALUE_HISTORY_CHUNK 60
220
221struct value_history_chunk
c5aa993b
JM
222 {
223 struct value_history_chunk *next;
f23631e4 224 struct value *values[VALUE_HISTORY_CHUNK];
c5aa993b 225 };
c906108c
SS
226
227/* Chain of chunks now in use. */
228
229static struct value_history_chunk *value_history_chain;
230
231static int value_history_count; /* Abs number of last entry stored */
bc3b79fd 232
c906108c
SS
233\f
234/* List of all value objects currently allocated
235 (except for those released by calls to release_value)
236 This is so they can be freed after each command. */
237
f23631e4 238static struct value *all_values;
c906108c 239
3e3d7139
JG
240/* Allocate a lazy value for type TYPE. Its actual content is
241 "lazily" allocated too: the content field of the return value is
242 NULL; it will be allocated when it is fetched from the target. */
c906108c 243
f23631e4 244struct value *
3e3d7139 245allocate_value_lazy (struct type *type)
c906108c 246{
f23631e4 247 struct value *val;
c906108c
SS
248 struct type *atype = check_typedef (type);
249
3e3d7139
JG
250 val = (struct value *) xzalloc (sizeof (struct value));
251 val->contents = NULL;
df407dfe 252 val->next = all_values;
c906108c 253 all_values = val;
df407dfe 254 val->type = type;
4754a64e 255 val->enclosing_type = type;
c906108c 256 VALUE_LVAL (val) = not_lval;
42ae5230 257 val->location.address = 0;
1df6926e 258 VALUE_FRAME_ID (val) = null_frame_id;
df407dfe
AC
259 val->offset = 0;
260 val->bitpos = 0;
261 val->bitsize = 0;
9ee8fc9d 262 VALUE_REGNUM (val) = -1;
3e3d7139 263 val->lazy = 1;
feb13ab0 264 val->optimized_out = 0;
13c3b5f5 265 val->embedded_offset = 0;
b44d461b 266 val->pointed_to_offset = 0;
c906108c 267 val->modifiable = 1;
42be36b3 268 val->initialized = 1; /* Default to initialized. */
828d3400
DJ
269
270 /* Values start out on the all_values chain. */
271 val->reference_count = 1;
272
c906108c
SS
273 return val;
274}
275
3e3d7139
JG
276/* Allocate the contents of VAL if it has not been allocated yet. */
277
278void
279allocate_value_contents (struct value *val)
280{
281 if (!val->contents)
282 val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
283}
284
285/* Allocate a value and its contents for type TYPE. */
286
287struct value *
288allocate_value (struct type *type)
289{
290 struct value *val = allocate_value_lazy (type);
291 allocate_value_contents (val);
292 val->lazy = 0;
293 return val;
294}
295
c906108c 296/* Allocate a value that has the correct length
938f5214 297 for COUNT repetitions of type TYPE. */
c906108c 298
f23631e4 299struct value *
fba45db2 300allocate_repeat_value (struct type *type, int count)
c906108c 301{
c5aa993b 302 int low_bound = current_language->string_lower_bound; /* ??? */
c906108c
SS
303 /* FIXME-type-allocation: need a way to free this type when we are
304 done with it. */
e3506a9f
UW
305 struct type *array_type
306 = lookup_array_range_type (type, low_bound, count + low_bound - 1);
307 return allocate_value (array_type);
c906108c
SS
308}
309
a08702d6
TJB
310/* Needed if another module needs to maintain its on list of values. */
311void
312value_prepend_to_list (struct value **head, struct value *val)
313{
314 val->next = *head;
315 *head = val;
316}
317
318/* Needed if another module needs to maintain its on list of values. */
319void
320value_remove_from_list (struct value **head, struct value *val)
321{
322 struct value *prev;
323
324 if (*head == val)
325 *head = (*head)->next;
326 else
327 for (prev = *head; prev->next; prev = prev->next)
328 if (prev->next == val)
329 {
330 prev->next = val->next;
331 break;
332 }
333}
334
5f5233d4
PA
335struct value *
336allocate_computed_value (struct type *type,
337 struct lval_funcs *funcs,
338 void *closure)
339{
340 struct value *v = allocate_value (type);
341 VALUE_LVAL (v) = lval_computed;
342 v->location.computed.funcs = funcs;
343 v->location.computed.closure = closure;
344 set_value_lazy (v, 1);
345
346 return v;
347}
348
df407dfe
AC
349/* Accessor methods. */
350
17cf0ecd
AC
351struct value *
352value_next (struct value *value)
353{
354 return value->next;
355}
356
df407dfe
AC
357struct type *
358value_type (struct value *value)
359{
360 return value->type;
361}
04624583
AC
362void
363deprecated_set_value_type (struct value *value, struct type *type)
364{
365 value->type = type;
366}
df407dfe
AC
367
368int
369value_offset (struct value *value)
370{
371 return value->offset;
372}
f5cf64a7
AC
373void
374set_value_offset (struct value *value, int offset)
375{
376 value->offset = offset;
377}
df407dfe
AC
378
379int
380value_bitpos (struct value *value)
381{
382 return value->bitpos;
383}
9bbda503
AC
384void
385set_value_bitpos (struct value *value, int bit)
386{
387 value->bitpos = bit;
388}
df407dfe
AC
389
390int
391value_bitsize (struct value *value)
392{
393 return value->bitsize;
394}
9bbda503
AC
395void
396set_value_bitsize (struct value *value, int bit)
397{
398 value->bitsize = bit;
399}
df407dfe 400
fc1a4b47 401gdb_byte *
990a07ab
AC
402value_contents_raw (struct value *value)
403{
3e3d7139
JG
404 allocate_value_contents (value);
405 return value->contents + value->embedded_offset;
990a07ab
AC
406}
407
fc1a4b47 408gdb_byte *
990a07ab
AC
409value_contents_all_raw (struct value *value)
410{
3e3d7139
JG
411 allocate_value_contents (value);
412 return value->contents;
990a07ab
AC
413}
414
4754a64e
AC
415struct type *
416value_enclosing_type (struct value *value)
417{
418 return value->enclosing_type;
419}
420
fc1a4b47 421const gdb_byte *
46615f07
AC
422value_contents_all (struct value *value)
423{
424 if (value->lazy)
425 value_fetch_lazy (value);
3e3d7139 426 return value->contents;
46615f07
AC
427}
428
d69fe07e
AC
429int
430value_lazy (struct value *value)
431{
432 return value->lazy;
433}
434
dfa52d88
AC
435void
436set_value_lazy (struct value *value, int val)
437{
438 value->lazy = val;
439}
440
fc1a4b47 441const gdb_byte *
0fd88904
AC
442value_contents (struct value *value)
443{
444 return value_contents_writeable (value);
445}
446
fc1a4b47 447gdb_byte *
0fd88904
AC
448value_contents_writeable (struct value *value)
449{
450 if (value->lazy)
451 value_fetch_lazy (value);
fc0c53a0 452 return value_contents_raw (value);
0fd88904
AC
453}
454
a6c442d8
MK
455/* Return non-zero if VAL1 and VAL2 have the same contents. Note that
456 this function is different from value_equal; in C the operator ==
457 can return 0 even if the two values being compared are equal. */
458
459int
460value_contents_equal (struct value *val1, struct value *val2)
461{
462 struct type *type1;
463 struct type *type2;
464 int len;
465
466 type1 = check_typedef (value_type (val1));
467 type2 = check_typedef (value_type (val2));
468 len = TYPE_LENGTH (type1);
469 if (len != TYPE_LENGTH (type2))
470 return 0;
471
472 return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
473}
474
feb13ab0
AC
475int
476value_optimized_out (struct value *value)
477{
478 return value->optimized_out;
479}
480
481void
482set_value_optimized_out (struct value *value, int val)
483{
484 value->optimized_out = val;
485}
13c3b5f5
AC
486
487int
488value_embedded_offset (struct value *value)
489{
490 return value->embedded_offset;
491}
492
493void
494set_value_embedded_offset (struct value *value, int val)
495{
496 value->embedded_offset = val;
497}
b44d461b
AC
498
499int
500value_pointed_to_offset (struct value *value)
501{
502 return value->pointed_to_offset;
503}
504
505void
506set_value_pointed_to_offset (struct value *value, int val)
507{
508 value->pointed_to_offset = val;
509}
13bb5560 510
5f5233d4
PA
511struct lval_funcs *
512value_computed_funcs (struct value *v)
513{
514 gdb_assert (VALUE_LVAL (v) == lval_computed);
515
516 return v->location.computed.funcs;
517}
518
519void *
520value_computed_closure (struct value *v)
521{
522 gdb_assert (VALUE_LVAL (v) == lval_computed);
523
524 return v->location.computed.closure;
525}
526
13bb5560
AC
527enum lval_type *
528deprecated_value_lval_hack (struct value *value)
529{
530 return &value->lval;
531}
532
42ae5230
TT
533CORE_ADDR
534value_address (struct value *value)
535{
536 if (value->lval == lval_internalvar
537 || value->lval == lval_internalvar_component)
538 return 0;
539 return value->location.address + value->offset;
540}
541
542CORE_ADDR
543value_raw_address (struct value *value)
544{
545 if (value->lval == lval_internalvar
546 || value->lval == lval_internalvar_component)
547 return 0;
548 return value->location.address;
549}
550
551void
552set_value_address (struct value *value, CORE_ADDR addr)
13bb5560 553{
42ae5230
TT
554 gdb_assert (value->lval != lval_internalvar
555 && value->lval != lval_internalvar_component);
556 value->location.address = addr;
13bb5560
AC
557}
558
559struct internalvar **
560deprecated_value_internalvar_hack (struct value *value)
561{
562 return &value->location.internalvar;
563}
564
565struct frame_id *
566deprecated_value_frame_id_hack (struct value *value)
567{
568 return &value->frame_id;
569}
570
571short *
572deprecated_value_regnum_hack (struct value *value)
573{
574 return &value->regnum;
575}
88e3b34b
AC
576
577int
578deprecated_value_modifiable (struct value *value)
579{
580 return value->modifiable;
581}
582void
583deprecated_set_value_modifiable (struct value *value, int modifiable)
584{
585 value->modifiable = modifiable;
586}
990a07ab 587\f
c906108c
SS
588/* Return a mark in the value chain. All values allocated after the
589 mark is obtained (except for those released) are subject to being freed
590 if a subsequent value_free_to_mark is passed the mark. */
f23631e4 591struct value *
fba45db2 592value_mark (void)
c906108c
SS
593{
594 return all_values;
595}
596
828d3400
DJ
597/* Take a reference to VAL. VAL will not be deallocated until all
598 references are released. */
599
600void
601value_incref (struct value *val)
602{
603 val->reference_count++;
604}
605
606/* Release a reference to VAL, which was acquired with value_incref.
607 This function is also called to deallocate values from the value
608 chain. */
609
3e3d7139
JG
610void
611value_free (struct value *val)
612{
613 if (val)
5f5233d4 614 {
828d3400
DJ
615 gdb_assert (val->reference_count > 0);
616 val->reference_count--;
617 if (val->reference_count > 0)
618 return;
619
5f5233d4
PA
620 if (VALUE_LVAL (val) == lval_computed)
621 {
622 struct lval_funcs *funcs = val->location.computed.funcs;
623
624 if (funcs->free_closure)
625 funcs->free_closure (val);
626 }
627
628 xfree (val->contents);
629 }
3e3d7139
JG
630 xfree (val);
631}
632
c906108c
SS
633/* Free all values allocated since MARK was obtained by value_mark
634 (except for those released). */
635void
f23631e4 636value_free_to_mark (struct value *mark)
c906108c 637{
f23631e4
AC
638 struct value *val;
639 struct value *next;
c906108c
SS
640
641 for (val = all_values; val && val != mark; val = next)
642 {
df407dfe 643 next = val->next;
c906108c
SS
644 value_free (val);
645 }
646 all_values = val;
647}
648
649/* Free all the values that have been allocated (except for those released).
650 Called after each command, successful or not. */
651
652void
fba45db2 653free_all_values (void)
c906108c 654{
f23631e4
AC
655 struct value *val;
656 struct value *next;
c906108c
SS
657
658 for (val = all_values; val; val = next)
659 {
df407dfe 660 next = val->next;
c906108c
SS
661 value_free (val);
662 }
663
664 all_values = 0;
665}
666
667/* Remove VAL from the chain all_values
668 so it will not be freed automatically. */
669
670void
f23631e4 671release_value (struct value *val)
c906108c 672{
f23631e4 673 struct value *v;
c906108c
SS
674
675 if (all_values == val)
676 {
677 all_values = val->next;
678 return;
679 }
680
681 for (v = all_values; v; v = v->next)
682 {
683 if (v->next == val)
684 {
685 v->next = val->next;
686 break;
687 }
688 }
689}
690
691/* Release all values up to mark */
f23631e4
AC
692struct value *
693value_release_to_mark (struct value *mark)
c906108c 694{
f23631e4
AC
695 struct value *val;
696 struct value *next;
c906108c 697
df407dfe
AC
698 for (val = next = all_values; next; next = next->next)
699 if (next->next == mark)
c906108c 700 {
df407dfe
AC
701 all_values = next->next;
702 next->next = NULL;
c906108c
SS
703 return val;
704 }
705 all_values = 0;
706 return val;
707}
708
709/* Return a copy of the value ARG.
710 It contains the same contents, for same memory address,
711 but it's a different block of storage. */
712
f23631e4
AC
713struct value *
714value_copy (struct value *arg)
c906108c 715{
4754a64e 716 struct type *encl_type = value_enclosing_type (arg);
3e3d7139
JG
717 struct value *val;
718
719 if (value_lazy (arg))
720 val = allocate_value_lazy (encl_type);
721 else
722 val = allocate_value (encl_type);
df407dfe 723 val->type = arg->type;
c906108c 724 VALUE_LVAL (val) = VALUE_LVAL (arg);
6f7c8fc2 725 val->location = arg->location;
df407dfe
AC
726 val->offset = arg->offset;
727 val->bitpos = arg->bitpos;
728 val->bitsize = arg->bitsize;
1df6926e 729 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
9ee8fc9d 730 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
d69fe07e 731 val->lazy = arg->lazy;
feb13ab0 732 val->optimized_out = arg->optimized_out;
13c3b5f5 733 val->embedded_offset = value_embedded_offset (arg);
b44d461b 734 val->pointed_to_offset = arg->pointed_to_offset;
c906108c 735 val->modifiable = arg->modifiable;
d69fe07e 736 if (!value_lazy (val))
c906108c 737 {
990a07ab 738 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
4754a64e 739 TYPE_LENGTH (value_enclosing_type (arg)));
c906108c
SS
740
741 }
5f5233d4
PA
742 if (VALUE_LVAL (val) == lval_computed)
743 {
744 struct lval_funcs *funcs = val->location.computed.funcs;
745
746 if (funcs->copy_closure)
747 val->location.computed.closure = funcs->copy_closure (val);
748 }
c906108c
SS
749 return val;
750}
74bcbdf3
PA
751
752void
753set_value_component_location (struct value *component, struct value *whole)
754{
755 if (VALUE_LVAL (whole) == lval_internalvar)
756 VALUE_LVAL (component) = lval_internalvar_component;
757 else
758 VALUE_LVAL (component) = VALUE_LVAL (whole);
5f5233d4 759
74bcbdf3 760 component->location = whole->location;
5f5233d4
PA
761 if (VALUE_LVAL (whole) == lval_computed)
762 {
763 struct lval_funcs *funcs = whole->location.computed.funcs;
764
765 if (funcs->copy_closure)
766 component->location.computed.closure = funcs->copy_closure (whole);
767 }
74bcbdf3
PA
768}
769
c906108c
SS
770\f
771/* Access to the value history. */
772
773/* Record a new value in the value history.
774 Returns the absolute history index of the entry.
775 Result of -1 indicates the value was not saved; otherwise it is the
776 value history index of this new item. */
777
778int
f23631e4 779record_latest_value (struct value *val)
c906108c
SS
780{
781 int i;
782
783 /* We don't want this value to have anything to do with the inferior anymore.
784 In particular, "set $1 = 50" should not affect the variable from which
785 the value was taken, and fast watchpoints should be able to assume that
786 a value on the value history never changes. */
d69fe07e 787 if (value_lazy (val))
c906108c
SS
788 value_fetch_lazy (val);
789 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
790 from. This is a bit dubious, because then *&$1 does not just return $1
791 but the current contents of that location. c'est la vie... */
792 val->modifiable = 0;
793 release_value (val);
794
795 /* Here we treat value_history_count as origin-zero
796 and applying to the value being stored now. */
797
798 i = value_history_count % VALUE_HISTORY_CHUNK;
799 if (i == 0)
800 {
f23631e4 801 struct value_history_chunk *new
c5aa993b
JM
802 = (struct value_history_chunk *)
803 xmalloc (sizeof (struct value_history_chunk));
c906108c
SS
804 memset (new->values, 0, sizeof new->values);
805 new->next = value_history_chain;
806 value_history_chain = new;
807 }
808
809 value_history_chain->values[i] = val;
810
811 /* Now we regard value_history_count as origin-one
812 and applying to the value just stored. */
813
814 return ++value_history_count;
815}
816
817/* Return a copy of the value in the history with sequence number NUM. */
818
f23631e4 819struct value *
fba45db2 820access_value_history (int num)
c906108c 821{
f23631e4 822 struct value_history_chunk *chunk;
52f0bd74
AC
823 int i;
824 int absnum = num;
c906108c
SS
825
826 if (absnum <= 0)
827 absnum += value_history_count;
828
829 if (absnum <= 0)
830 {
831 if (num == 0)
8a3fe4f8 832 error (_("The history is empty."));
c906108c 833 else if (num == 1)
8a3fe4f8 834 error (_("There is only one value in the history."));
c906108c 835 else
8a3fe4f8 836 error (_("History does not go back to $$%d."), -num);
c906108c
SS
837 }
838 if (absnum > value_history_count)
8a3fe4f8 839 error (_("History has not yet reached $%d."), absnum);
c906108c
SS
840
841 absnum--;
842
843 /* Now absnum is always absolute and origin zero. */
844
845 chunk = value_history_chain;
846 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
847 i > 0; i--)
848 chunk = chunk->next;
849
850 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
851}
852
c906108c 853static void
fba45db2 854show_values (char *num_exp, int from_tty)
c906108c 855{
52f0bd74 856 int i;
f23631e4 857 struct value *val;
c906108c
SS
858 static int num = 1;
859
860 if (num_exp)
861 {
f132ba9d
TJB
862 /* "show values +" should print from the stored position.
863 "show values <exp>" should print around value number <exp>. */
c906108c 864 if (num_exp[0] != '+' || num_exp[1] != '\0')
bb518678 865 num = parse_and_eval_long (num_exp) - 5;
c906108c
SS
866 }
867 else
868 {
f132ba9d 869 /* "show values" means print the last 10 values. */
c906108c
SS
870 num = value_history_count - 9;
871 }
872
873 if (num <= 0)
874 num = 1;
875
876 for (i = num; i < num + 10 && i <= value_history_count; i++)
877 {
79a45b7d 878 struct value_print_options opts;
c906108c 879 val = access_value_history (i);
a3f17187 880 printf_filtered (("$%d = "), i);
79a45b7d
TT
881 get_user_print_options (&opts);
882 value_print (val, gdb_stdout, &opts);
a3f17187 883 printf_filtered (("\n"));
c906108c
SS
884 }
885
f132ba9d 886 /* The next "show values +" should start after what we just printed. */
c906108c
SS
887 num += 10;
888
889 /* Hitting just return after this command should do the same thing as
f132ba9d
TJB
890 "show values +". If num_exp is null, this is unnecessary, since
891 "show values +" is not useful after "show values". */
c906108c
SS
892 if (from_tty && num_exp)
893 {
894 num_exp[0] = '+';
895 num_exp[1] = '\0';
896 }
897}
898\f
899/* Internal variables. These are variables within the debugger
900 that hold values assigned by debugger commands.
901 The user refers to them with a '$' prefix
902 that does not appear in the variable names stored internally. */
903
4fa62494
UW
904struct internalvar
905{
906 struct internalvar *next;
907 char *name;
4fa62494 908
78267919
UW
909 /* We support various different kinds of content of an internal variable.
910 enum internalvar_kind specifies the kind, and union internalvar_data
911 provides the data associated with this particular kind. */
912
913 enum internalvar_kind
914 {
915 /* The internal variable is empty. */
916 INTERNALVAR_VOID,
917
918 /* The value of the internal variable is provided directly as
919 a GDB value object. */
920 INTERNALVAR_VALUE,
921
922 /* A fresh value is computed via a call-back routine on every
923 access to the internal variable. */
924 INTERNALVAR_MAKE_VALUE,
4fa62494 925
78267919
UW
926 /* The internal variable holds a GDB internal convenience function. */
927 INTERNALVAR_FUNCTION,
928
929 /* The variable holds a simple scalar value. */
930 INTERNALVAR_SCALAR,
931
932 /* The variable holds a GDB-provided string. */
933 INTERNALVAR_STRING,
934
935 } kind;
4fa62494 936
4fa62494
UW
937 union internalvar_data
938 {
78267919
UW
939 /* A value object used with INTERNALVAR_VALUE. */
940 struct value *value;
941
942 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
943 internalvar_make_value make_value;
944
945 /* The internal function used with INTERNALVAR_FUNCTION. */
946 struct
947 {
948 struct internal_function *function;
949 /* True if this is the canonical name for the function. */
950 int canonical;
951 } fn;
952
953 /* A scalar value used with INTERNALVAR_SCALAR. */
954 struct
955 {
956 /* If type is non-NULL, it will be used as the type to generate
957 a value for this internal variable. If type is NULL, a default
958 integer type for the architecture is used. */
959 struct type *type;
960 union
961 {
962 LONGEST l; /* Used with TYPE_CODE_INT and NULL types. */
963 CORE_ADDR a; /* Used with TYPE_CODE_PTR types. */
964 } val;
965 } scalar;
966
967 /* A string value used with INTERNALVAR_STRING. */
968 char *string;
4fa62494
UW
969 } u;
970};
971
c906108c
SS
972static struct internalvar *internalvars;
973
53e5f3cf
AS
974/* If the variable does not already exist create it and give it the value given.
975 If no value is given then the default is zero. */
976static void
977init_if_undefined_command (char* args, int from_tty)
978{
979 struct internalvar* intvar;
980
981 /* Parse the expression - this is taken from set_command(). */
982 struct expression *expr = parse_expression (args);
983 register struct cleanup *old_chain =
984 make_cleanup (free_current_contents, &expr);
985
986 /* Validate the expression.
987 Was the expression an assignment?
988 Or even an expression at all? */
989 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
990 error (_("Init-if-undefined requires an assignment expression."));
991
992 /* Extract the variable from the parsed expression.
993 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
994 if (expr->elts[1].opcode != OP_INTERNALVAR)
995 error (_("The first parameter to init-if-undefined should be a GDB variable."));
996 intvar = expr->elts[2].internalvar;
997
998 /* Only evaluate the expression if the lvalue is void.
999 This may still fail if the expresssion is invalid. */
78267919 1000 if (intvar->kind == INTERNALVAR_VOID)
53e5f3cf
AS
1001 evaluate_expression (expr);
1002
1003 do_cleanups (old_chain);
1004}
1005
1006
c906108c
SS
1007/* Look up an internal variable with name NAME. NAME should not
1008 normally include a dollar sign.
1009
1010 If the specified internal variable does not exist,
c4a3d09a 1011 the return value is NULL. */
c906108c
SS
1012
1013struct internalvar *
bc3b79fd 1014lookup_only_internalvar (const char *name)
c906108c 1015{
52f0bd74 1016 struct internalvar *var;
c906108c
SS
1017
1018 for (var = internalvars; var; var = var->next)
5cb316ef 1019 if (strcmp (var->name, name) == 0)
c906108c
SS
1020 return var;
1021
c4a3d09a
MF
1022 return NULL;
1023}
1024
1025
1026/* Create an internal variable with name NAME and with a void value.
1027 NAME should not normally include a dollar sign. */
1028
1029struct internalvar *
bc3b79fd 1030create_internalvar (const char *name)
c4a3d09a
MF
1031{
1032 struct internalvar *var;
c906108c 1033 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
1754f103 1034 var->name = concat (name, (char *)NULL);
78267919 1035 var->kind = INTERNALVAR_VOID;
c906108c
SS
1036 var->next = internalvars;
1037 internalvars = var;
1038 return var;
1039}
1040
4aa995e1
PA
1041/* Create an internal variable with name NAME and register FUN as the
1042 function that value_of_internalvar uses to create a value whenever
1043 this variable is referenced. NAME should not normally include a
1044 dollar sign. */
1045
1046struct internalvar *
1047create_internalvar_type_lazy (char *name, internalvar_make_value fun)
1048{
4fa62494 1049 struct internalvar *var = create_internalvar (name);
78267919
UW
1050 var->kind = INTERNALVAR_MAKE_VALUE;
1051 var->u.make_value = fun;
4aa995e1
PA
1052 return var;
1053}
c4a3d09a
MF
1054
1055/* Look up an internal variable with name NAME. NAME should not
1056 normally include a dollar sign.
1057
1058 If the specified internal variable does not exist,
1059 one is created, with a void value. */
1060
1061struct internalvar *
bc3b79fd 1062lookup_internalvar (const char *name)
c4a3d09a
MF
1063{
1064 struct internalvar *var;
1065
1066 var = lookup_only_internalvar (name);
1067 if (var)
1068 return var;
1069
1070 return create_internalvar (name);
1071}
1072
78267919
UW
1073/* Return current value of internal variable VAR. For variables that
1074 are not inherently typed, use a value type appropriate for GDBARCH. */
1075
f23631e4 1076struct value *
78267919 1077value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
c906108c 1078{
f23631e4 1079 struct value *val;
c906108c 1080
78267919 1081 switch (var->kind)
5f5233d4 1082 {
78267919
UW
1083 case INTERNALVAR_VOID:
1084 val = allocate_value (builtin_type (gdbarch)->builtin_void);
1085 break;
4fa62494 1086
78267919
UW
1087 case INTERNALVAR_FUNCTION:
1088 val = allocate_value (builtin_type (gdbarch)->internal_fn);
1089 break;
4fa62494 1090
78267919
UW
1091 case INTERNALVAR_SCALAR:
1092 if (!var->u.scalar.type)
1093 val = value_from_longest (builtin_type (gdbarch)->builtin_int,
1094 var->u.scalar.val.l);
1095 else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT)
1096 val = value_from_longest (var->u.scalar.type, var->u.scalar.val.l);
1097 else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_PTR)
1098 val = value_from_pointer (var->u.scalar.type, var->u.scalar.val.a);
1099 else
1100 internal_error (__FILE__, __LINE__, "bad type");
1101 break;
4fa62494 1102
78267919
UW
1103 case INTERNALVAR_STRING:
1104 val = value_cstring (var->u.string, strlen (var->u.string),
1105 builtin_type (gdbarch)->builtin_char);
1106 break;
4fa62494 1107
78267919
UW
1108 case INTERNALVAR_VALUE:
1109 val = value_copy (var->u.value);
4aa995e1
PA
1110 if (value_lazy (val))
1111 value_fetch_lazy (val);
78267919 1112 break;
4aa995e1 1113
78267919
UW
1114 case INTERNALVAR_MAKE_VALUE:
1115 val = (*var->u.make_value) (gdbarch, var);
1116 break;
1117
1118 default:
1119 internal_error (__FILE__, __LINE__, "bad kind");
1120 }
1121
1122 /* Change the VALUE_LVAL to lval_internalvar so that future operations
1123 on this value go back to affect the original internal variable.
1124
1125 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
1126 no underlying modifyable state in the internal variable.
1127
1128 Likewise, if the variable's value is a computed lvalue, we want
1129 references to it to produce another computed lvalue, where
1130 references and assignments actually operate through the
1131 computed value's functions.
1132
1133 This means that internal variables with computed values
1134 behave a little differently from other internal variables:
1135 assignments to them don't just replace the previous value
1136 altogether. At the moment, this seems like the behavior we
1137 want. */
1138
1139 if (var->kind != INTERNALVAR_MAKE_VALUE
1140 && val->lval != lval_computed)
1141 {
1142 VALUE_LVAL (val) = lval_internalvar;
1143 VALUE_INTERNALVAR (val) = var;
5f5233d4 1144 }
d3c139e9 1145
4fa62494
UW
1146 return val;
1147}
d3c139e9 1148
4fa62494
UW
1149int
1150get_internalvar_integer (struct internalvar *var, LONGEST *result)
1151{
78267919 1152 switch (var->kind)
4fa62494 1153 {
78267919
UW
1154 case INTERNALVAR_SCALAR:
1155 if (var->u.scalar.type == NULL
1156 || TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT)
1157 {
1158 *result = var->u.scalar.val.l;
1159 return 1;
1160 }
1161 /* Fall through. */
d3c139e9 1162
4fa62494
UW
1163 default:
1164 return 0;
1165 }
1166}
d3c139e9 1167
4fa62494
UW
1168static int
1169get_internalvar_function (struct internalvar *var,
1170 struct internal_function **result)
1171{
78267919 1172 switch (var->kind)
d3c139e9 1173 {
78267919
UW
1174 case INTERNALVAR_FUNCTION:
1175 *result = var->u.fn.function;
4fa62494 1176 return 1;
d3c139e9 1177
4fa62494
UW
1178 default:
1179 return 0;
1180 }
c906108c
SS
1181}
1182
1183void
fba45db2 1184set_internalvar_component (struct internalvar *var, int offset, int bitpos,
f23631e4 1185 int bitsize, struct value *newval)
c906108c 1186{
4fa62494 1187 gdb_byte *addr;
c906108c 1188
78267919 1189 switch (var->kind)
4fa62494 1190 {
78267919
UW
1191 case INTERNALVAR_VALUE:
1192 addr = value_contents_writeable (var->u.value);
4fa62494
UW
1193
1194 if (bitsize)
50810684 1195 modify_field (value_type (var->u.value), addr + offset,
4fa62494
UW
1196 value_as_long (newval), bitpos, bitsize);
1197 else
1198 memcpy (addr + offset, value_contents (newval),
1199 TYPE_LENGTH (value_type (newval)));
1200 break;
78267919
UW
1201
1202 default:
1203 /* We can never get a component of any other kind. */
1204 internal_error (__FILE__, __LINE__, "set_internalvar_component");
4fa62494 1205 }
c906108c
SS
1206}
1207
1208void
f23631e4 1209set_internalvar (struct internalvar *var, struct value *val)
c906108c 1210{
78267919 1211 enum internalvar_kind new_kind;
4fa62494 1212 union internalvar_data new_data = { 0 };
c906108c 1213
78267919 1214 if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
bc3b79fd
TJB
1215 error (_("Cannot overwrite convenience function %s"), var->name);
1216
4fa62494 1217 /* Prepare new contents. */
78267919 1218 switch (TYPE_CODE (check_typedef (value_type (val))))
4fa62494
UW
1219 {
1220 case TYPE_CODE_VOID:
78267919 1221 new_kind = INTERNALVAR_VOID;
4fa62494
UW
1222 break;
1223
1224 case TYPE_CODE_INTERNAL_FUNCTION:
1225 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
78267919
UW
1226 new_kind = INTERNALVAR_FUNCTION;
1227 get_internalvar_function (VALUE_INTERNALVAR (val),
1228 &new_data.fn.function);
1229 /* Copies created here are never canonical. */
4fa62494
UW
1230 break;
1231
1232 case TYPE_CODE_INT:
78267919
UW
1233 new_kind = INTERNALVAR_SCALAR;
1234 new_data.scalar.type = value_type (val);
1235 new_data.scalar.val.l = value_as_long (val);
4fa62494
UW
1236 break;
1237
1238 case TYPE_CODE_PTR:
78267919
UW
1239 new_kind = INTERNALVAR_SCALAR;
1240 new_data.scalar.type = value_type (val);
1241 new_data.scalar.val.a = value_as_address (val);
4fa62494
UW
1242 break;
1243
1244 default:
78267919
UW
1245 new_kind = INTERNALVAR_VALUE;
1246 new_data.value = value_copy (val);
1247 new_data.value->modifiable = 1;
4fa62494
UW
1248
1249 /* Force the value to be fetched from the target now, to avoid problems
1250 later when this internalvar is referenced and the target is gone or
1251 has changed. */
78267919
UW
1252 if (value_lazy (new_data.value))
1253 value_fetch_lazy (new_data.value);
4fa62494
UW
1254
1255 /* Release the value from the value chain to prevent it from being
1256 deleted by free_all_values. From here on this function should not
1257 call error () until new_data is installed into the var->u to avoid
1258 leaking memory. */
78267919 1259 release_value (new_data.value);
4fa62494
UW
1260 break;
1261 }
1262
1263 /* Clean up old contents. */
1264 clear_internalvar (var);
1265
1266 /* Switch over. */
78267919 1267 var->kind = new_kind;
4fa62494 1268 var->u = new_data;
c906108c
SS
1269 /* End code which must not call error(). */
1270}
1271
4fa62494
UW
1272void
1273set_internalvar_integer (struct internalvar *var, LONGEST l)
1274{
1275 /* Clean up old contents. */
1276 clear_internalvar (var);
1277
78267919
UW
1278 var->kind = INTERNALVAR_SCALAR;
1279 var->u.scalar.type = NULL;
1280 var->u.scalar.val.l = l;
1281}
1282
1283void
1284set_internalvar_string (struct internalvar *var, const char *string)
1285{
1286 /* Clean up old contents. */
1287 clear_internalvar (var);
1288
1289 var->kind = INTERNALVAR_STRING;
1290 var->u.string = xstrdup (string);
4fa62494
UW
1291}
1292
1293static void
1294set_internalvar_function (struct internalvar *var, struct internal_function *f)
1295{
1296 /* Clean up old contents. */
1297 clear_internalvar (var);
1298
78267919
UW
1299 var->kind = INTERNALVAR_FUNCTION;
1300 var->u.fn.function = f;
1301 var->u.fn.canonical = 1;
1302 /* Variables installed here are always the canonical version. */
4fa62494
UW
1303}
1304
1305void
1306clear_internalvar (struct internalvar *var)
1307{
1308 /* Clean up old contents. */
78267919 1309 switch (var->kind)
4fa62494 1310 {
78267919
UW
1311 case INTERNALVAR_VALUE:
1312 value_free (var->u.value);
1313 break;
1314
1315 case INTERNALVAR_STRING:
1316 xfree (var->u.string);
4fa62494
UW
1317 break;
1318
1319 default:
4fa62494
UW
1320 break;
1321 }
1322
78267919
UW
1323 /* Reset to void kind. */
1324 var->kind = INTERNALVAR_VOID;
4fa62494
UW
1325}
1326
c906108c 1327char *
fba45db2 1328internalvar_name (struct internalvar *var)
c906108c
SS
1329{
1330 return var->name;
1331}
1332
4fa62494
UW
1333static struct internal_function *
1334create_internal_function (const char *name,
1335 internal_function_fn handler, void *cookie)
bc3b79fd 1336{
bc3b79fd
TJB
1337 struct internal_function *ifn = XNEW (struct internal_function);
1338 ifn->name = xstrdup (name);
1339 ifn->handler = handler;
1340 ifn->cookie = cookie;
4fa62494 1341 return ifn;
bc3b79fd
TJB
1342}
1343
1344char *
1345value_internal_function_name (struct value *val)
1346{
4fa62494
UW
1347 struct internal_function *ifn;
1348 int result;
1349
1350 gdb_assert (VALUE_LVAL (val) == lval_internalvar);
1351 result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
1352 gdb_assert (result);
1353
bc3b79fd
TJB
1354 return ifn->name;
1355}
1356
1357struct value *
d452c4bc
UW
1358call_internal_function (struct gdbarch *gdbarch,
1359 const struct language_defn *language,
1360 struct value *func, int argc, struct value **argv)
bc3b79fd 1361{
4fa62494
UW
1362 struct internal_function *ifn;
1363 int result;
1364
1365 gdb_assert (VALUE_LVAL (func) == lval_internalvar);
1366 result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
1367 gdb_assert (result);
1368
d452c4bc 1369 return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
bc3b79fd
TJB
1370}
1371
1372/* The 'function' command. This does nothing -- it is just a
1373 placeholder to let "help function NAME" work. This is also used as
1374 the implementation of the sub-command that is created when
1375 registering an internal function. */
1376static void
1377function_command (char *command, int from_tty)
1378{
1379 /* Do nothing. */
1380}
1381
1382/* Clean up if an internal function's command is destroyed. */
1383static void
1384function_destroyer (struct cmd_list_element *self, void *ignore)
1385{
1386 xfree (self->name);
1387 xfree (self->doc);
1388}
1389
1390/* Add a new internal function. NAME is the name of the function; DOC
1391 is a documentation string describing the function. HANDLER is
1392 called when the function is invoked. COOKIE is an arbitrary
1393 pointer which is passed to HANDLER and is intended for "user
1394 data". */
1395void
1396add_internal_function (const char *name, const char *doc,
1397 internal_function_fn handler, void *cookie)
1398{
1399 struct cmd_list_element *cmd;
4fa62494 1400 struct internal_function *ifn;
bc3b79fd 1401 struct internalvar *var = lookup_internalvar (name);
4fa62494
UW
1402
1403 ifn = create_internal_function (name, handler, cookie);
1404 set_internalvar_function (var, ifn);
bc3b79fd
TJB
1405
1406 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
1407 &functionlist);
1408 cmd->destroyer = function_destroyer;
1409}
1410
ae5a43e0
DJ
1411/* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
1412 prevent cycles / duplicates. */
1413
1414static void
1415preserve_one_value (struct value *value, struct objfile *objfile,
1416 htab_t copied_types)
1417{
1418 if (TYPE_OBJFILE (value->type) == objfile)
1419 value->type = copy_type_recursive (objfile, value->type, copied_types);
1420
1421 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
1422 value->enclosing_type = copy_type_recursive (objfile,
1423 value->enclosing_type,
1424 copied_types);
1425}
1426
78267919
UW
1427/* Likewise for internal variable VAR. */
1428
1429static void
1430preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
1431 htab_t copied_types)
1432{
1433 switch (var->kind)
1434 {
1435 case INTERNALVAR_SCALAR:
1436 if (var->u.scalar.type && TYPE_OBJFILE (var->u.scalar.type) == objfile)
1437 var->u.scalar.type
1438 = copy_type_recursive (objfile, var->u.scalar.type, copied_types);
1439 break;
1440
1441 case INTERNALVAR_VALUE:
1442 preserve_one_value (var->u.value, objfile, copied_types);
1443 break;
1444 }
1445}
1446
ae5a43e0
DJ
1447/* Update the internal variables and value history when OBJFILE is
1448 discarded; we must copy the types out of the objfile. New global types
1449 will be created for every convenience variable which currently points to
1450 this objfile's types, and the convenience variables will be adjusted to
1451 use the new global types. */
c906108c
SS
1452
1453void
ae5a43e0 1454preserve_values (struct objfile *objfile)
c906108c 1455{
ae5a43e0
DJ
1456 htab_t copied_types;
1457 struct value_history_chunk *cur;
52f0bd74 1458 struct internalvar *var;
a08702d6 1459 struct value *val;
ae5a43e0 1460 int i;
c906108c 1461
ae5a43e0
DJ
1462 /* Create the hash table. We allocate on the objfile's obstack, since
1463 it is soon to be deleted. */
1464 copied_types = create_copied_types_hash (objfile);
1465
1466 for (cur = value_history_chain; cur; cur = cur->next)
1467 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
1468 if (cur->values[i])
1469 preserve_one_value (cur->values[i], objfile, copied_types);
1470
1471 for (var = internalvars; var; var = var->next)
78267919 1472 preserve_one_internalvar (var, objfile, copied_types);
ae5a43e0 1473
a08702d6
TJB
1474 for (val = values_in_python; val; val = val->next)
1475 preserve_one_value (val, objfile, copied_types);
1476
ae5a43e0 1477 htab_delete (copied_types);
c906108c
SS
1478}
1479
1480static void
fba45db2 1481show_convenience (char *ignore, int from_tty)
c906108c 1482{
e17c207e 1483 struct gdbarch *gdbarch = get_current_arch ();
52f0bd74 1484 struct internalvar *var;
c906108c 1485 int varseen = 0;
79a45b7d 1486 struct value_print_options opts;
c906108c 1487
79a45b7d 1488 get_user_print_options (&opts);
c906108c
SS
1489 for (var = internalvars; var; var = var->next)
1490 {
c906108c
SS
1491 if (!varseen)
1492 {
1493 varseen = 1;
1494 }
a3f17187 1495 printf_filtered (("$%s = "), var->name);
78267919 1496 value_print (value_of_internalvar (gdbarch, var), gdb_stdout,
79a45b7d 1497 &opts);
a3f17187 1498 printf_filtered (("\n"));
c906108c
SS
1499 }
1500 if (!varseen)
a3f17187
AC
1501 printf_unfiltered (_("\
1502No debugger convenience variables now defined.\n\
c906108c 1503Convenience variables have names starting with \"$\";\n\
a3f17187 1504use \"set\" as in \"set $foo = 5\" to define them.\n"));
c906108c
SS
1505}
1506\f
1507/* Extract a value as a C number (either long or double).
1508 Knows how to convert fixed values to double, or
1509 floating values to long.
1510 Does not deallocate the value. */
1511
1512LONGEST
f23631e4 1513value_as_long (struct value *val)
c906108c
SS
1514{
1515 /* This coerces arrays and functions, which is necessary (e.g.
1516 in disassemble_command). It also dereferences references, which
1517 I suspect is the most logical thing to do. */
994b9211 1518 val = coerce_array (val);
0fd88904 1519 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
1520}
1521
1522DOUBLEST
f23631e4 1523value_as_double (struct value *val)
c906108c
SS
1524{
1525 DOUBLEST foo;
1526 int inv;
c5aa993b 1527
0fd88904 1528 foo = unpack_double (value_type (val), value_contents (val), &inv);
c906108c 1529 if (inv)
8a3fe4f8 1530 error (_("Invalid floating value found in program."));
c906108c
SS
1531 return foo;
1532}
4ef30785 1533
4478b372
JB
1534/* Extract a value as a C pointer. Does not deallocate the value.
1535 Note that val's type may not actually be a pointer; value_as_long
1536 handles all the cases. */
c906108c 1537CORE_ADDR
f23631e4 1538value_as_address (struct value *val)
c906108c 1539{
50810684
UW
1540 struct gdbarch *gdbarch = get_type_arch (value_type (val));
1541
c906108c
SS
1542 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1543 whether we want this to be true eventually. */
1544#if 0
bf6ae464 1545 /* gdbarch_addr_bits_remove is wrong if we are being called for a
c906108c
SS
1546 non-address (e.g. argument to "signal", "info break", etc.), or
1547 for pointers to char, in which the low bits *are* significant. */
50810684 1548 return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
c906108c 1549#else
f312f057
JB
1550
1551 /* There are several targets (IA-64, PowerPC, and others) which
1552 don't represent pointers to functions as simply the address of
1553 the function's entry point. For example, on the IA-64, a
1554 function pointer points to a two-word descriptor, generated by
1555 the linker, which contains the function's entry point, and the
1556 value the IA-64 "global pointer" register should have --- to
1557 support position-independent code. The linker generates
1558 descriptors only for those functions whose addresses are taken.
1559
1560 On such targets, it's difficult for GDB to convert an arbitrary
1561 function address into a function pointer; it has to either find
1562 an existing descriptor for that function, or call malloc and
1563 build its own. On some targets, it is impossible for GDB to
1564 build a descriptor at all: the descriptor must contain a jump
1565 instruction; data memory cannot be executed; and code memory
1566 cannot be modified.
1567
1568 Upon entry to this function, if VAL is a value of type `function'
1569 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
42ae5230 1570 value_address (val) is the address of the function. This is what
f312f057
JB
1571 you'll get if you evaluate an expression like `main'. The call
1572 to COERCE_ARRAY below actually does all the usual unary
1573 conversions, which includes converting values of type `function'
1574 to `pointer to function'. This is the challenging conversion
1575 discussed above. Then, `unpack_long' will convert that pointer
1576 back into an address.
1577
1578 So, suppose the user types `disassemble foo' on an architecture
1579 with a strange function pointer representation, on which GDB
1580 cannot build its own descriptors, and suppose further that `foo'
1581 has no linker-built descriptor. The address->pointer conversion
1582 will signal an error and prevent the command from running, even
1583 though the next step would have been to convert the pointer
1584 directly back into the same address.
1585
1586 The following shortcut avoids this whole mess. If VAL is a
1587 function, just return its address directly. */
df407dfe
AC
1588 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1589 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
42ae5230 1590 return value_address (val);
f312f057 1591
994b9211 1592 val = coerce_array (val);
fc0c74b1
AC
1593
1594 /* Some architectures (e.g. Harvard), map instruction and data
1595 addresses onto a single large unified address space. For
1596 instance: An architecture may consider a large integer in the
1597 range 0x10000000 .. 0x1000ffff to already represent a data
1598 addresses (hence not need a pointer to address conversion) while
1599 a small integer would still need to be converted integer to
1600 pointer to address. Just assume such architectures handle all
1601 integer conversions in a single function. */
1602
1603 /* JimB writes:
1604
1605 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1606 must admonish GDB hackers to make sure its behavior matches the
1607 compiler's, whenever possible.
1608
1609 In general, I think GDB should evaluate expressions the same way
1610 the compiler does. When the user copies an expression out of
1611 their source code and hands it to a `print' command, they should
1612 get the same value the compiler would have computed. Any
1613 deviation from this rule can cause major confusion and annoyance,
1614 and needs to be justified carefully. In other words, GDB doesn't
1615 really have the freedom to do these conversions in clever and
1616 useful ways.
1617
1618 AndrewC pointed out that users aren't complaining about how GDB
1619 casts integers to pointers; they are complaining that they can't
1620 take an address from a disassembly listing and give it to `x/i'.
1621 This is certainly important.
1622
79dd2d24 1623 Adding an architecture method like integer_to_address() certainly
fc0c74b1
AC
1624 makes it possible for GDB to "get it right" in all circumstances
1625 --- the target has complete control over how things get done, so
1626 people can Do The Right Thing for their target without breaking
1627 anyone else. The standard doesn't specify how integers get
1628 converted to pointers; usually, the ABI doesn't either, but
1629 ABI-specific code is a more reasonable place to handle it. */
1630
df407dfe
AC
1631 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
1632 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
50810684
UW
1633 && gdbarch_integer_to_address_p (gdbarch))
1634 return gdbarch_integer_to_address (gdbarch, value_type (val),
0fd88904 1635 value_contents (val));
fc0c74b1 1636
0fd88904 1637 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
1638#endif
1639}
1640\f
1641/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1642 as a long, or as a double, assuming the raw data is described
1643 by type TYPE. Knows how to convert different sizes of values
1644 and can convert between fixed and floating point. We don't assume
1645 any alignment for the raw data. Return value is in host byte order.
1646
1647 If you want functions and arrays to be coerced to pointers, and
1648 references to be dereferenced, call value_as_long() instead.
1649
1650 C++: It is assumed that the front-end has taken care of
1651 all matters concerning pointers to members. A pointer
1652 to member which reaches here is considered to be equivalent
1653 to an INT (or some size). After all, it is only an offset. */
1654
1655LONGEST
fc1a4b47 1656unpack_long (struct type *type, const gdb_byte *valaddr)
c906108c 1657{
e17a4113 1658 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
52f0bd74
AC
1659 enum type_code code = TYPE_CODE (type);
1660 int len = TYPE_LENGTH (type);
1661 int nosign = TYPE_UNSIGNED (type);
c906108c 1662
c906108c
SS
1663 switch (code)
1664 {
1665 case TYPE_CODE_TYPEDEF:
1666 return unpack_long (check_typedef (type), valaddr);
1667 case TYPE_CODE_ENUM:
4f2aea11 1668 case TYPE_CODE_FLAGS:
c906108c
SS
1669 case TYPE_CODE_BOOL:
1670 case TYPE_CODE_INT:
1671 case TYPE_CODE_CHAR:
1672 case TYPE_CODE_RANGE:
0d5de010 1673 case TYPE_CODE_MEMBERPTR:
c906108c 1674 if (nosign)
e17a4113 1675 return extract_unsigned_integer (valaddr, len, byte_order);
c906108c 1676 else
e17a4113 1677 return extract_signed_integer (valaddr, len, byte_order);
c906108c
SS
1678
1679 case TYPE_CODE_FLT:
96d2f608 1680 return extract_typed_floating (valaddr, type);
c906108c 1681
4ef30785
TJB
1682 case TYPE_CODE_DECFLOAT:
1683 /* libdecnumber has a function to convert from decimal to integer, but
1684 it doesn't work when the decimal number has a fractional part. */
e17a4113 1685 return decimal_to_doublest (valaddr, len, byte_order);
4ef30785 1686
c906108c
SS
1687 case TYPE_CODE_PTR:
1688 case TYPE_CODE_REF:
1689 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
c5aa993b 1690 whether we want this to be true eventually. */
4478b372 1691 return extract_typed_address (valaddr, type);
c906108c 1692
c906108c 1693 default:
8a3fe4f8 1694 error (_("Value can't be converted to integer."));
c906108c 1695 }
c5aa993b 1696 return 0; /* Placate lint. */
c906108c
SS
1697}
1698
1699/* Return a double value from the specified type and address.
1700 INVP points to an int which is set to 0 for valid value,
1701 1 for invalid value (bad float format). In either case,
1702 the returned double is OK to use. Argument is in target
1703 format, result is in host format. */
1704
1705DOUBLEST
fc1a4b47 1706unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
c906108c 1707{
e17a4113 1708 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
c906108c
SS
1709 enum type_code code;
1710 int len;
1711 int nosign;
1712
1713 *invp = 0; /* Assume valid. */
1714 CHECK_TYPEDEF (type);
1715 code = TYPE_CODE (type);
1716 len = TYPE_LENGTH (type);
1717 nosign = TYPE_UNSIGNED (type);
1718 if (code == TYPE_CODE_FLT)
1719 {
75bc7ddf
AC
1720 /* NOTE: cagney/2002-02-19: There was a test here to see if the
1721 floating-point value was valid (using the macro
1722 INVALID_FLOAT). That test/macro have been removed.
1723
1724 It turns out that only the VAX defined this macro and then
1725 only in a non-portable way. Fixing the portability problem
1726 wouldn't help since the VAX floating-point code is also badly
1727 bit-rotten. The target needs to add definitions for the
ea06eb3d 1728 methods gdbarch_float_format and gdbarch_double_format - these
75bc7ddf
AC
1729 exactly describe the target floating-point format. The
1730 problem here is that the corresponding floatformat_vax_f and
1731 floatformat_vax_d values these methods should be set to are
1732 also not defined either. Oops!
1733
1734 Hopefully someone will add both the missing floatformat
ac79b88b
DJ
1735 definitions and the new cases for floatformat_is_valid (). */
1736
1737 if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
1738 {
1739 *invp = 1;
1740 return 0.0;
1741 }
1742
96d2f608 1743 return extract_typed_floating (valaddr, type);
c906108c 1744 }
4ef30785 1745 else if (code == TYPE_CODE_DECFLOAT)
e17a4113 1746 return decimal_to_doublest (valaddr, len, byte_order);
c906108c
SS
1747 else if (nosign)
1748 {
1749 /* Unsigned -- be sure we compensate for signed LONGEST. */
c906108c 1750 return (ULONGEST) unpack_long (type, valaddr);
c906108c
SS
1751 }
1752 else
1753 {
1754 /* Signed -- we are OK with unpack_long. */
1755 return unpack_long (type, valaddr);
1756 }
1757}
1758
1759/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1760 as a CORE_ADDR, assuming the raw data is described by type TYPE.
1761 We don't assume any alignment for the raw data. Return value is in
1762 host byte order.
1763
1764 If you want functions and arrays to be coerced to pointers, and
1aa20aa8 1765 references to be dereferenced, call value_as_address() instead.
c906108c
SS
1766
1767 C++: It is assumed that the front-end has taken care of
1768 all matters concerning pointers to members. A pointer
1769 to member which reaches here is considered to be equivalent
1770 to an INT (or some size). After all, it is only an offset. */
1771
1772CORE_ADDR
fc1a4b47 1773unpack_pointer (struct type *type, const gdb_byte *valaddr)
c906108c
SS
1774{
1775 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1776 whether we want this to be true eventually. */
1777 return unpack_long (type, valaddr);
1778}
4478b372 1779
c906108c 1780\f
2c2738a0
DC
1781/* Get the value of the FIELDN'th field (which must be static) of
1782 TYPE. Return NULL if the field doesn't exist or has been
1783 optimized out. */
c906108c 1784
f23631e4 1785struct value *
fba45db2 1786value_static_field (struct type *type, int fieldno)
c906108c 1787{
948e66d9
DJ
1788 struct value *retval;
1789
d6a843b5 1790 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
c906108c 1791 {
948e66d9 1792 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
00a4c844 1793 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
c906108c
SS
1794 }
1795 else
1796 {
1797 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
2570f2b7 1798 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
948e66d9 1799 if (sym == NULL)
c906108c
SS
1800 {
1801 /* With some compilers, e.g. HP aCC, static data members are reported
c5aa993b
JM
1802 as non-debuggable symbols */
1803 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
c906108c
SS
1804 if (!msym)
1805 return NULL;
1806 else
c5aa993b 1807 {
948e66d9 1808 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
00a4c844 1809 SYMBOL_VALUE_ADDRESS (msym));
c906108c
SS
1810 }
1811 }
1812 else
1813 {
948e66d9
DJ
1814 /* SYM should never have a SYMBOL_CLASS which will require
1815 read_var_value to use the FRAME parameter. */
1816 if (symbol_read_needs_frame (sym))
8a3fe4f8
AC
1817 warning (_("static field's value depends on the current "
1818 "frame - bad debug info?"));
948e66d9 1819 retval = read_var_value (sym, NULL);
2b127877 1820 }
948e66d9
DJ
1821 if (retval && VALUE_LVAL (retval) == lval_memory)
1822 SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
42ae5230 1823 value_address (retval));
c906108c 1824 }
948e66d9 1825 return retval;
c906108c
SS
1826}
1827
2b127877
DB
1828/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1829 You have to be careful here, since the size of the data area for the value
1830 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
1831 than the old enclosing type, you have to allocate more space for the data.
1832 The return value is a pointer to the new version of this value structure. */
1833
f23631e4
AC
1834struct value *
1835value_change_enclosing_type (struct value *val, struct type *new_encl_type)
2b127877 1836{
3e3d7139
JG
1837 if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
1838 val->contents =
1839 (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
1840
1841 val->enclosing_type = new_encl_type;
1842 return val;
2b127877
DB
1843}
1844
c906108c
SS
1845/* Given a value ARG1 (offset by OFFSET bytes)
1846 of a struct or union type ARG_TYPE,
1847 extract and return the value of one of its (non-static) fields.
1848 FIELDNO says which field. */
1849
f23631e4
AC
1850struct value *
1851value_primitive_field (struct value *arg1, int offset,
aa1ee363 1852 int fieldno, struct type *arg_type)
c906108c 1853{
f23631e4 1854 struct value *v;
52f0bd74 1855 struct type *type;
c906108c
SS
1856
1857 CHECK_TYPEDEF (arg_type);
1858 type = TYPE_FIELD_TYPE (arg_type, fieldno);
1859
1860 /* Handle packed fields */
1861
1862 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
1863 {
1864 v = value_from_longest (type,
1865 unpack_field_as_long (arg_type,
0fd88904 1866 value_contents (arg1)
c5aa993b 1867 + offset,
c906108c 1868 fieldno));
df407dfe
AC
1869 v->bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
1870 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
1871 v->offset = value_offset (arg1) + offset
2e70b7b9 1872 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
c906108c
SS
1873 }
1874 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
1875 {
1876 /* This field is actually a base subobject, so preserve the
1877 entire object's contents for later references to virtual
1878 bases, etc. */
a4e2ee12
DJ
1879
1880 /* Lazy register values with offsets are not supported. */
1881 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
1882 value_fetch_lazy (arg1);
1883
1884 if (value_lazy (arg1))
3e3d7139 1885 v = allocate_value_lazy (value_enclosing_type (arg1));
c906108c 1886 else
3e3d7139
JG
1887 {
1888 v = allocate_value (value_enclosing_type (arg1));
1889 memcpy (value_contents_all_raw (v), value_contents_all_raw (arg1),
1890 TYPE_LENGTH (value_enclosing_type (arg1)));
1891 }
1892 v->type = type;
df407dfe 1893 v->offset = value_offset (arg1);
13c3b5f5
AC
1894 v->embedded_offset = (offset + value_embedded_offset (arg1)
1895 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
c906108c
SS
1896 }
1897 else
1898 {
1899 /* Plain old data member */
1900 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
a4e2ee12
DJ
1901
1902 /* Lazy register values with offsets are not supported. */
1903 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
1904 value_fetch_lazy (arg1);
1905
1906 if (value_lazy (arg1))
3e3d7139 1907 v = allocate_value_lazy (type);
c906108c 1908 else
3e3d7139
JG
1909 {
1910 v = allocate_value (type);
1911 memcpy (value_contents_raw (v),
1912 value_contents_raw (arg1) + offset,
1913 TYPE_LENGTH (type));
1914 }
df407dfe 1915 v->offset = (value_offset (arg1) + offset
13c3b5f5 1916 + value_embedded_offset (arg1));
c906108c 1917 }
74bcbdf3 1918 set_value_component_location (v, arg1);
9ee8fc9d 1919 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
0c16dd26 1920 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
c906108c
SS
1921 return v;
1922}
1923
1924/* Given a value ARG1 of a struct or union type,
1925 extract and return the value of one of its (non-static) fields.
1926 FIELDNO says which field. */
1927
f23631e4 1928struct value *
aa1ee363 1929value_field (struct value *arg1, int fieldno)
c906108c 1930{
df407dfe 1931 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
c906108c
SS
1932}
1933
1934/* Return a non-virtual function as a value.
1935 F is the list of member functions which contains the desired method.
0478d61c
FF
1936 J is an index into F which provides the desired method.
1937
1938 We only use the symbol for its address, so be happy with either a
1939 full symbol or a minimal symbol.
1940 */
c906108c 1941
f23631e4
AC
1942struct value *
1943value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
fba45db2 1944 int offset)
c906108c 1945{
f23631e4 1946 struct value *v;
52f0bd74 1947 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
0478d61c 1948 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c906108c 1949 struct symbol *sym;
0478d61c 1950 struct minimal_symbol *msym;
c906108c 1951
2570f2b7 1952 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
5ae326fa 1953 if (sym != NULL)
0478d61c 1954 {
5ae326fa
AC
1955 msym = NULL;
1956 }
1957 else
1958 {
1959 gdb_assert (sym == NULL);
0478d61c 1960 msym = lookup_minimal_symbol (physname, NULL, NULL);
5ae326fa
AC
1961 if (msym == NULL)
1962 return NULL;
0478d61c
FF
1963 }
1964
c906108c 1965 v = allocate_value (ftype);
0478d61c
FF
1966 if (sym)
1967 {
42ae5230 1968 set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
0478d61c
FF
1969 }
1970 else
1971 {
bccdca4a
UW
1972 /* The minimal symbol might point to a function descriptor;
1973 resolve it to the actual code address instead. */
1974 struct objfile *objfile = msymbol_objfile (msym);
1975 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1976
42ae5230
TT
1977 set_value_address (v,
1978 gdbarch_convert_from_func_ptr_addr
1979 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), &current_target));
0478d61c 1980 }
c906108c
SS
1981
1982 if (arg1p)
c5aa993b 1983 {
df407dfe 1984 if (type != value_type (*arg1p))
c5aa993b
JM
1985 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
1986 value_addr (*arg1p)));
1987
070ad9f0 1988 /* Move the `this' pointer according to the offset.
c5aa993b
JM
1989 VALUE_OFFSET (*arg1p) += offset;
1990 */
c906108c
SS
1991 }
1992
1993 return v;
1994}
1995
c906108c
SS
1996\f
1997/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1998 VALADDR.
1999
2000 Extracting bits depends on endianness of the machine. Compute the
2001 number of least significant bits to discard. For big endian machines,
2002 we compute the total number of bits in the anonymous object, subtract
2003 off the bit count from the MSB of the object to the MSB of the
2004 bitfield, then the size of the bitfield, which leaves the LSB discard
2005 count. For little endian machines, the discard count is simply the
2006 number of bits from the LSB of the anonymous object to the LSB of the
2007 bitfield.
2008
2009 If the field is signed, we also do sign extension. */
2010
2011LONGEST
fc1a4b47 2012unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
c906108c 2013{
e17a4113 2014 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
c906108c
SS
2015 ULONGEST val;
2016 ULONGEST valmask;
2017 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
2018 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
2019 int lsbcount;
2020 struct type *field_type;
2021
e17a4113
UW
2022 val = extract_unsigned_integer (valaddr + bitpos / 8,
2023 sizeof (val), byte_order);
c906108c
SS
2024 field_type = TYPE_FIELD_TYPE (type, fieldno);
2025 CHECK_TYPEDEF (field_type);
2026
2027 /* Extract bits. See comment above. */
2028
50810684 2029 if (gdbarch_bits_big_endian (get_type_arch (type)))
c906108c
SS
2030 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
2031 else
2032 lsbcount = (bitpos % 8);
2033 val >>= lsbcount;
2034
2035 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
2036 If the field is signed, and is negative, then sign extend. */
2037
2038 if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
2039 {
2040 valmask = (((ULONGEST) 1) << bitsize) - 1;
2041 val &= valmask;
2042 if (!TYPE_UNSIGNED (field_type))
2043 {
2044 if (val & (valmask ^ (valmask >> 1)))
2045 {
2046 val |= ~valmask;
2047 }
2048 }
2049 }
2050 return (val);
2051}
2052
2053/* Modify the value of a bitfield. ADDR points to a block of memory in
2054 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
2055 is the desired value of the field, in host byte order. BITPOS and BITSIZE
f4e88c8e
PH
2056 indicate which bits (in target bit order) comprise the bitfield.
2057 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
2058 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
c906108c
SS
2059
2060void
50810684
UW
2061modify_field (struct type *type, gdb_byte *addr,
2062 LONGEST fieldval, int bitpos, int bitsize)
c906108c 2063{
e17a4113 2064 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
f4e88c8e
PH
2065 ULONGEST oword;
2066 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
c906108c
SS
2067
2068 /* If a negative fieldval fits in the field in question, chop
2069 off the sign extension bits. */
f4e88c8e
PH
2070 if ((~fieldval & ~(mask >> 1)) == 0)
2071 fieldval &= mask;
c906108c
SS
2072
2073 /* Warn if value is too big to fit in the field in question. */
f4e88c8e 2074 if (0 != (fieldval & ~mask))
c906108c
SS
2075 {
2076 /* FIXME: would like to include fieldval in the message, but
c5aa993b 2077 we don't have a sprintf_longest. */
8a3fe4f8 2078 warning (_("Value does not fit in %d bits."), bitsize);
c906108c
SS
2079
2080 /* Truncate it, otherwise adjoining fields may be corrupted. */
f4e88c8e 2081 fieldval &= mask;
c906108c
SS
2082 }
2083
e17a4113 2084 oword = extract_unsigned_integer (addr, sizeof oword, byte_order);
c906108c
SS
2085
2086 /* Shifting for bit field depends on endianness of the target machine. */
50810684 2087 if (gdbarch_bits_big_endian (get_type_arch (type)))
c906108c
SS
2088 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
2089
f4e88c8e 2090 oword &= ~(mask << bitpos);
c906108c
SS
2091 oword |= fieldval << bitpos;
2092
e17a4113 2093 store_unsigned_integer (addr, sizeof oword, byte_order, oword);
c906108c
SS
2094}
2095\f
14d06750 2096/* Pack NUM into BUF using a target format of TYPE. */
c906108c 2097
14d06750
DJ
2098void
2099pack_long (gdb_byte *buf, struct type *type, LONGEST num)
c906108c 2100{
e17a4113 2101 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
52f0bd74 2102 int len;
14d06750
DJ
2103
2104 type = check_typedef (type);
c906108c
SS
2105 len = TYPE_LENGTH (type);
2106
14d06750 2107 switch (TYPE_CODE (type))
c906108c 2108 {
c906108c
SS
2109 case TYPE_CODE_INT:
2110 case TYPE_CODE_CHAR:
2111 case TYPE_CODE_ENUM:
4f2aea11 2112 case TYPE_CODE_FLAGS:
c906108c
SS
2113 case TYPE_CODE_BOOL:
2114 case TYPE_CODE_RANGE:
0d5de010 2115 case TYPE_CODE_MEMBERPTR:
e17a4113 2116 store_signed_integer (buf, len, byte_order, num);
c906108c 2117 break;
c5aa993b 2118
c906108c
SS
2119 case TYPE_CODE_REF:
2120 case TYPE_CODE_PTR:
14d06750 2121 store_typed_address (buf, type, (CORE_ADDR) num);
c906108c 2122 break;
c5aa993b 2123
c906108c 2124 default:
14d06750
DJ
2125 error (_("Unexpected type (%d) encountered for integer constant."),
2126 TYPE_CODE (type));
c906108c 2127 }
14d06750
DJ
2128}
2129
2130
2131/* Convert C numbers into newly allocated values. */
2132
2133struct value *
2134value_from_longest (struct type *type, LONGEST num)
2135{
2136 struct value *val = allocate_value (type);
2137
2138 pack_long (value_contents_raw (val), type, num);
2139
c906108c
SS
2140 return val;
2141}
2142
4478b372
JB
2143
2144/* Create a value representing a pointer of type TYPE to the address
2145 ADDR. */
f23631e4 2146struct value *
4478b372
JB
2147value_from_pointer (struct type *type, CORE_ADDR addr)
2148{
f23631e4 2149 struct value *val = allocate_value (type);
990a07ab 2150 store_typed_address (value_contents_raw (val), type, addr);
4478b372
JB
2151 return val;
2152}
2153
2154
8acb6b92
TT
2155/* Create a value of type TYPE whose contents come from VALADDR, if it
2156 is non-null, and whose memory address (in the inferior) is
2157 ADDRESS. */
2158
2159struct value *
2160value_from_contents_and_address (struct type *type,
2161 const gdb_byte *valaddr,
2162 CORE_ADDR address)
2163{
2164 struct value *v = allocate_value (type);
2165 if (valaddr == NULL)
2166 set_value_lazy (v, 1);
2167 else
2168 memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
42ae5230 2169 set_value_address (v, address);
33d502b4 2170 VALUE_LVAL (v) = lval_memory;
8acb6b92
TT
2171 return v;
2172}
2173
f23631e4 2174struct value *
fba45db2 2175value_from_double (struct type *type, DOUBLEST num)
c906108c 2176{
f23631e4 2177 struct value *val = allocate_value (type);
c906108c 2178 struct type *base_type = check_typedef (type);
52f0bd74
AC
2179 enum type_code code = TYPE_CODE (base_type);
2180 int len = TYPE_LENGTH (base_type);
c906108c
SS
2181
2182 if (code == TYPE_CODE_FLT)
2183 {
990a07ab 2184 store_typed_floating (value_contents_raw (val), base_type, num);
c906108c
SS
2185 }
2186 else
8a3fe4f8 2187 error (_("Unexpected type encountered for floating constant."));
c906108c
SS
2188
2189 return val;
2190}
994b9211 2191
27bc4d80 2192struct value *
4ef30785 2193value_from_decfloat (struct type *type, const gdb_byte *dec)
27bc4d80
TJB
2194{
2195 struct value *val = allocate_value (type);
27bc4d80 2196
4ef30785 2197 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
27bc4d80 2198
27bc4d80
TJB
2199 return val;
2200}
2201
994b9211
AC
2202struct value *
2203coerce_ref (struct value *arg)
2204{
df407dfe 2205 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
994b9211
AC
2206 if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)
2207 arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
df407dfe 2208 unpack_pointer (value_type (arg),
0fd88904 2209 value_contents (arg)));
994b9211
AC
2210 return arg;
2211}
2212
2213struct value *
2214coerce_array (struct value *arg)
2215{
f3134b88
TT
2216 struct type *type;
2217
994b9211 2218 arg = coerce_ref (arg);
f3134b88
TT
2219 type = check_typedef (value_type (arg));
2220
2221 switch (TYPE_CODE (type))
2222 {
2223 case TYPE_CODE_ARRAY:
2224 if (current_language->c_style_arrays)
2225 arg = value_coerce_array (arg);
2226 break;
2227 case TYPE_CODE_FUNC:
2228 arg = value_coerce_function (arg);
2229 break;
2230 }
994b9211
AC
2231 return arg;
2232}
c906108c 2233\f
c906108c 2234
48436ce6
AC
2235/* Return true if the function returning the specified type is using
2236 the convention of returning structures in memory (passing in the
82585c72 2237 address as a hidden first parameter). */
c906108c
SS
2238
2239int
d80b854b
UW
2240using_struct_return (struct gdbarch *gdbarch,
2241 struct type *func_type, struct type *value_type)
c906108c 2242{
52f0bd74 2243 enum type_code code = TYPE_CODE (value_type);
c906108c
SS
2244
2245 if (code == TYPE_CODE_ERROR)
8a3fe4f8 2246 error (_("Function return type unknown."));
c906108c 2247
667e784f
AC
2248 if (code == TYPE_CODE_VOID)
2249 /* A void return value is never in memory. See also corresponding
44e5158b 2250 code in "print_return_value". */
667e784f
AC
2251 return 0;
2252
92ad9cd9 2253 /* Probe the architecture for the return-value convention. */
d80b854b 2254 return (gdbarch_return_value (gdbarch, func_type, value_type,
92ad9cd9 2255 NULL, NULL, NULL)
31db7b6c 2256 != RETURN_VALUE_REGISTER_CONVENTION);
c906108c
SS
2257}
2258
42be36b3
CT
2259/* Set the initialized field in a value struct. */
2260
2261void
2262set_value_initialized (struct value *val, int status)
2263{
2264 val->initialized = status;
2265}
2266
2267/* Return the initialized field in a value struct. */
2268
2269int
2270value_initialized (struct value *val)
2271{
2272 return val->initialized;
2273}
2274
c906108c 2275void
fba45db2 2276_initialize_values (void)
c906108c 2277{
1a966eab
AC
2278 add_cmd ("convenience", no_class, show_convenience, _("\
2279Debugger convenience (\"$foo\") variables.\n\
c906108c 2280These variables are created when you assign them values;\n\
1a966eab
AC
2281thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
2282\n\
c906108c
SS
2283A few convenience variables are given values automatically:\n\
2284\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1a966eab 2285\"$__\" holds the contents of the last address examined with \"x\"."),
c906108c
SS
2286 &showlist);
2287
2288 add_cmd ("values", no_class, show_values,
1a966eab 2289 _("Elements of value history around item number IDX (or last ten)."),
c906108c 2290 &showlist);
53e5f3cf
AS
2291
2292 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
2293Initialize a convenience variable if necessary.\n\
2294init-if-undefined VARIABLE = EXPRESSION\n\
2295Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
2296exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
2297VARIABLE is already initialized."));
bc3b79fd
TJB
2298
2299 add_prefix_cmd ("function", no_class, function_command, _("\
2300Placeholder command for showing help on convenience functions."),
2301 &functionlist, "function ", 0, &cmdlist);
c906108c 2302}