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;
98 /* Allocate a value that has the correct length
99 for COUNT repetitions type TYPE. */
102 allocate_repeat_value (type, count)
108 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
109 VALUE_NEXT (val) = all_values;
111 VALUE_TYPE (val) = type;
112 VALUE_LVAL (val) = not_lval;
113 VALUE_ADDRESS (val) = 0;
114 VALUE_FRAME (val) = 0;
115 VALUE_OFFSET (val) = 0;
116 VALUE_BITPOS (val) = 0;
117 VALUE_BITSIZE (val) = 0;
118 VALUE_REPEATED (val) = 1;
119 VALUE_REPETITIONS (val) = count;
120 VALUE_REGNO (val) = -1;
121 VALUE_LAZY (val) = 0;
122 VALUE_OPTIMIZED_OUT (val) = 0;
126 /* Return a mark in the value chain. All values allocated after the
127 mark is obtained (except for those released) are subject to being freed
128 if a subsequent value_free_to_mark is passed the mark. */
135 /* Free all values allocated since MARK was obtained by value_mark
136 (except for those released). */
138 value_free_to_mark (mark)
143 for (val = all_values; val && val != mark; val = next)
145 next = VALUE_NEXT (val);
151 /* Free all the values that have been allocated (except for those released).
152 Called after each command, successful or not. */
157 register value val, next;
159 for (val = all_values; val; val = next)
161 next = VALUE_NEXT (val);
168 /* Remove VAL from the chain all_values
169 so it will not be freed automatically. */
177 if (all_values == val)
179 all_values = val->next;
183 for (v = all_values; v; v = v->next)
193 /* Return a copy of the value ARG.
194 It contains the same contents, for same memory address,
195 but it's a different block of storage. */
202 register struct type *type = VALUE_TYPE (arg);
203 if (VALUE_REPEATED (arg))
204 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
206 val = allocate_value (type);
207 VALUE_LVAL (val) = VALUE_LVAL (arg);
208 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
209 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
210 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
211 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
212 VALUE_REGNO (val) = VALUE_REGNO (arg);
213 VALUE_LAZY (val) = VALUE_LAZY (arg);
214 val->modifiable = arg->modifiable;
215 if (!VALUE_LAZY (val))
217 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS_RAW (arg),
218 TYPE_LENGTH (VALUE_TYPE (arg))
219 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
224 /* Access to the value history. */
226 /* Record a new value in the value history.
227 Returns the absolute history index of the entry.
228 Result of -1 indicates the value was not saved; otherwise it is the
229 value history index of this new item. */
232 record_latest_value (val)
237 /* Check error now if about to store an invalid float. We return -1
238 to the caller, but allow them to continue, e.g. to print it as "Nan". */
239 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT)
241 unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
242 if (i) return -1; /* Indicate value not saved in history */
245 /* Here we treat value_history_count as origin-zero
246 and applying to the value being stored now. */
248 i = value_history_count % VALUE_HISTORY_CHUNK;
251 register struct value_history_chunk *new
252 = (struct value_history_chunk *)
253 xmalloc (sizeof (struct value_history_chunk));
254 memset (new->values, 0, sizeof new->values);
255 new->next = value_history_chain;
256 value_history_chain = new;
259 value_history_chain->values[i] = val;
261 /* We don't want this value to have anything to do with the inferior anymore.
262 In particular, "set $1 = 50" should not affect the variable from which
263 the value was taken, and fast watchpoints should be able to assume that
264 a value on the value history never changes. */
265 if (VALUE_LAZY (val))
266 value_fetch_lazy (val);
267 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
268 from. This is a bit dubious, because then *&$1 does not just return $1
269 but the current contents of that location. c'est la vie... */
273 /* Now we regard value_history_count as origin-one
274 and applying to the value just stored. */
276 return ++value_history_count;
279 /* Return a copy of the value in the history with sequence number NUM. */
282 access_value_history (num)
285 register struct value_history_chunk *chunk;
287 register int absnum = num;
290 absnum += value_history_count;
295 error ("The history is empty.");
297 error ("There is only one value in the history.");
299 error ("History does not go back to $$%d.", -num);
301 if (absnum > value_history_count)
302 error ("History has not yet reached $%d.", absnum);
306 /* Now absnum is always absolute and origin zero. */
308 chunk = value_history_chain;
309 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
313 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
316 /* Clear the value history entirely.
317 Must be done when new symbol tables are loaded,
318 because the type pointers become invalid. */
321 clear_value_history ()
323 register struct value_history_chunk *next;
327 while (value_history_chain)
329 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
330 if ((val = value_history_chain->values[i]) != NULL)
332 next = value_history_chain->next;
333 free ((PTR)value_history_chain);
334 value_history_chain = next;
336 value_history_count = 0;
340 show_values (num_exp, from_tty)
350 /* "info history +" should print from the stored position.
351 "info history <exp>" should print around value number <exp>. */
352 if (num_exp[0] != '+' || num_exp[1] != '\0')
353 num = parse_and_eval_address (num_exp) - 5;
357 /* "info history" means print the last 10 values. */
358 num = value_history_count - 9;
364 for (i = num; i < num + 10 && i <= value_history_count; i++)
366 val = access_value_history (i);
367 printf_filtered ("$%d = ", i);
368 value_print (val, gdb_stdout, 0, Val_pretty_default);
369 printf_filtered ("\n");
372 /* The next "info history +" should start after what we just printed. */
375 /* Hitting just return after this command should do the same thing as
376 "info history +". If num_exp is null, this is unnecessary, since
377 "info history +" is not useful after "info history". */
378 if (from_tty && num_exp)
385 /* Internal variables. These are variables within the debugger
386 that hold values assigned by debugger commands.
387 The user refers to them with a '$' prefix
388 that does not appear in the variable names stored internally. */
390 static struct internalvar *internalvars;
392 /* Look up an internal variable with name NAME. NAME should not
393 normally include a dollar sign.
395 If the specified internal variable does not exist,
396 one is created, with a void value. */
399 lookup_internalvar (name)
402 register struct internalvar *var;
404 for (var = internalvars; var; var = var->next)
405 if (STREQ (var->name, name))
408 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
409 var->name = concat (name, NULL);
410 var->value = allocate_value (builtin_type_void);
411 release_value (var->value);
412 var->next = internalvars;
418 value_of_internalvar (var)
419 struct internalvar *var;
423 #ifdef IS_TRAPPED_INTERNALVAR
424 if (IS_TRAPPED_INTERNALVAR (var->name))
425 return VALUE_OF_TRAPPED_INTERNALVAR (var);
428 val = value_copy (var->value);
429 if (VALUE_LAZY (val))
430 value_fetch_lazy (val);
431 VALUE_LVAL (val) = lval_internalvar;
432 VALUE_INTERNALVAR (val) = var;
437 set_internalvar_component (var, offset, bitpos, bitsize, newval)
438 struct internalvar *var;
439 int offset, bitpos, bitsize;
442 register char *addr = VALUE_CONTENTS (var->value) + offset;
444 #ifdef IS_TRAPPED_INTERNALVAR
445 if (IS_TRAPPED_INTERNALVAR (var->name))
446 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
450 modify_field (addr, value_as_long (newval),
453 memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval)));
457 set_internalvar (var, val)
458 struct internalvar *var;
461 #ifdef IS_TRAPPED_INTERNALVAR
462 if (IS_TRAPPED_INTERNALVAR (var->name))
463 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
466 free ((PTR)var->value);
467 var->value = value_copy (val);
468 /* Force the value to be fetched from the target now, to avoid problems
469 later when this internalvar is referenced and the target is gone or
471 if (VALUE_LAZY (var->value))
472 value_fetch_lazy (var->value);
473 release_value (var->value);
477 internalvar_name (var)
478 struct internalvar *var;
483 /* Free all internalvars. Done when new symtabs are loaded,
484 because that makes the values invalid. */
487 clear_internalvars ()
489 register struct internalvar *var;
494 internalvars = var->next;
495 free ((PTR)var->name);
496 free ((PTR)var->value);
502 show_convenience (ignore, from_tty)
506 register struct internalvar *var;
509 for (var = internalvars; var; var = var->next)
511 #ifdef IS_TRAPPED_INTERNALVAR
512 if (IS_TRAPPED_INTERNALVAR (var->name))
519 printf_filtered ("$%s = ", var->name);
520 value_print (var->value, gdb_stdout, 0, Val_pretty_default);
521 printf_filtered ("\n");
524 printf_unfiltered ("No debugger convenience variables now defined.\n\
525 Convenience variables have names starting with \"$\";\n\
526 use \"set\" as in \"set $foo = 5\" to define them.\n");
529 /* Extract a value as a C number (either long or double).
530 Knows how to convert fixed values to double, or
531 floating values to long.
532 Does not deallocate the value. */
538 /* This coerces arrays and functions, which is necessary (e.g.
539 in disassemble_command). It also dereferences references, which
540 I suspect is the most logical thing to do. */
541 if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
543 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
547 value_as_double (val)
553 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
555 error ("Invalid floating value found in program.");
558 /* Extract a value as a C pointer.
559 Does not deallocate the value. */
561 value_as_pointer (val)
564 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
565 whether we want this to be true eventually. */
567 /* ADDR_BITS_REMOVE is wrong if we are being called for a
568 non-address (e.g. argument to "signal", "info break", etc.), or
569 for pointers to char, in which the low bits *are* significant. */
570 return ADDR_BITS_REMOVE(value_as_long (val));
572 return value_as_long (val);
576 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
577 as a long, or as a double, assuming the raw data is described
578 by type TYPE. Knows how to convert different sizes of values
579 and can convert between fixed and floating point. We don't assume
580 any alignment for the raw data. Return value is in host byte order.
582 If you want functions and arrays to be coerced to pointers, and
583 references to be dereferenced, call value_as_long() instead.
585 C++: It is assumed that the front-end has taken care of
586 all matters concerning pointers to members. A pointer
587 to member which reaches here is considered to be equivalent
588 to an INT (or some size). After all, it is only an offset. */
590 /* FIXME: This should be rewritten as a switch statement for speed and
591 ease of comprehension. */
594 unpack_long (type, valaddr)
598 register enum type_code code = TYPE_CODE (type);
599 register int len = TYPE_LENGTH (type);
600 register int nosign = TYPE_UNSIGNED (type);
609 return extract_unsigned_integer (valaddr, len);
611 return extract_signed_integer (valaddr, len);
614 return extract_floating (valaddr, len);
618 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
619 whether we want this to be true eventually. */
620 return extract_address (valaddr, len);
622 case TYPE_CODE_MEMBER:
623 error ("not implemented: member types in unpack_long");
626 error ("Value can't be converted to integer.");
628 return 0; /* Placate lint. */
631 /* Return a double value from the specified type and address.
632 INVP points to an int which is set to 0 for valid value,
633 1 for invalid value (bad float format). In either case,
634 the returned double is OK to use. Argument is in target
635 format, result is in host format. */
638 unpack_double (type, valaddr, invp)
643 register enum type_code code = TYPE_CODE (type);
644 register int len = TYPE_LENGTH (type);
645 register int nosign = TYPE_UNSIGNED (type);
647 *invp = 0; /* Assume valid. */
648 if (code == TYPE_CODE_FLT)
650 if (INVALID_FLOAT (valaddr, len))
653 return 1.234567891011121314;
655 return extract_floating (valaddr, len);
659 /* Unsigned -- be sure we compensate for signed LONGEST. */
660 return (unsigned LONGEST) unpack_long (type, valaddr);
664 /* Signed -- we are OK with unpack_long. */
665 return unpack_long (type, valaddr);
669 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
670 as a CORE_ADDR, assuming the raw data is described by type TYPE.
671 We don't assume any alignment for the raw data. Return value is in
674 If you want functions and arrays to be coerced to pointers, and
675 references to be dereferenced, call value_as_pointer() instead.
677 C++: It is assumed that the front-end has taken care of
678 all matters concerning pointers to members. A pointer
679 to member which reaches here is considered to be equivalent
680 to an INT (or some size). After all, it is only an offset. */
683 unpack_pointer (type, valaddr)
687 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
688 whether we want this to be true eventually. */
689 return unpack_long (type, valaddr);
692 /* Given a value ARG1 (offset by OFFSET bytes)
693 of a struct or union type ARG_TYPE,
694 extract and return the value of one of its fields.
695 FIELDNO says which field.
697 For C++, must also be able to return values from static fields */
700 value_primitive_field (arg1, offset, fieldno, arg_type)
703 register int fieldno;
704 register struct type *arg_type;
707 register struct type *type;
709 check_stub_type (arg_type);
710 type = TYPE_FIELD_TYPE (arg_type, fieldno);
712 /* Handle packed fields */
714 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
715 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
717 v = value_from_longest (type,
718 unpack_field_as_long (arg_type,
719 VALUE_CONTENTS (arg1),
721 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
722 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
726 v = allocate_value (type);
727 if (VALUE_LAZY (arg1))
730 memcpy (VALUE_CONTENTS_RAW (v), VALUE_CONTENTS_RAW (arg1) + offset,
733 VALUE_LVAL (v) = VALUE_LVAL (arg1);
734 if (VALUE_LVAL (arg1) == lval_internalvar)
735 VALUE_LVAL (v) = lval_internalvar_component;
736 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
737 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
741 /* Given a value ARG1 of a struct or union type,
742 extract and return the value of one of its fields.
743 FIELDNO says which field.
745 For C++, must also be able to return values from static fields */
748 value_field (arg1, fieldno)
750 register int fieldno;
752 return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
755 /* Return a non-virtual function as a value.
756 F is the list of member functions which contains the desired method.
757 J is an index into F which provides the desired method. */
760 value_fn_field (arg1p, f, j, type, offset)
768 register struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
771 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
772 0, VAR_NAMESPACE, 0, NULL);
776 error ("Internal error: could not find physical method named %s",
777 TYPE_FN_FIELD_PHYSNAME (f, j));
780 v = allocate_value (ftype);
781 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
782 VALUE_TYPE (v) = ftype;
786 if (type != VALUE_TYPE (*arg1p))
787 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
788 value_addr (*arg1p)));
790 /* Move the `this' pointer according to the offset.
791 VALUE_OFFSET (*arg1p) += offset;
798 /* Return a virtual function as a value.
799 ARG1 is the object which provides the virtual function
800 table pointer. *ARG1P is side-effected in calling this function.
801 F is the list of member functions which contains the desired virtual
803 J is an index into F which provides the desired virtual function.
805 TYPE is the type in which F is located. */
807 value_virtual_fn_field (arg1p, f, j, type, offset)
815 /* First, get the virtual function table pointer. That comes
816 with a strange type, so cast it to type `pointer to long' (which
817 should serve just fine as a function type). Then, index into
818 the table, and convert final value to appropriate function type. */
819 value entry, vfn, vtbl;
820 value vi = value_from_longest (builtin_type_int,
821 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
822 struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
823 struct type *context;
824 if (fcontext == NULL)
825 /* We don't have an fcontext (e.g. the program was compiled with
826 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
827 This won't work right for multiple inheritance, but at least we
828 should do as well as GDB 3.x did. */
829 fcontext = TYPE_VPTR_BASETYPE (type);
830 context = lookup_pointer_type (fcontext);
831 /* Now context is a pointer to the basetype containing the vtbl. */
832 if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
833 arg1 = value_ind (value_cast (context, value_addr (arg1)));
835 context = VALUE_TYPE (arg1);
836 /* Now context is the basetype containing the vtbl. */
838 /* This type may have been defined before its virtual function table
839 was. If so, fill in the virtual function table entry for the
841 if (TYPE_VPTR_FIELDNO (context) < 0)
842 fill_in_vptr_fieldno (context);
844 /* The virtual function table is now an array of structures
845 which have the form { int16 offset, delta; void *pfn; }. */
846 vtbl = value_ind (value_primitive_field (arg1, 0,
847 TYPE_VPTR_FIELDNO (context),
848 TYPE_VPTR_BASETYPE (context)));
850 /* Index into the virtual function table. This is hard-coded because
851 looking up a field is not cheap, and it may be important to save
852 time, e.g. if the user has set a conditional breakpoint calling
853 a virtual function. */
854 entry = value_subscript (vtbl, vi);
856 /* Move the `this' pointer according to the virtual function table. */
857 VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0))/* + offset*/;
859 if (! VALUE_LAZY (arg1))
861 VALUE_LAZY (arg1) = 1;
862 value_fetch_lazy (arg1);
865 vfn = value_field (entry, 2);
866 /* Reinstantiate the function pointer with the correct type. */
867 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
873 /* ARG is a pointer to an object we know to be at least
874 a DTYPE. BTYPE is the most derived basetype that has
875 already been searched (and need not be searched again).
876 After looking at the vtables between BTYPE and DTYPE,
877 return the most derived type we find. The caller must
878 be satisfied when the return value == DTYPE.
880 FIXME-tiemann: should work with dossier entries as well. */
883 value_headof (in_arg, btype, dtype)
885 struct type *btype, *dtype;
887 /* First collect the vtables we must look at for this object. */
888 /* FIXME-tiemann: right now, just look at top-most vtable. */
889 value arg, vtbl, entry, best_entry = 0;
891 int offset, best_offset = 0;
893 CORE_ADDR pc_for_sym;
894 char *demangled_name;
895 struct minimal_symbol *msymbol;
897 btype = TYPE_VPTR_BASETYPE (dtype);
898 check_stub_type (btype);
901 arg = value_cast (lookup_pointer_type (btype), arg);
902 vtbl = value_ind (value_field (value_ind (arg), TYPE_VPTR_FIELDNO (btype)));
904 /* Check that VTBL looks like it points to a virtual function table. */
905 msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
907 || !VTBL_PREFIX_P (demangled_name = SYMBOL_NAME (msymbol)))
909 /* If we expected to find a vtable, but did not, let the user
910 know that we aren't happy, but don't throw an error.
911 FIXME: there has to be a better way to do this. */
912 struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
913 memcpy (error_type, VALUE_TYPE (in_arg), sizeof (struct type));
914 TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
915 VALUE_TYPE (in_arg) = error_type;
919 /* Now search through the virtual function table. */
920 entry = value_ind (vtbl);
921 nelems = longest_to_int (value_as_long (value_field (entry, 2)));
922 for (i = 1; i <= nelems; i++)
924 entry = value_subscript (vtbl, value_from_longest (builtin_type_int,
926 offset = longest_to_int (value_as_long (value_field (entry, 0)));
927 /* If we use '<=' we can handle single inheritance
928 * where all offsets are zero - just use the first entry found. */
929 if (offset <= best_offset)
931 best_offset = offset;
935 /* Move the pointer according to BEST_ENTRY's offset, and figure
936 out what type we should return as the new pointer. */
939 /* An alternative method (which should no longer be necessary).
940 * But we leave it in for future use, when we will hopefully
941 * have optimizes the vtable to use thunks instead of offsets. */
942 /* Use the name of vtable itself to extract a base type. */
943 demangled_name += 4; /* Skip _vt$ prefix. */
947 pc_for_sym = value_as_pointer (value_field (best_entry, 2));
948 sym = find_pc_function (pc_for_sym);
949 demangled_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ANSI);
950 *(strchr (demangled_name, ':')) = '\0';
952 sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
954 error ("could not find type declaration for `%s'", demangled_name);
957 free (demangled_name);
958 arg = value_add (value_cast (builtin_type_int, arg),
959 value_field (best_entry, 0));
962 VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
966 /* ARG is a pointer object of type TYPE. If TYPE has virtual
967 function tables, probe ARG's tables (including the vtables
968 of its baseclasses) to figure out the most derived type that ARG
969 could actually be a pointer to. */
972 value_from_vtable_info (arg, type)
976 /* Take care of preliminaries. */
977 if (TYPE_VPTR_FIELDNO (type) < 0)
978 fill_in_vptr_fieldno (type);
979 if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
982 return value_headof (arg, 0, type);
985 /* Return true if the INDEXth field of TYPE is a virtual baseclass
986 pointer which is for the base class whose type is BASECLASS. */
989 vb_match (type, index, basetype)
992 struct type *basetype;
994 struct type *fieldtype;
995 char *name = TYPE_FIELD_NAME (type, index);
996 char *field_class_name = NULL;
1000 /* gcc 2.4 uses _vb$. */
1001 if (name[1] == 'v' && name[2] == 'b' && name[3] == CPLUS_MARKER)
1002 field_class_name = name + 4;
1003 /* gcc 2.5 will use __vb_. */
1004 if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_')
1005 field_class_name = name + 5;
1007 if (field_class_name == NULL)
1008 /* This field is not a virtual base class pointer. */
1011 /* It's a virtual baseclass pointer, now we just need to find out whether
1012 it is for this baseclass. */
1013 fieldtype = TYPE_FIELD_TYPE (type, index);
1014 if (fieldtype == NULL
1015 || TYPE_CODE (fieldtype) != TYPE_CODE_PTR)
1016 /* "Can't happen". */
1019 /* What we check for is that either the types are equal (needed for
1020 nameless types) or have the same name. This is ugly, and a more
1021 elegant solution should be devised (which would probably just push
1022 the ugliness into symbol reading unless we change the stabs format). */
1023 if (TYPE_TARGET_TYPE (fieldtype) == basetype)
1026 if (TYPE_NAME (basetype) != NULL
1027 && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL
1028 && STREQ (TYPE_NAME (basetype),
1029 TYPE_NAME (TYPE_TARGET_TYPE (fieldtype))))
1034 /* Compute the offset of the baseclass which is
1035 the INDEXth baseclass of class TYPE, for a value ARG,
1036 wih extra offset of OFFSET.
1037 The result is the offste of the baseclass value relative
1038 to (the address of)(ARG) + OFFSET.
1040 -1 is returned on error. */
1043 baseclass_offset (type, index, arg, offset)
1049 struct type *basetype = TYPE_BASECLASS (type, index);
1051 if (BASETYPE_VIA_VIRTUAL (type, index))
1053 /* Must hunt for the pointer to this virtual baseclass. */
1054 register int i, len = TYPE_NFIELDS (type);
1055 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1057 /* First look for the virtual baseclass pointer
1059 for (i = n_baseclasses; i < len; i++)
1061 if (vb_match (type, i, basetype))
1064 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1065 VALUE_CONTENTS (arg) + VALUE_OFFSET (arg)
1067 + (TYPE_FIELD_BITPOS (type, i) / 8));
1069 if (VALUE_LVAL (arg) != lval_memory)
1073 (LONGEST) (VALUE_ADDRESS (arg) + VALUE_OFFSET (arg) + offset);
1076 /* Not in the fields, so try looking through the baseclasses. */
1077 for (i = index+1; i < n_baseclasses; i++)
1080 baseclass_offset (type, i, arg, offset);
1088 /* Baseclass is easily computed. */
1089 return TYPE_BASECLASS_BITPOS (type, index) / 8;
1092 /* Compute the address of the baseclass which is
1093 the INDEXth baseclass of class TYPE. The TYPE base
1094 of the object is at VALADDR.
1096 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1097 or 0 if no error. In that case the return value is not the address
1098 of the baseclasss, but the address which could not be read
1101 /* FIXME Fix remaining uses of baseclass_addr to use baseclass_offset */
1104 baseclass_addr (type, index, valaddr, valuep, errp)
1111 struct type *basetype = TYPE_BASECLASS (type, index);
1116 if (BASETYPE_VIA_VIRTUAL (type, index))
1118 /* Must hunt for the pointer to this virtual baseclass. */
1119 register int i, len = TYPE_NFIELDS (type);
1120 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1122 /* First look for the virtual baseclass pointer
1124 for (i = n_baseclasses; i < len; i++)
1126 if (vb_match (type, i, basetype))
1128 value val = allocate_value (basetype);
1133 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1134 valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
1136 status = target_read_memory (addr,
1137 VALUE_CONTENTS_RAW (val),
1138 TYPE_LENGTH (basetype));
1139 VALUE_LVAL (val) = lval_memory;
1140 VALUE_ADDRESS (val) = addr;
1146 release_value (val);
1150 return (char *)addr;
1156 return (char *) VALUE_CONTENTS (val);
1160 /* Not in the fields, so try looking through the baseclasses. */
1161 for (i = index+1; i < n_baseclasses; i++)
1165 baddr = baseclass_addr (type, i, valaddr, valuep, errp);
1175 /* Baseclass is easily computed. */
1178 return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1181 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1184 Extracting bits depends on endianness of the machine. Compute the
1185 number of least significant bits to discard. For big endian machines,
1186 we compute the total number of bits in the anonymous object, subtract
1187 off the bit count from the MSB of the object to the MSB of the
1188 bitfield, then the size of the bitfield, which leaves the LSB discard
1189 count. For little endian machines, the discard count is simply the
1190 number of bits from the LSB of the anonymous object to the LSB of the
1193 If the field is signed, we also do sign extension. */
1196 unpack_field_as_long (type, valaddr, fieldno)
1201 unsigned LONGEST val;
1202 unsigned LONGEST valmask;
1203 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1204 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1207 val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1209 /* Extract bits. See comment above. */
1212 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1214 lsbcount = (bitpos % 8);
1218 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1219 If the field is signed, and is negative, then sign extend. */
1221 if ((bitsize > 0) && (bitsize < 8 * sizeof (val)))
1223 valmask = (((unsigned LONGEST) 1) << bitsize) - 1;
1225 if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type, fieldno)))
1227 if (val & (valmask ^ (valmask >> 1)))
1236 /* Modify the value of a bitfield. ADDR points to a block of memory in
1237 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1238 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1239 indicate which bits (in target bit order) comprise the bitfield. */
1242 modify_field (addr, fieldval, bitpos, bitsize)
1245 int bitpos, bitsize;
1249 /* Reject values too big to fit in the field in question,
1250 otherwise adjoining fields may be corrupted. */
1251 if (bitsize < (8 * sizeof (fieldval))
1252 && 0 != (fieldval & ~((1<<bitsize)-1)))
1254 /* FIXME: would like to include fieldval in the message, but
1255 we don't have a sprintf_longest. */
1256 error ("Value does not fit in %d bits.", bitsize);
1259 oword = extract_signed_integer (addr, sizeof oword);
1261 /* Shifting for bit field depends on endianness of the target machine. */
1263 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1266 /* Mask out old value, while avoiding shifts >= size of oword */
1267 if (bitsize < 8 * sizeof (oword))
1268 oword &= ~(((((unsigned LONGEST)1) << bitsize) - 1) << bitpos);
1270 oword &= ~((~(unsigned LONGEST)0) << bitpos);
1271 oword |= fieldval << bitpos;
1273 store_signed_integer (addr, sizeof oword, oword);
1276 /* Convert C numbers into newly allocated values */
1279 value_from_longest (type, num)
1281 register LONGEST num;
1283 register value val = allocate_value (type);
1284 register enum type_code code = TYPE_CODE (type);
1285 register int len = TYPE_LENGTH (type);
1290 case TYPE_CODE_CHAR:
1291 case TYPE_CODE_ENUM:
1292 case TYPE_CODE_BOOL:
1293 store_signed_integer (VALUE_CONTENTS_RAW (val), len, num);
1298 /* This assumes that all pointers of a given length
1299 have the same form. */
1300 store_address (VALUE_CONTENTS_RAW (val), len, (CORE_ADDR) num);
1304 error ("Unexpected type encountered for integer constant.");
1310 value_from_double (type, num)
1314 register value val = allocate_value (type);
1315 register enum type_code code = TYPE_CODE (type);
1316 register int len = TYPE_LENGTH (type);
1318 if (code == TYPE_CODE_FLT)
1320 store_floating (VALUE_CONTENTS_RAW (val), len, num);
1323 error ("Unexpected type encountered for floating constant.");
1328 /* Deal with the value that is "about to be returned". */
1330 /* Return the value that a function returning now
1331 would be returning to its caller, assuming its type is VALTYPE.
1332 RETBUF is where we look for what ought to be the contents
1333 of the registers (in raw form). This is because it is often
1334 desirable to restore old values to those registers
1335 after saving the contents of interest, and then call
1336 this function using the saved values.
1337 struct_return is non-zero when the function in question is
1338 using the structure return conventions on the machine in question;
1339 0 when it is using the value returning conventions (this often
1340 means returning pointer to where structure is vs. returning value). */
1343 value_being_returned (valtype, retbuf, struct_return)
1344 register struct type *valtype;
1345 char retbuf[REGISTER_BYTES];
1352 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1353 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1354 if (struct_return) {
1355 addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1357 error ("Function return value unknown");
1358 return value_at (valtype, addr);
1362 val = allocate_value (valtype);
1363 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1368 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1369 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1370 and TYPE is the type (which is known to be struct, union or array).
1372 On most machines, the struct convention is used unless we are
1373 using gcc and the type is of a special size. */
1374 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1375 native compiler. GCC 2.3.3 was the last release that did it the
1376 old way. Since gcc2_compiled was not changed, we have no
1377 way to correctly win in all cases, so we just do the right thing
1378 for gcc1 and for gcc2 after this change. Thus it loses for gcc
1379 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled
1380 would cause more chaos than dealing with some struct returns being
1382 #if !defined (USE_STRUCT_CONVENTION)
1383 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1384 (!((gcc_p == 1) && (TYPE_LENGTH (value_type) == 1 \
1385 || TYPE_LENGTH (value_type) == 2 \
1386 || TYPE_LENGTH (value_type) == 4 \
1387 || TYPE_LENGTH (value_type) == 8 \
1392 /* Return true if the function specified is using the structure returning
1393 convention on this machine to return arguments, or 0 if it is using
1394 the value returning convention. FUNCTION is the value representing
1395 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1396 is the type returned by the function. GCC_P is nonzero if compiled
1400 using_struct_return (function, funcaddr, value_type, gcc_p)
1403 struct type *value_type;
1407 register enum type_code code = TYPE_CODE (value_type);
1409 if (code == TYPE_CODE_ERROR)
1410 error ("Function return type unknown.");
1412 if (code == TYPE_CODE_STRUCT ||
1413 code == TYPE_CODE_UNION ||
1414 code == TYPE_CODE_ARRAY)
1415 return USE_STRUCT_CONVENTION (gcc_p, value_type);
1420 /* Store VAL so it will be returned if a function returns now.
1421 Does not verify that VAL's type matches what the current
1422 function wants to return. */
1425 set_return_value (val)
1428 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1432 if (code == TYPE_CODE_ERROR)
1433 error ("Function return type unknown.");
1435 if ( code == TYPE_CODE_STRUCT
1436 || code == TYPE_CODE_UNION) /* FIXME, implement struct return. */
1437 error ("GDB does not support specifying a struct or union return value.");
1439 /* FIXME, this is bogus. We don't know what the return conventions
1440 are, or how values should be promoted.... */
1441 if (code == TYPE_CODE_FLT)
1443 dbuf = value_as_double (val);
1445 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
1449 lbuf = value_as_long (val);
1450 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
1455 _initialize_values ()
1457 add_cmd ("convenience", no_class, show_convenience,
1458 "Debugger convenience (\"$foo\") variables.\n\
1459 These variables are created when you assign them values;\n\
1460 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1461 A few convenience variables are given values automatically:\n\
1462 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1463 \"$__\" holds the contents of the last address examined with \"x\".",
1466 add_cmd ("values", no_class, show_values,
1467 "Elements of value history around item number IDX (or last ten).",