1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
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
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
36 #include "gdb_assert.h"
40 /* Prototypes for exported functions. */
42 void _initialize_values (void);
46 /* Type of value; either not an lval, or one of the various
47 different possible kinds of lval. */
50 /* Is it modifiable? Only relevant if lval != not_lval. */
53 /* Location of value (if lval). */
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. */
61 /* Pointer to internal variable. */
62 struct internalvar
*internalvar
;
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. */
72 /* Only used for bitfields; number of bits contained in them. */
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. */
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
;
84 /* Type of the value. */
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
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:
100 - When printing the value, the user would probably rather see the
101 full object, not just the limited portion apparent from the
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.
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.
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.)
124 If we're not doing anything fancy, `enclosing_type' is equal to
125 `type', and `embedded_offset' is zero, so everything works
127 struct type
*enclosing_type
;
129 int pointed_to_offset
;
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
137 /* Register number if the value is from a register. */
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).
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! */
156 /* If nonzero, this is the value of a variable which does not
157 actually exist in the program. */
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
167 gdb_byte contents
[1];
168 DOUBLEST force_doublest_align
;
169 LONGEST force_longest_align
;
170 CORE_ADDR force_core_addr_align
;
171 void *force_pointer_align
;
173 /* Do not add any new members here -- contents above will trash
177 /* Prototypes for local functions. */
179 static void show_values (char *, int);
181 static void show_convenience (char *, int);
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. */
190 #define VALUE_HISTORY_CHUNK 60
192 struct value_history_chunk
194 struct value_history_chunk
*next
;
195 struct value
*values
[VALUE_HISTORY_CHUNK
];
198 /* Chain of chunks now in use. */
200 static struct value_history_chunk
*value_history_chain
;
202 static int value_history_count
; /* Abs number of last entry stored */
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. */
208 static struct value
*all_values
;
210 /* Allocate a value that has the correct length for type TYPE. */
213 allocate_value (struct type
*type
)
216 struct type
*atype
= check_typedef (type
);
218 val
= (struct value
*) xzalloc (sizeof (struct value
) + TYPE_LENGTH (atype
));
219 val
->next
= all_values
;
222 val
->enclosing_type
= type
;
223 VALUE_LVAL (val
) = not_lval
;
224 VALUE_ADDRESS (val
) = 0;
225 VALUE_FRAME_ID (val
) = null_frame_id
;
229 VALUE_REGNUM (val
) = -1;
231 val
->optimized_out
= 0;
232 val
->embedded_offset
= 0;
233 val
->pointed_to_offset
= 0;
238 /* Allocate a value that has the correct length
239 for COUNT repetitions type TYPE. */
242 allocate_repeat_value (struct type
*type
, int count
)
244 int low_bound
= current_language
->string_lower_bound
; /* ??? */
245 /* FIXME-type-allocation: need a way to free this type when we are
247 struct type
*range_type
248 = create_range_type ((struct type
*) NULL
, builtin_type_int
,
249 low_bound
, count
+ low_bound
- 1);
250 /* FIXME-type-allocation: need a way to free this type when we are
252 return allocate_value (create_array_type ((struct type
*) NULL
,
256 /* Accessor methods. */
259 value_next (struct value
*value
)
265 value_type (struct value
*value
)
270 deprecated_set_value_type (struct value
*value
, struct type
*type
)
276 value_offset (struct value
*value
)
278 return value
->offset
;
281 set_value_offset (struct value
*value
, int offset
)
283 value
->offset
= offset
;
287 value_bitpos (struct value
*value
)
289 return value
->bitpos
;
292 set_value_bitpos (struct value
*value
, int bit
)
298 value_bitsize (struct value
*value
)
300 return value
->bitsize
;
303 set_value_bitsize (struct value
*value
, int bit
)
305 value
->bitsize
= bit
;
309 value_contents_raw (struct value
*value
)
311 return value
->aligner
.contents
+ value
->embedded_offset
;
315 value_contents_all_raw (struct value
*value
)
317 return value
->aligner
.contents
;
321 value_enclosing_type (struct value
*value
)
323 return value
->enclosing_type
;
327 value_contents_all (struct value
*value
)
330 value_fetch_lazy (value
);
331 return value
->aligner
.contents
;
335 value_lazy (struct value
*value
)
341 set_value_lazy (struct value
*value
, int val
)
347 value_contents (struct value
*value
)
349 return value_contents_writeable (value
);
353 value_contents_writeable (struct value
*value
)
356 value_fetch_lazy (value
);
357 return value_contents_raw (value
);
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. */
365 value_contents_equal (struct value
*val1
, struct value
*val2
)
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
))
377 return (memcmp (value_contents (val1
), value_contents (val2
), len
) == 0);
381 value_optimized_out (struct value
*value
)
383 return value
->optimized_out
;
387 set_value_optimized_out (struct value
*value
, int val
)
389 value
->optimized_out
= val
;
393 value_embedded_offset (struct value
*value
)
395 return value
->embedded_offset
;
399 set_value_embedded_offset (struct value
*value
, int val
)
401 value
->embedded_offset
= val
;
405 value_pointed_to_offset (struct value
*value
)
407 return value
->pointed_to_offset
;
411 set_value_pointed_to_offset (struct value
*value
, int val
)
413 value
->pointed_to_offset
= val
;
417 deprecated_value_lval_hack (struct value
*value
)
423 deprecated_value_address_hack (struct value
*value
)
425 return &value
->location
.address
;
428 struct internalvar
**
429 deprecated_value_internalvar_hack (struct value
*value
)
431 return &value
->location
.internalvar
;
435 deprecated_value_frame_id_hack (struct value
*value
)
437 return &value
->frame_id
;
441 deprecated_value_regnum_hack (struct value
*value
)
443 return &value
->regnum
;
447 deprecated_value_modifiable (struct value
*value
)
449 return value
->modifiable
;
452 deprecated_set_value_modifiable (struct value
*value
, int modifiable
)
454 value
->modifiable
= modifiable
;
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. */
466 /* Free all values allocated since MARK was obtained by value_mark
467 (except for those released). */
469 value_free_to_mark (struct value
*mark
)
474 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
482 /* Free all the values that have been allocated (except for those released).
483 Called after each command, successful or not. */
486 free_all_values (void)
491 for (val
= all_values
; val
; val
= next
)
500 /* Remove VAL from the chain all_values
501 so it will not be freed automatically. */
504 release_value (struct value
*val
)
508 if (all_values
== val
)
510 all_values
= val
->next
;
514 for (v
= all_values
; v
; v
= v
->next
)
524 /* Release all values up to mark */
526 value_release_to_mark (struct value
*mark
)
531 for (val
= next
= all_values
; next
; next
= next
->next
)
532 if (next
->next
== mark
)
534 all_values
= next
->next
;
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. */
547 value_copy (struct value
*arg
)
549 struct type
*encl_type
= value_enclosing_type (arg
);
550 struct value
*val
= allocate_value (encl_type
);
551 val
->type
= arg
->type
;
552 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
553 val
->location
= arg
->location
;
554 val
->offset
= arg
->offset
;
555 val
->bitpos
= arg
->bitpos
;
556 val
->bitsize
= arg
->bitsize
;
557 VALUE_FRAME_ID (val
) = VALUE_FRAME_ID (arg
);
558 VALUE_REGNUM (val
) = VALUE_REGNUM (arg
);
559 val
->lazy
= arg
->lazy
;
560 val
->optimized_out
= arg
->optimized_out
;
561 val
->embedded_offset
= value_embedded_offset (arg
);
562 val
->pointed_to_offset
= arg
->pointed_to_offset
;
563 val
->modifiable
= arg
->modifiable
;
564 if (!value_lazy (val
))
566 memcpy (value_contents_all_raw (val
), value_contents_all_raw (arg
),
567 TYPE_LENGTH (value_enclosing_type (arg
)));
573 /* Access to the value history. */
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. */
581 record_latest_value (struct value
*val
)
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. */
589 if (value_lazy (val
))
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... */
597 /* Here we treat value_history_count as origin-zero
598 and applying to the value being stored now. */
600 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
603 struct value_history_chunk
*new
604 = (struct value_history_chunk
*)
605 xmalloc (sizeof (struct value_history_chunk
));
606 memset (new->values
, 0, sizeof new->values
);
607 new->next
= value_history_chain
;
608 value_history_chain
= new;
611 value_history_chain
->values
[i
] = val
;
613 /* Now we regard value_history_count as origin-one
614 and applying to the value just stored. */
616 return ++value_history_count
;
619 /* Return a copy of the value in the history with sequence number NUM. */
622 access_value_history (int num
)
624 struct value_history_chunk
*chunk
;
629 absnum
+= value_history_count
;
634 error (_("The history is empty."));
636 error (_("There is only one value in the history."));
638 error (_("History does not go back to $$%d."), -num
);
640 if (absnum
> value_history_count
)
641 error (_("History has not yet reached $%d."), absnum
);
645 /* Now absnum is always absolute and origin zero. */
647 chunk
= value_history_chain
;
648 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
652 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
656 show_values (char *num_exp
, int from_tty
)
664 /* "info history +" should print from the stored position.
665 "info history <exp>" should print around value number <exp>. */
666 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
667 num
= parse_and_eval_long (num_exp
) - 5;
671 /* "info history" means print the last 10 values. */
672 num
= value_history_count
- 9;
678 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
680 val
= access_value_history (i
);
681 printf_filtered (("$%d = "), i
);
682 value_print (val
, gdb_stdout
, 0, Val_pretty_default
);
683 printf_filtered (("\n"));
686 /* The next "info history +" should start after what we just printed. */
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
)
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. */
704 static struct internalvar
*internalvars
;
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. */
709 init_if_undefined_command (char* args
, int from_tty
)
711 struct internalvar
* intvar
;
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
);
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."));
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
;
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
);
735 do_cleanups (old_chain
);
739 /* Look up an internal variable with name NAME. NAME should not
740 normally include a dollar sign.
742 If the specified internal variable does not exist,
743 one is created, with a void value. */
746 lookup_internalvar (char *name
)
748 struct internalvar
*var
;
750 for (var
= internalvars
; var
; var
= var
->next
)
751 if (strcmp (var
->name
, name
) == 0)
754 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
755 var
->name
= concat (name
, (char *)NULL
);
756 var
->value
= allocate_value (builtin_type_void
);
757 var
->endian
= TARGET_BYTE_ORDER
;
758 release_value (var
->value
);
759 var
->next
= internalvars
;
765 value_of_internalvar (struct internalvar
*var
)
771 val
= value_copy (var
->value
);
772 if (value_lazy (val
))
773 value_fetch_lazy (val
);
774 VALUE_LVAL (val
) = lval_internalvar
;
775 VALUE_INTERNALVAR (val
) = var
;
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.
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.
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
789 if (var
->endian
!= TARGET_BYTE_ORDER
)
791 gdb_byte
*array
= value_contents_raw (val
);
792 struct type
*type
= check_typedef (value_enclosing_type (val
));
793 switch (TYPE_CODE (type
))
797 /* Reverse the bytes. */
798 for (i
= 0, j
= TYPE_LENGTH (type
) - 1; i
< j
; i
++, j
--)
812 set_internalvar_component (struct internalvar
*var
, int offset
, int bitpos
,
813 int bitsize
, struct value
*newval
)
815 gdb_byte
*addr
= value_contents_writeable (var
->value
) + offset
;
818 modify_field (addr
, value_as_long (newval
),
821 memcpy (addr
, value_contents (newval
), TYPE_LENGTH (value_type (newval
)));
825 set_internalvar (struct internalvar
*var
, struct value
*val
)
827 struct value
*newval
;
829 newval
= value_copy (val
);
830 newval
->modifiable
= 1;
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
835 if (value_lazy (newval
))
836 value_fetch_lazy (newval
);
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
846 var
->endian
= TARGET_BYTE_ORDER
;
847 release_value (newval
);
848 /* End code which must not call error(). */
852 internalvar_name (struct internalvar
*var
)
857 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
858 prevent cycles / duplicates. */
861 preserve_one_value (struct value
*value
, struct objfile
*objfile
,
864 if (TYPE_OBJFILE (value
->type
) == objfile
)
865 value
->type
= copy_type_recursive (objfile
, value
->type
, copied_types
);
867 if (TYPE_OBJFILE (value
->enclosing_type
) == objfile
)
868 value
->enclosing_type
= copy_type_recursive (objfile
,
869 value
->enclosing_type
,
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. */
880 preserve_values (struct objfile
*objfile
)
883 struct value_history_chunk
*cur
;
884 struct internalvar
*var
;
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
);
891 for (cur
= value_history_chain
; cur
; cur
= cur
->next
)
892 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
894 preserve_one_value (cur
->values
[i
], objfile
, copied_types
);
896 for (var
= internalvars
; var
; var
= var
->next
)
897 preserve_one_value (var
->value
, objfile
, copied_types
);
899 htab_delete (copied_types
);
903 show_convenience (char *ignore
, int from_tty
)
905 struct internalvar
*var
;
908 for (var
= internalvars
; var
; var
= var
->next
)
914 printf_filtered (("$%s = "), var
->name
);
915 value_print (value_of_internalvar (var
), gdb_stdout
,
916 0, Val_pretty_default
);
917 printf_filtered (("\n"));
920 printf_unfiltered (_("\
921 No debugger convenience variables now defined.\n\
922 Convenience variables have names starting with \"$\";\n\
923 use \"set\" as in \"set $foo = 5\" to define them.\n"));
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. */
932 value_as_long (struct value
*val
)
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. */
937 val
= coerce_array (val
);
938 return unpack_long (value_type (val
), value_contents (val
));
942 value_as_double (struct value
*val
)
947 foo
= unpack_double (value_type (val
), value_contents (val
), &inv
);
949 error (_("Invalid floating value found in program."));
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. */
956 value_as_address (struct value
*val
)
958 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
959 whether we want this to be true eventually. */
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. */
964 return ADDR_BITS_REMOVE (value_as_long (val
));
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.
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
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.
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.
1002 The following shortcut avoids this whole mess. If VAL is a
1003 function, just return its address directly. */
1004 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1005 || TYPE_CODE (value_type (val
)) == TYPE_CODE_METHOD
)
1006 return VALUE_ADDRESS (val
);
1008 val
= coerce_array (val
);
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. */
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.
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
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.
1039 Adding an architecture method like integer_to_address() certainly
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. */
1047 if (TYPE_CODE (value_type (val
)) != TYPE_CODE_PTR
1048 && TYPE_CODE (value_type (val
)) != TYPE_CODE_REF
1049 && gdbarch_integer_to_address_p (current_gdbarch
))
1050 return gdbarch_integer_to_address (current_gdbarch
, value_type (val
),
1051 value_contents (val
));
1053 return unpack_long (value_type (val
), value_contents (val
));
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.
1063 If you want functions and arrays to be coerced to pointers, and
1064 references to be dereferenced, call value_as_long() instead.
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. */
1072 unpack_long (struct type
*type
, const gdb_byte
*valaddr
)
1074 enum type_code code
= TYPE_CODE (type
);
1075 int len
= TYPE_LENGTH (type
);
1076 int nosign
= TYPE_UNSIGNED (type
);
1080 case TYPE_CODE_TYPEDEF
:
1081 return unpack_long (check_typedef (type
), valaddr
);
1082 case TYPE_CODE_ENUM
:
1083 case TYPE_CODE_FLAGS
:
1084 case TYPE_CODE_BOOL
:
1086 case TYPE_CODE_CHAR
:
1087 case TYPE_CODE_RANGE
:
1088 case TYPE_CODE_MEMBERPTR
:
1090 return extract_unsigned_integer (valaddr
, len
);
1092 return extract_signed_integer (valaddr
, len
);
1095 return extract_typed_floating (valaddr
, type
);
1099 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1100 whether we want this to be true eventually. */
1101 return extract_typed_address (valaddr
, type
);
1104 error (_("Value can't be converted to integer."));
1106 return 0; /* Placate lint. */
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. */
1116 unpack_double (struct type
*type
, const gdb_byte
*valaddr
, int *invp
)
1118 enum type_code code
;
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
)
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.
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!
1143 Hopefully someone will add both the missing floatformat
1144 definitions and the new cases for floatformat_is_valid (). */
1146 if (!floatformat_is_valid (floatformat_from_type (type
), valaddr
))
1152 return extract_typed_floating (valaddr
, type
);
1156 /* Unsigned -- be sure we compensate for signed LONGEST. */
1157 return (ULONGEST
) unpack_long (type
, valaddr
);
1161 /* Signed -- we are OK with unpack_long. */
1162 return unpack_long (type
, valaddr
);
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
1171 If you want functions and arrays to be coerced to pointers, and
1172 references to be dereferenced, call value_as_address() instead.
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. */
1180 unpack_pointer (struct type
*type
, const gdb_byte
*valaddr
)
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
);
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
1193 value_static_field (struct type
*type
, int fieldno
)
1195 struct value
*retval
;
1197 if (TYPE_FIELD_STATIC_HAS_ADDR (type
, fieldno
))
1199 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1200 TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1204 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1205 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0, NULL
);
1208 /* With some compilers, e.g. HP aCC, static data members are reported
1209 as non-debuggable symbols */
1210 struct minimal_symbol
*msym
= lookup_minimal_symbol (phys_name
, NULL
, NULL
);
1215 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1216 SYMBOL_VALUE_ADDRESS (msym
));
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
))
1224 warning (_("static field's value depends on the current "
1225 "frame - bad debug info?"));
1226 retval
= read_var_value (sym
, NULL
);
1228 if (retval
&& VALUE_LVAL (retval
) == lval_memory
)
1229 SET_FIELD_PHYSADDR (TYPE_FIELD (type
, fieldno
),
1230 VALUE_ADDRESS (retval
));
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. */
1242 value_change_enclosing_type (struct value
*val
, struct type
*new_encl_type
)
1244 if (TYPE_LENGTH (new_encl_type
) <= TYPE_LENGTH (value_enclosing_type (val
)))
1246 val
->enclosing_type
= new_encl_type
;
1251 struct value
*new_val
;
1254 new_val
= (struct value
*) xrealloc (val
, sizeof (struct value
) + TYPE_LENGTH (new_encl_type
));
1256 new_val
->enclosing_type
= new_encl_type
;
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... */
1263 if (val
!= all_values
)
1265 for (prev
= all_values
; prev
!= NULL
; prev
= prev
->next
)
1267 if (prev
->next
== val
)
1269 prev
->next
= new_val
;
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. */
1285 value_primitive_field (struct value
*arg1
, int offset
,
1286 int fieldno
, struct type
*arg_type
)
1291 CHECK_TYPEDEF (arg_type
);
1292 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
1294 /* Handle packed fields */
1296 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
1298 v
= value_from_longest (type
,
1299 unpack_field_as_long (arg_type
,
1300 value_contents (arg1
)
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
1306 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1308 else if (fieldno
< TYPE_N_BASECLASSES (arg_type
))
1310 /* This field is actually a base subobject, so preserve the
1311 entire object's contents for later references to virtual
1313 v
= allocate_value (value_enclosing_type (arg1
));
1315 if (value_lazy (arg1
))
1316 set_value_lazy (v
, 1);
1318 memcpy (value_contents_all_raw (v
), value_contents_all_raw (arg1
),
1319 TYPE_LENGTH (value_enclosing_type (arg1
)));
1320 v
->offset
= value_offset (arg1
);
1321 v
->embedded_offset
= (offset
+ value_embedded_offset (arg1
)
1322 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8);
1326 /* Plain old data member */
1327 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1328 v
= allocate_value (type
);
1329 if (value_lazy (arg1
))
1330 set_value_lazy (v
, 1);
1332 memcpy (value_contents_raw (v
),
1333 value_contents_raw (arg1
) + offset
,
1334 TYPE_LENGTH (type
));
1335 v
->offset
= (value_offset (arg1
) + offset
1336 + value_embedded_offset (arg1
));
1338 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
1339 if (VALUE_LVAL (arg1
) == lval_internalvar
)
1340 VALUE_LVAL (v
) = lval_internalvar_component
;
1341 v
->location
= arg1
->location
;
1342 VALUE_REGNUM (v
) = VALUE_REGNUM (arg1
);
1343 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (arg1
);
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. */
1352 value_field (struct value
*arg1
, int fieldno
)
1354 return value_primitive_field (arg1
, 0, fieldno
, value_type (arg1
));
1357 /* Return a non-virtual function as a value.
1358 F is the list of member functions which contains the desired method.
1359 J is an index into F which provides the desired method.
1361 We only use the symbol for its address, so be happy with either a
1362 full symbol or a minimal symbol.
1366 value_fn_field (struct value
**arg1p
, struct fn_field
*f
, int j
, struct type
*type
,
1370 struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
1371 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1373 struct minimal_symbol
*msym
;
1375 sym
= lookup_symbol (physname
, 0, VAR_DOMAIN
, 0, NULL
);
1382 gdb_assert (sym
== NULL
);
1383 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
1388 v
= allocate_value (ftype
);
1391 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1395 VALUE_ADDRESS (v
) = SYMBOL_VALUE_ADDRESS (msym
);
1400 if (type
!= value_type (*arg1p
))
1401 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
1402 value_addr (*arg1p
)));
1404 /* Move the `this' pointer according to the offset.
1405 VALUE_OFFSET (*arg1p) += offset;
1413 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
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
1425 If the field is signed, we also do sign extension. */
1428 unpack_field_as_long (struct type
*type
, const gdb_byte
*valaddr
, int fieldno
)
1432 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1433 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1435 struct type
*field_type
;
1437 val
= extract_unsigned_integer (valaddr
+ bitpos
/ 8, sizeof (val
));
1438 field_type
= TYPE_FIELD_TYPE (type
, fieldno
);
1439 CHECK_TYPEDEF (field_type
);
1441 /* Extract bits. See comment above. */
1443 if (BITS_BIG_ENDIAN
)
1444 lsbcount
= (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1446 lsbcount
= (bitpos
% 8);
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. */
1452 if ((bitsize
> 0) && (bitsize
< 8 * (int) sizeof (val
)))
1454 valmask
= (((ULONGEST
) 1) << bitsize
) - 1;
1456 if (!TYPE_UNSIGNED (field_type
))
1458 if (val
& (valmask
^ (valmask
>> 1)))
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
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. */
1475 modify_field (gdb_byte
*addr
, LONGEST fieldval
, int bitpos
, int bitsize
)
1478 ULONGEST mask
= (ULONGEST
) -1 >> (8 * sizeof (ULONGEST
) - bitsize
);
1480 /* If a negative fieldval fits in the field in question, chop
1481 off the sign extension bits. */
1482 if ((~fieldval
& ~(mask
>> 1)) == 0)
1485 /* Warn if value is too big to fit in the field in question. */
1486 if (0 != (fieldval
& ~mask
))
1488 /* FIXME: would like to include fieldval in the message, but
1489 we don't have a sprintf_longest. */
1490 warning (_("Value does not fit in %d bits."), bitsize
);
1492 /* Truncate it, otherwise adjoining fields may be corrupted. */
1496 oword
= extract_unsigned_integer (addr
, sizeof oword
);
1498 /* Shifting for bit field depends on endianness of the target machine. */
1499 if (BITS_BIG_ENDIAN
)
1500 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1502 oword
&= ~(mask
<< bitpos
);
1503 oword
|= fieldval
<< bitpos
;
1505 store_unsigned_integer (addr
, sizeof oword
, oword
);
1508 /* Convert C numbers into newly allocated values */
1511 value_from_longest (struct type
*type
, LONGEST num
)
1513 struct value
*val
= allocate_value (type
);
1514 enum type_code code
;
1517 code
= TYPE_CODE (type
);
1518 len
= TYPE_LENGTH (type
);
1522 case TYPE_CODE_TYPEDEF
:
1523 type
= check_typedef (type
);
1526 case TYPE_CODE_CHAR
:
1527 case TYPE_CODE_ENUM
:
1528 case TYPE_CODE_FLAGS
:
1529 case TYPE_CODE_BOOL
:
1530 case TYPE_CODE_RANGE
:
1531 case TYPE_CODE_MEMBERPTR
:
1532 store_signed_integer (value_contents_raw (val
), len
, num
);
1537 store_typed_address (value_contents_raw (val
), type
, (CORE_ADDR
) num
);
1541 error (_("Unexpected type (%d) encountered for integer constant."), code
);
1547 /* Create a value representing a pointer of type TYPE to the address
1550 value_from_pointer (struct type
*type
, CORE_ADDR addr
)
1552 struct value
*val
= allocate_value (type
);
1553 store_typed_address (value_contents_raw (val
), type
, addr
);
1558 /* Create a value for a string constant to be stored locally
1559 (not in the inferior's memory space, but in GDB memory).
1560 This is analogous to value_from_longest, which also does not
1561 use inferior memory. String shall NOT contain embedded nulls. */
1564 value_from_string (char *ptr
)
1567 int len
= strlen (ptr
);
1568 int lowbound
= current_language
->string_lower_bound
;
1569 struct type
*string_char_type
;
1570 struct type
*rangetype
;
1571 struct type
*stringtype
;
1573 rangetype
= create_range_type ((struct type
*) NULL
,
1575 lowbound
, len
+ lowbound
- 1);
1576 string_char_type
= language_string_char_type (current_language
,
1578 stringtype
= create_array_type ((struct type
*) NULL
,
1581 val
= allocate_value (stringtype
);
1582 memcpy (value_contents_raw (val
), ptr
, len
);
1587 value_from_double (struct type
*type
, DOUBLEST num
)
1589 struct value
*val
= allocate_value (type
);
1590 struct type
*base_type
= check_typedef (type
);
1591 enum type_code code
= TYPE_CODE (base_type
);
1592 int len
= TYPE_LENGTH (base_type
);
1594 if (code
== TYPE_CODE_FLT
)
1596 store_typed_floating (value_contents_raw (val
), base_type
, num
);
1599 error (_("Unexpected type encountered for floating constant."));
1605 coerce_ref (struct value
*arg
)
1607 struct type
*value_type_arg_tmp
= check_typedef (value_type (arg
));
1608 if (TYPE_CODE (value_type_arg_tmp
) == TYPE_CODE_REF
)
1609 arg
= value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp
),
1610 unpack_pointer (value_type (arg
),
1611 value_contents (arg
)));
1616 coerce_array (struct value
*arg
)
1618 arg
= coerce_ref (arg
);
1619 if (current_language
->c_style_arrays
1620 && TYPE_CODE (value_type (arg
)) == TYPE_CODE_ARRAY
)
1621 arg
= value_coerce_array (arg
);
1622 if (TYPE_CODE (value_type (arg
)) == TYPE_CODE_FUNC
)
1623 arg
= value_coerce_function (arg
);
1628 coerce_number (struct value
*arg
)
1630 arg
= coerce_array (arg
);
1631 arg
= coerce_enum (arg
);
1636 coerce_enum (struct value
*arg
)
1638 if (TYPE_CODE (check_typedef (value_type (arg
))) == TYPE_CODE_ENUM
)
1639 arg
= value_cast (builtin_type_unsigned_int
, arg
);
1644 /* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of
1645 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc and TYPE
1646 is the type (which is known to be struct, union or array).
1648 On most machines, the struct convention is used unless we are
1649 using gcc and the type is of a special size. */
1650 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1651 native compiler. GCC 2.3.3 was the last release that did it the
1652 old way. Since gcc2_compiled was not changed, we have no
1653 way to correctly win in all cases, so we just do the right thing
1654 for gcc1 and for gcc2 after this change. Thus it loses for gcc
1655 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled
1656 would cause more chaos than dealing with some struct returns being
1658 /* NOTE: cagney/2004-06-13: Deleted check for "gcc_p". GCC 1.x is
1662 generic_use_struct_convention (int gcc_p
, struct type
*value_type
)
1664 return !(TYPE_LENGTH (value_type
) == 1
1665 || TYPE_LENGTH (value_type
) == 2
1666 || TYPE_LENGTH (value_type
) == 4
1667 || TYPE_LENGTH (value_type
) == 8);
1670 /* Return true if the function returning the specified type is using
1671 the convention of returning structures in memory (passing in the
1672 address as a hidden first parameter). GCC_P is nonzero if compiled
1676 using_struct_return (struct type
*value_type
, int gcc_p
)
1678 enum type_code code
= TYPE_CODE (value_type
);
1680 if (code
== TYPE_CODE_ERROR
)
1681 error (_("Function return type unknown."));
1683 if (code
== TYPE_CODE_VOID
)
1684 /* A void return value is never in memory. See also corresponding
1685 code in "print_return_value". */
1688 /* Probe the architecture for the return-value convention. */
1689 return (gdbarch_return_value (current_gdbarch
, value_type
,
1691 != RETURN_VALUE_REGISTER_CONVENTION
);
1695 _initialize_values (void)
1697 add_cmd ("convenience", no_class
, show_convenience
, _("\
1698 Debugger convenience (\"$foo\") variables.\n\
1699 These variables are created when you assign them values;\n\
1700 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
1702 A few convenience variables are given values automatically:\n\
1703 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1704 \"$__\" holds the contents of the last address examined with \"x\"."),
1707 add_cmd ("values", no_class
, show_values
,
1708 _("Elements of value history around item number IDX (or last ten)."),
1711 add_com ("init-if-undefined", class_vars
, init_if_undefined_command
, _("\
1712 Initialize a convenience variable if necessary.\n\
1713 init-if-undefined VARIABLE = EXPRESSION\n\
1714 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
1715 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
1716 VARIABLE is already initialized."));