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