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. */
32 /* Local function prototypes. */
35 value_headof PARAMS ((value, struct type *, struct type *));
38 show_values PARAMS ((char *, int));
41 show_convenience PARAMS ((char *, int));
43 /* The value-history records all the values printed
44 by print commands during this session. Each chunk
45 records 60 consecutive values. The first chunk on
46 the chain records the most recent values.
47 The total number of values is in value_history_count. */
49 #define VALUE_HISTORY_CHUNK 60
51 struct value_history_chunk
53 struct value_history_chunk *next;
54 value values[VALUE_HISTORY_CHUNK];
57 /* Chain of chunks now in use. */
59 static struct value_history_chunk *value_history_chain;
61 static int value_history_count; /* Abs number of last entry stored */
63 /* List of all value objects currently allocated
64 (except for those released by calls to release_value)
65 This is so they can be freed after each command. */
67 static value all_values;
69 /* Allocate a value that has the correct length for type TYPE. */
77 check_stub_type (type);
79 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
80 VALUE_NEXT (val) = all_values;
82 VALUE_TYPE (val) = type;
83 VALUE_LVAL (val) = not_lval;
84 VALUE_ADDRESS (val) = 0;
85 VALUE_FRAME (val) = 0;
86 VALUE_OFFSET (val) = 0;
87 VALUE_BITPOS (val) = 0;
88 VALUE_BITSIZE (val) = 0;
89 VALUE_REPEATED (val) = 0;
90 VALUE_REPETITIONS (val) = 0;
91 VALUE_REGNO (val) = -1;
93 VALUE_OPTIMIZED_OUT (val) = 0;
97 /* Allocate a value that has the correct length
98 for COUNT repetitions type TYPE. */
101 allocate_repeat_value (type, count)
107 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
108 VALUE_NEXT (val) = all_values;
110 VALUE_TYPE (val) = type;
111 VALUE_LVAL (val) = not_lval;
112 VALUE_ADDRESS (val) = 0;
113 VALUE_FRAME (val) = 0;
114 VALUE_OFFSET (val) = 0;
115 VALUE_BITPOS (val) = 0;
116 VALUE_BITSIZE (val) = 0;
117 VALUE_REPEATED (val) = 1;
118 VALUE_REPETITIONS (val) = count;
119 VALUE_REGNO (val) = -1;
120 VALUE_LAZY (val) = 0;
121 VALUE_OPTIMIZED_OUT (val) = 0;
125 /* Return a mark in the value chain. All values allocated after the
126 mark is obtained (except for those released) are subject to being freed
127 if a subsequent value_free_to_mark is passed the mark. */
134 /* Free all values allocated since MARK was obtained by value_mark
135 (except for those released). */
137 value_free_to_mark (mark)
142 for (val = all_values; val && val != mark; val = next)
144 next = VALUE_NEXT (val);
150 /* Free all the values that have been allocated (except for those released).
151 Called after each command, successful or not. */
156 register value val, next;
158 for (val = all_values; val; val = next)
160 next = VALUE_NEXT (val);
167 /* Remove VAL from the chain all_values
168 so it will not be freed automatically. */
176 if (all_values == val)
178 all_values = val->next;
182 for (v = all_values; v; v = v->next)
192 /* Return a copy of the value ARG.
193 It contains the same contents, for same memory address,
194 but it's a different block of storage. */
201 register struct type *type = VALUE_TYPE (arg);
202 if (VALUE_REPEATED (arg))
203 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
205 val = allocate_value (type);
206 VALUE_LVAL (val) = VALUE_LVAL (arg);
207 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
208 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
209 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
210 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
211 VALUE_REGNO (val) = VALUE_REGNO (arg);
212 VALUE_LAZY (val) = VALUE_LAZY (arg);
213 if (!VALUE_LAZY (val))
215 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS_RAW (arg),
216 TYPE_LENGTH (VALUE_TYPE (arg))
217 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
222 /* Access to the value history. */
224 /* Record a new value in the value history.
225 Returns the absolute history index of the entry.
226 Result of -1 indicates the value was not saved; otherwise it is the
227 value history index of this new item. */
230 record_latest_value (val)
235 /* Check error now if about to store an invalid float. We return -1
236 to the caller, but allow them to continue, e.g. to print it as "Nan". */
237 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT)
239 unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
240 if (i) return -1; /* Indicate value not saved in history */
243 /* Here we treat value_history_count as origin-zero
244 and applying to the value being stored now. */
246 i = value_history_count % VALUE_HISTORY_CHUNK;
249 register struct value_history_chunk *new
250 = (struct value_history_chunk *)
251 xmalloc (sizeof (struct value_history_chunk));
252 memset (new->values, 0, sizeof new->values);
253 new->next = value_history_chain;
254 value_history_chain = new;
257 value_history_chain->values[i] = val;
259 /* We don't want this value to have anything to do with the inferior anymore.
260 In particular, "set $1 = 50" should not affect the variable from which
261 the value was taken, and fast watchpoints should be able to assume that
262 a value on the value history never changes. */
263 if (VALUE_LAZY (val))
264 value_fetch_lazy (val);
265 VALUE_LVAL (val) = not_lval;
268 /* Now we regard value_history_count as origin-one
269 and applying to the value just stored. */
271 return ++value_history_count;
274 /* Return a copy of the value in the history with sequence number NUM. */
277 access_value_history (num)
280 register struct value_history_chunk *chunk;
282 register int absnum = num;
285 absnum += value_history_count;
290 error ("The history is empty.");
292 error ("There is only one value in the history.");
294 error ("History does not go back to $$%d.", -num);
296 if (absnum > value_history_count)
297 error ("History has not yet reached $%d.", absnum);
301 /* Now absnum is always absolute and origin zero. */
303 chunk = value_history_chain;
304 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
308 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
311 /* Clear the value history entirely.
312 Must be done when new symbol tables are loaded,
313 because the type pointers become invalid. */
316 clear_value_history ()
318 register struct value_history_chunk *next;
322 while (value_history_chain)
324 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
325 if ((val = value_history_chain->values[i]) != NULL)
327 next = value_history_chain->next;
328 free ((PTR)value_history_chain);
329 value_history_chain = next;
331 value_history_count = 0;
335 show_values (num_exp, from_tty)
345 /* "info history +" should print from the stored position.
346 "info history <exp>" should print around value number <exp>. */
347 if (num_exp[0] != '+' || num_exp[1] != '\0')
348 num = parse_and_eval_address (num_exp) - 5;
352 /* "info history" means print the last 10 values. */
353 num = value_history_count - 9;
359 for (i = num; i < num + 10 && i <= value_history_count; i++)
361 val = access_value_history (i);
362 printf_filtered ("$%d = ", i);
363 value_print (val, stdout, 0, Val_pretty_default);
364 printf_filtered ("\n");
367 /* The next "info history +" should start after what we just printed. */
370 /* Hitting just return after this command should do the same thing as
371 "info history +". If num_exp is null, this is unnecessary, since
372 "info history +" is not useful after "info history". */
373 if (from_tty && num_exp)
380 /* Internal variables. These are variables within the debugger
381 that hold values assigned by debugger commands.
382 The user refers to them with a '$' prefix
383 that does not appear in the variable names stored internally. */
385 static struct internalvar *internalvars;
387 /* Look up an internal variable with name NAME. NAME should not
388 normally include a dollar sign.
390 If the specified internal variable does not exist,
391 one is created, with a void value. */
394 lookup_internalvar (name)
397 register struct internalvar *var;
399 for (var = internalvars; var; var = var->next)
400 if (STREQ (var->name, name))
403 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
404 var->name = concat (name, NULL);
405 var->value = allocate_value (builtin_type_void);
406 release_value (var->value);
407 var->next = internalvars;
413 value_of_internalvar (var)
414 struct internalvar *var;
418 #ifdef IS_TRAPPED_INTERNALVAR
419 if (IS_TRAPPED_INTERNALVAR (var->name))
420 return VALUE_OF_TRAPPED_INTERNALVAR (var);
423 val = value_copy (var->value);
424 if (VALUE_LAZY (val))
425 value_fetch_lazy (val);
426 VALUE_LVAL (val) = lval_internalvar;
427 VALUE_INTERNALVAR (val) = var;
432 set_internalvar_component (var, offset, bitpos, bitsize, newval)
433 struct internalvar *var;
434 int offset, bitpos, bitsize;
437 register char *addr = VALUE_CONTENTS (var->value) + offset;
439 #ifdef IS_TRAPPED_INTERNALVAR
440 if (IS_TRAPPED_INTERNALVAR (var->name))
441 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
445 modify_field (addr, value_as_long (newval),
448 memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval)));
452 set_internalvar (var, val)
453 struct internalvar *var;
456 #ifdef IS_TRAPPED_INTERNALVAR
457 if (IS_TRAPPED_INTERNALVAR (var->name))
458 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
461 free ((PTR)var->value);
462 var->value = value_copy (val);
463 /* Force the value to be fetched from the target now, to avoid problems
464 later when this internalvar is referenced and the target is gone or
466 if (VALUE_LAZY (var->value))
467 value_fetch_lazy (var->value);
468 release_value (var->value);
472 internalvar_name (var)
473 struct internalvar *var;
478 /* Free all internalvars. Done when new symtabs are loaded,
479 because that makes the values invalid. */
482 clear_internalvars ()
484 register struct internalvar *var;
489 internalvars = var->next;
490 free ((PTR)var->name);
491 free ((PTR)var->value);
497 show_convenience (ignore, from_tty)
501 register struct internalvar *var;
504 for (var = internalvars; var; var = var->next)
506 #ifdef IS_TRAPPED_INTERNALVAR
507 if (IS_TRAPPED_INTERNALVAR (var->name))
514 printf_filtered ("$%s = ", var->name);
515 value_print (var->value, stdout, 0, Val_pretty_default);
516 printf_filtered ("\n");
519 printf ("No debugger convenience variables now defined.\n\
520 Convenience variables have names starting with \"$\";\n\
521 use \"set\" as in \"set $foo = 5\" to define them.\n");
524 /* Extract a value as a C number (either long or double).
525 Knows how to convert fixed values to double, or
526 floating values to long.
527 Does not deallocate the value. */
533 /* This coerces arrays and functions, which is necessary (e.g.
534 in disassemble_command). It also dereferences references, which
535 I suspect is the most logical thing to do. */
536 if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
538 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
542 value_as_double (val)
548 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
550 error ("Invalid floating value found in program.");
553 /* Extract a value as a C pointer.
554 Does not deallocate the value. */
556 value_as_pointer (val)
559 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
560 whether we want this to be true eventually. */
562 /* ADDR_BITS_REMOVE is wrong if we are being called for a
563 non-address (e.g. argument to "signal", "info break", etc.), or
564 for pointers to char, in which the low bits *are* significant. */
565 return ADDR_BITS_REMOVE(value_as_long (val));
567 return value_as_long (val);
571 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
572 as a long, or as a double, assuming the raw data is described
573 by type TYPE. Knows how to convert different sizes of values
574 and can convert between fixed and floating point. We don't assume
575 any alignment for the raw data. Return value is in host byte order.
577 If you want functions and arrays to be coerced to pointers, and
578 references to be dereferenced, call value_as_long() instead.
580 C++: It is assumed that the front-end has taken care of
581 all matters concerning pointers to members. A pointer
582 to member which reaches here is considered to be equivalent
583 to an INT (or some size). After all, it is only an offset. */
585 /* FIXME: This should be rewritten as a switch statement for speed and
586 ease of comprehension. */
589 unpack_long (type, valaddr)
593 register enum type_code code = TYPE_CODE (type);
594 register int len = TYPE_LENGTH (type);
595 register int nosign = TYPE_UNSIGNED (type);
597 if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
598 code = TYPE_CODE_INT;
599 if (code == TYPE_CODE_FLT)
601 if (len == sizeof (float))
604 memcpy (&retval, valaddr, sizeof (retval));
605 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
609 if (len == sizeof (double))
612 memcpy (&retval, valaddr, sizeof (retval));
613 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
618 error ("Unexpected type of floating point number.");
621 else if ((code == TYPE_CODE_INT || code == TYPE_CODE_CHAR) && nosign)
623 return extract_unsigned_integer (valaddr, len);
625 else if (code == TYPE_CODE_INT || code == TYPE_CODE_CHAR)
627 return extract_signed_integer (valaddr, len);
629 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
630 whether we want this to be true eventually. */
631 else if (code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
633 return extract_address (valaddr, len);
635 else if (code == TYPE_CODE_MEMBER)
636 error ("not implemented: member types in unpack_long");
638 error ("Value not integer or pointer.");
639 return 0; /* For lint -- never reached */
642 /* Return a double value from the specified type and address.
643 INVP points to an int which is set to 0 for valid value,
644 1 for invalid value (bad float format). In either case,
645 the returned double is OK to use. Argument is in target
646 format, result is in host format. */
649 unpack_double (type, valaddr, invp)
654 register enum type_code code = TYPE_CODE (type);
655 register int len = TYPE_LENGTH (type);
656 register int nosign = TYPE_UNSIGNED (type);
658 *invp = 0; /* Assume valid. */
659 if (code == TYPE_CODE_FLT)
661 if (INVALID_FLOAT (valaddr, len))
664 return 1.234567891011121314;
667 if (len == sizeof (float))
670 memcpy (&retval, valaddr, sizeof (retval));
671 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
675 if (len == sizeof (double))
678 memcpy (&retval, valaddr, sizeof (retval));
679 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
684 error ("Unexpected type of floating point number.");
685 return 0; /* Placate lint. */
689 /* Unsigned -- be sure we compensate for signed LONGEST. */
690 return (unsigned LONGEST) unpack_long (type, valaddr);
692 /* Signed -- we are OK with unpack_long. */
693 return unpack_long (type, valaddr);
697 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
698 as a CORE_ADDR, assuming the raw data is described by type TYPE.
699 We don't assume any alignment for the raw data. Return value is in
702 If you want functions and arrays to be coerced to pointers, and
703 references to be dereferenced, call value_as_pointer() instead.
705 C++: It is assumed that the front-end has taken care of
706 all matters concerning pointers to members. A pointer
707 to member which reaches here is considered to be equivalent
708 to an INT (or some size). After all, it is only an offset. */
711 unpack_pointer (type, valaddr)
715 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
716 whether we want this to be true eventually. */
717 return unpack_long (type, valaddr);
720 /* Given a value ARG1 (offset by OFFSET bytes)
721 of a struct or union type ARG_TYPE,
722 extract and return the value of one of its fields.
723 FIELDNO says which field.
725 For C++, must also be able to return values from static fields */
728 value_primitive_field (arg1, offset, fieldno, arg_type)
731 register int fieldno;
732 register struct type *arg_type;
735 register struct type *type;
737 check_stub_type (arg_type);
738 type = TYPE_FIELD_TYPE (arg_type, fieldno);
740 /* Handle packed fields */
742 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
743 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
745 v = value_from_longest (type,
746 unpack_field_as_long (arg_type,
747 VALUE_CONTENTS (arg1),
749 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
750 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
754 v = allocate_value (type);
755 if (VALUE_LAZY (arg1))
758 memcpy (VALUE_CONTENTS_RAW (v), VALUE_CONTENTS_RAW (arg1) + offset,
761 VALUE_LVAL (v) = VALUE_LVAL (arg1);
762 if (VALUE_LVAL (arg1) == lval_internalvar)
763 VALUE_LVAL (v) = lval_internalvar_component;
764 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
765 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
769 /* Given a value ARG1 of a struct or union type,
770 extract and return the value of one of its fields.
771 FIELDNO says which field.
773 For C++, must also be able to return values from static fields */
776 value_field (arg1, fieldno)
778 register int fieldno;
780 return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
783 /* Return a non-virtual function as a value.
784 F is the list of member functions which contains the desired method.
785 J is an index into F which provides the desired method. */
788 value_fn_field (arg1p, f, j, type, offset)
796 register struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
799 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
800 0, VAR_NAMESPACE, 0, NULL);
801 if (! sym) error ("Internal error: could not find physical method named %s",
802 TYPE_FN_FIELD_PHYSNAME (f, j));
804 v = allocate_value (ftype);
805 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
806 VALUE_TYPE (v) = ftype;
810 if (type != VALUE_TYPE (*arg1p))
811 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
812 value_addr (*arg1p)));
814 /* Move the `this' pointer according to the offset. */
815 VALUE_OFFSET (*arg1p) += offset;
821 /* Return a virtual function as a value.
822 ARG1 is the object which provides the virtual function
823 table pointer. *ARG1P is side-effected in calling this function.
824 F is the list of member functions which contains the desired virtual
826 J is an index into F which provides the desired virtual function.
828 TYPE is the type in which F is located. */
830 value_virtual_fn_field (arg1p, f, j, type, offset)
838 /* First, get the virtual function table pointer. That comes
839 with a strange type, so cast it to type `pointer to long' (which
840 should serve just fine as a function type). Then, index into
841 the table, and convert final value to appropriate function type. */
842 value entry, vfn, vtbl;
843 value vi = value_from_longest (builtin_type_int,
844 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
845 struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
846 struct type *context;
847 if (fcontext == NULL)
848 /* We don't have an fcontext (e.g. the program was compiled with
849 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
850 This won't work right for multiple inheritance, but at least we
851 should do as well as GDB 3.x did. */
852 fcontext = TYPE_VPTR_BASETYPE (type);
853 context = lookup_pointer_type (fcontext);
854 /* Now context is a pointer to the basetype containing the vtbl. */
855 if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
856 arg1 = value_ind (value_cast (context, value_addr (arg1)));
858 context = VALUE_TYPE (arg1);
859 /* Now context is the basetype containing the vtbl. */
861 /* This type may have been defined before its virtual function table
862 was. If so, fill in the virtual function table entry for the
864 if (TYPE_VPTR_FIELDNO (context) < 0)
865 fill_in_vptr_fieldno (context);
867 /* The virtual function table is now an array of structures
868 which have the form { int16 offset, delta; void *pfn; }. */
869 vtbl = value_ind (value_primitive_field (arg1, 0,
870 TYPE_VPTR_FIELDNO (context),
871 TYPE_VPTR_BASETYPE (context)));
873 /* Index into the virtual function table. This is hard-coded because
874 looking up a field is not cheap, and it may be important to save
875 time, e.g. if the user has set a conditional breakpoint calling
876 a virtual function. */
877 entry = value_subscript (vtbl, vi);
879 /* Move the `this' pointer according to the virtual function table. */
880 VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0)) + offset;
881 if (! VALUE_LAZY (arg1))
883 VALUE_LAZY (arg1) = 1;
884 value_fetch_lazy (arg1);
887 vfn = value_field (entry, 2);
888 /* Reinstantiate the function pointer with the correct type. */
889 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
895 /* ARG is a pointer to an object we know to be at least
896 a DTYPE. BTYPE is the most derived basetype that has
897 already been searched (and need not be searched again).
898 After looking at the vtables between BTYPE and DTYPE,
899 return the most derived type we find. The caller must
900 be satisfied when the return value == DTYPE.
902 FIXME-tiemann: should work with dossier entries as well. */
905 value_headof (in_arg, btype, dtype)
907 struct type *btype, *dtype;
909 /* First collect the vtables we must look at for this object. */
910 /* FIXME-tiemann: right now, just look at top-most vtable. */
911 value arg, vtbl, entry, best_entry = 0;
913 int offset, best_offset = 0;
915 CORE_ADDR pc_for_sym;
916 char *demangled_name;
917 struct minimal_symbol *msymbol;
919 btype = TYPE_VPTR_BASETYPE (dtype);
920 check_stub_type (btype);
923 arg = value_cast (lookup_pointer_type (btype), arg);
924 vtbl = value_ind (value_field (value_ind (arg), TYPE_VPTR_FIELDNO (btype)));
926 /* Check that VTBL looks like it points to a virtual function table. */
927 msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
929 || !VTBL_PREFIX_P (demangled_name = SYMBOL_NAME (msymbol)))
931 /* If we expected to find a vtable, but did not, let the user
932 know that we aren't happy, but don't throw an error.
933 FIXME: there has to be a better way to do this. */
934 struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
935 memcpy (error_type, VALUE_TYPE (in_arg), sizeof (struct type));
936 TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
937 VALUE_TYPE (in_arg) = error_type;
941 /* Now search through the virtual function table. */
942 entry = value_ind (vtbl);
943 nelems = longest_to_int (value_as_long (value_field (entry, 2)));
944 for (i = 1; i <= nelems; i++)
946 entry = value_subscript (vtbl, value_from_longest (builtin_type_int,
948 offset = longest_to_int (value_as_long (value_field (entry, 0)));
949 /* If we use '<=' we can handle single inheritance
950 * where all offsets are zero - just use the first entry found. */
951 if (offset <= best_offset)
953 best_offset = offset;
957 /* Move the pointer according to BEST_ENTRY's offset, and figure
958 out what type we should return as the new pointer. */
961 /* An alternative method (which should no longer be necessary).
962 * But we leave it in for future use, when we will hopefully
963 * have optimizes the vtable to use thunks instead of offsets. */
964 /* Use the name of vtable itself to extract a base type. */
965 demangled_name += 4; /* Skip _vt$ prefix. */
969 pc_for_sym = value_as_pointer (value_field (best_entry, 2));
970 sym = find_pc_function (pc_for_sym);
971 demangled_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ANSI);
972 *(strchr (demangled_name, ':')) = '\0';
974 sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
976 error ("could not find type declaration for `%s'", demangled_name);
979 free (demangled_name);
980 arg = value_add (value_cast (builtin_type_int, arg),
981 value_field (best_entry, 0));
984 VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
988 /* ARG is a pointer object of type TYPE. If TYPE has virtual
989 function tables, probe ARG's tables (including the vtables
990 of its baseclasses) to figure out the most derived type that ARG
991 could actually be a pointer to. */
994 value_from_vtable_info (arg, type)
998 /* Take care of preliminaries. */
999 if (TYPE_VPTR_FIELDNO (type) < 0)
1000 fill_in_vptr_fieldno (type);
1001 if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
1004 return value_headof (arg, 0, type);
1007 /* Return true if the INDEXth field of TYPE is a virtual baseclass
1008 pointer which is for the base class whose type is BASECLASS. */
1011 vb_match (type, index, basetype)
1014 struct type *basetype;
1016 struct type *fieldtype;
1017 char *name = TYPE_FIELD_NAME (type, index);
1018 char *field_class_name = NULL;
1022 /* gcc 2.4 uses _vb$. */
1023 if (name[1] == 'v' && name[2] == 'b' && name[3] == CPLUS_MARKER)
1024 field_class_name = name + 4;
1025 /* gcc 2.5 will use __vb_. */
1026 if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_')
1027 field_class_name = name + 5;
1029 if (field_class_name == NULL)
1030 /* This field is not a virtual base class pointer. */
1033 /* It's a virtual baseclass pointer, now we just need to find out whether
1034 it is for this baseclass. */
1035 fieldtype = TYPE_FIELD_TYPE (type, index);
1036 if (fieldtype == NULL
1037 || TYPE_CODE (fieldtype) != TYPE_CODE_PTR)
1038 /* "Can't happen". */
1041 /* What we check for is that either the types are equal (needed for
1042 nameless types) or have the same name. This is ugly, and a more
1043 elegant solution should be devised (which would probably just push
1044 the ugliness into symbol reading unless we change the stabs format). */
1045 if (TYPE_TARGET_TYPE (fieldtype) == basetype)
1048 if (TYPE_NAME (basetype) != NULL
1049 && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL
1050 && STREQ (TYPE_NAME (basetype),
1051 TYPE_NAME (TYPE_TARGET_TYPE (fieldtype))))
1056 /* Compute the offset of the baseclass which is
1057 the INDEXth baseclass of class TYPE, for a value ARG,
1058 wih extra offset of OFFSET.
1059 The result is the offste of the baseclass value relative
1060 to (the address of)(ARG) + OFFSET.
1062 -1 is returned on error. */
1065 baseclass_offset (type, index, arg, offset)
1071 struct type *basetype = TYPE_BASECLASS (type, index);
1073 if (BASETYPE_VIA_VIRTUAL (type, index))
1075 /* Must hunt for the pointer to this virtual baseclass. */
1076 register int i, len = TYPE_NFIELDS (type);
1077 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1079 /* First look for the virtual baseclass pointer
1081 for (i = n_baseclasses; i < len; i++)
1083 if (vb_match (type, i, basetype))
1086 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1087 VALUE_CONTENTS (arg) + VALUE_OFFSET (arg)
1089 + (TYPE_FIELD_BITPOS (type, i) / 8));
1091 if (VALUE_LVAL (arg) != lval_memory)
1095 (LONGEST) (VALUE_ADDRESS (arg) + VALUE_OFFSET (arg) + offset);
1098 /* Not in the fields, so try looking through the baseclasses. */
1099 for (i = index+1; i < n_baseclasses; i++)
1102 baseclass_offset (type, i, arg, offset);
1110 /* Baseclass is easily computed. */
1111 return TYPE_BASECLASS_BITPOS (type, index) / 8;
1114 /* Compute the address of the baseclass which is
1115 the INDEXth baseclass of class TYPE. The TYPE base
1116 of the object is at VALADDR.
1118 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1119 or 0 if no error. In that case the return value is not the address
1120 of the baseclasss, but the address which could not be read
1123 /* FIXME Fix remaining uses of baseclass_addr to use baseclass_offset */
1126 baseclass_addr (type, index, valaddr, valuep, errp)
1133 struct type *basetype = TYPE_BASECLASS (type, index);
1138 if (BASETYPE_VIA_VIRTUAL (type, index))
1140 /* Must hunt for the pointer to this virtual baseclass. */
1141 register int i, len = TYPE_NFIELDS (type);
1142 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1144 /* First look for the virtual baseclass pointer
1146 for (i = n_baseclasses; i < len; i++)
1148 if (vb_match (type, i, basetype))
1150 value val = allocate_value (basetype);
1155 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1156 valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
1158 status = target_read_memory (addr,
1159 VALUE_CONTENTS_RAW (val),
1160 TYPE_LENGTH (basetype));
1161 VALUE_LVAL (val) = lval_memory;
1162 VALUE_ADDRESS (val) = addr;
1168 release_value (val);
1172 return (char *)addr;
1178 return (char *) VALUE_CONTENTS (val);
1182 /* Not in the fields, so try looking through the baseclasses. */
1183 for (i = index+1; i < n_baseclasses; i++)
1187 baddr = baseclass_addr (type, i, valaddr, valuep, errp);
1197 /* Baseclass is easily computed. */
1200 return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1203 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1206 Extracting bits depends on endianness of the machine. Compute the
1207 number of least significant bits to discard. For big endian machines,
1208 we compute the total number of bits in the anonymous object, subtract
1209 off the bit count from the MSB of the object to the MSB of the
1210 bitfield, then the size of the bitfield, which leaves the LSB discard
1211 count. For little endian machines, the discard count is simply the
1212 number of bits from the LSB of the anonymous object to the LSB of the
1215 If the field is signed, we also do sign extension. */
1218 unpack_field_as_long (type, valaddr, fieldno)
1223 unsigned LONGEST val;
1224 unsigned LONGEST valmask;
1225 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1226 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1229 val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1231 /* Extract bits. See comment above. */
1234 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1236 lsbcount = (bitpos % 8);
1240 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1241 If the field is signed, and is negative, then sign extend. */
1243 if ((bitsize > 0) && (bitsize < 8 * sizeof (val)))
1245 valmask = (((unsigned LONGEST) 1) << bitsize) - 1;
1247 if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type, fieldno)))
1249 if (val & (valmask ^ (valmask >> 1)))
1258 /* Modify the value of a bitfield. ADDR points to a block of memory in
1259 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1260 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1261 indicate which bits (in target bit order) comprise the bitfield. */
1264 modify_field (addr, fieldval, bitpos, bitsize)
1267 int bitpos, bitsize;
1271 /* Reject values too big to fit in the field in question,
1272 otherwise adjoining fields may be corrupted. */
1273 if (bitsize < (8 * sizeof (fieldval))
1274 && 0 != (fieldval & ~((1<<bitsize)-1)))
1276 /* FIXME: would like to include fieldval in the message, but
1277 we don't have a sprintf_longest. */
1278 error ("Value does not fit in %d bits.", bitsize);
1281 oword = extract_signed_integer (addr, sizeof oword);
1283 /* Shifting for bit field depends on endianness of the target machine. */
1285 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1288 /* Mask out old value, while avoiding shifts >= size of oword */
1289 if (bitsize < 8 * sizeof (oword))
1290 oword &= ~(((((unsigned LONGEST)1) << bitsize) - 1) << bitpos);
1292 oword &= ~((~(unsigned LONGEST)0) << bitpos);
1293 oword |= fieldval << bitpos;
1295 store_signed_integer (addr, sizeof oword, oword);
1298 /* Convert C numbers into newly allocated values */
1301 value_from_longest (type, num)
1303 register LONGEST num;
1305 register value val = allocate_value (type);
1306 register enum type_code code = TYPE_CODE (type);
1307 register int len = TYPE_LENGTH (type);
1312 case TYPE_CODE_CHAR:
1313 case TYPE_CODE_ENUM:
1314 case TYPE_CODE_BOOL:
1315 store_signed_integer (VALUE_CONTENTS_RAW (val), len, num);
1320 /* This assumes that all pointers of a given length
1321 have the same form. */
1322 store_address (VALUE_CONTENTS_RAW (val), len, (CORE_ADDR) num);
1326 error ("Unexpected type encountered for integer constant.");
1332 value_from_double (type, num)
1336 register value val = allocate_value (type);
1337 register enum type_code code = TYPE_CODE (type);
1338 register int len = TYPE_LENGTH (type);
1340 if (code == TYPE_CODE_FLT)
1342 if (len == sizeof (float))
1343 * (float *) VALUE_CONTENTS_RAW (val) = num;
1344 else if (len == sizeof (double))
1345 * (double *) VALUE_CONTENTS_RAW (val) = num;
1347 error ("Floating type encountered with unexpected data length.");
1350 error ("Unexpected type encountered for floating constant.");
1352 /* num was in host byte order. So now put the value's contents
1353 into target byte order. */
1354 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1359 /* Deal with the value that is "about to be returned". */
1361 /* Return the value that a function returning now
1362 would be returning to its caller, assuming its type is VALTYPE.
1363 RETBUF is where we look for what ought to be the contents
1364 of the registers (in raw form). This is because it is often
1365 desirable to restore old values to those registers
1366 after saving the contents of interest, and then call
1367 this function using the saved values.
1368 struct_return is non-zero when the function in question is
1369 using the structure return conventions on the machine in question;
1370 0 when it is using the value returning conventions (this often
1371 means returning pointer to where structure is vs. returning value). */
1374 value_being_returned (valtype, retbuf, struct_return)
1375 register struct type *valtype;
1376 char retbuf[REGISTER_BYTES];
1383 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1384 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1385 if (struct_return) {
1386 addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1388 error ("Function return value unknown");
1389 return value_at (valtype, addr);
1393 val = allocate_value (valtype);
1394 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1399 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1400 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1401 and TYPE is the type (which is known to be struct, union or array).
1403 On most machines, the struct convention is used unless we are
1404 using gcc and the type is of a special size. */
1405 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1406 native compiler. GCC 2.3.3 was the last release that did it the
1407 old way. Since gcc2_compiled was not changed, we have no
1408 way to correctly win in all cases, so we just do the right thing
1409 for gcc1 and for gcc2 after this change. Thus it loses for gcc
1410 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled
1411 would cause more chaos than dealing with some struct returns being
1413 #if !defined (USE_STRUCT_CONVENTION)
1414 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1415 (!((gcc_p == 1) && (TYPE_LENGTH (value_type) == 1 \
1416 || TYPE_LENGTH (value_type) == 2 \
1417 || TYPE_LENGTH (value_type) == 4 \
1418 || TYPE_LENGTH (value_type) == 8 \
1423 /* Return true if the function specified is using the structure returning
1424 convention on this machine to return arguments, or 0 if it is using
1425 the value returning convention. FUNCTION is the value representing
1426 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1427 is the type returned by the function. GCC_P is nonzero if compiled
1431 using_struct_return (function, funcaddr, value_type, gcc_p)
1434 struct type *value_type;
1438 register enum type_code code = TYPE_CODE (value_type);
1440 if (code == TYPE_CODE_ERROR)
1441 error ("Function return type unknown.");
1443 if (code == TYPE_CODE_STRUCT ||
1444 code == TYPE_CODE_UNION ||
1445 code == TYPE_CODE_ARRAY)
1446 return USE_STRUCT_CONVENTION (gcc_p, value_type);
1451 /* Store VAL so it will be returned if a function returns now.
1452 Does not verify that VAL's type matches what the current
1453 function wants to return. */
1456 set_return_value (val)
1459 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1463 if (code == TYPE_CODE_ERROR)
1464 error ("Function return type unknown.");
1466 if ( code == TYPE_CODE_STRUCT
1467 || code == TYPE_CODE_UNION) /* FIXME, implement struct return. */
1468 error ("GDB does not support specifying a struct or union return value.");
1470 /* FIXME, this is bogus. We don't know what the return conventions
1471 are, or how values should be promoted.... */
1472 if (code == TYPE_CODE_FLT)
1474 dbuf = value_as_double (val);
1476 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
1480 lbuf = value_as_long (val);
1481 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
1486 _initialize_values ()
1488 add_cmd ("convenience", no_class, show_convenience,
1489 "Debugger convenience (\"$foo\") variables.\n\
1490 These variables are created when you assign them values;\n\
1491 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1492 A few convenience variables are given values automatically:\n\
1493 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1494 \"$__\" holds the contents of the last address examined with \"x\".",
1497 add_cmd ("values", no_class, show_values,
1498 "Elements of value history around item number IDX (or last ten).",