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. */
30 /* The value-history records all the values printed
31 by print commands during this session. Each chunk
32 records 60 consecutive values. The first chunk on
33 the chain records the most recent values.
34 The total number of values is in value_history_count. */
36 #define VALUE_HISTORY_CHUNK 60
38 struct value_history_chunk
40 struct value_history_chunk
*next
;
41 value values
[VALUE_HISTORY_CHUNK
];
44 /* Chain of chunks now in use. */
46 static struct value_history_chunk
*value_history_chain
;
48 static int value_history_count
; /* Abs number of last entry stored */
51 /* List of all value objects currently allocated
52 (except for those released by calls to release_value)
53 This is so they can be freed after each command. */
55 static value all_values
;
57 /* Allocate a value that has the correct length for type TYPE. */
65 check_stub_type (type
);
67 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
68 VALUE_NEXT (val
) = all_values
;
70 VALUE_TYPE (val
) = type
;
71 VALUE_LVAL (val
) = not_lval
;
72 VALUE_ADDRESS (val
) = 0;
73 VALUE_FRAME (val
) = 0;
74 VALUE_OFFSET (val
) = 0;
75 VALUE_BITPOS (val
) = 0;
76 VALUE_BITSIZE (val
) = 0;
77 VALUE_REPEATED (val
) = 0;
78 VALUE_REPETITIONS (val
) = 0;
79 VALUE_REGNO (val
) = -1;
81 VALUE_OPTIMIZED_OUT (val
) = 0;
85 /* Allocate a value that has the correct length
86 for COUNT repetitions type TYPE. */
89 allocate_repeat_value (type
, count
)
95 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
96 VALUE_NEXT (val
) = all_values
;
98 VALUE_TYPE (val
) = type
;
99 VALUE_LVAL (val
) = not_lval
;
100 VALUE_ADDRESS (val
) = 0;
101 VALUE_FRAME (val
) = 0;
102 VALUE_OFFSET (val
) = 0;
103 VALUE_BITPOS (val
) = 0;
104 VALUE_BITSIZE (val
) = 0;
105 VALUE_REPEATED (val
) = 1;
106 VALUE_REPETITIONS (val
) = count
;
107 VALUE_REGNO (val
) = -1;
108 VALUE_LAZY (val
) = 0;
109 VALUE_OPTIMIZED_OUT (val
) = 0;
113 /* Free all the values that have been allocated (except for those released).
114 Called after each command, successful or not. */
119 register value val
, next
;
121 for (val
= all_values
; val
; val
= next
)
123 next
= VALUE_NEXT (val
);
130 /* Remove VAL from the chain all_values
131 so it will not be freed automatically. */
139 if (all_values
== val
)
141 all_values
= val
->next
;
145 for (v
= all_values
; v
; v
= v
->next
)
155 /* Return a copy of the value ARG.
156 It contains the same contents, for same memory address,
157 but it's a different block of storage. */
164 register struct type
*type
= VALUE_TYPE (arg
);
165 if (VALUE_REPEATED (arg
))
166 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
168 val
= allocate_value (type
);
169 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
170 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
171 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
172 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
173 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
174 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
175 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
176 if (!VALUE_LAZY (val
))
178 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
179 TYPE_LENGTH (VALUE_TYPE (arg
))
180 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
185 /* Access to the value history. */
187 /* Record a new value in the value history.
188 Returns the absolute history index of the entry.
189 Result of -1 indicates the value was not saved; otherwise it is the
190 value history index of this new item. */
193 record_latest_value (val
)
198 /* Check error now if about to store an invalid float. We return -1
199 to the caller, but allow them to continue, e.g. to print it as "Nan". */
200 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
201 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
202 if (i
) return -1; /* Indicate value not saved in history */
205 /* Here we treat value_history_count as origin-zero
206 and applying to the value being stored now. */
208 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
211 register struct value_history_chunk
*new
212 = (struct value_history_chunk
*)
213 xmalloc (sizeof (struct value_history_chunk
));
214 bzero (new->values
, sizeof new->values
);
215 new->next
= value_history_chain
;
216 value_history_chain
= new;
219 value_history_chain
->values
[i
] = val
;
222 /* Now we regard value_history_count as origin-one
223 and applying to the value just stored. */
225 return ++value_history_count
;
228 /* Return a copy of the value in the history with sequence number NUM. */
231 access_value_history (num
)
234 register struct value_history_chunk
*chunk
;
236 register int absnum
= num
;
239 absnum
+= value_history_count
;
244 error ("The history is empty.");
246 error ("There is only one value in the history.");
248 error ("History does not go back to $$%d.", -num
);
250 if (absnum
> value_history_count
)
251 error ("History has not yet reached $%d.", absnum
);
255 /* Now absnum is always absolute and origin zero. */
257 chunk
= value_history_chain
;
258 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
262 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
265 /* Clear the value history entirely.
266 Must be done when new symbol tables are loaded,
267 because the type pointers become invalid. */
270 clear_value_history ()
272 register struct value_history_chunk
*next
;
276 while (value_history_chain
)
278 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
279 if (val
= value_history_chain
->values
[i
])
281 next
= value_history_chain
->next
;
282 free (value_history_chain
);
283 value_history_chain
= next
;
285 value_history_count
= 0;
289 value_history_info (num_exp
, from_tty
)
299 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
300 /* "info history +" should print from the stored position. */
303 /* "info history <exp>" should print around value number <exp>. */
304 num
= parse_and_eval_address (num_exp
) - 5;
308 /* "info history" means print the last 10 values. */
309 num
= value_history_count
- 9;
315 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
317 val
= access_value_history (i
);
318 printf_filtered ("$%d = ", i
);
319 value_print (val
, stdout
, 0, Val_pretty_default
);
320 printf_filtered ("\n");
323 /* The next "info history +" should start after what we just printed. */
326 /* Hitting just return after this command should do the same thing as
327 "info history +". If num_exp is null, this is unnecessary, since
328 "info history +" is not useful after "info history". */
329 if (from_tty
&& num_exp
)
336 /* Internal variables. These are variables within the debugger
337 that hold values assigned by debugger commands.
338 The user refers to them with a '$' prefix
339 that does not appear in the variable names stored internally. */
341 static struct internalvar
*internalvars
;
343 /* Look up an internal variable with name NAME. NAME should not
344 normally include a dollar sign.
346 If the specified internal variable does not exist,
347 one is created, with a void value. */
350 lookup_internalvar (name
)
353 register struct internalvar
*var
;
355 for (var
= internalvars
; var
; var
= var
->next
)
356 if (!strcmp (var
->name
, name
))
359 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
360 var
->name
= concat (name
, "", "");
361 var
->value
= allocate_value (builtin_type_void
);
362 release_value (var
->value
);
363 var
->next
= internalvars
;
369 value_of_internalvar (var
)
370 struct internalvar
*var
;
374 #ifdef IS_TRAPPED_INTERNALVAR
375 if (IS_TRAPPED_INTERNALVAR (var
->name
))
376 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
379 val
= value_copy (var
->value
);
380 if (VALUE_LAZY (val
))
381 value_fetch_lazy (val
);
382 VALUE_LVAL (val
) = lval_internalvar
;
383 VALUE_INTERNALVAR (val
) = var
;
388 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
389 struct internalvar
*var
;
390 int offset
, bitpos
, bitsize
;
393 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
395 #ifdef IS_TRAPPED_INTERNALVAR
396 if (IS_TRAPPED_INTERNALVAR (var
->name
))
397 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
401 modify_field (addr
, (int) value_as_long (newval
),
404 bcopy (VALUE_CONTENTS (newval
), addr
,
405 TYPE_LENGTH (VALUE_TYPE (newval
)));
409 set_internalvar (var
, val
)
410 struct internalvar
*var
;
413 #ifdef IS_TRAPPED_INTERNALVAR
414 if (IS_TRAPPED_INTERNALVAR (var
->name
))
415 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
419 var
->value
= value_copy (val
);
420 release_value (var
->value
);
424 internalvar_name (var
)
425 struct internalvar
*var
;
430 /* Free all internalvars. Done when new symtabs are loaded,
431 because that makes the values invalid. */
434 clear_internalvars ()
436 register struct internalvar
*var
;
441 internalvars
= var
->next
;
451 register struct internalvar
*var
;
454 for (var
= internalvars
; var
; var
= var
->next
)
456 #ifdef IS_TRAPPED_INTERNALVAR
457 if (IS_TRAPPED_INTERNALVAR (var
->name
))
464 printf ("Debugger convenience variables:\n\n");
468 printf ("$%s = ", var
->name
);
469 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
473 printf ("No debugger convenience variables now defined.\n\
474 Convenience variables have names starting with \"$\";\n\
475 use \"set\" as in \"set $foo = 5\" to define them.\n");
478 /* Extract a value as a C number (either long or double).
479 Knows how to convert fixed values to double, or
480 floating values to long.
481 Does not deallocate the value. */
487 /* This coerces arrays and functions, which is necessary (e.g.
488 in disassemble_command). It also dereferences references, which
489 I suspect is the most logical thing to do. */
490 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
492 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
496 value_as_double (val
)
502 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
504 error ("Invalid floating value found in program.");
508 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
509 as a long, or as a double, assuming the raw data is described
510 by type TYPE. Knows how to convert different sizes of values
511 and can convert between fixed and floating point. We don't assume
512 any alignment for the raw data. Return value is in host byte order.
514 If you want functions and arrays to be coerced to pointers, and
515 references to be dereferenced, call value_as_long() instead.
517 C++: It is assumed that the front-end has taken care of
518 all matters concerning pointers to members. A pointer
519 to member which reaches here is considered to be equivalent
520 to an INT (or some size). After all, it is only an offset. */
523 unpack_long (type
, valaddr
)
527 register enum type_code code
= TYPE_CODE (type
);
528 register int len
= TYPE_LENGTH (type
);
529 register int nosign
= TYPE_UNSIGNED (type
);
531 if (code
== TYPE_CODE_ENUM
)
532 code
= TYPE_CODE_INT
;
533 if (code
== TYPE_CODE_FLT
)
535 if (len
== sizeof (float))
538 bcopy (valaddr
, &retval
, sizeof (retval
));
539 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
543 if (len
== sizeof (double))
546 bcopy (valaddr
, &retval
, sizeof (retval
));
547 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
552 error ("Unexpected type of floating point number.");
555 else if (code
== TYPE_CODE_INT
&& nosign
)
557 if (len
== sizeof (char))
559 unsigned char retval
= * (unsigned char *) valaddr
;
560 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
564 if (len
== sizeof (short))
566 unsigned short retval
;
567 bcopy (valaddr
, &retval
, sizeof (retval
));
568 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
572 if (len
== sizeof (int))
575 bcopy (valaddr
, &retval
, sizeof (retval
));
576 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
580 if (len
== sizeof (long))
582 unsigned long retval
;
583 bcopy (valaddr
, &retval
, sizeof (retval
));
584 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
588 if (len
== sizeof (long long))
590 unsigned long long retval
;
591 bcopy (valaddr
, &retval
, sizeof (retval
));
592 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
598 error ("That operation is not possible on an integer of that size.");
601 else if (code
== TYPE_CODE_INT
)
603 if (len
== sizeof (char))
606 bcopy (valaddr
, &retval
, sizeof (retval
));
607 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
611 if (len
== sizeof (short))
614 bcopy (valaddr
, &retval
, sizeof (retval
));
615 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
619 if (len
== sizeof (int))
622 bcopy (valaddr
, &retval
, sizeof (retval
));
623 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
627 if (len
== sizeof (long))
630 bcopy (valaddr
, &retval
, sizeof (retval
));
631 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
636 if (len
== sizeof (long long))
639 bcopy (valaddr
, &retval
, sizeof (retval
));
640 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
646 error ("That operation is not possible on an integer of that size.");
649 else if (code
== TYPE_CODE_PTR
650 || code
== TYPE_CODE_REF
)
652 if (len
== sizeof (char *))
655 bcopy (valaddr
, &retval
, sizeof (retval
));
656 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
660 else if (code
== TYPE_CODE_MEMBER
)
661 error ("not implemented: member types in unpack_long");
663 error ("Value not integer or pointer.");
664 return 0; /* For lint -- never reached */
667 /* Return a double value from the specified type and address.
668 INVP points to an int which is set to 0 for valid value,
669 1 for invalid value (bad float format). In either case,
670 the returned double is OK to use. Argument is in target
671 format, result is in host format. */
674 unpack_double (type
, valaddr
, invp
)
679 register enum type_code code
= TYPE_CODE (type
);
680 register int len
= TYPE_LENGTH (type
);
681 register int nosign
= TYPE_UNSIGNED (type
);
683 *invp
= 0; /* Assume valid. */
684 if (code
== TYPE_CODE_FLT
)
686 if (INVALID_FLOAT (valaddr
, len
))
689 return 1.234567891011121314;
692 if (len
== sizeof (float))
695 bcopy (valaddr
, &retval
, sizeof (retval
));
696 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
700 if (len
== sizeof (double))
703 bcopy (valaddr
, &retval
, sizeof (retval
));
704 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
709 error ("Unexpected type of floating point number.");
713 /* Unsigned -- be sure we compensate for signed LONGEST. */
715 return (unsigned long long) unpack_long (type
, valaddr
);
717 return (unsigned long ) unpack_long (type
, valaddr
);
720 /* Signed -- we are OK with unpack_long. */
721 return unpack_long (type
, valaddr
);
725 /* Given a value ARG1 (offset by OFFSET bytes)
726 of a struct or union type ARG_TYPE,
727 extract and return the value of one of its fields.
728 FIELDNO says which field.
730 For C++, must also be able to return values from static fields */
733 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
736 register int fieldno
;
737 register struct type
*arg_type
;
740 register struct type
*type
;
742 check_stub_type (arg_type
);
743 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
745 /* Handle packed fields */
747 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
748 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
750 v
= value_from_long (type
,
751 unpack_field_as_long (arg_type
,
752 VALUE_CONTENTS (arg1
),
754 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
755 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
759 v
= allocate_value (type
);
760 if (VALUE_LAZY (arg1
))
763 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
764 VALUE_CONTENTS_RAW (v
),
767 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
768 if (VALUE_LVAL (arg1
) == lval_internalvar
)
769 VALUE_LVAL (v
) = lval_internalvar_component
;
770 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
771 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
775 /* Given a value ARG1 of a struct or union type,
776 extract and return the value of one of its fields.
777 FIELDNO says which field.
779 For C++, must also be able to return values from static fields */
782 value_field (arg1
, fieldno
)
784 register int fieldno
;
786 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
790 value_fn_field (arg1
, fieldno
, subfieldno
)
792 register int fieldno
;
796 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
797 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
800 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
801 0, VAR_NAMESPACE
, 0, NULL
);
802 if (! sym
) error ("Internal error: could not find physical method named %s",
803 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
805 v
= allocate_value (type
);
806 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
807 VALUE_TYPE (v
) = type
;
811 /* Return a virtual function as a value.
812 ARG1 is the object which provides the virtual function
813 table pointer. ARG1 is side-effected in calling this function.
814 F is the list of member functions which contains the desired virtual
816 J is an index into F which provides the desired virtual function. */
818 value_virtual_fn_field (arg1
, f
, j
)
823 /* First, get the virtual function table pointer. That comes
824 with a strange type, so cast it to type `pointer to long' (which
825 should serve just fine as a function type). Then, index into
826 the table, and convert final value to appropriate function type. */
827 value entry
, vfn
, vtbl
;
828 value vi
= value_from_long (builtin_type_int
,
829 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
830 struct type
*context
= lookup_pointer_type (TYPE_FN_FIELD_FCONTEXT (f
, j
));
831 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
832 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
834 context
= VALUE_TYPE (arg1
);
836 /* This type may have been defined before its virtual function table
837 was. If so, fill in the virtual function table entry for the
839 if (TYPE_VPTR_FIELDNO (context
) < 0)
840 TYPE_VPTR_FIELDNO (context
)
841 = fill_in_vptr_fieldno (context
);
843 /* The virtual function table is now an array of structures
844 which have the form { int16 offset, delta; void *pfn; }. */
845 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (context
)));
847 /* Index into the virtual function table. This is hard-coded because
848 looking up a field is not cheap, and it may be important to save
849 time, e.g. if the user has set a conditional breakpoint calling
850 a virtual function. */
851 entry
= value_subscript (vtbl
, vi
);
853 /* Move the `this' pointer according to the virtual function table. */
854 VALUE_OFFSET (arg1
) += value_as_long (value_field (entry
, 0));
855 if (! VALUE_LAZY (arg1
))
857 VALUE_LAZY (arg1
) = 1;
858 value_fetch_lazy (arg1
);
861 vfn
= value_field (entry
, 2);
862 /* Reinstantiate the function pointer with the correct type. */
863 VALUE_TYPE (vfn
) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
868 /* The value of a static class member does not depend
869 on its instance, only on its type. If FIELDNO >= 0,
870 then fieldno is a valid field number and is used directly.
871 Otherwise, FIELDNAME is the name of the field we are
872 searching for. If it is not a static field name, an
873 error is signaled. TYPE is the type in which we look for the
874 static field member. */
876 value_static_field (type
, fieldname
, fieldno
)
877 register struct type
*type
;
879 register int fieldno
;
887 register struct type
*t
= type
;
888 /* Look for static field. */
892 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
893 if (! strcmp (TYPE_FIELD_NAME (t
, i
), fieldname
))
895 if (TYPE_FIELD_STATIC (t
, i
))
901 error ("field `%s' is not static");
903 /* FIXME: this does not recursively check multiple baseclasses. */
904 t
= TYPE_N_BASECLASSES (t
) ? TYPE_BASECLASS (t
, 0) : 0;
909 if (destructor_name_p (fieldname
, t
))
910 error ("Cannot get value of destructor");
916 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; i
--)
918 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), fieldname
))
920 error ("Cannot get value of method \"%s\"", fieldname
);
923 t
= TYPE_N_BASECLASSES (t
) ? TYPE_BASECLASS (t
, 0) : 0;
925 error("there is no field named %s", fieldname
);
929 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
930 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
931 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
933 type
= TYPE_FIELD_TYPE (type
, fieldno
);
934 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
938 /* Compute the address of the baseclass which is
939 the INDEXth baseclass of TYPE. The TYPE base
940 of the object is at VALADDR. */
943 baseclass_addr (type
, index
, valaddr
, valuep
)
949 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
951 if (BASETYPE_VIA_VIRTUAL (type
, index
))
953 /* Must hunt for the pointer to this virtual baseclass. */
954 register int i
, len
= TYPE_NFIELDS (type
);
955 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
956 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
958 if (TYPE_MAIN_VARIANT (basetype
))
959 basetype
= TYPE_MAIN_VARIANT (basetype
);
961 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
962 sprintf (vbase_name
, "_vb$%s", type_name
);
963 /* First look for the virtual baseclass pointer
965 for (i
= n_baseclasses
; i
< len
; i
++)
967 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
969 value v
= value_at (basetype
,
970 unpack_long (TYPE_FIELD_TYPE (type
, i
),
971 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8)));
974 return (char *) VALUE_CONTENTS (v
);
977 /* Not in the fields, so try looking through the baseclasses. */
978 for (i
= index
+1; i
< n_baseclasses
; i
++)
982 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
);
992 /* Baseclass is easily computed. */
995 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
998 /* Ugly hack to convert method stubs into method types.
1000 He ain't kiddin'. This demangles the name of the method into a string
1001 including argument types, parses out each argument type, generates
1002 a string casting a zero to that type, evaluates the string, and stuffs
1003 the resulting type into an argtype vector!!! Then it knows the type
1004 of the whole function (including argument types for overloading),
1005 which info used to be in the stab's but was removed to hack back
1006 the space required for them. */
1008 check_stub_method (type
, i
, j
)
1012 extern char *gdb_mangle_typename (), *strchr ();
1013 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1014 char *inner_name
= gdb_mangle_typename (type
);
1016 = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type
, i
))
1017 + strlen (inner_name
)
1018 + strlen (TYPE_FN_FIELD_PHYSNAME (f
, j
))
1020 char *demangled_name
, *cplus_demangle ();
1021 char *argtypetext
, *p
;
1022 int depth
= 0, argcount
= 1;
1023 struct type
**argtypes
;
1025 strcpy (mangled_name
, TYPE_FN_FIELDLIST_NAME (type
, i
));
1026 strcat (mangled_name
, inner_name
);
1027 strcat (mangled_name
, TYPE_FN_FIELD_PHYSNAME (f
, j
));
1028 demangled_name
= cplus_demangle (mangled_name
, 0);
1030 /* Now, read in the parameters that define this type. */
1031 argtypetext
= strchr (demangled_name
, '(') + 1;
1039 else if (*p
== ',' && depth
== 0)
1044 /* We need one more slot for the void [...] or NULL [end of arglist] */
1045 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1047 argtypes
[0] = lookup_pointer_type (type
);
1050 if (*p
!= ')') /* () means no args, skip while */
1059 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1061 char *tmp
= (char *)alloca (p
- argtypetext
+ 4);
1064 bcopy (argtypetext
, tmp
+1, p
- argtypetext
);
1065 tmp
[p
-argtypetext
+1] = ')';
1066 tmp
[p
-argtypetext
+2] = '0';
1067 tmp
[p
-argtypetext
+3] = '\0';
1068 val
= parse_and_eval (tmp
);
1069 argtypes
[argcount
] = VALUE_TYPE (val
);
1071 argtypetext
= p
+ 1;
1077 if (p
[-2] != '.') /* ... */
1078 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1080 argtypes
[argcount
] = NULL
; /* List terminator */
1082 free (demangled_name
);
1083 smash_to_method_type (TYPE_FN_FIELD_TYPE (f
, j
), type
,
1084 TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
1086 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1087 TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f
, j
)) &= ~TYPE_FLAG_STUB
;
1091 unpack_field_as_long (type
, valaddr
, fieldno
)
1097 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1098 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1100 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1101 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1103 /* Extracting bits depends on endianness of the machine. */
1104 #ifdef BITS_BIG_ENDIAN
1105 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1107 val
= val
>> (bitpos
% 8);
1110 val
&= (1 << bitsize
) - 1;
1114 /* Modify the value of a bitfield. ADDR points to a block of memory in
1115 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1116 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1117 indicate which bits (in target bit order) comprise the bitfield. */
1120 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1123 int bitpos
, bitsize
;
1127 /* Reject values too big to fit in the field in question.
1128 Otherwise adjoining fields may be corrupted. */
1129 if (fieldval
& ~((1<<bitsize
)-1))
1130 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1132 bcopy (addr
, &oword
, sizeof oword
);
1133 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1135 /* Shifting for bit field depends on endianness of the target machine. */
1136 #ifdef BITS_BIG_ENDIAN
1137 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1140 oword
&= ~(((1 << bitsize
) - 1) << bitpos
);
1141 oword
|= fieldval
<< bitpos
;
1143 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1144 bcopy (&oword
, addr
, sizeof oword
);
1147 /* Convert C numbers into newly allocated values */
1150 value_from_long (type
, num
)
1152 register LONGEST num
;
1154 register value val
= allocate_value (type
);
1155 register enum type_code code
= TYPE_CODE (type
);
1156 register int len
= TYPE_LENGTH (type
);
1158 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
)
1160 if (len
== sizeof (char))
1161 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1162 else if (len
== sizeof (short))
1163 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1164 else if (len
== sizeof (int))
1165 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1166 else if (len
== sizeof (long))
1167 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1169 else if (len
== sizeof (long long))
1170 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1173 error ("Integer type encountered with unexpected data length.");
1176 error ("Unexpected type encountered for integer constant.");
1178 /* num was in host byte order. So now put the value's contents
1179 into target byte order. */
1180 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1186 value_from_double (type
, num
)
1190 register value val
= allocate_value (type
);
1191 register enum type_code code
= TYPE_CODE (type
);
1192 register int len
= TYPE_LENGTH (type
);
1194 if (code
== TYPE_CODE_FLT
)
1196 if (len
== sizeof (float))
1197 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1198 else if (len
== sizeof (double))
1199 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1201 error ("Floating type encountered with unexpected data length.");
1204 error ("Unexpected type encountered for floating constant.");
1206 /* num was in host byte order. So now put the value's contents
1207 into target byte order. */
1208 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1213 /* Deal with the value that is "about to be returned". */
1215 /* Return the value that a function returning now
1216 would be returning to its caller, assuming its type is VALTYPE.
1217 RETBUF is where we look for what ought to be the contents
1218 of the registers (in raw form). This is because it is often
1219 desirable to restore old values to those registers
1220 after saving the contents of interest, and then call
1221 this function using the saved values.
1222 struct_return is non-zero when the function in question is
1223 using the structure return conventions on the machine in question;
1224 0 when it is using the value returning conventions (this often
1225 means returning pointer to where structure is vs. returning value). */
1228 value_being_returned (valtype
, retbuf
, struct_return
)
1229 register struct type
*valtype
;
1230 char retbuf
[REGISTER_BYTES
];
1237 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1238 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1239 if (struct_return
) {
1240 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1242 error ("Function return value unknown");
1243 return value_at (valtype
, addr
);
1247 val
= allocate_value (valtype
);
1248 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1253 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1254 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1255 and TYPE is the type (which is known to be struct, union or array).
1257 On most machines, the struct convention is used unless we are
1258 using gcc and the type is of a special size. */
1259 #if !defined (USE_STRUCT_CONVENTION)
1260 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1261 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1262 || TYPE_LENGTH (value_type) == 2 \
1263 || TYPE_LENGTH (value_type) == 4 \
1264 || TYPE_LENGTH (value_type) == 8 \
1269 /* Return true if the function specified is using the structure returning
1270 convention on this machine to return arguments, or 0 if it is using
1271 the value returning convention. FUNCTION is the value representing
1272 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1273 is the type returned by the function. GCC_P is nonzero if compiled
1277 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1280 struct type
*value_type
;
1284 register enum type_code code
= TYPE_CODE (value_type
);
1286 if (code
== TYPE_CODE_ERROR
)
1287 error ("Function return type unknown.");
1289 if (code
== TYPE_CODE_STRUCT
||
1290 code
== TYPE_CODE_UNION
||
1291 code
== TYPE_CODE_ARRAY
)
1292 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1297 /* Store VAL so it will be returned if a function returns now.
1298 Does not verify that VAL's type matches what the current
1299 function wants to return. */
1302 set_return_value (val
)
1305 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1309 if (code
== TYPE_CODE_ERROR
)
1310 error ("Function return type unknown.");
1312 if (code
== TYPE_CODE_STRUCT
1313 || code
== TYPE_CODE_UNION
)
1314 error ("Specifying a struct or union return value is not supported.");
1316 /* FIXME, this is bogus. We don't know what the return conventions
1317 are, or how values should be promoted.... */
1318 if (code
== TYPE_CODE_FLT
)
1320 dbuf
= value_as_double (val
);
1322 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1326 lbuf
= value_as_long (val
);
1327 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1332 _initialize_values ()
1334 add_info ("convenience", convenience_info
,
1335 "Debugger convenience (\"$foo\") variables.\n\
1336 These variables are created when you assign them values;\n\
1337 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1338 A few convenience variables are given values automatically:\n\
1339 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1340 \"$__\" holds the contents of the last address examined with \"x\".");
1342 add_info ("values", value_history_info
,
1343 "Elements of value history around item number IDX (or last ten).");
1344 add_info_alias ("history", "values", 0);