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 ();
38 char **unsigned_type_table
;
39 char **signed_type_table
;
40 char **float_type_table
;
42 /* Print the value VAL in C-ish syntax on stream STREAM.
43 If the object printed is a string pointer, returns
44 the number of string bytes printed. */
46 value_print (val
, stream
)
50 register int i
, n
, typelen
;
52 /* A "repeated" value really contains several values in a row.
53 They are made by the @ operator.
54 Print such values as if they were arrays. */
56 if (VALUE_REPEATED (val
))
58 n
= VALUE_REPETITIONS (val
);
59 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
61 /* Print arrays of characters using string syntax. */
62 if (VALUE_TYPE (val
) == builtin_type_char
63 || VALUE_TYPE (val
) == builtin_type_unsigned_char
)
66 for (i
= 0; i
< n
&& i
< print_max
; i
++)
69 printchar (VALUE_CONTENTS (val
)[i
], stream
);
72 fprintf (stream
, "...");
77 for (i
= 0; i
< n
&& i
< print_max
; i
++)
80 fprintf (stream
, ", ");
81 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
82 VALUE_ADDRESS (val
) + typelen
* i
, stream
);
85 fprintf (stream
, "...");
91 /* A simple (nonrepeated) value */
92 /* If it is a pointer, indicate what it points to. */
93 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_PTR
)
95 fprintf (stream
, "(");
96 type_print (VALUE_TYPE (val
), "", stream
, -1);
97 fprintf (stream
, ") ");
99 return val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
100 VALUE_ADDRESS (val
), stream
);
104 /* Print on STREAM data stored in debugger at address VALADDR
105 according to the format of type TYPE.
106 ADDRESS is the location in the inferior that the data
107 is supposed to have come from.
109 If the data are a string pointer, returns the number of
110 sting characters printed. */
113 val_print (type
, valaddr
, address
, stream
)
121 struct type
*elttype
;
128 switch (TYPE_CODE (type
))
130 case TYPE_CODE_ARRAY
:
131 if (TYPE_LENGTH (type
) >= 0)
133 elttype
= TYPE_TARGET_TYPE (type
);
134 eltlen
= TYPE_LENGTH (elttype
);
135 len
= TYPE_LENGTH (type
) / eltlen
;
136 fprintf (stream
, "{");
137 /* For an array of chars, print with string syntax. */
138 if (elttype
== builtin_type_char
139 || elttype
== builtin_type_unsigned_char
)
142 for (i
= 0; i
< len
&& i
< print_max
; i
++)
145 printchar (valaddr
[i
], stream
);
148 fprintf (stream
, "...");
153 for (i
= 0; i
< len
&& i
< print_max
; i
++)
155 if (i
) fprintf (stream
, ", ");
156 val_print (elttype
, valaddr
+ i
* eltlen
,
160 fprintf (stream
, "...");
162 fprintf (stream
, "}");
165 /* Array of unspecified length: treat like pointer. */
168 fprintf (stream
, "0x%x", * (int *) valaddr
);
169 /* For a pointer to char or unsigned char,
170 also print the string pointed to, unless pointer is null. */
171 if ((TYPE_TARGET_TYPE (type
) == builtin_type_char
172 || TYPE_TARGET_TYPE (type
) == builtin_type_unsigned_char
)
173 && unpack_long (type
, valaddr
) != 0)
177 for (i
= 0; i
< print_max
; i
++)
180 read_memory (unpack_long (type
, valaddr
) + i
, &c
, 1);
183 printchar (c
, stream
);
187 fprintf (stream
, "...");
189 /* Return number of characters printed, plus one for the
190 terminating null if we have "reached the end". */
191 return i
+ (i
!= print_max
);
195 case TYPE_CODE_STRUCT
:
196 case TYPE_CODE_UNION
:
197 fprintf (stream
, "{");
198 len
= TYPE_NFIELDS (type
);
199 for (i
= 0; i
< len
; i
++)
201 if (i
) fprintf (stream
, ", ");
202 fprintf (stream
, "%s = ", TYPE_FIELD_NAME (type
, i
));
203 if (TYPE_FIELD_PACKED (type
, i
))
205 val
= unpack_field_as_long (type
, valaddr
, i
);
206 val_print (TYPE_FIELD_TYPE (type
, i
), &val
, 0, stream
);
209 val_print (TYPE_FIELD_TYPE (type
, i
),
210 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
213 fprintf (stream
, "}");
217 len
= TYPE_NFIELDS (type
);
218 val
= unpack_long (builtin_type_int
, valaddr
);
219 for (i
= 0; i
< len
; i
++)
222 if (val
== TYPE_FIELD_VALUE (type
, i
))
226 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
228 fprintf (stream
, "%d", val
);
232 fprintf (stream
, "{");
233 type_print (type
, "", stream
, -1);
234 fprintf (stream
, "} ");
235 fprintf (stream
, "0x%x", address
);
240 TYPE_UNSIGNED (type
) ? "%u" : "%d",
241 unpack_long (type
, valaddr
));
242 if (type
== builtin_type_char
243 || type
== builtin_type_unsigned_char
)
245 fprintf (stream
, " '");
246 printchar (unpack_long (type
, valaddr
), stream
);
247 fputc ('\'', stream
);
253 if (is_nan (unpack_double (type
, valaddr
)))
255 fprintf (stream
, "Nan");
259 fprintf (stream
, "%g", unpack_double (type
, valaddr
));
263 fprintf (stream
, "void");
267 error ("Invalid type code in symbol table.");
279 /* Nonzero if ARG (a double) is a NAN. */
285 int lowhalf
, highhalf
;
286 union { int i
; char c
; } test
;
288 /* Separate the high and low words of the double.
289 Distinguish big and little-endian machines. */
292 /* Big-endian machine */
293 lowhalf
= arg
.i
[1], highhalf
= arg
.i
[0];
295 lowhalf
= arg
.i
[0], highhalf
= arg
.i
[1];
297 /* Nan: exponent is the maximum possible, and fraction is nonzero. */
298 return (((highhalf
>>20) & 0x7ff) == 0x7ff
300 ! ((highhalf
& 0xfffff == 0) && (lowhalf
== 0)));
304 /* Print a description of a type TYPE
305 in the form of a declaration of a variable named VARSTRING.
306 Output goes to STREAM (via stdio).
307 If SHOW is positive, we show the contents of the outermost level
308 of structure even if there is a type name that could be used instead.
309 If SHOW is negative, we never show the details of elements' types. */
311 type_print (type
, varstring
, stream
, show
)
317 type_print_1 (type
, varstring
, stream
, show
, 0);
320 /* LEVEL is the depth to indent lines by. */
322 type_print_1 (type
, varstring
, stream
, show
, level
)
329 register enum type_code code
;
330 type_print_base (type
, stream
, show
, level
);
331 code
= TYPE_CODE (type
);
332 if ((varstring
&& *varstring
)
334 /* Need a space if going to print stars or brackets;
335 but not if we will print just a type name. */
336 ((show
> 0 || TYPE_NAME (type
) == 0)
338 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
339 || code
== TYPE_CODE_ARRAY
)))
340 fprintf (stream
, " ");
341 type_print_varspec_prefix (type
, stream
, show
, 0);
342 fprintf (stream
, "%s", varstring
);
343 type_print_varspec_suffix (type
, stream
, show
, 0);
346 /* Print any asterisks or open-parentheses needed before the
347 variable name (to describe its type).
349 On outermost call, pass 0 for PASSED_A_PTR.
350 On outermost call, SHOW > 0 means should ignore
351 any typename for TYPE and show its details.
352 SHOW is always zero on recursive calls. */
355 type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
361 if (TYPE_NAME (type
) && show
<= 0)
366 switch (TYPE_CODE (type
))
369 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
374 case TYPE_CODE_ARRAY
:
375 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
382 /* Print any array sizes, function arguments or close parentheses
383 needed after the variable name (to describe its type).
384 Args work like type_print_varspec_prefix. */
387 type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
)
393 if (TYPE_NAME (type
) && show
<= 0)
398 switch (TYPE_CODE (type
))
400 case TYPE_CODE_ARRAY
:
401 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
403 fprintf (stream
, ")");
404 fprintf (stream
, "[");
405 if (TYPE_LENGTH (type
) >= 0)
406 fprintf (stream
, "%d",
407 TYPE_LENGTH (type
) / TYPE_LENGTH (TYPE_TARGET_TYPE (type
)));
408 fprintf (stream
, "]");
412 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
416 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
418 fprintf (stream
, ")");
419 fprintf (stream
, "()");
424 /* Print the name of the type (or the ultimate pointer target,
425 function value or array element), or the description of a
428 SHOW nonzero means don't print this type as just its name;
429 show its real definition even if it has a name.
430 SHOW zero means print just typename or struct tag if there is one
431 SHOW negative means abbreviate structure elements.
432 SHOW is decremented for printing of structure elements.
434 LEVEL is the depth to indent by.
435 We increase it for some recursive calls. */
438 type_print_base (type
, stream
, show
, level
)
447 register int lastval
;
451 if (TYPE_NAME (type
) && show
<= 0)
453 fprintf (stream
, TYPE_NAME (type
));
457 switch (TYPE_CODE (type
))
459 case TYPE_CODE_ARRAY
:
462 type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
465 case TYPE_CODE_STRUCT
:
466 fprintf (stream
, "struct ");
469 case TYPE_CODE_UNION
:
470 fprintf (stream
, "union ");
472 if (TYPE_NAME (type
) && (name
= TYPE_NAME (type
)))
474 while (*name
!= ' ') name
++;
475 fprintf (stream
, "%s ", name
+ 1);
478 fprintf (stream
, "{...}");
481 fprintf (stream
, "{");
482 len
= TYPE_NFIELDS (type
);
483 fprintf (stream
, "\n");
484 for (i
= 0; i
< len
; i
++)
487 print_spaces (level
+ 4, stream
);
489 /* If this is a bit-field and there is a gap before it,
490 print a nameless field to account for the gap. */
492 if (TYPE_FIELD_PACKED (type
, i
))
494 int gap
= (TYPE_FIELD_BITPOS (type
, i
)
496 ? (TYPE_FIELD_BITPOS (type
, i
- 1)
497 + (TYPE_FIELD_PACKED (type
, i
- 1)
498 ? TYPE_FIELD_BITSIZE (type
, i
- 1)
499 : TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
- 1)) * 8))
503 fprintf (stream
, "int : %d;\n", gap
);
504 print_spaces (level
+ 4, stream
);
508 /* Print the declaration of this field. */
510 type_print_1 (TYPE_FIELD_TYPE (type
, i
),
511 TYPE_FIELD_NAME (type
, i
),
512 stream
, show
- 1, level
+ 4);
514 /* Print the field width. */
516 if (TYPE_FIELD_PACKED (type
, i
))
517 fprintf (stream
, " : %d", TYPE_FIELD_BITSIZE (type
, i
));
519 fprintf (stream
, ";\n");
521 print_spaces (level
, stream
);
527 fprintf (stream
, "enum ");
528 if (TYPE_NAME (type
))
530 name
= TYPE_NAME (type
);
531 while (*name
!= ' ') name
++;
532 fprintf (stream
, "%s ", name
+ 1);
535 fprintf (stream
, "{...}");
538 fprintf (stream
, "{");
539 len
= TYPE_NFIELDS (type
);
541 for (i
= 0; i
< len
; i
++)
544 if (i
) fprintf (stream
, ", ");
545 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
546 if (lastval
!= TYPE_FIELD_VALUE (type
, i
))
548 fprintf (stream
, " : %d", TYPE_FIELD_VALUE (type
, i
));
549 lastval
= TYPE_FIELD_VALUE (type
, i
);
553 fprintf (stream
, "}");
558 if (TYPE_UNSIGNED (type
))
559 name
= unsigned_type_table
[TYPE_LENGTH (type
)];
561 name
= signed_type_table
[TYPE_LENGTH (type
)];
562 fprintf (stream
, "%s", name
);
566 name
= float_type_table
[TYPE_LENGTH (type
)];
567 fprintf (stream
, "%s", name
);
571 fprintf (stream
, "void");
575 fprintf (stream
, "struct unknown");
579 error ("Invalid type code in symbol table.");
584 set_maximum_command (arg
)
587 if (!arg
) error_no_arg ("value for maximum elements to print");
588 print_max
= atoi (arg
);
594 add_com ("set-maximum", class_vars
, set_maximum_command
,
595 "Set NUMBER as limit on string chars or array elements to print.");
600 = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
601 bzero (unsigned_type_table
, (1 + sizeof (unsigned long)));
602 unsigned_type_table
[sizeof (unsigned char)] = "unsigned char";
603 unsigned_type_table
[sizeof (unsigned short)] = "unsigned short";
604 unsigned_type_table
[sizeof (unsigned long)] = "unsigned long";
605 unsigned_type_table
[sizeof (unsigned int)] = "unsigned int";
608 = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
609 bzero (signed_type_table
, (1 + sizeof (long)));
610 signed_type_table
[sizeof (char)] = "char";
611 signed_type_table
[sizeof (short)] = "short";
612 signed_type_table
[sizeof (long)] = "long";
613 signed_type_table
[sizeof (int)] = "int";
616 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
617 bzero (float_type_table
, (1 + sizeof (double)));
618 float_type_table
[sizeof (float)] = "float";
619 float_type_table
[sizeof (double)] = "double";