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. */
31 /* Local function prototypes. */
34 value_headof PARAMS ((value, struct type *, struct type *));
37 show_values PARAMS ((char *, int));
40 show_convenience PARAMS ((char *, int));
42 /* The value-history records all the values printed
43 by print commands during this session. Each chunk
44 records 60 consecutive values. The first chunk on
45 the chain records the most recent values.
46 The total number of values is in value_history_count. */
48 #define VALUE_HISTORY_CHUNK 60
50 struct value_history_chunk
52 struct value_history_chunk *next;
53 value values[VALUE_HISTORY_CHUNK];
56 /* Chain of chunks now in use. */
58 static struct value_history_chunk *value_history_chain;
60 static int value_history_count; /* Abs number of last entry stored */
62 /* List of all value objects currently allocated
63 (except for those released by calls to release_value)
64 This is so they can be freed after each command. */
66 static value all_values;
68 /* Allocate a value that has the correct length for type TYPE. */
76 check_stub_type (type);
78 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
79 VALUE_NEXT (val) = all_values;
81 VALUE_TYPE (val) = type;
82 VALUE_LVAL (val) = not_lval;
83 VALUE_ADDRESS (val) = 0;
84 VALUE_FRAME (val) = 0;
85 VALUE_OFFSET (val) = 0;
86 VALUE_BITPOS (val) = 0;
87 VALUE_BITSIZE (val) = 0;
88 VALUE_REPEATED (val) = 0;
89 VALUE_REPETITIONS (val) = 0;
90 VALUE_REGNO (val) = -1;
92 VALUE_OPTIMIZED_OUT (val) = 0;
96 /* Allocate a value that has the correct length
97 for COUNT repetitions type TYPE. */
100 allocate_repeat_value (type, count)
106 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
107 VALUE_NEXT (val) = all_values;
109 VALUE_TYPE (val) = type;
110 VALUE_LVAL (val) = not_lval;
111 VALUE_ADDRESS (val) = 0;
112 VALUE_FRAME (val) = 0;
113 VALUE_OFFSET (val) = 0;
114 VALUE_BITPOS (val) = 0;
115 VALUE_BITSIZE (val) = 0;
116 VALUE_REPEATED (val) = 1;
117 VALUE_REPETITIONS (val) = count;
118 VALUE_REGNO (val) = -1;
119 VALUE_LAZY (val) = 0;
120 VALUE_OPTIMIZED_OUT (val) = 0;
124 /* Return a mark in the value chain. All values allocated after the
125 mark is obtained (except for those released) are subject to being freed
126 if a subsequent value_free_to_mark is passed the mark. */
133 /* Free all values allocated since MARK was obtained by value_mark
134 (except for those released). */
136 value_free_to_mark (mark)
141 for (val = all_values; val && val != mark; val = next)
143 next = VALUE_NEXT (val);
149 /* Free all the values that have been allocated (except for those released).
150 Called after each command, successful or not. */
155 register value val, next;
157 for (val = all_values; val; val = next)
159 next = VALUE_NEXT (val);
166 /* Remove VAL from the chain all_values
167 so it will not be freed automatically. */
175 if (all_values == val)
177 all_values = val->next;
181 for (v = all_values; v; v = v->next)
191 /* Return a copy of the value ARG.
192 It contains the same contents, for same memory address,
193 but it's a different block of storage. */
200 register struct type *type = VALUE_TYPE (arg);
201 if (VALUE_REPEATED (arg))
202 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
204 val = allocate_value (type);
205 VALUE_LVAL (val) = VALUE_LVAL (arg);
206 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
207 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
208 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
209 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
210 VALUE_REGNO (val) = VALUE_REGNO (arg);
211 VALUE_LAZY (val) = VALUE_LAZY (arg);
212 if (!VALUE_LAZY (val))
214 bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
215 TYPE_LENGTH (VALUE_TYPE (arg))
216 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
221 /* Access to the value history. */
223 /* Record a new value in the value history.
224 Returns the absolute history index of the entry.
225 Result of -1 indicates the value was not saved; otherwise it is the
226 value history index of this new item. */
229 record_latest_value (val)
234 /* Check error now if about to store an invalid float. We return -1
235 to the caller, but allow them to continue, e.g. to print it as "Nan". */
236 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
237 (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
238 if (i) return -1; /* Indicate value not saved in history */
241 /* Here we treat value_history_count as origin-zero
242 and applying to the value being stored now. */
244 i = value_history_count % VALUE_HISTORY_CHUNK;
247 register struct value_history_chunk *new
248 = (struct value_history_chunk *)
249 xmalloc (sizeof (struct value_history_chunk));
250 bzero (new->values, sizeof new->values);
251 new->next = value_history_chain;
252 value_history_chain = new;
255 value_history_chain->values[i] = val;
258 /* Now we regard value_history_count as origin-one
259 and applying to the value just stored. */
261 return ++value_history_count;
264 /* Return a copy of the value in the history with sequence number NUM. */
267 access_value_history (num)
270 register struct value_history_chunk *chunk;
272 register int absnum = num;
275 absnum += value_history_count;
280 error ("The history is empty.");
282 error ("There is only one value in the history.");
284 error ("History does not go back to $$%d.", -num);
286 if (absnum > value_history_count)
287 error ("History has not yet reached $%d.", absnum);
291 /* Now absnum is always absolute and origin zero. */
293 chunk = value_history_chain;
294 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
298 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
301 /* Clear the value history entirely.
302 Must be done when new symbol tables are loaded,
303 because the type pointers become invalid. */
306 clear_value_history ()
308 register struct value_history_chunk *next;
312 while (value_history_chain)
314 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
315 if (val = value_history_chain->values[i])
317 next = value_history_chain->next;
318 free ((PTR)value_history_chain);
319 value_history_chain = next;
321 value_history_count = 0;
325 show_values (num_exp, from_tty)
335 if (num_exp[0] == '+' && num_exp[1] == '\0')
336 /* "info history +" should print from the stored position. */
339 /* "info history <exp>" should print around value number <exp>. */
340 num = parse_and_eval_address (num_exp) - 5;
344 /* "info history" means print the last 10 values. */
345 num = value_history_count - 9;
351 for (i = num; i < num + 10 && i <= value_history_count; i++)
353 val = access_value_history (i);
354 printf_filtered ("$%d = ", i);
355 value_print (val, stdout, 0, Val_pretty_default);
356 printf_filtered ("\n");
359 /* The next "info history +" should start after what we just printed. */
362 /* Hitting just return after this command should do the same thing as
363 "info history +". If num_exp is null, this is unnecessary, since
364 "info history +" is not useful after "info history". */
365 if (from_tty && num_exp)
372 /* Internal variables. These are variables within the debugger
373 that hold values assigned by debugger commands.
374 The user refers to them with a '$' prefix
375 that does not appear in the variable names stored internally. */
377 static struct internalvar *internalvars;
379 /* Look up an internal variable with name NAME. NAME should not
380 normally include a dollar sign.
382 If the specified internal variable does not exist,
383 one is created, with a void value. */
386 lookup_internalvar (name)
389 register struct internalvar *var;
391 for (var = internalvars; var; var = var->next)
392 if (!strcmp (var->name, name))
395 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
396 var->name = concat (name, NULL);
397 var->value = allocate_value (builtin_type_void);
398 release_value (var->value);
399 var->next = internalvars;
405 value_of_internalvar (var)
406 struct internalvar *var;
410 #ifdef IS_TRAPPED_INTERNALVAR
411 if (IS_TRAPPED_INTERNALVAR (var->name))
412 return VALUE_OF_TRAPPED_INTERNALVAR (var);
415 val = value_copy (var->value);
416 if (VALUE_LAZY (val))
417 value_fetch_lazy (val);
418 VALUE_LVAL (val) = lval_internalvar;
419 VALUE_INTERNALVAR (val) = var;
424 set_internalvar_component (var, offset, bitpos, bitsize, newval)
425 struct internalvar *var;
426 int offset, bitpos, bitsize;
429 register char *addr = VALUE_CONTENTS (var->value) + offset;
431 #ifdef IS_TRAPPED_INTERNALVAR
432 if (IS_TRAPPED_INTERNALVAR (var->name))
433 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
437 modify_field (addr, (int) value_as_long (newval),
440 bcopy (VALUE_CONTENTS (newval), addr,
441 TYPE_LENGTH (VALUE_TYPE (newval)));
445 set_internalvar (var, val)
446 struct internalvar *var;
449 #ifdef IS_TRAPPED_INTERNALVAR
450 if (IS_TRAPPED_INTERNALVAR (var->name))
451 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
454 free ((PTR)var->value);
455 var->value = value_copy (val);
456 /* Force the value to be fetched from the target now, to avoid problems
457 later when this internalvar is referenced and the target is gone or
459 if (VALUE_LAZY (var->value))
460 value_fetch_lazy (var->value);
461 release_value (var->value);
465 internalvar_name (var)
466 struct internalvar *var;
471 /* Free all internalvars. Done when new symtabs are loaded,
472 because that makes the values invalid. */
475 clear_internalvars ()
477 register struct internalvar *var;
482 internalvars = var->next;
483 free ((PTR)var->name);
484 free ((PTR)var->value);
490 show_convenience (ignore, from_tty)
494 register struct internalvar *var;
497 for (var = internalvars; var; var = var->next)
499 #ifdef IS_TRAPPED_INTERNALVAR
500 if (IS_TRAPPED_INTERNALVAR (var->name))
507 printf_filtered ("$%s = ", var->name);
508 value_print (var->value, stdout, 0, Val_pretty_default);
509 printf_filtered ("\n");
512 printf ("No debugger convenience variables now defined.\n\
513 Convenience variables have names starting with \"$\";\n\
514 use \"set\" as in \"set $foo = 5\" to define them.\n");
517 /* Extract a value as a C number (either long or double).
518 Knows how to convert fixed values to double, or
519 floating values to long.
520 Does not deallocate the value. */
526 /* This coerces arrays and functions, which is necessary (e.g.
527 in disassemble_command). It also dereferences references, which
528 I suspect is the most logical thing to do. */
529 if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
531 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
535 value_as_double (val)
541 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
543 error ("Invalid floating value found in program.");
546 /* Extract a value as a C pointer.
547 Does not deallocate the value. */
549 value_as_pointer (val)
552 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
553 whether we want this to be true eventually. */
554 return value_as_long (val);
557 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
558 as a long, or as a double, assuming the raw data is described
559 by type TYPE. Knows how to convert different sizes of values
560 and can convert between fixed and floating point. We don't assume
561 any alignment for the raw data. Return value is in host byte order.
563 If you want functions and arrays to be coerced to pointers, and
564 references to be dereferenced, call value_as_long() instead.
566 C++: It is assumed that the front-end has taken care of
567 all matters concerning pointers to members. A pointer
568 to member which reaches here is considered to be equivalent
569 to an INT (or some size). After all, it is only an offset. */
571 /* FIXME: This should be rewritten as a switch statement for speed and
572 ease of comprehension. */
575 unpack_long (type, valaddr)
579 register enum type_code code = TYPE_CODE (type);
580 register int len = TYPE_LENGTH (type);
581 register int nosign = TYPE_UNSIGNED (type);
583 if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
584 code = TYPE_CODE_INT;
585 if (code == TYPE_CODE_FLT)
587 if (len == sizeof (float))
590 bcopy (valaddr, &retval, sizeof (retval));
591 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
595 if (len == sizeof (double))
598 bcopy (valaddr, &retval, sizeof (retval));
599 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
604 error ("Unexpected type of floating point number.");
607 else if (code == TYPE_CODE_INT && nosign)
609 if (len == sizeof (char))
611 unsigned char retval = * (unsigned char *) valaddr;
612 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
616 if (len == sizeof (short))
618 unsigned short retval;
619 bcopy (valaddr, &retval, sizeof (retval));
620 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
624 if (len == sizeof (int))
627 bcopy (valaddr, &retval, sizeof (retval));
628 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
632 if (len == sizeof (long))
634 unsigned long retval;
635 bcopy (valaddr, &retval, sizeof (retval));
636 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
640 if (len == sizeof (long long))
642 unsigned long long retval;
643 bcopy (valaddr, &retval, sizeof (retval));
644 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
650 error ("That operation is not possible on an integer of that size.");
653 else if (code == TYPE_CODE_INT)
655 if (len == sizeof (char))
657 SIGNED char retval; /* plain chars might be unsigned on host */
658 bcopy (valaddr, &retval, sizeof (retval));
659 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
663 if (len == sizeof (short))
666 bcopy (valaddr, &retval, sizeof (retval));
667 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
671 if (len == sizeof (int))
674 bcopy (valaddr, &retval, sizeof (retval));
675 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
679 if (len == sizeof (long))
682 bcopy (valaddr, &retval, sizeof (retval));
683 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
688 if (len == sizeof (long long))
691 bcopy (valaddr, &retval, sizeof (retval));
692 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
698 error ("That operation is not possible on an integer of that size.");
701 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
702 whether we want this to be true eventually. */
703 else if (code == TYPE_CODE_PTR
704 || code == TYPE_CODE_REF)
706 if (len == sizeof(long))
709 bcopy (valaddr, &retval, sizeof(retval));
710 SWAP_TARGET_AND_HOST (&retval, sizeof(retval));
713 else if (len == sizeof(short))
716 bcopy (valaddr, &retval, len);
717 SWAP_TARGET_AND_HOST (&retval, len);
721 else if (code == TYPE_CODE_MEMBER)
722 error ("not implemented: member types in unpack_long");
723 else if (code == TYPE_CODE_CHAR)
724 return *(unsigned char *)valaddr;
726 error ("Value not integer or pointer.");
727 return 0; /* For lint -- never reached */
730 /* Return a double value from the specified type and address.
731 INVP points to an int which is set to 0 for valid value,
732 1 for invalid value (bad float format). In either case,
733 the returned double is OK to use. Argument is in target
734 format, result is in host format. */
737 unpack_double (type, valaddr, invp)
742 register enum type_code code = TYPE_CODE (type);
743 register int len = TYPE_LENGTH (type);
744 register int nosign = TYPE_UNSIGNED (type);
746 *invp = 0; /* Assume valid. */
747 if (code == TYPE_CODE_FLT)
749 if (INVALID_FLOAT (valaddr, len))
752 return 1.234567891011121314;
755 if (len == sizeof (float))
758 bcopy (valaddr, &retval, sizeof (retval));
759 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
763 if (len == sizeof (double))
766 bcopy (valaddr, &retval, sizeof (retval));
767 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
772 error ("Unexpected type of floating point number.");
773 return 0; /* Placate lint. */
777 /* Unsigned -- be sure we compensate for signed LONGEST. */
779 return (unsigned long long) unpack_long (type, valaddr);
781 return (unsigned long ) unpack_long (type, valaddr);
784 /* Signed -- we are OK with unpack_long. */
785 return unpack_long (type, valaddr);
789 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
790 as a CORE_ADDR, assuming the raw data is described by type TYPE.
791 We don't assume any alignment for the raw data. Return value is in
794 If you want functions and arrays to be coerced to pointers, and
795 references to be dereferenced, call value_as_pointer() instead.
797 C++: It is assumed that the front-end has taken care of
798 all matters concerning pointers to members. A pointer
799 to member which reaches here is considered to be equivalent
800 to an INT (or some size). After all, it is only an offset. */
803 unpack_pointer (type, valaddr)
808 /* The user should be able to use an int (e.g. 0x7892) in contexts
809 where a pointer is expected. So this doesn't do enough. */
810 register enum type_code code = TYPE_CODE (type);
811 register int len = TYPE_LENGTH (type);
813 if (code == TYPE_CODE_PTR
814 || code == TYPE_CODE_REF)
816 if (len == sizeof (CORE_ADDR))
819 bcopy (valaddr, &retval, sizeof (retval));
820 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
823 error ("Unrecognized pointer size.");
825 else if (code == TYPE_CODE_MEMBER)
826 error ("not implemented: member types in unpack_pointer");
828 error ("Value is not a pointer.");
829 return 0; /* For lint -- never reached */
831 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
832 whether we want this to be true eventually. */
833 return unpack_long (type, valaddr);
837 /* Given a value ARG1 (offset by OFFSET bytes)
838 of a struct or union type ARG_TYPE,
839 extract and return the value of one of its fields.
840 FIELDNO says which field.
842 For C++, must also be able to return values from static fields */
845 value_primitive_field (arg1, offset, fieldno, arg_type)
848 register int fieldno;
849 register struct type *arg_type;
852 register struct type *type;
854 check_stub_type (arg_type);
855 type = TYPE_FIELD_TYPE (arg_type, fieldno);
857 /* Handle packed fields */
859 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
860 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
862 v = value_from_longest (type,
863 unpack_field_as_long (arg_type,
864 VALUE_CONTENTS (arg1),
866 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
867 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
871 v = allocate_value (type);
872 if (VALUE_LAZY (arg1))
875 bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
876 VALUE_CONTENTS_RAW (v),
879 VALUE_LVAL (v) = VALUE_LVAL (arg1);
880 if (VALUE_LVAL (arg1) == lval_internalvar)
881 VALUE_LVAL (v) = lval_internalvar_component;
882 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
883 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
887 /* Given a value ARG1 of a struct or union type,
888 extract and return the value of one of its fields.
889 FIELDNO says which field.
891 For C++, must also be able to return values from static fields */
894 value_field (arg1, fieldno)
896 register int fieldno;
898 return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
901 /* Return a non-virtual function as a value.
902 F is the list of member functions which contains the desired method.
903 J is an index into F which provides the desired method. */
906 value_fn_field (f, j)
911 register struct type *type = TYPE_FN_FIELD_TYPE (f, j);
914 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
915 0, VAR_NAMESPACE, 0, NULL);
916 if (! sym) error ("Internal error: could not find physical method named %s",
917 TYPE_FN_FIELD_PHYSNAME (f, j));
919 v = allocate_value (type);
920 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
921 VALUE_TYPE (v) = type;
925 /* Return a virtual function as a value.
926 ARG1 is the object which provides the virtual function
927 table pointer. ARG1 is side-effected in calling this function.
928 F is the list of member functions which contains the desired virtual
930 J is an index into F which provides the desired virtual function.
932 TYPE is the type in which F is located. */
934 value_virtual_fn_field (arg1, f, j, type)
940 /* First, get the virtual function table pointer. That comes
941 with a strange type, so cast it to type `pointer to long' (which
942 should serve just fine as a function type). Then, index into
943 the table, and convert final value to appropriate function type. */
944 value entry, vfn, vtbl;
945 value vi = value_from_longest (builtin_type_int,
946 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
947 struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
948 struct type *context;
949 if (fcontext == NULL)
950 /* We don't have an fcontext (e.g. the program was compiled with
951 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
952 This won't work right for multiple inheritance, but at least we
953 should do as well as GDB 3.x did. */
954 fcontext = TYPE_VPTR_BASETYPE (type);
955 context = lookup_pointer_type (fcontext);
956 /* Now context is a pointer to the basetype containing the vtbl. */
957 if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
958 arg1 = value_ind (value_cast (context, value_addr (arg1)));
960 context = VALUE_TYPE (arg1);
961 /* Now context is the basetype containing the vtbl. */
963 /* This type may have been defined before its virtual function table
964 was. If so, fill in the virtual function table entry for the
966 if (TYPE_VPTR_FIELDNO (context) < 0)
967 fill_in_vptr_fieldno (context);
969 /* The virtual function table is now an array of structures
970 which have the form { int16 offset, delta; void *pfn; }. */
971 vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
973 /* Index into the virtual function table. This is hard-coded because
974 looking up a field is not cheap, and it may be important to save
975 time, e.g. if the user has set a conditional breakpoint calling
976 a virtual function. */
977 entry = value_subscript (vtbl, vi);
979 /* Move the `this' pointer according to the virtual function table. */
980 VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
981 if (! VALUE_LAZY (arg1))
983 VALUE_LAZY (arg1) = 1;
984 value_fetch_lazy (arg1);
987 vfn = value_field (entry, 2);
988 /* Reinstantiate the function pointer with the correct type. */
989 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
994 /* ARG is a pointer to an object we know to be at least
995 a DTYPE. BTYPE is the most derived basetype that has
996 already been searched (and need not be searched again).
997 After looking at the vtables between BTYPE and DTYPE,
998 return the most derived type we find. The caller must
999 be satisfied when the return value == DTYPE.
1001 FIXME-tiemann: should work with dossier entries as well. */
1004 value_headof (arg, btype, dtype)
1006 struct type *btype, *dtype;
1008 /* First collect the vtables we must look at for this object. */
1009 /* FIXME-tiemann: right now, just look at top-most vtable. */
1010 value vtbl, entry, best_entry = 0;
1011 /* FIXME: entry_type is never used. */
1012 struct type *entry_type;
1014 int offset, best_offset = 0;
1016 CORE_ADDR pc_for_sym;
1017 char *demangled_name;
1018 struct minimal_symbol *msymbol;
1020 btype = TYPE_VPTR_BASETYPE (dtype);
1021 check_stub_type (btype);
1023 vtbl = value_cast (lookup_pointer_type (btype), arg);
1026 vtbl = value_ind (value_field (value_ind (vtbl), TYPE_VPTR_FIELDNO (btype)));
1028 /* Check that VTBL looks like it points to a virtual function table. */
1029 msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
1031 || !VTBL_PREFIX_P (demangled_name = msymbol -> name))
1033 /* If we expected to find a vtable, but did not, let the user
1034 know that we aren't happy, but don't throw an error.
1035 FIXME: there has to be a better way to do this. */
1036 struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
1037 bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
1038 TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
1039 VALUE_TYPE (arg) = error_type;
1043 /* Now search through the virtual function table. */
1044 entry = value_ind (vtbl);
1045 nelems = longest_to_int (value_as_long (value_field (entry, 2)));
1046 for (i = 1; i <= nelems; i++)
1048 entry = value_subscript (vtbl, value_from_longest (builtin_type_int,
1050 offset = longest_to_int (value_as_long (value_field (entry, 0)));
1051 /* If we use '<=' we can handle single inheritance
1052 * where all offsets are zero - just use the first entry found. */
1053 if (offset <= best_offset)
1055 best_offset = offset;
1059 /* Move the pointer according to BEST_ENTRY's offset, and figure
1060 out what type we should return as the new pointer. */
1061 if (best_entry == 0)
1063 /* An alternative method (which should no longer be necessary).
1064 * But we leave it in for future use, when we will hopefully
1065 * have optimizes the vtable to use thunks instead of offsets. */
1066 /* Use the name of vtable itself to extract a base type. */
1067 demangled_name += 4; /* Skip _vt$ prefix. */
1071 pc_for_sym = value_as_pointer (value_field (best_entry, 2));
1072 sym = find_pc_function (pc_for_sym);
1073 demangled_name = cplus_demangle (SYMBOL_NAME (sym), 0);
1074 *(strchr (demangled_name, ':')) = '\0';
1076 sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
1078 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
1081 free (demangled_name);
1082 arg = value_add (value_cast (builtin_type_int, arg),
1083 value_field (best_entry, 0));
1085 VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
1089 /* ARG is a pointer object of type TYPE. If TYPE has virtual
1090 function tables, probe ARG's tables (including the vtables
1091 of its baseclasses) to figure out the most derived type that ARG
1092 could actually be a pointer to. */
1095 value_from_vtable_info (arg, type)
1099 /* Take care of preliminaries. */
1100 if (TYPE_VPTR_FIELDNO (type) < 0)
1101 fill_in_vptr_fieldno (type);
1102 if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
1105 return value_headof (arg, 0, type);
1108 /* Compute the address of the baseclass which is
1109 the INDEXth baseclass of class TYPE. The TYPE base
1110 of the object is at VALADDR.
1112 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1113 or 0 if no error. In that case the return value is not the address
1114 of the baseclasss, but the address which could not be read
1118 baseclass_addr (type, index, valaddr, valuep, errp)
1125 struct type *basetype = TYPE_BASECLASS (type, index);
1130 if (BASETYPE_VIA_VIRTUAL (type, index))
1132 /* Must hunt for the pointer to this virtual baseclass. */
1133 register int i, len = TYPE_NFIELDS (type);
1134 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1135 char *vbase_name, *type_name = type_name_no_tag (basetype);
1137 vbase_name = (char *)alloca (strlen (type_name) + 8);
1138 sprintf (vbase_name, "_vb$%s", type_name);
1139 /* First look for the virtual baseclass pointer
1141 for (i = n_baseclasses; i < len; i++)
1143 if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
1145 value val = allocate_value (basetype);
1150 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1151 valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
1153 status = target_read_memory (addr,
1154 VALUE_CONTENTS_RAW (val),
1155 TYPE_LENGTH (basetype));
1156 VALUE_LVAL (val) = lval_memory;
1157 VALUE_ADDRESS (val) = addr;
1163 release_value (val);
1167 return (char *)addr;
1173 return (char *) VALUE_CONTENTS (val);
1177 /* Not in the fields, so try looking through the baseclasses. */
1178 for (i = index+1; i < n_baseclasses; i++)
1182 baddr = baseclass_addr (type, i, valaddr, valuep, errp);
1192 /* Baseclass is easily computed. */
1195 return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1198 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1201 Extracting bits depends on endianness of the machine. Compute the
1202 number of least significant bits to discard. For big endian machines,
1203 we compute the total number of bits in the anonymous object, subtract
1204 off the bit count from the MSB of the object to the MSB of the
1205 bitfield, then the size of the bitfield, which leaves the LSB discard
1206 count. For little endian machines, the discard count is simply the
1207 number of bits from the LSB of the anonymous object to the LSB of the
1210 If the field is signed, we also do sign extension. */
1213 unpack_field_as_long (type, valaddr, fieldno)
1218 unsigned LONGEST val;
1219 unsigned LONGEST valmask;
1220 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1221 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1224 bcopy (valaddr + bitpos / 8, &val, sizeof (val));
1225 SWAP_TARGET_AND_HOST (&val, sizeof (val));
1227 /* Extract bits. See comment above. */
1230 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1232 lsbcount = (bitpos % 8);
1236 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1237 If the field is signed, and is negative, then sign extend. */
1239 if ((bitsize > 0) && (bitsize < 8 * sizeof (val)))
1241 valmask = (((unsigned LONGEST) 1) << bitsize) - 1;
1243 if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type, fieldno)))
1245 if (val & (valmask ^ (valmask >> 1)))
1254 /* Modify the value of a bitfield. ADDR points to a block of memory in
1255 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1256 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1257 indicate which bits (in target bit order) comprise the bitfield. */
1260 modify_field (addr, fieldval, bitpos, bitsize)
1263 int bitpos, bitsize;
1267 /* Reject values too big to fit in the field in question,
1268 otherwise adjoining fields may be corrupted. */
1269 if (bitsize < (8 * sizeof (fieldval))
1270 && 0 != (fieldval & ~((1<<bitsize)-1)))
1271 error ("Value %d does not fit in %d bits.", fieldval, bitsize);
1273 bcopy (addr, &oword, sizeof oword);
1274 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To host format */
1276 /* Shifting for bit field depends on endianness of the target machine. */
1278 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1281 /* Mask out old value, while avoiding shifts >= longword size */
1282 if (bitsize < 8 * sizeof (oword))
1283 oword &= ~(((((unsigned long)1) << bitsize) - 1) << bitpos);
1285 oword &= ~((-1) << bitpos);
1286 oword |= fieldval << bitpos;
1288 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To target format */
1289 bcopy (&oword, addr, sizeof oword);
1292 /* Convert C numbers into newly allocated values */
1295 value_from_longest (type, num)
1297 register LONGEST num;
1299 register value val = allocate_value (type);
1300 register enum type_code code = TYPE_CODE (type);
1301 register int len = TYPE_LENGTH (type);
1303 /* FIXME, we assume that pointers have the same form and byte order as
1304 integers, and that all pointers have the same form. */
1305 if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM ||
1306 code == TYPE_CODE_CHAR || code == TYPE_CODE_PTR ||
1307 code == TYPE_CODE_REF)
1309 if (len == sizeof (char))
1310 * (char *) VALUE_CONTENTS_RAW (val) = num;
1311 else if (len == sizeof (short))
1312 * (short *) VALUE_CONTENTS_RAW (val) = num;
1313 else if (len == sizeof (int))
1314 * (int *) VALUE_CONTENTS_RAW (val) = num;
1315 else if (len == sizeof (long))
1316 * (long *) VALUE_CONTENTS_RAW (val) = num;
1318 else if (len == sizeof (long long))
1319 * (long long *) VALUE_CONTENTS_RAW (val) = num;
1322 error ("Integer type encountered with unexpected data length.");
1325 error ("Unexpected type encountered for integer constant.");
1327 /* num was in host byte order. So now put the value's contents
1328 into target byte order. */
1329 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1335 value_from_double (type, num)
1339 register value val = allocate_value (type);
1340 register enum type_code code = TYPE_CODE (type);
1341 register int len = TYPE_LENGTH (type);
1343 if (code == TYPE_CODE_FLT)
1345 if (len == sizeof (float))
1346 * (float *) VALUE_CONTENTS_RAW (val) = num;
1347 else if (len == sizeof (double))
1348 * (double *) VALUE_CONTENTS_RAW (val) = num;
1350 error ("Floating type encountered with unexpected data length.");
1353 error ("Unexpected type encountered for floating constant.");
1355 /* num was in host byte order. So now put the value's contents
1356 into target byte order. */
1357 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1362 /* Deal with the value that is "about to be returned". */
1364 /* Return the value that a function returning now
1365 would be returning to its caller, assuming its type is VALTYPE.
1366 RETBUF is where we look for what ought to be the contents
1367 of the registers (in raw form). This is because it is often
1368 desirable to restore old values to those registers
1369 after saving the contents of interest, and then call
1370 this function using the saved values.
1371 struct_return is non-zero when the function in question is
1372 using the structure return conventions on the machine in question;
1373 0 when it is using the value returning conventions (this often
1374 means returning pointer to where structure is vs. returning value). */
1377 value_being_returned (valtype, retbuf, struct_return)
1378 register struct type *valtype;
1379 char retbuf[REGISTER_BYTES];
1386 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1387 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1388 if (struct_return) {
1389 addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1391 error ("Function return value unknown");
1392 return value_at (valtype, addr);
1396 val = allocate_value (valtype);
1397 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1402 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1403 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1404 and TYPE is the type (which is known to be struct, union or array).
1406 On most machines, the struct convention is used unless we are
1407 using gcc and the type is of a special size. */
1408 #if !defined (USE_STRUCT_CONVENTION)
1409 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1410 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1411 || TYPE_LENGTH (value_type) == 2 \
1412 || TYPE_LENGTH (value_type) == 4 \
1413 || TYPE_LENGTH (value_type) == 8 \
1418 /* Return true if the function specified is using the structure returning
1419 convention on this machine to return arguments, or 0 if it is using
1420 the value returning convention. FUNCTION is the value representing
1421 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1422 is the type returned by the function. GCC_P is nonzero if compiled
1426 using_struct_return (function, funcaddr, value_type, gcc_p)
1429 struct type *value_type;
1433 register enum type_code code = TYPE_CODE (value_type);
1435 if (code == TYPE_CODE_ERROR)
1436 error ("Function return type unknown.");
1438 if (code == TYPE_CODE_STRUCT ||
1439 code == TYPE_CODE_UNION ||
1440 code == TYPE_CODE_ARRAY)
1441 return USE_STRUCT_CONVENTION (gcc_p, value_type);
1446 /* Store VAL so it will be returned if a function returns now.
1447 Does not verify that VAL's type matches what the current
1448 function wants to return. */
1451 set_return_value (val)
1454 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1458 if (code == TYPE_CODE_ERROR)
1459 error ("Function return type unknown.");
1461 if ( code == TYPE_CODE_STRUCT
1462 || code == TYPE_CODE_UNION) /* FIXME, implement struct return. */
1463 error ("GDB does not support specifying a struct or union return value.");
1465 /* FIXME, this is bogus. We don't know what the return conventions
1466 are, or how values should be promoted.... */
1467 if (code == TYPE_CODE_FLT)
1469 dbuf = value_as_double (val);
1471 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
1475 lbuf = value_as_long (val);
1476 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
1481 _initialize_values ()
1483 add_cmd ("convenience", no_class, show_convenience,
1484 "Debugger convenience (\"$foo\") variables.\n\
1485 These variables are created when you assign them values;\n\
1486 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1487 A few convenience variables are given values automatically:\n\
1488 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1489 \"$__\" holds the contents of the last address examined with \"x\".",
1492 add_cmd ("values", no_class, show_values,
1493 "Elements of value history around item number IDX (or last ten).",