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 "gdb_string.h"
34 #include "gdb_assert.h"
40 #include "cli/cli-decode.h"
42 #include "python/python.h"
44 /* Prototypes for exported functions. */
46 void _initialize_values (void);
48 /* Definition of a user function. */
49 struct internal_function
51 /* The name of the function. It is a bit odd to have this in the
52 function itself -- the user might use a differently-named
53 convenience variable to hold the function. */
57 internal_function_fn handler
;
59 /* User data for the handler. */
63 static struct cmd_list_element
*functionlist
;
67 /* Type of value; either not an lval, or one of the various
68 different possible kinds of lval. */
71 /* Is it modifiable? Only relevant if lval != not_lval. */
74 /* Location of value (if lval). */
77 /* If lval == lval_memory, this is the address in the inferior.
78 If lval == lval_register, this is the byte offset into the
79 registers structure. */
82 /* Pointer to internal variable. */
83 struct internalvar
*internalvar
;
85 /* If lval == lval_computed, this is a set of function pointers
86 to use to access and describe the value, and a closure pointer
90 struct lval_funcs
*funcs
; /* Functions to call. */
91 void *closure
; /* Closure for those functions to use. */
95 /* Describes offset of a value within lval of a structure in bytes.
96 If lval == lval_memory, this is an offset to the address. If
97 lval == lval_register, this is a further offset from
98 location.address within the registers structure. Note also the
99 member embedded_offset below. */
102 /* Only used for bitfields; number of bits contained in them. */
105 /* Only used for bitfields; position of start of field. For
106 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
107 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
110 /* Frame register value is relative to. This will be described in
111 the lval enum above as "lval_register". */
112 struct frame_id frame_id
;
114 /* Type of the value. */
117 /* If a value represents a C++ object, then the `type' field gives
118 the object's compile-time type. If the object actually belongs
119 to some class derived from `type', perhaps with other base
120 classes and additional members, then `type' is just a subobject
121 of the real thing, and the full object is probably larger than
122 `type' would suggest.
124 If `type' is a dynamic class (i.e. one with a vtable), then GDB
125 can actually determine the object's run-time type by looking at
126 the run-time type information in the vtable. When this
127 information is available, we may elect to read in the entire
128 object, for several reasons:
130 - When printing the value, the user would probably rather see the
131 full object, not just the limited portion apparent from the
134 - If `type' has virtual base classes, then even printing `type'
135 alone may require reaching outside the `type' portion of the
136 object to wherever the virtual base class has been stored.
138 When we store the entire object, `enclosing_type' is the run-time
139 type -- the complete object -- and `embedded_offset' is the
140 offset of `type' within that larger type, in bytes. The
141 value_contents() macro takes `embedded_offset' into account, so
142 most GDB code continues to see the `type' portion of the value,
143 just as the inferior would.
145 If `type' is a pointer to an object, then `enclosing_type' is a
146 pointer to the object's run-time type, and `pointed_to_offset' is
147 the offset in bytes from the full object to the pointed-to object
148 -- that is, the value `embedded_offset' would have if we followed
149 the pointer and fetched the complete object. (I don't really see
150 the point. Why not just determine the run-time type when you
151 indirect, and avoid the special case? The contents don't matter
152 until you indirect anyway.)
154 If we're not doing anything fancy, `enclosing_type' is equal to
155 `type', and `embedded_offset' is zero, so everything works
157 struct type
*enclosing_type
;
159 int pointed_to_offset
;
161 /* Values are stored in a chain, so that they can be deleted easily
162 over calls to the inferior. Values assigned to internal
163 variables, put into the value history or exposed to Python are
164 taken off this list. */
167 /* Register number if the value is from a register. */
170 /* If zero, contents of this value are in the contents field. If
171 nonzero, contents are in inferior. If the lval field is lval_memory,
172 the contents are in inferior memory at location.address plus offset.
173 The lval field may also be lval_register.
175 WARNING: This field is used by the code which handles watchpoints
176 (see breakpoint.c) to decide whether a particular value can be
177 watched by hardware watchpoints. If the lazy flag is set for
178 some member of a value chain, it is assumed that this member of
179 the chain doesn't need to be watched as part of watching the
180 value itself. This is how GDB avoids watching the entire struct
181 or array when the user wants to watch a single struct member or
182 array element. If you ever change the way lazy flag is set and
183 reset, be sure to consider this use as well! */
186 /* If nonzero, this is the value of a variable which does not
187 actually exist in the program. */
190 /* If value is a variable, is it initialized or not. */
193 /* Actual contents of the value. Target byte-order. NULL or not
194 valid if lazy is nonzero. */
198 /* Prototypes for local functions. */
200 static void show_values (char *, int);
202 static void show_convenience (char *, int);
205 /* The value-history records all the values printed
206 by print commands during this session. Each chunk
207 records 60 consecutive values. The first chunk on
208 the chain records the most recent values.
209 The total number of values is in value_history_count. */
211 #define VALUE_HISTORY_CHUNK 60
213 struct value_history_chunk
215 struct value_history_chunk
*next
;
216 struct value
*values
[VALUE_HISTORY_CHUNK
];
219 /* Chain of chunks now in use. */
221 static struct value_history_chunk
*value_history_chain
;
223 static int value_history_count
; /* Abs number of last entry stored */
226 /* List of all value objects currently allocated
227 (except for those released by calls to release_value)
228 This is so they can be freed after each command. */
230 static struct value
*all_values
;
232 /* Allocate a lazy value for type TYPE. Its actual content is
233 "lazily" allocated too: the content field of the return value is
234 NULL; it will be allocated when it is fetched from the target. */
237 allocate_value_lazy (struct type
*type
)
240 struct type
*atype
= check_typedef (type
);
242 val
= (struct value
*) xzalloc (sizeof (struct value
));
243 val
->contents
= NULL
;
244 val
->next
= all_values
;
247 val
->enclosing_type
= type
;
248 VALUE_LVAL (val
) = not_lval
;
249 val
->location
.address
= 0;
250 VALUE_FRAME_ID (val
) = null_frame_id
;
254 VALUE_REGNUM (val
) = -1;
256 val
->optimized_out
= 0;
257 val
->embedded_offset
= 0;
258 val
->pointed_to_offset
= 0;
260 val
->initialized
= 1; /* Default to initialized. */
264 /* Allocate the contents of VAL if it has not been allocated yet. */
267 allocate_value_contents (struct value
*val
)
270 val
->contents
= (gdb_byte
*) xzalloc (TYPE_LENGTH (val
->enclosing_type
));
273 /* Allocate a value and its contents for type TYPE. */
276 allocate_value (struct type
*type
)
278 struct value
*val
= allocate_value_lazy (type
);
279 allocate_value_contents (val
);
284 /* Allocate a value that has the correct length
285 for COUNT repetitions of type TYPE. */
288 allocate_repeat_value (struct type
*type
, int count
)
290 int low_bound
= current_language
->string_lower_bound
; /* ??? */
291 /* FIXME-type-allocation: need a way to free this type when we are
293 struct type
*range_type
294 = create_range_type ((struct type
*) NULL
, builtin_type_int32
,
295 low_bound
, count
+ low_bound
- 1);
296 /* FIXME-type-allocation: need a way to free this type when we are
298 return allocate_value (create_array_type ((struct type
*) NULL
,
302 /* Needed if another module needs to maintain its on list of values. */
304 value_prepend_to_list (struct value
**head
, struct value
*val
)
310 /* Needed if another module needs to maintain its on list of values. */
312 value_remove_from_list (struct value
**head
, struct value
*val
)
317 *head
= (*head
)->next
;
319 for (prev
= *head
; prev
->next
; prev
= prev
->next
)
320 if (prev
->next
== val
)
322 prev
->next
= val
->next
;
328 allocate_computed_value (struct type
*type
,
329 struct lval_funcs
*funcs
,
332 struct value
*v
= allocate_value (type
);
333 VALUE_LVAL (v
) = lval_computed
;
334 v
->location
.computed
.funcs
= funcs
;
335 v
->location
.computed
.closure
= closure
;
336 set_value_lazy (v
, 1);
341 /* Accessor methods. */
344 value_next (struct value
*value
)
350 value_type (struct value
*value
)
355 deprecated_set_value_type (struct value
*value
, struct type
*type
)
361 value_offset (struct value
*value
)
363 return value
->offset
;
366 set_value_offset (struct value
*value
, int offset
)
368 value
->offset
= offset
;
372 value_bitpos (struct value
*value
)
374 return value
->bitpos
;
377 set_value_bitpos (struct value
*value
, int bit
)
383 value_bitsize (struct value
*value
)
385 return value
->bitsize
;
388 set_value_bitsize (struct value
*value
, int bit
)
390 value
->bitsize
= bit
;
394 value_contents_raw (struct value
*value
)
396 allocate_value_contents (value
);
397 return value
->contents
+ value
->embedded_offset
;
401 value_contents_all_raw (struct value
*value
)
403 allocate_value_contents (value
);
404 return value
->contents
;
408 value_enclosing_type (struct value
*value
)
410 return value
->enclosing_type
;
414 value_contents_all (struct value
*value
)
417 value_fetch_lazy (value
);
418 return value
->contents
;
422 value_lazy (struct value
*value
)
428 set_value_lazy (struct value
*value
, int val
)
434 value_contents (struct value
*value
)
436 return value_contents_writeable (value
);
440 value_contents_writeable (struct value
*value
)
443 value_fetch_lazy (value
);
444 return value_contents_raw (value
);
447 /* Return non-zero if VAL1 and VAL2 have the same contents. Note that
448 this function is different from value_equal; in C the operator ==
449 can return 0 even if the two values being compared are equal. */
452 value_contents_equal (struct value
*val1
, struct value
*val2
)
458 type1
= check_typedef (value_type (val1
));
459 type2
= check_typedef (value_type (val2
));
460 len
= TYPE_LENGTH (type1
);
461 if (len
!= TYPE_LENGTH (type2
))
464 return (memcmp (value_contents (val1
), value_contents (val2
), len
) == 0);
468 value_optimized_out (struct value
*value
)
470 return value
->optimized_out
;
474 set_value_optimized_out (struct value
*value
, int val
)
476 value
->optimized_out
= val
;
480 value_embedded_offset (struct value
*value
)
482 return value
->embedded_offset
;
486 set_value_embedded_offset (struct value
*value
, int val
)
488 value
->embedded_offset
= val
;
492 value_pointed_to_offset (struct value
*value
)
494 return value
->pointed_to_offset
;
498 set_value_pointed_to_offset (struct value
*value
, int val
)
500 value
->pointed_to_offset
= val
;
504 value_computed_funcs (struct value
*v
)
506 gdb_assert (VALUE_LVAL (v
) == lval_computed
);
508 return v
->location
.computed
.funcs
;
512 value_computed_closure (struct value
*v
)
514 gdb_assert (VALUE_LVAL (v
) == lval_computed
);
516 return v
->location
.computed
.closure
;
520 deprecated_value_lval_hack (struct value
*value
)
526 value_address (struct value
*value
)
528 if (value
->lval
== lval_internalvar
529 || value
->lval
== lval_internalvar_component
)
531 return value
->location
.address
+ value
->offset
;
535 value_raw_address (struct value
*value
)
537 if (value
->lval
== lval_internalvar
538 || value
->lval
== lval_internalvar_component
)
540 return value
->location
.address
;
544 set_value_address (struct value
*value
, CORE_ADDR addr
)
546 gdb_assert (value
->lval
!= lval_internalvar
547 && value
->lval
!= lval_internalvar_component
);
548 value
->location
.address
= addr
;
551 struct internalvar
**
552 deprecated_value_internalvar_hack (struct value
*value
)
554 return &value
->location
.internalvar
;
558 deprecated_value_frame_id_hack (struct value
*value
)
560 return &value
->frame_id
;
564 deprecated_value_regnum_hack (struct value
*value
)
566 return &value
->regnum
;
570 deprecated_value_modifiable (struct value
*value
)
572 return value
->modifiable
;
575 deprecated_set_value_modifiable (struct value
*value
, int modifiable
)
577 value
->modifiable
= modifiable
;
580 /* Return a mark in the value chain. All values allocated after the
581 mark is obtained (except for those released) are subject to being freed
582 if a subsequent value_free_to_mark is passed the mark. */
590 value_free (struct value
*val
)
594 if (VALUE_LVAL (val
) == lval_computed
)
596 struct lval_funcs
*funcs
= val
->location
.computed
.funcs
;
598 if (funcs
->free_closure
)
599 funcs
->free_closure (val
);
602 xfree (val
->contents
);
607 /* Free all values allocated since MARK was obtained by value_mark
608 (except for those released). */
610 value_free_to_mark (struct value
*mark
)
615 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
623 /* Free all the values that have been allocated (except for those released).
624 Called after each command, successful or not. */
627 free_all_values (void)
632 for (val
= all_values
; val
; val
= next
)
641 /* Remove VAL from the chain all_values
642 so it will not be freed automatically. */
645 release_value (struct value
*val
)
649 if (all_values
== val
)
651 all_values
= val
->next
;
655 for (v
= all_values
; v
; v
= v
->next
)
665 /* Release all values up to mark */
667 value_release_to_mark (struct value
*mark
)
672 for (val
= next
= all_values
; next
; next
= next
->next
)
673 if (next
->next
== mark
)
675 all_values
= next
->next
;
683 /* Return a copy of the value ARG.
684 It contains the same contents, for same memory address,
685 but it's a different block of storage. */
688 value_copy (struct value
*arg
)
690 struct type
*encl_type
= value_enclosing_type (arg
);
693 if (value_lazy (arg
))
694 val
= allocate_value_lazy (encl_type
);
696 val
= allocate_value (encl_type
);
697 val
->type
= arg
->type
;
698 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
699 val
->location
= arg
->location
;
700 val
->offset
= arg
->offset
;
701 val
->bitpos
= arg
->bitpos
;
702 val
->bitsize
= arg
->bitsize
;
703 VALUE_FRAME_ID (val
) = VALUE_FRAME_ID (arg
);
704 VALUE_REGNUM (val
) = VALUE_REGNUM (arg
);
705 val
->lazy
= arg
->lazy
;
706 val
->optimized_out
= arg
->optimized_out
;
707 val
->embedded_offset
= value_embedded_offset (arg
);
708 val
->pointed_to_offset
= arg
->pointed_to_offset
;
709 val
->modifiable
= arg
->modifiable
;
710 if (!value_lazy (val
))
712 memcpy (value_contents_all_raw (val
), value_contents_all_raw (arg
),
713 TYPE_LENGTH (value_enclosing_type (arg
)));
716 if (VALUE_LVAL (val
) == lval_computed
)
718 struct lval_funcs
*funcs
= val
->location
.computed
.funcs
;
720 if (funcs
->copy_closure
)
721 val
->location
.computed
.closure
= funcs
->copy_closure (val
);
727 set_value_component_location (struct value
*component
, struct value
*whole
)
729 if (VALUE_LVAL (whole
) == lval_internalvar
)
730 VALUE_LVAL (component
) = lval_internalvar_component
;
732 VALUE_LVAL (component
) = VALUE_LVAL (whole
);
734 component
->location
= whole
->location
;
735 if (VALUE_LVAL (whole
) == lval_computed
)
737 struct lval_funcs
*funcs
= whole
->location
.computed
.funcs
;
739 if (funcs
->copy_closure
)
740 component
->location
.computed
.closure
= funcs
->copy_closure (whole
);
745 /* Access to the value history. */
747 /* Record a new value in the value history.
748 Returns the absolute history index of the entry.
749 Result of -1 indicates the value was not saved; otherwise it is the
750 value history index of this new item. */
753 record_latest_value (struct value
*val
)
757 /* We don't want this value to have anything to do with the inferior anymore.
758 In particular, "set $1 = 50" should not affect the variable from which
759 the value was taken, and fast watchpoints should be able to assume that
760 a value on the value history never changes. */
761 if (value_lazy (val
))
762 value_fetch_lazy (val
);
763 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
764 from. This is a bit dubious, because then *&$1 does not just return $1
765 but the current contents of that location. c'est la vie... */
769 /* Here we treat value_history_count as origin-zero
770 and applying to the value being stored now. */
772 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
775 struct value_history_chunk
*new
776 = (struct value_history_chunk
*)
777 xmalloc (sizeof (struct value_history_chunk
));
778 memset (new->values
, 0, sizeof new->values
);
779 new->next
= value_history_chain
;
780 value_history_chain
= new;
783 value_history_chain
->values
[i
] = val
;
785 /* Now we regard value_history_count as origin-one
786 and applying to the value just stored. */
788 return ++value_history_count
;
791 /* Return a copy of the value in the history with sequence number NUM. */
794 access_value_history (int num
)
796 struct value_history_chunk
*chunk
;
801 absnum
+= value_history_count
;
806 error (_("The history is empty."));
808 error (_("There is only one value in the history."));
810 error (_("History does not go back to $$%d."), -num
);
812 if (absnum
> value_history_count
)
813 error (_("History has not yet reached $%d."), absnum
);
817 /* Now absnum is always absolute and origin zero. */
819 chunk
= value_history_chain
;
820 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
824 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
828 show_values (char *num_exp
, int from_tty
)
836 /* "show values +" should print from the stored position.
837 "show values <exp>" should print around value number <exp>. */
838 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
839 num
= parse_and_eval_long (num_exp
) - 5;
843 /* "show values" means print the last 10 values. */
844 num
= value_history_count
- 9;
850 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
852 struct value_print_options opts
;
853 val
= access_value_history (i
);
854 printf_filtered (("$%d = "), i
);
855 get_user_print_options (&opts
);
856 value_print (val
, gdb_stdout
, &opts
);
857 printf_filtered (("\n"));
860 /* The next "show values +" should start after what we just printed. */
863 /* Hitting just return after this command should do the same thing as
864 "show values +". If num_exp is null, this is unnecessary, since
865 "show values +" is not useful after "show values". */
866 if (from_tty
&& num_exp
)
873 /* Internal variables. These are variables within the debugger
874 that hold values assigned by debugger commands.
875 The user refers to them with a '$' prefix
876 that does not appear in the variable names stored internally. */
880 struct internalvar
*next
;
883 /* We support various different kinds of content of an internal variable.
884 enum internalvar_kind specifies the kind, and union internalvar_data
885 provides the data associated with this particular kind. */
887 enum internalvar_kind
889 /* The internal variable is empty. */
892 /* The value of the internal variable is provided directly as
893 a GDB value object. */
896 /* A fresh value is computed via a call-back routine on every
897 access to the internal variable. */
898 INTERNALVAR_MAKE_VALUE
,
900 /* The internal variable holds a GDB internal convenience function. */
901 INTERNALVAR_FUNCTION
,
903 /* The variable holds a simple scalar value. */
906 /* The variable holds a GDB-provided string. */
911 union internalvar_data
913 /* A value object used with INTERNALVAR_VALUE. */
916 /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */
917 internalvar_make_value make_value
;
919 /* The internal function used with INTERNALVAR_FUNCTION. */
922 struct internal_function
*function
;
923 /* True if this is the canonical name for the function. */
927 /* A scalar value used with INTERNALVAR_SCALAR. */
930 /* If type is non-NULL, it will be used as the type to generate
931 a value for this internal variable. If type is NULL, a default
932 integer type for the architecture is used. */
936 LONGEST l
; /* Used with TYPE_CODE_INT and NULL types. */
937 CORE_ADDR a
; /* Used with TYPE_CODE_PTR types. */
941 /* A string value used with INTERNALVAR_STRING. */
946 static struct internalvar
*internalvars
;
948 /* If the variable does not already exist create it and give it the value given.
949 If no value is given then the default is zero. */
951 init_if_undefined_command (char* args
, int from_tty
)
953 struct internalvar
* intvar
;
955 /* Parse the expression - this is taken from set_command(). */
956 struct expression
*expr
= parse_expression (args
);
957 register struct cleanup
*old_chain
=
958 make_cleanup (free_current_contents
, &expr
);
960 /* Validate the expression.
961 Was the expression an assignment?
962 Or even an expression at all? */
963 if (expr
->nelts
== 0 || expr
->elts
[0].opcode
!= BINOP_ASSIGN
)
964 error (_("Init-if-undefined requires an assignment expression."));
966 /* Extract the variable from the parsed expression.
967 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
968 if (expr
->elts
[1].opcode
!= OP_INTERNALVAR
)
969 error (_("The first parameter to init-if-undefined should be a GDB variable."));
970 intvar
= expr
->elts
[2].internalvar
;
972 /* Only evaluate the expression if the lvalue is void.
973 This may still fail if the expresssion is invalid. */
974 if (intvar
->kind
== INTERNALVAR_VOID
)
975 evaluate_expression (expr
);
977 do_cleanups (old_chain
);
981 /* Look up an internal variable with name NAME. NAME should not
982 normally include a dollar sign.
984 If the specified internal variable does not exist,
985 the return value is NULL. */
988 lookup_only_internalvar (const char *name
)
990 struct internalvar
*var
;
992 for (var
= internalvars
; var
; var
= var
->next
)
993 if (strcmp (var
->name
, name
) == 0)
1000 /* Create an internal variable with name NAME and with a void value.
1001 NAME should not normally include a dollar sign. */
1003 struct internalvar
*
1004 create_internalvar (const char *name
)
1006 struct internalvar
*var
;
1007 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
1008 var
->name
= concat (name
, (char *)NULL
);
1009 var
->kind
= INTERNALVAR_VOID
;
1010 var
->next
= internalvars
;
1015 /* Create an internal variable with name NAME and register FUN as the
1016 function that value_of_internalvar uses to create a value whenever
1017 this variable is referenced. NAME should not normally include a
1020 struct internalvar
*
1021 create_internalvar_type_lazy (char *name
, internalvar_make_value fun
)
1023 struct internalvar
*var
= create_internalvar (name
);
1024 var
->kind
= INTERNALVAR_MAKE_VALUE
;
1025 var
->u
.make_value
= fun
;
1029 /* Look up an internal variable with name NAME. NAME should not
1030 normally include a dollar sign.
1032 If the specified internal variable does not exist,
1033 one is created, with a void value. */
1035 struct internalvar
*
1036 lookup_internalvar (const char *name
)
1038 struct internalvar
*var
;
1040 var
= lookup_only_internalvar (name
);
1044 return create_internalvar (name
);
1047 /* Return current value of internal variable VAR. For variables that
1048 are not inherently typed, use a value type appropriate for GDBARCH. */
1051 value_of_internalvar (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1057 case INTERNALVAR_VOID
:
1058 val
= allocate_value (builtin_type (gdbarch
)->builtin_void
);
1061 case INTERNALVAR_FUNCTION
:
1062 val
= allocate_value (builtin_type (gdbarch
)->internal_fn
);
1065 case INTERNALVAR_SCALAR
:
1066 if (!var
->u
.scalar
.type
)
1067 val
= value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1068 var
->u
.scalar
.val
.l
);
1069 else if (TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_INT
)
1070 val
= value_from_longest (var
->u
.scalar
.type
, var
->u
.scalar
.val
.l
);
1071 else if (TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_PTR
)
1072 val
= value_from_pointer (var
->u
.scalar
.type
, var
->u
.scalar
.val
.a
);
1074 internal_error (__FILE__
, __LINE__
, "bad type");
1077 case INTERNALVAR_STRING
:
1078 val
= value_cstring (var
->u
.string
, strlen (var
->u
.string
),
1079 builtin_type (gdbarch
)->builtin_char
);
1082 case INTERNALVAR_VALUE
:
1083 val
= value_copy (var
->u
.value
);
1084 if (value_lazy (val
))
1085 value_fetch_lazy (val
);
1088 case INTERNALVAR_MAKE_VALUE
:
1089 val
= (*var
->u
.make_value
) (gdbarch
, var
);
1093 internal_error (__FILE__
, __LINE__
, "bad kind");
1096 /* Change the VALUE_LVAL to lval_internalvar so that future operations
1097 on this value go back to affect the original internal variable.
1099 Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
1100 no underlying modifyable state in the internal variable.
1102 Likewise, if the variable's value is a computed lvalue, we want
1103 references to it to produce another computed lvalue, where
1104 references and assignments actually operate through the
1105 computed value's functions.
1107 This means that internal variables with computed values
1108 behave a little differently from other internal variables:
1109 assignments to them don't just replace the previous value
1110 altogether. At the moment, this seems like the behavior we
1113 if (var
->kind
!= INTERNALVAR_MAKE_VALUE
1114 && val
->lval
!= lval_computed
)
1116 VALUE_LVAL (val
) = lval_internalvar
;
1117 VALUE_INTERNALVAR (val
) = var
;
1124 get_internalvar_integer (struct internalvar
*var
, LONGEST
*result
)
1128 case INTERNALVAR_SCALAR
:
1129 if (var
->u
.scalar
.type
== NULL
1130 || TYPE_CODE (var
->u
.scalar
.type
) == TYPE_CODE_INT
)
1132 *result
= var
->u
.scalar
.val
.l
;
1143 get_internalvar_function (struct internalvar
*var
,
1144 struct internal_function
**result
)
1148 case INTERNALVAR_FUNCTION
:
1149 *result
= var
->u
.fn
.function
;
1158 set_internalvar_component (struct internalvar
*var
, int offset
, int bitpos
,
1159 int bitsize
, struct value
*newval
)
1165 case INTERNALVAR_VALUE
:
1166 addr
= value_contents_writeable (var
->u
.value
);
1169 modify_field (addr
+ offset
,
1170 value_as_long (newval
), bitpos
, bitsize
);
1172 memcpy (addr
+ offset
, value_contents (newval
),
1173 TYPE_LENGTH (value_type (newval
)));
1177 /* We can never get a component of any other kind. */
1178 internal_error (__FILE__
, __LINE__
, "set_internalvar_component");
1183 set_internalvar (struct internalvar
*var
, struct value
*val
)
1185 enum internalvar_kind new_kind
;
1186 union internalvar_data new_data
= { 0 };
1188 if (var
->kind
== INTERNALVAR_FUNCTION
&& var
->u
.fn
.canonical
)
1189 error (_("Cannot overwrite convenience function %s"), var
->name
);
1191 /* Prepare new contents. */
1192 switch (TYPE_CODE (check_typedef (value_type (val
))))
1194 case TYPE_CODE_VOID
:
1195 new_kind
= INTERNALVAR_VOID
;
1198 case TYPE_CODE_INTERNAL_FUNCTION
:
1199 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
1200 new_kind
= INTERNALVAR_FUNCTION
;
1201 get_internalvar_function (VALUE_INTERNALVAR (val
),
1202 &new_data
.fn
.function
);
1203 /* Copies created here are never canonical. */
1207 new_kind
= INTERNALVAR_SCALAR
;
1208 new_data
.scalar
.type
= value_type (val
);
1209 new_data
.scalar
.val
.l
= value_as_long (val
);
1213 new_kind
= INTERNALVAR_SCALAR
;
1214 new_data
.scalar
.type
= value_type (val
);
1215 new_data
.scalar
.val
.a
= value_as_address (val
);
1219 new_kind
= INTERNALVAR_VALUE
;
1220 new_data
.value
= value_copy (val
);
1221 new_data
.value
->modifiable
= 1;
1223 /* Force the value to be fetched from the target now, to avoid problems
1224 later when this internalvar is referenced and the target is gone or
1226 if (value_lazy (new_data
.value
))
1227 value_fetch_lazy (new_data
.value
);
1229 /* Release the value from the value chain to prevent it from being
1230 deleted by free_all_values. From here on this function should not
1231 call error () until new_data is installed into the var->u to avoid
1233 release_value (new_data
.value
);
1237 /* Clean up old contents. */
1238 clear_internalvar (var
);
1241 var
->kind
= new_kind
;
1243 /* End code which must not call error(). */
1247 set_internalvar_integer (struct internalvar
*var
, LONGEST l
)
1249 /* Clean up old contents. */
1250 clear_internalvar (var
);
1252 var
->kind
= INTERNALVAR_SCALAR
;
1253 var
->u
.scalar
.type
= NULL
;
1254 var
->u
.scalar
.val
.l
= l
;
1258 set_internalvar_string (struct internalvar
*var
, const char *string
)
1260 /* Clean up old contents. */
1261 clear_internalvar (var
);
1263 var
->kind
= INTERNALVAR_STRING
;
1264 var
->u
.string
= xstrdup (string
);
1268 set_internalvar_function (struct internalvar
*var
, struct internal_function
*f
)
1270 /* Clean up old contents. */
1271 clear_internalvar (var
);
1273 var
->kind
= INTERNALVAR_FUNCTION
;
1274 var
->u
.fn
.function
= f
;
1275 var
->u
.fn
.canonical
= 1;
1276 /* Variables installed here are always the canonical version. */
1280 clear_internalvar (struct internalvar
*var
)
1282 /* Clean up old contents. */
1285 case INTERNALVAR_VALUE
:
1286 value_free (var
->u
.value
);
1289 case INTERNALVAR_STRING
:
1290 xfree (var
->u
.string
);
1297 /* Reset to void kind. */
1298 var
->kind
= INTERNALVAR_VOID
;
1302 internalvar_name (struct internalvar
*var
)
1307 static struct internal_function
*
1308 create_internal_function (const char *name
,
1309 internal_function_fn handler
, void *cookie
)
1311 struct internal_function
*ifn
= XNEW (struct internal_function
);
1312 ifn
->name
= xstrdup (name
);
1313 ifn
->handler
= handler
;
1314 ifn
->cookie
= cookie
;
1319 value_internal_function_name (struct value
*val
)
1321 struct internal_function
*ifn
;
1324 gdb_assert (VALUE_LVAL (val
) == lval_internalvar
);
1325 result
= get_internalvar_function (VALUE_INTERNALVAR (val
), &ifn
);
1326 gdb_assert (result
);
1332 call_internal_function (struct value
*func
, int argc
, struct value
**argv
)
1334 struct internal_function
*ifn
;
1337 gdb_assert (VALUE_LVAL (func
) == lval_internalvar
);
1338 result
= get_internalvar_function (VALUE_INTERNALVAR (func
), &ifn
);
1339 gdb_assert (result
);
1341 return (*ifn
->handler
) (ifn
->cookie
, argc
, argv
);
1344 /* The 'function' command. This does nothing -- it is just a
1345 placeholder to let "help function NAME" work. This is also used as
1346 the implementation of the sub-command that is created when
1347 registering an internal function. */
1349 function_command (char *command
, int from_tty
)
1354 /* Clean up if an internal function's command is destroyed. */
1356 function_destroyer (struct cmd_list_element
*self
, void *ignore
)
1362 /* Add a new internal function. NAME is the name of the function; DOC
1363 is a documentation string describing the function. HANDLER is
1364 called when the function is invoked. COOKIE is an arbitrary
1365 pointer which is passed to HANDLER and is intended for "user
1368 add_internal_function (const char *name
, const char *doc
,
1369 internal_function_fn handler
, void *cookie
)
1371 struct cmd_list_element
*cmd
;
1372 struct internal_function
*ifn
;
1373 struct internalvar
*var
= lookup_internalvar (name
);
1375 ifn
= create_internal_function (name
, handler
, cookie
);
1376 set_internalvar_function (var
, ifn
);
1378 cmd
= add_cmd (xstrdup (name
), no_class
, function_command
, (char *) doc
,
1380 cmd
->destroyer
= function_destroyer
;
1383 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
1384 prevent cycles / duplicates. */
1387 preserve_one_value (struct value
*value
, struct objfile
*objfile
,
1388 htab_t copied_types
)
1390 if (TYPE_OBJFILE (value
->type
) == objfile
)
1391 value
->type
= copy_type_recursive (objfile
, value
->type
, copied_types
);
1393 if (TYPE_OBJFILE (value
->enclosing_type
) == objfile
)
1394 value
->enclosing_type
= copy_type_recursive (objfile
,
1395 value
->enclosing_type
,
1399 /* Likewise for internal variable VAR. */
1402 preserve_one_internalvar (struct internalvar
*var
, struct objfile
*objfile
,
1403 htab_t copied_types
)
1407 case INTERNALVAR_SCALAR
:
1408 if (var
->u
.scalar
.type
&& TYPE_OBJFILE (var
->u
.scalar
.type
) == objfile
)
1410 = copy_type_recursive (objfile
, var
->u
.scalar
.type
, copied_types
);
1413 case INTERNALVAR_VALUE
:
1414 preserve_one_value (var
->u
.value
, objfile
, copied_types
);
1419 /* Update the internal variables and value history when OBJFILE is
1420 discarded; we must copy the types out of the objfile. New global types
1421 will be created for every convenience variable which currently points to
1422 this objfile's types, and the convenience variables will be adjusted to
1423 use the new global types. */
1426 preserve_values (struct objfile
*objfile
)
1428 htab_t copied_types
;
1429 struct value_history_chunk
*cur
;
1430 struct internalvar
*var
;
1434 /* Create the hash table. We allocate on the objfile's obstack, since
1435 it is soon to be deleted. */
1436 copied_types
= create_copied_types_hash (objfile
);
1438 for (cur
= value_history_chain
; cur
; cur
= cur
->next
)
1439 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
1441 preserve_one_value (cur
->values
[i
], objfile
, copied_types
);
1443 for (var
= internalvars
; var
; var
= var
->next
)
1444 preserve_one_internalvar (var
, objfile
, copied_types
);
1446 for (val
= values_in_python
; val
; val
= val
->next
)
1447 preserve_one_value (val
, objfile
, copied_types
);
1449 htab_delete (copied_types
);
1453 show_convenience (char *ignore
, int from_tty
)
1455 struct gdbarch
*gdbarch
= current_gdbarch
;
1456 struct internalvar
*var
;
1458 struct value_print_options opts
;
1460 get_user_print_options (&opts
);
1461 for (var
= internalvars
; var
; var
= var
->next
)
1467 printf_filtered (("$%s = "), var
->name
);
1468 value_print (value_of_internalvar (gdbarch
, var
), gdb_stdout
,
1470 printf_filtered (("\n"));
1473 printf_unfiltered (_("\
1474 No debugger convenience variables now defined.\n\
1475 Convenience variables have names starting with \"$\";\n\
1476 use \"set\" as in \"set $foo = 5\" to define them.\n"));
1479 /* Extract a value as a C number (either long or double).
1480 Knows how to convert fixed values to double, or
1481 floating values to long.
1482 Does not deallocate the value. */
1485 value_as_long (struct value
*val
)
1487 /* This coerces arrays and functions, which is necessary (e.g.
1488 in disassemble_command). It also dereferences references, which
1489 I suspect is the most logical thing to do. */
1490 val
= coerce_array (val
);
1491 return unpack_long (value_type (val
), value_contents (val
));
1495 value_as_double (struct value
*val
)
1500 foo
= unpack_double (value_type (val
), value_contents (val
), &inv
);
1502 error (_("Invalid floating value found in program."));
1506 /* Extract a value as a C pointer. Does not deallocate the value.
1507 Note that val's type may not actually be a pointer; value_as_long
1508 handles all the cases. */
1510 value_as_address (struct value
*val
)
1512 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1513 whether we want this to be true eventually. */
1515 /* gdbarch_addr_bits_remove is wrong if we are being called for a
1516 non-address (e.g. argument to "signal", "info break", etc.), or
1517 for pointers to char, in which the low bits *are* significant. */
1518 return gdbarch_addr_bits_remove (current_gdbarch
, value_as_long (val
));
1521 /* There are several targets (IA-64, PowerPC, and others) which
1522 don't represent pointers to functions as simply the address of
1523 the function's entry point. For example, on the IA-64, a
1524 function pointer points to a two-word descriptor, generated by
1525 the linker, which contains the function's entry point, and the
1526 value the IA-64 "global pointer" register should have --- to
1527 support position-independent code. The linker generates
1528 descriptors only for those functions whose addresses are taken.
1530 On such targets, it's difficult for GDB to convert an arbitrary
1531 function address into a function pointer; it has to either find
1532 an existing descriptor for that function, or call malloc and
1533 build its own. On some targets, it is impossible for GDB to
1534 build a descriptor at all: the descriptor must contain a jump
1535 instruction; data memory cannot be executed; and code memory
1538 Upon entry to this function, if VAL is a value of type `function'
1539 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
1540 value_address (val) is the address of the function. This is what
1541 you'll get if you evaluate an expression like `main'. The call
1542 to COERCE_ARRAY below actually does all the usual unary
1543 conversions, which includes converting values of type `function'
1544 to `pointer to function'. This is the challenging conversion
1545 discussed above. Then, `unpack_long' will convert that pointer
1546 back into an address.
1548 So, suppose the user types `disassemble foo' on an architecture
1549 with a strange function pointer representation, on which GDB
1550 cannot build its own descriptors, and suppose further that `foo'
1551 has no linker-built descriptor. The address->pointer conversion
1552 will signal an error and prevent the command from running, even
1553 though the next step would have been to convert the pointer
1554 directly back into the same address.
1556 The following shortcut avoids this whole mess. If VAL is a
1557 function, just return its address directly. */
1558 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
1559 || TYPE_CODE (value_type (val
)) == TYPE_CODE_METHOD
)
1560 return value_address (val
);
1562 val
= coerce_array (val
);
1564 /* Some architectures (e.g. Harvard), map instruction and data
1565 addresses onto a single large unified address space. For
1566 instance: An architecture may consider a large integer in the
1567 range 0x10000000 .. 0x1000ffff to already represent a data
1568 addresses (hence not need a pointer to address conversion) while
1569 a small integer would still need to be converted integer to
1570 pointer to address. Just assume such architectures handle all
1571 integer conversions in a single function. */
1575 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1576 must admonish GDB hackers to make sure its behavior matches the
1577 compiler's, whenever possible.
1579 In general, I think GDB should evaluate expressions the same way
1580 the compiler does. When the user copies an expression out of
1581 their source code and hands it to a `print' command, they should
1582 get the same value the compiler would have computed. Any
1583 deviation from this rule can cause major confusion and annoyance,
1584 and needs to be justified carefully. In other words, GDB doesn't
1585 really have the freedom to do these conversions in clever and
1588 AndrewC pointed out that users aren't complaining about how GDB
1589 casts integers to pointers; they are complaining that they can't
1590 take an address from a disassembly listing and give it to `x/i'.
1591 This is certainly important.
1593 Adding an architecture method like integer_to_address() certainly
1594 makes it possible for GDB to "get it right" in all circumstances
1595 --- the target has complete control over how things get done, so
1596 people can Do The Right Thing for their target without breaking
1597 anyone else. The standard doesn't specify how integers get
1598 converted to pointers; usually, the ABI doesn't either, but
1599 ABI-specific code is a more reasonable place to handle it. */
1601 if (TYPE_CODE (value_type (val
)) != TYPE_CODE_PTR
1602 && TYPE_CODE (value_type (val
)) != TYPE_CODE_REF
1603 && gdbarch_integer_to_address_p (current_gdbarch
))
1604 return gdbarch_integer_to_address (current_gdbarch
, value_type (val
),
1605 value_contents (val
));
1607 return unpack_long (value_type (val
), value_contents (val
));
1611 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1612 as a long, or as a double, assuming the raw data is described
1613 by type TYPE. Knows how to convert different sizes of values
1614 and can convert between fixed and floating point. We don't assume
1615 any alignment for the raw data. Return value is in host byte order.
1617 If you want functions and arrays to be coerced to pointers, and
1618 references to be dereferenced, call value_as_long() instead.
1620 C++: It is assumed that the front-end has taken care of
1621 all matters concerning pointers to members. A pointer
1622 to member which reaches here is considered to be equivalent
1623 to an INT (or some size). After all, it is only an offset. */
1626 unpack_long (struct type
*type
, const gdb_byte
*valaddr
)
1628 enum type_code code
= TYPE_CODE (type
);
1629 int len
= TYPE_LENGTH (type
);
1630 int nosign
= TYPE_UNSIGNED (type
);
1634 case TYPE_CODE_TYPEDEF
:
1635 return unpack_long (check_typedef (type
), valaddr
);
1636 case TYPE_CODE_ENUM
:
1637 case TYPE_CODE_FLAGS
:
1638 case TYPE_CODE_BOOL
:
1640 case TYPE_CODE_CHAR
:
1641 case TYPE_CODE_RANGE
:
1642 case TYPE_CODE_MEMBERPTR
:
1644 return extract_unsigned_integer (valaddr
, len
);
1646 return extract_signed_integer (valaddr
, len
);
1649 return extract_typed_floating (valaddr
, type
);
1651 case TYPE_CODE_DECFLOAT
:
1652 /* libdecnumber has a function to convert from decimal to integer, but
1653 it doesn't work when the decimal number has a fractional part. */
1654 return decimal_to_doublest (valaddr
, len
);
1658 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1659 whether we want this to be true eventually. */
1660 return extract_typed_address (valaddr
, type
);
1663 error (_("Value can't be converted to integer."));
1665 return 0; /* Placate lint. */
1668 /* Return a double value from the specified type and address.
1669 INVP points to an int which is set to 0 for valid value,
1670 1 for invalid value (bad float format). In either case,
1671 the returned double is OK to use. Argument is in target
1672 format, result is in host format. */
1675 unpack_double (struct type
*type
, const gdb_byte
*valaddr
, int *invp
)
1677 enum type_code code
;
1681 *invp
= 0; /* Assume valid. */
1682 CHECK_TYPEDEF (type
);
1683 code
= TYPE_CODE (type
);
1684 len
= TYPE_LENGTH (type
);
1685 nosign
= TYPE_UNSIGNED (type
);
1686 if (code
== TYPE_CODE_FLT
)
1688 /* NOTE: cagney/2002-02-19: There was a test here to see if the
1689 floating-point value was valid (using the macro
1690 INVALID_FLOAT). That test/macro have been removed.
1692 It turns out that only the VAX defined this macro and then
1693 only in a non-portable way. Fixing the portability problem
1694 wouldn't help since the VAX floating-point code is also badly
1695 bit-rotten. The target needs to add definitions for the
1696 methods gdbarch_float_format and gdbarch_double_format - these
1697 exactly describe the target floating-point format. The
1698 problem here is that the corresponding floatformat_vax_f and
1699 floatformat_vax_d values these methods should be set to are
1700 also not defined either. Oops!
1702 Hopefully someone will add both the missing floatformat
1703 definitions and the new cases for floatformat_is_valid (). */
1705 if (!floatformat_is_valid (floatformat_from_type (type
), valaddr
))
1711 return extract_typed_floating (valaddr
, type
);
1713 else if (code
== TYPE_CODE_DECFLOAT
)
1714 return decimal_to_doublest (valaddr
, len
);
1717 /* Unsigned -- be sure we compensate for signed LONGEST. */
1718 return (ULONGEST
) unpack_long (type
, valaddr
);
1722 /* Signed -- we are OK with unpack_long. */
1723 return unpack_long (type
, valaddr
);
1727 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1728 as a CORE_ADDR, assuming the raw data is described by type TYPE.
1729 We don't assume any alignment for the raw data. Return value is in
1732 If you want functions and arrays to be coerced to pointers, and
1733 references to be dereferenced, call value_as_address() instead.
1735 C++: It is assumed that the front-end has taken care of
1736 all matters concerning pointers to members. A pointer
1737 to member which reaches here is considered to be equivalent
1738 to an INT (or some size). After all, it is only an offset. */
1741 unpack_pointer (struct type
*type
, const gdb_byte
*valaddr
)
1743 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1744 whether we want this to be true eventually. */
1745 return unpack_long (type
, valaddr
);
1749 /* Get the value of the FIELDN'th field (which must be static) of
1750 TYPE. Return NULL if the field doesn't exist or has been
1754 value_static_field (struct type
*type
, int fieldno
)
1756 struct value
*retval
;
1758 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1760 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1761 TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1765 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1766 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0);
1769 /* With some compilers, e.g. HP aCC, static data members are reported
1770 as non-debuggable symbols */
1771 struct minimal_symbol
*msym
= lookup_minimal_symbol (phys_name
, NULL
, NULL
);
1776 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
1777 SYMBOL_VALUE_ADDRESS (msym
));
1782 /* SYM should never have a SYMBOL_CLASS which will require
1783 read_var_value to use the FRAME parameter. */
1784 if (symbol_read_needs_frame (sym
))
1785 warning (_("static field's value depends on the current "
1786 "frame - bad debug info?"));
1787 retval
= read_var_value (sym
, NULL
);
1789 if (retval
&& VALUE_LVAL (retval
) == lval_memory
)
1790 SET_FIELD_PHYSADDR (TYPE_FIELD (type
, fieldno
),
1791 value_address (retval
));
1796 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1797 You have to be careful here, since the size of the data area for the value
1798 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
1799 than the old enclosing type, you have to allocate more space for the data.
1800 The return value is a pointer to the new version of this value structure. */
1803 value_change_enclosing_type (struct value
*val
, struct type
*new_encl_type
)
1805 if (TYPE_LENGTH (new_encl_type
) > TYPE_LENGTH (value_enclosing_type (val
)))
1807 (gdb_byte
*) xrealloc (val
->contents
, TYPE_LENGTH (new_encl_type
));
1809 val
->enclosing_type
= new_encl_type
;
1813 /* Given a value ARG1 (offset by OFFSET bytes)
1814 of a struct or union type ARG_TYPE,
1815 extract and return the value of one of its (non-static) fields.
1816 FIELDNO says which field. */
1819 value_primitive_field (struct value
*arg1
, int offset
,
1820 int fieldno
, struct type
*arg_type
)
1825 CHECK_TYPEDEF (arg_type
);
1826 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
1828 /* Handle packed fields */
1830 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
1832 v
= value_from_longest (type
,
1833 unpack_field_as_long (arg_type
,
1834 value_contents (arg1
)
1837 v
->bitpos
= TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
1838 v
->bitsize
= TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
1839 v
->offset
= value_offset (arg1
) + offset
1840 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1842 else if (fieldno
< TYPE_N_BASECLASSES (arg_type
))
1844 /* This field is actually a base subobject, so preserve the
1845 entire object's contents for later references to virtual
1848 /* Lazy register values with offsets are not supported. */
1849 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
1850 value_fetch_lazy (arg1
);
1852 if (value_lazy (arg1
))
1853 v
= allocate_value_lazy (value_enclosing_type (arg1
));
1856 v
= allocate_value (value_enclosing_type (arg1
));
1857 memcpy (value_contents_all_raw (v
), value_contents_all_raw (arg1
),
1858 TYPE_LENGTH (value_enclosing_type (arg1
)));
1861 v
->offset
= value_offset (arg1
);
1862 v
->embedded_offset
= (offset
+ value_embedded_offset (arg1
)
1863 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8);
1867 /* Plain old data member */
1868 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1870 /* Lazy register values with offsets are not supported. */
1871 if (VALUE_LVAL (arg1
) == lval_register
&& value_lazy (arg1
))
1872 value_fetch_lazy (arg1
);
1874 if (value_lazy (arg1
))
1875 v
= allocate_value_lazy (type
);
1878 v
= allocate_value (type
);
1879 memcpy (value_contents_raw (v
),
1880 value_contents_raw (arg1
) + offset
,
1881 TYPE_LENGTH (type
));
1883 v
->offset
= (value_offset (arg1
) + offset
1884 + value_embedded_offset (arg1
));
1886 set_value_component_location (v
, arg1
);
1887 VALUE_REGNUM (v
) = VALUE_REGNUM (arg1
);
1888 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (arg1
);
1892 /* Given a value ARG1 of a struct or union type,
1893 extract and return the value of one of its (non-static) fields.
1894 FIELDNO says which field. */
1897 value_field (struct value
*arg1
, int fieldno
)
1899 return value_primitive_field (arg1
, 0, fieldno
, value_type (arg1
));
1902 /* Return a non-virtual function as a value.
1903 F is the list of member functions which contains the desired method.
1904 J is an index into F which provides the desired method.
1906 We only use the symbol for its address, so be happy with either a
1907 full symbol or a minimal symbol.
1911 value_fn_field (struct value
**arg1p
, struct fn_field
*f
, int j
, struct type
*type
,
1915 struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
1916 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1918 struct minimal_symbol
*msym
;
1920 sym
= lookup_symbol (physname
, 0, VAR_DOMAIN
, 0);
1927 gdb_assert (sym
== NULL
);
1928 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
1933 v
= allocate_value (ftype
);
1936 set_value_address (v
, BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)));
1940 /* The minimal symbol might point to a function descriptor;
1941 resolve it to the actual code address instead. */
1942 struct objfile
*objfile
= msymbol_objfile (msym
);
1943 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1945 set_value_address (v
,
1946 gdbarch_convert_from_func_ptr_addr
1947 (gdbarch
, SYMBOL_VALUE_ADDRESS (msym
), ¤t_target
));
1952 if (type
!= value_type (*arg1p
))
1953 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
1954 value_addr (*arg1p
)));
1956 /* Move the `this' pointer according to the offset.
1957 VALUE_OFFSET (*arg1p) += offset;
1965 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1968 Extracting bits depends on endianness of the machine. Compute the
1969 number of least significant bits to discard. For big endian machines,
1970 we compute the total number of bits in the anonymous object, subtract
1971 off the bit count from the MSB of the object to the MSB of the
1972 bitfield, then the size of the bitfield, which leaves the LSB discard
1973 count. For little endian machines, the discard count is simply the
1974 number of bits from the LSB of the anonymous object to the LSB of the
1977 If the field is signed, we also do sign extension. */
1980 unpack_field_as_long (struct type
*type
, const gdb_byte
*valaddr
, int fieldno
)
1984 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1985 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1987 struct type
*field_type
;
1989 val
= extract_unsigned_integer (valaddr
+ bitpos
/ 8, sizeof (val
));
1990 field_type
= TYPE_FIELD_TYPE (type
, fieldno
);
1991 CHECK_TYPEDEF (field_type
);
1993 /* Extract bits. See comment above. */
1995 if (gdbarch_bits_big_endian (current_gdbarch
))
1996 lsbcount
= (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1998 lsbcount
= (bitpos
% 8);
2001 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
2002 If the field is signed, and is negative, then sign extend. */
2004 if ((bitsize
> 0) && (bitsize
< 8 * (int) sizeof (val
)))
2006 valmask
= (((ULONGEST
) 1) << bitsize
) - 1;
2008 if (!TYPE_UNSIGNED (field_type
))
2010 if (val
& (valmask
^ (valmask
>> 1)))
2019 /* Modify the value of a bitfield. ADDR points to a block of memory in
2020 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
2021 is the desired value of the field, in host byte order. BITPOS and BITSIZE
2022 indicate which bits (in target bit order) comprise the bitfield.
2023 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
2024 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
2027 modify_field (gdb_byte
*addr
, LONGEST fieldval
, int bitpos
, int bitsize
)
2030 ULONGEST mask
= (ULONGEST
) -1 >> (8 * sizeof (ULONGEST
) - bitsize
);
2032 /* If a negative fieldval fits in the field in question, chop
2033 off the sign extension bits. */
2034 if ((~fieldval
& ~(mask
>> 1)) == 0)
2037 /* Warn if value is too big to fit in the field in question. */
2038 if (0 != (fieldval
& ~mask
))
2040 /* FIXME: would like to include fieldval in the message, but
2041 we don't have a sprintf_longest. */
2042 warning (_("Value does not fit in %d bits."), bitsize
);
2044 /* Truncate it, otherwise adjoining fields may be corrupted. */
2048 oword
= extract_unsigned_integer (addr
, sizeof oword
);
2050 /* Shifting for bit field depends on endianness of the target machine. */
2051 if (gdbarch_bits_big_endian (current_gdbarch
))
2052 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
2054 oword
&= ~(mask
<< bitpos
);
2055 oword
|= fieldval
<< bitpos
;
2057 store_unsigned_integer (addr
, sizeof oword
, oword
);
2060 /* Pack NUM into BUF using a target format of TYPE. */
2063 pack_long (gdb_byte
*buf
, struct type
*type
, LONGEST num
)
2067 type
= check_typedef (type
);
2068 len
= TYPE_LENGTH (type
);
2070 switch (TYPE_CODE (type
))
2073 case TYPE_CODE_CHAR
:
2074 case TYPE_CODE_ENUM
:
2075 case TYPE_CODE_FLAGS
:
2076 case TYPE_CODE_BOOL
:
2077 case TYPE_CODE_RANGE
:
2078 case TYPE_CODE_MEMBERPTR
:
2079 store_signed_integer (buf
, len
, num
);
2084 store_typed_address (buf
, type
, (CORE_ADDR
) num
);
2088 error (_("Unexpected type (%d) encountered for integer constant."),
2094 /* Convert C numbers into newly allocated values. */
2097 value_from_longest (struct type
*type
, LONGEST num
)
2099 struct value
*val
= allocate_value (type
);
2101 pack_long (value_contents_raw (val
), type
, num
);
2107 /* Create a value representing a pointer of type TYPE to the address
2110 value_from_pointer (struct type
*type
, CORE_ADDR addr
)
2112 struct value
*val
= allocate_value (type
);
2113 store_typed_address (value_contents_raw (val
), type
, addr
);
2118 /* Create a value of type TYPE whose contents come from VALADDR, if it
2119 is non-null, and whose memory address (in the inferior) is
2123 value_from_contents_and_address (struct type
*type
,
2124 const gdb_byte
*valaddr
,
2127 struct value
*v
= allocate_value (type
);
2128 if (valaddr
== NULL
)
2129 set_value_lazy (v
, 1);
2131 memcpy (value_contents_raw (v
), valaddr
, TYPE_LENGTH (type
));
2132 set_value_address (v
, address
);
2133 VALUE_LVAL (v
) = lval_memory
;
2138 value_from_double (struct type
*type
, DOUBLEST num
)
2140 struct value
*val
= allocate_value (type
);
2141 struct type
*base_type
= check_typedef (type
);
2142 enum type_code code
= TYPE_CODE (base_type
);
2143 int len
= TYPE_LENGTH (base_type
);
2145 if (code
== TYPE_CODE_FLT
)
2147 store_typed_floating (value_contents_raw (val
), base_type
, num
);
2150 error (_("Unexpected type encountered for floating constant."));
2156 value_from_decfloat (struct type
*type
, const gdb_byte
*dec
)
2158 struct value
*val
= allocate_value (type
);
2160 memcpy (value_contents_raw (val
), dec
, TYPE_LENGTH (type
));
2166 coerce_ref (struct value
*arg
)
2168 struct type
*value_type_arg_tmp
= check_typedef (value_type (arg
));
2169 if (TYPE_CODE (value_type_arg_tmp
) == TYPE_CODE_REF
)
2170 arg
= value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp
),
2171 unpack_pointer (value_type (arg
),
2172 value_contents (arg
)));
2177 coerce_array (struct value
*arg
)
2181 arg
= coerce_ref (arg
);
2182 type
= check_typedef (value_type (arg
));
2184 switch (TYPE_CODE (type
))
2186 case TYPE_CODE_ARRAY
:
2187 if (current_language
->c_style_arrays
)
2188 arg
= value_coerce_array (arg
);
2190 case TYPE_CODE_FUNC
:
2191 arg
= value_coerce_function (arg
);
2198 /* Return true if the function returning the specified type is using
2199 the convention of returning structures in memory (passing in the
2200 address as a hidden first parameter). */
2203 using_struct_return (struct gdbarch
*gdbarch
,
2204 struct type
*func_type
, struct type
*value_type
)
2206 enum type_code code
= TYPE_CODE (value_type
);
2208 if (code
== TYPE_CODE_ERROR
)
2209 error (_("Function return type unknown."));
2211 if (code
== TYPE_CODE_VOID
)
2212 /* A void return value is never in memory. See also corresponding
2213 code in "print_return_value". */
2216 /* Probe the architecture for the return-value convention. */
2217 return (gdbarch_return_value (gdbarch
, func_type
, value_type
,
2219 != RETURN_VALUE_REGISTER_CONVENTION
);
2222 /* Set the initialized field in a value struct. */
2225 set_value_initialized (struct value
*val
, int status
)
2227 val
->initialized
= status
;
2230 /* Return the initialized field in a value struct. */
2233 value_initialized (struct value
*val
)
2235 return val
->initialized
;
2239 _initialize_values (void)
2241 add_cmd ("convenience", no_class
, show_convenience
, _("\
2242 Debugger convenience (\"$foo\") variables.\n\
2243 These variables are created when you assign them values;\n\
2244 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
2246 A few convenience variables are given values automatically:\n\
2247 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
2248 \"$__\" holds the contents of the last address examined with \"x\"."),
2251 add_cmd ("values", no_class
, show_values
,
2252 _("Elements of value history around item number IDX (or last ten)."),
2255 add_com ("init-if-undefined", class_vars
, init_if_undefined_command
, _("\
2256 Initialize a convenience variable if necessary.\n\
2257 init-if-undefined VARIABLE = EXPRESSION\n\
2258 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
2259 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
2260 VARIABLE is already initialized."));
2262 add_prefix_cmd ("function", no_class
, function_command
, _("\
2263 Placeholder command for showing help on convenience functions."),
2264 &functionlist
, "function ", 0, &cmdlist
);