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