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. */
30 /* Local function prototypes. */
33 value_headof
PARAMS ((value
, struct type
*, struct type
*));
36 show_values
PARAMS ((char *, int));
39 show_convenience
PARAMS ((void));
41 /* The value-history records all the values printed
42 by print commands during this session. Each chunk
43 records 60 consecutive values. The first chunk on
44 the chain records the most recent values.
45 The total number of values is in value_history_count. */
47 #define VALUE_HISTORY_CHUNK 60
49 struct value_history_chunk
51 struct value_history_chunk
*next
;
52 value values
[VALUE_HISTORY_CHUNK
];
55 /* Chain of chunks now in use. */
57 static struct value_history_chunk
*value_history_chain
;
59 static int value_history_count
; /* Abs number of last entry stored */
61 /* List of all value objects currently allocated
62 (except for those released by calls to release_value)
63 This is so they can be freed after each command. */
65 static value all_values
;
67 /* Allocate a value that has the correct length for type TYPE. */
75 check_stub_type (type
);
77 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
78 VALUE_NEXT (val
) = all_values
;
80 VALUE_TYPE (val
) = type
;
81 VALUE_LVAL (val
) = not_lval
;
82 VALUE_ADDRESS (val
) = 0;
83 VALUE_FRAME (val
) = 0;
84 VALUE_OFFSET (val
) = 0;
85 VALUE_BITPOS (val
) = 0;
86 VALUE_BITSIZE (val
) = 0;
87 VALUE_REPEATED (val
) = 0;
88 VALUE_REPETITIONS (val
) = 0;
89 VALUE_REGNO (val
) = -1;
91 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
)
105 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
106 VALUE_NEXT (val
) = all_values
;
108 VALUE_TYPE (val
) = type
;
109 VALUE_LVAL (val
) = not_lval
;
110 VALUE_ADDRESS (val
) = 0;
111 VALUE_FRAME (val
) = 0;
112 VALUE_OFFSET (val
) = 0;
113 VALUE_BITPOS (val
) = 0;
114 VALUE_BITSIZE (val
) = 0;
115 VALUE_REPEATED (val
) = 1;
116 VALUE_REPETITIONS (val
) = count
;
117 VALUE_REGNO (val
) = -1;
118 VALUE_LAZY (val
) = 0;
119 VALUE_OPTIMIZED_OUT (val
) = 0;
123 /* Return a mark in the value chain. All values allocated after the
124 mark is obtained (except for those released) are subject to being freed
125 if a subsequent value_free_to_mark is passed the mark. */
132 /* Free all values allocated since MARK was obtained by value_mark
133 (except for those released). */
135 value_free_to_mark (mark
)
140 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
142 next
= VALUE_NEXT (val
);
148 /* Free all the values that have been allocated (except for those released).
149 Called after each command, successful or not. */
154 register value val
, next
;
156 for (val
= all_values
; val
; val
= next
)
158 next
= VALUE_NEXT (val
);
165 /* Remove VAL from the chain all_values
166 so it will not be freed automatically. */
174 if (all_values
== val
)
176 all_values
= val
->next
;
180 for (v
= all_values
; v
; v
= v
->next
)
190 /* Return a copy of the value ARG.
191 It contains the same contents, for same memory address,
192 but it's a different block of storage. */
199 register struct type
*type
= VALUE_TYPE (arg
);
200 if (VALUE_REPEATED (arg
))
201 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
203 val
= allocate_value (type
);
204 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
205 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
206 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
207 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
208 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
209 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
210 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
211 if (!VALUE_LAZY (val
))
213 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
214 TYPE_LENGTH (VALUE_TYPE (arg
))
215 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
220 /* Access to the value history. */
222 /* Record a new value in the value history.
223 Returns the absolute history index of the entry.
224 Result of -1 indicates the value was not saved; otherwise it is the
225 value history index of this new item. */
228 record_latest_value (val
)
233 /* Check error now if about to store an invalid float. We return -1
234 to the caller, but allow them to continue, e.g. to print it as "Nan". */
235 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
236 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
237 if (i
) return -1; /* Indicate value not saved in history */
240 /* Here we treat value_history_count as origin-zero
241 and applying to the value being stored now. */
243 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
246 register struct value_history_chunk
*new
247 = (struct value_history_chunk
*)
248 xmalloc (sizeof (struct value_history_chunk
));
249 bzero (new->values
, sizeof new->values
);
250 new->next
= value_history_chain
;
251 value_history_chain
= new;
254 value_history_chain
->values
[i
] = val
;
257 /* Now we regard value_history_count as origin-one
258 and applying to the value just stored. */
260 return ++value_history_count
;
263 /* Return a copy of the value in the history with sequence number NUM. */
266 access_value_history (num
)
269 register struct value_history_chunk
*chunk
;
271 register int absnum
= num
;
274 absnum
+= value_history_count
;
279 error ("The history is empty.");
281 error ("There is only one value in the history.");
283 error ("History does not go back to $$%d.", -num
);
285 if (absnum
> value_history_count
)
286 error ("History has not yet reached $%d.", absnum
);
290 /* Now absnum is always absolute and origin zero. */
292 chunk
= value_history_chain
;
293 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
297 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
300 /* Clear the value history entirely.
301 Must be done when new symbol tables are loaded,
302 because the type pointers become invalid. */
305 clear_value_history ()
307 register struct value_history_chunk
*next
;
311 while (value_history_chain
)
313 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
314 if (val
= value_history_chain
->values
[i
])
316 next
= value_history_chain
->next
;
317 free ((PTR
)value_history_chain
);
318 value_history_chain
= next
;
320 value_history_count
= 0;
324 show_values (num_exp
, from_tty
)
334 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
335 /* "info history +" should print from the stored position. */
338 /* "info history <exp>" should print around value number <exp>. */
339 num
= parse_and_eval_address (num_exp
) - 5;
343 /* "info history" means print the last 10 values. */
344 num
= value_history_count
- 9;
350 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
352 val
= access_value_history (i
);
353 printf_filtered ("$%d = ", i
);
354 value_print (val
, stdout
, 0, Val_pretty_default
);
355 printf_filtered ("\n");
358 /* The next "info history +" should start after what we just printed. */
361 /* Hitting just return after this command should do the same thing as
362 "info history +". If num_exp is null, this is unnecessary, since
363 "info history +" is not useful after "info history". */
364 if (from_tty
&& num_exp
)
371 /* Internal variables. These are variables within the debugger
372 that hold values assigned by debugger commands.
373 The user refers to them with a '$' prefix
374 that does not appear in the variable names stored internally. */
376 static struct internalvar
*internalvars
;
378 /* Look up an internal variable with name NAME. NAME should not
379 normally include a dollar sign.
381 If the specified internal variable does not exist,
382 one is created, with a void value. */
385 lookup_internalvar (name
)
388 register struct internalvar
*var
;
390 for (var
= internalvars
; var
; var
= var
->next
)
391 if (!strcmp (var
->name
, name
))
394 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
395 var
->name
= concat (name
, NULL
);
396 var
->value
= allocate_value (builtin_type_void
);
397 release_value (var
->value
);
398 var
->next
= internalvars
;
404 value_of_internalvar (var
)
405 struct internalvar
*var
;
409 #ifdef IS_TRAPPED_INTERNALVAR
410 if (IS_TRAPPED_INTERNALVAR (var
->name
))
411 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
414 val
= value_copy (var
->value
);
415 if (VALUE_LAZY (val
))
416 value_fetch_lazy (val
);
417 VALUE_LVAL (val
) = lval_internalvar
;
418 VALUE_INTERNALVAR (val
) = var
;
423 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
424 struct internalvar
*var
;
425 int offset
, bitpos
, bitsize
;
428 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
430 #ifdef IS_TRAPPED_INTERNALVAR
431 if (IS_TRAPPED_INTERNALVAR (var
->name
))
432 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
436 modify_field (addr
, (int) value_as_long (newval
),
439 bcopy (VALUE_CONTENTS (newval
), addr
,
440 TYPE_LENGTH (VALUE_TYPE (newval
)));
444 set_internalvar (var
, val
)
445 struct internalvar
*var
;
448 #ifdef IS_TRAPPED_INTERNALVAR
449 if (IS_TRAPPED_INTERNALVAR (var
->name
))
450 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
453 free ((PTR
)var
->value
);
454 var
->value
= value_copy (val
);
455 release_value (var
->value
);
459 internalvar_name (var
)
460 struct internalvar
*var
;
465 /* Free all internalvars. Done when new symtabs are loaded,
466 because that makes the values invalid. */
469 clear_internalvars ()
471 register struct internalvar
*var
;
476 internalvars
= var
->next
;
477 free ((PTR
)var
->name
);
478 free ((PTR
)var
->value
);
486 register struct internalvar
*var
;
489 for (var
= internalvars
; var
; var
= var
->next
)
491 #ifdef IS_TRAPPED_INTERNALVAR
492 if (IS_TRAPPED_INTERNALVAR (var
->name
))
499 printf ("Debugger convenience variables:\n\n");
503 printf_filtered ("$%s = ", var
->name
);
504 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
505 printf_filtered ("\n");
508 printf ("No debugger convenience variables now defined.\n\
509 Convenience variables have names starting with \"$\";\n\
510 use \"set\" as in \"set $foo = 5\" to define them.\n");
513 /* Extract a value as a C number (either long or double).
514 Knows how to convert fixed values to double, or
515 floating values to long.
516 Does not deallocate the value. */
522 /* This coerces arrays and functions, which is necessary (e.g.
523 in disassemble_command). It also dereferences references, which
524 I suspect is the most logical thing to do. */
525 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
527 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
531 value_as_double (val
)
537 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
539 error ("Invalid floating value found in program.");
542 /* Extract a value as a C pointer.
543 Does not deallocate the value. */
545 value_as_pointer (val
)
548 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
549 whether we want this to be true eventually. */
550 return value_as_long (val
);
553 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
554 as a long, or as a double, assuming the raw data is described
555 by type TYPE. Knows how to convert different sizes of values
556 and can convert between fixed and floating point. We don't assume
557 any alignment for the raw data. Return value is in host byte order.
559 If you want functions and arrays to be coerced to pointers, and
560 references to be dereferenced, call value_as_long() instead.
562 C++: It is assumed that the front-end has taken care of
563 all matters concerning pointers to members. A pointer
564 to member which reaches here is considered to be equivalent
565 to an INT (or some size). After all, it is only an offset. */
567 /* FIXME: This should be rewritten as a switch statement for speed and
568 ease of comprehension. */
571 unpack_long (type
, valaddr
)
575 register enum type_code code
= TYPE_CODE (type
);
576 register int len
= TYPE_LENGTH (type
);
577 register int nosign
= TYPE_UNSIGNED (type
);
579 if (code
== TYPE_CODE_ENUM
|| code
== TYPE_CODE_BOOL
)
580 code
= TYPE_CODE_INT
;
581 if (code
== TYPE_CODE_FLT
)
583 if (len
== sizeof (float))
586 bcopy (valaddr
, &retval
, sizeof (retval
));
587 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
591 if (len
== sizeof (double))
594 bcopy (valaddr
, &retval
, sizeof (retval
));
595 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
600 error ("Unexpected type of floating point number.");
603 else if (code
== TYPE_CODE_INT
&& nosign
)
605 if (len
== sizeof (char))
607 unsigned char retval
= * (unsigned char *) valaddr
;
608 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
612 if (len
== sizeof (short))
614 unsigned short retval
;
615 bcopy (valaddr
, &retval
, sizeof (retval
));
616 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
620 if (len
== sizeof (int))
623 bcopy (valaddr
, &retval
, sizeof (retval
));
624 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
628 if (len
== sizeof (long))
630 unsigned long retval
;
631 bcopy (valaddr
, &retval
, sizeof (retval
));
632 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
636 if (len
== sizeof (long long))
638 unsigned long long retval
;
639 bcopy (valaddr
, &retval
, sizeof (retval
));
640 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
646 error ("That operation is not possible on an integer of that size.");
649 else if (code
== TYPE_CODE_INT
)
651 if (len
== sizeof (char))
654 bcopy (valaddr
, &retval
, sizeof (retval
));
655 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
659 if (len
== sizeof (short))
662 bcopy (valaddr
, &retval
, sizeof (retval
));
663 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
667 if (len
== sizeof (int))
670 bcopy (valaddr
, &retval
, sizeof (retval
));
671 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
675 if (len
== sizeof (long))
678 bcopy (valaddr
, &retval
, sizeof (retval
));
679 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
684 if (len
== sizeof (long long))
687 bcopy (valaddr
, &retval
, sizeof (retval
));
688 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
694 error ("That operation is not possible on an integer of that size.");
697 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
698 whether we want this to be true eventually. */
699 else if (code
== TYPE_CODE_PTR
700 || code
== TYPE_CODE_REF
)
702 if (len
== sizeof(long))
705 bcopy (valaddr
, &retval
, sizeof(retval
));
706 SWAP_TARGET_AND_HOST (&retval
, sizeof(retval
));
709 else if (len
== sizeof(short))
712 bcopy (valaddr
, &retval
, len
);
713 SWAP_TARGET_AND_HOST (&retval
, len
);
717 else if (code
== TYPE_CODE_MEMBER
)
718 error ("not implemented: member types in unpack_long");
719 else if (code
== TYPE_CODE_CHAR
)
720 return *(unsigned char *)valaddr
;
722 error ("Value not integer or pointer.");
723 return 0; /* For lint -- never reached */
726 /* Return a double value from the specified type and address.
727 INVP points to an int which is set to 0 for valid value,
728 1 for invalid value (bad float format). In either case,
729 the returned double is OK to use. Argument is in target
730 format, result is in host format. */
733 unpack_double (type
, valaddr
, invp
)
738 register enum type_code code
= TYPE_CODE (type
);
739 register int len
= TYPE_LENGTH (type
);
740 register int nosign
= TYPE_UNSIGNED (type
);
742 *invp
= 0; /* Assume valid. */
743 if (code
== TYPE_CODE_FLT
)
745 if (INVALID_FLOAT (valaddr
, len
))
748 return 1.234567891011121314;
751 if (len
== sizeof (float))
754 bcopy (valaddr
, &retval
, sizeof (retval
));
755 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
759 if (len
== sizeof (double))
762 bcopy (valaddr
, &retval
, sizeof (retval
));
763 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
768 error ("Unexpected type of floating point number.");
769 return 0; /* Placate lint. */
773 /* Unsigned -- be sure we compensate for signed LONGEST. */
775 return (unsigned long long) unpack_long (type
, valaddr
);
777 return (unsigned long ) unpack_long (type
, valaddr
);
780 /* Signed -- we are OK with unpack_long. */
781 return unpack_long (type
, valaddr
);
785 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
786 as a CORE_ADDR, assuming the raw data is described by type TYPE.
787 We don't assume any alignment for the raw data. Return value is in
790 If you want functions and arrays to be coerced to pointers, and
791 references to be dereferenced, call value_as_pointer() instead.
793 C++: It is assumed that the front-end has taken care of
794 all matters concerning pointers to members. A pointer
795 to member which reaches here is considered to be equivalent
796 to an INT (or some size). After all, it is only an offset. */
799 unpack_pointer (type
, valaddr
)
804 /* The user should be able to use an int (e.g. 0x7892) in contexts
805 where a pointer is expected. So this doesn't do enough. */
806 register enum type_code code
= TYPE_CODE (type
);
807 register int len
= TYPE_LENGTH (type
);
809 if (code
== TYPE_CODE_PTR
810 || code
== TYPE_CODE_REF
)
812 if (len
== sizeof (CORE_ADDR
))
815 bcopy (valaddr
, &retval
, sizeof (retval
));
816 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
819 error ("Unrecognized pointer size.");
821 else if (code
== TYPE_CODE_MEMBER
)
822 error ("not implemented: member types in unpack_pointer");
824 error ("Value is not a pointer.");
825 return 0; /* For lint -- never reached */
827 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
828 whether we want this to be true eventually. */
829 return unpack_long (type
, valaddr
);
833 /* Given a value ARG1 (offset by OFFSET bytes)
834 of a struct or union type ARG_TYPE,
835 extract and return the value of one of its fields.
836 FIELDNO says which field.
838 For C++, must also be able to return values from static fields */
841 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
844 register int fieldno
;
845 register struct type
*arg_type
;
848 register struct type
*type
;
850 check_stub_type (arg_type
);
851 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
853 /* Handle packed fields */
855 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
856 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
858 v
= value_from_longest (type
,
859 unpack_field_as_long (arg_type
,
860 VALUE_CONTENTS (arg1
),
862 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
863 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
867 v
= allocate_value (type
);
868 if (VALUE_LAZY (arg1
))
871 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
872 VALUE_CONTENTS_RAW (v
),
875 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
876 if (VALUE_LVAL (arg1
) == lval_internalvar
)
877 VALUE_LVAL (v
) = lval_internalvar_component
;
878 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
879 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
883 /* Given a value ARG1 of a struct or union type,
884 extract and return the value of one of its fields.
885 FIELDNO says which field.
887 For C++, must also be able to return values from static fields */
890 value_field (arg1
, fieldno
)
892 register int fieldno
;
894 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
897 /* Return a non-virtual function as a value.
898 F is the list of member functions which contains the desired method.
899 J is an index into F which provides the desired method. */
902 value_fn_field (f
, j
)
907 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, j
);
910 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
911 0, VAR_NAMESPACE
, 0, NULL
);
912 if (! sym
) error ("Internal error: could not find physical method named %s",
913 TYPE_FN_FIELD_PHYSNAME (f
, j
));
915 v
= allocate_value (type
);
916 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
917 VALUE_TYPE (v
) = type
;
921 /* Return a virtual function as a value.
922 ARG1 is the object which provides the virtual function
923 table pointer. ARG1 is side-effected in calling this function.
924 F is the list of member functions which contains the desired virtual
926 J is an index into F which provides the desired virtual function.
928 TYPE is the type in which F is located. */
930 value_virtual_fn_field (arg1
, f
, j
, type
)
936 /* First, get the virtual function table pointer. That comes
937 with a strange type, so cast it to type `pointer to long' (which
938 should serve just fine as a function type). Then, index into
939 the table, and convert final value to appropriate function type. */
940 value entry
, vfn
, vtbl
;
941 value vi
= value_from_longest (builtin_type_int
,
942 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
943 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
944 struct type
*context
;
945 if (fcontext
== NULL
)
946 /* We don't have an fcontext (e.g. the program was compiled with
947 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
948 This won't work right for multiple inheritance, but at least we
949 should do as well as GDB 3.x did. */
950 fcontext
= TYPE_VPTR_BASETYPE (type
);
951 context
= lookup_pointer_type (fcontext
);
952 /* Now context is a pointer to the basetype containing the vtbl. */
953 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
954 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
956 context
= VALUE_TYPE (arg1
);
957 /* Now context is the basetype containing the vtbl. */
959 /* This type may have been defined before its virtual function table
960 was. If so, fill in the virtual function table entry for the
962 if (TYPE_VPTR_FIELDNO (context
) < 0)
963 fill_in_vptr_fieldno (context
);
965 /* The virtual function table is now an array of structures
966 which have the form { int16 offset, delta; void *pfn; }. */
967 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
969 /* Index into the virtual function table. This is hard-coded because
970 looking up a field is not cheap, and it may be important to save
971 time, e.g. if the user has set a conditional breakpoint calling
972 a virtual function. */
973 entry
= value_subscript (vtbl
, vi
);
975 /* Move the `this' pointer according to the virtual function table. */
976 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
977 if (! VALUE_LAZY (arg1
))
979 VALUE_LAZY (arg1
) = 1;
980 value_fetch_lazy (arg1
);
983 vfn
= value_field (entry
, 2);
984 /* Reinstantiate the function pointer with the correct type. */
985 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
990 /* ARG is a pointer to an object we know to be at least
991 a DTYPE. BTYPE is the most derived basetype that has
992 already been searched (and need not be searched again).
993 After looking at the vtables between BTYPE and DTYPE,
994 return the most derived type we find. The caller must
995 be satisfied when the return value == DTYPE.
997 FIXME-tiemann: should work with dossier entries as well. */
1000 value_headof (arg
, btype
, dtype
)
1002 struct type
*btype
, *dtype
;
1004 /* First collect the vtables we must look at for this object. */
1005 /* FIXME-tiemann: right now, just look at top-most vtable. */
1006 value vtbl
, entry
, best_entry
= 0;
1007 /* FIXME: entry_type is never used. */
1008 struct type
*entry_type
;
1010 int offset
, best_offset
= 0;
1012 CORE_ADDR pc_for_sym
;
1013 char *demangled_name
;
1014 struct minimal_symbol
*msymbol
;
1016 btype
= TYPE_VPTR_BASETYPE (dtype
);
1017 check_stub_type (btype
);
1019 vtbl
= value_cast (lookup_pointer_type (btype
), arg
);
1022 vtbl
= value_ind (value_field (value_ind (vtbl
), TYPE_VPTR_FIELDNO (btype
)));
1024 /* Check that VTBL looks like it points to a virtual function table. */
1025 msymbol
= lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl
));
1027 || !VTBL_PREFIX_P (demangled_name
= msymbol
-> name
))
1029 /* If we expected to find a vtable, but did not, let the user
1030 know that we aren't happy, but don't throw an error.
1031 FIXME: there has to be a better way to do this. */
1032 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
1033 bcopy (VALUE_TYPE (arg
), error_type
, sizeof (struct type
));
1034 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
1035 VALUE_TYPE (arg
) = error_type
;
1039 /* Now search through the virtual function table. */
1040 entry
= value_ind (vtbl
);
1041 nelems
= longest_to_int (value_as_long (value_field (entry
, 2)));
1042 for (i
= 1; i
<= nelems
; i
++)
1044 entry
= value_subscript (vtbl
, value_from_longest (builtin_type_int
,
1046 offset
= longest_to_int (value_as_long (value_field (entry
, 0)));
1047 /* If we use '<=' we can handle single inheritance
1048 * where all offsets are zero - just use the first entry found. */
1049 if (offset
<= best_offset
)
1051 best_offset
= offset
;
1055 /* Move the pointer according to BEST_ENTRY's offset, and figure
1056 out what type we should return as the new pointer. */
1057 if (best_entry
== 0)
1059 /* An alternative method (which should no longer be necessary).
1060 * But we leave it in for future use, when we will hopefully
1061 * have optimizes the vtable to use thunks instead of offsets. */
1062 /* Use the name of vtable itself to extract a base type. */
1063 demangled_name
+= 4; /* Skip _vt$ prefix. */
1067 pc_for_sym
= value_as_pointer (value_field (best_entry
, 2));
1068 sym
= find_pc_function (pc_for_sym
);
1069 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), -1);
1070 *(strchr (demangled_name
, ':')) = '\0';
1072 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
1074 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym
));
1077 free (demangled_name
);
1078 arg
= value_add (value_cast (builtin_type_int
, arg
),
1079 value_field (best_entry
, 0));
1081 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
1085 /* ARG is a pointer object of type TYPE. If TYPE has virtual
1086 function tables, probe ARG's tables (including the vtables
1087 of its baseclasses) to figure out the most derived type that ARG
1088 could actually be a pointer to. */
1091 value_from_vtable_info (arg
, type
)
1095 /* Take care of preliminaries. */
1096 if (TYPE_VPTR_FIELDNO (type
) < 0)
1097 fill_in_vptr_fieldno (type
);
1098 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
1101 return value_headof (arg
, 0, type
);
1104 /* Compute the address of the baseclass which is
1105 the INDEXth baseclass of class TYPE. The TYPE base
1106 of the object is at VALADDR.
1108 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1109 or 0 if no error. In that case the return value is not the address
1110 of the baseclasss, but the address which could not be read
1114 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1121 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1126 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1128 /* Must hunt for the pointer to this virtual baseclass. */
1129 register int i
, len
= TYPE_NFIELDS (type
);
1130 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1131 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
1133 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
1134 sprintf (vbase_name
, "_vb$%s", type_name
);
1135 /* First look for the virtual baseclass pointer
1137 for (i
= n_baseclasses
; i
< len
; i
++)
1139 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
1141 value val
= allocate_value (basetype
);
1146 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1147 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1149 status
= target_read_memory (addr
,
1150 VALUE_CONTENTS_RAW (val
),
1151 TYPE_LENGTH (basetype
));
1152 VALUE_LVAL (val
) = lval_memory
;
1153 VALUE_ADDRESS (val
) = addr
;
1159 release_value (val
);
1163 return (char *)addr
;
1169 return (char *) VALUE_CONTENTS (val
);
1173 /* Not in the fields, so try looking through the baseclasses. */
1174 for (i
= index
+1; i
< n_baseclasses
; i
++)
1178 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
, errp
);
1188 /* Baseclass is easily computed. */
1191 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1195 unpack_field_as_long (type
, valaddr
, fieldno
)
1201 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1202 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1204 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1205 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1207 /* Extracting bits depends on endianness of the machine. */
1209 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1211 val
= val
>> (bitpos
% 8);
1214 if (bitsize
< 8 * sizeof (val
))
1215 val
&= (((unsigned long)1) << bitsize
) - 1;
1219 /* Modify the value of a bitfield. ADDR points to a block of memory in
1220 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1221 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1222 indicate which bits (in target bit order) comprise the bitfield. */
1225 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1228 int bitpos
, bitsize
;
1232 /* Reject values too big to fit in the field in question,
1233 otherwise adjoining fields may be corrupted. */
1234 if (bitsize
< (8 * sizeof (fieldval
))
1235 && 0 != (fieldval
& ~((1<<bitsize
)-1)))
1236 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1238 bcopy (addr
, &oword
, sizeof oword
);
1239 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1241 /* Shifting for bit field depends on endianness of the target machine. */
1243 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1246 /* Mask out old value, while avoiding shifts >= longword size */
1247 if (bitsize
< 8 * sizeof (oword
))
1248 oword
&= ~(((((unsigned long)1) << bitsize
) - 1) << bitpos
);
1250 oword
&= ~((-1) << bitpos
);
1251 oword
|= fieldval
<< bitpos
;
1253 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1254 bcopy (&oword
, addr
, sizeof oword
);
1257 /* Convert C numbers into newly allocated values */
1260 value_from_longest (type
, num
)
1262 register LONGEST num
;
1264 register value val
= allocate_value (type
);
1265 register enum type_code code
= TYPE_CODE (type
);
1266 register int len
= TYPE_LENGTH (type
);
1268 /* FIXME, we assume that pointers have the same form and byte order as
1269 integers, and that all pointers have the same form. */
1270 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
||
1271 code
== TYPE_CODE_CHAR
|| code
== TYPE_CODE_PTR
||
1272 code
== TYPE_CODE_REF
)
1274 if (len
== sizeof (char))
1275 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1276 else if (len
== sizeof (short))
1277 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1278 else if (len
== sizeof (int))
1279 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1280 else if (len
== sizeof (long))
1281 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1283 else if (len
== sizeof (long long))
1284 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1287 error ("Integer type encountered with unexpected data length.");
1290 error ("Unexpected type encountered for integer constant.");
1292 /* num was in host byte order. So now put the value's contents
1293 into target byte order. */
1294 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1300 value_from_double (type
, num
)
1304 register value val
= allocate_value (type
);
1305 register enum type_code code
= TYPE_CODE (type
);
1306 register int len
= TYPE_LENGTH (type
);
1308 if (code
== TYPE_CODE_FLT
)
1310 if (len
== sizeof (float))
1311 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1312 else if (len
== sizeof (double))
1313 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1315 error ("Floating type encountered with unexpected data length.");
1318 error ("Unexpected type encountered for floating constant.");
1320 /* num was in host byte order. So now put the value's contents
1321 into target byte order. */
1322 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1327 /* Deal with the value that is "about to be returned". */
1329 /* Return the value that a function returning now
1330 would be returning to its caller, assuming its type is VALTYPE.
1331 RETBUF is where we look for what ought to be the contents
1332 of the registers (in raw form). This is because it is often
1333 desirable to restore old values to those registers
1334 after saving the contents of interest, and then call
1335 this function using the saved values.
1336 struct_return is non-zero when the function in question is
1337 using the structure return conventions on the machine in question;
1338 0 when it is using the value returning conventions (this often
1339 means returning pointer to where structure is vs. returning value). */
1342 value_being_returned (valtype
, retbuf
, struct_return
)
1343 register struct type
*valtype
;
1344 char retbuf
[REGISTER_BYTES
];
1351 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1352 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1353 if (struct_return
) {
1354 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1356 error ("Function return value unknown");
1357 return value_at (valtype
, addr
);
1361 val
= allocate_value (valtype
);
1362 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1367 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1368 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1369 and TYPE is the type (which is known to be struct, union or array).
1371 On most machines, the struct convention is used unless we are
1372 using gcc and the type is of a special size. */
1373 #if !defined (USE_STRUCT_CONVENTION)
1374 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1375 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1376 || TYPE_LENGTH (value_type) == 2 \
1377 || TYPE_LENGTH (value_type) == 4 \
1378 || TYPE_LENGTH (value_type) == 8 \
1383 /* Return true if the function specified is using the structure returning
1384 convention on this machine to return arguments, or 0 if it is using
1385 the value returning convention. FUNCTION is the value representing
1386 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1387 is the type returned by the function. GCC_P is nonzero if compiled
1391 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1394 struct type
*value_type
;
1398 register enum type_code code
= TYPE_CODE (value_type
);
1400 if (code
== TYPE_CODE_ERROR
)
1401 error ("Function return type unknown.");
1403 if (code
== TYPE_CODE_STRUCT
||
1404 code
== TYPE_CODE_UNION
||
1405 code
== TYPE_CODE_ARRAY
)
1406 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1411 /* Store VAL so it will be returned if a function returns now.
1412 Does not verify that VAL's type matches what the current
1413 function wants to return. */
1416 set_return_value (val
)
1419 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1423 if (code
== TYPE_CODE_ERROR
)
1424 error ("Function return type unknown.");
1426 if ( code
== TYPE_CODE_STRUCT
1427 || code
== TYPE_CODE_UNION
) /* FIXME, implement struct return. */
1428 error ("GDB does not support specifying a struct or union return value.");
1430 /* FIXME, this is bogus. We don't know what the return conventions
1431 are, or how values should be promoted.... */
1432 if (code
== TYPE_CODE_FLT
)
1434 dbuf
= value_as_double (val
);
1436 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1440 lbuf
= value_as_long (val
);
1441 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1446 _initialize_values ()
1448 add_cmd ("convenience", no_class
, show_convenience
,
1449 "Debugger convenience (\"$foo\") variables.\n\
1450 These variables are created when you assign them values;\n\
1451 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1452 A few convenience variables are given values automatically:\n\
1453 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1454 \"$__\" holds the contents of the last address examined with \"x\".",
1457 add_cmd ("values", no_class
, show_values
,
1458 "Elements of value history around item number IDX (or last ten).",