1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30 extern char *cplus_demangle ();
32 /* The value-history records all the values printed
33 by print commands during this session. Each chunk
34 records 60 consecutive values. The first chunk on
35 the chain records the most recent values.
36 The total number of values is in value_history_count. */
38 #define VALUE_HISTORY_CHUNK 60
40 struct value_history_chunk
42 struct value_history_chunk
*next
;
43 value values
[VALUE_HISTORY_CHUNK
];
46 /* Chain of chunks now in use. */
48 static struct value_history_chunk
*value_history_chain
;
50 static int value_history_count
; /* Abs number of last entry stored */
52 /* List of all value objects currently allocated
53 (except for those released by calls to release_value)
54 This is so they can be freed after each command. */
56 static value all_values
;
58 /* Allocate a value that has the correct length for type TYPE. */
66 check_stub_type (type
);
68 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
69 VALUE_NEXT (val
) = all_values
;
71 VALUE_TYPE (val
) = type
;
72 VALUE_LVAL (val
) = not_lval
;
73 VALUE_ADDRESS (val
) = 0;
74 VALUE_FRAME (val
) = 0;
75 VALUE_OFFSET (val
) = 0;
76 VALUE_BITPOS (val
) = 0;
77 VALUE_BITSIZE (val
) = 0;
78 VALUE_REPEATED (val
) = 0;
79 VALUE_REPETITIONS (val
) = 0;
80 VALUE_REGNO (val
) = -1;
82 VALUE_OPTIMIZED_OUT (val
) = 0;
86 /* Allocate a value that has the correct length
87 for COUNT repetitions type TYPE. */
90 allocate_repeat_value (type
, count
)
96 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
97 VALUE_NEXT (val
) = all_values
;
99 VALUE_TYPE (val
) = type
;
100 VALUE_LVAL (val
) = not_lval
;
101 VALUE_ADDRESS (val
) = 0;
102 VALUE_FRAME (val
) = 0;
103 VALUE_OFFSET (val
) = 0;
104 VALUE_BITPOS (val
) = 0;
105 VALUE_BITSIZE (val
) = 0;
106 VALUE_REPEATED (val
) = 1;
107 VALUE_REPETITIONS (val
) = count
;
108 VALUE_REGNO (val
) = -1;
109 VALUE_LAZY (val
) = 0;
110 VALUE_OPTIMIZED_OUT (val
) = 0;
114 /* Return a mark in the value chain. All values allocated after the
115 mark is obtained (except for those released) are subject to being freed
116 if a subsequent value_free_to_mark is passed the mark. */
123 /* Free all values allocated since MARK was obtained by value_mark
124 (except for those released). */
126 value_free_to_mark (mark
)
131 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
133 next
= VALUE_NEXT (val
);
139 /* Free all the values that have been allocated (except for those released).
140 Called after each command, successful or not. */
145 register value val
, next
;
147 for (val
= all_values
; val
; val
= next
)
149 next
= VALUE_NEXT (val
);
156 /* Remove VAL from the chain all_values
157 so it will not be freed automatically. */
165 if (all_values
== val
)
167 all_values
= val
->next
;
171 for (v
= all_values
; v
; v
= v
->next
)
181 /* Return a copy of the value ARG.
182 It contains the same contents, for same memory address,
183 but it's a different block of storage. */
190 register struct type
*type
= VALUE_TYPE (arg
);
191 if (VALUE_REPEATED (arg
))
192 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
194 val
= allocate_value (type
);
195 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
196 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
197 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
198 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
199 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
200 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
201 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
202 if (!VALUE_LAZY (val
))
204 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
205 TYPE_LENGTH (VALUE_TYPE (arg
))
206 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
211 /* Access to the value history. */
213 /* Record a new value in the value history.
214 Returns the absolute history index of the entry.
215 Result of -1 indicates the value was not saved; otherwise it is the
216 value history index of this new item. */
219 record_latest_value (val
)
224 /* Check error now if about to store an invalid float. We return -1
225 to the caller, but allow them to continue, e.g. to print it as "Nan". */
226 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
227 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
228 if (i
) return -1; /* Indicate value not saved in history */
231 /* Here we treat value_history_count as origin-zero
232 and applying to the value being stored now. */
234 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
237 register struct value_history_chunk
*new
238 = (struct value_history_chunk
*)
239 xmalloc (sizeof (struct value_history_chunk
));
240 bzero (new->values
, sizeof new->values
);
241 new->next
= value_history_chain
;
242 value_history_chain
= new;
245 value_history_chain
->values
[i
] = val
;
248 /* Now we regard value_history_count as origin-one
249 and applying to the value just stored. */
251 return ++value_history_count
;
254 /* Return a copy of the value in the history with sequence number NUM. */
257 access_value_history (num
)
260 register struct value_history_chunk
*chunk
;
262 register int absnum
= num
;
265 absnum
+= value_history_count
;
270 error ("The history is empty.");
272 error ("There is only one value in the history.");
274 error ("History does not go back to $$%d.", -num
);
276 if (absnum
> value_history_count
)
277 error ("History has not yet reached $%d.", absnum
);
281 /* Now absnum is always absolute and origin zero. */
283 chunk
= value_history_chain
;
284 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
288 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
291 /* Clear the value history entirely.
292 Must be done when new symbol tables are loaded,
293 because the type pointers become invalid. */
296 clear_value_history ()
298 register struct value_history_chunk
*next
;
302 while (value_history_chain
)
304 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
305 if (val
= value_history_chain
->values
[i
])
307 next
= value_history_chain
->next
;
308 free (value_history_chain
);
309 value_history_chain
= next
;
311 value_history_count
= 0;
315 show_values (num_exp
, from_tty
)
325 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
326 /* "info history +" should print from the stored position. */
329 /* "info history <exp>" should print around value number <exp>. */
330 num
= parse_and_eval_address (num_exp
) - 5;
334 /* "info history" means print the last 10 values. */
335 num
= value_history_count
- 9;
341 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
343 val
= access_value_history (i
);
344 printf_filtered ("$%d = ", i
);
345 value_print (val
, stdout
, 0, Val_pretty_default
);
346 printf_filtered ("\n");
349 /* The next "info history +" should start after what we just printed. */
352 /* Hitting just return after this command should do the same thing as
353 "info history +". If num_exp is null, this is unnecessary, since
354 "info history +" is not useful after "info history". */
355 if (from_tty
&& num_exp
)
362 /* Internal variables. These are variables within the debugger
363 that hold values assigned by debugger commands.
364 The user refers to them with a '$' prefix
365 that does not appear in the variable names stored internally. */
367 static struct internalvar
*internalvars
;
369 /* Look up an internal variable with name NAME. NAME should not
370 normally include a dollar sign.
372 If the specified internal variable does not exist,
373 one is created, with a void value. */
376 lookup_internalvar (name
)
379 register struct internalvar
*var
;
381 for (var
= internalvars
; var
; var
= var
->next
)
382 if (!strcmp (var
->name
, name
))
385 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
386 var
->name
= concat (name
, NULL
);
387 var
->value
= allocate_value (builtin_type_void
);
388 release_value (var
->value
);
389 var
->next
= internalvars
;
395 value_of_internalvar (var
)
396 struct internalvar
*var
;
400 #ifdef IS_TRAPPED_INTERNALVAR
401 if (IS_TRAPPED_INTERNALVAR (var
->name
))
402 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
405 val
= value_copy (var
->value
);
406 if (VALUE_LAZY (val
))
407 value_fetch_lazy (val
);
408 VALUE_LVAL (val
) = lval_internalvar
;
409 VALUE_INTERNALVAR (val
) = var
;
414 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
415 struct internalvar
*var
;
416 int offset
, bitpos
, bitsize
;
419 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
421 #ifdef IS_TRAPPED_INTERNALVAR
422 if (IS_TRAPPED_INTERNALVAR (var
->name
))
423 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
427 modify_field (addr
, (int) value_as_long (newval
),
430 bcopy (VALUE_CONTENTS (newval
), addr
,
431 TYPE_LENGTH (VALUE_TYPE (newval
)));
435 set_internalvar (var
, val
)
436 struct internalvar
*var
;
439 #ifdef IS_TRAPPED_INTERNALVAR
440 if (IS_TRAPPED_INTERNALVAR (var
->name
))
441 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
445 var
->value
= value_copy (val
);
446 release_value (var
->value
);
450 internalvar_name (var
)
451 struct internalvar
*var
;
456 /* Free all internalvars. Done when new symtabs are loaded,
457 because that makes the values invalid. */
460 clear_internalvars ()
462 register struct internalvar
*var
;
467 internalvars
= var
->next
;
477 register struct internalvar
*var
;
480 for (var
= internalvars
; var
; var
= var
->next
)
482 #ifdef IS_TRAPPED_INTERNALVAR
483 if (IS_TRAPPED_INTERNALVAR (var
->name
))
490 printf ("Debugger convenience variables:\n\n");
494 printf_filtered ("$%s = ", var
->name
);
495 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
496 printf_filtered ("\n");
499 printf ("No debugger convenience variables now defined.\n\
500 Convenience variables have names starting with \"$\";\n\
501 use \"set\" as in \"set $foo = 5\" to define them.\n");
504 /* Extract a value as a C number (either long or double).
505 Knows how to convert fixed values to double, or
506 floating values to long.
507 Does not deallocate the value. */
513 /* This coerces arrays and functions, which is necessary (e.g.
514 in disassemble_command). It also dereferences references, which
515 I suspect is the most logical thing to do. */
516 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
518 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
522 value_as_double (val
)
528 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
530 error ("Invalid floating value found in program.");
533 /* Extract a value as a C pointer.
534 Does not deallocate the value. */
536 value_as_pointer (val
)
539 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
540 whether we want this to be true eventually. */
541 return value_as_long (val
);
544 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
545 as a long, or as a double, assuming the raw data is described
546 by type TYPE. Knows how to convert different sizes of values
547 and can convert between fixed and floating point. We don't assume
548 any alignment for the raw data. Return value is in host byte order.
550 If you want functions and arrays to be coerced to pointers, and
551 references to be dereferenced, call value_as_long() instead.
553 C++: It is assumed that the front-end has taken care of
554 all matters concerning pointers to members. A pointer
555 to member which reaches here is considered to be equivalent
556 to an INT (or some size). After all, it is only an offset. */
558 /* FIXME: This should be rewritten as a switch statement for speed and
559 ease of comprehension. */
562 unpack_long (type
, valaddr
)
566 register enum type_code code
= TYPE_CODE (type
);
567 register int len
= TYPE_LENGTH (type
);
568 register int nosign
= TYPE_UNSIGNED (type
);
570 if (code
== TYPE_CODE_ENUM
|| code
== TYPE_CODE_BOOL
)
571 code
= TYPE_CODE_INT
;
572 if (code
== TYPE_CODE_FLT
)
574 if (len
== sizeof (float))
577 bcopy (valaddr
, &retval
, sizeof (retval
));
578 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
582 if (len
== sizeof (double))
585 bcopy (valaddr
, &retval
, sizeof (retval
));
586 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
591 error ("Unexpected type of floating point number.");
594 else if (code
== TYPE_CODE_INT
&& nosign
)
596 if (len
== sizeof (char))
598 unsigned char retval
= * (unsigned char *) valaddr
;
599 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
603 if (len
== sizeof (short))
605 unsigned short retval
;
606 bcopy (valaddr
, &retval
, sizeof (retval
));
607 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
611 if (len
== sizeof (int))
614 bcopy (valaddr
, &retval
, sizeof (retval
));
615 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
619 if (len
== sizeof (long))
621 unsigned long retval
;
622 bcopy (valaddr
, &retval
, sizeof (retval
));
623 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
627 if (len
== sizeof (long long))
629 unsigned long long retval
;
630 bcopy (valaddr
, &retval
, sizeof (retval
));
631 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
637 error ("That operation is not possible on an integer of that size.");
640 else if (code
== TYPE_CODE_INT
)
642 if (len
== sizeof (char))
645 bcopy (valaddr
, &retval
, sizeof (retval
));
646 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
650 if (len
== sizeof (short))
653 bcopy (valaddr
, &retval
, sizeof (retval
));
654 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
658 if (len
== sizeof (int))
661 bcopy (valaddr
, &retval
, sizeof (retval
));
662 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
666 if (len
== sizeof (long))
669 bcopy (valaddr
, &retval
, sizeof (retval
));
670 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
675 if (len
== sizeof (long long))
678 bcopy (valaddr
, &retval
, sizeof (retval
));
679 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
685 error ("That operation is not possible on an integer of that size.");
688 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
689 whether we want this to be true eventually. */
690 else if (code
== TYPE_CODE_PTR
691 || code
== TYPE_CODE_REF
)
693 if (len
== sizeof (CORE_ADDR
))
696 bcopy (valaddr
, &retval
, sizeof (retval
));
697 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
701 else if (code
== TYPE_CODE_MEMBER
)
702 error ("not implemented: member types in unpack_long");
703 else if (code
== TYPE_CODE_CHAR
)
704 return *(unsigned char *)valaddr
;
706 error ("Value not integer or pointer.");
707 return 0; /* For lint -- never reached */
710 /* Return a double value from the specified type and address.
711 INVP points to an int which is set to 0 for valid value,
712 1 for invalid value (bad float format). In either case,
713 the returned double is OK to use. Argument is in target
714 format, result is in host format. */
717 unpack_double (type
, valaddr
, invp
)
722 register enum type_code code
= TYPE_CODE (type
);
723 register int len
= TYPE_LENGTH (type
);
724 register int nosign
= TYPE_UNSIGNED (type
);
726 *invp
= 0; /* Assume valid. */
727 if (code
== TYPE_CODE_FLT
)
729 if (INVALID_FLOAT (valaddr
, len
))
732 return 1.234567891011121314;
735 if (len
== sizeof (float))
738 bcopy (valaddr
, &retval
, sizeof (retval
));
739 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
743 if (len
== sizeof (double))
746 bcopy (valaddr
, &retval
, sizeof (retval
));
747 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
752 error ("Unexpected type of floating point number.");
753 return 0; /* Placate lint. */
757 /* Unsigned -- be sure we compensate for signed LONGEST. */
759 return (unsigned long long) unpack_long (type
, valaddr
);
761 return (unsigned long ) unpack_long (type
, valaddr
);
764 /* Signed -- we are OK with unpack_long. */
765 return unpack_long (type
, valaddr
);
769 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
770 as a CORE_ADDR, assuming the raw data is described by type TYPE.
771 We don't assume any alignment for the raw data. Return value is in
774 If you want functions and arrays to be coerced to pointers, and
775 references to be dereferenced, call value_as_pointer() instead.
777 C++: It is assumed that the front-end has taken care of
778 all matters concerning pointers to members. A pointer
779 to member which reaches here is considered to be equivalent
780 to an INT (or some size). After all, it is only an offset. */
783 unpack_pointer (type
, valaddr
)
788 /* The user should be able to use an int (e.g. 0x7892) in contexts
789 where a pointer is expected. So this doesn't do enough. */
790 register enum type_code code
= TYPE_CODE (type
);
791 register int len
= TYPE_LENGTH (type
);
793 if (code
== TYPE_CODE_PTR
794 || code
== TYPE_CODE_REF
)
796 if (len
== sizeof (CORE_ADDR
))
799 bcopy (valaddr
, &retval
, sizeof (retval
));
800 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
803 error ("Unrecognized pointer size.");
805 else if (code
== TYPE_CODE_MEMBER
)
806 error ("not implemented: member types in unpack_pointer");
808 error ("Value is not a pointer.");
809 return 0; /* For lint -- never reached */
811 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
812 whether we want this to be true eventually. */
813 return unpack_long (type
, valaddr
);
817 /* Given a value ARG1 (offset by OFFSET bytes)
818 of a struct or union type ARG_TYPE,
819 extract and return the value of one of its fields.
820 FIELDNO says which field.
822 For C++, must also be able to return values from static fields */
825 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
828 register int fieldno
;
829 register struct type
*arg_type
;
832 register struct type
*type
;
834 check_stub_type (arg_type
);
835 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
837 /* Handle packed fields */
839 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
840 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
842 v
= value_from_longest (type
,
843 unpack_field_as_long (arg_type
,
844 VALUE_CONTENTS (arg1
),
846 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
847 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
851 v
= allocate_value (type
);
852 if (VALUE_LAZY (arg1
))
855 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
856 VALUE_CONTENTS_RAW (v
),
859 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
860 if (VALUE_LVAL (arg1
) == lval_internalvar
)
861 VALUE_LVAL (v
) = lval_internalvar_component
;
862 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
863 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
867 /* Given a value ARG1 of a struct or union type,
868 extract and return the value of one of its fields.
869 FIELDNO says which field.
871 For C++, must also be able to return values from static fields */
874 value_field (arg1
, fieldno
)
876 register int fieldno
;
878 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
881 /* Return a non-virtual function as a value.
882 F is the list of member functions which contains the desired method.
883 J is an index into F which provides the desired method. */
886 value_fn_field (f
, j
)
891 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, j
);
894 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
895 0, VAR_NAMESPACE
, 0, NULL
);
896 if (! sym
) error ("Internal error: could not find physical method named %s",
897 TYPE_FN_FIELD_PHYSNAME (f
, j
));
899 v
= allocate_value (type
);
900 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
901 VALUE_TYPE (v
) = type
;
905 /* Return a virtual function as a value.
906 ARG1 is the object which provides the virtual function
907 table pointer. ARG1 is side-effected in calling this function.
908 F is the list of member functions which contains the desired virtual
910 J is an index into F which provides the desired virtual function.
912 TYPE is the type in which F is located. */
914 value_virtual_fn_field (arg1
, f
, j
, type
)
920 /* First, get the virtual function table pointer. That comes
921 with a strange type, so cast it to type `pointer to long' (which
922 should serve just fine as a function type). Then, index into
923 the table, and convert final value to appropriate function type. */
924 value entry
, vfn
, vtbl
;
925 value vi
= value_from_longest (builtin_type_int
,
926 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
927 struct type
*fcontext
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
928 struct type
*context
;
929 if (fcontext
== NULL
)
930 /* We don't have an fcontext (e.g. the program was compiled with
931 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
932 This won't work right for multiple inheritance, but at least we
933 should do as well as GDB 3.x did. */
934 fcontext
= TYPE_VPTR_BASETYPE (type
);
935 context
= lookup_pointer_type (fcontext
);
936 /* Now context is a pointer to the basetype containing the vtbl. */
937 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
938 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
940 context
= VALUE_TYPE (arg1
);
941 /* Now context is the basetype containing the vtbl. */
943 /* This type may have been defined before its virtual function table
944 was. If so, fill in the virtual function table entry for the
946 if (TYPE_VPTR_FIELDNO (context
) < 0)
947 fill_in_vptr_fieldno (context
);
949 /* The virtual function table is now an array of structures
950 which have the form { int16 offset, delta; void *pfn; }. */
951 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
953 /* Index into the virtual function table. This is hard-coded because
954 looking up a field is not cheap, and it may be important to save
955 time, e.g. if the user has set a conditional breakpoint calling
956 a virtual function. */
957 entry
= value_subscript (vtbl
, vi
);
959 /* Move the `this' pointer according to the virtual function table. */
960 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
961 if (! VALUE_LAZY (arg1
))
963 VALUE_LAZY (arg1
) = 1;
964 value_fetch_lazy (arg1
);
967 vfn
= value_field (entry
, 2);
968 /* Reinstantiate the function pointer with the correct type. */
969 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
974 /* ARG is a pointer to an object we know to be at least
975 a DTYPE. BTYPE is the most derived basetype that has
976 already been searched (and need not be searched again).
977 After looking at the vtables between BTYPE and DTYPE,
978 return the most derived type we find. The caller must
979 be satisfied when the return value == DTYPE.
981 FIXME-tiemann: should work with dossier entries as well. */
984 value_headof (arg
, btype
, dtype
)
986 struct type
*btype
, *dtype
;
988 /* First collect the vtables we must look at for this object. */
989 /* FIXME-tiemann: right now, just look at top-most vtable. */
990 value vtbl
, entry
, best_entry
= 0;
991 /* FIXME: entry_type is never used. */
992 struct type
*entry_type
;
994 int offset
, best_offset
= 0;
996 CORE_ADDR pc_for_sym
;
997 char *demangled_name
;
998 btype
= TYPE_VPTR_BASETYPE (dtype
);
999 check_stub_type (btype
);
1001 vtbl
= value_cast (lookup_pointer_type (btype
), arg
);
1004 vtbl
= value_ind (value_field (value_ind (vtbl
), TYPE_VPTR_FIELDNO (btype
)));
1006 /* Check that VTBL looks like it points to a virtual function table. */
1007 i
= find_pc_misc_function (VALUE_ADDRESS (vtbl
));
1008 if (i
< 0 || ! VTBL_PREFIX_P (demangled_name
= misc_function_vector
[i
].name
))
1010 /* If we expected to find a vtable, but did not, let the user
1011 know that we aren't happy, but don't throw an error.
1012 FIXME: there has to be a better way to do this. */
1013 struct type
*error_type
= (struct type
*)xmalloc (sizeof (struct type
));
1014 bcopy (VALUE_TYPE (arg
), error_type
, sizeof (struct type
));
1015 TYPE_NAME (error_type
) = savestring ("suspicious *", sizeof ("suspicious *"));
1016 VALUE_TYPE (arg
) = error_type
;
1020 /* Now search through the virtual function table. */
1021 entry
= value_ind (vtbl
);
1022 nelems
= longest_to_int (value_as_long (value_field (entry
, 2)));
1023 for (i
= 1; i
<= nelems
; i
++)
1025 entry
= value_subscript (vtbl
, value_from_longest (builtin_type_int
,
1027 offset
= longest_to_int (value_as_long (value_field (entry
, 0)));
1028 /* If we use '<=' we can handle single inheritance
1029 * where all offsets are zero - just use the first entry found. */
1030 if (offset
<= best_offset
)
1032 best_offset
= offset
;
1036 /* Move the pointer according to BEST_ENTRY's offset, and figure
1037 out what type we should return as the new pointer. */
1038 if (best_entry
== 0)
1040 /* An alternative method (which should no longer be necessary).
1041 * But we leave it in for future use, when we will hopefully
1042 * have optimizes the vtable to use thunks instead of offsets. */
1043 /* Use the name of vtable itself to extract a base type. */
1044 demangled_name
+= 4; /* Skip _vt$ prefix. */
1048 pc_for_sym
= value_as_pointer (value_field (best_entry
, 2));
1049 sym
= find_pc_function (pc_for_sym
);
1050 demangled_name
= cplus_demangle (SYMBOL_NAME (sym
), -1);
1051 *(strchr (demangled_name
, ':')) = '\0';
1053 sym
= lookup_symbol (demangled_name
, 0, VAR_NAMESPACE
, 0, 0);
1055 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym
));
1058 free (demangled_name
);
1059 arg
= value_add (value_cast (builtin_type_int
, arg
),
1060 value_field (best_entry
, 0));
1062 VALUE_TYPE (arg
) = lookup_pointer_type (SYMBOL_TYPE (sym
));
1066 /* ARG is a pointer object of type TYPE. If TYPE has virtual
1067 function tables, probe ARG's tables (including the vtables
1068 of its baseclasses) to figure out the most derived type that ARG
1069 could actually be a pointer to. */
1072 value_from_vtable_info (arg
, type
)
1076 /* Take care of preliminaries. */
1077 if (TYPE_VPTR_FIELDNO (type
) < 0)
1078 fill_in_vptr_fieldno (type
);
1079 if (TYPE_VPTR_FIELDNO (type
) < 0 || VALUE_REPEATED (arg
))
1082 return value_headof (arg
, 0, type
);
1085 /* The value of a static class member does not depend
1086 on its instance, only on its type. If FIELDNO >= 0,
1087 then fieldno is a valid field number and is used directly.
1088 Otherwise, FIELDNAME is the name of the field we are
1089 searching for. If it is not a static field name, an
1090 error is signaled. TYPE is the type in which we look for the
1091 static field member.
1093 Return zero if we couldn't find anything; the caller may signal
1094 an error in that case. */
1097 value_static_field (type
, fieldname
, fieldno
)
1098 register struct type
*type
;
1100 register int fieldno
;
1108 /* Look for static field. */
1110 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1111 if (! strcmp (TYPE_FIELD_NAME (type
, i
), fieldname
))
1113 if (TYPE_FIELD_STATIC (type
, i
))
1119 error ("field `%s' is not static", fieldname
);
1123 v
= value_static_field (TYPE_BASECLASS (type
, i
), fieldname
, -1);
1128 if (destructor_name_p (fieldname
, type
))
1129 error ("Cannot get value of destructor");
1131 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1133 if (! strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), fieldname
))
1134 error ("Cannot get value of method \"%s\"", fieldname
);
1136 error("there is no field named %s", fieldname
);
1140 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1141 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1142 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1144 type
= TYPE_FIELD_TYPE (type
, fieldno
);
1145 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1149 /* Compute the address of the baseclass which is
1150 the INDEXth baseclass of TYPE. The TYPE base
1151 of the object is at VALADDR.
1153 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1154 or 0 if no error. In that case the return value is not the address
1155 of the baseclasss, but the address which could not be read
1159 baseclass_addr (type
, index
, valaddr
, valuep
, errp
)
1166 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
1171 if (BASETYPE_VIA_VIRTUAL (type
, index
))
1173 /* Must hunt for the pointer to this virtual baseclass. */
1174 register int i
, len
= TYPE_NFIELDS (type
);
1175 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
1176 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
1178 if (TYPE_MAIN_VARIANT (basetype
))
1179 basetype
= TYPE_MAIN_VARIANT (basetype
);
1181 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
1182 sprintf (vbase_name
, "_vb$%s", type_name
);
1183 /* First look for the virtual baseclass pointer
1185 for (i
= n_baseclasses
; i
< len
; i
++)
1187 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
1189 value val
= allocate_value (basetype
);
1194 = unpack_pointer (TYPE_FIELD_TYPE (type
, i
),
1195 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8));
1197 status
= target_read_memory (addr
,
1198 VALUE_CONTENTS_RAW (val
),
1199 TYPE_LENGTH (basetype
));
1200 VALUE_LVAL (val
) = lval_memory
;
1201 VALUE_ADDRESS (val
) = addr
;
1207 release_value (val
);
1211 return (char *)addr
;
1217 return (char *) VALUE_CONTENTS (val
);
1221 /* Not in the fields, so try looking through the baseclasses. */
1222 for (i
= index
+1; i
< n_baseclasses
; i
++)
1226 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
, errp
);
1236 /* Baseclass is easily computed. */
1239 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1242 /* Ugly hack to convert method stubs into method types.
1244 He ain't kiddin'. This demangles the name of the method into a string
1245 including argument types, parses out each argument type, generates
1246 a string casting a zero to that type, evaluates the string, and stuffs
1247 the resulting type into an argtype vector!!! Then it knows the type
1248 of the whole function (including argument types for overloading),
1249 which info used to be in the stab's but was removed to hack back
1250 the space required for them. */
1252 check_stub_method (type
, i
, j
)
1256 extern char *gdb_mangle_name (), *strchr ();
1257 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1258 char *mangled_name
= gdb_mangle_name (type
, i
, j
);
1259 char *demangled_name
= cplus_demangle (mangled_name
, 0);
1260 char *argtypetext
, *p
;
1261 int depth
= 0, argcount
= 1;
1262 struct type
**argtypes
;
1264 /* Now, read in the parameters that define this type. */
1265 argtypetext
= strchr (demangled_name
, '(') + 1;
1273 else if (*p
== ',' && depth
== 0)
1278 /* We need one more slot for the void [...] or NULL [end of arglist] */
1279 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1281 argtypes
[0] = lookup_pointer_type (type
);
1284 if (*p
!= ')') /* () means no args, skip while */
1289 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1291 argtypes
[argcount
] =
1292 parse_and_eval_type (argtypetext
, p
- argtypetext
);
1294 argtypetext
= p
+ 1;
1306 if (p
[-2] != '.') /* ... */
1307 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1309 argtypes
[argcount
] = NULL
; /* List terminator */
1311 free (demangled_name
);
1313 type
= lookup_method_type (type
, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)), argtypes
);
1314 /* Free the stub type...it's no longer needed. */
1315 free (TYPE_FN_FIELD_TYPE (f
, j
));
1316 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1317 TYPE_FN_FIELD_TYPE (f
, j
) = type
;
1321 unpack_field_as_long (type
, valaddr
, fieldno
)
1327 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1328 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1330 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1331 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1333 /* Extracting bits depends on endianness of the machine. */
1335 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1337 val
= val
>> (bitpos
% 8);
1340 if (bitsize
< 8 * sizeof (val
))
1341 val
&= (((unsigned long)1) << bitsize
) - 1;
1345 /* Modify the value of a bitfield. ADDR points to a block of memory in
1346 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1347 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1348 indicate which bits (in target bit order) comprise the bitfield. */
1351 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1354 int bitpos
, bitsize
;
1358 /* Reject values too big to fit in the field in question,
1359 otherwise adjoining fields may be corrupted. */
1360 if (bitsize
< (8 * sizeof (fieldval
))
1361 && 0 != (fieldval
& ~((1<<bitsize
)-1)))
1362 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1364 bcopy (addr
, &oword
, sizeof oword
);
1365 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1367 /* Shifting for bit field depends on endianness of the target machine. */
1369 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1372 /* Mask out old value, while avoiding shifts >= longword size */
1373 if (bitsize
< 8 * sizeof (oword
))
1374 oword
&= ~(((((unsigned long)1) << bitsize
) - 1) << bitpos
);
1376 oword
&= ~((-1) << bitpos
);
1377 oword
|= fieldval
<< bitpos
;
1379 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1380 bcopy (&oword
, addr
, sizeof oword
);
1383 /* Convert C numbers into newly allocated values */
1386 value_from_longest (type
, num
)
1388 register LONGEST num
;
1390 register value val
= allocate_value (type
);
1391 register enum type_code code
= TYPE_CODE (type
);
1392 register int len
= TYPE_LENGTH (type
);
1394 /* FIXME, we assume that pointers have the same form and byte order as
1395 integers, and that all pointers have the same form. */
1396 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
||
1397 code
== TYPE_CODE_CHAR
|| code
== TYPE_CODE_PTR
)
1399 if (len
== sizeof (char))
1400 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1401 else if (len
== sizeof (short))
1402 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1403 else if (len
== sizeof (int))
1404 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1405 else if (len
== sizeof (long))
1406 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1408 else if (len
== sizeof (long long))
1409 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1412 error ("Integer type encountered with unexpected data length.");
1415 error ("Unexpected type encountered for integer constant.");
1417 /* num was in host byte order. So now put the value's contents
1418 into target byte order. */
1419 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1425 value_from_double (type
, num
)
1429 register value val
= allocate_value (type
);
1430 register enum type_code code
= TYPE_CODE (type
);
1431 register int len
= TYPE_LENGTH (type
);
1433 if (code
== TYPE_CODE_FLT
)
1435 if (len
== sizeof (float))
1436 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1437 else if (len
== sizeof (double))
1438 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1440 error ("Floating type encountered with unexpected data length.");
1443 error ("Unexpected type encountered for floating constant.");
1445 /* num was in host byte order. So now put the value's contents
1446 into target byte order. */
1447 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1452 /* Deal with the value that is "about to be returned". */
1454 /* Return the value that a function returning now
1455 would be returning to its caller, assuming its type is VALTYPE.
1456 RETBUF is where we look for what ought to be the contents
1457 of the registers (in raw form). This is because it is often
1458 desirable to restore old values to those registers
1459 after saving the contents of interest, and then call
1460 this function using the saved values.
1461 struct_return is non-zero when the function in question is
1462 using the structure return conventions on the machine in question;
1463 0 when it is using the value returning conventions (this often
1464 means returning pointer to where structure is vs. returning value). */
1467 value_being_returned (valtype
, retbuf
, struct_return
)
1468 register struct type
*valtype
;
1469 char retbuf
[REGISTER_BYTES
];
1476 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1477 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1478 if (struct_return
) {
1479 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1481 error ("Function return value unknown");
1482 return value_at (valtype
, addr
);
1486 val
= allocate_value (valtype
);
1487 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1492 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1493 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1494 and TYPE is the type (which is known to be struct, union or array).
1496 On most machines, the struct convention is used unless we are
1497 using gcc and the type is of a special size. */
1498 #if !defined (USE_STRUCT_CONVENTION)
1499 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1500 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1501 || TYPE_LENGTH (value_type) == 2 \
1502 || TYPE_LENGTH (value_type) == 4 \
1503 || TYPE_LENGTH (value_type) == 8 \
1508 /* Return true if the function specified is using the structure returning
1509 convention on this machine to return arguments, or 0 if it is using
1510 the value returning convention. FUNCTION is the value representing
1511 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1512 is the type returned by the function. GCC_P is nonzero if compiled
1516 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1519 struct type
*value_type
;
1523 register enum type_code code
= TYPE_CODE (value_type
);
1525 if (code
== TYPE_CODE_ERROR
)
1526 error ("Function return type unknown.");
1528 if (code
== TYPE_CODE_STRUCT
||
1529 code
== TYPE_CODE_UNION
||
1530 code
== TYPE_CODE_ARRAY
)
1531 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1536 /* Store VAL so it will be returned if a function returns now.
1537 Does not verify that VAL's type matches what the current
1538 function wants to return. */
1541 set_return_value (val
)
1544 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1548 if (code
== TYPE_CODE_ERROR
)
1549 error ("Function return type unknown.");
1551 if (code
== TYPE_CODE_STRUCT
1552 || code
== TYPE_CODE_UNION
)
1553 error ("Specifying a struct or union return value is not supported.");
1555 /* FIXME, this is bogus. We don't know what the return conventions
1556 are, or how values should be promoted.... */
1557 if (code
== TYPE_CODE_FLT
)
1559 dbuf
= value_as_double (val
);
1561 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1565 lbuf
= value_as_long (val
);
1566 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1571 _initialize_values ()
1573 add_cmd ("convenience", no_class
, show_convenience
,
1574 "Debugger convenience (\"$foo\") variables.\n\
1575 These variables are created when you assign them values;\n\
1576 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1577 A few convenience variables are given values automatically:\n\
1578 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1579 \"$__\" holds the contents of the last address examined with \"x\".",
1582 add_cmd ("values", no_class
, show_values
,
1583 "Elements of value history around item number IDX (or last ten).",