1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* Local function prototypes. */
34 static value_ptr value_headof
PARAMS ((value
, struct type
*, struct type
*));
36 static void show_values
PARAMS ((char *, int));
38 static void show_convenience
PARAMS ((char *, int));
40 /* The value-history records all the values printed
41 by print commands during this session. Each chunk
42 records 60 consecutive values. The first chunk on
43 the chain records the most recent values.
44 The total number of values is in value_history_count. */
46 #define VALUE_HISTORY_CHUNK 60
48 struct value_history_chunk
50 struct value_history_chunk
*next
;
51 value_ptr values
[VALUE_HISTORY_CHUNK
];
54 /* Chain of chunks now in use. */
56 static struct value_history_chunk
*value_history_chain
;
58 static int value_history_count
; /* Abs number of last entry stored */
60 /* List of all value objects currently allocated
61 (except for those released by calls to release_value)
62 This is so they can be freed after each command. */
64 static value_ptr all_values
;
66 /* Allocate a value that has the correct length for type TYPE. */
72 register value_ptr val
;
74 check_stub_type (type
);
76 val
= (struct value
*) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
77 VALUE_NEXT (val
) = all_values
;
79 VALUE_TYPE (val
) = type
;
80 VALUE_LVAL (val
) = not_lval
;
81 VALUE_ADDRESS (val
) = 0;
82 VALUE_FRAME (val
) = 0;
83 VALUE_OFFSET (val
) = 0;
84 VALUE_BITPOS (val
) = 0;
85 VALUE_BITSIZE (val
) = 0;
86 VALUE_REPEATED (val
) = 0;
87 VALUE_REPETITIONS (val
) = 0;
88 VALUE_REGNO (val
) = -1;
90 VALUE_OPTIMIZED_OUT (val
) = 0;
95 /* Allocate a value that has the correct length
96 for COUNT repetitions type TYPE. */
99 allocate_repeat_value (type
, count
)
103 register value_ptr val
;
106 (value_ptr
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
107 VALUE_NEXT (val
) = all_values
;
109 VALUE_TYPE (val
) = type
;
110 VALUE_LVAL (val
) = not_lval
;
111 VALUE_ADDRESS (val
) = 0;
112 VALUE_FRAME (val
) = 0;
113 VALUE_OFFSET (val
) = 0;
114 VALUE_BITPOS (val
) = 0;
115 VALUE_BITSIZE (val
) = 0;
116 VALUE_REPEATED (val
) = 1;
117 VALUE_REPETITIONS (val
) = count
;
118 VALUE_REGNO (val
) = -1;
119 VALUE_LAZY (val
) = 0;
120 VALUE_OPTIMIZED_OUT (val
) = 0;
124 /* Return a mark in the value chain. All values allocated after the
125 mark is obtained (except for those released) are subject to being freed
126 if a subsequent value_free_to_mark is passed the mark. */
133 /* Free all values allocated since MARK was obtained by value_mark
134 (except for those released). */
136 value_free_to_mark (mark
)
141 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
143 next
= VALUE_NEXT (val
);
149 /* Free all the values that have been allocated (except for those released).
150 Called after each command, successful or not. */
155 register value_ptr val
, next
;
157 for (val
= all_values
; val
; val
= next
)
159 next
= VALUE_NEXT (val
);
166 /* Remove VAL from the chain all_values
167 so it will not be freed automatically. */
171 register value_ptr val
;
173 register value_ptr v
;
175 if (all_values
== val
)
177 all_values
= val
->next
;
181 for (v
= all_values
; v
; v
= v
->next
)
191 /* Return a copy of the value ARG.
192 It contains the same contents, for same memory address,
193 but it's a different block of storage. */
199 register value_ptr val
;
200 register struct type
*type
= VALUE_TYPE (arg
);
201 if (VALUE_REPEATED (arg
))
202 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
204 val
= allocate_value (type
);
205 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
206 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
207 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
208 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
209 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
210 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
211 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
212 val
->modifiable
= arg
->modifiable
;
213 if (!VALUE_LAZY (val
))
215 memcpy (VALUE_CONTENTS_RAW (val
), VALUE_CONTENTS_RAW (arg
),
216 TYPE_LENGTH (VALUE_TYPE (arg
))
217 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
222 /* Access to the value history. */
224 /* Record a new value in the value history.
225 Returns the absolute history index of the entry.
226 Result of -1 indicates the value was not saved; otherwise it is the
227 value history index of this new item. */
230 record_latest_value (val
)
235 /* Check error now if about to store an invalid float. We return -1
236 to the caller, but allow them to continue, e.g. to print it as "Nan". */
237 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
)
239 unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
240 if (i
) return -1; /* Indicate value not saved in history */
243 /* Here we treat value_history_count as origin-zero
244 and applying to the value being stored now. */
246 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
249 register struct value_history_chunk
*new
250 = (struct value_history_chunk
*)
251 xmalloc (sizeof (struct value_history_chunk
));
252 memset (new->values
, 0, sizeof new->values
);
253 new->next
= value_history_chain
;
254 value_history_chain
= new;
257 value_history_chain
->values
[i
] = val
;
259 /* We don't want this value to have anything to do with the inferior anymore.
260 In particular, "set $1 = 50" should not affect the variable from which
261 the value was taken, and fast watchpoints should be able to assume that
262 a value on the value history never changes. */
263 if (VALUE_LAZY (val
))
264 value_fetch_lazy (val
);
265 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
266 from. This is a bit dubious, because then *&$1 does not just return $1
267 but the current contents of that location. c'est la vie... */
271 /* Now we regard value_history_count as origin-one
272 and applying to the value just stored. */
274 return ++value_history_count
;
277 /* Return a copy of the value in the history with sequence number NUM. */
280 access_value_history (num
)
283 register struct value_history_chunk
*chunk
;
285 register int absnum
= num
;
288 absnum
+= value_history_count
;
293 error ("The history is empty.");
295 error ("There is only one value in the history.");
297 error ("History does not go back to $$%d.", -num
);
299 if (absnum
> value_history_count
)
300 error ("History has not yet reached $%d.", absnum
);
304 /* Now absnum is always absolute and origin zero. */
306 chunk
= value_history_chain
;
307 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
311 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
314 /* Clear the value history entirely.
315 Must be done when new symbol tables are loaded,
316 because the type pointers become invalid. */
319 clear_value_history ()
321 register struct value_history_chunk
*next
;
323 register value_ptr val
;
325 while (value_history_chain
)
327 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
328 if ((val
= value_history_chain
->values
[i
]) != NULL
)
330 next
= value_history_chain
->next
;
331 free ((PTR
)value_history_chain
);
332 value_history_chain
= next
;
334 value_history_count
= 0;
338 show_values (num_exp
, from_tty
)
343 register value_ptr val
;
348 /* "info history +" should print from the stored position.
349 "info history <exp>" should print around value number <exp>. */
350 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
351 num
= parse_and_eval_address (num_exp
) - 5;
355 /* "info history" means print the last 10 values. */
356 num
= value_history_count
- 9;
362 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
364 val
= access_value_history (i
);
365 printf_filtered ("$%d = ", i
);
366 value_print (val
, gdb_stdout
, 0, Val_pretty_default
);
367 printf_filtered ("\n");
370 /* The next "info history +" should start after what we just printed. */
373 /* Hitting just return after this command should do the same thing as
374 "info history +". If num_exp is null, this is unnecessary, since
375 "info history +" is not useful after "info history". */
376 if (from_tty
&& num_exp
)
383 /* Internal variables. These are variables within the debugger
384 that hold values assigned by debugger commands.
385 The user refers to them with a '$' prefix
386 that does not appear in the variable names stored internally. */
388 static struct internalvar
*internalvars
;
390 /* Look up an internal variable with name NAME. NAME should not
391 normally include a dollar sign.
393 If the specified internal variable does not exist,
394 one is created, with a void value. */
397 lookup_internalvar (name
)
400 register struct internalvar
*var
;
402 for (var
= internalvars
; var
; var
= var
->next
)
403 if (STREQ (var
->name
, name
))
406 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
407 var
->name
= concat (name
, NULL
);
408 var
->value
= allocate_value (builtin_type_void
);
409 release_value (var
->value
);
410 var
->next
= internalvars
;
416 value_of_internalvar (var
)
417 struct internalvar
*var
;
419 register value_ptr val
;
421 #ifdef IS_TRAPPED_INTERNALVAR
422 if (IS_TRAPPED_INTERNALVAR (var
->name
))
423 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
426 val
= value_copy (var
->value
);
427 if (VALUE_LAZY (val
))
428 value_fetch_lazy (val
);
429 VALUE_LVAL (val
) = lval_internalvar
;
430 VALUE_INTERNALVAR (val
) = var
;
435 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
436 struct internalvar
*var
;
437 int offset
, bitpos
, bitsize
;
440 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
442 #ifdef IS_TRAPPED_INTERNALVAR
443 if (IS_TRAPPED_INTERNALVAR (var
->name
))
444 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
448 modify_field (addr
, value_as_long (newval
),
451 memcpy (addr
, VALUE_CONTENTS (newval
), TYPE_LENGTH (VALUE_TYPE (newval
)));
455 set_internalvar (var
, val
)
456 struct internalvar
*var
;
459 #ifdef IS_TRAPPED_INTERNALVAR
460 if (IS_TRAPPED_INTERNALVAR (var
->name
))
461 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
464 free ((PTR
)var
->value
);
465 var
->value
= value_copy (val
);
466 /* Force the value to be fetched from the target now, to avoid problems
467 later when this internalvar is referenced and the target is gone or
469 if (VALUE_LAZY (var
->value
))
470 value_fetch_lazy (var
->value
);
471 release_value (var
->value
);
475 internalvar_name (var
)
476 struct internalvar
*var
;
481 /* Free all internalvars. Done when new symtabs are loaded,
482 because that makes the values invalid. */
485 clear_internalvars ()
487 register struct internalvar
*var
;
492 internalvars
= var
->next
;
493 free ((PTR
)var
->name
);
494 free ((PTR
)var
->value
);
500 show_convenience (ignore
, from_tty
)
504 register struct internalvar
*var
;
507 for (var
= internalvars
; var
; var
= var
->next
)
509 #ifdef IS_TRAPPED_INTERNALVAR
510 if (IS_TRAPPED_INTERNALVAR (var
->name
))
517 printf_filtered ("$%s = ", var
->name
);
518 value_print (var
->value
, gdb_stdout
, 0, Val_pretty_default
);
519 printf_filtered ("\n");
522 printf_unfiltered ("No debugger convenience variables now defined.\n\
523 Convenience variables have names starting with \"$\";\n\
524 use \"set\" as in \"set $foo = 5\" to define them.\n");
527 /* Extract a value as a C number (either long or double).
528 Knows how to convert fixed values to double, or
529 floating values to long.
530 Does not deallocate the value. */
534 register value_ptr val
;
536 /* This coerces arrays and functions, which is necessary (e.g.
537 in disassemble_command). It also dereferences references, which
538 I suspect is the most logical thing to do. */
539 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
541 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
545 value_as_double (val
)
546 register value_ptr val
;
551 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
553 error ("Invalid floating value found in program.");
556 /* Extract a value as a C pointer.
557 Does not deallocate the value. */
559 value_as_pointer (val
)
562 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
563 whether we want this to be true eventually. */
565 /* ADDR_BITS_REMOVE is wrong if we are being called for a
566 non-address (e.g. argument to "signal", "info break", etc.), or
567 for pointers to char, in which the low bits *are* significant. */
568 return ADDR_BITS_REMOVE(value_as_long (val
));
570 return value_as_long (val
);
574 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
575 as a long, or as a double, assuming the raw data is described
576 by type TYPE. Knows how to convert different sizes of values
577 and can convert between fixed and floating point. We don't assume
578 any alignment for the raw data. Return value is in host byte order.
580 If you want functions and arrays to be coerced to pointers, and
581 references to be dereferenced, call value_as_long() instead.
583 C++: It is assumed that the front-end has taken care of
584 all matters concerning pointers to members. A pointer
585 to member which reaches here is considered to be equivalent
586 to an INT (or some size). After all, it is only an offset. */
588 /* FIXME: This should be rewritten as a switch statement for speed and
589 ease of comprehension. */
592 unpack_long (type
, valaddr
)
596 register enum type_code code
= TYPE_CODE (type
);
597 register int len
= TYPE_LENGTH (type
);
598 register int nosign
= TYPE_UNSIGNED (type
);
607 return extract_unsigned_integer (valaddr
, len
);
609 return extract_signed_integer (valaddr
, len
);
612 return extract_floating (valaddr
, len
);
616 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
617 whether we want this to be true eventually. */
618 return extract_address (valaddr
, len
);
620 case TYPE_CODE_MEMBER
:
621 error ("not implemented: member types in unpack_long");
624 error ("Value can't be converted to integer.");
626 return 0; /* Placate lint. */
629 /* Return a double value from the specified type and address.
630 INVP points to an int which is set to 0 for valid value,
631 1 for invalid value (bad float format). In either case,
632 the returned double is OK to use. Argument is in target
633 format, result is in host format. */
636 unpack_double (type
, valaddr
, invp
)
641 register enum type_code code
= TYPE_CODE (type
);
642 register int len
= TYPE_LENGTH (type
);
643 register int nosign
= TYPE_UNSIGNED (type
);
645 *invp
= 0; /* Assume valid. */
646 if (code
== TYPE_CODE_FLT
)
648 if (INVALID_FLOAT (valaddr
, len
))
651 return 1.234567891011121314;
653 return extract_floating (valaddr
, len
);
657 /* Unsigned -- be sure we compensate for signed LONGEST. */
658 return (unsigned LONGEST
) unpack_long (type
, valaddr
);
662 /* Signed -- we are OK with unpack_long. */
663 return unpack_long (type
, valaddr
);
667 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
668 as a CORE_ADDR, assuming the raw data is described by type TYPE.
669 We don't assume any alignment for the raw data. Return value is in
672 If you want functions and arrays to be coerced to pointers, and
673 references to be dereferenced, call value_as_pointer() instead.
675 C++: It is assumed that the front-end has taken care of
676 all matters concerning pointers to members. A pointer
677 to member which reaches here is considered to be equivalent
678 to an INT (or some size). After all, it is only an offset. */
681 unpack_pointer (type
, valaddr
)
685 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
686 whether we want this to be true eventually. */
687 return unpack_long (type
, valaddr
);
690 /* Given a value ARG1 (offset by OFFSET bytes)
691 of a struct or union type ARG_TYPE,
692 extract and return the value of one of its fields.
693 FIELDNO says which field.
695 For C++, must also be able to return values from static fields */
698 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
699 register value_ptr arg1
;
701 register int fieldno
;
702 register struct type
*arg_type
;
704 register value_ptr v
;
705 register struct type
*type
;
707 check_stub_type (arg_type
);
708 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
710 /* Handle packed fields */
712 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
713 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
715 v
= value_from_longest (type
,
716 unpack_field_as_long (arg_type
,
717 VALUE_CONTENTS (arg1
),
719 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
720 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
724 v
= allocate_value (type
);
725 if (VALUE_LAZY (arg1
))
728 memcpy (VALUE_CONTENTS_RAW (v
), VALUE_CONTENTS_RAW (arg1
) + offset
,
731 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
732 if (VALUE_LVAL (arg1
) == lval_internalvar
)
733 VALUE_LVAL (v
) = lval_internalvar_component
;
734 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
735 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
739 /* Given a value ARG1 of a struct or union type,
740 extract and return the value of one of its fields.
741 FIELDNO says which field.
743 For C++, must also be able to return values from static fields */
746 value_field (arg1
, fieldno
)
747 register value_ptr arg1
;
748 register int fieldno
;
750 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
753 /* Return a non-virtual function as a value.
754 F is the list of member functions which contains the desired method.
755 J is an index into F which provides the desired method. */
758 value_fn_field (arg1p
, f
, j
, type
, offset
)
765 register value_ptr v
;
766 register struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
769 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
770 0, VAR_NAMESPACE
, 0, NULL
);
774 error ("Internal error: could not find physical method named %s",
775 TYPE_FN_FIELD_PHYSNAME (f, j));
778 v
= allocate_value (ftype
);
779 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
780 VALUE_TYPE (v
) = ftype
;
784 if (type
!= VALUE_TYPE (*arg1p
))
785 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
786 value_addr (*arg1p
)));
788 /* Move the `this' pointer according to the offset.
789 VALUE_OFFSET (*arg1p) += offset;
796 /* Return a virtual function as a value.
797 ARG1 is the object which provides the virtual function
798 table pointer. *ARG1P is side-effected in calling this function.
799 F is the list of member functions which contains the desired virtual
801 J is an index into F which provides the desired virtual function.
803 TYPE is the type in which F is located. */
805 value_virtual_fn_field (arg1p
, f
, j
, type
, offset
)
812 value_ptr arg1
= *arg1p
;
813 /* First, get the virtual function table pointer. That comes
814 with a strange type, so cast it to type `pointer to long' (which
815 should serve just fine as a function type). Then, index into
816 the table, and convert final value to appropriate function type. */
817 value_ptr entry
, vfn
, vtbl
;
818 value_ptr vi
= value_from_longest (builtin_type_int
,
819 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
820 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
821 struct type
*context
;
822 if (fcontext
== NULL
)
823 /* We don't have an fcontext (e.g. the program was compiled with
824 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
825 This won't work right for multiple inheritance, but at least we
826 should do as well as GDB 3.x did. */
827 fcontext
= TYPE_VPTR_BASETYPE (type
);
828 context
= lookup_pointer_type (fcontext
);
829 /* Now context is a pointer to the basetype containing the vtbl. */
830 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
831 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
833 context
= VALUE_TYPE (arg1
);
834 /* Now context is the basetype containing the vtbl. */
836 /* This type may have been defined before its virtual function table
837 was. If so, fill in the virtual function table entry for the
839 if (TYPE_VPTR_FIELDNO (context
) < 0)
840 fill_in_vptr_fieldno (context
);
842 /* The virtual function table is now an array of structures
843 which have the form { int16 offset, delta; void *pfn; }. */
844 vtbl
= value_ind (value_primitive_field (arg1
, 0,
845 TYPE_VPTR_FIELDNO (context
),
846 TYPE_VPTR_BASETYPE (context
)));
848 /* Index into the virtual function table. This is hard-coded because
849 looking up a field is not cheap, and it may be important to save
850 time, e.g. if the user has set a conditional breakpoint calling
851 a virtual function. */
852 entry
= value_subscript (vtbl
, vi
);
854 /* Move the `this' pointer according to the virtual function table. */
855 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0))/* + offset*/;
857 if (! VALUE_LAZY (arg1
))
859 VALUE_LAZY (arg1
) = 1;
860 value_fetch_lazy (arg1
);
863 vfn
= value_field (entry
, 2);
864 /* Reinstantiate the function pointer with the correct type. */
865 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
871 /* ARG is a pointer to an object we know to be at least
872 a DTYPE. BTYPE is the most derived basetype that has
873 already been searched (and need not be searched again).
874 After looking at the vtables between BTYPE and DTYPE,
875 return the most derived type we find. The caller must
876 be satisfied when the return value == DTYPE.
878 FIXME-tiemann: should work with dossier entries as well. */
881 value_headof (in_arg
, btype
, dtype
)
883 struct type
*btype
, *dtype
;
885 /* First collect the vtables we must look at for this object. */
886 /* FIXME-tiemann: right now, just look at top-most vtable. */
887 value_ptr arg
, vtbl
, entry
, best_entry
= 0;
889 int offset
, best_offset
= 0;
891 CORE_ADDR pc_for_sym
;
892 char *demangled_name
;
893 struct minimal_symbol
*msymbol
;
895 btype
= TYPE_VPTR_BASETYPE (dtype
);
896 check_stub_type (btype
);
899 arg
= value_cast (lookup_pointer_type (btype
), arg
);
900 vtbl
= value_ind (value_field (value_ind (arg
), TYPE_VPTR_FIELDNO (btype
)));
902 /* Check that VTBL looks like it points to a virtual function table. */
903 msymbol
= lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl
));
905 || !VTBL_PREFIX_P (demangled_name
= SYMBOL_NAME (msymbol
)))
907 /* If we expected to find a vtable, but did not, let the user
908 know that we aren't happy, but don't throw an error.
909 FIXME: there has to be a better way to do this. */
910 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
911 memcpy (error_type
, VALUE_TYPE (in_arg
), sizeof (struct type
));
912 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
913 VALUE_TYPE (in_arg
) = error_type
;
917 /* Now search through the virtual function table. */
918 entry
= value_ind (vtbl
);
919 nelems
= longest_to_int (value_as_long (value_field (entry
, 2)));
920 for (i
= 1; i
<= nelems
; i
++)
922 entry
= value_subscript (vtbl
, value_from_longest (builtin_type_int
,
924 offset
= longest_to_int (value_as_long (value_field (entry
, 0)));
925 /* If we use '<=' we can handle single inheritance
926 * where all offsets are zero - just use the first entry found. */
927 if (offset
<= best_offset
)
929 best_offset
= offset
;
933 /* Move the pointer according to BEST_ENTRY's offset, and figure
934 out what type we should return as the new pointer. */
937 /* An alternative method (which should no longer be necessary).
938 * But we leave it in for future use, when we will hopefully
939 * have optimizes the vtable to use thunks instead of offsets. */
940 /* Use the name of vtable itself to extract a base type. */
941 demangled_name
+= 4; /* Skip _vt$ prefix. */
945 pc_for_sym
= value_as_pointer (value_field (best_entry
, 2));
946 sym
= find_pc_function (pc_for_sym
);
947 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), DMGL_ANSI
);
948 *(strchr (demangled_name
, ':')) = '\0';
950 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
952 error ("could not find type declaration for `%s'", demangled_name
);
955 free (demangled_name
);
956 arg
= value_add (value_cast (builtin_type_int
, arg
),
957 value_field (best_entry
, 0));
960 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
964 /* ARG is a pointer object of type TYPE. If TYPE has virtual
965 function tables, probe ARG's tables (including the vtables
966 of its baseclasses) to figure out the most derived type that ARG
967 could actually be a pointer to. */
970 value_from_vtable_info (arg
, type
)
974 /* Take care of preliminaries. */
975 if (TYPE_VPTR_FIELDNO (type
) < 0)
976 fill_in_vptr_fieldno (type
);
977 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
980 return value_headof (arg
, 0, type
);
983 /* Return true if the INDEXth field of TYPE is a virtual baseclass
984 pointer which is for the base class whose type is BASECLASS. */
987 vb_match (type
, index
, basetype
)
990 struct type
*basetype
;
992 struct type
*fieldtype
;
993 char *name
= TYPE_FIELD_NAME (type
, index
);
994 char *field_class_name
= NULL
;
998 /* gcc 2.4 uses _vb$. */
999 if (name
[1] == 'v' && name
[2] == 'b' && name
[3] == CPLUS_MARKER
)
1000 field_class_name
= name
+ 4;
1001 /* gcc 2.5 will use __vb_. */
1002 if (name
[1] == '_' && name
[2] == 'v' && name
[3] == 'b' && name
[4] == '_')
1003 field_class_name
= name
+ 5;
1005 if (field_class_name
== NULL
)
1006 /* This field is not a virtual base class pointer. */
1009 /* It's a virtual baseclass pointer, now we just need to find out whether
1010 it is for this baseclass. */
1011 fieldtype
= TYPE_FIELD_TYPE (type
, index
);
1012 if (fieldtype
== NULL
1013 || TYPE_CODE (fieldtype
) != TYPE_CODE_PTR
)
1014 /* "Can't happen". */
1017 /* What we check for is that either the types are equal (needed for
1018 nameless types) or have the same name. This is ugly, and a more
1019 elegant solution should be devised (which would probably just push
1020 the ugliness into symbol reading unless we change the stabs format). */
1021 if (TYPE_TARGET_TYPE (fieldtype
) == basetype
)
1024 if (TYPE_NAME (basetype
) != NULL
1025 && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype
)) != NULL
1026 && STREQ (TYPE_NAME (basetype
),
1027 TYPE_NAME (TYPE_TARGET_TYPE (fieldtype
))))
1032 /* Compute the offset of the baseclass which is
1033 the INDEXth baseclass of class TYPE, for a value ARG,
1034 wih extra offset of OFFSET.
1035 The result is the offste of the baseclass value relative
1036 to (the address of)(ARG) + OFFSET.
1038 -1 is returned on error. */
1041 baseclass_offset (type
, index
, arg
, offset
)
1047 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1049 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1051 /* Must hunt for the pointer to this virtual baseclass. */
1052 register int i
, len
= TYPE_NFIELDS (type
);
1053 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1055 /* First look for the virtual baseclass pointer
1057 for (i
= n_baseclasses
; i
< len
; i
++)
1059 if (vb_match (type
, i
, basetype
))
1062 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1063 VALUE_CONTENTS (arg
) + VALUE_OFFSET (arg
)
1065 + (TYPE_FIELD_BITPOS (type
, i
) / 8));
1067 if (VALUE_LVAL (arg
) != lval_memory
)
1071 (LONGEST
) (VALUE_ADDRESS (arg
) + VALUE_OFFSET (arg
) + offset
);
1074 /* Not in the fields, so try looking through the baseclasses. */
1075 for (i
= index
+1; i
< n_baseclasses
; i
++)
1078 baseclass_offset (type
, i
, arg
, offset
);
1086 /* Baseclass is easily computed. */
1087 return TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1090 /* Compute the address of the baseclass which is
1091 the INDEXth baseclass of class TYPE. The TYPE base
1092 of the object is at VALADDR.
1094 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1095 or 0 if no error. In that case the return value is not the address
1096 of the baseclasss, but the address which could not be read
1099 /* FIXME Fix remaining uses of baseclass_addr to use baseclass_offset */
1102 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1109 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1114 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1116 /* Must hunt for the pointer to this virtual baseclass. */
1117 register int i
, len
= TYPE_NFIELDS (type
);
1118 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1120 /* First look for the virtual baseclass pointer
1122 for (i
= n_baseclasses
; i
< len
; i
++)
1124 if (vb_match (type
, i
, basetype
))
1126 value_ptr val
= allocate_value (basetype
);
1131 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1132 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1134 status
= target_read_memory (addr
,
1135 VALUE_CONTENTS_RAW (val
),
1136 TYPE_LENGTH (basetype
));
1137 VALUE_LVAL (val
) = lval_memory
;
1138 VALUE_ADDRESS (val
) = addr
;
1144 release_value (val
);
1148 return (char *)addr
;
1154 return (char *) VALUE_CONTENTS (val
);
1158 /* Not in the fields, so try looking through the baseclasses. */
1159 for (i
= index
+1; i
< n_baseclasses
; i
++)
1163 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
, errp
);
1173 /* Baseclass is easily computed. */
1176 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1179 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1182 Extracting bits depends on endianness of the machine. Compute the
1183 number of least significant bits to discard. For big endian machines,
1184 we compute the total number of bits in the anonymous object, subtract
1185 off the bit count from the MSB of the object to the MSB of the
1186 bitfield, then the size of the bitfield, which leaves the LSB discard
1187 count. For little endian machines, the discard count is simply the
1188 number of bits from the LSB of the anonymous object to the LSB of the
1191 If the field is signed, we also do sign extension. */
1194 unpack_field_as_long (type
, valaddr
, fieldno
)
1199 unsigned LONGEST val
;
1200 unsigned LONGEST valmask
;
1201 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1202 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1205 val
= extract_unsigned_integer (valaddr
+ bitpos
/ 8, sizeof (val
));
1207 /* Extract bits. See comment above. */
1210 lsbcount
= (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1212 lsbcount
= (bitpos
% 8);
1216 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1217 If the field is signed, and is negative, then sign extend. */
1219 if ((bitsize
> 0) && (bitsize
< 8 * sizeof (val
)))
1221 valmask
= (((unsigned LONGEST
) 1) << bitsize
) - 1;
1223 if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type
, fieldno
)))
1225 if (val
& (valmask
^ (valmask
>> 1)))
1234 /* Modify the value of a bitfield. ADDR points to a block of memory in
1235 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1236 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1237 indicate which bits (in target bit order) comprise the bitfield. */
1240 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1243 int bitpos
, bitsize
;
1247 /* Reject values too big to fit in the field in question,
1248 otherwise adjoining fields may be corrupted. */
1249 if (bitsize
< (8 * sizeof (fieldval
))
1250 && 0 != (fieldval
& ~((1<<bitsize
)-1)))
1252 /* FIXME: would like to include fieldval in the message, but
1253 we don't have a sprintf_longest. */
1254 error ("Value does not fit in %d bits.", bitsize
);
1257 oword
= extract_signed_integer (addr
, sizeof oword
);
1259 /* Shifting for bit field depends on endianness of the target machine. */
1261 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1264 /* Mask out old value, while avoiding shifts >= size of oword */
1265 if (bitsize
< 8 * sizeof (oword
))
1266 oword
&= ~(((((unsigned LONGEST
)1) << bitsize
) - 1) << bitpos
);
1268 oword
&= ~((~(unsigned LONGEST
)0) << bitpos
);
1269 oword
|= fieldval
<< bitpos
;
1271 store_signed_integer (addr
, sizeof oword
, oword
);
1274 /* Convert C numbers into newly allocated values */
1277 value_from_longest (type
, num
)
1279 register LONGEST num
;
1281 register value_ptr val
= allocate_value (type
);
1282 register enum type_code code
= TYPE_CODE (type
);
1283 register int len
= TYPE_LENGTH (type
);
1288 case TYPE_CODE_CHAR
:
1289 case TYPE_CODE_ENUM
:
1290 case TYPE_CODE_BOOL
:
1291 store_signed_integer (VALUE_CONTENTS_RAW (val
), len
, num
);
1296 /* This assumes that all pointers of a given length
1297 have the same form. */
1298 store_address (VALUE_CONTENTS_RAW (val
), len
, (CORE_ADDR
) num
);
1302 error ("Unexpected type encountered for integer constant.");
1308 value_from_double (type
, num
)
1312 register value_ptr val
= allocate_value (type
);
1313 register enum type_code code
= TYPE_CODE (type
);
1314 register int len
= TYPE_LENGTH (type
);
1316 if (code
== TYPE_CODE_FLT
)
1318 store_floating (VALUE_CONTENTS_RAW (val
), len
, num
);
1321 error ("Unexpected type encountered for floating constant.");
1326 /* Deal with the value that is "about to be returned". */
1328 /* Return the value that a function returning now
1329 would be returning to its caller, assuming its type is VALTYPE.
1330 RETBUF is where we look for what ought to be the contents
1331 of the registers (in raw form). This is because it is often
1332 desirable to restore old values to those registers
1333 after saving the contents of interest, and then call
1334 this function using the saved values.
1335 struct_return is non-zero when the function in question is
1336 using the structure return conventions on the machine in question;
1337 0 when it is using the value returning conventions (this often
1338 means returning pointer to where structure is vs. returning value). */
1341 value_being_returned (valtype
, retbuf
, struct_return
)
1342 register struct type
*valtype
;
1343 char retbuf
[REGISTER_BYTES
];
1347 register value_ptr val
;
1350 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1351 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1352 if (struct_return
) {
1353 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1355 error ("Function return value unknown");
1356 return value_at (valtype
, addr
);
1360 val
= allocate_value (valtype
);
1361 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1366 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1367 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1368 and TYPE is the type (which is known to be struct, union or array).
1370 On most machines, the struct convention is used unless we are
1371 using gcc and the type is of a special size. */
1372 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1373 native compiler. GCC 2.3.3 was the last release that did it the
1374 old way. Since gcc2_compiled was not changed, we have no
1375 way to correctly win in all cases, so we just do the right thing
1376 for gcc1 and for gcc2 after this change. Thus it loses for gcc
1377 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled
1378 would cause more chaos than dealing with some struct returns being
1380 #if !defined (USE_STRUCT_CONVENTION)
1381 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1382 (!((gcc_p == 1) && (TYPE_LENGTH (value_type) == 1 \
1383 || TYPE_LENGTH (value_type) == 2 \
1384 || TYPE_LENGTH (value_type) == 4 \
1385 || TYPE_LENGTH (value_type) == 8 \
1390 /* Return true if the function specified is using the structure returning
1391 convention on this machine to return arguments, or 0 if it is using
1392 the value returning convention. FUNCTION is the value representing
1393 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1394 is the type returned by the function. GCC_P is nonzero if compiled
1398 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1401 struct type
*value_type
;
1405 register enum type_code code
= TYPE_CODE (value_type
);
1407 if (code
== TYPE_CODE_ERROR
)
1408 error ("Function return type unknown.");
1410 if (code
== TYPE_CODE_STRUCT
||
1411 code
== TYPE_CODE_UNION
||
1412 code
== TYPE_CODE_ARRAY
)
1413 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1418 /* Store VAL so it will be returned if a function returns now.
1419 Does not verify that VAL's type matches what the current
1420 function wants to return. */
1423 set_return_value (val
)
1426 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1430 if (code
== TYPE_CODE_ERROR
)
1431 error ("Function return type unknown.");
1433 if ( code
== TYPE_CODE_STRUCT
1434 || code
== TYPE_CODE_UNION
) /* FIXME, implement struct return. */
1435 error ("GDB does not support specifying a struct or union return value.");
1437 /* FIXME, this is bogus. We don't know what the return conventions
1438 are, or how values should be promoted.... */
1439 if (code
== TYPE_CODE_FLT
)
1441 dbuf
= value_as_double (val
);
1443 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1447 lbuf
= value_as_long (val
);
1448 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1453 _initialize_values ()
1455 add_cmd ("convenience", no_class
, show_convenience
,
1456 "Debugger convenience (\"$foo\") variables.\n\
1457 These variables are created when you assign them values;\n\
1458 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1459 A few convenience variables are given values automatically:\n\
1460 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1461 \"$__\" holds the contents of the last address examined with \"x\".",
1464 add_cmd ("values", no_class
, show_values
,
1465 "Elements of value history around item number IDX (or last ten).",