8087ef8248cd4be6e03dab3396512f068639bcf7
1 /* Print values for GNU debugger gdb.
2 Copyright (C) 1986, 1988 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 /* Maximum number of chars to print for a string pointer value
29 or vector contents. */
33 static void type_print_varspec_suffix ();
34 static void type_print_varspec_prefix ();
35 static void type_print_base ();
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 FORMAT is a format-letter, or 0 for print in natural format of data type.
45 If the object printed is a string pointer, returns
46 the number of string bytes printed. */
48 value_print (val
, stream
, format
)
53 register int i
, n
, typelen
;
55 /* A "repeated" value really contains several values in a row.
56 They are made by the @ operator.
57 Print such values as if they were arrays. */
59 if (VALUE_REPEATED (val
))
61 n
= VALUE_REPETITIONS (val
);
62 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
64 /* Print arrays of characters using string syntax. */
65 if (typelen
== 1 && TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_INT
69 for (i
= 0; i
< n
&& i
< print_max
; i
++)
72 printchar (VALUE_CONTENTS (val
)[i
], stream
, '"');
75 fprintf (stream
, "...");
80 for (i
= 0; i
< n
&& i
< print_max
; i
++)
83 fprintf (stream
, ", ");
84 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
85 VALUE_ADDRESS (val
) + typelen
* i
, stream
, format
);
88 fprintf (stream
, "...");
94 /* A simple (nonrepeated) value */
95 /* If it is a pointer, indicate what it points to. */
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
, format
);
107 /* Print data of type TYPE located at VALADDR (within GDB),
108 which came from the inferior at address ADDRESS,
109 onto stdio stream STREAM according to FORMAT
110 (a letter or 0 for natural format).
112 If the data are a string pointer, returns the number of
113 sting characters printed. */
116 val_print (type
, valaddr
, address
, stream
, format
)
125 struct type
*elttype
;
132 switch (TYPE_CODE (type
))
134 case TYPE_CODE_ARRAY
:
135 if (TYPE_LENGTH (type
) >= 0)
137 elttype
= TYPE_TARGET_TYPE (type
);
138 eltlen
= TYPE_LENGTH (elttype
);
139 len
= TYPE_LENGTH (type
) / eltlen
;
140 fprintf (stream
, "{");
141 /* For an array of chars, print with string syntax. */
142 if (eltlen
== 1 && TYPE_CODE (elttype
) == TYPE_CODE_INT
146 for (i
= 0; i
< len
&& i
< print_max
; i
++)
149 printchar (valaddr
[i
], stream
, '"');
152 fprintf (stream
, "...");
157 for (i
= 0; i
< len
&& i
< print_max
; i
++)
159 if (i
) fprintf (stream
, ", ");
160 val_print (elttype
, valaddr
+ i
* eltlen
,
164 fprintf (stream
, "...");
166 fprintf (stream
, "}");
169 /* Array of unspecified length: treat like pointer. */
174 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
177 fprintf (stream
, "0x%x", * (int *) valaddr
);
178 /* For a pointer to char or unsigned char,
179 also print the string pointed to, unless pointer is null. */
181 /* For an array of chars, print with string syntax. */
182 elttype
= TYPE_TARGET_TYPE (type
);
183 if (TYPE_LENGTH (elttype
) == 1 && TYPE_CODE (elttype
) == TYPE_CODE_INT
185 && unpack_long (type
, valaddr
) != 0)
189 for (i
= 0; i
< print_max
; i
++)
192 read_memory (unpack_long (type
, valaddr
) + i
, &c
, 1);
195 printchar (c
, stream
, '"');
199 fprintf (stream
, "...");
201 /* Return number of characters printed, plus one for the
202 terminating null if we have "reached the end". */
203 return i
+ (i
!= print_max
);
207 case TYPE_CODE_STRUCT
:
208 case TYPE_CODE_UNION
:
209 fprintf (stream
, "{");
210 len
= TYPE_NFIELDS (type
);
211 for (i
= 0; i
< len
; i
++)
213 if (i
) fprintf (stream
, ", ");
214 fprintf (stream
, "%s = ", TYPE_FIELD_NAME (type
, i
));
215 if (TYPE_FIELD_PACKED (type
, i
))
217 val
= unpack_field_as_long (type
, valaddr
, i
);
218 val_print (TYPE_FIELD_TYPE (type
, i
), &val
, 0, stream
, format
);
221 val_print (TYPE_FIELD_TYPE (type
, i
),
222 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
225 fprintf (stream
, "}");
231 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
234 len
= TYPE_NFIELDS (type
);
235 val
= unpack_long (builtin_type_int
, valaddr
);
236 for (i
= 0; i
< len
; i
++)
239 if (val
== TYPE_FIELD_VALUE (type
, i
))
243 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
245 fprintf (stream
, "%d", val
);
251 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
254 fprintf (stream
, "{");
255 type_print (type
, "", stream
, -1);
256 fprintf (stream
, "} ");
257 fprintf (stream
, "0x%x", address
);
263 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
267 TYPE_UNSIGNED (type
) ? "%u" : "%d",
268 unpack_long (type
, valaddr
));
269 if (TYPE_LENGTH (type
) == 1)
271 fprintf (stream
, " '");
272 printchar (unpack_long (type
, valaddr
), stream
, '\'');
273 fputc ('\'', stream
);
280 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
284 if (is_nan (unpack_double (type
, valaddr
)))
286 fprintf (stream
, "Nan");
290 fprintf (stream
, "%g", unpack_double (type
, valaddr
));
294 fprintf (stream
, "void");
298 error ("Invalid type code in symbol table.");
310 /* Nonzero if ARG (a double) is a NAN. */
316 int lowhalf
, highhalf
;
317 union { int i
; char c
; } test
;
319 /* Separate the high and low words of the double.
320 Distinguish big and little-endian machines. */
323 /* Big-endian machine */
324 lowhalf
= arg
.i
[1], highhalf
= arg
.i
[0];
326 lowhalf
= arg
.i
[0], highhalf
= arg
.i
[1];
328 /* Nan: exponent is the maximum possible, and fraction is nonzero. */
329 return (((highhalf
>>20) & 0x7ff) == 0x7ff
331 ! ((highhalf
& 0xfffff == 0) && (lowhalf
== 0)));
335 /* Print a description of a type TYPE
336 in the form of a declaration of a variable named VARSTRING.
337 Output goes to STREAM (via stdio).
338 If SHOW is positive, we show the contents of the outermost level
339 of structure even if there is a type name that could be used instead.
340 If SHOW is negative, we never show the details of elements' types. */
342 type_print (type
, varstring
, stream
, show
)
348 type_print_1 (type
, varstring
, stream
, show
, 0);
351 /* LEVEL is the depth to indent lines by. */
353 type_print_1 (type
, varstring
, stream
, show
, level
)
360 register enum type_code code
;
361 type_print_base (type
, stream
, show
, level
);
362 code
= TYPE_CODE (type
);
363 if ((varstring
&& *varstring
)
365 /* Need a space if going to print stars or brackets;
366 but not if we will print just a type name. */
367 ((show
> 0 || TYPE_NAME (type
) == 0)
369 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
370 || code
== TYPE_CODE_ARRAY
)))
371 fprintf (stream
, " ");
372 type_print_varspec_prefix (type
, stream
, show
, 0);
373 fprintf (stream
, "%s", varstring
);
374 type_print_varspec_suffix (type
, stream
, show
, 0);
377 /* Print any asterisks or open-parentheses needed before the
378 variable name (to describe its type).
380 On outermost call, pass 0 for PASSED_A_PTR.
381 On outermost call, SHOW > 0 means should ignore
382 any typename for TYPE and show its details.
383 SHOW is always zero on recursive calls. */
386 type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
395 if (TYPE_NAME (type
) && show
<= 0)
400 switch (TYPE_CODE (type
))
403 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
408 case TYPE_CODE_ARRAY
:
409 type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
416 /* Print any array sizes, function arguments or close parentheses
417 needed after the variable name (to describe its type).
418 Args work like type_print_varspec_prefix. */
421 type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
)
430 if (TYPE_NAME (type
) && show
<= 0)
435 switch (TYPE_CODE (type
))
437 case TYPE_CODE_ARRAY
:
438 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
440 fprintf (stream
, ")");
441 fprintf (stream
, "[");
442 if (TYPE_LENGTH (type
) >= 0)
443 fprintf (stream
, "%d",
444 TYPE_LENGTH (type
) / TYPE_LENGTH (TYPE_TARGET_TYPE (type
)));
445 fprintf (stream
, "]");
449 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
453 type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
455 fprintf (stream
, ")");
456 fprintf (stream
, "()");
461 /* Print the name of the type (or the ultimate pointer target,
462 function value or array element), or the description of a
465 SHOW nonzero means don't print this type as just its name;
466 show its real definition even if it has a name.
467 SHOW zero means print just typename or struct tag if there is one
468 SHOW negative means abbreviate structure elements.
469 SHOW is decremented for printing of structure elements.
471 LEVEL is the depth to indent by.
472 We increase it for some recursive calls. */
475 type_print_base (type
, stream
, show
, level
)
484 register int lastval
;
490 fprintf (stream
, "type unknown");
494 if (TYPE_NAME (type
) && show
<= 0)
496 fprintf (stream
, TYPE_NAME (type
));
500 switch (TYPE_CODE (type
))
502 case TYPE_CODE_ARRAY
:
505 type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
508 case TYPE_CODE_STRUCT
:
509 fprintf (stream
, "struct ");
512 case TYPE_CODE_UNION
:
513 fprintf (stream
, "union ");
515 if (TYPE_NAME (type
) && (name
= TYPE_NAME (type
)))
517 while (*name
!= ' ') name
++;
518 fprintf (stream
, "%s ", name
+ 1);
521 fprintf (stream
, "{...}");
524 fprintf (stream
, "{");
525 len
= TYPE_NFIELDS (type
);
526 fprintf (stream
, "\n");
527 for (i
= 0; i
< len
; i
++)
530 print_spaces (level
+ 4, stream
);
532 /* If this is a bit-field and there is a gap before it,
533 print a nameless field to account for the gap. */
535 if (TYPE_FIELD_PACKED (type
, i
))
537 int gap
= (TYPE_FIELD_BITPOS (type
, i
)
539 ? (TYPE_FIELD_BITPOS (type
, i
- 1)
540 + (TYPE_FIELD_PACKED (type
, i
- 1)
541 ? TYPE_FIELD_BITSIZE (type
, i
- 1)
542 : TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
- 1)) * 8))
546 fprintf (stream
, "int : %d;\n", gap
);
547 print_spaces (level
+ 4, stream
);
551 /* Print the declaration of this field. */
553 type_print_1 (TYPE_FIELD_TYPE (type
, i
),
554 TYPE_FIELD_NAME (type
, i
),
555 stream
, show
- 1, level
+ 4);
557 /* Print the field width. */
559 if (TYPE_FIELD_PACKED (type
, i
))
560 fprintf (stream
, " : %d", TYPE_FIELD_BITSIZE (type
, i
));
562 fprintf (stream
, ";\n");
564 print_spaces (level
, stream
);
570 fprintf (stream
, "enum ");
571 if (TYPE_NAME (type
))
573 name
= TYPE_NAME (type
);
574 while (*name
!= ' ') name
++;
575 fprintf (stream
, "%s ", name
+ 1);
578 fprintf (stream
, "{...}");
581 fprintf (stream
, "{");
582 len
= TYPE_NFIELDS (type
);
584 for (i
= 0; i
< len
; i
++)
587 if (i
) fprintf (stream
, ", ");
588 fprintf (stream
, "%s", TYPE_FIELD_NAME (type
, i
));
589 if (lastval
!= TYPE_FIELD_VALUE (type
, i
))
591 fprintf (stream
, " : %d", TYPE_FIELD_VALUE (type
, i
));
592 lastval
= TYPE_FIELD_VALUE (type
, i
);
596 fprintf (stream
, "}");
601 if (TYPE_UNSIGNED (type
))
602 name
= unsigned_type_table
[TYPE_LENGTH (type
)];
604 name
= signed_type_table
[TYPE_LENGTH (type
)];
605 fprintf (stream
, "%s", name
);
609 name
= float_type_table
[TYPE_LENGTH (type
)];
610 fprintf (stream
, "%s", name
);
614 fprintf (stream
, "void");
618 fprintf (stream
, "struct unknown");
622 error ("Invalid type code in symbol table.");
627 set_maximum_command (arg
)
630 if (!arg
) error_no_arg ("value for maximum elements to print");
631 print_max
= atoi (arg
);
637 add_com ("set-maximum", class_vars
, set_maximum_command
,
638 "Set NUMBER as limit on string chars or array elements to print.");
643 = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
644 bzero (unsigned_type_table
, (1 + sizeof (unsigned long)));
645 unsigned_type_table
[sizeof (unsigned char)] = "unsigned char";
646 unsigned_type_table
[sizeof (unsigned short)] = "unsigned short";
647 unsigned_type_table
[sizeof (unsigned long)] = "unsigned long";
648 unsigned_type_table
[sizeof (unsigned int)] = "unsigned int";
651 = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
652 bzero (signed_type_table
, (1 + sizeof (long)));
653 signed_type_table
[sizeof (char)] = "char";
654 signed_type_table
[sizeof (short)] = "short";
655 signed_type_table
[sizeof (long)] = "long";
656 signed_type_table
[sizeof (int)] = "int";
659 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
660 bzero (float_type_table
, (1 + sizeof (double)));
661 float_type_table
[sizeof (float)] = "float";
662 float_type_table
[sizeof (double)] = "double";
This page took 0.043077 seconds and 4 git commands to generate.