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 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 extern char *cplus_demangle ();
32 extern char *cplus_mangle_opname ();
34 /* The value-history records all the values printed
35 by print commands during this session. Each chunk
36 records 60 consecutive values. The first chunk on
37 the chain records the most recent values.
38 The total number of values is in value_history_count. */
40 #define VALUE_HISTORY_CHUNK 60
42 struct value_history_chunk
44 struct value_history_chunk
*next
;
45 value values
[VALUE_HISTORY_CHUNK
];
48 /* Chain of chunks now in use. */
50 static struct value_history_chunk
*value_history_chain
;
52 static int value_history_count
; /* Abs number of last entry stored */
54 /* List of all value objects currently allocated
55 (except for those released by calls to release_value)
56 This is so they can be freed after each command. */
58 static value all_values
;
60 /* Allocate a value that has the correct length for type TYPE. */
68 check_stub_type (type
);
70 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
71 VALUE_NEXT (val
) = all_values
;
73 VALUE_TYPE (val
) = type
;
74 VALUE_LVAL (val
) = not_lval
;
75 VALUE_ADDRESS (val
) = 0;
76 VALUE_FRAME (val
) = 0;
77 VALUE_OFFSET (val
) = 0;
78 VALUE_BITPOS (val
) = 0;
79 VALUE_BITSIZE (val
) = 0;
80 VALUE_REPEATED (val
) = 0;
81 VALUE_REPETITIONS (val
) = 0;
82 VALUE_REGNO (val
) = -1;
84 VALUE_OPTIMIZED_OUT (val
) = 0;
88 /* Allocate a value that has the correct length
89 for COUNT repetitions type TYPE. */
92 allocate_repeat_value (type
, count
)
98 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
99 VALUE_NEXT (val
) = all_values
;
101 VALUE_TYPE (val
) = type
;
102 VALUE_LVAL (val
) = not_lval
;
103 VALUE_ADDRESS (val
) = 0;
104 VALUE_FRAME (val
) = 0;
105 VALUE_OFFSET (val
) = 0;
106 VALUE_BITPOS (val
) = 0;
107 VALUE_BITSIZE (val
) = 0;
108 VALUE_REPEATED (val
) = 1;
109 VALUE_REPETITIONS (val
) = count
;
110 VALUE_REGNO (val
) = -1;
111 VALUE_LAZY (val
) = 0;
112 VALUE_OPTIMIZED_OUT (val
) = 0;
116 /* Return a mark in the value chain. All values allocated after the
117 mark is obtained (except for those released) are subject to being freed
118 if a subsequent value_free_to_mark is passed the mark. */
125 /* Free all values allocated since MARK was obtained by value_mark
126 (except for those released). */
128 value_free_to_mark (mark
)
133 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
135 next
= VALUE_NEXT (val
);
141 /* Free all the values that have been allocated (except for those released).
142 Called after each command, successful or not. */
147 register value val
, next
;
149 for (val
= all_values
; val
; val
= next
)
151 next
= VALUE_NEXT (val
);
158 /* Remove VAL from the chain all_values
159 so it will not be freed automatically. */
167 if (all_values
== val
)
169 all_values
= val
->next
;
173 for (v
= all_values
; v
; v
= v
->next
)
183 /* Return a copy of the value ARG.
184 It contains the same contents, for same memory address,
185 but it's a different block of storage. */
192 register struct type
*type
= VALUE_TYPE (arg
);
193 if (VALUE_REPEATED (arg
))
194 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
196 val
= allocate_value (type
);
197 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
198 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
199 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
200 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
201 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
202 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
203 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
204 if (!VALUE_LAZY (val
))
206 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
207 TYPE_LENGTH (VALUE_TYPE (arg
))
208 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
213 /* Access to the value history. */
215 /* Record a new value in the value history.
216 Returns the absolute history index of the entry.
217 Result of -1 indicates the value was not saved; otherwise it is the
218 value history index of this new item. */
221 record_latest_value (val
)
226 /* Check error now if about to store an invalid float. We return -1
227 to the caller, but allow them to continue, e.g. to print it as "Nan". */
228 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
229 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
230 if (i
) return -1; /* Indicate value not saved in history */
233 /* Here we treat value_history_count as origin-zero
234 and applying to the value being stored now. */
236 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
239 register struct value_history_chunk
*new
240 = (struct value_history_chunk
*)
241 xmalloc (sizeof (struct value_history_chunk
));
242 bzero (new->values
, sizeof new->values
);
243 new->next
= value_history_chain
;
244 value_history_chain
= new;
247 value_history_chain
->values
[i
] = val
;
250 /* Now we regard value_history_count as origin-one
251 and applying to the value just stored. */
253 return ++value_history_count
;
256 /* Return a copy of the value in the history with sequence number NUM. */
259 access_value_history (num
)
262 register struct value_history_chunk
*chunk
;
264 register int absnum
= num
;
267 absnum
+= value_history_count
;
272 error ("The history is empty.");
274 error ("There is only one value in the history.");
276 error ("History does not go back to $$%d.", -num
);
278 if (absnum
> value_history_count
)
279 error ("History has not yet reached $%d.", absnum
);
283 /* Now absnum is always absolute and origin zero. */
285 chunk
= value_history_chain
;
286 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
290 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
293 /* Clear the value history entirely.
294 Must be done when new symbol tables are loaded,
295 because the type pointers become invalid. */
298 clear_value_history ()
300 register struct value_history_chunk
*next
;
304 while (value_history_chain
)
306 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
307 if (val
= value_history_chain
->values
[i
])
309 next
= value_history_chain
->next
;
310 free (value_history_chain
);
311 value_history_chain
= next
;
313 value_history_count
= 0;
317 show_values (num_exp
, from_tty
)
327 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
328 /* "info history +" should print from the stored position. */
331 /* "info history <exp>" should print around value number <exp>. */
332 num
= parse_and_eval_address (num_exp
) - 5;
336 /* "info history" means print the last 10 values. */
337 num
= value_history_count
- 9;
343 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
345 val
= access_value_history (i
);
346 printf_filtered ("$%d = ", i
);
347 value_print (val
, stdout
, 0, Val_pretty_default
);
348 printf_filtered ("\n");
351 /* The next "info history +" should start after what we just printed. */
354 /* Hitting just return after this command should do the same thing as
355 "info history +". If num_exp is null, this is unnecessary, since
356 "info history +" is not useful after "info history". */
357 if (from_tty
&& num_exp
)
364 /* Internal variables. These are variables within the debugger
365 that hold values assigned by debugger commands.
366 The user refers to them with a '$' prefix
367 that does not appear in the variable names stored internally. */
369 static struct internalvar
*internalvars
;
371 /* Look up an internal variable with name NAME. NAME should not
372 normally include a dollar sign.
374 If the specified internal variable does not exist,
375 one is created, with a void value. */
378 lookup_internalvar (name
)
381 register struct internalvar
*var
;
383 for (var
= internalvars
; var
; var
= var
->next
)
384 if (!strcmp (var
->name
, name
))
387 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
388 var
->name
= concat (name
, "", "");
389 var
->value
= allocate_value (builtin_type_void
);
390 release_value (var
->value
);
391 var
->next
= internalvars
;
397 value_of_internalvar (var
)
398 struct internalvar
*var
;
402 #ifdef IS_TRAPPED_INTERNALVAR
403 if (IS_TRAPPED_INTERNALVAR (var
->name
))
404 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
407 val
= value_copy (var
->value
);
408 if (VALUE_LAZY (val
))
409 value_fetch_lazy (val
);
410 VALUE_LVAL (val
) = lval_internalvar
;
411 VALUE_INTERNALVAR (val
) = var
;
416 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
417 struct internalvar
*var
;
418 int offset
, bitpos
, bitsize
;
421 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
423 #ifdef IS_TRAPPED_INTERNALVAR
424 if (IS_TRAPPED_INTERNALVAR (var
->name
))
425 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
429 modify_field (addr
, (int) value_as_long (newval
),
432 bcopy (VALUE_CONTENTS (newval
), addr
,
433 TYPE_LENGTH (VALUE_TYPE (newval
)));
437 set_internalvar (var
, val
)
438 struct internalvar
*var
;
441 #ifdef IS_TRAPPED_INTERNALVAR
442 if (IS_TRAPPED_INTERNALVAR (var
->name
))
443 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
447 var
->value
= value_copy (val
);
448 release_value (var
->value
);
452 internalvar_name (var
)
453 struct internalvar
*var
;
458 /* Free all internalvars. Done when new symtabs are loaded,
459 because that makes the values invalid. */
462 clear_internalvars ()
464 register struct internalvar
*var
;
469 internalvars
= var
->next
;
479 register struct internalvar
*var
;
482 for (var
= internalvars
; var
; var
= var
->next
)
484 #ifdef IS_TRAPPED_INTERNALVAR
485 if (IS_TRAPPED_INTERNALVAR (var
->name
))
492 printf ("Debugger convenience variables:\n\n");
496 printf ("$%s = ", var
->name
);
497 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
501 printf ("No debugger convenience variables now defined.\n\
502 Convenience variables have names starting with \"$\";\n\
503 use \"set\" as in \"set $foo = 5\" to define them.\n");
506 /* Extract a value as a C number (either long or double).
507 Knows how to convert fixed values to double, or
508 floating values to long.
509 Does not deallocate the value. */
515 /* This coerces arrays and functions, which is necessary (e.g.
516 in disassemble_command). It also dereferences references, which
517 I suspect is the most logical thing to do. */
518 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
520 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
524 value_as_double (val
)
530 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
532 error ("Invalid floating value found in program.");
535 /* Extract a value as a C pointer.
536 Does not deallocate the value. */
538 value_as_pointer (val
)
541 /* This coerces arrays and functions, which is necessary (e.g.
542 in disassemble_command). It also dereferences references, which
543 I suspect is the most logical thing to do. */
544 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
546 return unpack_pointer (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
549 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
550 as a long, or as a double, assuming the raw data is described
551 by type TYPE. Knows how to convert different sizes of values
552 and can convert between fixed and floating point. We don't assume
553 any alignment for the raw data. Return value is in host byte order.
555 If you want functions and arrays to be coerced to pointers, and
556 references to be dereferenced, call value_as_long() instead.
558 C++: It is assumed that the front-end has taken care of
559 all matters concerning pointers to members. A pointer
560 to member which reaches here is considered to be equivalent
561 to an INT (or some size). After all, it is only an offset. */
564 unpack_long (type
, valaddr
)
568 register enum type_code code
= TYPE_CODE (type
);
569 register int len
= TYPE_LENGTH (type
);
570 register int nosign
= TYPE_UNSIGNED (type
);
572 if (code
== TYPE_CODE_ENUM
)
573 code
= TYPE_CODE_INT
;
574 if (code
== TYPE_CODE_FLT
)
576 if (len
== sizeof (float))
579 bcopy (valaddr
, &retval
, sizeof (retval
));
580 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
584 if (len
== sizeof (double))
587 bcopy (valaddr
, &retval
, sizeof (retval
));
588 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
593 error ("Unexpected type of floating point number.");
596 else if (code
== TYPE_CODE_INT
&& nosign
)
598 if (len
== sizeof (char))
600 unsigned char retval
= * (unsigned char *) valaddr
;
601 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
605 if (len
== sizeof (short))
607 unsigned short retval
;
608 bcopy (valaddr
, &retval
, sizeof (retval
));
609 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
613 if (len
== sizeof (int))
616 bcopy (valaddr
, &retval
, sizeof (retval
));
617 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
621 if (len
== sizeof (long))
623 unsigned long retval
;
624 bcopy (valaddr
, &retval
, sizeof (retval
));
625 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
629 if (len
== sizeof (long long))
631 unsigned long long retval
;
632 bcopy (valaddr
, &retval
, sizeof (retval
));
633 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
639 error ("That operation is not possible on an integer of that size.");
642 else if (code
== TYPE_CODE_INT
)
644 if (len
== sizeof (char))
647 bcopy (valaddr
, &retval
, sizeof (retval
));
648 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
652 if (len
== sizeof (short))
655 bcopy (valaddr
, &retval
, sizeof (retval
));
656 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
660 if (len
== sizeof (int))
663 bcopy (valaddr
, &retval
, sizeof (retval
));
664 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
668 if (len
== sizeof (long))
671 bcopy (valaddr
, &retval
, sizeof (retval
));
672 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
677 if (len
== sizeof (long long))
680 bcopy (valaddr
, &retval
, sizeof (retval
));
681 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
687 error ("That operation is not possible on an integer of that size.");
691 /* There is no guarantee that a pointer can fit within a LONGEST.
692 Callers should use unpack_pointer instead. */
693 else if (code
== TYPE_CODE_PTR
694 || code
== TYPE_CODE_REF
)
696 if (len
== sizeof (CORE_ADDR
))
699 bcopy (valaddr
, &retval
, sizeof (retval
));
700 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
705 else if (code
== TYPE_CODE_MEMBER
)
706 error ("not implemented: member types in unpack_long");
709 error ("Value not integer or pointer.");
711 error ("Value not integer.");
713 return 0; /* For lint -- never reached */
716 /* Return a double value from the specified type and address.
717 INVP points to an int which is set to 0 for valid value,
718 1 for invalid value (bad float format). In either case,
719 the returned double is OK to use. Argument is in target
720 format, result is in host format. */
723 unpack_double (type
, valaddr
, invp
)
728 register enum type_code code
= TYPE_CODE (type
);
729 register int len
= TYPE_LENGTH (type
);
730 register int nosign
= TYPE_UNSIGNED (type
);
732 *invp
= 0; /* Assume valid. */
733 if (code
== TYPE_CODE_FLT
)
735 if (INVALID_FLOAT (valaddr
, len
))
738 return 1.234567891011121314;
741 if (len
== sizeof (float))
744 bcopy (valaddr
, &retval
, sizeof (retval
));
745 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
749 if (len
== sizeof (double))
752 bcopy (valaddr
, &retval
, sizeof (retval
));
753 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
758 error ("Unexpected type of floating point number.");
759 return 0; /* Placate lint. */
763 /* Unsigned -- be sure we compensate for signed LONGEST. */
765 return (unsigned long long) unpack_long (type
, valaddr
);
767 return (unsigned long ) unpack_long (type
, valaddr
);
770 /* Signed -- we are OK with unpack_long. */
771 return unpack_long (type
, valaddr
);
775 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
776 as a CORE_ADDR, assuming the raw data is described by type TYPE.
777 We don't assume any alignment for the raw data. Return value is in
780 If you want functions and arrays to be coerced to pointers, and
781 references to be dereferenced, call value_as_pointer() instead.
783 C++: It is assumed that the front-end has taken care of
784 all matters concerning pointers to members. A pointer
785 to member which reaches here is considered to be equivalent
786 to an INT (or some size). After all, it is only an offset. */
789 unpack_pointer (type
, valaddr
)
793 register enum type_code code
= TYPE_CODE (type
);
794 register int len
= TYPE_LENGTH (type
);
796 if (code
== TYPE_CODE_PTR
797 || code
== TYPE_CODE_REF
)
799 if (len
== sizeof (CORE_ADDR
))
802 bcopy (valaddr
, &retval
, sizeof (retval
));
803 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
806 error ("Unrecognized pointer size.");
808 else if (code
== TYPE_CODE_MEMBER
)
809 error ("not implemented: member types in unpack_pointer");
811 error ("Value is not a pointer.");
812 return 0; /* For lint -- never reached */
815 /* Given a value ARG1 (offset by OFFSET bytes)
816 of a struct or union type ARG_TYPE,
817 extract and return the value of one of its fields.
818 FIELDNO says which field.
820 For C++, must also be able to return values from static fields */
823 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
826 register int fieldno
;
827 register struct type
*arg_type
;
830 register struct type
*type
;
832 check_stub_type (arg_type
);
833 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
835 /* Handle packed fields */
837 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
838 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
840 v
= value_from_long (type
,
841 unpack_field_as_long (arg_type
,
842 VALUE_CONTENTS (arg1
),
844 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
845 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
849 v
= allocate_value (type
);
850 if (VALUE_LAZY (arg1
))
853 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
854 VALUE_CONTENTS_RAW (v
),
857 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
858 if (VALUE_LVAL (arg1
) == lval_internalvar
)
859 VALUE_LVAL (v
) = lval_internalvar_component
;
860 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
861 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
865 /* Given a value ARG1 of a struct or union type,
866 extract and return the value of one of its fields.
867 FIELDNO says which field.
869 For C++, must also be able to return values from static fields */
872 value_field (arg1
, fieldno
)
874 register int fieldno
;
876 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
880 value_fn_field (arg1
, fieldno
, subfieldno
)
882 register int fieldno
;
886 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
887 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
890 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
891 0, VAR_NAMESPACE
, 0, NULL
);
892 if (! sym
) error ("Internal error: could not find physical method named %s",
893 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
895 v
= allocate_value (type
);
896 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
897 VALUE_TYPE (v
) = type
;
901 /* Return a virtual function as a value.
902 ARG1 is the object which provides the virtual function
903 table pointer. ARG1 is side-effected in calling this function.
904 F is the list of member functions which contains the desired virtual
906 J is an index into F which provides the desired virtual function.
908 TYPE is the type in which F is located. */
910 value_virtual_fn_field (arg1
, f
, j
, type
)
916 /* First, get the virtual function table pointer. That comes
917 with a strange type, so cast it to type `pointer to long' (which
918 should serve just fine as a function type). Then, index into
919 the table, and convert final value to appropriate function type. */
920 value entry
, vfn
, vtbl
;
921 value vi
= value_from_long (builtin_type_int
,
922 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
923 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
924 struct type
*context
;
925 if (fcontext
== NULL
)
926 /* We don't have an fcontext (e.g. the program was compiled with
927 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
928 This won't work right for multiple inheritance, but at least we
929 should do as well as GDB 3.x did. */
930 fcontext
= TYPE_VPTR_BASETYPE (type
);
931 context
= lookup_pointer_type (fcontext
);
932 /* Now context is a pointer to the basetype containing the vtbl. */
933 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
934 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
936 context
= VALUE_TYPE (arg1
);
937 /* Now context is the basetype containing the vtbl. */
939 /* This type may have been defined before its virtual function table
940 was. If so, fill in the virtual function table entry for the
942 if (TYPE_VPTR_FIELDNO (context
) < 0)
943 fill_in_vptr_fieldno (context
);
945 /* The virtual function table is now an array of structures
946 which have the form { int16 offset, delta; void *pfn; }. */
947 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
949 /* Index into the virtual function table. This is hard-coded because
950 looking up a field is not cheap, and it may be important to save
951 time, e.g. if the user has set a conditional breakpoint calling
952 a virtual function. */
953 entry
= value_subscript (vtbl
, vi
);
955 /* Move the `this' pointer according to the virtual function table. */
956 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
957 if (! VALUE_LAZY (arg1
))
959 VALUE_LAZY (arg1
) = 1;
960 value_fetch_lazy (arg1
);
963 vfn
= value_field (entry
, 2);
964 /* Reinstantiate the function pointer with the correct type. */
965 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
970 /* ARG is a pointer to an object we know to be at least
971 a DTYPE. BTYPE is the most derived basetype that has
972 already been searched (and need not be searched again).
973 After looking at the vtables between BTYPE and DTYPE,
974 return the most derived type we find. The caller must
975 be satisfied when the return value == DTYPE.
977 FIXME-tiemann: should work with dossier entries as well. */
980 value_headof (arg
, btype
, dtype
)
982 struct type
*btype
, *dtype
;
984 /* First collect the vtables we must look at for this object. */
985 /* FIXME-tiemann: right now, just look at top-most vtable. */
986 value vtbl
, entry
, best_entry
= 0;
987 /* FIXME: entry_type is never used. */
988 struct type
*entry_type
;
990 int offset
, best_offset
= 0;
992 CORE_ADDR pc_for_sym
;
993 char *demangled_name
;
995 btype
= TYPE_VPTR_BASETYPE (dtype
);
996 check_stub_type (btype
);
998 vtbl
= value_cast (lookup_pointer_type (btype
), arg
);
1001 vtbl
= value_ind (value_field (value_ind (vtbl
), TYPE_VPTR_FIELDNO (btype
)));
1003 /* Check that VTBL looks like it points to a virtual function table. */
1004 i
= find_pc_misc_function (VALUE_ADDRESS (vtbl
));
1005 if (i
< 0 || ! VTBL_PREFIX_P (misc_function_vector
[i
].name
))
1007 /* If we expected to find a vtable, but did not, let the user
1008 know that we aren't happy, but don't throw an error.
1009 FIXME: there has to be a better way to do this. */
1010 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
1011 bcopy (VALUE_TYPE (arg
), error_type
, sizeof (struct type
));
1012 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
1013 VALUE_TYPE (arg
) = error_type
;
1017 /* Now search through the virtual function table. */
1018 entry
= value_ind (vtbl
);
1019 entry_type
= VALUE_TYPE (entry
);
1020 nelems
= longest_to_int (value_as_long (value_field (entry
, 2)));
1021 for (i
= 1; i
<= nelems
; i
++)
1023 entry
= value_subscript (vtbl
, value_from_long (builtin_type_int
, i
));
1024 offset
= longest_to_int (value_as_long (value_field (entry
, 0)));
1025 if (offset
< best_offset
)
1027 best_offset
= offset
;
1031 if (best_entry
== 0)
1034 /* Move the pointer according to BEST_ENTRY's offset, and figure
1035 out what type we should return as the new pointer. */
1036 pc_for_sym
= value_as_pointer (value_field (best_entry
, 2));
1037 sym
= find_pc_function (pc_for_sym
);
1038 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), -1);
1039 *(strchr (demangled_name
, ':')) = '\0';
1040 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
1042 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym
));
1043 free (demangled_name
);
1044 arg
= value_add (value_cast (builtin_type_int
, arg
),
1045 value_field (best_entry
, 0));
1046 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
1050 /* ARG is a pointer object of type TYPE. If TYPE has virtual
1051 function tables, probe ARG's tables (including the vtables
1052 of its baseclasses) to figure out the most derived type that ARG
1053 could actually be a pointer to. */
1056 value_from_vtable_info (arg
, type
)
1060 /* Take care of preliminaries. */
1061 if (TYPE_VPTR_FIELDNO (type
) < 0)
1062 fill_in_vptr_fieldno (type
);
1063 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
1066 return value_headof (arg
, 0, type
);
1069 /* The value of a static class member does not depend
1070 on its instance, only on its type. If FIELDNO >= 0,
1071 then fieldno is a valid field number and is used directly.
1072 Otherwise, FIELDNAME is the name of the field we are
1073 searching for. If it is not a static field name, an
1074 error is signaled. TYPE is the type in which we look for the
1075 static field member.
1077 Return zero if we couldn't find anything; the caller may signal
1078 an error in that case. */
1081 value_static_field (type
, fieldname
, fieldno
)
1082 register struct type
*type
;
1084 register int fieldno
;
1092 /* Look for static field. */
1094 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1095 if (! strcmp (TYPE_FIELD_NAME (type
, i
), fieldname
))
1097 if (TYPE_FIELD_STATIC (type
, i
))
1103 error ("field `%s' is not static", fieldname
);
1107 v
= value_static_field (TYPE_BASECLASS (type
, i
), fieldname
, -1);
1112 if (destructor_name_p (fieldname
, type
))
1113 error ("Cannot get value of destructor");
1115 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1117 if (! strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), fieldname
))
1118 error ("Cannot get value of method \"%s\"", fieldname
);
1120 error("there is no field named %s", fieldname
);
1124 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1125 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1126 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1128 type
= TYPE_FIELD_TYPE (type
, fieldno
);
1129 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1133 /* Compute the address of the baseclass which is
1134 the INDEXth baseclass of TYPE. The TYPE base
1135 of the object is at VALADDR.
1137 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1138 or 0 if no error. In that case the return value is not the address
1139 of the baseclasss, but the address which could not be read
1143 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1150 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1155 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1157 /* Must hunt for the pointer to this virtual baseclass. */
1158 register int i
, len
= TYPE_NFIELDS (type
);
1159 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1160 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
1162 if (TYPE_MAIN_VARIANT (basetype
))
1163 basetype
= TYPE_MAIN_VARIANT (basetype
);
1165 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
1166 sprintf (vbase_name
, "_vb$%s", type_name
);
1167 /* First look for the virtual baseclass pointer
1169 for (i
= n_baseclasses
; i
< len
; i
++)
1171 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
1173 value val
= allocate_value (basetype
);
1178 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1179 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1181 status
= target_read_memory (addr
,
1182 VALUE_CONTENTS_RAW (val
),
1183 TYPE_LENGTH (type
));
1184 VALUE_LVAL (val
) = lval_memory
;
1185 VALUE_ADDRESS (val
) = addr
;
1191 release_value (val
);
1195 return (char *)addr
;
1201 return (char *) VALUE_CONTENTS (val
);
1205 /* Not in the fields, so try looking through the baseclasses. */
1206 for (i
= index
+1; i
< n_baseclasses
; i
++)
1210 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
, errp
);
1220 /* Baseclass is easily computed. */
1223 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1226 /* Ugly hack to convert method stubs into method types.
1228 He ain't kiddin'. This demangles the name of the method into a string
1229 including argument types, parses out each argument type, generates
1230 a string casting a zero to that type, evaluates the string, and stuffs
1231 the resulting type into an argtype vector!!! Then it knows the type
1232 of the whole function (including argument types for overloading),
1233 which info used to be in the stab's but was removed to hack back
1234 the space required for them. */
1236 check_stub_method (type
, i
, j
)
1240 extern char *gdb_mangle_typename (), *strchr ();
1241 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1242 char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1243 char *inner_name
= gdb_mangle_typename (type
);
1244 int mangled_name_len
= (strlen (field_name
)
1245 + strlen (inner_name
)
1246 + strlen (TYPE_FN_FIELD_PHYSNAME (f
, j
))
1249 char *demangled_name
;
1250 char *argtypetext
, *p
;
1251 int depth
= 0, argcount
= 1;
1252 struct type
**argtypes
;
1254 if (OPNAME_PREFIX_P (field_name
))
1256 char *opname
= cplus_mangle_opname (field_name
+ 3);
1258 error ("No mangling for \"%s\"", field_name
);
1259 mangled_name_len
+= strlen (opname
);
1260 mangled_name
= (char *)xmalloc (mangled_name_len
);
1262 strncpy (mangled_name
, field_name
, 3);
1263 mangled_name
[3] = '\0';
1264 strcat (mangled_name
, opname
);
1268 mangled_name
= (char *)xmalloc (mangled_name_len
);
1269 strcpy (mangled_name
, TYPE_FN_FIELDLIST_NAME (type
, i
));
1271 strcat (mangled_name
, inner_name
);
1272 strcat (mangled_name
, TYPE_FN_FIELD_PHYSNAME (f
, j
));
1273 demangled_name
= cplus_demangle (mangled_name
, 0);
1275 /* Now, read in the parameters that define this type. */
1276 argtypetext
= strchr (demangled_name
, '(') + 1;
1284 else if (*p
== ',' && depth
== 0)
1289 /* We need one more slot for the void [...] or NULL [end of arglist] */
1290 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1292 argtypes
[0] = lookup_pointer_type (type
);
1295 if (*p
!= ')') /* () means no args, skip while */
1304 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1306 char *tmp
= (char *)alloca (p
- argtypetext
+ 4);
1309 bcopy (argtypetext
, tmp
+1, p
- argtypetext
);
1310 tmp
[p
-argtypetext
+1] = ')';
1311 tmp
[p
-argtypetext
+2] = '0';
1312 tmp
[p
-argtypetext
+3] = '\0';
1313 val
= parse_and_eval (tmp
);
1314 argtypes
[argcount
] = VALUE_TYPE (val
);
1316 argtypetext
= p
+ 1;
1322 if (p
[-2] != '.') /* ... */
1323 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1325 argtypes
[argcount
] = NULL
; /* List terminator */
1327 free (demangled_name
);
1329 type
= lookup_method_type (type
, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)), argtypes
);
1330 /* Free the stub type...it's no longer needed. */
1331 free (TYPE_FN_FIELD_TYPE (f
, j
));
1332 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1333 TYPE_FN_FIELD_TYPE (f
, j
) = type
;
1337 unpack_field_as_long (type
, valaddr
, fieldno
)
1343 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1344 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1346 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1347 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1349 /* Extracting bits depends on endianness of the machine. */
1351 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1353 val
= val
>> (bitpos
% 8);
1356 val
&= (1 << bitsize
) - 1;
1360 /* Modify the value of a bitfield. ADDR points to a block of memory in
1361 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1362 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1363 indicate which bits (in target bit order) comprise the bitfield. */
1366 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1369 int bitpos
, bitsize
;
1373 /* Reject values too big to fit in the field in question.
1374 Otherwise adjoining fields may be corrupted. */
1375 if (fieldval
& ~((1<<bitsize
)-1))
1376 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1378 bcopy (addr
, &oword
, sizeof oword
);
1379 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1381 /* Shifting for bit field depends on endianness of the target machine. */
1383 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1386 oword
&= ~(((1 << bitsize
) - 1) << bitpos
);
1387 oword
|= fieldval
<< bitpos
;
1389 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1390 bcopy (&oword
, addr
, sizeof oword
);
1393 /* Convert C numbers into newly allocated values */
1396 value_from_long (type
, num
)
1398 register LONGEST num
;
1400 register value val
= allocate_value (type
);
1401 register enum type_code code
= TYPE_CODE (type
);
1402 register int len
= TYPE_LENGTH (type
);
1404 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
)
1406 if (len
== sizeof (char))
1407 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1408 else if (len
== sizeof (short))
1409 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1410 else if (len
== sizeof (int))
1411 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1412 else if (len
== sizeof (long))
1413 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1415 else if (len
== sizeof (long long))
1416 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1419 error ("Integer type encountered with unexpected data length.");
1422 error ("Unexpected type encountered for integer constant.");
1424 /* num was in host byte order. So now put the value's contents
1425 into target byte order. */
1426 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1432 value_from_double (type
, num
)
1436 register value val
= allocate_value (type
);
1437 register enum type_code code
= TYPE_CODE (type
);
1438 register int len
= TYPE_LENGTH (type
);
1440 if (code
== TYPE_CODE_FLT
)
1442 if (len
== sizeof (float))
1443 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1444 else if (len
== sizeof (double))
1445 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1447 error ("Floating type encountered with unexpected data length.");
1450 error ("Unexpected type encountered for floating constant.");
1452 /* num was in host byte order. So now put the value's contents
1453 into target byte order. */
1454 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1459 /* Deal with the value that is "about to be returned". */
1461 /* Return the value that a function returning now
1462 would be returning to its caller, assuming its type is VALTYPE.
1463 RETBUF is where we look for what ought to be the contents
1464 of the registers (in raw form). This is because it is often
1465 desirable to restore old values to those registers
1466 after saving the contents of interest, and then call
1467 this function using the saved values.
1468 struct_return is non-zero when the function in question is
1469 using the structure return conventions on the machine in question;
1470 0 when it is using the value returning conventions (this often
1471 means returning pointer to where structure is vs. returning value). */
1474 value_being_returned (valtype
, retbuf
, struct_return
)
1475 register struct type
*valtype
;
1476 char retbuf
[REGISTER_BYTES
];
1483 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1484 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1485 if (struct_return
) {
1486 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1488 error ("Function return value unknown");
1489 return value_at (valtype
, addr
);
1493 val
= allocate_value (valtype
);
1494 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1499 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1500 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1501 and TYPE is the type (which is known to be struct, union or array).
1503 On most machines, the struct convention is used unless we are
1504 using gcc and the type is of a special size. */
1505 #if !defined (USE_STRUCT_CONVENTION)
1506 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1507 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1508 || TYPE_LENGTH (value_type) == 2 \
1509 || TYPE_LENGTH (value_type) == 4 \
1510 || TYPE_LENGTH (value_type) == 8 \
1515 /* Return true if the function specified is using the structure returning
1516 convention on this machine to return arguments, or 0 if it is using
1517 the value returning convention. FUNCTION is the value representing
1518 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1519 is the type returned by the function. GCC_P is nonzero if compiled
1523 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1526 struct type
*value_type
;
1530 register enum type_code code
= TYPE_CODE (value_type
);
1532 if (code
== TYPE_CODE_ERROR
)
1533 error ("Function return type unknown.");
1535 if (code
== TYPE_CODE_STRUCT
||
1536 code
== TYPE_CODE_UNION
||
1537 code
== TYPE_CODE_ARRAY
)
1538 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1543 /* Store VAL so it will be returned if a function returns now.
1544 Does not verify that VAL's type matches what the current
1545 function wants to return. */
1548 set_return_value (val
)
1551 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1555 if (code
== TYPE_CODE_ERROR
)
1556 error ("Function return type unknown.");
1558 if (code
== TYPE_CODE_STRUCT
1559 || code
== TYPE_CODE_UNION
)
1560 error ("Specifying a struct or union return value is not supported.");
1562 /* FIXME, this is bogus. We don't know what the return conventions
1563 are, or how values should be promoted.... */
1564 if (code
== TYPE_CODE_FLT
)
1566 dbuf
= value_as_double (val
);
1568 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1572 lbuf
= value_as_long (val
);
1573 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1578 _initialize_values ()
1580 add_cmd ("convenience", no_class
, show_convenience
,
1581 "Debugger convenience (\"$foo\") variables.\n\
1582 These variables are created when you assign them values;\n\
1583 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1584 A few convenience variables are given values automatically:\n\
1585 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1586 \"$__\" holds the contents of the last address examined with \"x\".",
1589 add_cmd ("values", no_class
, show_values
,
1590 "Elements of value history around item number IDX (or last ten).",