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