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 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_MEMBER
)
173 struct type
*domain
= TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type
));
174 struct type
*target
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
));
179 val
= unpack_long (builtin_type_int
, valaddr
);
180 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
)
184 len
= TYPE_NFN_FIELDS (domain
);
185 for (i
= 0; i
< len
; i
++)
187 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
188 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
190 for (j
= 0; j
< len2
; j
++)
193 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == val
)
203 struct symbol
*sym
= find_pc_function (val
);
205 error ("invalid pointer to member function");
206 len
= TYPE_NFN_FIELDS (domain
);
207 for (i
= 0; i
< len
; i
++)
209 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
210 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
212 for (j
= 0; j
< len2
; j
++)
215 if (!strcmp (SYMBOL_NAME (sym
), TYPE_FN_FIELD_PHYSNAME (f
, j
)))
224 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, 0);
225 fprintf (stream
, kind
);
226 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
227 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == '$')
228 type_print_method_args
229 (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
230 TYPE_FN_FIELDLIST_NAME (domain
, i
), stream
);
232 type_print_method_args
233 (TYPE_FN_FIELD_ARGS (f
, j
), "",
234 TYPE_FN_FIELDLIST_NAME (domain
, i
), stream
);
240 /* VAL is a byte offset into the structure type DOMAIN.
241 Find the name of the field for that offset and
245 len
= TYPE_NFIELDS (domain
);
246 val
<<= 3; /* @@ Make VAL into bit offset */
247 for (i
= 0; i
< len
; i
++)
249 int bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
253 if (val
< bitpos
&& i
> 0)
255 int ptrsize
= (TYPE_LENGTH (builtin_type_char
) * TYPE_LENGTH (target
));
256 /* Somehow pointing into a field. */
258 extra
= (val
- TYPE_FIELD_BITPOS (domain
, i
));
269 type_print_base (domain
, stream
, 0, 0);
270 fprintf (stream
, "::");
271 fprintf (stream
, "%s", TYPE_FIELD_NAME (domain
, i
));
273 fprintf (stream
, " + %d bytes", extra
);
275 fprintf (stream
, " (offset in bits)");
280 type_print (type
, "", stream
, -1);
281 fprintf (stream
, ") %d", val
>> 3);
285 fprintf (stream
, "0x%x", * (int *) valaddr
);
286 /* For a pointer to char or unsigned char,
287 also print the string pointed to, unless pointer is null. */
288 if ((TYPE_TARGET_TYPE (type
) == builtin_type_char
289 || TYPE_TARGET_TYPE (type
) == builtin_type_unsigned_char
)
290 && unpack_long (type
, valaddr
) != 0)
294 for (i
= 0; i
< print_max
; i
++)
297 read_memory (unpack_long (type
, valaddr
) + i
, &c
, 1);
300 printchar (c
, stream
);
304 fprintf (stream
, "...");
306 /* Return number of characters printed, plus one for the
307 terminating null if we have "reached the end". */
308 return i
+ (i
!= print_max
);
313 case TYPE_CODE_MEMBER
:
314 error ("not implemented: member type in val_print");
318 fprintf (stream
, "(0x%x &) = ", * (int *) valaddr
);
319 /* De-reference the reference. */
320 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_UNDEF
)
322 value val
= value_at (TYPE_TARGET_TYPE (type
), * (int *)valaddr
);
323 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
324 VALUE_ADDRESS (val
), stream
);
327 fprintf (stream
, "???");
330 case TYPE_CODE_STRUCT
:
331 case TYPE_CODE_UNION
:
332 fprintf (stream
, "{");
333 len
= TYPE_NFIELDS (type
);
334 if (TYPE_BASECLASS (type
))
337 fprintf (stream
, "<%s> = ", TYPE_NAME (TYPE_BASECLASS (type
)));
338 val_print (TYPE_FIELD_TYPE (type
, 0),
339 valaddr
+ TYPE_FIELD_BITPOS (type
, 0) / 8,
346 if (i
) fprintf (stream
, ", ");
347 fprintf (stream
, "%s = ", TYPE_FIELD_NAME (type
, i
));
348 /* check if static field */
349 if (TYPE_FIELD_STATIC (type
, i
))
353 v
= value_static_field (type
, TYPE_FIELD_NAME (type
, i
), i
);
354 val_print (TYPE_FIELD_TYPE (type
, i
),
355 VALUE_CONTENTS (v
), 0, stream
);
357 else if (TYPE_FIELD_PACKED (type
, i
))
359 val
= unpack_field_as_long (type
, valaddr
, i
);
360 val_print (TYPE_FIELD_TYPE (type
, i
), &val
, 0, stream
);
364 val_print (TYPE_FIELD_TYPE (type
, i
),
365 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
369 fprintf (stream
, "}");
373 len
= TYPE_NFIELDS (type
);
374 val
= unpack_long (builtin_type_int
, valaddr
);
375 for (i
= 0; i
< len
; i
++)
378 if (val
== TYPE_FIELD_VALUE (type
, i
))
382 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
384 fprintf (stream
, "%d", val
);
388 fprintf (stream
, "{");
389 type_print (type
, "", stream
, -1);
390 fprintf (stream
, "} ");
391 fprintf (stream
, "0x%x", address
);
396 TYPE_UNSIGNED (type
) ? "%u" : "%d",
397 unpack_long (type
, valaddr
));
398 if (type
== builtin_type_char
399 || type
== builtin_type_unsigned_char
)
401 fprintf (stream
, " '");
402 printchar (unpack_long (type
, valaddr
), stream
);
403 fputc ('\'', stream
);
409 if (is_nan (unpack_double (type
, valaddr
)))
411 fprintf (stream
, "Nan");
415 fprintf (stream
, "%g", unpack_double (type
, valaddr
));
419 fprintf (stream
, "void");
423 error ("Invalid type code in symbol table.");
435 /* Nonzero if ARG (a double) is a NAN. */
441 int lowhalf
, highhalf
;
442 union { int i
; char c
; } test
;
444 /* Separate the high and low words of the double.
445 Distinguish big and little-endian machines. */
448 /* Big-endian machine */
449 lowhalf
= arg
.i
[1], highhalf
= arg
.i
[0];
451 lowhalf
= arg
.i
[0], highhalf
= arg
.i
[1];
453 /* Nan: exponent is the maximum possible, and fraction is nonzero. */
454 return (((highhalf
>>20) & 0x7ff) == 0x7ff
456 ! ((highhalf
& 0xfffff == 0) && (lowhalf
== 0)));
460 /* Print a description of a type TYPE
461 in the form of a declaration of a variable named VARSTRING.
462 Output goes to STREAM (via stdio).
463 If SHOW is positive, we show the contents of the outermost level
464 of structure even if there is a type name that could be used instead.
465 If SHOW is negative, we never show the details of elements' types. */
467 type_print (type
, varstring
, stream
, show
)
473 type_print_1 (type
, varstring
, stream
, show
, 0);
476 /* LEVEL is the depth to indent lines by. */
478 type_print_1 (type
, varstring
, stream
, show
, level
)
485 register enum type_code code
;
486 type_print_base (type
, stream
, show
, level
);
487 code
= TYPE_CODE (type
);
488 if ((varstring
&& *varstring
)
490 /* Need a space if going to print stars or brackets;
491 but not if we will print just a type name. */
492 ((show
> 0 || TYPE_NAME (type
) == 0)
494 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
495 || code
== TYPE_CODE_ARRAY
496 || code
== TYPE_CODE_MEMBER
497 || code
== TYPE_CODE_REF
)))
498 fprintf (stream
, " ");
499 type_print_varspec_prefix (type
, stream
, show
, 0);
500 fprintf (stream
, "%s", varstring
);
501 type_print_varspec_suffix (type
, stream
, show
, 0);
504 /* Print the method arguments ARGS to the file STREAM. */
506 type_print_method_args (args
, prefix
, varstring
, stream
)
508 char *prefix
, *varstring
;
513 fprintf (stream
, " %s%s (", prefix
, varstring
);
514 if (args
[1] && args
[1]->code
!= TYPE_CODE_VOID
)
516 i
= 1; /* skip the class variable */
519 type_print (args
[i
++], "", stream
, 0);
522 fprintf (stream
, " ...");
525 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
527 fprintf (stream
, ", ");
532 fprintf (stream
, ")");
535 /* Print any asterisks or open-parentheses needed before the
536 variable name (to describe its type).
538 On outermost call, pass 0 for PASSED_A_PTR.
539 On outermost call, SHOW > 0 means should ignore
540 any typename for TYPE and show its details.
541 SHOW is always zero on recursive calls. */
544 type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
550 if (TYPE_NAME (type
) && show
<= 0)
555 switch (TYPE_CODE (type
))
558 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
562 case TYPE_CODE_MEMBER
:
563 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0,
566 type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0,
568 fprintf (stream
, "::");
572 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
577 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0,
583 case TYPE_CODE_ARRAY
:
584 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0,
589 /* Print any array sizes, function arguments or close parentheses
590 needed after the variable name (to describe its type).
591 Args work like type_print_varspec_prefix. */
594 type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
)
600 if (TYPE_NAME (type
) && show
<= 0)
605 switch (TYPE_CODE (type
))
607 case TYPE_CODE_ARRAY
:
608 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
610 fprintf (stream
, "[");
611 if (TYPE_LENGTH (type
) >= 0)
612 fprintf (stream
, "%d",
613 TYPE_LENGTH (type
) / TYPE_LENGTH (TYPE_TARGET_TYPE (type
)));
614 fprintf (stream
, "]");
617 case TYPE_CODE_MEMBER
:
620 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
625 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
629 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
632 fprintf (stream
, ")");
633 fprintf (stream
, "()");
638 /* Print the name of the type (or the ultimate pointer target,
639 function value or array element), or the description of a
642 SHOW nonzero means don't print this type as just its name;
643 show its real definition even if it has a name.
644 SHOW zero means print just typename or struct tag if there is one
645 SHOW negative means abbreviate structure elements.
646 SHOW is decremented for printing of structure elements.
648 LEVEL is the depth to indent by.
649 We increase it for some recursive calls. */
652 type_print_base (type
, stream
, show
, level
)
661 register int lastval
;
665 if (TYPE_NAME (type
) && show
<= 0)
667 fprintf (stream
, TYPE_NAME (type
));
671 switch (TYPE_CODE (type
))
673 case TYPE_CODE_ARRAY
:
675 case TYPE_CODE_MEMBER
:
678 type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
681 case TYPE_CODE_STRUCT
:
682 fprintf (stream
, "struct ");
685 case TYPE_CODE_UNION
:
686 fprintf (stream
, "union ");
688 if (TYPE_NAME (type
) && (name
= TYPE_NAME (type
)))
690 while (*name
!= ' ') name
++;
691 fprintf (stream
, "%s ", name
+ 1);
694 fprintf (stream
, "{...}");
697 struct type
*basetype
, *dtype
;
700 basetype
= TYPE_BASECLASS (type
);
703 if (TYPE_NAME (basetype
) && (name
= TYPE_NAME (basetype
)))
705 while (*name
!= ' ') name
++;
706 fprintf (stream
, ": %s %s ",
707 TYPE_VIA_PUBLIC (dtype
) ? "public" : "private",
711 basetype
= TYPE_BASECLASS (basetype
);
713 fprintf (stream
, "{");
714 len
= TYPE_NFIELDS (type
);
715 if (len
) fprintf (stream
, "\n");
716 else fprintf (stream
, "<no data fields>\n");
718 /* If there is a base class for this type,
719 do not print the field that it occupies. */
720 for (i
= !! TYPE_BASECLASS (type
); i
< len
; i
++)
723 /* Don't print out virtual function table. */
724 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
728 print_spaces (level
+ 4, stream
);
729 if (TYPE_FIELD_STATIC (type
, i
))
731 fprintf (stream
, "static ");
733 type_print_1 (TYPE_FIELD_TYPE (type
, i
),
734 TYPE_FIELD_NAME (type
, i
),
735 stream
, show
- 1, level
+ 4);
736 if (!TYPE_FIELD_STATIC (type
, i
)
737 && TYPE_FIELD_PACKED (type
, i
))
739 /* ??? don't know what to put here ??? */;
741 fprintf (stream
, ";\n");
744 /* C++: print out the methods */
745 len
= TYPE_NFN_FIELDS (type
);
746 if (len
) fprintf (stream
, "\n");
747 for (i
= 0; i
< len
; i
++)
749 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
750 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
752 for (j
= 0; j
< len2
; j
++)
755 print_spaces (level
+ 4, stream
);
756 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
757 fprintf (stream
, "virtual ");
758 type_print (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
))), "", stream
, 0);
759 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
760 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == '$')
761 type_print_method_args
762 (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
763 TYPE_FN_FIELDLIST_NAME (type
, i
), stream
);
765 type_print_method_args
766 (TYPE_FN_FIELD_ARGS (f
, j
), "",
767 TYPE_FN_FIELDLIST_NAME (type
, i
), stream
);
769 fprintf (stream
, ";\n");
771 if (len2
) fprintf (stream
, "\n");
774 print_spaces (level
, stream
);
780 fprintf (stream
, "enum ");
781 if (TYPE_NAME (type
))
783 name
= TYPE_NAME (type
);
784 while (*name
!= ' ') name
++;
785 fprintf (stream
, "%s ", name
+ 1);
788 fprintf (stream
, "{...}");
791 fprintf (stream
, "{");
792 len
= TYPE_NFIELDS (type
);
794 for (i
= 0; i
< len
; i
++)
797 if (i
) fprintf (stream
, ", ");
798 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
799 if (lastval
!= TYPE_FIELD_VALUE (type
, i
))
801 fprintf (stream
, " : %d", TYPE_FIELD_VALUE (type
, i
));
802 lastval
= TYPE_FIELD_VALUE (type
, i
);
806 fprintf (stream
, "}");
811 if (TYPE_UNSIGNED (type
))
812 name
= unsigned_type_table
[TYPE_LENGTH (type
)];
814 name
= signed_type_table
[TYPE_LENGTH (type
)];
815 fprintf (stream
, "%s", name
);
819 name
= float_type_table
[TYPE_LENGTH (type
)];
820 fprintf (stream
, "%s", name
);
824 fprintf (stream
, "void");
828 fprintf (stream
, "struct unknown");
832 error ("Invalid type code in symbol table.");
837 set_maximum_command (arg
)
840 if (!arg
) error_no_arg ("value for maximum elements to print");
841 print_max
= atoi (arg
);
847 add_com ("set-maximum", class_vars
, set_maximum_command
,
848 "Set NUMBER as limit on string chars or array elements to print.");
853 = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
854 bzero (unsigned_type_table
, (1 + sizeof (unsigned long)));
855 unsigned_type_table
[sizeof (unsigned char)] = "unsigned char";
856 unsigned_type_table
[sizeof (unsigned short)] = "unsigned short";
857 unsigned_type_table
[sizeof (unsigned long)] = "unsigned long";
858 unsigned_type_table
[sizeof (unsigned int)] = "unsigned int";
861 = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
862 bzero (signed_type_table
, (1 + sizeof (long)));
863 signed_type_table
[sizeof (char)] = "char";
864 signed_type_table
[sizeof (short)] = "short";
865 signed_type_table
[sizeof (long)] = "long";
866 signed_type_table
[sizeof (int)] = "int";
869 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
870 bzero (float_type_table
, (1 + sizeof (double)));
871 float_type_table
[sizeof (float)] = "float";
872 float_type_table
[sizeof (double)] = "double";