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 */
55 /* List of all value objects currently allocated
56 (except for those released by calls to release_value)
57 This is so they can be freed after each command. */
59 static value all_values
;
61 /* Allocate a value that has the correct length for type TYPE. */
69 check_stub_type (type
);
71 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
72 VALUE_NEXT (val
) = all_values
;
74 VALUE_TYPE (val
) = type
;
75 VALUE_LVAL (val
) = not_lval
;
76 VALUE_ADDRESS (val
) = 0;
77 VALUE_FRAME (val
) = 0;
78 VALUE_OFFSET (val
) = 0;
79 VALUE_BITPOS (val
) = 0;
80 VALUE_BITSIZE (val
) = 0;
81 VALUE_REPEATED (val
) = 0;
82 VALUE_REPETITIONS (val
) = 0;
83 VALUE_REGNO (val
) = -1;
85 VALUE_OPTIMIZED_OUT (val
) = 0;
89 /* Allocate a value that has the correct length
90 for COUNT repetitions type TYPE. */
93 allocate_repeat_value (type
, count
)
99 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
100 VALUE_NEXT (val
) = all_values
;
102 VALUE_TYPE (val
) = type
;
103 VALUE_LVAL (val
) = not_lval
;
104 VALUE_ADDRESS (val
) = 0;
105 VALUE_FRAME (val
) = 0;
106 VALUE_OFFSET (val
) = 0;
107 VALUE_BITPOS (val
) = 0;
108 VALUE_BITSIZE (val
) = 0;
109 VALUE_REPEATED (val
) = 1;
110 VALUE_REPETITIONS (val
) = count
;
111 VALUE_REGNO (val
) = -1;
112 VALUE_LAZY (val
) = 0;
113 VALUE_OPTIMIZED_OUT (val
) = 0;
117 /* Return a mark in the value chain. All values allocated after the
118 mark is obtained (except for those released) are subject to being freed
119 if a subsequent value_free_to_mark is passed the mark. */
126 /* Free all values allocated since MARK was obtained by value_mark
127 (except for those released). */
129 value_free_to_mark (mark
)
134 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
136 next
= VALUE_NEXT (val
);
142 /* Free all the values that have been allocated (except for those released).
143 Called after each command, successful or not. */
148 register value val
, next
;
150 for (val
= all_values
; val
; val
= next
)
152 next
= VALUE_NEXT (val
);
159 /* Remove VAL from the chain all_values
160 so it will not be freed automatically. */
168 if (all_values
== val
)
170 all_values
= val
->next
;
174 for (v
= all_values
; v
; v
= v
->next
)
184 /* Return a copy of the value ARG.
185 It contains the same contents, for same memory address,
186 but it's a different block of storage. */
193 register struct type
*type
= VALUE_TYPE (arg
);
194 if (VALUE_REPEATED (arg
))
195 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
197 val
= allocate_value (type
);
198 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
199 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
200 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
201 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
202 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
203 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
204 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
205 if (!VALUE_LAZY (val
))
207 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
208 TYPE_LENGTH (VALUE_TYPE (arg
))
209 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
214 /* Access to the value history. */
216 /* Record a new value in the value history.
217 Returns the absolute history index of the entry.
218 Result of -1 indicates the value was not saved; otherwise it is the
219 value history index of this new item. */
222 record_latest_value (val
)
227 /* Check error now if about to store an invalid float. We return -1
228 to the caller, but allow them to continue, e.g. to print it as "Nan". */
229 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
230 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
231 if (i
) return -1; /* Indicate value not saved in history */
234 /* Here we treat value_history_count as origin-zero
235 and applying to the value being stored now. */
237 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
240 register struct value_history_chunk
*new
241 = (struct value_history_chunk
*)
242 xmalloc (sizeof (struct value_history_chunk
));
243 bzero (new->values
, sizeof new->values
);
244 new->next
= value_history_chain
;
245 value_history_chain
= new;
248 value_history_chain
->values
[i
] = val
;
251 /* Now we regard value_history_count as origin-one
252 and applying to the value just stored. */
254 return ++value_history_count
;
257 /* Return a copy of the value in the history with sequence number NUM. */
260 access_value_history (num
)
263 register struct value_history_chunk
*chunk
;
265 register int absnum
= num
;
268 absnum
+= value_history_count
;
273 error ("The history is empty.");
275 error ("There is only one value in the history.");
277 error ("History does not go back to $$%d.", -num
);
279 if (absnum
> value_history_count
)
280 error ("History has not yet reached $%d.", absnum
);
284 /* Now absnum is always absolute and origin zero. */
286 chunk
= value_history_chain
;
287 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
291 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
294 /* Clear the value history entirely.
295 Must be done when new symbol tables are loaded,
296 because the type pointers become invalid. */
299 clear_value_history ()
301 register struct value_history_chunk
*next
;
305 while (value_history_chain
)
307 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
308 if (val
= value_history_chain
->values
[i
])
310 next
= value_history_chain
->next
;
311 free (value_history_chain
);
312 value_history_chain
= next
;
314 value_history_count
= 0;
318 show_values (num_exp
, from_tty
)
328 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
329 /* "info history +" should print from the stored position. */
332 /* "info history <exp>" should print around value number <exp>. */
333 num
= parse_and_eval_address (num_exp
) - 5;
337 /* "info history" means print the last 10 values. */
338 num
= value_history_count
- 9;
344 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
346 val
= access_value_history (i
);
347 printf_filtered ("$%d = ", i
);
348 value_print (val
, stdout
, 0, Val_pretty_default
);
349 printf_filtered ("\n");
352 /* The next "info history +" should start after what we just printed. */
355 /* Hitting just return after this command should do the same thing as
356 "info history +". If num_exp is null, this is unnecessary, since
357 "info history +" is not useful after "info history". */
358 if (from_tty
&& num_exp
)
365 /* Internal variables. These are variables within the debugger
366 that hold values assigned by debugger commands.
367 The user refers to them with a '$' prefix
368 that does not appear in the variable names stored internally. */
370 static struct internalvar
*internalvars
;
372 /* Look up an internal variable with name NAME. NAME should not
373 normally include a dollar sign.
375 If the specified internal variable does not exist,
376 one is created, with a void value. */
379 lookup_internalvar (name
)
382 register struct internalvar
*var
;
384 for (var
= internalvars
; var
; var
= var
->next
)
385 if (!strcmp (var
->name
, name
))
388 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
389 var
->name
= concat (name
, "", "");
390 var
->value
= allocate_value (builtin_type_void
);
391 release_value (var
->value
);
392 var
->next
= internalvars
;
398 value_of_internalvar (var
)
399 struct internalvar
*var
;
403 #ifdef IS_TRAPPED_INTERNALVAR
404 if (IS_TRAPPED_INTERNALVAR (var
->name
))
405 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
408 val
= value_copy (var
->value
);
409 if (VALUE_LAZY (val
))
410 value_fetch_lazy (val
);
411 VALUE_LVAL (val
) = lval_internalvar
;
412 VALUE_INTERNALVAR (val
) = var
;
417 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
418 struct internalvar
*var
;
419 int offset
, bitpos
, bitsize
;
422 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
424 #ifdef IS_TRAPPED_INTERNALVAR
425 if (IS_TRAPPED_INTERNALVAR (var
->name
))
426 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
430 modify_field (addr
, (int) value_as_long (newval
),
433 bcopy (VALUE_CONTENTS (newval
), addr
,
434 TYPE_LENGTH (VALUE_TYPE (newval
)));
438 set_internalvar (var
, val
)
439 struct internalvar
*var
;
442 #ifdef IS_TRAPPED_INTERNALVAR
443 if (IS_TRAPPED_INTERNALVAR (var
->name
))
444 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
448 var
->value
= value_copy (val
);
449 release_value (var
->value
);
453 internalvar_name (var
)
454 struct internalvar
*var
;
459 /* Free all internalvars. Done when new symtabs are loaded,
460 because that makes the values invalid. */
463 clear_internalvars ()
465 register struct internalvar
*var
;
470 internalvars
= var
->next
;
480 register struct internalvar
*var
;
483 for (var
= internalvars
; var
; var
= var
->next
)
485 #ifdef IS_TRAPPED_INTERNALVAR
486 if (IS_TRAPPED_INTERNALVAR (var
->name
))
493 printf ("Debugger convenience variables:\n\n");
497 printf ("$%s = ", var
->name
);
498 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
502 printf ("No debugger convenience variables now defined.\n\
503 Convenience variables have names starting with \"$\";\n\
504 use \"set\" as in \"set $foo = 5\" to define them.\n");
507 /* Extract a value as a C number (either long or double).
508 Knows how to convert fixed values to double, or
509 floating values to long.
510 Does not deallocate the value. */
516 /* This coerces arrays and functions, which is necessary (e.g.
517 in disassemble_command). It also dereferences references, which
518 I suspect is the most logical thing to do. */
519 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
521 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
525 value_as_double (val
)
531 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
533 error ("Invalid floating value found in program.");
537 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
538 as a long, or as a double, assuming the raw data is described
539 by type TYPE. Knows how to convert different sizes of values
540 and can convert between fixed and floating point. We don't assume
541 any alignment for the raw data. Return value is in host byte order.
543 If you want functions and arrays to be coerced to pointers, and
544 references to be dereferenced, call value_as_long() instead.
546 C++: It is assumed that the front-end has taken care of
547 all matters concerning pointers to members. A pointer
548 to member which reaches here is considered to be equivalent
549 to an INT (or some size). After all, it is only an offset. */
552 unpack_long (type
, valaddr
)
556 register enum type_code code
= TYPE_CODE (type
);
557 register int len
= TYPE_LENGTH (type
);
558 register int nosign
= TYPE_UNSIGNED (type
);
560 if (code
== TYPE_CODE_ENUM
)
561 code
= TYPE_CODE_INT
;
562 if (code
== TYPE_CODE_FLT
)
564 if (len
== sizeof (float))
567 bcopy (valaddr
, &retval
, sizeof (retval
));
568 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
572 if (len
== sizeof (double))
575 bcopy (valaddr
, &retval
, sizeof (retval
));
576 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
581 error ("Unexpected type of floating point number.");
584 else if (code
== TYPE_CODE_INT
&& nosign
)
586 if (len
== sizeof (char))
588 unsigned char retval
= * (unsigned char *) valaddr
;
589 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
593 if (len
== sizeof (short))
595 unsigned short retval
;
596 bcopy (valaddr
, &retval
, sizeof (retval
));
597 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
601 if (len
== sizeof (int))
604 bcopy (valaddr
, &retval
, sizeof (retval
));
605 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
609 if (len
== sizeof (long))
611 unsigned long retval
;
612 bcopy (valaddr
, &retval
, sizeof (retval
));
613 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
617 if (len
== sizeof (long long))
619 unsigned long long retval
;
620 bcopy (valaddr
, &retval
, sizeof (retval
));
621 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
627 error ("That operation is not possible on an integer of that size.");
630 else if (code
== TYPE_CODE_INT
)
632 if (len
== sizeof (char))
635 bcopy (valaddr
, &retval
, sizeof (retval
));
636 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
640 if (len
== sizeof (short))
643 bcopy (valaddr
, &retval
, sizeof (retval
));
644 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
648 if (len
== sizeof (int))
651 bcopy (valaddr
, &retval
, sizeof (retval
));
652 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
656 if (len
== sizeof (long))
659 bcopy (valaddr
, &retval
, sizeof (retval
));
660 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
665 if (len
== sizeof (long long))
668 bcopy (valaddr
, &retval
, sizeof (retval
));
669 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
675 error ("That operation is not possible on an integer of that size.");
678 else if (code
== TYPE_CODE_PTR
679 || code
== TYPE_CODE_REF
)
681 if (len
== sizeof (char *))
684 bcopy (valaddr
, &retval
, sizeof (retval
));
685 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
689 else if (code
== TYPE_CODE_MEMBER
)
690 error ("not implemented: member types in unpack_long");
692 error ("Value not integer or pointer.");
693 return 0; /* For lint -- never reached */
696 /* Return a double value from the specified type and address.
697 INVP points to an int which is set to 0 for valid value,
698 1 for invalid value (bad float format). In either case,
699 the returned double is OK to use. Argument is in target
700 format, result is in host format. */
703 unpack_double (type
, valaddr
, invp
)
708 register enum type_code code
= TYPE_CODE (type
);
709 register int len
= TYPE_LENGTH (type
);
710 register int nosign
= TYPE_UNSIGNED (type
);
712 *invp
= 0; /* Assume valid. */
713 if (code
== TYPE_CODE_FLT
)
715 if (INVALID_FLOAT (valaddr
, len
))
718 return 1.234567891011121314;
721 if (len
== sizeof (float))
724 bcopy (valaddr
, &retval
, sizeof (retval
));
725 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
729 if (len
== sizeof (double))
732 bcopy (valaddr
, &retval
, sizeof (retval
));
733 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
738 error ("Unexpected type of floating point number.");
742 /* Unsigned -- be sure we compensate for signed LONGEST. */
744 return (unsigned long long) unpack_long (type
, valaddr
);
746 return (unsigned long ) unpack_long (type
, valaddr
);
749 /* Signed -- we are OK with unpack_long. */
750 return unpack_long (type
, valaddr
);
754 /* Given a value ARG1 (offset by OFFSET bytes)
755 of a struct or union type ARG_TYPE,
756 extract and return the value of one of its fields.
757 FIELDNO says which field.
759 For C++, must also be able to return values from static fields */
762 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
765 register int fieldno
;
766 register struct type
*arg_type
;
769 register struct type
*type
;
771 check_stub_type (arg_type
);
772 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
774 /* Handle packed fields */
776 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
777 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
779 v
= value_from_long (type
,
780 unpack_field_as_long (arg_type
,
781 VALUE_CONTENTS (arg1
),
783 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
784 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
788 v
= allocate_value (type
);
789 if (VALUE_LAZY (arg1
))
792 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
793 VALUE_CONTENTS_RAW (v
),
796 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
797 if (VALUE_LVAL (arg1
) == lval_internalvar
)
798 VALUE_LVAL (v
) = lval_internalvar_component
;
799 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
800 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
804 /* Given a value ARG1 of a struct or union type,
805 extract and return the value of one of its fields.
806 FIELDNO says which field.
808 For C++, must also be able to return values from static fields */
811 value_field (arg1
, fieldno
)
813 register int fieldno
;
815 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
819 value_fn_field (arg1
, fieldno
, subfieldno
)
821 register int fieldno
;
825 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
826 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
829 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
830 0, VAR_NAMESPACE
, 0, NULL
);
831 if (! sym
) error ("Internal error: could not find physical method named %s",
832 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
834 v
= allocate_value (type
);
835 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
836 VALUE_TYPE (v
) = type
;
840 /* Return a virtual function as a value.
841 ARG1 is the object which provides the virtual function
842 table pointer. ARG1 is side-effected in calling this function.
843 F is the list of member functions which contains the desired virtual
845 J is an index into F which provides the desired virtual function.
847 TYPE is the type in which F is located. */
849 value_virtual_fn_field (arg1
, f
, j
, type
)
855 /* First, get the virtual function table pointer. That comes
856 with a strange type, so cast it to type `pointer to long' (which
857 should serve just fine as a function type). Then, index into
858 the table, and convert final value to appropriate function type. */
859 value entry
, vfn
, vtbl
;
860 value vi
= value_from_long (builtin_type_int
,
861 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
862 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
863 struct type
*context
;
864 if (fcontext
== NULL
)
865 /* We don't have an fcontext (e.g. the program was compiled with
866 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
867 This won't work right for multiple inheritance, but at least we
868 should do as well as GDB 3.x did. */
869 fcontext
= TYPE_VPTR_BASETYPE (type
);
870 context
= lookup_pointer_type (fcontext
);
871 /* Now context is a pointer to the basetype containing the vtbl. */
872 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
873 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
875 context
= VALUE_TYPE (arg1
);
876 /* Now context is the basetype containing the vtbl. */
878 /* This type may have been defined before its virtual function table
879 was. If so, fill in the virtual function table entry for the
881 if (TYPE_VPTR_FIELDNO (context
) < 0)
882 fill_in_vptr_fieldno (context
);
884 /* The virtual function table is now an array of structures
885 which have the form { int16 offset, delta; void *pfn; }. */
886 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
888 /* Index into the virtual function table. This is hard-coded because
889 looking up a field is not cheap, and it may be important to save
890 time, e.g. if the user has set a conditional breakpoint calling
891 a virtual function. */
892 entry
= value_subscript (vtbl
, vi
);
894 /* Move the `this' pointer according to the virtual function table. */
895 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
896 if (! VALUE_LAZY (arg1
))
898 VALUE_LAZY (arg1
) = 1;
899 value_fetch_lazy (arg1
);
902 vfn
= value_field (entry
, 2);
903 /* Reinstantiate the function pointer with the correct type. */
904 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
909 /* ARG is a pointer to an object we know to be at least
910 a DTYPE. BTYPE is the most derived basetype that has
911 already been searched (and need not be searched again).
912 After looking at the vtables between BTYPE and DTYPE,
913 return the most derived type we find. The caller must
914 be satisfied when the return value == DTYPE.
916 FIXME-tiemann: should work with dossier entries as well. */
919 value_headof (arg
, btype
, dtype
)
921 struct type
*btype
, *dtype
;
923 /* First collect the vtables we must look at for this object. */
924 /* FIXME-tiemann: right now, just look at top-most vtable. */
925 value vtbl
, entry
, best_entry
= 0;
926 struct type
*entry_type
;
928 int offset
, best_offset
= 0;
930 CORE_ADDR pc_for_sym
;
931 char *demangled_name
;
933 vtbl
= value_ind (value_field (value_ind (arg
), TYPE_VPTR_FIELDNO (dtype
)));
935 /* Check that VTBL looks like it points to a virtual function table. */
936 i
= find_pc_misc_function (VALUE_ADDRESS (vtbl
));
937 if (i
< 0 || ! VTBL_PREFIX_P (misc_function_vector
[i
].name
))
939 /* If we expected to find a vtable, but did not, let the user
940 know that we aren't happy, but don't throw an error.
941 FIXME: there has to be a better way to do this. */
942 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
943 bcopy (VALUE_TYPE (arg
), error_type
, sizeof (struct type
));
944 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
945 VALUE_TYPE (arg
) = error_type
;
949 /* Now search through the virtual function table. */
950 entry
= value_ind (vtbl
);
951 entry_type
= VALUE_TYPE (entry
);
952 nelems
= value_as_long (value_field (entry
, 2));
953 for (i
= 1; i
<= nelems
; i
++)
955 entry
= value_subscript (vtbl
, value_from_long (builtin_type_int
, i
));
956 offset
= value_as_long (value_field (entry
, 0));
957 if (offset
< best_offset
)
959 best_offset
= offset
;
966 /* Move the pointer according to BEST_ENTRY's offset, and figure
967 out what type we should return as the new pointer. */
968 pc_for_sym
= value_as_long (value_field (best_entry
, 2));
969 sym
= find_pc_function (pc_for_sym
);
970 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), -1);
971 *(strchr (demangled_name
, ':')) = '\0';
972 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
974 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym
));
975 free (demangled_name
);
976 arg
= value_add (value_cast (builtin_type_int
, arg
),
977 value_field (best_entry
, 0));
978 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
982 /* ARG is a pointer object of type TYPE. If TYPE has virtual
983 function tables, probe ARG's tables (including the vtables
984 of its baseclasses) to figure out the most derived type that ARG
985 could actually be a pointer to. */
988 value_from_vtable_info (arg
, type
)
992 /* Take care of preliminaries. */
993 if (TYPE_VPTR_FIELDNO (type
) < 0)
994 fill_in_vptr_fieldno (type
);
995 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
998 return value_headof (arg
, 0, type
);
1001 /* The value of a static class member does not depend
1002 on its instance, only on its type. If FIELDNO >= 0,
1003 then fieldno is a valid field number and is used directly.
1004 Otherwise, FIELDNAME is the name of the field we are
1005 searching for. If it is not a static field name, an
1006 error is signaled. TYPE is the type in which we look for the
1007 static field member.
1009 Return zero if we couldn't find anything; the caller may signal
1010 an error in that case. */
1013 value_static_field (type
, fieldname
, fieldno
)
1014 register struct type
*type
;
1016 register int fieldno
;
1024 register struct type
*t
= type
;
1025 /* Look for static field. */
1027 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1028 if (! strcmp (TYPE_FIELD_NAME (type
, i
), fieldname
))
1030 if (TYPE_FIELD_STATIC (type
, i
))
1036 error ("field `%s' is not static", fieldname
);
1040 v
= value_static_field (TYPE_BASECLASS (type
, i
), fieldname
, -1);
1045 if (destructor_name_p (fieldname
, type
))
1046 error ("Cannot get value of destructor");
1048 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1050 if (! strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), fieldname
))
1051 error ("Cannot get value of method \"%s\"", fieldname
);
1053 error("there is no field named %s", fieldname
);
1057 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1058 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1059 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1061 type
= TYPE_FIELD_TYPE (type
, fieldno
);
1062 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1066 /* Compute the address of the baseclass which is
1067 the INDEXth baseclass of TYPE. The TYPE base
1068 of the object is at VALADDR.
1070 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1071 or 0 if no error. In that case the return value is not the address
1072 of the baseclasss, but the address which could not be read
1076 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1083 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1088 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1090 /* Must hunt for the pointer to this virtual baseclass. */
1091 register int i
, len
= TYPE_NFIELDS (type
);
1092 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1093 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
1095 if (TYPE_MAIN_VARIANT (basetype
))
1096 basetype
= TYPE_MAIN_VARIANT (basetype
);
1098 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
1099 sprintf (vbase_name
, "_vb$%s", type_name
);
1100 /* First look for the virtual baseclass pointer
1102 for (i
= n_baseclasses
; i
< len
; i
++)
1104 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
1106 value val
= allocate_value (basetype
);
1110 addr
= unpack_long (TYPE_FIELD_TYPE (type
, i
),
1111 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1113 status
= target_read_memory (addr
,
1114 VALUE_CONTENTS_RAW (val
),
1115 TYPE_LENGTH (type
));
1116 VALUE_LVAL (val
) = lval_memory
;
1117 VALUE_ADDRESS (val
) = addr
;
1123 release_value (val
);
1127 return (char *)addr
;
1133 return (char *) VALUE_CONTENTS (val
);
1137 /* Not in the fields, so try looking through the baseclasses. */
1138 for (i
= index
+1; i
< n_baseclasses
; i
++)
1142 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
);
1152 /* Baseclass is easily computed. */
1155 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1158 /* Ugly hack to convert method stubs into method types.
1160 He ain't kiddin'. This demangles the name of the method into a string
1161 including argument types, parses out each argument type, generates
1162 a string casting a zero to that type, evaluates the string, and stuffs
1163 the resulting type into an argtype vector!!! Then it knows the type
1164 of the whole function (including argument types for overloading),
1165 which info used to be in the stab's but was removed to hack back
1166 the space required for them. */
1168 check_stub_method (type
, i
, j
)
1172 extern char *gdb_mangle_typename (), *strchr ();
1173 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1174 char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1175 char *inner_name
= gdb_mangle_typename (type
);
1176 int mangled_name_len
= (strlen (field_name
)
1177 + strlen (inner_name
)
1178 + strlen (TYPE_FN_FIELD_PHYSNAME (f
, j
))
1181 char *demangled_name
;
1182 char *argtypetext
, *p
;
1183 int depth
= 0, argcount
= 1;
1184 struct type
**argtypes
;
1186 if (OPNAME_PREFIX_P (field_name
))
1188 char *opname
= cplus_mangle_opname (field_name
+ 3);
1189 mangled_name_len
+= strlen (opname
);
1190 mangled_name
= (char *)xmalloc (mangled_name_len
);
1192 strncpy (mangled_name
, field_name
, 3);
1193 mangled_name
[3] = '\0';
1194 strcat (mangled_name
, opname
);
1198 mangled_name
= (char *)xmalloc (mangled_name_len
);
1199 strcpy (mangled_name
, TYPE_FN_FIELDLIST_NAME (type
, i
));
1201 strcat (mangled_name
, inner_name
);
1202 strcat (mangled_name
, TYPE_FN_FIELD_PHYSNAME (f
, j
));
1203 demangled_name
= cplus_demangle (mangled_name
, 0);
1205 /* Now, read in the parameters that define this type. */
1206 argtypetext
= strchr (demangled_name
, '(') + 1;
1214 else if (*p
== ',' && depth
== 0)
1219 /* We need one more slot for the void [...] or NULL [end of arglist] */
1220 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1222 argtypes
[0] = lookup_pointer_type (type
);
1225 if (*p
!= ')') /* () means no args, skip while */
1234 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1236 char *tmp
= (char *)alloca (p
- argtypetext
+ 4);
1239 bcopy (argtypetext
, tmp
+1, p
- argtypetext
);
1240 tmp
[p
-argtypetext
+1] = ')';
1241 tmp
[p
-argtypetext
+2] = '0';
1242 tmp
[p
-argtypetext
+3] = '\0';
1243 val
= parse_and_eval (tmp
);
1244 argtypes
[argcount
] = VALUE_TYPE (val
);
1246 argtypetext
= p
+ 1;
1252 if (p
[-2] != '.') /* ... */
1253 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1255 argtypes
[argcount
] = NULL
; /* List terminator */
1257 free (demangled_name
);
1259 type
= lookup_method_type (type
, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)), argtypes
);
1260 /* Free the stub type...it's no longer needed. */
1261 free (TYPE_FN_FIELD_TYPE (f
, j
));
1262 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1263 TYPE_FN_FIELD_TYPE (f
, j
) = type
;
1267 unpack_field_as_long (type
, valaddr
, fieldno
)
1273 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1274 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1276 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1277 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1279 /* Extracting bits depends on endianness of the machine. */
1280 #ifdef BITS_BIG_ENDIAN
1281 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1283 val
= val
>> (bitpos
% 8);
1286 val
&= (1 << bitsize
) - 1;
1290 /* Modify the value of a bitfield. ADDR points to a block of memory in
1291 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1292 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1293 indicate which bits (in target bit order) comprise the bitfield. */
1296 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1299 int bitpos
, bitsize
;
1303 /* Reject values too big to fit in the field in question.
1304 Otherwise adjoining fields may be corrupted. */
1305 if (fieldval
& ~((1<<bitsize
)-1))
1306 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1308 bcopy (addr
, &oword
, sizeof oword
);
1309 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1311 /* Shifting for bit field depends on endianness of the target machine. */
1312 #ifdef BITS_BIG_ENDIAN
1313 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1316 oword
&= ~(((1 << bitsize
) - 1) << bitpos
);
1317 oword
|= fieldval
<< bitpos
;
1319 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1320 bcopy (&oword
, addr
, sizeof oword
);
1323 /* Convert C numbers into newly allocated values */
1326 value_from_long (type
, num
)
1328 register LONGEST num
;
1330 register value val
= allocate_value (type
);
1331 register enum type_code code
= TYPE_CODE (type
);
1332 register int len
= TYPE_LENGTH (type
);
1334 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
)
1336 if (len
== sizeof (char))
1337 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1338 else if (len
== sizeof (short))
1339 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1340 else if (len
== sizeof (int))
1341 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1342 else if (len
== sizeof (long))
1343 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1345 else if (len
== sizeof (long long))
1346 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1349 error ("Integer type encountered with unexpected data length.");
1352 error ("Unexpected type encountered for integer constant.");
1354 /* num was in host byte order. So now put the value's contents
1355 into target byte order. */
1356 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1362 value_from_double (type
, num
)
1366 register value val
= allocate_value (type
);
1367 register enum type_code code
= TYPE_CODE (type
);
1368 register int len
= TYPE_LENGTH (type
);
1370 if (code
== TYPE_CODE_FLT
)
1372 if (len
== sizeof (float))
1373 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1374 else if (len
== sizeof (double))
1375 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1377 error ("Floating type encountered with unexpected data length.");
1380 error ("Unexpected type encountered for floating constant.");
1382 /* num was in host byte order. So now put the value's contents
1383 into target byte order. */
1384 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1389 /* Deal with the value that is "about to be returned". */
1391 /* Return the value that a function returning now
1392 would be returning to its caller, assuming its type is VALTYPE.
1393 RETBUF is where we look for what ought to be the contents
1394 of the registers (in raw form). This is because it is often
1395 desirable to restore old values to those registers
1396 after saving the contents of interest, and then call
1397 this function using the saved values.
1398 struct_return is non-zero when the function in question is
1399 using the structure return conventions on the machine in question;
1400 0 when it is using the value returning conventions (this often
1401 means returning pointer to where structure is vs. returning value). */
1404 value_being_returned (valtype
, retbuf
, struct_return
)
1405 register struct type
*valtype
;
1406 char retbuf
[REGISTER_BYTES
];
1413 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1414 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1415 if (struct_return
) {
1416 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1418 error ("Function return value unknown");
1419 return value_at (valtype
, addr
);
1423 val
= allocate_value (valtype
);
1424 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1429 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1430 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1431 and TYPE is the type (which is known to be struct, union or array).
1433 On most machines, the struct convention is used unless we are
1434 using gcc and the type is of a special size. */
1435 #if !defined (USE_STRUCT_CONVENTION)
1436 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1437 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1438 || TYPE_LENGTH (value_type) == 2 \
1439 || TYPE_LENGTH (value_type) == 4 \
1440 || TYPE_LENGTH (value_type) == 8 \
1445 /* Return true if the function specified is using the structure returning
1446 convention on this machine to return arguments, or 0 if it is using
1447 the value returning convention. FUNCTION is the value representing
1448 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1449 is the type returned by the function. GCC_P is nonzero if compiled
1453 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1456 struct type
*value_type
;
1460 register enum type_code code
= TYPE_CODE (value_type
);
1462 if (code
== TYPE_CODE_ERROR
)
1463 error ("Function return type unknown.");
1465 if (code
== TYPE_CODE_STRUCT
||
1466 code
== TYPE_CODE_UNION
||
1467 code
== TYPE_CODE_ARRAY
)
1468 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1473 /* Store VAL so it will be returned if a function returns now.
1474 Does not verify that VAL's type matches what the current
1475 function wants to return. */
1478 set_return_value (val
)
1481 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1485 if (code
== TYPE_CODE_ERROR
)
1486 error ("Function return type unknown.");
1488 if (code
== TYPE_CODE_STRUCT
1489 || code
== TYPE_CODE_UNION
)
1490 error ("Specifying a struct or union return value is not supported.");
1492 /* FIXME, this is bogus. We don't know what the return conventions
1493 are, or how values should be promoted.... */
1494 if (code
== TYPE_CODE_FLT
)
1496 dbuf
= value_as_double (val
);
1498 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1502 lbuf
= value_as_long (val
);
1503 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1508 _initialize_values ()
1510 add_cmd ("convenience", no_class
, show_convenience
,
1511 "Debugger convenience (\"$foo\") variables.\n\
1512 These variables are created when you assign them values;\n\
1513 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1514 A few convenience variables are given values automatically:\n\
1515 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1516 \"$__\" holds the contents of the last address examined with \"x\".",
1519 add_cmd ("values", no_class
, show_values
,
1520 "Elements of value history around item number IDX (or last ten).",