1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
23 #include "initialize.h"
28 /* The value-history records all the values printed
29 by print commands during this session. Each chunk
30 records 60 consecutive values. The first chunk on
31 the chain records the most recent values.
32 The total number of values is in value_history_count. */
34 #define VALUE_HISTORY_CHUNK 60
36 struct value_history_chunk
38 struct value_history_chunk
*next
;
39 value values
[VALUE_HISTORY_CHUNK
];
42 /* Chain of chunks now in use. */
44 static struct value_history_chunk
*value_history_chain
;
46 static int value_history_count
; /* Abs number of last entry stored */
50 /* List of all value objects currently allocated
51 (except for those released by calls to release_value)
52 This is so they can be freed after each command. */
54 static value all_values
;
56 /* Allocate a value that has the correct length for type TYPE. */
64 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
));
65 VALUE_NEXT (val
) = all_values
;
67 VALUE_TYPE (val
) = type
;
68 VALUE_LVAL (val
) = not_lval
;
69 VALUE_ADDRESS (val
) = 0;
70 VALUE_OFFSET (val
) = 0;
71 VALUE_BITPOS (val
) = 0;
72 VALUE_BITSIZE (val
) = 0;
73 VALUE_REPEATED (val
) = 0;
74 VALUE_REPETITIONS (val
) = 0;
75 VALUE_REGNO (val
) = -1;
79 /* Allocate a value that has the correct length
80 for COUNT repetitions type TYPE. */
83 allocate_repeat_value (type
, count
)
89 val
= (value
) xmalloc (sizeof (struct value
) + TYPE_LENGTH (type
) * count
);
90 VALUE_NEXT (val
) = all_values
;
92 VALUE_TYPE (val
) = type
;
93 VALUE_LVAL (val
) = not_lval
;
94 VALUE_ADDRESS (val
) = 0;
95 VALUE_OFFSET (val
) = 0;
96 VALUE_BITPOS (val
) = 0;
97 VALUE_BITSIZE (val
) = 0;
98 VALUE_REPEATED (val
) = 1;
99 VALUE_REPETITIONS (val
) = count
;
100 VALUE_REGNO (val
) = -1;
104 /* Free all the values that have been allocated (except for those released).
105 Called after each command, successful or not. */
110 register value val
, next
;
112 for (val
= all_values
; val
; val
= next
)
114 next
= VALUE_NEXT (val
);
121 /* Remove VAL from the chain all_values
122 so it will not be freed automatically. */
130 if (all_values
== val
)
132 all_values
= val
->next
;
136 for (v
= all_values
; v
; v
= v
->next
)
146 /* Return a copy of the value ARG.
147 It contains the same contents, for same memory address,
148 but it's a different block of storage. */
155 register struct type
*type
= VALUE_TYPE (arg
);
156 if (VALUE_REPEATED (arg
))
157 val
= allocate_repeat_value (type
, VALUE_REPETITIONS (arg
));
159 val
= allocate_value (type
);
160 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
161 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
162 VALUE_OFFSET (val
) = VALUE_OFFSET (arg
);
163 VALUE_BITPOS (val
) = VALUE_BITPOS (arg
);
164 VALUE_BITSIZE (val
) = VALUE_BITSIZE (arg
);
165 VALUE_REGNO (val
) = VALUE_REGNO (arg
);
166 bcopy (VALUE_CONTENTS (arg
), VALUE_CONTENTS (val
),
167 TYPE_LENGTH (VALUE_TYPE (arg
))
168 * (VALUE_REPEATED (arg
) ? VALUE_REPETITIONS (arg
) : 1));
172 /* Access to the value history. */
174 /* Record a new value in the value history.
175 Returns the absolute history index of the entry. */
178 record_latest_value (val
)
183 /* Get error now if about to store an invalid float. */
184 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
)
185 value_as_double (val
);
187 /* Here we treat value_history_count as origin-zero
188 and applying to the value being stored now. */
190 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
193 register struct value_history_chunk
*new
194 = (struct value_history_chunk
*) xmalloc (sizeof (struct value_history_chunk
));
195 bzero (new->values
, sizeof new->values
);
196 new->next
= value_history_chain
;
197 value_history_chain
= new;
200 value_history_chain
->values
[i
] = val
;
203 /* Now we regard value_history_count as origin-one
204 and applying to the value just stored. */
206 return ++value_history_count
;
209 /* Return a copy of the value in the history with sequence number NUM. */
212 access_value_history (num
)
215 register struct value_history_chunk
*chunk
;
217 register int absnum
= num
;
220 absnum
+= value_history_count
;
225 error ("The history is empty.");
227 error ("There is only one value in the history.");
229 error ("History does not go back to $$%d.", -num
);
231 if (absnum
> value_history_count
)
232 error ("History has not yet reached $%d.", absnum
);
236 /* Now absnum is always absolute and origin zero. */
238 chunk
= value_history_chain
;
239 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
243 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
246 /* Clear the value history entirely.
247 Must be done when new symbol tables are loaded,
248 because the type pointers become invalid. */
251 clear_value_history ()
253 register struct value_history_chunk
*next
;
257 while (value_history_chain
)
259 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
260 if (val
= value_history_chain
->values
[i
])
262 next
= value_history_chain
->next
;
263 free (value_history_chain
);
264 value_history_chain
= next
;
266 value_history_count
= 0;
270 history_info (num_exp
)
278 num
= parse_and_eval_address (num_exp
) - 5;
280 num
= value_history_count
- 9;
285 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
287 val
= access_value_history (i
);
288 printf ("$%d = ", i
);
289 value_print (val
, stdout
);
294 /* Internal variables. These are variables within the debugger
295 that hold values assigned by debugger commands.
296 The user refers to them with a '$' prefix
297 that does not appear in the variable names stored internally. */
299 static struct internalvar
*internalvars
;
301 /* Look up an internal variable with name NAME. NAME should not
302 normally include a dollar sign.
304 If the specified internal variable does not exist,
305 one is created, with a void value. */
308 lookup_internalvar (name
)
311 register struct internalvar
*var
;
313 for (var
= internalvars
; var
; var
= var
->next
)
314 if (!strcmp (var
->name
, name
))
317 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
318 var
->name
= concat (name
, "", "");
319 var
->value
= allocate_value (builtin_type_void
);
320 release_value (var
->value
);
321 var
->next
= internalvars
;
327 value_of_internalvar (var
)
328 struct internalvar
*var
;
330 register value val
= value_copy (var
->value
);
331 VALUE_LVAL (val
) = lval_internalvar
;
332 VALUE_INTERNALVAR (val
) = var
;
336 set_internalvar_component (var
, offset
, bitpos
, bitsize
, newval
)
337 struct internalvar
*var
;
338 int offset
, bitpos
, bitsize
;
341 register char *addr
= VALUE_CONTENTS (var
->value
) + offset
;
343 modify_field (addr
, value_as_long (newval
),
346 bcopy (VALUE_CONTENTS (newval
), addr
,
347 TYPE_LENGTH (VALUE_TYPE (newval
)));
351 set_internalvar (var
, val
)
352 struct internalvar
*var
;
356 var
->value
= value_copy (val
);
357 release_value (var
->value
);
361 internalvar_name (var
)
362 struct internalvar
*var
;
367 /* Free all internalvars. Done when new symtabs are loaded,
368 because that makes the values invalid. */
371 clear_internalvars ()
373 register struct internalvar
*var
;
378 internalvars
= var
->next
;
388 register struct internalvar
*var
;
391 printf ("Debugger convenience variables:\n\n");
393 printf ("No debugger convenience variables now defined.\n\
394 Convenience variables have names starting with \"$\";\n\
395 use \"set\" as in \"set $foo = 5\" to define them.\n");
397 for (var
= internalvars
; var
; var
= var
->next
)
399 printf ("$%s: ", var
->name
);
400 value_print (var
->value
, stdout
);
405 /* Extract a value as a C number (either long or double).
406 Knows how to convert fixed values to double, or
407 floating values to long.
408 Does not deallocate the value. */
414 return unpack_long (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
418 value_as_double (val
)
421 return unpack_double (VALUE_TYPE (val
), VALUE_CONTENTS (val
));
424 /* Unpack raw data (copied from debugee) at VALADDR
425 as a long, or as a double, assuming the raw data is described
426 by type TYPE. Knows how to convert different sizes of values
427 and can convert between fixed and floating point.
429 C++: It is assumed that the front-end has taken care of
430 all matters concerning pointers to members. A pointer
431 to member which reaches here is considered to be equivalent
432 to an INT (or some size). After all, it is only an offset. */
435 unpack_long (type
, valaddr
)
439 register enum type_code code
= TYPE_CODE (type
);
440 register int len
= TYPE_LENGTH (type
);
441 register int nosign
= TYPE_UNSIGNED (type
);
443 if (code
== TYPE_CODE_ENUM
)
444 code
= TYPE_CODE_INT
;
445 if (code
== TYPE_CODE_FLT
)
447 if (len
== sizeof (float))
448 return * (float *) valaddr
;
450 if (len
== sizeof (double))
451 return * (double *) valaddr
;
453 else if (code
== TYPE_CODE_INT
&& nosign
)
455 if (len
== sizeof (char))
456 return * (unsigned char *) valaddr
;
458 if (len
== sizeof (short))
459 return * (unsigned short *) valaddr
;
461 if (len
== sizeof (int))
462 return * (unsigned int *) valaddr
;
464 if (len
== sizeof (long))
465 return * (unsigned long *) valaddr
;
467 else if (code
== TYPE_CODE_INT
)
469 if (len
== sizeof (char))
470 return * (char *) valaddr
;
472 if (len
== sizeof (short))
473 return * (short *) valaddr
;
475 if (len
== sizeof (int))
476 return * (int *) valaddr
;
478 if (len
== sizeof (long))
479 return * (long *) valaddr
;
481 else if (code
== TYPE_CODE_PTR
482 || code
== TYPE_CODE_REF
)
484 if (len
== sizeof (char *))
485 return (CORE_ADDR
) * (char **) valaddr
;
487 else if (code
== TYPE_CODE_MEMBER
)
488 error ("not impelmented: member types in unpack_long");
490 error ("Value not integer or pointer.");
494 unpack_double (type
, valaddr
)
498 register enum type_code code
= TYPE_CODE (type
);
499 register int len
= TYPE_LENGTH (type
);
500 register int nosign
= TYPE_UNSIGNED (type
);
502 if (code
== TYPE_CODE_FLT
)
504 if (INVALID_FLOAT (valaddr
, len
))
505 error ("Invalid floating value found in program.");
507 if (len
== sizeof (float))
508 return * (float *) valaddr
;
510 if (len
== sizeof (double))
511 return * (double *) valaddr
;
513 else if (code
== TYPE_CODE_INT
&& nosign
)
515 if (len
== sizeof (char))
516 return * (unsigned char *) valaddr
;
518 if (len
== sizeof (short))
519 return * (unsigned short *) valaddr
;
521 if (len
== sizeof (int))
522 return * (unsigned int *) valaddr
;
524 if (len
== sizeof (long))
525 return * (unsigned long *) valaddr
;
527 else if (code
== TYPE_CODE_INT
)
529 if (len
== sizeof (char))
530 return * (char *) valaddr
;
532 if (len
== sizeof (short))
533 return * (short *) valaddr
;
535 if (len
== sizeof (int))
536 return * (int *) valaddr
;
538 if (len
== sizeof (long))
539 return * (long *) valaddr
;
542 error ("Value not floating number.");
545 /* Given a value ARG1 of a struct or union type,
546 extract and return the value of one of its fields.
547 FIELDNO says which field.
549 For C++, must also be able to return values from static fields */
552 value_field (arg1
, fieldno
)
554 register int fieldno
;
557 register struct type
*type
= TYPE_FIELD_TYPE (VALUE_TYPE (arg1
), fieldno
);
560 /* Handle packed fields */
562 offset
= TYPE_FIELD_BITPOS (VALUE_TYPE (arg1
), fieldno
) / 8;
563 if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1
), fieldno
))
565 v
= value_from_long (type
,
566 unpack_field_as_long (VALUE_TYPE (arg1
),
567 VALUE_CONTENTS (arg1
),
569 VALUE_BITPOS (v
) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1
), fieldno
) % 8;
570 VALUE_BITSIZE (v
) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1
), fieldno
);
574 v
= allocate_value (type
);
575 bcopy (VALUE_CONTENTS (arg1
) + offset
,
579 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
580 if (VALUE_LVAL (arg1
) == lval_internalvar
)
581 VALUE_LVAL (v
) = lval_internalvar_component
;
582 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
583 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (arg1
);
588 value_fn_field (arg1
, fieldno
, subfieldno
)
590 register int fieldno
;
593 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1
), fieldno
);
594 register struct type
*type
= TYPE_FN_FIELD_TYPE (f
, subfieldno
);
597 sym
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
),
599 if (! sym
) error ("Internal error: could not find physical method named %s",
600 TYPE_FN_FIELD_PHYSNAME (f
, subfieldno
));
602 v
= allocate_value (type
);
603 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
604 VALUE_TYPE (v
) = type
;
608 /* The value of a static class member does not depend
609 on its instance, only on its type. If FIELDNO >= 0,
610 then fieldno is a valid field number and is used directly.
611 Otherwise, FIELDNAME is the name of the field we are
612 searching for. If it is not a static field name, an
613 error is signaled. TYPE is the type in which we look for the
614 static field member. */
616 value_static_field (type
, fieldname
, fieldno
)
617 register struct type
*type
;
619 register int fieldno
;
626 register struct type
*t
= type
;
627 /* Look for static field. */
631 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
632 if (! strcmp (TYPE_FIELD_NAME (t
, i
), fieldname
))
634 if (TYPE_FIELD_STATIC (t
, i
))
640 error ("field `%s' is not static");
642 t
= TYPE_BASECLASS (t
);
647 if (destructor_name_p (fieldname
, t
))
648 error ("use `info method' command to print out value of destructor");
654 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; i
--)
656 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), fieldname
))
658 error ("use `info method' command to print value of method \"%s\"", fieldname
);
661 t
= TYPE_BASECLASS (t
);
663 error("there is no field named %s", fieldname
);
668 sym
= lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
),
670 if (! sym
) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type
, fieldno
));
672 type
= TYPE_FIELD_TYPE (type
, fieldno
);
673 v
= value_at (type
, (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
678 unpack_field_as_long (type
, valaddr
, fieldno
)
684 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
685 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
686 union { int i
; char c
; } test
;
688 bcopy (valaddr
+ bitpos
/ 8, &val
, sizeof val
);
690 /* Extracting bits depends on endianness of the machine. */
694 val
= val
>> (bitpos
% 8);
696 val
= val
>> (sizeof val
* 8 - bitpos
% 8 - bitsize
);
698 val
&= (1 << bitsize
) - 1;
702 modify_field (addr
, fieldval
, bitpos
, bitsize
)
708 union { int i
; char c
; } test
;
710 bcopy (addr
, &oword
, sizeof oword
);
712 /* Shifting for bit field depends on endianness of the machine. */
715 /* not little-endian: assume big-endian. */
716 bitpos
= sizeof oword
* 8 - bitpos
- bitsize
;
718 oword
&= ~(((1 << bitsize
) - 1) << bitpos
);
719 oword
|= fieldval
<< bitpos
;
720 bcopy (&oword
, addr
, sizeof oword
);
723 /* Convert C numbers into newly allocated values */
726 value_from_long (type
, num
)
730 register value val
= allocate_value (type
);
731 register enum type_code code
= TYPE_CODE (type
);
732 register int len
= TYPE_LENGTH (type
);
734 if (code
== TYPE_CODE_INT
|| code
== TYPE_CODE_ENUM
)
736 if (len
== sizeof (char))
737 * (char *) VALUE_CONTENTS (val
) = num
;
738 else if (len
== sizeof (short))
739 * (short *) VALUE_CONTENTS (val
) = num
;
740 else if (len
== sizeof (int))
741 * (int *) VALUE_CONTENTS (val
) = num
;
742 else if (len
== sizeof (long))
743 * (long *) VALUE_CONTENTS (val
) = num
;
745 error ("Integer type encountered with unexpected data length.");
748 error ("Unexpected type encountered for integer constant.");
754 value_from_double (type
, num
)
758 register value val
= allocate_value (type
);
759 register enum type_code code
= TYPE_CODE (type
);
760 register int len
= TYPE_LENGTH (type
);
762 if (code
== TYPE_CODE_FLT
)
764 if (len
== sizeof (float))
765 * (float *) VALUE_CONTENTS (val
) = num
;
766 else if (len
== sizeof (double))
767 * (double *) VALUE_CONTENTS (val
) = num
;
769 error ("Floating type encountered with unexpected data length.");
772 error ("Unexpected type encountered for floating constant.");
777 /* Deal with the value that is "about to be returned". */
779 /* Return the value that a function returning now
780 would be returning to its caller, assuming its type is VALTYPE.
781 RETBUF is where we look for what ought to be the contents
782 of the registers (in raw form). This is because it is often
783 desirable to restore old values to those registers
784 after saving the contents of interest, and then call
785 this function using the saved values. */
788 value_being_returned (valtype
, retbuf
)
789 register struct type
*valtype
;
790 char retbuf
[REGISTER_BYTES
];
794 if (TYPE_CODE (valtype
) == TYPE_CODE_STRUCT
795 || TYPE_CODE (valtype
) == TYPE_CODE_UNION
)
796 return value_at (valtype
, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf
));
798 val
= allocate_value (valtype
);
799 EXTRACT_RETURN_VALUE (valtype
, retbuf
, VALUE_CONTENTS (val
));
804 /* Store VAL so it will be returned if a function returns now.
805 Does not verify that VAL's type matches what the current
806 function wants to return. */
809 set_return_value (val
)
812 register enum type_code code
= TYPE_CODE (VALUE_TYPE (val
));
813 char regbuf
[REGISTER_BYTES
];
817 if (code
== TYPE_CODE_STRUCT
818 || code
== TYPE_CODE_UNION
)
819 error ("Specifying a struct or union return value is not supported.");
821 if (code
== TYPE_CODE_FLT
)
823 dbuf
= value_as_double (val
);
825 STORE_RETURN_VALUE (VALUE_TYPE (val
), &dbuf
);
829 lbuf
= value_as_long (val
);
830 STORE_RETURN_VALUE (VALUE_TYPE (val
), &lbuf
);
837 add_info ("convenience", convenience_info
,
838 "Debugger convenience (\"$foo\") variables.\n\
839 These variables are created when you assign them values;\n\
840 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
841 A few convenience variables are given values automatically GDB:\n\
842 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
843 \"$__\" holds the contents of the last address examined with \"x\".");
845 add_info ("history", history_info
,
846 "Elements of value history (around item number IDX, or last ten).");