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)))
223 fprintf (stream, "& ");
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));
268 fprintf (stream, "& ");
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);
520 if (args[i]->code != TYPE_CODE_VOID)
522 fprintf (stream, ", ");
527 fprintf (stream, ")");
530 /* Print any asterisks or open-parentheses needed before the
531 variable name (to describe its type).
533 On outermost call, pass 0 for PASSED_A_PTR.
534 On outermost call, SHOW > 0 means should ignore
535 any typename for TYPE and show its details.
536 SHOW is always zero on recursive calls. */
539 type_print_varspec_prefix (type, stream, show, passed_a_ptr)
545 if (TYPE_NAME (type) && show <= 0)
550 switch (TYPE_CODE (type))
553 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
557 case TYPE_CODE_MEMBER:
558 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
559 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
561 fprintf (stream, "::");
565 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
570 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
576 case TYPE_CODE_ARRAY:
577 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
582 /* Print any array sizes, function arguments or close parentheses
583 needed after the variable name (to describe its type).
584 Args work like type_print_varspec_prefix. */
587 type_print_varspec_suffix (type, stream, show, passed_a_ptr)
593 if (TYPE_NAME (type) && show <= 0)
598 switch (TYPE_CODE (type))
600 case TYPE_CODE_ARRAY:
601 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
603 fprintf (stream, "[");
604 if (TYPE_LENGTH (type) >= 0)
605 fprintf (stream, "%d",
606 TYPE_LENGTH (type) / TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
607 fprintf (stream, "]");
610 case TYPE_CODE_MEMBER:
613 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
618 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
622 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
625 fprintf (stream, ")");
626 fprintf (stream, "()");
631 /* Print the name of the type (or the ultimate pointer target,
632 function value or array element), or the description of a
635 SHOW nonzero means don't print this type as just its name;
636 show its real definition even if it has a name.
637 SHOW zero means print just typename or struct tag if there is one
638 SHOW negative means abbreviate structure elements.
639 SHOW is decremented for printing of structure elements.
641 LEVEL is the depth to indent by.
642 We increase it for some recursive calls. */
645 type_print_base (type, stream, show, level)
654 register int lastval;
658 if (TYPE_NAME (type) && show <= 0)
660 fprintf (stream, TYPE_NAME (type));
664 switch (TYPE_CODE (type))
666 case TYPE_CODE_ARRAY:
668 case TYPE_CODE_MEMBER:
671 type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
674 case TYPE_CODE_STRUCT:
675 fprintf (stream, "struct ");
678 case TYPE_CODE_UNION:
679 fprintf (stream, "union ");
681 if (TYPE_NAME (type) && (name = TYPE_NAME (type)))
683 while (*name != ' ') name++;
684 fprintf (stream, "%s ", name + 1);
687 fprintf (stream, "{...}");
690 struct type *basetype, *dtype;
693 basetype = TYPE_BASECLASS (type);
696 if (TYPE_NAME (basetype) && (name = TYPE_NAME (basetype)))
698 while (*name != ' ') name++;
699 fprintf (stream, ": %s %s ",
700 TYPE_VIA_PUBLIC (dtype) ? "public" : "private",
704 basetype = TYPE_BASECLASS (basetype);
706 fprintf (stream, "{");
707 len = TYPE_NFIELDS (type);
708 if (len) fprintf (stream, "\n");
709 else fprintf (stream, "<no data fields>\n");
711 /* If there is a base class for this type,
712 do not print the field that it occupies. */
713 for (i = !! TYPE_BASECLASS (type); i < len; i++)
716 /* Don't print out virtual function table. */
717 if (! strncmp (TYPE_FIELD_NAME (type, i),
721 print_spaces (level + 4, stream);
722 if (TYPE_FIELD_STATIC (type, i))
724 fprintf (stream, "static ");
726 type_print_1 (TYPE_FIELD_TYPE (type, i),
727 TYPE_FIELD_NAME (type, i),
728 stream, show - 1, level + 4);
729 if (!TYPE_FIELD_STATIC (type, i)
730 && TYPE_FIELD_PACKED (type, i))
732 /* ??? don't know what to put here ??? */;
734 fprintf (stream, ";\n");
737 /* C++: print out the methods */
738 len = TYPE_NFN_FIELDS (type);
739 if (len) fprintf (stream, "\n");
740 for (i = 0; i < len; i++)
742 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
743 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
745 for (j = 0; j < len2; j++)
748 print_spaces (level + 4, stream);
749 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
750 fprintf (stream, "virtual ");
751 type_print_base (TYPE_FN_FIELD_TYPE (f, j), stream, 0, level);
752 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
753 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
754 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == '$')
755 type_print_method_args
756 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
757 TYPE_FN_FIELDLIST_NAME (type, i), stream);
759 type_print_method_args
760 (TYPE_FN_FIELD_ARGS (f, j), "",
761 TYPE_FN_FIELDLIST_NAME (type, i), stream);
763 fprintf (stream, ";\n");
765 if (len2) fprintf (stream, "\n");
768 print_spaces (level, stream);
774 fprintf (stream, "enum ");
775 if (TYPE_NAME (type))
777 name = TYPE_NAME (type);
778 while (*name != ' ') name++;
779 fprintf (stream, "%s ", name + 1);
782 fprintf (stream, "{...}");
785 fprintf (stream, "{");
786 len = TYPE_NFIELDS (type);
788 for (i = 0; i < len; i++)
791 if (i) fprintf (stream, ", ");
792 fprintf (stream, "%s", TYPE_FIELD_NAME (type, i));
793 if (lastval != TYPE_FIELD_VALUE (type, i))
795 fprintf (stream, " : %d", TYPE_FIELD_VALUE (type, i));
796 lastval = TYPE_FIELD_VALUE (type, i);
800 fprintf (stream, "}");
805 if (TYPE_UNSIGNED (type))
806 name = unsigned_type_table[TYPE_LENGTH (type)];
808 name = signed_type_table[TYPE_LENGTH (type)];
809 fprintf (stream, "%s", name);
813 name = float_type_table[TYPE_LENGTH (type)];
814 fprintf (stream, "%s", name);
818 fprintf (stream, "void");
822 fprintf (stream, "struct unknown");
826 error ("Invalid type code in symbol table.");
831 set_maximum_command (arg)
834 if (!arg) error_no_arg ("value for maximum elements to print");
835 print_max = atoi (arg);
841 add_com ("set-maximum", class_vars, set_maximum_command,
842 "Set NUMBER as limit on string chars or array elements to print.");
847 = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
848 bzero (unsigned_type_table, (1 + sizeof (unsigned long)));
849 unsigned_type_table[sizeof (unsigned char)] = "unsigned char";
850 unsigned_type_table[sizeof (unsigned short)] = "unsigned short";
851 unsigned_type_table[sizeof (unsigned long)] = "unsigned long";
852 unsigned_type_table[sizeof (unsigned int)] = "unsigned int";
855 = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
856 bzero (signed_type_table, (1 + sizeof (long)));
857 signed_type_table[sizeof (char)] = "char";
858 signed_type_table[sizeof (short)] = "short";
859 signed_type_table[sizeof (long)] = "long";
860 signed_type_table[sizeof (int)] = "int";
863 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
864 bzero (float_type_table, (1 + sizeof (double)));
865 float_type_table[sizeof (float)] = "float";
866 float_type_table[sizeof (double)] = "double";