1 /* Print values for GNU debugger gdb.
2 Copyright (C) 1986 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"
27 /* Maximum number of chars to print for a string pointer value
28 or vector contents. */
32 static void type_print_varspec_suffix ();
33 static void type_print_varspec_prefix ();
34 static void type_print_base ();
35 static void type_print_method_args ();
39 char **unsigned_type_table
;
40 char **signed_type_table
;
41 char **float_type_table
;
43 /* Print the value VAL in C-ish syntax on stream STREAM.
44 If the object printed is a string pointer, returns
45 the number of string bytes printed. */
47 value_print (val
, stream
)
51 register int i
, n
, typelen
;
53 /* A "repeated" value really contains several values in a row.
54 They are made by the @ operator.
55 Print such values as if they were arrays. */
57 if (VALUE_REPEATED (val
))
59 n
= VALUE_REPETITIONS (val
);
60 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
62 /* Print arrays of characters using string syntax. */
63 if (VALUE_TYPE (val
) == builtin_type_char
64 || VALUE_TYPE (val
) == builtin_type_unsigned_char
)
67 for (i
= 0; i
< n
&& i
< print_max
; i
++)
70 printchar (VALUE_CONTENTS (val
)[i
], stream
);
73 fprintf (stream
, "...");
78 for (i
= 0; i
< n
&& i
< print_max
; i
++)
81 fprintf (stream
, ", ");
82 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
83 VALUE_ADDRESS (val
) + typelen
* i
, stream
);
86 fprintf (stream
, "...");
92 /* If it is a pointer, indicate what it points to.
94 C++: if it is a member pointer, we will take care
95 of that when we print it. */
96 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_PTR
)
98 fprintf (stream
, "(");
99 type_print (VALUE_TYPE (val
), "", stream
, -1);
100 fprintf (stream
, ") ");
102 return val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
103 VALUE_ADDRESS (val
), stream
);
107 /* Print on STREAM data stored in debugger at address VALADDR
108 according to the format of type TYPE.
109 ADDRESS is the location in the inferior that the data
110 is supposed to have come from.
112 If the data are a string pointer, returns the number of
113 sting characters printed. */
116 val_print (type
, valaddr
, address
, stream
)
124 struct type
*elttype
;
131 switch (TYPE_CODE (type
))
133 case TYPE_CODE_ARRAY
:
134 if (TYPE_LENGTH (type
) >= 0)
136 elttype
= TYPE_TARGET_TYPE (type
);
137 eltlen
= TYPE_LENGTH (elttype
);
138 len
= TYPE_LENGTH (type
) / eltlen
;
139 fprintf (stream
, "{");
140 /* For an array of chars, print with string syntax. */
141 if (elttype
== builtin_type_char
142 || elttype
== builtin_type_unsigned_char
)
145 for (i
= 0; i
< len
&& i
< print_max
; i
++)
148 printchar (valaddr
[i
], stream
);
151 fprintf (stream
, "...");
156 for (i
= 0; i
< len
&& i
< print_max
; i
++)
158 if (i
) fprintf (stream
, ", ");
159 val_print (elttype
, valaddr
+ i
* eltlen
,
163 fprintf (stream
, "...");
165 fprintf (stream
, "}");
168 /* Array of unspecified length: treat like pointer. */
171 fprintf (stream
, "0x%x", * (int *) valaddr
);
172 /* For a pointer to char or unsigned char,
173 also print the string pointed to, unless pointer is null. */
174 if ((TYPE_TARGET_TYPE (type
) == builtin_type_char
175 || TYPE_TARGET_TYPE (type
) == builtin_type_unsigned_char
)
176 && unpack_long (type
, valaddr
) != 0)
180 for (i
= 0; i
< print_max
; i
++)
183 read_memory (unpack_long (type
, valaddr
) + i
, &c
, 1);
186 printchar (c
, stream
);
190 fprintf (stream
, "...");
192 /* Return number of characters printed, plus one for the
193 terminating null if we have "reached the end". */
194 return i
+ (i
!= print_max
);
200 struct type
*domain
= TYPE_DOMAIN_TYPE (type
);
201 struct type
*target
= TYPE_TARGET_TYPE (type
);
206 val
= unpack_long (builtin_type_int
, valaddr
);
207 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
)
211 len
= TYPE_NFN_FIELDS (domain
);
212 for (i
= 0; i
< len
; i
++)
214 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
215 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
217 for (j
= 0; j
< len2
; j
++)
220 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == val
)
230 struct symbol
*sym
= find_pc_function (val
);
232 error ("invalid pointer to member function");
233 len
= TYPE_NFN_FIELDS (domain
);
234 for (i
= 0; i
< len
; i
++)
236 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
237 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
239 for (j
= 0; j
< len2
; j
++)
242 if (!strcmp (SYMBOL_NAME (sym
), TYPE_FN_FIELD_PHYSNAME (f
, j
)))
250 fprintf (stream
, "& ");
251 type_print_base (domain
, stream
, 0, 0);
252 fprintf (stream
, "::%s", kind
);
253 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, 0);
254 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
255 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == '$')
256 type_print_method_args
257 (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
258 TYPE_FN_FIELDLIST_NAME (domain
, i
), stream
);
260 type_print_method_args
261 (TYPE_FN_FIELD_ARGS (f
, j
), "",
262 TYPE_FN_FIELDLIST_NAME (domain
, i
), stream
);
268 /* VAL is a byte offset into the structure type DOMAIN.
269 Find the name of the field for that offset and
273 len
= TYPE_NFIELDS (domain
);
274 val
<<= 3; /* @@ Make VAL into bit offset */
275 for (i
= 0; i
< len
; i
++)
277 int bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
281 if (val
< bitpos
&& i
> 0)
283 int ptrsize
= (TYPE_LENGTH (builtin_type_char
) * TYPE_LENGTH (target
));
284 /* Somehow pointing into a field. */
286 extra
= (val
- TYPE_FIELD_BITPOS (domain
, i
));
296 fprintf (stream
, "& ");
297 type_print_base (domain
, stream
, 0, 0);
298 fprintf (stream
, "::");
299 fprintf (stream
, "%s", TYPE_FIELD_NAME (domain
, i
));
301 fprintf (stream
, " + %d bytes", extra
);
303 fprintf (stream
, " (offset in bits)");
308 type_print (type
, "", stream
, -1);
309 fprintf (stream
, ") %d", val
>> 3);
314 fprintf (stream
, "(0x%x &) = ", * (int *) valaddr
);
315 /* De-reference the reference. */
316 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_UNDEF
)
318 value val
= value_at (TYPE_TARGET_TYPE (type
), * (int *)valaddr
);
319 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
320 VALUE_ADDRESS (val
), stream
);
323 fprintf (stream
, "???");
326 case TYPE_CODE_STRUCT
:
327 case TYPE_CODE_UNION
:
328 fprintf (stream
, "{");
329 len
= TYPE_NFIELDS (type
);
330 if (TYPE_BASECLASS (type
))
333 fprintf (stream
, "<%s> = ", TYPE_NAME (TYPE_BASECLASS (type
)));
334 val_print (TYPE_FIELD_TYPE (type
, 0),
335 valaddr
+ TYPE_FIELD_BITPOS (type
, 0) / 8,
342 if (i
) fprintf (stream
, ", ");
343 fprintf (stream
, "%s = ", TYPE_FIELD_NAME (type
, i
));
344 /* check if static field */
345 if (TYPE_FIELD_STATIC (type
, i
))
349 v
= value_static_field (type
, TYPE_FIELD_NAME (type
, i
), i
);
350 val_print (TYPE_FIELD_TYPE (type
, i
),
351 VALUE_CONTENTS (v
), 0, stream
);
353 else if (TYPE_FIELD_PACKED (type
, i
))
355 val
= unpack_field_as_long (type
, valaddr
, i
);
356 val_print (TYPE_FIELD_TYPE (type
, i
), &val
, 0, stream
);
360 val_print (TYPE_FIELD_TYPE (type
, i
),
361 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
365 fprintf (stream
, "}");
369 len
= TYPE_NFIELDS (type
);
370 val
= unpack_long (builtin_type_int
, valaddr
);
371 for (i
= 0; i
< len
; i
++)
374 if (val
== TYPE_FIELD_VALUE (type
, i
))
378 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
380 fprintf (stream
, "%d", val
);
384 fprintf (stream
, "{");
385 type_print (type
, "", stream
, -1);
386 fprintf (stream
, "} ");
387 fprintf (stream
, "0x%x", address
);
392 TYPE_UNSIGNED (type
) ? "%u" : "%d",
393 unpack_long (type
, valaddr
));
394 if (type
== builtin_type_char
395 || type
== builtin_type_unsigned_char
)
397 fprintf (stream
, " '");
398 printchar (unpack_long (type
, valaddr
), stream
);
399 fputc ('\'', stream
);
405 if (is_nan (unpack_double (type
, valaddr
)))
407 fprintf (stream
, "Nan");
411 fprintf (stream
, "%g", unpack_double (type
, valaddr
));
415 fprintf (stream
, "void");
419 error ("Invalid type code in symbol table.");
431 /* Nonzero if ARG (a double) is a NAN. */
437 int lowhalf
, highhalf
;
438 union { int i
; char c
; } test
;
440 /* Separate the high and low words of the double.
441 Distinguish big and little-endian machines. */
444 /* Big-endian machine */
445 lowhalf
= arg
.i
[1], highhalf
= arg
.i
[0];
447 lowhalf
= arg
.i
[0], highhalf
= arg
.i
[1];
449 /* Nan: exponent is the maximum possible, and fraction is nonzero. */
450 return (((highhalf
>>20) & 0x7ff) == 0x7ff
452 ! ((highhalf
& 0xfffff == 0) && (lowhalf
== 0)));
456 /* Print a description of a type TYPE
457 in the form of a declaration of a variable named VARSTRING.
458 Output goes to STREAM (via stdio).
459 If SHOW is positive, we show the contents of the outermost level
460 of structure even if there is a type name that could be used instead.
461 If SHOW is negative, we never show the details of elements' types. */
463 type_print (type
, varstring
, stream
, show
)
469 type_print_1 (type
, varstring
, stream
, show
, 0);
472 /* LEVEL is the depth to indent lines by. */
474 type_print_1 (type
, varstring
, stream
, show
, level
)
481 register enum type_code code
;
482 type_print_base (type
, stream
, show
, level
);
483 code
= TYPE_CODE (type
);
484 if ((varstring
&& *varstring
)
486 /* Need a space if going to print stars or brackets;
487 but not if we will print just a type name. */
488 ((show
> 0 || TYPE_NAME (type
) == 0)
490 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
491 || code
== TYPE_CODE_ARRAY
492 || code
== TYPE_CODE_MPTR
493 || code
== TYPE_CODE_REF
)))
494 fprintf (stream
, " ");
495 type_print_varspec_prefix (type
, stream
, show
, 0);
496 fprintf (stream
, "%s", varstring
);
497 type_print_varspec_suffix (type
, stream
, show
, 0);
500 /* Print the method arguments ARGS to the file STREAM. */
502 type_print_method_args (args
, prefix
, varstring
, stream
)
504 char *prefix
, *varstring
;
509 fprintf (stream
, " %s%s (", prefix
, varstring
);
510 if (args
[1] && args
[1]->code
!= TYPE_CODE_VOID
)
512 i
= 1; /* skip the class variable */
515 type_print (args
[i
++], "", stream
, 0);
516 if (args
[i
]->code
!= TYPE_CODE_VOID
)
518 fprintf (stream
, ", ");
523 fprintf (stream
, ")");
526 /* Print any asterisks or open-parentheses needed before the
527 variable name (to describe its type).
529 On outermost call, pass 0 for PASSED_A_PTR.
530 On outermost call, SHOW > 0 means should ignore
531 any typename for TYPE and show its details.
532 SHOW is always zero on recursive calls. */
535 type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
541 if (TYPE_NAME (type
) && show
<= 0)
546 switch (TYPE_CODE (type
))
549 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
554 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
557 type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, 1);
558 fprintf (stream
, "::*");
562 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
567 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0,
573 case TYPE_CODE_ARRAY
:
574 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0,
579 /* Print any array sizes, function arguments or close parentheses
580 needed after the variable name (to describe its type).
581 Args work like type_print_varspec_prefix. */
584 type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
)
590 if (TYPE_NAME (type
) && show
<= 0)
595 switch (TYPE_CODE (type
))
597 case TYPE_CODE_ARRAY
:
598 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
600 fprintf (stream
, "[");
601 if (TYPE_LENGTH (type
) >= 0)
602 fprintf (stream
, "%d",
603 TYPE_LENGTH (type
) / TYPE_LENGTH (TYPE_TARGET_TYPE (type
)));
604 fprintf (stream
, "]");
613 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
617 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
620 fprintf (stream
, ")");
621 fprintf (stream
, "()");
626 /* Print the name of the type (or the ultimate pointer target,
627 function value or array element), or the description of a
630 SHOW nonzero means don't print this type as just its name;
631 show its real definition even if it has a name.
632 SHOW zero means print just typename or struct tag if there is one
633 SHOW negative means abbreviate structure elements.
634 SHOW is decremented for printing of structure elements.
636 LEVEL is the depth to indent by.
637 We increase it for some recursive calls. */
640 type_print_base (type
, stream
, show
, level
)
649 register int lastval
;
653 if (TYPE_NAME (type
) && show
<= 0)
655 fprintf (stream
, TYPE_NAME (type
));
659 switch (TYPE_CODE (type
))
661 case TYPE_CODE_ARRAY
:
666 type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
669 case TYPE_CODE_STRUCT
:
670 fprintf (stream
, "struct ");
673 case TYPE_CODE_UNION
:
674 fprintf (stream
, "union ");
676 if (TYPE_NAME (type
) && (name
= TYPE_NAME (type
)))
678 while (*name
!= ' ') name
++;
679 fprintf (stream
, "%s ", name
+ 1);
682 fprintf (stream
, "{...}");
685 struct type
*basetype
, *dtype
;
688 basetype
= TYPE_BASECLASS (type
);
691 if (TYPE_NAME (basetype
) && (name
= TYPE_NAME (basetype
)))
693 while (*name
!= ' ') name
++;
694 fprintf (stream
, ": %s %s ",
695 TYPE_VIA_PUBLIC (dtype
) ? "public" : "private",
699 basetype
= TYPE_BASECLASS (basetype
);
701 fprintf (stream
, "{");
702 len
= TYPE_NFIELDS (type
);
703 if (len
) fprintf (stream
, "\n");
704 else fprintf (stream
, "<no data fields>\n");
706 /* If there is a base class for this type,
707 do not print the field that it occupies. */
708 for (i
= !! TYPE_BASECLASS (type
); i
< len
; i
++)
711 /* Don't print out virtual function table. */
712 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
716 print_spaces (level
+ 4, stream
);
717 if (TYPE_FIELD_STATIC (type
, i
))
719 fprintf (stream
, "static ");
721 type_print_1 (TYPE_FIELD_TYPE (type
, i
),
722 TYPE_FIELD_NAME (type
, i
),
723 stream
, show
- 1, level
+ 4);
724 if (!TYPE_FIELD_STATIC (type
, i
)
725 && TYPE_FIELD_PACKED (type
, i
))
727 /* ??? don't know what to put here ??? */;
729 fprintf (stream
, ";\n");
732 /* C++: print out the methods */
733 len
= TYPE_NFN_FIELDS (type
);
734 if (len
) fprintf (stream
, "\n");
735 for (i
= 0; i
< len
; i
++)
737 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
738 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
740 for (j
= 0; j
< len2
; j
++)
743 print_spaces (level
+ 4, stream
);
744 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
745 fprintf (stream
, "virtual ");
746 type_print_base (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, level
);
747 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, 0);
748 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
749 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == '$')
750 type_print_method_args
751 (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
752 TYPE_FN_FIELDLIST_NAME (type
, i
), stream
);
754 type_print_method_args
755 (TYPE_FN_FIELD_ARGS (f
, j
), "",
756 TYPE_FN_FIELDLIST_NAME (type
, i
), stream
);
758 fprintf (stream
, ";\n");
760 if (len2
) fprintf (stream
, "\n");
763 print_spaces (level
, stream
);
769 fprintf (stream
, "enum ");
770 if (TYPE_NAME (type
))
772 name
= TYPE_NAME (type
);
773 while (*name
!= ' ') name
++;
774 fprintf (stream
, "%s ", name
+ 1);
777 fprintf (stream
, "{...}");
780 fprintf (stream
, "{");
781 len
= TYPE_NFIELDS (type
);
783 for (i
= 0; i
< len
; i
++)
786 if (i
) fprintf (stream
, ", ");
787 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
788 if (lastval
!= TYPE_FIELD_VALUE (type
, i
))
790 fprintf (stream
, " : %d", TYPE_FIELD_VALUE (type
, i
));
791 lastval
= TYPE_FIELD_VALUE (type
, i
);
795 fprintf (stream
, "}");
800 if (TYPE_UNSIGNED (type
))
801 name
= unsigned_type_table
[TYPE_LENGTH (type
)];
803 name
= signed_type_table
[TYPE_LENGTH (type
)];
804 fprintf (stream
, "%s", name
);
808 name
= float_type_table
[TYPE_LENGTH (type
)];
809 fprintf (stream
, "%s", name
);
813 fprintf (stream
, "void");
817 fprintf (stream
, "struct unknown");
821 error ("Invalid type code in symbol table.");
826 set_maximum_command (arg
)
829 if (!arg
) error_no_arg ("value for maximum elements to print");
830 print_max
= atoi (arg
);
836 add_com ("set-maximum", class_vars
, set_maximum_command
,
837 "Set NUMBER as limit on string chars or array elements to print.");
842 = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
843 bzero (unsigned_type_table
, (1 + sizeof (unsigned long)));
844 unsigned_type_table
[sizeof (unsigned char)] = "unsigned char";
845 unsigned_type_table
[sizeof (unsigned short)] = "unsigned short";
846 unsigned_type_table
[sizeof (unsigned long)] = "unsigned long";
847 unsigned_type_table
[sizeof (unsigned int)] = "unsigned int";
850 = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
851 bzero (signed_type_table
, (1 + sizeof (long)));
852 signed_type_table
[sizeof (char)] = "char";
853 signed_type_table
[sizeof (short)] = "short";
854 signed_type_table
[sizeof (long)] = "long";
855 signed_type_table
[sizeof (int)] = "int";
858 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
859 bzero (float_type_table
, (1 + sizeof (double)));
860 float_type_table
[sizeof (float)] = "float";
861 float_type_table
[sizeof (double)] = "double";