* stack.c (print_frame_args): Fix typos in comments.
[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,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4f2aea11 5 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"
c906108c
SS
39
40/* Prototypes for exported functions. */
41
a14ed312 42void _initialize_values (void);
c906108c 43
91294c83
AC
44struct value
45{
46 /* Type of value; either not an lval, or one of the various
47 different possible kinds of lval. */
48 enum lval_type lval;
49
50 /* Is it modifiable? Only relevant if lval != not_lval. */
51 int modifiable;
52
53 /* Location of value (if lval). */
54 union
55 {
56 /* If lval == lval_memory, this is the address in the inferior.
57 If lval == lval_register, this is the byte offset into the
58 registers structure. */
59 CORE_ADDR address;
60
61 /* Pointer to internal variable. */
62 struct internalvar *internalvar;
63 } location;
64
65 /* Describes offset of a value within lval of a structure in bytes.
66 If lval == lval_memory, this is an offset to the address. If
67 lval == lval_register, this is a further offset from
68 location.address within the registers structure. Note also the
69 member embedded_offset below. */
70 int offset;
71
72 /* Only used for bitfields; number of bits contained in them. */
73 int bitsize;
74
75 /* Only used for bitfields; position of start of field. For
32c9a795
MD
76 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
77 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
91294c83
AC
78 int bitpos;
79
80 /* Frame register value is relative to. This will be described in
81 the lval enum above as "lval_register". */
82 struct frame_id frame_id;
83
84 /* Type of the value. */
85 struct type *type;
86
87 /* If a value represents a C++ object, then the `type' field gives
88 the object's compile-time type. If the object actually belongs
89 to some class derived from `type', perhaps with other base
90 classes and additional members, then `type' is just a subobject
91 of the real thing, and the full object is probably larger than
92 `type' would suggest.
93
94 If `type' is a dynamic class (i.e. one with a vtable), then GDB
95 can actually determine the object's run-time type by looking at
96 the run-time type information in the vtable. When this
97 information is available, we may elect to read in the entire
98 object, for several reasons:
99
100 - When printing the value, the user would probably rather see the
101 full object, not just the limited portion apparent from the
102 compile-time type.
103
104 - If `type' has virtual base classes, then even printing `type'
105 alone may require reaching outside the `type' portion of the
106 object to wherever the virtual base class has been stored.
107
108 When we store the entire object, `enclosing_type' is the run-time
109 type -- the complete object -- and `embedded_offset' is the
110 offset of `type' within that larger type, in bytes. The
111 value_contents() macro takes `embedded_offset' into account, so
112 most GDB code continues to see the `type' portion of the value,
113 just as the inferior would.
114
115 If `type' is a pointer to an object, then `enclosing_type' is a
116 pointer to the object's run-time type, and `pointed_to_offset' is
117 the offset in bytes from the full object to the pointed-to object
118 -- that is, the value `embedded_offset' would have if we followed
119 the pointer and fetched the complete object. (I don't really see
120 the point. Why not just determine the run-time type when you
121 indirect, and avoid the special case? The contents don't matter
122 until you indirect anyway.)
123
124 If we're not doing anything fancy, `enclosing_type' is equal to
125 `type', and `embedded_offset' is zero, so everything works
126 normally. */
127 struct type *enclosing_type;
128 int embedded_offset;
129 int pointed_to_offset;
130
131 /* Values are stored in a chain, so that they can be deleted easily
132 over calls to the inferior. Values assigned to internal
133 variables or put into the value history are taken off this
134 list. */
135 struct value *next;
136
137 /* Register number if the value is from a register. */
138 short regnum;
139
140 /* If zero, contents of this value are in the contents field. If
9214ee5f
DJ
141 nonzero, contents are in inferior. If the lval field is lval_memory,
142 the contents are in inferior memory at location.address plus offset.
143 The lval field may also be lval_register.
91294c83
AC
144
145 WARNING: This field is used by the code which handles watchpoints
146 (see breakpoint.c) to decide whether a particular value can be
147 watched by hardware watchpoints. If the lazy flag is set for
148 some member of a value chain, it is assumed that this member of
149 the chain doesn't need to be watched as part of watching the
150 value itself. This is how GDB avoids watching the entire struct
151 or array when the user wants to watch a single struct member or
152 array element. If you ever change the way lazy flag is set and
153 reset, be sure to consider this use as well! */
154 char lazy;
155
156 /* If nonzero, this is the value of a variable which does not
157 actually exist in the program. */
158 char optimized_out;
159
42be36b3
CT
160 /* If value is a variable, is it initialized or not. */
161 int initialized;
162
91294c83
AC
163 /* Actual contents of the value. For use of this value; setting it
164 uses the stuff above. Not valid if lazy is nonzero. Target
165 byte-order. We force it to be aligned properly for any possible
166 value. Note that a value therefore extends beyond what is
167 declared here. */
168 union
169 {
fc1a4b47 170 gdb_byte contents[1];
91294c83
AC
171 DOUBLEST force_doublest_align;
172 LONGEST force_longest_align;
173 CORE_ADDR force_core_addr_align;
174 void *force_pointer_align;
175 } aligner;
176 /* Do not add any new members here -- contents above will trash
177 them. */
178};
179
c906108c
SS
180/* Prototypes for local functions. */
181
a14ed312 182static void show_values (char *, int);
c906108c 183
a14ed312 184static void show_convenience (char *, int);
c906108c 185
c906108c
SS
186
187/* The value-history records all the values printed
188 by print commands during this session. Each chunk
189 records 60 consecutive values. The first chunk on
190 the chain records the most recent values.
191 The total number of values is in value_history_count. */
192
193#define VALUE_HISTORY_CHUNK 60
194
195struct value_history_chunk
c5aa993b
JM
196 {
197 struct value_history_chunk *next;
f23631e4 198 struct value *values[VALUE_HISTORY_CHUNK];
c5aa993b 199 };
c906108c
SS
200
201/* Chain of chunks now in use. */
202
203static struct value_history_chunk *value_history_chain;
204
205static int value_history_count; /* Abs number of last entry stored */
206\f
207/* List of all value objects currently allocated
208 (except for those released by calls to release_value)
209 This is so they can be freed after each command. */
210
f23631e4 211static struct value *all_values;
c906108c
SS
212
213/* Allocate a value that has the correct length for type TYPE. */
214
f23631e4 215struct value *
fba45db2 216allocate_value (struct type *type)
c906108c 217{
f23631e4 218 struct value *val;
c906108c
SS
219 struct type *atype = check_typedef (type);
220
5b90c7b5 221 val = (struct value *) xzalloc (sizeof (struct value) + TYPE_LENGTH (atype));
df407dfe 222 val->next = all_values;
c906108c 223 all_values = val;
df407dfe 224 val->type = type;
4754a64e 225 val->enclosing_type = type;
c906108c
SS
226 VALUE_LVAL (val) = not_lval;
227 VALUE_ADDRESS (val) = 0;
1df6926e 228 VALUE_FRAME_ID (val) = null_frame_id;
df407dfe
AC
229 val->offset = 0;
230 val->bitpos = 0;
231 val->bitsize = 0;
9ee8fc9d 232 VALUE_REGNUM (val) = -1;
d69fe07e 233 val->lazy = 0;
feb13ab0 234 val->optimized_out = 0;
13c3b5f5 235 val->embedded_offset = 0;
b44d461b 236 val->pointed_to_offset = 0;
c906108c 237 val->modifiable = 1;
42be36b3 238 val->initialized = 1; /* Default to initialized. */
c906108c
SS
239 return val;
240}
241
242/* Allocate a value that has the correct length
938f5214 243 for COUNT repetitions of type TYPE. */
c906108c 244
f23631e4 245struct value *
fba45db2 246allocate_repeat_value (struct type *type, int count)
c906108c 247{
c5aa993b 248 int low_bound = current_language->string_lower_bound; /* ??? */
c906108c
SS
249 /* FIXME-type-allocation: need a way to free this type when we are
250 done with it. */
251 struct type *range_type
6d84d3d8 252 = create_range_type ((struct type *) NULL, builtin_type_int32,
c5aa993b 253 low_bound, count + low_bound - 1);
c906108c
SS
254 /* FIXME-type-allocation: need a way to free this type when we are
255 done with it. */
256 return allocate_value (create_array_type ((struct type *) NULL,
257 type, range_type));
258}
259
df407dfe
AC
260/* Accessor methods. */
261
17cf0ecd
AC
262struct value *
263value_next (struct value *value)
264{
265 return value->next;
266}
267
df407dfe
AC
268struct type *
269value_type (struct value *value)
270{
271 return value->type;
272}
04624583
AC
273void
274deprecated_set_value_type (struct value *value, struct type *type)
275{
276 value->type = type;
277}
df407dfe
AC
278
279int
280value_offset (struct value *value)
281{
282 return value->offset;
283}
f5cf64a7
AC
284void
285set_value_offset (struct value *value, int offset)
286{
287 value->offset = offset;
288}
df407dfe
AC
289
290int
291value_bitpos (struct value *value)
292{
293 return value->bitpos;
294}
9bbda503
AC
295void
296set_value_bitpos (struct value *value, int bit)
297{
298 value->bitpos = bit;
299}
df407dfe
AC
300
301int
302value_bitsize (struct value *value)
303{
304 return value->bitsize;
305}
9bbda503
AC
306void
307set_value_bitsize (struct value *value, int bit)
308{
309 value->bitsize = bit;
310}
df407dfe 311
fc1a4b47 312gdb_byte *
990a07ab
AC
313value_contents_raw (struct value *value)
314{
315 return value->aligner.contents + value->embedded_offset;
316}
317
fc1a4b47 318gdb_byte *
990a07ab
AC
319value_contents_all_raw (struct value *value)
320{
321 return value->aligner.contents;
322}
323
4754a64e
AC
324struct type *
325value_enclosing_type (struct value *value)
326{
327 return value->enclosing_type;
328}
329
fc1a4b47 330const gdb_byte *
46615f07
AC
331value_contents_all (struct value *value)
332{
333 if (value->lazy)
334 value_fetch_lazy (value);
335 return value->aligner.contents;
336}
337
d69fe07e
AC
338int
339value_lazy (struct value *value)
340{
341 return value->lazy;
342}
343
dfa52d88
AC
344void
345set_value_lazy (struct value *value, int val)
346{
347 value->lazy = val;
348}
349
fc1a4b47 350const gdb_byte *
0fd88904
AC
351value_contents (struct value *value)
352{
353 return value_contents_writeable (value);
354}
355
fc1a4b47 356gdb_byte *
0fd88904
AC
357value_contents_writeable (struct value *value)
358{
359 if (value->lazy)
360 value_fetch_lazy (value);
fc0c53a0 361 return value_contents_raw (value);
0fd88904
AC
362}
363
a6c442d8
MK
364/* Return non-zero if VAL1 and VAL2 have the same contents. Note that
365 this function is different from value_equal; in C the operator ==
366 can return 0 even if the two values being compared are equal. */
367
368int
369value_contents_equal (struct value *val1, struct value *val2)
370{
371 struct type *type1;
372 struct type *type2;
373 int len;
374
375 type1 = check_typedef (value_type (val1));
376 type2 = check_typedef (value_type (val2));
377 len = TYPE_LENGTH (type1);
378 if (len != TYPE_LENGTH (type2))
379 return 0;
380
381 return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
382}
383
feb13ab0
AC
384int
385value_optimized_out (struct value *value)
386{
387 return value->optimized_out;
388}
389
390void
391set_value_optimized_out (struct value *value, int val)
392{
393 value->optimized_out = val;
394}
13c3b5f5
AC
395
396int
397value_embedded_offset (struct value *value)
398{
399 return value->embedded_offset;
400}
401
402void
403set_value_embedded_offset (struct value *value, int val)
404{
405 value->embedded_offset = val;
406}
b44d461b
AC
407
408int
409value_pointed_to_offset (struct value *value)
410{
411 return value->pointed_to_offset;
412}
413
414void
415set_value_pointed_to_offset (struct value *value, int val)
416{
417 value->pointed_to_offset = val;
418}
13bb5560
AC
419
420enum lval_type *
421deprecated_value_lval_hack (struct value *value)
422{
423 return &value->lval;
424}
425
426CORE_ADDR *
427deprecated_value_address_hack (struct value *value)
428{
429 return &value->location.address;
430}
431
432struct internalvar **
433deprecated_value_internalvar_hack (struct value *value)
434{
435 return &value->location.internalvar;
436}
437
438struct frame_id *
439deprecated_value_frame_id_hack (struct value *value)
440{
441 return &value->frame_id;
442}
443
444short *
445deprecated_value_regnum_hack (struct value *value)
446{
447 return &value->regnum;
448}
88e3b34b
AC
449
450int
451deprecated_value_modifiable (struct value *value)
452{
453 return value->modifiable;
454}
455void
456deprecated_set_value_modifiable (struct value *value, int modifiable)
457{
458 value->modifiable = modifiable;
459}
990a07ab 460\f
c906108c
SS
461/* Return a mark in the value chain. All values allocated after the
462 mark is obtained (except for those released) are subject to being freed
463 if a subsequent value_free_to_mark is passed the mark. */
f23631e4 464struct value *
fba45db2 465value_mark (void)
c906108c
SS
466{
467 return all_values;
468}
469
470/* Free all values allocated since MARK was obtained by value_mark
471 (except for those released). */
472void
f23631e4 473value_free_to_mark (struct value *mark)
c906108c 474{
f23631e4
AC
475 struct value *val;
476 struct value *next;
c906108c
SS
477
478 for (val = all_values; val && val != mark; val = next)
479 {
df407dfe 480 next = val->next;
c906108c
SS
481 value_free (val);
482 }
483 all_values = val;
484}
485
486/* Free all the values that have been allocated (except for those released).
487 Called after each command, successful or not. */
488
489void
fba45db2 490free_all_values (void)
c906108c 491{
f23631e4
AC
492 struct value *val;
493 struct value *next;
c906108c
SS
494
495 for (val = all_values; val; val = next)
496 {
df407dfe 497 next = val->next;
c906108c
SS
498 value_free (val);
499 }
500
501 all_values = 0;
502}
503
504/* Remove VAL from the chain all_values
505 so it will not be freed automatically. */
506
507void
f23631e4 508release_value (struct value *val)
c906108c 509{
f23631e4 510 struct value *v;
c906108c
SS
511
512 if (all_values == val)
513 {
514 all_values = val->next;
515 return;
516 }
517
518 for (v = all_values; v; v = v->next)
519 {
520 if (v->next == val)
521 {
522 v->next = val->next;
523 break;
524 }
525 }
526}
527
528/* Release all values up to mark */
f23631e4
AC
529struct value *
530value_release_to_mark (struct value *mark)
c906108c 531{
f23631e4
AC
532 struct value *val;
533 struct value *next;
c906108c 534
df407dfe
AC
535 for (val = next = all_values; next; next = next->next)
536 if (next->next == mark)
c906108c 537 {
df407dfe
AC
538 all_values = next->next;
539 next->next = NULL;
c906108c
SS
540 return val;
541 }
542 all_values = 0;
543 return val;
544}
545
546/* Return a copy of the value ARG.
547 It contains the same contents, for same memory address,
548 but it's a different block of storage. */
549
f23631e4
AC
550struct value *
551value_copy (struct value *arg)
c906108c 552{
4754a64e 553 struct type *encl_type = value_enclosing_type (arg);
f23631e4 554 struct value *val = allocate_value (encl_type);
df407dfe 555 val->type = arg->type;
c906108c 556 VALUE_LVAL (val) = VALUE_LVAL (arg);
6f7c8fc2 557 val->location = arg->location;
df407dfe
AC
558 val->offset = arg->offset;
559 val->bitpos = arg->bitpos;
560 val->bitsize = arg->bitsize;
1df6926e 561 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
9ee8fc9d 562 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
d69fe07e 563 val->lazy = arg->lazy;
feb13ab0 564 val->optimized_out = arg->optimized_out;
13c3b5f5 565 val->embedded_offset = value_embedded_offset (arg);
b44d461b 566 val->pointed_to_offset = arg->pointed_to_offset;
c906108c 567 val->modifiable = arg->modifiable;
d69fe07e 568 if (!value_lazy (val))
c906108c 569 {
990a07ab 570 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
4754a64e 571 TYPE_LENGTH (value_enclosing_type (arg)));
c906108c
SS
572
573 }
574 return val;
575}
576\f
577/* Access to the value history. */
578
579/* Record a new value in the value history.
580 Returns the absolute history index of the entry.
581 Result of -1 indicates the value was not saved; otherwise it is the
582 value history index of this new item. */
583
584int
f23631e4 585record_latest_value (struct value *val)
c906108c
SS
586{
587 int i;
588
589 /* We don't want this value to have anything to do with the inferior anymore.
590 In particular, "set $1 = 50" should not affect the variable from which
591 the value was taken, and fast watchpoints should be able to assume that
592 a value on the value history never changes. */
d69fe07e 593 if (value_lazy (val))
c906108c
SS
594 value_fetch_lazy (val);
595 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
596 from. This is a bit dubious, because then *&$1 does not just return $1
597 but the current contents of that location. c'est la vie... */
598 val->modifiable = 0;
599 release_value (val);
600
601 /* Here we treat value_history_count as origin-zero
602 and applying to the value being stored now. */
603
604 i = value_history_count % VALUE_HISTORY_CHUNK;
605 if (i == 0)
606 {
f23631e4 607 struct value_history_chunk *new
c5aa993b
JM
608 = (struct value_history_chunk *)
609 xmalloc (sizeof (struct value_history_chunk));
c906108c
SS
610 memset (new->values, 0, sizeof new->values);
611 new->next = value_history_chain;
612 value_history_chain = new;
613 }
614
615 value_history_chain->values[i] = val;
616
617 /* Now we regard value_history_count as origin-one
618 and applying to the value just stored. */
619
620 return ++value_history_count;
621}
622
623/* Return a copy of the value in the history with sequence number NUM. */
624
f23631e4 625struct value *
fba45db2 626access_value_history (int num)
c906108c 627{
f23631e4 628 struct value_history_chunk *chunk;
52f0bd74
AC
629 int i;
630 int absnum = num;
c906108c
SS
631
632 if (absnum <= 0)
633 absnum += value_history_count;
634
635 if (absnum <= 0)
636 {
637 if (num == 0)
8a3fe4f8 638 error (_("The history is empty."));
c906108c 639 else if (num == 1)
8a3fe4f8 640 error (_("There is only one value in the history."));
c906108c 641 else
8a3fe4f8 642 error (_("History does not go back to $$%d."), -num);
c906108c
SS
643 }
644 if (absnum > value_history_count)
8a3fe4f8 645 error (_("History has not yet reached $%d."), absnum);
c906108c
SS
646
647 absnum--;
648
649 /* Now absnum is always absolute and origin zero. */
650
651 chunk = value_history_chain;
652 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
653 i > 0; i--)
654 chunk = chunk->next;
655
656 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
657}
658
c906108c 659static void
fba45db2 660show_values (char *num_exp, int from_tty)
c906108c 661{
52f0bd74 662 int i;
f23631e4 663 struct value *val;
c906108c
SS
664 static int num = 1;
665
666 if (num_exp)
667 {
f132ba9d
TJB
668 /* "show values +" should print from the stored position.
669 "show values <exp>" should print around value number <exp>. */
c906108c 670 if (num_exp[0] != '+' || num_exp[1] != '\0')
bb518678 671 num = parse_and_eval_long (num_exp) - 5;
c906108c
SS
672 }
673 else
674 {
f132ba9d 675 /* "show values" means print the last 10 values. */
c906108c
SS
676 num = value_history_count - 9;
677 }
678
679 if (num <= 0)
680 num = 1;
681
682 for (i = num; i < num + 10 && i <= value_history_count; i++)
683 {
684 val = access_value_history (i);
a3f17187 685 printf_filtered (("$%d = "), i);
c906108c 686 value_print (val, gdb_stdout, 0, Val_pretty_default);
a3f17187 687 printf_filtered (("\n"));
c906108c
SS
688 }
689
f132ba9d 690 /* The next "show values +" should start after what we just printed. */
c906108c
SS
691 num += 10;
692
693 /* Hitting just return after this command should do the same thing as
f132ba9d
TJB
694 "show values +". If num_exp is null, this is unnecessary, since
695 "show values +" is not useful after "show values". */
c906108c
SS
696 if (from_tty && num_exp)
697 {
698 num_exp[0] = '+';
699 num_exp[1] = '\0';
700 }
701}
702\f
703/* Internal variables. These are variables within the debugger
704 that hold values assigned by debugger commands.
705 The user refers to them with a '$' prefix
706 that does not appear in the variable names stored internally. */
707
708static struct internalvar *internalvars;
709
53e5f3cf
AS
710/* If the variable does not already exist create it and give it the value given.
711 If no value is given then the default is zero. */
712static void
713init_if_undefined_command (char* args, int from_tty)
714{
715 struct internalvar* intvar;
716
717 /* Parse the expression - this is taken from set_command(). */
718 struct expression *expr = parse_expression (args);
719 register struct cleanup *old_chain =
720 make_cleanup (free_current_contents, &expr);
721
722 /* Validate the expression.
723 Was the expression an assignment?
724 Or even an expression at all? */
725 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
726 error (_("Init-if-undefined requires an assignment expression."));
727
728 /* Extract the variable from the parsed expression.
729 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
730 if (expr->elts[1].opcode != OP_INTERNALVAR)
731 error (_("The first parameter to init-if-undefined should be a GDB variable."));
732 intvar = expr->elts[2].internalvar;
733
734 /* Only evaluate the expression if the lvalue is void.
735 This may still fail if the expresssion is invalid. */
736 if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
737 evaluate_expression (expr);
738
739 do_cleanups (old_chain);
740}
741
742
c906108c
SS
743/* Look up an internal variable with name NAME. NAME should not
744 normally include a dollar sign.
745
746 If the specified internal variable does not exist,
c4a3d09a 747 the return value is NULL. */
c906108c
SS
748
749struct internalvar *
c4a3d09a 750lookup_only_internalvar (char *name)
c906108c 751{
52f0bd74 752 struct internalvar *var;
c906108c
SS
753
754 for (var = internalvars; var; var = var->next)
5cb316ef 755 if (strcmp (var->name, name) == 0)
c906108c
SS
756 return var;
757
c4a3d09a
MF
758 return NULL;
759}
760
761
762/* Create an internal variable with name NAME and with a void value.
763 NAME should not normally include a dollar sign. */
764
765struct internalvar *
766create_internalvar (char *name)
767{
768 struct internalvar *var;
c906108c 769 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
1754f103 770 var->name = concat (name, (char *)NULL);
c906108c 771 var->value = allocate_value (builtin_type_void);
0d20ae72 772 var->endian = gdbarch_byte_order (current_gdbarch);
c906108c
SS
773 release_value (var->value);
774 var->next = internalvars;
775 internalvars = var;
776 return var;
777}
778
c4a3d09a
MF
779
780/* Look up an internal variable with name NAME. NAME should not
781 normally include a dollar sign.
782
783 If the specified internal variable does not exist,
784 one is created, with a void value. */
785
786struct internalvar *
787lookup_internalvar (char *name)
788{
789 struct internalvar *var;
790
791 var = lookup_only_internalvar (name);
792 if (var)
793 return var;
794
795 return create_internalvar (name);
796}
797
f23631e4 798struct value *
fba45db2 799value_of_internalvar (struct internalvar *var)
c906108c 800{
f23631e4 801 struct value *val;
d3c139e9
AS
802 int i, j;
803 gdb_byte temp;
c906108c 804
c906108c 805 val = value_copy (var->value);
d69fe07e 806 if (value_lazy (val))
c906108c
SS
807 value_fetch_lazy (val);
808 VALUE_LVAL (val) = lval_internalvar;
809 VALUE_INTERNALVAR (val) = var;
d3c139e9
AS
810
811 /* Values are always stored in the target's byte order. When connected to a
812 target this will most likely always be correct, so there's normally no
813 need to worry about it.
814
815 However, internal variables can be set up before the target endian is
816 known and so may become out of date. Fix it up before anybody sees.
817
818 Internal variables usually hold simple scalar values, and we can
819 correct those. More complex values (e.g. structures and floating
820 point types) are left alone, because they would be too complicated
821 to correct. */
822
0d20ae72 823 if (var->endian != gdbarch_byte_order (current_gdbarch))
d3c139e9
AS
824 {
825 gdb_byte *array = value_contents_raw (val);
826 struct type *type = check_typedef (value_enclosing_type (val));
827 switch (TYPE_CODE (type))
828 {
829 case TYPE_CODE_INT:
830 case TYPE_CODE_PTR:
831 /* Reverse the bytes. */
832 for (i = 0, j = TYPE_LENGTH (type) - 1; i < j; i++, j--)
833 {
834 temp = array[j];
835 array[j] = array[i];
836 array[i] = temp;
837 }
838 break;
839 }
840 }
841
c906108c
SS
842 return val;
843}
844
845void
fba45db2 846set_internalvar_component (struct internalvar *var, int offset, int bitpos,
f23631e4 847 int bitsize, struct value *newval)
c906108c 848{
fc1a4b47 849 gdb_byte *addr = value_contents_writeable (var->value) + offset;
c906108c 850
c906108c
SS
851 if (bitsize)
852 modify_field (addr, value_as_long (newval),
853 bitpos, bitsize);
854 else
0fd88904 855 memcpy (addr, value_contents (newval), TYPE_LENGTH (value_type (newval)));
c906108c
SS
856}
857
858void
f23631e4 859set_internalvar (struct internalvar *var, struct value *val)
c906108c 860{
f23631e4 861 struct value *newval;
c906108c 862
c906108c
SS
863 newval = value_copy (val);
864 newval->modifiable = 1;
865
866 /* Force the value to be fetched from the target now, to avoid problems
867 later when this internalvar is referenced and the target is gone or
868 has changed. */
d69fe07e 869 if (value_lazy (newval))
c906108c
SS
870 value_fetch_lazy (newval);
871
872 /* Begin code which must not call error(). If var->value points to
873 something free'd, an error() obviously leaves a dangling pointer.
874 But we also get a danling pointer if var->value points to
875 something in the value chain (i.e., before release_value is
876 called), because after the error free_all_values will get called before
877 long. */
b8c9b27d 878 xfree (var->value);
c906108c 879 var->value = newval;
0d20ae72 880 var->endian = gdbarch_byte_order (current_gdbarch);
c906108c
SS
881 release_value (newval);
882 /* End code which must not call error(). */
883}
884
885char *
fba45db2 886internalvar_name (struct internalvar *var)
c906108c
SS
887{
888 return var->name;
889}
890
ae5a43e0
DJ
891/* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
892 prevent cycles / duplicates. */
893
894static void
895preserve_one_value (struct value *value, struct objfile *objfile,
896 htab_t copied_types)
897{
898 if (TYPE_OBJFILE (value->type) == objfile)
899 value->type = copy_type_recursive (objfile, value->type, copied_types);
900
901 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
902 value->enclosing_type = copy_type_recursive (objfile,
903 value->enclosing_type,
904 copied_types);
905}
906
907/* Update the internal variables and value history when OBJFILE is
908 discarded; we must copy the types out of the objfile. New global types
909 will be created for every convenience variable which currently points to
910 this objfile's types, and the convenience variables will be adjusted to
911 use the new global types. */
c906108c
SS
912
913void
ae5a43e0 914preserve_values (struct objfile *objfile)
c906108c 915{
ae5a43e0
DJ
916 htab_t copied_types;
917 struct value_history_chunk *cur;
52f0bd74 918 struct internalvar *var;
ae5a43e0 919 int i;
c906108c 920
ae5a43e0
DJ
921 /* Create the hash table. We allocate on the objfile's obstack, since
922 it is soon to be deleted. */
923 copied_types = create_copied_types_hash (objfile);
924
925 for (cur = value_history_chain; cur; cur = cur->next)
926 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
927 if (cur->values[i])
928 preserve_one_value (cur->values[i], objfile, copied_types);
929
930 for (var = internalvars; var; var = var->next)
931 preserve_one_value (var->value, objfile, copied_types);
932
933 htab_delete (copied_types);
c906108c
SS
934}
935
936static void
fba45db2 937show_convenience (char *ignore, int from_tty)
c906108c 938{
52f0bd74 939 struct internalvar *var;
c906108c
SS
940 int varseen = 0;
941
942 for (var = internalvars; var; var = var->next)
943 {
c906108c
SS
944 if (!varseen)
945 {
946 varseen = 1;
947 }
a3f17187 948 printf_filtered (("$%s = "), var->name);
d3c139e9
AS
949 value_print (value_of_internalvar (var), gdb_stdout,
950 0, Val_pretty_default);
a3f17187 951 printf_filtered (("\n"));
c906108c
SS
952 }
953 if (!varseen)
a3f17187
AC
954 printf_unfiltered (_("\
955No debugger convenience variables now defined.\n\
c906108c 956Convenience variables have names starting with \"$\";\n\
a3f17187 957use \"set\" as in \"set $foo = 5\" to define them.\n"));
c906108c
SS
958}
959\f
960/* Extract a value as a C number (either long or double).
961 Knows how to convert fixed values to double, or
962 floating values to long.
963 Does not deallocate the value. */
964
965LONGEST
f23631e4 966value_as_long (struct value *val)
c906108c
SS
967{
968 /* This coerces arrays and functions, which is necessary (e.g.
969 in disassemble_command). It also dereferences references, which
970 I suspect is the most logical thing to do. */
994b9211 971 val = coerce_array (val);
0fd88904 972 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
973}
974
975DOUBLEST
f23631e4 976value_as_double (struct value *val)
c906108c
SS
977{
978 DOUBLEST foo;
979 int inv;
c5aa993b 980
0fd88904 981 foo = unpack_double (value_type (val), value_contents (val), &inv);
c906108c 982 if (inv)
8a3fe4f8 983 error (_("Invalid floating value found in program."));
c906108c
SS
984 return foo;
985}
4ef30785 986
4478b372
JB
987/* Extract a value as a C pointer. Does not deallocate the value.
988 Note that val's type may not actually be a pointer; value_as_long
989 handles all the cases. */
c906108c 990CORE_ADDR
f23631e4 991value_as_address (struct value *val)
c906108c
SS
992{
993 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
994 whether we want this to be true eventually. */
995#if 0
bf6ae464 996 /* gdbarch_addr_bits_remove is wrong if we are being called for a
c906108c
SS
997 non-address (e.g. argument to "signal", "info break", etc.), or
998 for pointers to char, in which the low bits *are* significant. */
bf6ae464 999 return gdbarch_addr_bits_remove (current_gdbarch, value_as_long (val));
c906108c 1000#else
f312f057
JB
1001
1002 /* There are several targets (IA-64, PowerPC, and others) which
1003 don't represent pointers to functions as simply the address of
1004 the function's entry point. For example, on the IA-64, a
1005 function pointer points to a two-word descriptor, generated by
1006 the linker, which contains the function's entry point, and the
1007 value the IA-64 "global pointer" register should have --- to
1008 support position-independent code. The linker generates
1009 descriptors only for those functions whose addresses are taken.
1010
1011 On such targets, it's difficult for GDB to convert an arbitrary
1012 function address into a function pointer; it has to either find
1013 an existing descriptor for that function, or call malloc and
1014 build its own. On some targets, it is impossible for GDB to
1015 build a descriptor at all: the descriptor must contain a jump
1016 instruction; data memory cannot be executed; and code memory
1017 cannot be modified.
1018
1019 Upon entry to this function, if VAL is a value of type `function'
1020 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
1021 VALUE_ADDRESS (val) is the address of the function. This is what
1022 you'll get if you evaluate an expression like `main'. The call
1023 to COERCE_ARRAY below actually does all the usual unary
1024 conversions, which includes converting values of type `function'
1025 to `pointer to function'. This is the challenging conversion
1026 discussed above. Then, `unpack_long' will convert that pointer
1027 back into an address.
1028
1029 So, suppose the user types `disassemble foo' on an architecture
1030 with a strange function pointer representation, on which GDB
1031 cannot build its own descriptors, and suppose further that `foo'
1032 has no linker-built descriptor. The address->pointer conversion
1033 will signal an error and prevent the command from running, even
1034 though the next step would have been to convert the pointer
1035 directly back into the same address.
1036
1037 The following shortcut avoids this whole mess. If VAL is a
1038 function, just return its address directly. */
df407dfe
AC
1039 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1040 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
f312f057
JB
1041 return VALUE_ADDRESS (val);
1042
994b9211 1043 val = coerce_array (val);
fc0c74b1
AC
1044
1045 /* Some architectures (e.g. Harvard), map instruction and data
1046 addresses onto a single large unified address space. For
1047 instance: An architecture may consider a large integer in the
1048 range 0x10000000 .. 0x1000ffff to already represent a data
1049 addresses (hence not need a pointer to address conversion) while
1050 a small integer would still need to be converted integer to
1051 pointer to address. Just assume such architectures handle all
1052 integer conversions in a single function. */
1053
1054 /* JimB writes:
1055
1056 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1057 must admonish GDB hackers to make sure its behavior matches the
1058 compiler's, whenever possible.
1059
1060 In general, I think GDB should evaluate expressions the same way
1061 the compiler does. When the user copies an expression out of
1062 their source code and hands it to a `print' command, they should
1063 get the same value the compiler would have computed. Any
1064 deviation from this rule can cause major confusion and annoyance,
1065 and needs to be justified carefully. In other words, GDB doesn't
1066 really have the freedom to do these conversions in clever and
1067 useful ways.
1068
1069 AndrewC pointed out that users aren't complaining about how GDB
1070 casts integers to pointers; they are complaining that they can't
1071 take an address from a disassembly listing and give it to `x/i'.
1072 This is certainly important.
1073
79dd2d24 1074 Adding an architecture method like integer_to_address() certainly
fc0c74b1
AC
1075 makes it possible for GDB to "get it right" in all circumstances
1076 --- the target has complete control over how things get done, so
1077 people can Do The Right Thing for their target without breaking
1078 anyone else. The standard doesn't specify how integers get
1079 converted to pointers; usually, the ABI doesn't either, but
1080 ABI-specific code is a more reasonable place to handle it. */
1081
df407dfe
AC
1082 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
1083 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
79dd2d24
AC
1084 && gdbarch_integer_to_address_p (current_gdbarch))
1085 return gdbarch_integer_to_address (current_gdbarch, value_type (val),
0fd88904 1086 value_contents (val));
fc0c74b1 1087
0fd88904 1088 return unpack_long (value_type (val), value_contents (val));
c906108c
SS
1089#endif
1090}
1091\f
1092/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1093 as a long, or as a double, assuming the raw data is described
1094 by type TYPE. Knows how to convert different sizes of values
1095 and can convert between fixed and floating point. We don't assume
1096 any alignment for the raw data. Return value is in host byte order.
1097
1098 If you want functions and arrays to be coerced to pointers, and
1099 references to be dereferenced, call value_as_long() instead.
1100
1101 C++: It is assumed that the front-end has taken care of
1102 all matters concerning pointers to members. A pointer
1103 to member which reaches here is considered to be equivalent
1104 to an INT (or some size). After all, it is only an offset. */
1105
1106LONGEST
fc1a4b47 1107unpack_long (struct type *type, const gdb_byte *valaddr)
c906108c 1108{
52f0bd74
AC
1109 enum type_code code = TYPE_CODE (type);
1110 int len = TYPE_LENGTH (type);
1111 int nosign = TYPE_UNSIGNED (type);
c906108c 1112
c906108c
SS
1113 switch (code)
1114 {
1115 case TYPE_CODE_TYPEDEF:
1116 return unpack_long (check_typedef (type), valaddr);
1117 case TYPE_CODE_ENUM:
4f2aea11 1118 case TYPE_CODE_FLAGS:
c906108c
SS
1119 case TYPE_CODE_BOOL:
1120 case TYPE_CODE_INT:
1121 case TYPE_CODE_CHAR:
1122 case TYPE_CODE_RANGE:
0d5de010 1123 case TYPE_CODE_MEMBERPTR:
c906108c
SS
1124 if (nosign)
1125 return extract_unsigned_integer (valaddr, len);
1126 else
1127 return extract_signed_integer (valaddr, len);
1128
1129 case TYPE_CODE_FLT:
96d2f608 1130 return extract_typed_floating (valaddr, type);
c906108c 1131
4ef30785
TJB
1132 case TYPE_CODE_DECFLOAT:
1133 /* libdecnumber has a function to convert from decimal to integer, but
1134 it doesn't work when the decimal number has a fractional part. */
ba759613 1135 return decimal_to_doublest (valaddr, len);
4ef30785 1136
c906108c
SS
1137 case TYPE_CODE_PTR:
1138 case TYPE_CODE_REF:
1139 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
c5aa993b 1140 whether we want this to be true eventually. */
4478b372 1141 return extract_typed_address (valaddr, type);
c906108c 1142
c906108c 1143 default:
8a3fe4f8 1144 error (_("Value can't be converted to integer."));
c906108c 1145 }
c5aa993b 1146 return 0; /* Placate lint. */
c906108c
SS
1147}
1148
1149/* Return a double value from the specified type and address.
1150 INVP points to an int which is set to 0 for valid value,
1151 1 for invalid value (bad float format). In either case,
1152 the returned double is OK to use. Argument is in target
1153 format, result is in host format. */
1154
1155DOUBLEST
fc1a4b47 1156unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
c906108c
SS
1157{
1158 enum type_code code;
1159 int len;
1160 int nosign;
1161
1162 *invp = 0; /* Assume valid. */
1163 CHECK_TYPEDEF (type);
1164 code = TYPE_CODE (type);
1165 len = TYPE_LENGTH (type);
1166 nosign = TYPE_UNSIGNED (type);
1167 if (code == TYPE_CODE_FLT)
1168 {
75bc7ddf
AC
1169 /* NOTE: cagney/2002-02-19: There was a test here to see if the
1170 floating-point value was valid (using the macro
1171 INVALID_FLOAT). That test/macro have been removed.
1172
1173 It turns out that only the VAX defined this macro and then
1174 only in a non-portable way. Fixing the portability problem
1175 wouldn't help since the VAX floating-point code is also badly
1176 bit-rotten. The target needs to add definitions for the
ea06eb3d 1177 methods gdbarch_float_format and gdbarch_double_format - these
75bc7ddf
AC
1178 exactly describe the target floating-point format. The
1179 problem here is that the corresponding floatformat_vax_f and
1180 floatformat_vax_d values these methods should be set to are
1181 also not defined either. Oops!
1182
1183 Hopefully someone will add both the missing floatformat
ac79b88b
DJ
1184 definitions and the new cases for floatformat_is_valid (). */
1185
1186 if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
1187 {
1188 *invp = 1;
1189 return 0.0;
1190 }
1191
96d2f608 1192 return extract_typed_floating (valaddr, type);
c906108c 1193 }
4ef30785 1194 else if (code == TYPE_CODE_DECFLOAT)
ba759613 1195 return decimal_to_doublest (valaddr, len);
c906108c
SS
1196 else if (nosign)
1197 {
1198 /* Unsigned -- be sure we compensate for signed LONGEST. */
c906108c 1199 return (ULONGEST) unpack_long (type, valaddr);
c906108c
SS
1200 }
1201 else
1202 {
1203 /* Signed -- we are OK with unpack_long. */
1204 return unpack_long (type, valaddr);
1205 }
1206}
1207
1208/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1209 as a CORE_ADDR, assuming the raw data is described by type TYPE.
1210 We don't assume any alignment for the raw data. Return value is in
1211 host byte order.
1212
1213 If you want functions and arrays to be coerced to pointers, and
1aa20aa8 1214 references to be dereferenced, call value_as_address() instead.
c906108c
SS
1215
1216 C++: It is assumed that the front-end has taken care of
1217 all matters concerning pointers to members. A pointer
1218 to member which reaches here is considered to be equivalent
1219 to an INT (or some size). After all, it is only an offset. */
1220
1221CORE_ADDR
fc1a4b47 1222unpack_pointer (struct type *type, const gdb_byte *valaddr)
c906108c
SS
1223{
1224 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1225 whether we want this to be true eventually. */
1226 return unpack_long (type, valaddr);
1227}
4478b372 1228
c906108c 1229\f
2c2738a0
DC
1230/* Get the value of the FIELDN'th field (which must be static) of
1231 TYPE. Return NULL if the field doesn't exist or has been
1232 optimized out. */
c906108c 1233
f23631e4 1234struct value *
fba45db2 1235value_static_field (struct type *type, int fieldno)
c906108c 1236{
948e66d9
DJ
1237 struct value *retval;
1238
c906108c
SS
1239 if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno))
1240 {
948e66d9 1241 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
00a4c844 1242 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
c906108c
SS
1243 }
1244 else
1245 {
1246 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
2570f2b7 1247 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
948e66d9 1248 if (sym == NULL)
c906108c
SS
1249 {
1250 /* With some compilers, e.g. HP aCC, static data members are reported
c5aa993b
JM
1251 as non-debuggable symbols */
1252 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
c906108c
SS
1253 if (!msym)
1254 return NULL;
1255 else
c5aa993b 1256 {
948e66d9 1257 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
00a4c844 1258 SYMBOL_VALUE_ADDRESS (msym));
c906108c
SS
1259 }
1260 }
1261 else
1262 {
948e66d9
DJ
1263 /* SYM should never have a SYMBOL_CLASS which will require
1264 read_var_value to use the FRAME parameter. */
1265 if (symbol_read_needs_frame (sym))
8a3fe4f8
AC
1266 warning (_("static field's value depends on the current "
1267 "frame - bad debug info?"));
948e66d9 1268 retval = read_var_value (sym, NULL);
2b127877 1269 }
948e66d9
DJ
1270 if (retval && VALUE_LVAL (retval) == lval_memory)
1271 SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
1272 VALUE_ADDRESS (retval));
c906108c 1273 }
948e66d9 1274 return retval;
c906108c
SS
1275}
1276
2b127877
DB
1277/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1278 You have to be careful here, since the size of the data area for the value
1279 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
1280 than the old enclosing type, you have to allocate more space for the data.
1281 The return value is a pointer to the new version of this value structure. */
1282
f23631e4
AC
1283struct value *
1284value_change_enclosing_type (struct value *val, struct type *new_encl_type)
2b127877 1285{
4754a64e 1286 if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (value_enclosing_type (val)))
2b127877 1287 {
4754a64e 1288 val->enclosing_type = new_encl_type;
2b127877
DB
1289 return val;
1290 }
1291 else
1292 {
f23631e4
AC
1293 struct value *new_val;
1294 struct value *prev;
2b127877 1295
f23631e4 1296 new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type));
cc303028 1297
4754a64e 1298 new_val->enclosing_type = new_encl_type;
cc303028 1299
2b127877
DB
1300 /* We have to make sure this ends up in the same place in the value
1301 chain as the original copy, so it's clean-up behavior is the same.
1302 If the value has been released, this is a waste of time, but there
1303 is no way to tell that in advance, so... */
1304
1305 if (val != all_values)
1306 {
1307 for (prev = all_values; prev != NULL; prev = prev->next)
1308 {
1309 if (prev->next == val)
1310 {
1311 prev->next = new_val;
1312 break;
1313 }
1314 }
1315 }
1316
1317 return new_val;
1318 }
1319}
1320
c906108c
SS
1321/* Given a value ARG1 (offset by OFFSET bytes)
1322 of a struct or union type ARG_TYPE,
1323 extract and return the value of one of its (non-static) fields.
1324 FIELDNO says which field. */
1325
f23631e4
AC
1326struct value *
1327value_primitive_field (struct value *arg1, int offset,
aa1ee363 1328 int fieldno, struct type *arg_type)
c906108c 1329{
f23631e4 1330 struct value *v;
52f0bd74 1331 struct type *type;
c906108c
SS
1332
1333 CHECK_TYPEDEF (arg_type);
1334 type = TYPE_FIELD_TYPE (arg_type, fieldno);
1335
1336 /* Handle packed fields */
1337
1338 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
1339 {
1340 v = value_from_longest (type,
1341 unpack_field_as_long (arg_type,
0fd88904 1342 value_contents (arg1)
c5aa993b 1343 + offset,
c906108c 1344 fieldno));
df407dfe
AC
1345 v->bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
1346 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
1347 v->offset = value_offset (arg1) + offset
2e70b7b9 1348 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
c906108c
SS
1349 }
1350 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
1351 {
1352 /* This field is actually a base subobject, so preserve the
1353 entire object's contents for later references to virtual
1354 bases, etc. */
4754a64e 1355 v = allocate_value (value_enclosing_type (arg1));
df407dfe 1356 v->type = type;
a4e2ee12
DJ
1357
1358 /* Lazy register values with offsets are not supported. */
1359 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
1360 value_fetch_lazy (arg1);
1361
1362 if (value_lazy (arg1))
dfa52d88 1363 set_value_lazy (v, 1);
c906108c 1364 else
990a07ab 1365 memcpy (value_contents_all_raw (v), value_contents_all_raw (arg1),
4754a64e 1366 TYPE_LENGTH (value_enclosing_type (arg1)));
df407dfe 1367 v->offset = value_offset (arg1);
13c3b5f5
AC
1368 v->embedded_offset = (offset + value_embedded_offset (arg1)
1369 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
c906108c
SS
1370 }
1371 else
1372 {
1373 /* Plain old data member */
1374 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
1375 v = allocate_value (type);
a4e2ee12
DJ
1376
1377 /* Lazy register values with offsets are not supported. */
1378 if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
1379 value_fetch_lazy (arg1);
1380
1381 if (value_lazy (arg1))
dfa52d88 1382 set_value_lazy (v, 1);
c906108c 1383 else
990a07ab
AC
1384 memcpy (value_contents_raw (v),
1385 value_contents_raw (arg1) + offset,
c906108c 1386 TYPE_LENGTH (type));
df407dfe 1387 v->offset = (value_offset (arg1) + offset
13c3b5f5 1388 + value_embedded_offset (arg1));
c906108c
SS
1389 }
1390 VALUE_LVAL (v) = VALUE_LVAL (arg1);
1391 if (VALUE_LVAL (arg1) == lval_internalvar)
1392 VALUE_LVAL (v) = lval_internalvar_component;
7d85ee02 1393 v->location = arg1->location;
9ee8fc9d 1394 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
0c16dd26 1395 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
c906108c
SS
1396 return v;
1397}
1398
1399/* Given a value ARG1 of a struct or union type,
1400 extract and return the value of one of its (non-static) fields.
1401 FIELDNO says which field. */
1402
f23631e4 1403struct value *
aa1ee363 1404value_field (struct value *arg1, int fieldno)
c906108c 1405{
df407dfe 1406 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
c906108c
SS
1407}
1408
1409/* Return a non-virtual function as a value.
1410 F is the list of member functions which contains the desired method.
0478d61c
FF
1411 J is an index into F which provides the desired method.
1412
1413 We only use the symbol for its address, so be happy with either a
1414 full symbol or a minimal symbol.
1415 */
c906108c 1416
f23631e4
AC
1417struct value *
1418value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
fba45db2 1419 int offset)
c906108c 1420{
f23631e4 1421 struct value *v;
52f0bd74 1422 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
0478d61c 1423 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c906108c 1424 struct symbol *sym;
0478d61c 1425 struct minimal_symbol *msym;
c906108c 1426
2570f2b7 1427 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
5ae326fa 1428 if (sym != NULL)
0478d61c 1429 {
5ae326fa
AC
1430 msym = NULL;
1431 }
1432 else
1433 {
1434 gdb_assert (sym == NULL);
0478d61c 1435 msym = lookup_minimal_symbol (physname, NULL, NULL);
5ae326fa
AC
1436 if (msym == NULL)
1437 return NULL;
0478d61c
FF
1438 }
1439
c906108c 1440 v = allocate_value (ftype);
0478d61c
FF
1441 if (sym)
1442 {
1443 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1444 }
1445 else
1446 {
bccdca4a
UW
1447 /* The minimal symbol might point to a function descriptor;
1448 resolve it to the actual code address instead. */
1449 struct objfile *objfile = msymbol_objfile (msym);
1450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1451
1452 VALUE_ADDRESS (v)
1453 = gdbarch_convert_from_func_ptr_addr
1454 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), &current_target);
0478d61c 1455 }
c906108c
SS
1456
1457 if (arg1p)
c5aa993b 1458 {
df407dfe 1459 if (type != value_type (*arg1p))
c5aa993b
JM
1460 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
1461 value_addr (*arg1p)));
1462
070ad9f0 1463 /* Move the `this' pointer according to the offset.
c5aa993b
JM
1464 VALUE_OFFSET (*arg1p) += offset;
1465 */
c906108c
SS
1466 }
1467
1468 return v;
1469}
1470
c906108c
SS
1471\f
1472/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1473 VALADDR.
1474
1475 Extracting bits depends on endianness of the machine. Compute the
1476 number of least significant bits to discard. For big endian machines,
1477 we compute the total number of bits in the anonymous object, subtract
1478 off the bit count from the MSB of the object to the MSB of the
1479 bitfield, then the size of the bitfield, which leaves the LSB discard
1480 count. For little endian machines, the discard count is simply the
1481 number of bits from the LSB of the anonymous object to the LSB of the
1482 bitfield.
1483
1484 If the field is signed, we also do sign extension. */
1485
1486LONGEST
fc1a4b47 1487unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
c906108c
SS
1488{
1489 ULONGEST val;
1490 ULONGEST valmask;
1491 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1492 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1493 int lsbcount;
1494 struct type *field_type;
1495
1496 val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1497 field_type = TYPE_FIELD_TYPE (type, fieldno);
1498 CHECK_TYPEDEF (field_type);
1499
1500 /* Extract bits. See comment above. */
1501
32c9a795 1502 if (gdbarch_bits_big_endian (current_gdbarch))
c906108c
SS
1503 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1504 else
1505 lsbcount = (bitpos % 8);
1506 val >>= lsbcount;
1507
1508 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1509 If the field is signed, and is negative, then sign extend. */
1510
1511 if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
1512 {
1513 valmask = (((ULONGEST) 1) << bitsize) - 1;
1514 val &= valmask;
1515 if (!TYPE_UNSIGNED (field_type))
1516 {
1517 if (val & (valmask ^ (valmask >> 1)))
1518 {
1519 val |= ~valmask;
1520 }
1521 }
1522 }
1523 return (val);
1524}
1525
1526/* Modify the value of a bitfield. ADDR points to a block of memory in
1527 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1528 is the desired value of the field, in host byte order. BITPOS and BITSIZE
f4e88c8e
PH
1529 indicate which bits (in target bit order) comprise the bitfield.
1530 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
1531 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
c906108c
SS
1532
1533void
fc1a4b47 1534modify_field (gdb_byte *addr, LONGEST fieldval, int bitpos, int bitsize)
c906108c 1535{
f4e88c8e
PH
1536 ULONGEST oword;
1537 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
c906108c
SS
1538
1539 /* If a negative fieldval fits in the field in question, chop
1540 off the sign extension bits. */
f4e88c8e
PH
1541 if ((~fieldval & ~(mask >> 1)) == 0)
1542 fieldval &= mask;
c906108c
SS
1543
1544 /* Warn if value is too big to fit in the field in question. */
f4e88c8e 1545 if (0 != (fieldval & ~mask))
c906108c
SS
1546 {
1547 /* FIXME: would like to include fieldval in the message, but
c5aa993b 1548 we don't have a sprintf_longest. */
8a3fe4f8 1549 warning (_("Value does not fit in %d bits."), bitsize);
c906108c
SS
1550
1551 /* Truncate it, otherwise adjoining fields may be corrupted. */
f4e88c8e 1552 fieldval &= mask;
c906108c
SS
1553 }
1554
f4e88c8e 1555 oword = extract_unsigned_integer (addr, sizeof oword);
c906108c
SS
1556
1557 /* Shifting for bit field depends on endianness of the target machine. */
32c9a795 1558 if (gdbarch_bits_big_endian (current_gdbarch))
c906108c
SS
1559 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1560
f4e88c8e 1561 oword &= ~(mask << bitpos);
c906108c
SS
1562 oword |= fieldval << bitpos;
1563
f4e88c8e 1564 store_unsigned_integer (addr, sizeof oword, oword);
c906108c
SS
1565}
1566\f
14d06750 1567/* Pack NUM into BUF using a target format of TYPE. */
c906108c 1568
14d06750
DJ
1569void
1570pack_long (gdb_byte *buf, struct type *type, LONGEST num)
c906108c 1571{
52f0bd74 1572 int len;
14d06750
DJ
1573
1574 type = check_typedef (type);
c906108c
SS
1575 len = TYPE_LENGTH (type);
1576
14d06750 1577 switch (TYPE_CODE (type))
c906108c 1578 {
c906108c
SS
1579 case TYPE_CODE_INT:
1580 case TYPE_CODE_CHAR:
1581 case TYPE_CODE_ENUM:
4f2aea11 1582 case TYPE_CODE_FLAGS:
c906108c
SS
1583 case TYPE_CODE_BOOL:
1584 case TYPE_CODE_RANGE:
0d5de010 1585 case TYPE_CODE_MEMBERPTR:
14d06750 1586 store_signed_integer (buf, len, num);
c906108c 1587 break;
c5aa993b 1588
c906108c
SS
1589 case TYPE_CODE_REF:
1590 case TYPE_CODE_PTR:
14d06750 1591 store_typed_address (buf, type, (CORE_ADDR) num);
c906108c 1592 break;
c5aa993b 1593
c906108c 1594 default:
14d06750
DJ
1595 error (_("Unexpected type (%d) encountered for integer constant."),
1596 TYPE_CODE (type));
c906108c 1597 }
14d06750
DJ
1598}
1599
1600
1601/* Convert C numbers into newly allocated values. */
1602
1603struct value *
1604value_from_longest (struct type *type, LONGEST num)
1605{
1606 struct value *val = allocate_value (type);
1607
1608 pack_long (value_contents_raw (val), type, num);
1609
c906108c
SS
1610 return val;
1611}
1612
4478b372
JB
1613
1614/* Create a value representing a pointer of type TYPE to the address
1615 ADDR. */
f23631e4 1616struct value *
4478b372
JB
1617value_from_pointer (struct type *type, CORE_ADDR addr)
1618{
f23631e4 1619 struct value *val = allocate_value (type);
990a07ab 1620 store_typed_address (value_contents_raw (val), type, addr);
4478b372
JB
1621 return val;
1622}
1623
1624
0f71a2f6 1625/* Create a value for a string constant to be stored locally
070ad9f0 1626 (not in the inferior's memory space, but in GDB memory).
0f71a2f6
JM
1627 This is analogous to value_from_longest, which also does not
1628 use inferior memory. String shall NOT contain embedded nulls. */
1629
f23631e4 1630struct value *
fba45db2 1631value_from_string (char *ptr)
0f71a2f6 1632{
f23631e4 1633 struct value *val;
c5aa993b 1634 int len = strlen (ptr);
0f71a2f6 1635 int lowbound = current_language->string_lower_bound;
f290d38e
AC
1636 struct type *string_char_type;
1637 struct type *rangetype;
1638 struct type *stringtype;
1639
1640 rangetype = create_range_type ((struct type *) NULL,
6d84d3d8 1641 builtin_type_int32,
f290d38e
AC
1642 lowbound, len + lowbound - 1);
1643 string_char_type = language_string_char_type (current_language,
1644 current_gdbarch);
1645 stringtype = create_array_type ((struct type *) NULL,
1646 string_char_type,
1647 rangetype);
0f71a2f6 1648 val = allocate_value (stringtype);
990a07ab 1649 memcpy (value_contents_raw (val), ptr, len);
0f71a2f6
JM
1650 return val;
1651}
1652
f23631e4 1653struct value *
fba45db2 1654value_from_double (struct type *type, DOUBLEST num)
c906108c 1655{
f23631e4 1656 struct value *val = allocate_value (type);
c906108c 1657 struct type *base_type = check_typedef (type);
52f0bd74
AC
1658 enum type_code code = TYPE_CODE (base_type);
1659 int len = TYPE_LENGTH (base_type);
c906108c
SS
1660
1661 if (code == TYPE_CODE_FLT)
1662 {
990a07ab 1663 store_typed_floating (value_contents_raw (val), base_type, num);
c906108c
SS
1664 }
1665 else
8a3fe4f8 1666 error (_("Unexpected type encountered for floating constant."));
c906108c
SS
1667
1668 return val;
1669}
994b9211 1670
27bc4d80 1671struct value *
4ef30785 1672value_from_decfloat (struct type *type, const gdb_byte *dec)
27bc4d80
TJB
1673{
1674 struct value *val = allocate_value (type);
27bc4d80 1675
4ef30785 1676 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
27bc4d80 1677
27bc4d80
TJB
1678 return val;
1679}
1680
994b9211
AC
1681struct value *
1682coerce_ref (struct value *arg)
1683{
df407dfe 1684 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
994b9211
AC
1685 if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)
1686 arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
df407dfe 1687 unpack_pointer (value_type (arg),
0fd88904 1688 value_contents (arg)));
994b9211
AC
1689 return arg;
1690}
1691
1692struct value *
1693coerce_array (struct value *arg)
1694{
1695 arg = coerce_ref (arg);
1696 if (current_language->c_style_arrays
df407dfe 1697 && TYPE_CODE (value_type (arg)) == TYPE_CODE_ARRAY)
994b9211 1698 arg = value_coerce_array (arg);
df407dfe 1699 if (TYPE_CODE (value_type (arg)) == TYPE_CODE_FUNC)
994b9211 1700 arg = value_coerce_function (arg);
994b9211
AC
1701 return arg;
1702}
c906108c 1703\f
c906108c 1704
48436ce6
AC
1705/* Return true if the function returning the specified type is using
1706 the convention of returning structures in memory (passing in the
82585c72 1707 address as a hidden first parameter). */
c906108c
SS
1708
1709int
c055b101 1710using_struct_return (struct type *func_type, struct type *value_type)
c906108c 1711{
52f0bd74 1712 enum type_code code = TYPE_CODE (value_type);
c906108c
SS
1713
1714 if (code == TYPE_CODE_ERROR)
8a3fe4f8 1715 error (_("Function return type unknown."));
c906108c 1716
667e784f
AC
1717 if (code == TYPE_CODE_VOID)
1718 /* A void return value is never in memory. See also corresponding
44e5158b 1719 code in "print_return_value". */
667e784f
AC
1720 return 0;
1721
92ad9cd9 1722 /* Probe the architecture for the return-value convention. */
c055b101 1723 return (gdbarch_return_value (current_gdbarch, func_type, value_type,
92ad9cd9 1724 NULL, NULL, NULL)
31db7b6c 1725 != RETURN_VALUE_REGISTER_CONVENTION);
c906108c
SS
1726}
1727
42be36b3
CT
1728/* Set the initialized field in a value struct. */
1729
1730void
1731set_value_initialized (struct value *val, int status)
1732{
1733 val->initialized = status;
1734}
1735
1736/* Return the initialized field in a value struct. */
1737
1738int
1739value_initialized (struct value *val)
1740{
1741 return val->initialized;
1742}
1743
c906108c 1744void
fba45db2 1745_initialize_values (void)
c906108c 1746{
1a966eab
AC
1747 add_cmd ("convenience", no_class, show_convenience, _("\
1748Debugger convenience (\"$foo\") variables.\n\
c906108c 1749These variables are created when you assign them values;\n\
1a966eab
AC
1750thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
1751\n\
c906108c
SS
1752A few convenience variables are given values automatically:\n\
1753\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1a966eab 1754\"$__\" holds the contents of the last address examined with \"x\"."),
c906108c
SS
1755 &showlist);
1756
1757 add_cmd ("values", no_class, show_values,
1a966eab 1758 _("Elements of value history around item number IDX (or last ten)."),
c906108c 1759 &showlist);
53e5f3cf
AS
1760
1761 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
1762Initialize a convenience variable if necessary.\n\
1763init-if-undefined VARIABLE = EXPRESSION\n\
1764Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
1765exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
1766VARIABLE is already initialized."));
c906108c 1767}
This page took 0.94449 seconds and 4 git commands to generate.