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, 2008,
5 2009 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
24 #include "gdb_string.h"
35 #include "gdb_assert.h"
41 #include "cli/cli-decode.h"
43 #include "python/python.h"
45 /* Prototypes for exported functions. */
47 void _initialize_values (void);
49 /* Definition of a user function. */
50 struct internal_function
52 /* The name of the function. It is a bit odd to have this in the
53 function itself -- the user might use a differently-named
54 convenience variable to hold the function. */
58 internal_function_fn handler
;
60 /* User data for the handler. */
64 static struct cmd_list_element
*functionlist
;
68 /* Type of value; either not an lval, or one of the various
69 different possible kinds of lval. */
72 /* Is it modifiable? Only relevant if lval != not_lval. */
75 /* Location of value (if lval). */
78 /* If lval == lval_memory, this is the address in the inferior.
79 If lval == lval_register, this is the byte offset into the
80 registers structure. */
83 /* Pointer to internal variable. */
84 struct internalvar
*internalvar
;
86 /* If lval == lval_computed, this is a set of function pointers
87 to use to access and describe the value, and a closure pointer
91 struct lval_funcs
*funcs
; /* Functions to call. */
92 void *closure
; /* Closure for those functions to use. */
96 /* Describes offset of a value within lval of a structure in bytes.
97 If lval == lval_memory, this is an offset to the address. If
98 lval == lval_register, this is a further offset from
99 location.address within the registers structure. Note also the
100 member embedded_offset below. */
103 /* Only used for bitfields; number of bits contained in them. */
106 /* Only used for bitfields; position of start of field. For
107 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
108 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
111 /* Frame register value is relative to. This will be described in
112 the lval enum above as "lval_register". */
113 struct frame_id frame_id
;
115 /* Type of the value. */
118 /* If a value represents a C++ object, then the `type' field gives
119 the object's compile-time type. If the object actually belongs
120 to some class derived from `type', perhaps with other base
121 classes and additional members, then `type' is just a subobject
122 of the real thing, and the full object is probably larger than
123 `type' would suggest.
125 If `type' is a dynamic class (i.e. one with a vtable), then GDB
126 can actually determine the object's run-time type by looking at
127 the run-time type information in the vtable. When this
128 information is available, we may elect to read in the entire
129 object, for several reasons:
131 - When printing the value, the user would probably rather see the
132 full object, not just the limited portion apparent from the
135 - If `type' has virtual base classes, then even printing `type'
136 alone may require reaching outside the `type' portion of the
137 object to wherever the virtual base class has been stored.
139 When we store the entire object, `enclosing_type' is the run-time
140 type -- the complete object -- and `embedded_offset' is the
141 offset of `type' within that larger type, in bytes. The
142 value_contents() macro takes `embedded_offset' into account, so
143 most GDB code continues to see the `type' portion of the value,
144 just as the inferior would.
146 If `type' is a pointer to an object, then `enclosing_type' is a
147 pointer to the object's run-time type, and `pointed_to_offset' is
148 the offset in bytes from the full object to the pointed-to object
149 -- that is, the value `embedded_offset' would have if we followed
150 the pointer and fetched the complete object. (I don't really see
151 the point. Why not just determine the run-time type when you
152 indirect, and avoid the special case? The contents don't matter
153 until you indirect anyway.)
155 If we're not doing anything fancy, `enclosing_type' is equal to
156 `type', and `embedded_offset' is zero, so everything works
158 struct type
*enclosing_type
;
160 int pointed_to_offset
;
162 /* Values are stored in a chain, so that they can be deleted easily
163 over calls to the inferior. Values assigned to internal
164 variables, put into the value history or exposed to Python are
165 taken off this list. */
168 /* Register number if the value is from a register. */
171 /* If zero, contents of this value are in the contents field. If
172 nonzero, contents are in inferior. If the lval field is lval_memory,
173 the contents are in inferior memory at location.address plus offset.
174 The lval field may also be lval_register.
176 WARNING: This field is used by the code which handles watchpoints
177 (see breakpoint.c) to decide whether a particular value can be
178 watched by hardware watchpoints. If the lazy flag is set for
179 some member of a value chain, it is assumed that this member of
180 the chain doesn't need to be watched as part of watching the
181 value itself. This is how GDB avoids watching the entire struct
182 or array when the user wants to watch a single struct member or
183 array element. If you ever change the way lazy flag is set and
184 reset, be sure to consider this use as well! */
187 /* If nonzero, this is the value of a variable which does not
188 actually exist in the program. */
191 /* If value is a variable, is it initialized or not. */
194 /* Actual contents of the value. Target byte-order. NULL or not
195 valid if lazy is nonzero. */
198 /* The number of references to this value. When a value is created,
199 the value chain holds a reference, so REFERENCE_COUNT is 1. If
200 release_value is called, this value is removed from the chain but
201 the caller of release_value now has a reference to this value.
202 The caller must arrange for a call to value_free later. */
206 /* Prototypes for local functions. */
208 static void show_values (char *, int);
210 static void show_convenience (char *, int);
213 /* The value-history records all the values printed
214 by print commands during this session. Each chunk
215 records 60 consecutive values. The first chunk on
216 the chain records the most recent values.
217 The total number of values is in value_history_count. */
219 #define VALUE_HISTORY_CHUNK 60
221 struct value_history_chunk
223 struct value_history_chunk
*next
;
224 struct value
*values
[VALUE_HISTORY_CHUNK
];
227 /* Chain of chunks now in use. */
229 static struct value_history_chunk
*value_history_chain
;
231 static int value_history_count
; /* Abs number of last entry stored */
234 /* List of all value objects currently allocated
235 (except for those released by calls to release_value)
236 This is so they can be freed after each command. */
238 static struct value
*all_values
;
240 /* Allocate a lazy value for type TYPE. Its actual content is
241 "lazily" allocated too: the content field of the return value is
242 NULL; it will be allocated when it is fetched from the target. */
245 allocate_value_lazy (struct type
*type
)
248 struct type
*atype
= check_typedef (type
);
250 val
= (struct value
*) xzalloc (sizeof (struct value
));
251 val
->contents
= NULL
;
252 val
->next
= all_values
;
255 val
->enclosing_type
= type
;
256 VALUE_LVAL (val
) = not_lval
;
257 val
->location
.address
= 0;
258 VALUE_FRAME_ID (val
) = null_frame_id
;
262 VALUE_REGNUM (val
) = -1;
264 val
->optimized_out
= 0;
265 val
->embedded_offset
= 0;
266 val
->pointed_to_offset
= 0;
268 val
->initialized
= 1; /* Default to initialized. */
270 /* Values start out on the all_values chain. */
271 val
->reference_count
= 1;
276 /* Allocate the contents of VAL if it has not been allocated yet. */
279 allocate_value_contents (struct value
*val
)
282 val
->contents
= (gdb_byte
*) xzalloc (TYPE_LENGTH (val
->enclosing_type
));
285 /* Allocate a value and its contents for type TYPE. */
288 allocate_value (struct type
*type
)
290 struct value
*val
= allocate_value_lazy (type
);
291 allocate_value_contents (val
);
296 /* Allocate a value that has the correct length
297 for COUNT repetitions of type TYPE. */
300 allocate_repeat_value (struct type
*type
, int count
)
302 int low_bound
= current_language
->string_lower_bound
; /* ??? */
303 /* FIXME-type-allocation: need a way to free this type when we are
305 struct type
*array_type
306 = lookup_array_range_type (type
, low_bound
, count
+ low_bound
- 1);
307 return allocate_value (array_type
);
310 /* Needed if another module needs to maintain its on list of values. */
312 value_prepend_to_list (struct value
**head
, struct value
*val
)
318 /* Needed if another module needs to maintain its on list of values. */
320 value_remove_from_list (struct value
**head
, struct value
*val
)
325 *head
= (*head
)->next
;
327 for (prev
= *head
; prev
->next
; prev
= prev
->next
)
328 if (prev
->next
== val
)
330 prev
->next
= val
->next
;
336 allocate_computed_value (struct type
*type
,
337 struct lval_funcs
*funcs
,
340 struct value
*v
= allocate_value (type
);
341 VALUE_LVAL (v
) = lval_computed
;
342 v
->location
.computed
.funcs
= funcs
;
343 v
->location
.computed
.closure
= closure
;
344 set_value_lazy (v
, 1);
349 /* Accessor methods. */
352 value_next (struct value
*value
)
358 value_type (struct value
*value
)
363 deprecated_set_value_type (struct value
*value
, struct type
*type
)
369 value_offset (struct value
*value
)
371 return value
->offset
;
374 set_value_offset (struct value
*value
, int offset
)
376 value
->offset
= offset
;
380 value_bitpos (struct value
*value
)
382 return value
->bitpos
;
385 set_value_bitpos (struct value
*value
, int bit
)
391 value_bitsize (struct value
*value
)
393 return value
->bitsize
;
396 set_value_bitsize (struct value
*value
, int bit
)
398 value
->bitsize
= bit
;
402 value_contents_raw (struct value
*value
)
404 allocate_value_contents (value
);
405 return value
->contents
+ value
->embedded_offset
;
409 value_contents_all_raw (struct value
*value
)
411 allocate_value_contents (value
);
412 return value
->contents
;
416 value_enclosing_type (struct value
*value
)
418 return value
->enclosing_type
;
422 value_contents_all (struct value
*value
)
425 value_fetch_lazy (value
);
426 return value
->contents
;
430 value_lazy (struct value
*value
)
436 set_value_lazy (struct value
*value
, int val
)
442 value_contents (struct value
*value
)
444 return value_contents_writeable (value
);
448 value_contents_writeable (struct value
*value
)
451 value_fetch_lazy (value
);
452 return value_contents_raw (value
);
455 /* Return non-zero if VAL1 and VAL2 have the same contents. Note that
456 this function is different from value_equal; in C the operator ==
457 can return 0 even if the two values being compared are equal. */
460 value_contents_equal (struct value
*val1
, struct value
*val2
)
466 type1
= check_typedef (value_type (val1
));
467 type2
= check_typedef (value_type (val2
));
468 len
= TYPE_LENGTH (type1
);
469 if (len
!= TYPE_LENGTH (type2
))
472 return (memcmp (value_contents (val1
), value_contents (val2
), len
) == 0);
476 value_optimized_out (struct value
*value
)
478 return value
->optimized_out
;
482 set_value_optimized_out (struct value
*value
, int val
)
484 value
->optimized_out
= val
;
488 value_embedded_offset (struct value
*value
)
490 return value
->embedded_offset
;
494 set_value_embedded_offset (struct value
*value
, int val
)
496 value
->embedded_offset
= val
;
500 value_pointed_to_offset (struct value
*value
)
502 return value
->pointed_to_offset
;
506 set_value_pointed_to_offset (struct value
*value
, int val
)
508 value
->pointed_to_offset
= val
;
512 value_computed_funcs (struct value
*v
)
514 gdb_assert (VALUE_LVAL (v
) == lval_computed
);
516 return v
->location
.computed
.funcs
;
520 value_computed_closure (struct value
*v
)
522 gdb_assert (VALUE_LVAL (v
) == lval_computed
);
524 return v
->location
.computed
.closure
;
528 deprecated_value_lval_hack (struct value
*value
)
534 value_address (struct value
*value
)
536 if (value
->lval
== lval_internalvar
537 || value
->lval
== lval_internalvar_component
)
539 return value
->location
.address
+ value
->offset
;
543 value_raw_address (struct value
*value
)
545 if (value
->lval
== lval_internalvar
546 || value
->lval
== lval_internalvar_component
)
548 return value
->location
.address
;
552 set_value_address (struct value
*value
, CORE_ADDR addr
)
554 gdb_assert (value
->lval
!= lval_internalvar
555 && value
->lval
!= lval_internalvar_component
);
556 value
->location
.address
= addr
;
559 struct internalvar
**
560 deprecated_value_internalvar_hack (struct value
*value
)
562 return &value
->location
.internalvar
;
566 deprecated_value_frame_id_hack (struct value
*value
)
568 return &value
->frame_id
;
572 deprecated_value_regnum_hack (struct value
*value
)
574 return &value
->regnum
;
578 deprecated_value_modifiable (struct value
*value
)
580 return value
->modifiable
;
583 deprecated_set_value_modifiable (struct value
*value
, int modifiable
)
585 value
->modifiable
= modifiable
;
588 /* Return a mark in the value chain. All values allocated after the
589 mark is obtained (except for those released) are subject to being freed
590 if a subsequent value_free_to_mark is passed the mark. */
597 /* Take a reference to VAL. VAL will not be deallocated until all
598 references are released. */
601 value_incref (struct value
*val
)
603 val
->reference_count
++;
606 /* Release a reference to VAL, which was acquired with value_incref.
607 This function is also called to deallocate values from the value
611 value_free (struct value
*val
)
615 gdb_assert (val
->reference_count
> 0);
616 val
->reference_count
--;
617 if (val
->reference_count
> 0)
620 if (VALUE_LVAL (val
) == lval_computed
)
622 struct lval_funcs
*funcs
= val
->location
.computed
.funcs
;
624 if (funcs
->free_closure
)
625 funcs
->free_closure (val
);
628 xfree (val
->contents
);
633 /* Free all values allocated since MARK was obtained by value_mark
634 (except for those released). */
636 value_free_to_mark (struct value
*mark
)
641 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
649 /* Free all the values that have been allocated (except for those released).
650 Called after each command, successful or not. */
653 free_all_values (void)
658 for (val
= all_values
; val
; val
= next
)
667 /* Remove VAL from the chain all_values
668 so it will not be freed automatically. */
671 release_value (struct value
*val
)
675 if (all_values
== val
)
677 all_values
= val
->next
;
681 for (v
= all_values
; v
; v
= v
->next
)
691 /* Release all values up to mark */
693 value_release_to_mark (struct value
*mark
)
698 for (val
= next
= all_values
; next
; next
= next
->next
)
699 if (next
->next
== mark
)
701 all_values
= next
->next
;
709 /* Return a copy of the value ARG.
710 It contains the same contents, for same memory address,
711 but it's a different block of storage. */
714 value_copy (struct value
*arg
)
716 struct type
*encl_type
= value_enclosing_type (arg
);
719 if (value_lazy (arg
))
720 val
= allocate_value_lazy (encl_type
);
722 val
= allocate_value (encl_type
);
723 val
->type
= arg
->type
;
724 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
725 val
->location
= arg
->location
;
726 val
->offset
= arg
->offset
;
727 val
->bitpos
= arg
->bitpos
;
728 val
->bitsize
= arg
->bitsize
;
729 VALUE_FRAME_ID (val
) = VALUE_FRAME_ID (arg
);
730 VALUE_REGNUM (val
) = VALUE_REGNUM (arg
);
731 val
->lazy
= arg
->lazy
;
732 val
->optimized_out
= arg
->optimized_out
;
733 val
->embedded_offset
= value_embedded_offset (arg
);
734 val
->pointed_to_offset
= arg
->pointed_to_offset
;
735 val
->modifiable
= arg
->modifiable
;
736 if (!value_lazy (val
))
738 memcpy (value_contents_all_raw (val
), value_contents_all_raw (arg
),
739 TYPE_LENGTH (value_enclosing_type (arg
)));
742 if (VALUE_LVAL (val
) == lval_computed
)
744 struct lval_funcs
*funcs
= val
->location
.computed
.funcs
;
746 if (funcs
->copy_closure
)
747 val
->location
.computed
.closure
= funcs
->copy_closure (val
);
753 set_value_component_location (struct value
*component
, struct value
*whole
)
755 if (VALUE_LVAL (whole
) == lval_internalvar
)
756 VALUE_LVAL (component
) = lval_internalvar_component
;
758 VALUE_LVAL (component
) = VALUE_LVAL (whole
);
760 component
->location
= whole
->location
;
761 if (VALUE_LVAL (whole
) == lval_computed
)
763 struct lval_funcs
*funcs
= whole
->location
.computed
.funcs
;
765 if (funcs
->copy_closure
)
766 component
->location
.computed
.closure
= funcs
->copy_closure (whole
);
771 /* Access to the value history. */
773 /* Record a new value in the value history.
774 Returns the absolute history index of the entry.
775 Result of -1 indicates the value was not saved; otherwise it is the
776 value history index of this new item. */
779 record_latest_value (struct value
*val
)
783 /* We don't want this value to have anything to do with the inferior anymore.
784 In particular, "set $1 = 50" should not affect the variable from which
785 the value was taken, and fast watchpoints should be able to assume that
786 a value on the value history never changes. */
787 if (value_lazy (val
))
788 value_fetch_lazy (val
);
789 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
790 from. This is a bit dubious, because then *&$1 does not just return $1
791 but the current contents of that location. c'est la vie... */
795 /* Here we treat value_history_count as origin-zero
796 and applying to the value being stored now. */
798 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
801 struct value_history_chunk
*new
802 = (struct value_history_chunk
*)
803 xmalloc (sizeof (struct value_history_chunk
));
804 memset (new->values
, 0, sizeof new->values
);
805 new->next
= value_history_chain
;
806 value_history_chain
= new;
809 value_history_chain
->values
[i
] = val
;
811 /* Now we regard value_history_count as origin-one
812 and applying to the value just stored. */
814 return ++value_history_count
;
817 /* Return a copy of the value in the history with sequence number NUM. */
820 access_value_history (int num
)
822 struct value_history_chunk
*chunk
;
827 absnum
+= value_history_count
;
832 error (_("The history is empty."));
834 error (_("There is only one value in the history."));
836 error (_("History does not go back to $$%d."), -num
);
838 if (absnum
> value_history_count
)
839 error (_("History has not yet reached $%d."), absnum
);
843 /* Now absnum is always absolute and origin zero. */
845 chunk
= value_history_chain
;
846 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
850 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
854 show_values (char *num_exp
, int from_tty
)
862 /* "show values +" should print from the stored position.
863 "show values <exp>" should print around value number <exp>. */
864 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
865 num
= parse_and_eval_long (num_exp
) - 5;
869 /* "show values" means print the last 10 values. */
870 num
= value_history_count
- 9;
876 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
878 struct value_print_options opts
;
879 val
= access_value_history (i
);
880 printf_filtered (("$%d = "), i
);
881 get_user_print_options (&opts
);
882 value_print (val
, gdb_stdout
, &opts
);
883 printf_filtered (("\n"));
886 /* The next "show values +" should start after what we just printed. */
889 /* Hitting just return after this command should do the same thing as
890 "show values +". If num_exp is null, this is unnecessary, since
891 "show values +" is not useful after "show values". */
892 if (from_tty
&& num_exp
)
899 /* Internal variables. These are variables within the debugger
900 that hold values assigned by debugger commands.
901 The user refers to them with a '$' prefix
902 that does not appear in the variable names stored internally. */
906 struct internalvar
*next
;
909 /* We support various different kinds of content of an internal variable.
910 enum internalvar_kind specifies the kind, and union internalvar_data
911 provides the data associated with this particular kind. */
913 enum internalvar_kind
915 /* The internal variable is empty. */
918 /* The value of the internal variable is provided directly as
919 a GDB value object. */
922 /* A fresh value is computed via a call-back routine on every
923 access to the internal variable. */
924 INTERNALVAR_MAKE_VALUE
,
926 /* The internal variable holds a GDB internal convenience function. */
927 INTERNALVAR_FUNCTION
,
929 /* The variable holds a simple scalar value. */
932 /* The variable holds a GDB-provided string. */
937 union internalvar_data
939 /* A value object used with INTERNALVAR_VALUE. */
942 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
943 internalvar_make_value make_value
;
945 /* The internal function used with INTERNALVAR_FUNCTION. */
948 struct internal_function
*function
;
949 /* True if this is the canonical name for the function. */
953 /* A scalar value used with INTERNALVAR_SCALAR. */
956 /* If type is non-NULL, it will be used as the type to generate
957 a value for this internal variable. If type is NULL, a default
958 integer type for the architecture is used. */
962 LONGEST l
; /* Used with TYPE_CODE_INT and NULL types. */
963 CORE_ADDR a
; /* Used with TYPE_CODE_PTR types. */
967 /* A string value used with INTERNALVAR_STRING. */
972 static struct internalvar
*internalvars
;
974 /* If the variable does not already exist create it and give it the value given.
975 If no value is given then the default is zero. */
977 init_if_undefined_command (char* args
, int from_tty
)
979 struct internalvar
* intvar
;
981 /* Parse the expression - this is taken from set_command(). */
982 struct expression
*expr
= parse_expression (args
);
983 register struct cleanup
*old_chain
=
984 make_cleanup (free_current_contents
, &expr
);
986 /* Validate the expression.
987 Was the expression an assignment?
988 Or even an expression at all? */
989 if (expr
->nelts
== 0 || expr
->elts
[0].opcode
!= BINOP_ASSIGN
)
990 error (_("Init-if-undefined requires an assignment expression."));
992 /* Extract the variable from the parsed expression.
993 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
994 if (expr
->elts
[1].opcode
!= OP_INTERNALVAR
)
995 error (_("The first parameter to init-if-undefined should be a GDB variable."));
996 intvar
= expr
->elts
[2].internalvar
;
998 /* Only evaluate the expression if the lvalue is void.
999 This may still fail if the expresssion is invalid. */
1000 if (intvar
->kind
== INTERNALVAR_VOID
)
1001 evaluate_expression (expr
);
1003 do_cleanups (old_chain
);
1007 /* Look up an internal variable with name NAME. NAME should not
1008 normally include a dollar sign.
1010 If the specified internal variable does not exist,
1011 the return value is NULL. */
1013 struct internalvar
*
1014 lookup_only_internalvar (const char *name
)
1016 struct internalvar
*var
;
1018 for (var
= internalvars
; var
; var
= var
->next
)
1019 if (strcmp (var
->name
, name
) == 0)
1026 /* Create an internal variable with name NAME and with a void value.
1027 NAME should not normally include a dollar sign. */
1029 struct internalvar
*
1030 create_internalvar (const char *name
)
1032 struct internalvar
*var
;
1033 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
1034 var
->name
= concat (name
, (char *)NULL
);
1035 var
->kind
= INTERNALVAR_VOID
;
1036 var
->next
= internalvars
;
1041 /* Create an internal variable with name NAME and register FUN as the
1042 function that value_of_internalvar uses to create a value whenever
1043 this variable is referenced. NAME should not normally include a
1046 struct internalvar
*
1047 create_internalvar_type_lazy (char *name
, internalvar_make_value fun
)
1049 struct internalvar
*var
= create_internalvar (name
);
1050 var
->kind
= INTERNALVAR_MAKE_VALUE
;
1051 var
->u
.make_value
= fun
;
1055 /* Look up an internal variable with name NAME. NAME should not
1056 normally include a dollar sign.
1058 If the specified internal variable does not exist,
1059 one is created, with a void value. */
1061 struct internalvar
*
1062 lookup_internalvar (const char *name
)
1064 struct internalvar
*var
;
1066 var
= lookup_only_internalvar (name
);
1070 return create_internalvar (name
);
1073 /* Return current value of internal variable VAR. For variables that
1074 are not inherently typed, use a value type appropriate for GDBARCH. */
1077 value_of_internalvar (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1083 case INTERNALVAR_VOID
:
1084 val
= allocate_value (builtin_type (gdbarch
)->builtin_void
);
1087 case INTERNALVAR_FUNCTION
:
1088 val
= allocate_value (builtin_type (gdbarch
)->internal_fn
);
1091 case INTERNALVAR_SCALAR
:
1092 if (!var
->u
.scalar
.type
)
1093 val
= value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1094 var
->u
.scalar
.val
.l
);
1095 else if (TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_INT
)
1096 val
= value_from_longest (var
->u
.scalar
.type
, var
->u
.scalar
.val
.l
);
1097 else if (TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_PTR
)
1098 val
= value_from_pointer (var
->u
.scalar
.type
, var
->u
.scalar
.val
.a
);
1100 internal_error (__FILE__
, __LINE__
, "bad type");
1103 case INTERNALVAR_STRING
:
1104 val
= value_cstring (var
->u
.string
, strlen (var
->u
.string
),
1105 builtin_type (gdbarch
)->builtin_char
);
1108 case INTERNALVAR_VALUE
:
1109 val
= value_copy (var
->u
.value
);
1110 if (value_lazy (val
))
1111 value_fetch_lazy (val
);
1114 case INTERNALVAR_MAKE_VALUE
:
1115 val
= (*var
->u
.make_value
) (gdbarch
, var
);
1119 internal_error (__FILE__
, __LINE__
, "bad kind");
1122 /* Change the VALUE_LVAL to lval_internalvar so that future operations
1123 on this value go back to affect the original internal variable.
1125 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
1126 no underlying modifyable state in the internal variable.
1128 Likewise, if the variable's value is a computed lvalue, we want
1129 references to it to produce another computed lvalue, where
1130 references and assignments actually operate through the
1131 computed value's functions.
1133 This means that internal variables with computed values
1134 behave a little differently from other internal variables:
1135 assignments to them don't just replace the previous value
1136 altogether. At the moment, this seems like the behavior we
1139 if (var
->kind
!= INTERNALVAR_MAKE_VALUE
1140 && val
->lval
!= lval_computed
)
1142 VALUE_LVAL (val
) = lval_internalvar
;
1143 VALUE_INTERNALVAR (val
) = var
;
1150 get_internalvar_integer (struct internalvar
*var
, LONGEST
*result
)
1154 case INTERNALVAR_SCALAR
:
1155 if (var
->u
.scalar
.type
== NULL
1156 || TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_INT
)
1158 *result
= var
->u
.scalar
.val
.l
;
1169 get_internalvar_function (struct internalvar
*var
,
1170 struct internal_function
**result
)
1174 case INTERNALVAR_FUNCTION
:
1175 *result
= var
->u
.fn
.function
;
1184 set_internalvar_component (struct internalvar
*var
, int offset
, int bitpos
,
1185 int bitsize
, struct value
*newval
)
1191 case INTERNALVAR_VALUE
:
1192 addr
= value_contents_writeable (var
->u
.value
);
1195 modify_field (value_type (var
->u
.value
), addr
+ offset
,
1196 value_as_long (newval
), bitpos
, bitsize
);
1198 memcpy (addr
+ offset
, value_contents (newval
),
1199 TYPE_LENGTH (value_type (newval
)));
1203 /* We can never get a component of any other kind. */
1204 internal_error (__FILE__
, __LINE__
, "set_internalvar_component");
1209 set_internalvar (struct internalvar
*var
, struct value
*val
)
1211 enum internalvar_kind new_kind
;
1212 union internalvar_data new_data
= { 0 };
1214 if (var
->kind
== INTERNALVAR_FUNCTION
&& var
->u
.fn
.canonical
)
1215 error (_("Cannot overwrite convenience function %s"), var
->name
);
1217 /* Prepare new contents. */
1218 switch (TYPE_CODE (check_typedef (value_type (val
))))
1220 case TYPE_CODE_VOID
:
1221 new_kind
= INTERNALVAR_VOID
;
1224 case TYPE_CODE_INTERNAL_FUNCTION
:
1225 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
1226 new_kind
= INTERNALVAR_FUNCTION
;
1227 get_internalvar_function (VALUE_INTERNALVAR (val
),
1228 &new_data
.fn
.function
);
1229 /* Copies created here are never canonical. */
1233 new_kind
= INTERNALVAR_SCALAR
;
1234 new_data
.scalar
.type
= value_type (val
);
1235 new_data
.scalar
.val
.l
= value_as_long (val
);
1239 new_kind
= INTERNALVAR_SCALAR
;
1240 new_data
.scalar
.type
= value_type (val
);
1241 new_data
.scalar
.val
.a
= value_as_address (val
);
1245 new_kind
= INTERNALVAR_VALUE
;
1246 new_data
.value
= value_copy (val
);
1247 new_data
.value
->modifiable
= 1;
1249 /* Force the value to be fetched from the target now, to avoid problems
1250 later when this internalvar is referenced and the target is gone or
1252 if (value_lazy (new_data
.value
))
1253 value_fetch_lazy (new_data
.value
);
1255 /* Release the value from the value chain to prevent it from being
1256 deleted by free_all_values. From here on this function should not
1257 call error () until new_data is installed into the var->u to avoid
1259 release_value (new_data
.value
);
1263 /* Clean up old contents. */
1264 clear_internalvar (var
);
1267 var
->kind
= new_kind
;
1269 /* End code which must not call error(). */
1273 set_internalvar_integer (struct internalvar
*var
, LONGEST l
)
1275 /* Clean up old contents. */
1276 clear_internalvar (var
);
1278 var
->kind
= INTERNALVAR_SCALAR
;
1279 var
->u
.scalar
.type
= NULL
;
1280 var
->u
.scalar
.val
.l
= l
;
1284 set_internalvar_string (struct internalvar
*var
, const char *string
)
1286 /* Clean up old contents. */
1287 clear_internalvar (var
);
1289 var
->kind
= INTERNALVAR_STRING
;
1290 var
->u
.string
= xstrdup (string
);
1294 set_internalvar_function (struct internalvar
*var
, struct internal_function
*f
)
1296 /* Clean up old contents. */
1297 clear_internalvar (var
);
1299 var
->kind
= INTERNALVAR_FUNCTION
;
1300 var
->u
.fn
.function
= f
;
1301 var
->u
.fn
.canonical
= 1;
1302 /* Variables installed here are always the canonical version. */
1306 clear_internalvar (struct internalvar
*var
)
1308 /* Clean up old contents. */
1311 case INTERNALVAR_VALUE
:
1312 value_free (var
->u
.value
);
1315 case INTERNALVAR_STRING
:
1316 xfree (var
->u
.string
);
1323 /* Reset to void kind. */
1324 var
->kind
= INTERNALVAR_VOID
;
1328 internalvar_name (struct internalvar
*var
)
1333 static struct internal_function
*
1334 create_internal_function (const char *name
,
1335 internal_function_fn handler
, void *cookie
)
1337 struct internal_function
*ifn
= XNEW (struct internal_function
);
1338 ifn
->name
= xstrdup (name
);
1339 ifn
->handler
= handler
;
1340 ifn
->cookie
= cookie
;
1345 value_internal_function_name (struct value
*val
)
1347 struct internal_function
*ifn
;
1350 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
1351 result
= get_internalvar_function (VALUE_INTERNALVAR (val
), &ifn
);
1352 gdb_assert (result
);
1358 call_internal_function (struct gdbarch
*gdbarch
,
1359 const struct language_defn
*language
,
1360 struct value
*func
, int argc
, struct value
**argv
)
1362 struct internal_function
*ifn
;
1365 gdb_assert (VALUE_LVAL (func
) == lval_internalvar
);
1366 result
= get_internalvar_function (VALUE_INTERNALVAR (func
), &ifn
);
1367 gdb_assert (result
);
1369 return (*ifn
->handler
) (gdbarch
, language
, ifn
->cookie
, argc
, argv
);
1372 /* The 'function' command. This does nothing -- it is just a
1373 placeholder to let "help function NAME" work. This is also used as
1374 the implementation of the sub-command that is created when
1375 registering an internal function. */
1377 function_command (char *command
, int from_tty
)
1382 /* Clean up if an internal function's command is destroyed. */
1384 function_destroyer (struct cmd_list_element
*self
, void *ignore
)
1390 /* Add a new internal function. NAME is the name of the function; DOC
1391 is a documentation string describing the function. HANDLER is
1392 called when the function is invoked. COOKIE is an arbitrary
1393 pointer which is passed to HANDLER and is intended for "user
1396 add_internal_function (const char *name
, const char *doc
,
1397 internal_function_fn handler
, void *cookie
)
1399 struct cmd_list_element
*cmd
;
1400 struct internal_function
*ifn
;
1401 struct internalvar
*var
= lookup_internalvar (name
);
1403 ifn
= create_internal_function (name
, handler
, cookie
);
1404 set_internalvar_function (var
, ifn
);
1406 cmd
= add_cmd (xstrdup (name
), no_class
, function_command
, (char *) doc
,
1408 cmd
->destroyer
= function_destroyer
;
1411 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
1412 prevent cycles / duplicates. */
1415 preserve_one_value (struct value
*value
, struct objfile
*objfile
,
1416 htab_t copied_types
)
1418 if (TYPE_OBJFILE (value
->type
) == objfile
)
1419 value
->type
= copy_type_recursive (objfile
, value
->type
, copied_types
);
1421 if (TYPE_OBJFILE (value
->enclosing_type
) == objfile
)
1422 value
->enclosing_type
= copy_type_recursive (objfile
,
1423 value
->enclosing_type
,
1427 /* Likewise for internal variable VAR. */
1430 preserve_one_internalvar (struct internalvar
*var
, struct objfile
*objfile
,
1431 htab_t copied_types
)
1435 case INTERNALVAR_SCALAR
:
1436 if (var
->u
.scalar
.type
&& TYPE_OBJFILE (var
->u
.scalar
.type
) == objfile
)
1438 = copy_type_recursive (objfile
, var
->u
.scalar
.type
, copied_types
);
1441 case INTERNALVAR_VALUE
:
1442 preserve_one_value (var
->u
.value
, objfile
, copied_types
);
1447 /* Update the internal variables and value history when OBJFILE is
1448 discarded; we must copy the types out of the objfile. New global types
1449 will be created for every convenience variable which currently points to
1450 this objfile's types, and the convenience variables will be adjusted to
1451 use the new global types. */
1454 preserve_values (struct objfile
*objfile
)
1456 htab_t copied_types
;
1457 struct value_history_chunk
*cur
;
1458 struct internalvar
*var
;
1462 /* Create the hash table. We allocate on the objfile's obstack, since
1463 it is soon to be deleted. */
1464 copied_types
= create_copied_types_hash (objfile
);
1466 for (cur
= value_history_chain
; cur
; cur
= cur
->next
)
1467 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
1469 preserve_one_value (cur
->values
[i
], objfile
, copied_types
);
1471 for (var
= internalvars
; var
; var
= var
->next
)
1472 preserve_one_internalvar (var
, objfile
, copied_types
);
1474 for (val
= values_in_python
; val
; val
= val
->next
)
1475 preserve_one_value (val
, objfile
, copied_types
);
1477 htab_delete (copied_types
);
1481 show_convenience (char *ignore
, int from_tty
)
1483 struct gdbarch
*gdbarch
= get_current_arch ();
1484 struct internalvar
*var
;
1486 struct value_print_options opts
;
1488 get_user_print_options (&opts
);
1489 for (var
= internalvars
; var
; var
= var
->next
)
1495 printf_filtered (("$%s = "), var
->name
);
1496 value_print (value_of_internalvar (gdbarch
, var
), gdb_stdout
,
1498 printf_filtered (("\n"));
1501 printf_unfiltered (_("\
1502 No debugger convenience variables now defined.\n\
1503 Convenience variables have names starting with \"$\";\n\
1504 use \"set\" as in \"set $foo = 5\" to define them.\n"));
1507 /* Extract a value as a C number (either long or double).
1508 Knows how to convert fixed values to double, or
1509 floating values to long.
1510 Does not deallocate the value. */
1513 value_as_long (struct value
*val
)
1515 /* This coerces arrays and functions, which is necessary (e.g.
1516 in disassemble_command). It also dereferences references, which
1517 I suspect is the most logical thing to do. */
1518 val
= coerce_array (val
);
1519 return unpack_long (value_type (val
), value_contents (val
));
1523 value_as_double (struct value
*val
)
1528 foo
= unpack_double (value_type (val
), value_contents (val
), &inv
);
1530 error (_("Invalid floating value found in program."));
1534 /* Extract a value as a C pointer. Does not deallocate the value.
1535 Note that val's type may not actually be a pointer; value_as_long
1536 handles all the cases. */
1538 value_as_address (struct value
*val
)
1540 struct gdbarch
*gdbarch
= get_type_arch (value_type (val
));
1542 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1543 whether we want this to be true eventually. */
1545 /* gdbarch_addr_bits_remove is wrong if we are being called for a
1546 non-address (e.g. argument to "signal", "info break", etc.), or
1547 for pointers to char, in which the low bits *are* significant. */
1548 return gdbarch_addr_bits_remove (gdbarch
, value_as_long (val
));
1551 /* There are several targets (IA-64, PowerPC, and others) which
1552 don't represent pointers to functions as simply the address of
1553 the function's entry point. For example, on the IA-64, a
1554 function pointer points to a two-word descriptor, generated by
1555 the linker, which contains the function's entry point, and the
1556 value the IA-64 "global pointer" register should have --- to
1557 support position-independent code. The linker generates
1558 descriptors only for those functions whose addresses are taken.
1560 On such targets, it's difficult for GDB to convert an arbitrary
1561 function address into a function pointer; it has to either find
1562 an existing descriptor for that function, or call malloc and
1563 build its own. On some targets, it is impossible for GDB to
1564 build a descriptor at all: the descriptor must contain a jump
1565 instruction; data memory cannot be executed; and code memory
1568 Upon entry to this function, if VAL is a value of type `function'
1569 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
1570 value_address (val) is the address of the function. This is what
1571 you'll get if you evaluate an expression like `main'. The call
1572 to COERCE_ARRAY below actually does all the usual unary
1573 conversions, which includes converting values of type `function'
1574 to `pointer to function'. This is the challenging conversion
1575 discussed above. Then, `unpack_long' will convert that pointer
1576 back into an address.
1578 So, suppose the user types `disassemble foo' on an architecture
1579 with a strange function pointer representation, on which GDB
1580 cannot build its own descriptors, and suppose further that `foo'
1581 has no linker-built descriptor. The address->pointer conversion
1582 will signal an error and prevent the command from running, even
1583 though the next step would have been to convert the pointer
1584 directly back into the same address.
1586 The following shortcut avoids this whole mess. If VAL is a
1587 function, just return its address directly. */
1588 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1589 || TYPE_CODE (value_type (val
)) == TYPE_CODE_METHOD
)
1590 return value_address (val
);
1592 val
= coerce_array (val
);
1594 /* Some architectures (e.g. Harvard), map instruction and data
1595 addresses onto a single large unified address space. For
1596 instance: An architecture may consider a large integer in the
1597 range 0x10000000 .. 0x1000ffff to already represent a data
1598 addresses (hence not need a pointer to address conversion) while
1599 a small integer would still need to be converted integer to
1600 pointer to address. Just assume such architectures handle all
1601 integer conversions in a single function. */
1605 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1606 must admonish GDB hackers to make sure its behavior matches the
1607 compiler's, whenever possible.
1609 In general, I think GDB should evaluate expressions the same way
1610 the compiler does. When the user copies an expression out of
1611 their source code and hands it to a `print' command, they should
1612 get the same value the compiler would have computed. Any
1613 deviation from this rule can cause major confusion and annoyance,
1614 and needs to be justified carefully. In other words, GDB doesn't
1615 really have the freedom to do these conversions in clever and
1618 AndrewC pointed out that users aren't complaining about how GDB
1619 casts integers to pointers; they are complaining that they can't
1620 take an address from a disassembly listing and give it to `x/i'.
1621 This is certainly important.
1623 Adding an architecture method like integer_to_address() certainly
1624 makes it possible for GDB to "get it right" in all circumstances
1625 --- the target has complete control over how things get done, so
1626 people can Do The Right Thing for their target without breaking
1627 anyone else. The standard doesn't specify how integers get
1628 converted to pointers; usually, the ABI doesn't either, but
1629 ABI-specific code is a more reasonable place to handle it. */
1631 if (TYPE_CODE (value_type (val
)) != TYPE_CODE_PTR
1632 && TYPE_CODE (value_type (val
)) != TYPE_CODE_REF
1633 && gdbarch_integer_to_address_p (gdbarch
))
1634 return gdbarch_integer_to_address (gdbarch
, value_type (val
),
1635 value_contents (val
));
1637 return unpack_long (value_type (val
), value_contents (val
));
1641 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1642 as a long, or as a double, assuming the raw data is described
1643 by type TYPE. Knows how to convert different sizes of values
1644 and can convert between fixed and floating point. We don't assume
1645 any alignment for the raw data. Return value is in host byte order.
1647 If you want functions and arrays to be coerced to pointers, and
1648 references to be dereferenced, call value_as_long() instead.
1650 C++: It is assumed that the front-end has taken care of
1651 all matters concerning pointers to members. A pointer
1652 to member which reaches here is considered to be equivalent
1653 to an INT (or some size). After all, it is only an offset. */
1656 unpack_long (struct type
*type
, const gdb_byte
*valaddr
)
1658 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1659 enum type_code code
= TYPE_CODE (type
);
1660 int len
= TYPE_LENGTH (type
);
1661 int nosign
= TYPE_UNSIGNED (type
);
1665 case TYPE_CODE_TYPEDEF
:
1666 return unpack_long (check_typedef (type
), valaddr
);
1667 case TYPE_CODE_ENUM
:
1668 case TYPE_CODE_FLAGS
:
1669 case TYPE_CODE_BOOL
:
1671 case TYPE_CODE_CHAR
:
1672 case TYPE_CODE_RANGE
:
1673 case TYPE_CODE_MEMBERPTR
:
1675 return extract_unsigned_integer (valaddr
, len
, byte_order
);
1677 return extract_signed_integer (valaddr
, len
, byte_order
);
1680 return extract_typed_floating (valaddr
, type
);
1682 case TYPE_CODE_DECFLOAT
:
1683 /* libdecnumber has a function to convert from decimal to integer, but
1684 it doesn't work when the decimal number has a fractional part. */
1685 return decimal_to_doublest (valaddr
, len
, byte_order
);
1689 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1690 whether we want this to be true eventually. */
1691 return extract_typed_address (valaddr
, type
);
1694 error (_("Value can't be converted to integer."));
1696 return 0; /* Placate lint. */
1699 /* Return a double value from the specified type and address.
1700 INVP points to an int which is set to 0 for valid value,
1701 1 for invalid value (bad float format). In either case,
1702 the returned double is OK to use. Argument is in target
1703 format, result is in host format. */
1706 unpack_double (struct type
*type
, const gdb_byte
*valaddr
, int *invp
)
1708 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1709 enum type_code code
;
1713 *invp
= 0; /* Assume valid. */
1714 CHECK_TYPEDEF (type
);
1715 code
= TYPE_CODE (type
);
1716 len
= TYPE_LENGTH (type
);
1717 nosign
= TYPE_UNSIGNED (type
);
1718 if (code
== TYPE_CODE_FLT
)
1720 /* NOTE: cagney/2002-02-19: There was a test here to see if the
1721 floating-point value was valid (using the macro
1722 INVALID_FLOAT). That test/macro have been removed.
1724 It turns out that only the VAX defined this macro and then
1725 only in a non-portable way. Fixing the portability problem
1726 wouldn't help since the VAX floating-point code is also badly
1727 bit-rotten. The target needs to add definitions for the
1728 methods gdbarch_float_format and gdbarch_double_format - these
1729 exactly describe the target floating-point format. The
1730 problem here is that the corresponding floatformat_vax_f and
1731 floatformat_vax_d values these methods should be set to are
1732 also not defined either. Oops!
1734 Hopefully someone will add both the missing floatformat
1735 definitions and the new cases for floatformat_is_valid (). */
1737 if (!floatformat_is_valid (floatformat_from_type (type
), valaddr
))
1743 return extract_typed_floating (valaddr
, type
);
1745 else if (code
== TYPE_CODE_DECFLOAT
)
1746 return decimal_to_doublest (valaddr
, len
, byte_order
);
1749 /* Unsigned -- be sure we compensate for signed LONGEST. */
1750 return (ULONGEST
) unpack_long (type
, valaddr
);
1754 /* Signed -- we are OK with unpack_long. */
1755 return unpack_long (type
, valaddr
);
1759 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1760 as a CORE_ADDR, assuming the raw data is described by type TYPE.
1761 We don't assume any alignment for the raw data. Return value is in
1764 If you want functions and arrays to be coerced to pointers, and
1765 references to be dereferenced, call value_as_address() instead.
1767 C++: It is assumed that the front-end has taken care of
1768 all matters concerning pointers to members. A pointer
1769 to member which reaches here is considered to be equivalent
1770 to an INT (or some size). After all, it is only an offset. */
1773 unpack_pointer (struct type
*type
, const gdb_byte
*valaddr
)
1775 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1776 whether we want this to be true eventually. */
1777 return unpack_long (type
, valaddr
);
1781 /* Get the value of the FIELDN'th field (which must be static) of
1782 TYPE. Return NULL if the field doesn't exist or has been
1786 value_static_field (struct type
*type
, int fieldno
)
1788 struct value
*retval
;
1790 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1792 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1793 TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1797 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1798 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0);
1801 /* With some compilers, e.g. HP aCC, static data members are reported
1802 as non-debuggable symbols */
1803 struct minimal_symbol
*msym
= lookup_minimal_symbol (phys_name
, NULL
, NULL
);
1808 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1809 SYMBOL_VALUE_ADDRESS (msym
));
1814 /* SYM should never have a SYMBOL_CLASS which will require
1815 read_var_value to use the FRAME parameter. */
1816 if (symbol_read_needs_frame (sym
))
1817 warning (_("static field's value depends on the current "
1818 "frame - bad debug info?"));
1819 retval
= read_var_value (sym
, NULL
);
1821 if (retval
&& VALUE_LVAL (retval
) == lval_memory
)
1822 SET_FIELD_PHYSADDR (TYPE_FIELD (type
, fieldno
),
1823 value_address (retval
));
1828 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1829 You have to be careful here, since the size of the data area for the value
1830 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
1831 than the old enclosing type, you have to allocate more space for the data.
1832 The return value is a pointer to the new version of this value structure. */
1835 value_change_enclosing_type (struct value
*val
, struct type
*new_encl_type
)
1837 if (TYPE_LENGTH (new_encl_type
) > TYPE_LENGTH (value_enclosing_type (val
)))
1839 (gdb_byte
*) xrealloc (val
->contents
, TYPE_LENGTH (new_encl_type
));
1841 val
->enclosing_type
= new_encl_type
;
1845 /* Given a value ARG1 (offset by OFFSET bytes)
1846 of a struct or union type ARG_TYPE,
1847 extract and return the value of one of its (non-static) fields.
1848 FIELDNO says which field. */
1851 value_primitive_field (struct value
*arg1
, int offset
,
1852 int fieldno
, struct type
*arg_type
)
1857 CHECK_TYPEDEF (arg_type
);
1858 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
1860 /* Handle packed fields */
1862 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
1864 v
= value_from_longest (type
,
1865 unpack_field_as_long (arg_type
,
1866 value_contents (arg1
)
1869 v
->bitpos
= TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
1870 v
->bitsize
= TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
1871 v
->offset
= value_offset (arg1
) + offset
1872 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1874 else if (fieldno
< TYPE_N_BASECLASSES (arg_type
))
1876 /* This field is actually a base subobject, so preserve the
1877 entire object's contents for later references to virtual
1880 /* Lazy register values with offsets are not supported. */
1881 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
1882 value_fetch_lazy (arg1
);
1884 if (value_lazy (arg1
))
1885 v
= allocate_value_lazy (value_enclosing_type (arg1
));
1888 v
= allocate_value (value_enclosing_type (arg1
));
1889 memcpy (value_contents_all_raw (v
), value_contents_all_raw (arg1
),
1890 TYPE_LENGTH (value_enclosing_type (arg1
)));
1893 v
->offset
= value_offset (arg1
);
1894 v
->embedded_offset
= (offset
+ value_embedded_offset (arg1
)
1895 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8);
1899 /* Plain old data member */
1900 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1902 /* Lazy register values with offsets are not supported. */
1903 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
1904 value_fetch_lazy (arg1
);
1906 if (value_lazy (arg1
))
1907 v
= allocate_value_lazy (type
);
1910 v
= allocate_value (type
);
1911 memcpy (value_contents_raw (v
),
1912 value_contents_raw (arg1
) + offset
,
1913 TYPE_LENGTH (type
));
1915 v
->offset
= (value_offset (arg1
) + offset
1916 + value_embedded_offset (arg1
));
1918 set_value_component_location (v
, arg1
);
1919 VALUE_REGNUM (v
) = VALUE_REGNUM (arg1
);
1920 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (arg1
);
1924 /* Given a value ARG1 of a struct or union type,
1925 extract and return the value of one of its (non-static) fields.
1926 FIELDNO says which field. */
1929 value_field (struct value
*arg1
, int fieldno
)
1931 return value_primitive_field (arg1
, 0, fieldno
, value_type (arg1
));
1934 /* Return a non-virtual function as a value.
1935 F is the list of member functions which contains the desired method.
1936 J is an index into F which provides the desired method.
1938 We only use the symbol for its address, so be happy with either a
1939 full symbol or a minimal symbol.
1943 value_fn_field (struct value
**arg1p
, struct fn_field
*f
, int j
, struct type
*type
,
1947 struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
1948 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1950 struct minimal_symbol
*msym
;
1952 sym
= lookup_symbol (physname
, 0, VAR_DOMAIN
, 0);
1959 gdb_assert (sym
== NULL
);
1960 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
1965 v
= allocate_value (ftype
);
1968 set_value_address (v
, BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)));
1972 /* The minimal symbol might point to a function descriptor;
1973 resolve it to the actual code address instead. */
1974 struct objfile
*objfile
= msymbol_objfile (msym
);
1975 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1977 set_value_address (v
,
1978 gdbarch_convert_from_func_ptr_addr
1979 (gdbarch
, SYMBOL_VALUE_ADDRESS (msym
), ¤t_target
));
1984 if (type
!= value_type (*arg1p
))
1985 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
1986 value_addr (*arg1p
)));
1988 /* Move the `this' pointer according to the offset.
1989 VALUE_OFFSET (*arg1p) += offset;
1997 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
2000 Extracting bits depends on endianness of the machine. Compute the
2001 number of least significant bits to discard. For big endian machines,
2002 we compute the total number of bits in the anonymous object, subtract
2003 off the bit count from the MSB of the object to the MSB of the
2004 bitfield, then the size of the bitfield, which leaves the LSB discard
2005 count. For little endian machines, the discard count is simply the
2006 number of bits from the LSB of the anonymous object to the LSB of the
2009 If the field is signed, we also do sign extension. */
2012 unpack_field_as_long (struct type
*type
, const gdb_byte
*valaddr
, int fieldno
)
2014 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2017 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
2018 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
2020 struct type
*field_type
;
2022 val
= extract_unsigned_integer (valaddr
+ bitpos
/ 8,
2023 sizeof (val
), byte_order
);
2024 field_type
= TYPE_FIELD_TYPE (type
, fieldno
);
2025 CHECK_TYPEDEF (field_type
);
2027 /* Extract bits. See comment above. */
2029 if (gdbarch_bits_big_endian (get_type_arch (type
)))
2030 lsbcount
= (sizeof val
* 8 - bitpos
% 8 - bitsize
);
2032 lsbcount
= (bitpos
% 8);
2035 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
2036 If the field is signed, and is negative, then sign extend. */
2038 if ((bitsize
> 0) && (bitsize
< 8 * (int) sizeof (val
)))
2040 valmask
= (((ULONGEST
) 1) << bitsize
) - 1;
2042 if (!TYPE_UNSIGNED (field_type
))
2044 if (val
& (valmask
^ (valmask
>> 1)))
2053 /* Modify the value of a bitfield. ADDR points to a block of memory in
2054 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
2055 is the desired value of the field, in host byte order. BITPOS and BITSIZE
2056 indicate which bits (in target bit order) comprise the bitfield.
2057 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
2058 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
2061 modify_field (struct type
*type
, gdb_byte
*addr
,
2062 LONGEST fieldval
, int bitpos
, int bitsize
)
2064 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2066 ULONGEST mask
= (ULONGEST
) -1 >> (8 * sizeof (ULONGEST
) - bitsize
);
2068 /* If a negative fieldval fits in the field in question, chop
2069 off the sign extension bits. */
2070 if ((~fieldval
& ~(mask
>> 1)) == 0)
2073 /* Warn if value is too big to fit in the field in question. */
2074 if (0 != (fieldval
& ~mask
))
2076 /* FIXME: would like to include fieldval in the message, but
2077 we don't have a sprintf_longest. */
2078 warning (_("Value does not fit in %d bits."), bitsize
);
2080 /* Truncate it, otherwise adjoining fields may be corrupted. */
2084 oword
= extract_unsigned_integer (addr
, sizeof oword
, byte_order
);
2086 /* Shifting for bit field depends on endianness of the target machine. */
2087 if (gdbarch_bits_big_endian (get_type_arch (type
)))
2088 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
2090 oword
&= ~(mask
<< bitpos
);
2091 oword
|= fieldval
<< bitpos
;
2093 store_unsigned_integer (addr
, sizeof oword
, byte_order
, oword
);
2096 /* Pack NUM into BUF using a target format of TYPE. */
2099 pack_long (gdb_byte
*buf
, struct type
*type
, LONGEST num
)
2101 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2104 type
= check_typedef (type
);
2105 len
= TYPE_LENGTH (type
);
2107 switch (TYPE_CODE (type
))
2110 case TYPE_CODE_CHAR
:
2111 case TYPE_CODE_ENUM
:
2112 case TYPE_CODE_FLAGS
:
2113 case TYPE_CODE_BOOL
:
2114 case TYPE_CODE_RANGE
:
2115 case TYPE_CODE_MEMBERPTR
:
2116 store_signed_integer (buf
, len
, byte_order
, num
);
2121 store_typed_address (buf
, type
, (CORE_ADDR
) num
);
2125 error (_("Unexpected type (%d) encountered for integer constant."),
2131 /* Convert C numbers into newly allocated values. */
2134 value_from_longest (struct type
*type
, LONGEST num
)
2136 struct value
*val
= allocate_value (type
);
2138 pack_long (value_contents_raw (val
), type
, num
);
2144 /* Create a value representing a pointer of type TYPE to the address
2147 value_from_pointer (struct type
*type
, CORE_ADDR addr
)
2149 struct value
*val
= allocate_value (type
);
2150 store_typed_address (value_contents_raw (val
), type
, addr
);
2155 /* Create a value of type TYPE whose contents come from VALADDR, if it
2156 is non-null, and whose memory address (in the inferior) is
2160 value_from_contents_and_address (struct type
*type
,
2161 const gdb_byte
*valaddr
,
2164 struct value
*v
= allocate_value (type
);
2165 if (valaddr
== NULL
)
2166 set_value_lazy (v
, 1);
2168 memcpy (value_contents_raw (v
), valaddr
, TYPE_LENGTH (type
));
2169 set_value_address (v
, address
);
2170 VALUE_LVAL (v
) = lval_memory
;
2175 value_from_double (struct type
*type
, DOUBLEST num
)
2177 struct value
*val
= allocate_value (type
);
2178 struct type
*base_type
= check_typedef (type
);
2179 enum type_code code
= TYPE_CODE (base_type
);
2180 int len
= TYPE_LENGTH (base_type
);
2182 if (code
== TYPE_CODE_FLT
)
2184 store_typed_floating (value_contents_raw (val
), base_type
, num
);
2187 error (_("Unexpected type encountered for floating constant."));
2193 value_from_decfloat (struct type
*type
, const gdb_byte
*dec
)
2195 struct value
*val
= allocate_value (type
);
2197 memcpy (value_contents_raw (val
), dec
, TYPE_LENGTH (type
));
2203 coerce_ref (struct value
*arg
)
2205 struct type
*value_type_arg_tmp
= check_typedef (value_type (arg
));
2206 if (TYPE_CODE (value_type_arg_tmp
) == TYPE_CODE_REF
)
2207 arg
= value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp
),
2208 unpack_pointer (value_type (arg
),
2209 value_contents (arg
)));
2214 coerce_array (struct value
*arg
)
2218 arg
= coerce_ref (arg
);
2219 type
= check_typedef (value_type (arg
));
2221 switch (TYPE_CODE (type
))
2223 case TYPE_CODE_ARRAY
:
2224 if (current_language
->c_style_arrays
)
2225 arg
= value_coerce_array (arg
);
2227 case TYPE_CODE_FUNC
:
2228 arg
= value_coerce_function (arg
);
2235 /* Return true if the function returning the specified type is using
2236 the convention of returning structures in memory (passing in the
2237 address as a hidden first parameter). */
2240 using_struct_return (struct gdbarch
*gdbarch
,
2241 struct type
*func_type
, struct type
*value_type
)
2243 enum type_code code
= TYPE_CODE (value_type
);
2245 if (code
== TYPE_CODE_ERROR
)
2246 error (_("Function return type unknown."));
2248 if (code
== TYPE_CODE_VOID
)
2249 /* A void return value is never in memory. See also corresponding
2250 code in "print_return_value". */
2253 /* Probe the architecture for the return-value convention. */
2254 return (gdbarch_return_value (gdbarch
, func_type
, value_type
,
2256 != RETURN_VALUE_REGISTER_CONVENTION
);
2259 /* Set the initialized field in a value struct. */
2262 set_value_initialized (struct value
*val
, int status
)
2264 val
->initialized
= status
;
2267 /* Return the initialized field in a value struct. */
2270 value_initialized (struct value
*val
)
2272 return val
->initialized
;
2276 _initialize_values (void)
2278 add_cmd ("convenience", no_class
, show_convenience
, _("\
2279 Debugger convenience (\"$foo\") variables.\n\
2280 These variables are created when you assign them values;\n\
2281 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
2283 A few convenience variables are given values automatically:\n\
2284 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
2285 \"$__\" holds the contents of the last address examined with \"x\"."),
2288 add_cmd ("values", no_class
, show_values
,
2289 _("Elements of value history around item number IDX (or last ten)."),
2292 add_com ("init-if-undefined", class_vars
, init_if_undefined_command
, _("\
2293 Initialize a convenience variable if necessary.\n\
2294 init-if-undefined VARIABLE = EXPRESSION\n\
2295 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
2296 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
2297 VARIABLE is already initialized."));
2299 add_prefix_cmd ("function", no_class
, function_command
, _("\
2300 Placeholder command for showing help on convenience functions."),
2301 &functionlist
, "function ", 0, &cmdlist
);