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