1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* The value-history records all the values printed
32 by print commands during this session. Each chunk
33 records 60 consecutive values. The first chunk on
34 the chain records the most recent values.
35 The total number of values is in value_history_count. */
37 #define VALUE_HISTORY_CHUNK 60
39 struct value_history_chunk
41 struct value_history_chunk
*next
;
42 value values
[VALUE_HISTORY_CHUNK
];
45 /* Chain of chunks now in use. */
47 static struct value_history_chunk
*value_history_chain
;
49 static int value_history_count
; /* Abs number of last entry stored */
52 /* List of all value objects currently allocated
53 (except for those released by calls to release_value)
54 This is so they can be freed after each command. */
56 static value all_values
;
58 /* Allocate a value that has the correct length for type TYPE. */
66 check_stub_type (type
);
68 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
69 VALUE_NEXT (val
) = all_values
;
71 VALUE_TYPE (val
) = type
;
72 VALUE_LVAL (val
) = not_lval
;
73 VALUE_ADDRESS (val
) = 0;
74 VALUE_FRAME (val
) = 0;
75 VALUE_OFFSET (val
) = 0;
76 VALUE_BITPOS (val
) = 0;
77 VALUE_BITSIZE (val
) = 0;
78 VALUE_REPEATED (val
) = 0;
79 VALUE_REPETITIONS (val
) = 0;
80 VALUE_REGNO (val
) = -1;
82 VALUE_OPTIMIZED_OUT (val
) = 0;
86 /* Allocate a value that has the correct length
87 for COUNT repetitions type TYPE. */
90 allocate_repeat_value (type
, count
)
96 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
97 VALUE_NEXT (val
) = all_values
;
99 VALUE_TYPE (val
) = type
;
100 VALUE_LVAL (val
) = not_lval
;
101 VALUE_ADDRESS (val
) = 0;
102 VALUE_FRAME (val
) = 0;
103 VALUE_OFFSET (val
) = 0;
104 VALUE_BITPOS (val
) = 0;
105 VALUE_BITSIZE (val
) = 0;
106 VALUE_REPEATED (val
) = 1;
107 VALUE_REPETITIONS (val
) = count
;
108 VALUE_REGNO (val
) = -1;
109 VALUE_LAZY (val
) = 0;
110 VALUE_OPTIMIZED_OUT (val
) = 0;
114 /* Return a mark in the value chain. All values allocated after the
115 mark is obtained (except for those released) are subject to being freed
116 if a subsequent value_free_to_mark is passed the mark. */
123 /* Free all values allocated since MARK was obtained by value_mark
124 (except for those released). */
126 value_free_to_mark (mark
)
131 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
133 next
= VALUE_NEXT (val
);
139 /* Free all the values that have been allocated (except for those released).
140 Called after each command, successful or not. */
145 register value val
, next
;
147 for (val
= all_values
; val
; val
= next
)
149 next
= VALUE_NEXT (val
);
156 /* Remove VAL from the chain all_values
157 so it will not be freed automatically. */
165 if (all_values
== val
)
167 all_values
= val
->next
;
171 for (v
= all_values
; v
; v
= v
->next
)
181 /* Return a copy of the value ARG.
182 It contains the same contents, for same memory address,
183 but it's a different block of storage. */
190 register struct type
*type
= VALUE_TYPE (arg
);
191 if (VALUE_REPEATED (arg
))
192 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
194 val
= allocate_value (type
);
195 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
196 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
197 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
198 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
199 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
200 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
201 VALUE_LAZY (val
) = VALUE_LAZY (arg
);
202 if (!VALUE_LAZY (val
))
204 bcopy (VALUE_CONTENTS_RAW (arg
), VALUE_CONTENTS_RAW (val
),
205 TYPE_LENGTH (VALUE_TYPE (arg
))
206 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
211 /* Access to the value history. */
213 /* Record a new value in the value history.
214 Returns the absolute history index of the entry.
215 Result of -1 indicates the value was not saved; otherwise it is the
216 value history index of this new item. */
219 record_latest_value (val
)
224 /* Check error now if about to store an invalid float. We return -1
225 to the caller, but allow them to continue, e.g. to print it as "Nan". */
226 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
) {
227 (void) unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &i
);
228 if (i
) return -1; /* Indicate value not saved in history */
231 /* Here we treat value_history_count as origin-zero
232 and applying to the value being stored now. */
234 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
237 register struct value_history_chunk
*new
238 = (struct value_history_chunk
*)
239 xmalloc (sizeof (struct value_history_chunk
));
240 bzero (new->values
, sizeof new->values
);
241 new->next
= value_history_chain
;
242 value_history_chain
= new;
245 value_history_chain
->values
[i
] = val
;
248 /* Now we regard value_history_count as origin-one
249 and applying to the value just stored. */
251 return ++value_history_count
;
254 /* Return a copy of the value in the history with sequence number NUM. */
257 access_value_history (num
)
260 register struct value_history_chunk
*chunk
;
262 register int absnum
= num
;
265 absnum
+= value_history_count
;
270 error ("The history is empty.");
272 error ("There is only one value in the history.");
274 error ("History does not go back to $$%d.", -num
);
276 if (absnum
> value_history_count
)
277 error ("History has not yet reached $%d.", absnum
);
281 /* Now absnum is always absolute and origin zero. */
283 chunk
= value_history_chain
;
284 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
288 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
291 /* Clear the value history entirely.
292 Must be done when new symbol tables are loaded,
293 because the type pointers become invalid. */
296 clear_value_history ()
298 register struct value_history_chunk
*next
;
302 while (value_history_chain
)
304 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
305 if (val
= value_history_chain
->values
[i
])
307 next
= value_history_chain
->next
;
308 free (value_history_chain
);
309 value_history_chain
= next
;
311 value_history_count
= 0;
315 show_values (num_exp
, from_tty
)
325 if (num_exp
[0] == '+' && num_exp
[1] == '\0')
326 /* "info history +" should print from the stored position. */
329 /* "info history <exp>" should print around value number <exp>. */
330 num
= parse_and_eval_address (num_exp
) - 5;
334 /* "info history" means print the last 10 values. */
335 num
= value_history_count
- 9;
341 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
343 val
= access_value_history (i
);
344 printf_filtered ("$%d = ", i
);
345 value_print (val
, stdout
, 0, Val_pretty_default
);
346 printf_filtered ("\n");
349 /* The next "info history +" should start after what we just printed. */
352 /* Hitting just return after this command should do the same thing as
353 "info history +". If num_exp is null, this is unnecessary, since
354 "info history +" is not useful after "info history". */
355 if (from_tty
&& num_exp
)
362 /* Internal variables. These are variables within the debugger
363 that hold values assigned by debugger commands.
364 The user refers to them with a '$' prefix
365 that does not appear in the variable names stored internally. */
367 static struct internalvar
*internalvars
;
369 /* Look up an internal variable with name NAME. NAME should not
370 normally include a dollar sign.
372 If the specified internal variable does not exist,
373 one is created, with a void value. */
376 lookup_internalvar (name
)
379 register struct internalvar
*var
;
381 for (var
= internalvars
; var
; var
= var
->next
)
382 if (!strcmp (var
->name
, name
))
385 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
386 var
->name
= concat (name
, "", "");
387 var
->value
= allocate_value (builtin_type_void
);
388 release_value (var
->value
);
389 var
->next
= internalvars
;
395 value_of_internalvar (var
)
396 struct internalvar
*var
;
400 #ifdef IS_TRAPPED_INTERNALVAR
401 if (IS_TRAPPED_INTERNALVAR (var
->name
))
402 return VALUE_OF_TRAPPED_INTERNALVAR (var
);
405 val
= value_copy (var
->value
);
406 if (VALUE_LAZY (val
))
407 value_fetch_lazy (val
);
408 VALUE_LVAL (val
) = lval_internalvar
;
409 VALUE_INTERNALVAR (val
) = var
;
414 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
415 struct internalvar
*var
;
416 int offset
, bitpos
, bitsize
;
419 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
421 #ifdef IS_TRAPPED_INTERNALVAR
422 if (IS_TRAPPED_INTERNALVAR (var
->name
))
423 SET_TRAPPED_INTERNALVAR (var
, newval
, bitpos
, bitsize
, offset
);
427 modify_field (addr
, (int) value_as_long (newval
),
430 bcopy (VALUE_CONTENTS (newval
), addr
,
431 TYPE_LENGTH (VALUE_TYPE (newval
)));
435 set_internalvar (var
, val
)
436 struct internalvar
*var
;
439 #ifdef IS_TRAPPED_INTERNALVAR
440 if (IS_TRAPPED_INTERNALVAR (var
->name
))
441 SET_TRAPPED_INTERNALVAR (var
, val
, 0, 0, 0);
445 var
->value
= value_copy (val
);
446 release_value (var
->value
);
450 internalvar_name (var
)
451 struct internalvar
*var
;
456 /* Free all internalvars. Done when new symtabs are loaded,
457 because that makes the values invalid. */
460 clear_internalvars ()
462 register struct internalvar
*var
;
467 internalvars
= var
->next
;
477 register struct internalvar
*var
;
480 for (var
= internalvars
; var
; var
= var
->next
)
482 #ifdef IS_TRAPPED_INTERNALVAR
483 if (IS_TRAPPED_INTERNALVAR (var
->name
))
490 printf ("Debugger convenience variables:\n\n");
494 printf ("$%s = ", var
->name
);
495 value_print (var
->value
, stdout
, 0, Val_pretty_default
);
499 printf ("No debugger convenience variables now defined.\n\
500 Convenience variables have names starting with \"$\";\n\
501 use \"set\" as in \"set $foo = 5\" to define them.\n");
504 /* Extract a value as a C number (either long or double).
505 Knows how to convert fixed values to double, or
506 floating values to long.
507 Does not deallocate the value. */
513 /* This coerces arrays and functions, which is necessary (e.g.
514 in disassemble_command). It also dereferences references, which
515 I suspect is the most logical thing to do. */
516 if (TYPE_CODE (VALUE_TYPE (val
)) != TYPE_CODE_ENUM
)
518 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
522 value_as_double (val
)
528 foo
= unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
), &inv
);
530 error ("Invalid floating value found in program.");
534 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
535 as a long, or as a double, assuming the raw data is described
536 by type TYPE. Knows how to convert different sizes of values
537 and can convert between fixed and floating point. We don't assume
538 any alignment for the raw data. Return value is in host byte order.
540 If you want functions and arrays to be coerced to pointers, and
541 references to be dereferenced, call value_as_long() instead.
543 C++: It is assumed that the front-end has taken care of
544 all matters concerning pointers to members. A pointer
545 to member which reaches here is considered to be equivalent
546 to an INT (or some size). After all, it is only an offset. */
549 unpack_long (type
, valaddr
)
553 register enum type_code code
= TYPE_CODE (type
);
554 register int len
= TYPE_LENGTH (type
);
555 register int nosign
= TYPE_UNSIGNED (type
);
557 if (code
== TYPE_CODE_ENUM
)
558 code
= TYPE_CODE_INT
;
559 if (code
== TYPE_CODE_FLT
)
561 if (len
== sizeof (float))
564 bcopy (valaddr
, &retval
, sizeof (retval
));
565 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
569 if (len
== sizeof (double))
572 bcopy (valaddr
, &retval
, sizeof (retval
));
573 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
578 error ("Unexpected type of floating point number.");
581 else if (code
== TYPE_CODE_INT
&& nosign
)
583 if (len
== sizeof (char))
585 unsigned char retval
= * (unsigned char *) valaddr
;
586 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
590 if (len
== sizeof (short))
592 unsigned short retval
;
593 bcopy (valaddr
, &retval
, sizeof (retval
));
594 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
598 if (len
== sizeof (int))
601 bcopy (valaddr
, &retval
, sizeof (retval
));
602 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
606 if (len
== sizeof (long))
608 unsigned long retval
;
609 bcopy (valaddr
, &retval
, sizeof (retval
));
610 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
614 if (len
== sizeof (long long))
616 unsigned long long retval
;
617 bcopy (valaddr
, &retval
, sizeof (retval
));
618 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
624 error ("That operation is not possible on an integer of that size.");
627 else if (code
== TYPE_CODE_INT
)
629 if (len
== sizeof (char))
632 bcopy (valaddr
, &retval
, sizeof (retval
));
633 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
637 if (len
== sizeof (short))
640 bcopy (valaddr
, &retval
, sizeof (retval
));
641 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
645 if (len
== sizeof (int))
648 bcopy (valaddr
, &retval
, sizeof (retval
));
649 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
653 if (len
== sizeof (long))
656 bcopy (valaddr
, &retval
, sizeof (retval
));
657 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
662 if (len
== sizeof (long long))
665 bcopy (valaddr
, &retval
, sizeof (retval
));
666 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
672 error ("That operation is not possible on an integer of that size.");
675 else if (code
== TYPE_CODE_PTR
676 || code
== TYPE_CODE_REF
)
678 if (len
== sizeof (char *))
681 bcopy (valaddr
, &retval
, sizeof (retval
));
682 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
686 else if (code
== TYPE_CODE_MEMBER
)
687 error ("not implemented: member types in unpack_long");
689 error ("Value not integer or pointer.");
690 return 0; /* For lint -- never reached */
693 /* Return a double value from the specified type and address.
694 INVP points to an int which is set to 0 for valid value,
695 1 for invalid value (bad float format). In either case,
696 the returned double is OK to use. Argument is in target
697 format, result is in host format. */
700 unpack_double (type
, valaddr
, invp
)
705 register enum type_code code
= TYPE_CODE (type
);
706 register int len
= TYPE_LENGTH (type
);
707 register int nosign
= TYPE_UNSIGNED (type
);
709 *invp
= 0; /* Assume valid. */
710 if (code
== TYPE_CODE_FLT
)
712 if (INVALID_FLOAT (valaddr
, len
))
715 return 1.234567891011121314;
718 if (len
== sizeof (float))
721 bcopy (valaddr
, &retval
, sizeof (retval
));
722 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
726 if (len
== sizeof (double))
729 bcopy (valaddr
, &retval
, sizeof (retval
));
730 SWAP_TARGET_AND_HOST (&retval
, sizeof (retval
));
735 error ("Unexpected type of floating point number.");
739 /* Unsigned -- be sure we compensate for signed LONGEST. */
741 return (unsigned long long) unpack_long (type
, valaddr
);
743 return (unsigned long ) unpack_long (type
, valaddr
);
746 /* Signed -- we are OK with unpack_long. */
747 return unpack_long (type
, valaddr
);
751 /* Given a value ARG1 (offset by OFFSET bytes)
752 of a struct or union type ARG_TYPE,
753 extract and return the value of one of its fields.
754 FIELDNO says which field.
756 For C++, must also be able to return values from static fields */
759 value_primitive_field (arg1
, offset
, fieldno
, arg_type
)
762 register int fieldno
;
763 register struct type
*arg_type
;
766 register struct type
*type
;
768 check_stub_type (arg_type
);
769 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
771 /* Handle packed fields */
773 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
774 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
776 v
= value_from_long (type
,
777 unpack_field_as_long (arg_type
,
778 VALUE_CONTENTS (arg1
),
780 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
781 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
785 v
= allocate_value (type
);
786 if (VALUE_LAZY (arg1
))
789 bcopy (VALUE_CONTENTS_RAW (arg1
) + offset
,
790 VALUE_CONTENTS_RAW (v
),
793 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
794 if (VALUE_LVAL (arg1
) == lval_internalvar
)
795 VALUE_LVAL (v
) = lval_internalvar_component
;
796 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
797 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
801 /* Given a value ARG1 of a struct or union type,
802 extract and return the value of one of its fields.
803 FIELDNO says which field.
805 For C++, must also be able to return values from static fields */
808 value_field (arg1
, fieldno
)
810 register int fieldno
;
812 return value_primitive_field (arg1
, 0, fieldno
, VALUE_TYPE (arg1
));
816 value_fn_field (arg1
, fieldno
, subfieldno
)
818 register int fieldno
;
822 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
823 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
826 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
827 0, VAR_NAMESPACE
, 0, NULL
);
828 if (! sym
) error ("Internal error: could not find physical method named %s",
829 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
831 v
= allocate_value (type
);
832 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
833 VALUE_TYPE (v
) = type
;
837 /* Return a virtual function as a value.
838 ARG1 is the object which provides the virtual function
839 table pointer. ARG1 is side-effected in calling this function.
840 F is the list of member functions which contains the desired virtual
842 J is an index into F which provides the desired virtual function. */
844 value_virtual_fn_field (arg1
, f
, j
)
849 /* First, get the virtual function table pointer. That comes
850 with a strange type, so cast it to type `pointer to long' (which
851 should serve just fine as a function type). Then, index into
852 the table, and convert final value to appropriate function type. */
853 value entry
, vfn
, vtbl
;
854 value vi
= value_from_long (builtin_type_int
,
855 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
856 struct type
*context
= lookup_pointer_type (TYPE_FN_FIELD_FCONTEXT (f
, j
));
857 if (TYPE_TARGET_TYPE (context
) != VALUE_TYPE (arg1
))
858 arg1
= value_ind (value_cast (context
, value_addr (arg1
)));
860 context
= VALUE_TYPE (arg1
);
862 /* This type may have been defined before its virtual function table
863 was. If so, fill in the virtual function table entry for the
865 if (TYPE_VPTR_FIELDNO (context
) < 0)
866 TYPE_VPTR_FIELDNO (context
)
867 = fill_in_vptr_fieldno (context
);
869 /* The virtual function table is now an array of structures
870 which have the form { int16 offset, delta; void *pfn; }. */
871 vtbl
= value_ind (value_field (arg1
, TYPE_VPTR_FIELDNO (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));
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
));
894 /* The value of a static class member does not depend
895 on its instance, only on its type. If FIELDNO >= 0,
896 then fieldno is a valid field number and is used directly.
897 Otherwise, FIELDNAME is the name of the field we are
898 searching for. If it is not a static field name, an
899 error is signaled. TYPE is the type in which we look for the
900 static field member. */
902 value_static_field (type
, fieldname
, fieldno
)
903 register struct type
*type
;
905 register int fieldno
;
913 register struct type
*t
= type
;
914 /* Look for static field. */
918 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
919 if (! strcmp (TYPE_FIELD_NAME (t
, i
), fieldname
))
921 if (TYPE_FIELD_STATIC (t
, i
))
927 error ("field `%s' is not static");
929 /* FIXME: this does not recursively check multiple baseclasses. */
930 t
= TYPE_N_BASECLASSES (t
) ? TYPE_BASECLASS (t
, 0) : 0;
935 if (destructor_name_p (fieldname
, t
))
936 error ("Cannot get value of destructor");
942 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; i
--)
944 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), fieldname
))
946 error ("Cannot get value of method \"%s\"", fieldname
);
949 t
= TYPE_N_BASECLASSES (t
) ? TYPE_BASECLASS (t
, 0) : 0;
951 error("there is no field named %s", fieldname
);
955 phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
956 sym
= lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
957 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
959 type
= TYPE_FIELD_TYPE (type
, fieldno
);
960 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
964 /* Compute the address of the baseclass which is
965 the INDEXth baseclass of TYPE. The TYPE base
966 of the object is at VALADDR. */
969 baseclass_addr (type
, index
, valaddr
, valuep
)
975 struct type
*basetype
= TYPE_BASECLASS (type
, index
);
977 if (BASETYPE_VIA_VIRTUAL (type
, index
))
979 /* Must hunt for the pointer to this virtual baseclass. */
980 register int i
, len
= TYPE_NFIELDS (type
);
981 register int n_baseclasses
= TYPE_N_BASECLASSES (type
);
982 char *vbase_name
, *type_name
= type_name_no_tag (basetype
);
984 if (TYPE_MAIN_VARIANT (basetype
))
985 basetype
= TYPE_MAIN_VARIANT (basetype
);
987 vbase_name
= (char *)alloca (strlen (type_name
) + 8);
988 sprintf (vbase_name
, "_vb$%s", type_name
);
989 /* First look for the virtual baseclass pointer
991 for (i
= n_baseclasses
; i
< len
; i
++)
993 if (! strcmp (vbase_name
, TYPE_FIELD_NAME (type
, i
)))
995 value v
= value_at (basetype
,
996 unpack_long (TYPE_FIELD_TYPE (type
, i
),
997 valaddr
+ (TYPE_FIELD_BITPOS (type
, i
) / 8)));
1000 return (char *) VALUE_CONTENTS (v
);
1003 /* Not in the fields, so try looking through the baseclasses. */
1004 for (i
= index
+1; i
< n_baseclasses
; i
++)
1008 baddr
= baseclass_addr (type
, i
, valaddr
, valuep
);
1018 /* Baseclass is easily computed. */
1021 return valaddr
+ TYPE_BASECLASS_BITPOS (type
, index
) / 8;
1024 /* Ugly hack to convert method stubs into method types.
1026 He ain't kiddin'. This demangles the name of the method into a string
1027 including argument types, parses out each argument type, generates
1028 a string casting a zero to that type, evaluates the string, and stuffs
1029 the resulting type into an argtype vector!!! Then it knows the type
1030 of the whole function (including argument types for overloading),
1031 which info used to be in the stab's but was removed to hack back
1032 the space required for them. */
1034 check_stub_method (type
, i
, j
)
1038 extern char *gdb_mangle_typename (), *strchr ();
1039 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1040 char *inner_name
= gdb_mangle_typename (type
);
1042 = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type
, i
))
1043 + strlen (inner_name
)
1044 + strlen (TYPE_FN_FIELD_PHYSNAME (f
, j
))
1046 char *demangled_name
, *cplus_demangle ();
1047 char *argtypetext
, *p
;
1048 int depth
= 0, argcount
= 1;
1049 struct type
**argtypes
;
1051 strcpy (mangled_name
, TYPE_FN_FIELDLIST_NAME (type
, i
));
1052 strcat (mangled_name
, inner_name
);
1053 strcat (mangled_name
, TYPE_FN_FIELD_PHYSNAME (f
, j
));
1054 demangled_name
= cplus_demangle (mangled_name
, 0);
1056 /* Now, read in the parameters that define this type. */
1057 argtypetext
= strchr (demangled_name
, '(') + 1;
1065 else if (*p
== ',' && depth
== 0)
1070 /* We need one more slot for the void [...] or NULL [end of arglist] */
1071 argtypes
= (struct type
**)xmalloc ((argcount
+1) * sizeof (struct type
*));
1073 argtypes
[0] = lookup_pointer_type (type
);
1076 if (*p
!= ')') /* () means no args, skip while */
1085 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
1087 char *tmp
= (char *)alloca (p
- argtypetext
+ 4);
1090 bcopy (argtypetext
, tmp
+1, p
- argtypetext
);
1091 tmp
[p
-argtypetext
+1] = ')';
1092 tmp
[p
-argtypetext
+2] = '0';
1093 tmp
[p
-argtypetext
+3] = '\0';
1094 val
= parse_and_eval (tmp
);
1095 argtypes
[argcount
] = VALUE_TYPE (val
);
1097 argtypetext
= p
+ 1;
1103 if (p
[-2] != '.') /* ... */
1104 argtypes
[argcount
] = builtin_type_void
; /* Ellist terminator */
1106 argtypes
[argcount
] = NULL
; /* List terminator */
1108 free (demangled_name
);
1109 smash_to_method_type (TYPE_FN_FIELD_TYPE (f
, j
), type
,
1110 TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
1112 TYPE_FN_FIELD_PHYSNAME (f
, j
) = mangled_name
;
1113 TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f
, j
)) &= ~TYPE_FLAG_STUB
;
1117 unpack_field_as_long (type
, valaddr
, fieldno
)
1123 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1124 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1126 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
1127 SWAP_TARGET_AND_HOST (&val
, sizeof val
);
1129 /* Extracting bits depends on endianness of the machine. */
1130 #ifdef BITS_BIG_ENDIAN
1131 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1133 val
= val
>> (bitpos
% 8);
1136 val
&= (1 << bitsize
) - 1;
1140 /* Modify the value of a bitfield. ADDR points to a block of memory in
1141 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1142 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1143 indicate which bits (in target bit order) comprise the bitfield. */
1146 modify_field (addr
, fieldval
, bitpos
, bitsize
)
1149 int bitpos
, bitsize
;
1153 /* Reject values too big to fit in the field in question.
1154 Otherwise adjoining fields may be corrupted. */
1155 if (fieldval
& ~((1<<bitsize
)-1))
1156 error ("Value %d does not fit in %d bits.", fieldval
, bitsize
);
1158 bcopy (addr
, &oword
, sizeof oword
);
1159 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To host format */
1161 /* Shifting for bit field depends on endianness of the target machine. */
1162 #ifdef BITS_BIG_ENDIAN
1163 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1166 oword
&= ~(((1 << bitsize
) - 1) << bitpos
);
1167 oword
|= fieldval
<< bitpos
;
1169 SWAP_TARGET_AND_HOST (&oword
, sizeof oword
); /* To target format */
1170 bcopy (&oword
, addr
, sizeof oword
);
1173 /* Convert C numbers into newly allocated values */
1176 value_from_long (type
, num
)
1178 register LONGEST num
;
1180 register value val
= allocate_value (type
);
1181 register enum type_code code
= TYPE_CODE (type
);
1182 register int len
= TYPE_LENGTH (type
);
1184 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
)
1186 if (len
== sizeof (char))
1187 * (char *) VALUE_CONTENTS_RAW (val
) = num
;
1188 else if (len
== sizeof (short))
1189 * (short *) VALUE_CONTENTS_RAW (val
) = num
;
1190 else if (len
== sizeof (int))
1191 * (int *) VALUE_CONTENTS_RAW (val
) = num
;
1192 else if (len
== sizeof (long))
1193 * (long *) VALUE_CONTENTS_RAW (val
) = num
;
1195 else if (len
== sizeof (long long))
1196 * (long long *) VALUE_CONTENTS_RAW (val
) = num
;
1199 error ("Integer type encountered with unexpected data length.");
1202 error ("Unexpected type encountered for integer constant.");
1204 /* num was in host byte order. So now put the value's contents
1205 into target byte order. */
1206 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1212 value_from_double (type
, num
)
1216 register value val
= allocate_value (type
);
1217 register enum type_code code
= TYPE_CODE (type
);
1218 register int len
= TYPE_LENGTH (type
);
1220 if (code
== TYPE_CODE_FLT
)
1222 if (len
== sizeof (float))
1223 * (float *) VALUE_CONTENTS_RAW (val
) = num
;
1224 else if (len
== sizeof (double))
1225 * (double *) VALUE_CONTENTS_RAW (val
) = num
;
1227 error ("Floating type encountered with unexpected data length.");
1230 error ("Unexpected type encountered for floating constant.");
1232 /* num was in host byte order. So now put the value's contents
1233 into target byte order. */
1234 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val
), len
);
1239 /* Deal with the value that is "about to be returned". */
1241 /* Return the value that a function returning now
1242 would be returning to its caller, assuming its type is VALTYPE.
1243 RETBUF is where we look for what ought to be the contents
1244 of the registers (in raw form). This is because it is often
1245 desirable to restore old values to those registers
1246 after saving the contents of interest, and then call
1247 this function using the saved values.
1248 struct_return is non-zero when the function in question is
1249 using the structure return conventions on the machine in question;
1250 0 when it is using the value returning conventions (this often
1251 means returning pointer to where structure is vs. returning value). */
1254 value_being_returned (valtype
, retbuf
, struct_return
)
1255 register struct type
*valtype
;
1256 char retbuf
[REGISTER_BYTES
];
1263 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1264 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1265 if (struct_return
) {
1266 addr
= EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
);
1268 error ("Function return value unknown");
1269 return value_at (valtype
, addr
);
1273 val
= allocate_value (valtype
);
1274 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS_RAW (val
));
1279 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1280 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1281 and TYPE is the type (which is known to be struct, union or array).
1283 On most machines, the struct convention is used unless we are
1284 using gcc and the type is of a special size. */
1285 #if !defined (USE_STRUCT_CONVENTION)
1286 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1287 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1288 || TYPE_LENGTH (value_type) == 2 \
1289 || TYPE_LENGTH (value_type) == 4 \
1290 || TYPE_LENGTH (value_type) == 8 \
1295 /* Return true if the function specified is using the structure returning
1296 convention on this machine to return arguments, or 0 if it is using
1297 the value returning convention. FUNCTION is the value representing
1298 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1299 is the type returned by the function. GCC_P is nonzero if compiled
1303 using_struct_return (function
, funcaddr
, value_type
, gcc_p
)
1306 struct type
*value_type
;
1310 register enum type_code code
= TYPE_CODE (value_type
);
1312 if (code
== TYPE_CODE_ERROR
)
1313 error ("Function return type unknown.");
1315 if (code
== TYPE_CODE_STRUCT
||
1316 code
== TYPE_CODE_UNION
||
1317 code
== TYPE_CODE_ARRAY
)
1318 return USE_STRUCT_CONVENTION (gcc_p
, value_type
);
1323 /* Store VAL so it will be returned if a function returns now.
1324 Does not verify that VAL's type matches what the current
1325 function wants to return. */
1328 set_return_value (val
)
1331 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
1335 if (code
== TYPE_CODE_ERROR
)
1336 error ("Function return type unknown.");
1338 if (code
== TYPE_CODE_STRUCT
1339 || code
== TYPE_CODE_UNION
)
1340 error ("Specifying a struct or union return value is not supported.");
1342 /* FIXME, this is bogus. We don't know what the return conventions
1343 are, or how values should be promoted.... */
1344 if (code
== TYPE_CODE_FLT
)
1346 dbuf
= value_as_double (val
);
1348 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&dbuf
);
1352 lbuf
= value_as_long (val
);
1353 STORE_RETURN_VALUE (VALUE_TYPE (val
), (char *)&lbuf
);
1358 _initialize_values ()
1360 add_cmd ("convenience", no_class
, show_convenience
,
1361 "Debugger convenience (\"$foo\") variables.\n\
1362 These variables are created when you assign them values;\n\
1363 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1364 A few convenience variables are given values automatically:\n\
1365 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1366 \"$__\" holds the contents of the last address examined with \"x\".",
1369 add_cmd ("values", no_class
, show_values
,
1370 "Elements of value history around item number IDX (or last ten).",