1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987, 1989 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. */
31 extern char *cplus_demangle ();
33 /* The value-history records all the values printed
34 by print commands during this session. Each chunk
35 records 60 consecutive values. The first chunk on
36 the chain records the most recent values.
37 The total number of values is in value_history_count. */
39 #define VALUE_HISTORY_CHUNK 60
41 struct value_history_chunk
43 struct value_history_chunk
*next
;
44 value values
[VALUE_HISTORY_CHUNK
];
47 /* Chain of chunks now in use. */
49 static struct value_history_chunk
*value_history_chain
;
51 static int value_history_count
; /* Abs number of last entry stored */
53 /* List of all value objects currently allocated
54 (except for those released by calls to release_value)
55 This is so they can be freed after each command. */
57 static value all_values
;
59 /* Allocate a value that has the correct length for type TYPE. */
67 check_stub_type (type
);
69 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
70 VALUE_NEXT (val
) = all_values
;
72 VALUE_TYPE (val
) = type
;
73 VALUE_LVAL (val
) = not_lval
;
74 VALUE_ADDRESS (val
) = 0;
75 VALUE_FRAME (val
) = 0;
76 VALUE_OFFSET (val
) = 0;
77 VALUE_BITPOS (val
) = 0;
78 VALUE_BITSIZE (val
) = 0;
79 VALUE_REPEATED (val
) = 0;
80 VALUE_REPETITIONS (val
) = 0;
81 VALUE_REGNO (val
) = -1;
83 VALUE_OPTIMIZED_OUT (val
) = 0;
87 /* Allocate a value that has the correct length
88 for COUNT repetitions type TYPE. */
91 allocate_repeat_value (type
, count
)
97 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
98 VALUE_NEXT (val
) = all_values
;
100 VALUE_TYPE (val
) = type
;
101 VALUE_LVAL (val
) = not_lval
;
102 VALUE_ADDRESS (val
) = 0;
103 VALUE_FRAME (val
) = 0;
104 VALUE_OFFSET (val
) = 0;
105 VALUE_BITPOS (val
) = 0;
106 VALUE_BITSIZE (val
) = 0;
107 VALUE_REPEATED (val
) = 1;
108 VALUE_REPETITIONS (val
) = count
;
109 VALUE_REGNO (val
) = -1;
110 VALUE_LAZY (val
) = 0;
111 VALUE_OPTIMIZED_OUT (val
) = 0;
115 /* Return a mark in the value chain. All values allocated after the
116 mark is obtained (except for those released) are subject to being freed
117 if a subsequent value_free_to_mark is passed the mark. */
124 /* Free all values allocated since MARK was obtained by value_mark
125 (except for those released). */
127 value_free_to_mark (mark
)
132 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
134 next
= VALUE_NEXT (val
);
140 /* Free all the values that have been allocated (except for those released).
141 Called after each command, successful or not. */
146 register value val
, next
;
148 for (val
= all_values
; val
; val
= next
)
150 next
= VALUE_NEXT (val
);
157 /* Remove VAL from the chain all_values
158 so it will not be freed automatically. */
166 if (all_values
== val
)
168 all_values
= val
->next
;
172 for (v
= all_values
; v
; v
= v
->next
)
182 /* Return a copy of the value ARG.
183 It contains the same contents, for same memory address,
184 but it's a different block of storage. */
191 register struct type
*type
= VALUE_TYPE (arg
);
192 if (VALUE_REPEATED (arg
))
193 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
195 val
= allocate_value (type
);
196 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
197 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
198 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
199 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
200 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
201 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
202 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
203 if (!VALUE_LAZY (val
))
205 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
206 TYPE_LENGTH (VALUE_TYPE (arg
))
207 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
212 /* Access to the value history. */
214 /* Record a new value in the value history.
215 Returns the absolute history index of the entry.
216 Result of -1 indicates the value was not saved; otherwise it is the
217 value history index of this new item. */
220 record_latest_value (val
)
225 /* Check error now if about to store an invalid float. We return -1
226 to the caller, but allow them to continue, e.g. to print it as "Nan". */
227 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
228 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
229 if (i
) return -1; /* Indicate value not saved in history */
232 /* Here we treat value_history_count as origin-zero
233 and applying to the value being stored now. */
235 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
238 register struct value_history_chunk
*new
239 = (struct value_history_chunk
*)
240 xmalloc (sizeof (struct value_history_chunk
));
241 bzero (new->values
, sizeof new->values
);
242 new->next
= value_history_chain
;
243 value_history_chain
= new;
246 value_history_chain
->values
[i
] = val
;
249 /* Now we regard value_history_count as origin-one
250 and applying to the value just stored. */
252 return ++value_history_count
;
255 /* Return a copy of the value in the history with sequence number NUM. */
258 access_value_history (num
)
261 register struct value_history_chunk
*chunk
;
263 register int absnum
= num
;
266 absnum
+= value_history_count
;
271 error ("The history is empty.");
273 error ("There is only one value in the history.");
275 error ("History does not go back to $$%d.", -num
);
277 if (absnum
> value_history_count
)
278 error ("History has not yet reached $%d.", absnum
);
282 /* Now absnum is always absolute and origin zero. */
284 chunk
= value_history_chain
;
285 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
289 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
292 /* Clear the value history entirely.
293 Must be done when new symbol tables are loaded,
294 because the type pointers become invalid. */
297 clear_value_history ()
299 register struct value_history_chunk
*next
;
303 while (value_history_chain
)
305 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
306 if (val
= value_history_chain
->values
[i
])
308 next
= value_history_chain
->next
;
309 free (value_history_chain
);
310 value_history_chain
= next
;
312 value_history_count
= 0;
316 show_values (num_exp
, from_tty
)
326 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
327 /* "info history +" should print from the stored position. */
330 /* "info history <exp>" should print around value number <exp>. */
331 num
= parse_and_eval_address (num_exp
) - 5;
335 /* "info history" means print the last 10 values. */
336 num
= value_history_count
- 9;
342 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
344 val
= access_value_history (i
);
345 printf_filtered ("$%d = ", i
);
346 value_print (val
, stdout
, 0, Val_pretty_default
);
347 printf_filtered ("\n");
350 /* The next "info history +" should start after what we just printed. */
353 /* Hitting just return after this command should do the same thing as
354 "info history +". If num_exp is null, this is unnecessary, since
355 "info history +" is not useful after "info history". */
356 if (from_tty
&& num_exp
)
363 /* Internal variables. These are variables within the debugger
364 that hold values assigned by debugger commands.
365 The user refers to them with a '$' prefix
366 that does not appear in the variable names stored internally. */
368 static struct internalvar
*internalvars
;
370 /* Look up an internal variable with name NAME. NAME should not
371 normally include a dollar sign.
373 If the specified internal variable does not exist,
374 one is created, with a void value. */
377 lookup_internalvar (name
)
380 register struct internalvar
*var
;
382 for (var
= internalvars
; var
; var
= var
->next
)
383 if (!strcmp (var
->name
, name
))
386 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
387 var
->name
= concat (name
, "", "");
388 var
->value
= allocate_value (builtin_type_void
);
389 release_value (var
->value
);
390 var
->next
= internalvars
;
396 value_of_internalvar (var
)
397 struct internalvar
*var
;
401 #ifdef IS_TRAPPED_INTERNALVAR
402 if (IS_TRAPPED_INTERNALVAR (var
->name
))
403 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
406 val
= value_copy (var
->value
);
407 if (VALUE_LAZY (val
))
408 value_fetch_lazy (val
);
409 VALUE_LVAL (val
) = lval_internalvar
;
410 VALUE_INTERNALVAR (val
) = var
;
415 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
416 struct internalvar
*var
;
417 int offset
, bitpos
, bitsize
;
420 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
422 #ifdef IS_TRAPPED_INTERNALVAR
423 if (IS_TRAPPED_INTERNALVAR (var
->name
))
424 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
428 modify_field (addr
, (int) value_as_long (newval
),
431 bcopy (VALUE_CONTENTS (newval
), addr
,
432 TYPE_LENGTH (VALUE_TYPE (newval
)));
436 set_internalvar (var
, val
)
437 struct internalvar
*var
;
440 #ifdef IS_TRAPPED_INTERNALVAR
441 if (IS_TRAPPED_INTERNALVAR (var
->name
))
442 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
446 var
->value
= value_copy (val
);
447 release_value (var
->value
);
451 internalvar_name (var
)
452 struct internalvar
*var
;
457 /* Free all internalvars. Done when new symtabs are loaded,
458 because that makes the values invalid. */
461 clear_internalvars ()
463 register struct internalvar
*var
;
468 internalvars
= var
->next
;
478 register struct internalvar
*var
;
481 for (var
= internalvars
; var
; var
= var
->next
)
483 #ifdef IS_TRAPPED_INTERNALVAR
484 if (IS_TRAPPED_INTERNALVAR (var
->name
))
491 printf ("Debugger convenience variables:\n\n");
495 printf_filtered ("$%s = ", var
->name
);
496 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
497 printf_filtered ("\n");
500 printf ("No debugger convenience variables now defined.\n\
501 Convenience variables have names starting with \"$\";\n\
502 use \"set\" as in \"set $foo = 5\" to define them.\n");
505 /* Extract a value as a C number (either long or double).
506 Knows how to convert fixed values to double, or
507 floating values to long.
508 Does not deallocate the value. */
514 /* This coerces arrays and functions, which is necessary (e.g.
515 in disassemble_command). It also dereferences references, which
516 I suspect is the most logical thing to do. */
517 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
519 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
523 value_as_double (val
)
529 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
531 error ("Invalid floating value found in program.");
534 /* Extract a value as a C pointer.
535 Does not deallocate the value. */
537 value_as_pointer (val
)
540 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
541 whether we want this to be true eventually. */
542 return value_as_long (val
);
545 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
546 as a long, or as a double, assuming the raw data is described
547 by type TYPE. Knows how to convert different sizes of values
548 and can convert between fixed and floating point. We don't assume
549 any alignment for the raw data. Return value is in host byte order.
551 If you want functions and arrays to be coerced to pointers, and
552 references to be dereferenced, call value_as_long() instead.
554 C++: It is assumed that the front-end has taken care of
555 all matters concerning pointers to members. A pointer
556 to member which reaches here is considered to be equivalent
557 to an INT (or some size). After all, it is only an offset. */
559 /* FIXME: This should be rewritten as a switch statement for speed and
560 ease of comprehension. */
563 unpack_long (type
, valaddr
)
567 register enum type_code code
= TYPE_CODE (type
);
568 register int len
= TYPE_LENGTH (type
);
569 register int nosign
= TYPE_UNSIGNED (type
);
571 if (code
== TYPE_CODE_ENUM
|| code
== TYPE_CODE_BOOL
)
572 code
= TYPE_CODE_INT
;
573 if (code
== TYPE_CODE_FLT
)
575 if (len
== sizeof (float))
578 bcopy (valaddr
, &retval
, sizeof (retval
));
579 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
583 if (len
== sizeof (double))
586 bcopy (valaddr
, &retval
, sizeof (retval
));
587 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
592 error ("Unexpected type of floating point number.");
595 else if (code
== TYPE_CODE_INT
&& nosign
)
597 if (len
== sizeof (char))
599 unsigned char retval
= * (unsigned char *) valaddr
;
600 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
604 if (len
== sizeof (short))
606 unsigned short retval
;
607 bcopy (valaddr
, &retval
, sizeof (retval
));
608 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
612 if (len
== sizeof (int))
615 bcopy (valaddr
, &retval
, sizeof (retval
));
616 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
620 if (len
== sizeof (long))
622 unsigned long retval
;
623 bcopy (valaddr
, &retval
, sizeof (retval
));
624 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
628 if (len
== sizeof (long long))
630 unsigned long long retval
;
631 bcopy (valaddr
, &retval
, sizeof (retval
));
632 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
638 error ("That operation is not possible on an integer of that size.");
641 else if (code
== TYPE_CODE_INT
)
643 if (len
== sizeof (char))
646 bcopy (valaddr
, &retval
, sizeof (retval
));
647 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
651 if (len
== sizeof (short))
654 bcopy (valaddr
, &retval
, sizeof (retval
));
655 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
659 if (len
== sizeof (int))
662 bcopy (valaddr
, &retval
, sizeof (retval
));
663 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
667 if (len
== sizeof (long))
670 bcopy (valaddr
, &retval
, sizeof (retval
));
671 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
676 if (len
== sizeof (long long))
679 bcopy (valaddr
, &retval
, sizeof (retval
));
680 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
686 error ("That operation is not possible on an integer of that size.");
689 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
690 whether we want this to be true eventually. */
691 else if (code
== TYPE_CODE_PTR
692 || code
== TYPE_CODE_REF
)
694 if (len
== sizeof (CORE_ADDR
))
697 bcopy (valaddr
, &retval
, sizeof (retval
));
698 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
702 else if (code
== TYPE_CODE_MEMBER
)
703 error ("not implemented: member types in unpack_long");
704 else if (code
== TYPE_CODE_CHAR
)
705 return *(unsigned char *)valaddr
;
707 error ("Value not integer or pointer.");
708 return 0; /* For lint -- never reached */
711 /* Return a double value from the specified type and address.
712 INVP points to an int which is set to 0 for valid value,
713 1 for invalid value (bad float format). In either case,
714 the returned double is OK to use. Argument is in target
715 format, result is in host format. */
718 unpack_double (type
, valaddr
, invp
)
723 register enum type_code code
= TYPE_CODE (type
);
724 register int len
= TYPE_LENGTH (type
);
725 register int nosign
= TYPE_UNSIGNED (type
);
727 *invp
= 0; /* Assume valid. */
728 if (code
== TYPE_CODE_FLT
)
730 if (INVALID_FLOAT (valaddr
, len
))
733 return 1.234567891011121314;
736 if (len
== sizeof (float))
739 bcopy (valaddr
, &retval
, sizeof (retval
));
740 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
744 if (len
== sizeof (double))
747 bcopy (valaddr
, &retval
, sizeof (retval
));
748 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
753 error ("Unexpected type of floating point number.");
754 return 0; /* Placate lint. */
758 /* Unsigned -- be sure we compensate for signed LONGEST. */
760 return (unsigned long long) unpack_long (type
, valaddr
);
762 return (unsigned long ) unpack_long (type
, valaddr
);
765 /* Signed -- we are OK with unpack_long. */
766 return unpack_long (type
, valaddr
);
770 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
771 as a CORE_ADDR, assuming the raw data is described by type TYPE.
772 We don't assume any alignment for the raw data. Return value is in
775 If you want functions and arrays to be coerced to pointers, and
776 references to be dereferenced, call value_as_pointer() instead.
778 C++: It is assumed that the front-end has taken care of
779 all matters concerning pointers to members. A pointer
780 to member which reaches here is considered to be equivalent
781 to an INT (or some size). After all, it is only an offset. */
784 unpack_pointer (type
, valaddr
)
789 /* The user should be able to use an int (e.g. 0x7892) in contexts
790 where a pointer is expected. So this doesn't do enough. */
791 register enum type_code code
= TYPE_CODE (type
);
792 register int len
= TYPE_LENGTH (type
);
794 if (code
== TYPE_CODE_PTR
795 || code
== TYPE_CODE_REF
)
797 if (len
== sizeof (CORE_ADDR
))
800 bcopy (valaddr
, &retval
, sizeof (retval
));
801 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
804 error ("Unrecognized pointer size.");
806 else if (code
== TYPE_CODE_MEMBER
)
807 error ("not implemented: member types in unpack_pointer");
809 error ("Value is not a pointer.");
810 return 0; /* For lint -- never reached */
812 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
813 whether we want this to be true eventually. */
814 return unpack_long (type
, valaddr
);
818 /* Given a value ARG1 (offset by OFFSET bytes)
819 of a struct or union type ARG_TYPE,
820 extract and return the value of one of its fields.
821 FIELDNO says which field.
823 For C++, must also be able to return values from static fields */
826 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
829 register int fieldno
;
830 register struct type
*arg_type
;
833 register struct type
*type
;
835 check_stub_type (arg_type
);
836 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
838 /* Handle packed fields */
840 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
841 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
843 v
= value_from_longest (type
,
844 unpack_field_as_long (arg_type
,
845 VALUE_CONTENTS (arg1
),
847 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
848 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
852 v
= allocate_value (type
);
853 if (VALUE_LAZY (arg1
))
856 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
857 VALUE_CONTENTS_RAW (v
),
860 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
861 if (VALUE_LVAL (arg1
) == lval_internalvar
)
862 VALUE_LVAL (v
) = lval_internalvar_component
;
863 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
864 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
868 /* Given a value ARG1 of a struct or union type,
869 extract and return the value of one of its fields.
870 FIELDNO says which field.
872 For C++, must also be able to return values from static fields */
875 value_field (arg1
, fieldno
)
877 register int fieldno
;
879 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
883 value_fn_field (arg1
, fieldno
, subfieldno
)
885 register int fieldno
;
889 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
890 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
893 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
894 0, VAR_NAMESPACE
, 0, NULL
);
895 if (! sym
) error ("Internal error: could not find physical method named %s",
896 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
898 v
= allocate_value (type
);
899 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
900 VALUE_TYPE (v
) = type
;
904 /* Return a virtual function as a value.
905 ARG1 is the object which provides the virtual function
906 table pointer. ARG1 is side-effected in calling this function.
907 F is the list of member functions which contains the desired virtual
909 J is an index into F which provides the desired virtual function.
911 TYPE is the type in which F is located. */
913 value_virtual_fn_field (arg1
, f
, j
, type
)
919 /* First, get the virtual function table pointer. That comes
920 with a strange type, so cast it to type `pointer to long' (which
921 should serve just fine as a function type). Then, index into
922 the table, and convert final value to appropriate function type. */
923 value entry
, vfn
, vtbl
;
924 value vi
= value_from_longest (builtin_type_int
,
925 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
926 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
927 struct type
*context
;
928 if (fcontext
== NULL
)
929 /* We don't have an fcontext (e.g. the program was compiled with
930 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
931 This won't work right for multiple inheritance, but at least we
932 should do as well as GDB 3.x did. */
933 fcontext
= TYPE_VPTR_BASETYPE (type
);
934 context
= lookup_pointer_type (fcontext
);
935 /* Now context is a pointer to the basetype containing the vtbl. */
936 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
937 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
939 context
= VALUE_TYPE (arg1
);
940 /* Now context is the basetype containing the vtbl. */
942 /* This type may have been defined before its virtual function table
943 was. If so, fill in the virtual function table entry for the
945 if (TYPE_VPTR_FIELDNO (context
) < 0)
946 fill_in_vptr_fieldno (context
);
948 /* The virtual function table is now an array of structures
949 which have the form { int16 offset, delta; void *pfn; }. */
950 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
952 /* Index into the virtual function table. This is hard-coded because
953 looking up a field is not cheap, and it may be important to save
954 time, e.g. if the user has set a conditional breakpoint calling
955 a virtual function. */
956 entry
= value_subscript (vtbl
, vi
);
958 /* Move the `this' pointer according to the virtual function table. */
959 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
960 if (! VALUE_LAZY (arg1
))
962 VALUE_LAZY (arg1
) = 1;
963 value_fetch_lazy (arg1
);
966 vfn
= value_field (entry
, 2);
967 /* Reinstantiate the function pointer with the correct type. */
968 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
973 /* ARG is a pointer to an object we know to be at least
974 a DTYPE. BTYPE is the most derived basetype that has
975 already been searched (and need not be searched again).
976 After looking at the vtables between BTYPE and DTYPE,
977 return the most derived type we find. The caller must
978 be satisfied when the return value == DTYPE.
980 FIXME-tiemann: should work with dossier entries as well. */
983 value_headof (arg
, btype
, dtype
)
985 struct type
*btype
, *dtype
;
987 /* First collect the vtables we must look at for this object. */
988 /* FIXME-tiemann: right now, just look at top-most vtable. */
989 value vtbl
, entry
, best_entry
= 0;
990 /* FIXME: entry_type is never used. */
991 struct type
*entry_type
;
993 int offset
, best_offset
= 0;
995 CORE_ADDR pc_for_sym
;
996 char *demangled_name
;
997 btype
= TYPE_VPTR_BASETYPE (dtype
);
998 check_stub_type (btype
);
1000 vtbl
= value_cast (lookup_pointer_type (btype
), arg
);
1003 vtbl
= value_ind (value_field (value_ind (vtbl
), TYPE_VPTR_FIELDNO (btype
)));
1005 /* Check that VTBL looks like it points to a virtual function table. */
1006 i
= find_pc_misc_function (VALUE_ADDRESS (vtbl
));
1007 if (i
< 0 || ! VTBL_PREFIX_P (demangled_name
= misc_function_vector
[i
].name
))
1009 /* If we expected to find a vtable, but did not, let the user
1010 know that we aren't happy, but don't throw an error.
1011 FIXME: there has to be a better way to do this. */
1012 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
1013 bcopy (VALUE_TYPE (arg
), error_type
, sizeof (struct type
));
1014 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
1015 VALUE_TYPE (arg
) = error_type
;
1019 /* Now search through the virtual function table. */
1020 entry
= value_ind (vtbl
);
1021 nelems
= longest_to_int (value_as_long (value_field (entry
, 2)));
1022 for (i
= 1; i
<= nelems
; i
++)
1024 entry
= value_subscript (vtbl
, value_from_longest (builtin_type_int
,
1026 offset
= longest_to_int (value_as_long (value_field (entry
, 0)));
1027 /* If we use '<=' we can handle single inheritance
1028 * where all offsets are zero - just use the first entry found. */
1029 if (offset
<= best_offset
)
1031 best_offset
= offset
;
1035 /* Move the pointer according to BEST_ENTRY's offset, and figure
1036 out what type we should return as the new pointer. */
1037 if (best_entry
== 0)
1039 /* An alternative method (which should no longer be necessary).
1040 * But we leave it in for future use, when we will hopefully
1041 * have optimizes the vtable to use thunks instead of offsets. */
1042 /* Use the name of vtable itself to extract a base type. */
1043 demangled_name
+= 4; /* Skip _vt$ prefix. */
1047 pc_for_sym
= value_as_pointer (value_field (best_entry
, 2));
1048 sym
= find_pc_function (pc_for_sym
);
1049 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), -1);
1050 *(strchr (demangled_name
, ':')) = '\0';
1052 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
1054 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym
));
1057 free (demangled_name
);
1058 arg
= value_add (value_cast (builtin_type_int
, arg
),
1059 value_field (best_entry
, 0));
1061 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
1065 /* ARG is a pointer object of type TYPE. If TYPE has virtual
1066 function tables, probe ARG's tables (including the vtables
1067 of its baseclasses) to figure out the most derived type that ARG
1068 could actually be a pointer to. */
1071 value_from_vtable_info (arg
, type
)
1075 /* Take care of preliminaries. */
1076 if (TYPE_VPTR_FIELDNO (type
) < 0)
1077 fill_in_vptr_fieldno (type
);
1078 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
1081 return value_headof (arg
, 0, type
);
1084 /* The value of a static class member does not depend
1085 on its instance, only on its type. If FIELDNO >= 0,
1086 then fieldno is a valid field number and is used directly.
1087 Otherwise, FIELDNAME is the name of the field we are
1088 searching for. If it is not a static field name, an
1089 error is signaled. TYPE is the type in which we look for the
1090 static field member.
1092 Return zero if we couldn't find anything; the caller may signal
1093 an error in that case. */
1096 value_static_field (type
, fieldname
, fieldno
)
1097 register struct type
*type
;
1099 register int fieldno
;
1107 /* Look for static field. */
1109 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1110 if (! strcmp (TYPE_FIELD_NAME (type
, i
), fieldname
))
1112 if (TYPE_FIELD_STATIC (type
, i
))
1118 error ("field `%s' is not static", fieldname
);
1122 v
= value_static_field (TYPE_BASECLASS (type
, i
), fieldname
, -1);
1127 if (destructor_name_p (fieldname
, type
))
1128 error ("Cannot get value of destructor");
1130 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1132 if (! strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), fieldname
))
1133 error ("Cannot get value of method \"%s\"", fieldname
);
1135 error("there is no field named %s", fieldname
);
1139 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1140 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1141 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1143 type
= TYPE_FIELD_TYPE (type
, fieldno
);
1144 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1148 /* Compute the address of the baseclass which is
1149 the INDEXth baseclass of TYPE. The TYPE base
1150 of the object is at VALADDR.
1152 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1153 or 0 if no error. In that case the return value is not the address
1154 of the baseclasss, but the address which could not be read
1158 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1165 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1170 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1172 /* Must hunt for the pointer to this virtual baseclass. */
1173 register int i
, len
= TYPE_NFIELDS (type
);
1174 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1175 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
1177 if (TYPE_MAIN_VARIANT (basetype
))
1178 basetype
= TYPE_MAIN_VARIANT (basetype
);
1180 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
1181 sprintf (vbase_name
, "_vb$%s", type_name
);
1182 /* First look for the virtual baseclass pointer
1184 for (i
= n_baseclasses
; i
< len
; i
++)
1186 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
1188 value val
= allocate_value (basetype
);
1193 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1194 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1196 status
= target_read_memory (addr
,
1197 VALUE_CONTENTS_RAW (val
),
1198 TYPE_LENGTH (basetype
));
1199 VALUE_LVAL (val
) = lval_memory
;
1200 VALUE_ADDRESS (val
) = addr
;
1206 release_value (val
);
1210 return (char *)addr
;
1216 return (char *) VALUE_CONTENTS (val
);
1220 /* Not in the fields, so try looking through the baseclasses. */
1221 for (i
= index
+1; i
< n_baseclasses
; i
++)
1225 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
, errp
);
1235 /* Baseclass is easily computed. */
1238 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1241 /* Ugly hack to convert method stubs into method types.
1243 He ain't kiddin'. This demangles the name of the method into a string
1244 including argument types, parses out each argument type, generates
1245 a string casting a zero to that type, evaluates the string, and stuffs
1246 the resulting type into an argtype vector!!! Then it knows the type
1247 of the whole function (including argument types for overloading),
1248 which info used to be in the stab's but was removed to hack back
1249 the space required for them. */
1251 check_stub_method (type
, i
, j
)
1255 extern char *gdb_mangle_name (), *strchr ();
1256 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1257 char *mangled_name
= gdb_mangle_name (type
, i
, j
);
1258 char *demangled_name
= cplus_demangle (mangled_name
, 0);
1259 char *argtypetext
, *p
;
1260 int depth
= 0, argcount
= 1;
1261 struct type
**argtypes
;
1263 /* Now, read in the parameters that define this type. */
1264 argtypetext
= strchr (demangled_name
, '(') + 1;
1272 else if (*p
== ',' && depth
== 0)
1277 /* We need one more slot for the void [...] or NULL [end of arglist] */
1278 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1280 argtypes
[0] = lookup_pointer_type (type
);
1283 if (*p
!= ')') /* () means no args, skip while */
1292 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1294 char *tmp
= (char *)alloca (p
- argtypetext
+ 4);
1297 bcopy (argtypetext
, tmp
+1, p
- argtypetext
);
1298 tmp
[p
-argtypetext
+1] = ')';
1299 tmp
[p
-argtypetext
+2] = '0';
1300 tmp
[p
-argtypetext
+3] = '\0';
1301 val
= parse_and_eval (tmp
);
1302 argtypes
[argcount
] = VALUE_TYPE (val
);
1304 argtypetext
= p
+ 1;
1310 if (p
[-2] != '.') /* ... */
1311 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1313 argtypes
[argcount
] = NULL
; /* List terminator */
1315 free (demangled_name
);
1317 type
= lookup_method_type (type
, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)), argtypes
);
1318 /* Free the stub type...it's no longer needed. */
1319 free (TYPE_FN_FIELD_TYPE (f
, j
));
1320 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1321 TYPE_FN_FIELD_TYPE (f
, j
) = type
;
1325 unpack_field_as_long (type
, valaddr
, fieldno
)
1331 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1332 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1334 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1335 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1337 /* Extracting bits depends on endianness of the machine. */
1339 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1341 val
= val
>> (bitpos
% 8);
1344 if (bitsize
< 8 * sizeof (val
))
1345 val
&= (((unsigned long)1) << bitsize
) - 1;
1349 /* Modify the value of a bitfield. ADDR points to a block of memory in
1350 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1351 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1352 indicate which bits (in target bit order) comprise the bitfield. */
1355 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1358 int bitpos
, bitsize
;
1362 /* Reject values too big to fit in the field in question,
1363 otherwise adjoining fields may be corrupted. */
1364 if (bitsize
< (8 * sizeof (fieldval
))
1365 && 0 != (fieldval
& ~((1<<bitsize
)-1)))
1366 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1368 bcopy (addr
, &oword
, sizeof oword
);
1369 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1371 /* Shifting for bit field depends on endianness of the target machine. */
1373 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1376 /* Mask out old value, while avoiding shifts >= longword size */
1377 if (bitsize
< 8 * sizeof (oword
))
1378 oword
&= ~(((((unsigned long)1) << bitsize
) - 1) << bitpos
);
1380 oword
&= ~((-1) << bitpos
);
1381 oword
|= fieldval
<< bitpos
;
1383 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1384 bcopy (&oword
, addr
, sizeof oword
);
1387 /* Convert C numbers into newly allocated values */
1390 value_from_longest (type
, num
)
1392 register LONGEST num
;
1394 register value val
= allocate_value (type
);
1395 register enum type_code code
= TYPE_CODE (type
);
1396 register int len
= TYPE_LENGTH (type
);
1398 /* FIXME, we assume that pointers have the same form and byte order as
1399 integers, and that all pointers have the same form. */
1400 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
||
1401 code
== TYPE_CODE_CHAR
|| code
== TYPE_CODE_PTR
)
1403 if (len
== sizeof (char))
1404 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1405 else if (len
== sizeof (short))
1406 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1407 else if (len
== sizeof (int))
1408 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1409 else if (len
== sizeof (long))
1410 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1412 else if (len
== sizeof (long long))
1413 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1416 error ("Integer type encountered with unexpected data length.");
1419 error ("Unexpected type encountered for integer constant.");
1421 /* num was in host byte order. So now put the value's contents
1422 into target byte order. */
1423 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1429 value_from_double (type
, num
)
1433 register value val
= allocate_value (type
);
1434 register enum type_code code
= TYPE_CODE (type
);
1435 register int len
= TYPE_LENGTH (type
);
1437 if (code
== TYPE_CODE_FLT
)
1439 if (len
== sizeof (float))
1440 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1441 else if (len
== sizeof (double))
1442 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1444 error ("Floating type encountered with unexpected data length.");
1447 error ("Unexpected type encountered for floating constant.");
1449 /* num was in host byte order. So now put the value's contents
1450 into target byte order. */
1451 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1456 /* Deal with the value that is "about to be returned". */
1458 /* Return the value that a function returning now
1459 would be returning to its caller, assuming its type is VALTYPE.
1460 RETBUF is where we look for what ought to be the contents
1461 of the registers (in raw form). This is because it is often
1462 desirable to restore old values to those registers
1463 after saving the contents of interest, and then call
1464 this function using the saved values.
1465 struct_return is non-zero when the function in question is
1466 using the structure return conventions on the machine in question;
1467 0 when it is using the value returning conventions (this often
1468 means returning pointer to where structure is vs. returning value). */
1471 value_being_returned (valtype
, retbuf
, struct_return
)
1472 register struct type
*valtype
;
1473 char retbuf
[REGISTER_BYTES
];
1480 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1481 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1482 if (struct_return
) {
1483 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1485 error ("Function return value unknown");
1486 return value_at (valtype
, addr
);
1490 val
= allocate_value (valtype
);
1491 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1496 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1497 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1498 and TYPE is the type (which is known to be struct, union or array).
1500 On most machines, the struct convention is used unless we are
1501 using gcc and the type is of a special size. */
1502 #if !defined (USE_STRUCT_CONVENTION)
1503 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1504 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1505 || TYPE_LENGTH (value_type) == 2 \
1506 || TYPE_LENGTH (value_type) == 4 \
1507 || TYPE_LENGTH (value_type) == 8 \
1512 /* Return true if the function specified is using the structure returning
1513 convention on this machine to return arguments, or 0 if it is using
1514 the value returning convention. FUNCTION is the value representing
1515 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1516 is the type returned by the function. GCC_P is nonzero if compiled
1520 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1523 struct type
*value_type
;
1527 register enum type_code code
= TYPE_CODE (value_type
);
1529 if (code
== TYPE_CODE_ERROR
)
1530 error ("Function return type unknown.");
1532 if (code
== TYPE_CODE_STRUCT
||
1533 code
== TYPE_CODE_UNION
||
1534 code
== TYPE_CODE_ARRAY
)
1535 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1540 /* Store VAL so it will be returned if a function returns now.
1541 Does not verify that VAL's type matches what the current
1542 function wants to return. */
1545 set_return_value (val
)
1548 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1552 if (code
== TYPE_CODE_ERROR
)
1553 error ("Function return type unknown.");
1555 if (code
== TYPE_CODE_STRUCT
1556 || code
== TYPE_CODE_UNION
)
1557 error ("Specifying a struct or union return value is not supported.");
1559 /* FIXME, this is bogus. We don't know what the return conventions
1560 are, or how values should be promoted.... */
1561 if (code
== TYPE_CODE_FLT
)
1563 dbuf
= value_as_double (val
);
1565 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1569 lbuf
= value_as_long (val
);
1570 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1575 _initialize_values ()
1577 add_cmd ("convenience", no_class
, show_convenience
,
1578 "Debugger convenience (\"$foo\") variables.\n\
1579 These variables are created when you assign them values;\n\
1580 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1581 A few convenience variables are given values automatically:\n\
1582 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1583 \"$__\" holds the contents of the last address examined with \"x\".",
1586 add_cmd ("values", no_class
, show_values
,
1587 "Elements of value history around item number IDX (or last ten).",