1 /* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
34 /* Prototypes for local functions */
37 print_hex_chars
PARAMS ((FILE *, unsigned char *, unsigned int));
40 show_print
PARAMS ((char *, int));
43 set_print
PARAMS ((char *, int));
46 set_radix
PARAMS ((char *, int, struct cmd_list_element
*));
49 set_output_radix
PARAMS ((char *, int, struct cmd_list_element
*));
52 value_print_array_elements
PARAMS ((value
, FILE *, int, enum val_prettyprint
));
54 /* Maximum number of chars to print for a string pointer value
55 or vector contents, or UINT_MAX for no limit. */
57 unsigned int print_max
;
59 /* Default input and output radixes, and output format letter. */
61 unsigned input_radix
= 10;
62 unsigned output_radix
= 10;
63 int output_format
= 0;
65 /* Print repeat counts if there are more than this many repetitions of an
66 element in an array. Referenced by the low level language dependent
69 unsigned int repeat_count_threshold
= 10;
71 int prettyprint_structs
; /* Controls pretty printing of structures */
72 int prettyprint_arrays
; /* Controls pretty printing of arrays. */
74 /* If nonzero, causes unions inside structures or other unions to be
77 int unionprint
; /* Controls printing of nested unions. */
79 /* If nonzero, causes machine addresses to be printed in certain contexts. */
81 int addressprint
; /* Controls printing of machine addresses */
84 /* Print data of type TYPE located at VALADDR (within GDB),
85 which came from the inferior at address ADDRESS,
86 onto stdio stream STREAM according to FORMAT
87 (a letter or 0 for natural format). The data at VALADDR
88 is in target byte order.
90 If the data are a string pointer, returns the number of
91 sting characters printed.
93 if DEREF_REF is nonzero, then dereference references,
94 otherwise just print them like pointers.
96 The PRETTY parameter controls prettyprinting. */
99 val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
,
108 enum val_prettyprint pretty
;
110 if (pretty
== Val_pretty_default
)
112 pretty
= prettyprint_structs
? Val_prettyprint
: Val_no_prettyprint
;
117 /* Ensure that the type is complete and not just a stub. If the type is
118 only a stub and we can't find and substitute its complete type, then
119 print appropriate string and return. Typical types that my be stubs
120 are structs, unions, and C++ methods. */
122 check_stub_type (type
);
123 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
125 fprintf_filtered (stream
, "<incomplete type>");
130 return (LA_VAL_PRINT (type
, valaddr
, address
, stream
, format
, deref_ref
,
134 /* Print the value VAL in C-ish syntax on stream STREAM.
135 FORMAT is a format-letter, or 0 for print in natural format of data type.
136 If the object printed is a string pointer, returns
137 the number of string bytes printed. */
140 value_print (val
, stream
, format
, pretty
)
144 enum val_prettyprint pretty
;
146 register unsigned int n
, typelen
;
150 printf_filtered ("<address of value unknown>");
153 if (VALUE_OPTIMIZED_OUT (val
))
155 printf_filtered ("<value optimized out>");
159 /* A "repeated" value really contains several values in a row.
160 They are made by the @ operator.
161 Print such values as if they were arrays. */
163 if (VALUE_REPEATED (val
))
165 n
= VALUE_REPETITIONS (val
);
166 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
167 fprintf_filtered (stream
, "{");
168 /* Print arrays of characters using string syntax. */
169 if (typelen
== 1 && TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_INT
171 LA_PRINT_STRING (stream
, VALUE_CONTENTS (val
), n
, 0);
174 value_print_array_elements (val
, stream
, format
, pretty
);
176 fprintf_filtered (stream
, "}");
177 return (n
* typelen
);
181 struct type
*type
= VALUE_TYPE (val
);
183 /* If it is a pointer, indicate what it points to.
185 Print type also if it is a reference.
187 C++: if it is a member pointer, we will take care
188 of that when we print it. */
189 if (TYPE_CODE (type
) == TYPE_CODE_PTR
||
190 TYPE_CODE (type
) == TYPE_CODE_REF
)
192 /* Hack: remove (char *) for char strings. Their
193 type is indicated by the quoted string anyway. */
194 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&&
195 TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) == sizeof(char) &&
196 TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_INT
&&
197 !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type
)))
203 fprintf_filtered (stream
, "(");
204 type_print (type
, "", stream
, -1);
205 fprintf_filtered (stream
, ") ");
208 return (val_print (type
, VALUE_CONTENTS (val
),
209 VALUE_ADDRESS (val
), stream
, format
, 1, 0, pretty
));
213 /* Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
216 val_print_type_code_int (type
, valaddr
, stream
)
222 /* Pointer to first (i.e. lowest address) nonzero character. */
226 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
228 if (TYPE_UNSIGNED (type
))
230 /* First figure out whether the number in fact has zeros
231 in all its bytes more significant than least significant
232 sizeof (LONGEST) ones. */
233 len
= TYPE_LENGTH (type
);
235 #if TARGET_BYTE_ORDER == BIG_ENDIAN
237 len
> sizeof (LONGEST
) && p
< valaddr
+ TYPE_LENGTH (type
);
239 #else /* Little endian. */
240 first_addr
= valaddr
;
241 for (p
= valaddr
+ TYPE_LENGTH (type
);
242 len
> sizeof (LONGEST
) && p
>= valaddr
;
244 #endif /* Little endian. */
255 #if TARGET_BYTE_ORDER == BIG_ENDIAN
258 if (len
<= sizeof (LONGEST
))
260 /* We can print it in decimal. */
263 #if defined (LONG_LONG)
268 unpack_long (BUILTIN_TYPE_LONGEST
, first_addr
));
272 /* It is big, so print it in hex. */
273 print_hex_chars (stream
, (unsigned char *) first_addr
, len
);
278 /* Signed. One could assume two's complement (a reasonable
279 assumption, I think) and do better than this. */
280 print_hex_chars (stream
, (unsigned char *) valaddr
,
286 #ifdef PRINT_TYPELESS_INTEGER
287 PRINT_TYPELESS_INTEGER (stream
, type
, unpack_long (type
, valaddr
));
289 fprintf_filtered (stream
, TYPE_UNSIGNED (type
) ?
290 #if defined (LONG_LONG)
295 unpack_long (type
, valaddr
));
300 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
304 print_floating (valaddr
, type
, stream
)
311 unsigned len
= TYPE_LENGTH (type
);
313 #if defined (IEEE_FLOAT)
315 /* Check for NaN's. Note that this code does not depend on us being
316 on an IEEE conforming system. It only depends on the target
317 machine using IEEE representation. This means (a)
318 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
319 be defined for systems like the 68881, which uses IEEE
320 representation, but is not IEEE conforming. */
324 /* Is the sign bit 0? */
326 /* Is it is a NaN (i.e. the exponent is all ones and
327 the fraction is nonzero)? */
330 if (len
== sizeof (float))
332 /* It's single precision. */
333 memcpy ((char *) &low
, valaddr
, sizeof (low
));
334 /* target -> host. */
335 SWAP_TARGET_AND_HOST (&low
, sizeof (float));
336 nonnegative
= low
>= 0;
337 is_nan
= ((((low
>> 23) & 0xFF) == 0xFF)
338 && 0 != (low
& 0x7FFFFF));
344 /* It's double precision. Get the high and low words. */
346 #if TARGET_BYTE_ORDER == BIG_ENDIAN
347 memcpy (&low
, valaddr
+4, sizeof (low
));
348 memcpy (&high
, valaddr
+0, sizeof (high
));
350 memcpy (&low
, valaddr
+0, sizeof (low
));
351 memcpy (&high
, valaddr
+4, sizeof (high
));
353 SWAP_TARGET_AND_HOST (&low
, sizeof (low
));
354 SWAP_TARGET_AND_HOST (&high
, sizeof (high
));
355 nonnegative
= high
>= 0;
356 is_nan
= (((high
>> 20) & 0x7ff) == 0x7ff
357 && ! ((((high
& 0xfffff) == 0)) && (low
== 0)));
363 /* The meaning of the sign and fraction is not defined by IEEE.
364 But the user might know what they mean. For example, they
365 (in an implementation-defined manner) distinguish between
366 signaling and quiet NaN's. */
368 fprintf_filtered (stream
, "-NaN(0x%lx%.8lx)" + nonnegative
,
371 fprintf_filtered (stream
, "-NaN(0x%lx)" + nonnegative
, low
);
375 #endif /* IEEE_FLOAT. */
377 doub
= unpack_double (type
, valaddr
, &inv
);
379 fprintf_filtered (stream
, "<invalid float value>");
381 fprintf_filtered (stream
, len
<= sizeof(float) ? "%.9g" : "%.17g", doub
);
384 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
387 print_hex_chars (stream
, valaddr
, len
)
389 unsigned char *valaddr
;
394 fprintf_filtered (stream
, "0x");
395 #if TARGET_BYTE_ORDER == BIG_ENDIAN
399 #else /* Little endian. */
400 for (p
= valaddr
+ len
- 1;
405 fprintf_filtered (stream
, "%02x", *p
);
409 /* Called by various <lang>_val_print routines to print elements of an
410 array in the form "<elem1>, <elem2>, <elem3>, ...".
412 (FIXME?) Assumes array element separator is a comma, which is correct
413 for all languages currently handled.
414 (FIXME?) Some languages have a notation for repeated array elements,
415 perhaps we should try to use that notation when appropriate.
419 val_print_array_elements (type
, valaddr
, address
, stream
, format
, deref_ref
,
428 enum val_prettyprint pretty
;
431 unsigned int things_printed
= 0;
433 struct type
*elttype
;
435 /* Position of the array element we are examining to see
436 whether it is repeated. */
438 /* Number of repetitions we have detected so far. */
441 elttype
= TYPE_TARGET_TYPE (type
);
442 eltlen
= TYPE_LENGTH (elttype
);
443 len
= TYPE_LENGTH (type
) / eltlen
;
445 for (; i
< len
&& things_printed
< print_max
; i
++)
449 if (prettyprint_arrays
)
451 fprintf_filtered (stream
, ",\n");
452 print_spaces_filtered (2 + 2 * recurse
, stream
);
456 fprintf_filtered (stream
, ", ");
459 wrap_here (n_spaces (2 + 2 * recurse
));
463 while ((rep1
< len
) &&
464 !memcmp (valaddr
+ i
* eltlen
, valaddr
+ rep1
* eltlen
, eltlen
))
470 if (reps
> repeat_count_threshold
)
472 val_print (elttype
, valaddr
+ i
* eltlen
, 0, stream
, format
,
473 deref_ref
, recurse
+ 1, pretty
);
474 fprintf_filtered (stream
, " <repeats %u times>", reps
);
476 things_printed
+= repeat_count_threshold
;
480 val_print (elttype
, valaddr
+ i
* eltlen
, 0, stream
, format
,
481 deref_ref
, recurse
+ 1, pretty
);
487 fprintf_filtered (stream
, "...");
492 value_print_array_elements (val
, stream
, format
, pretty
)
496 enum val_prettyprint pretty
;
498 unsigned int things_printed
= 0;
499 register unsigned int i
, n
, typelen
;
500 /* Position of the array elem we are examining to see if it is repeated. */
502 /* Number of repetitions we have detected so far. */
505 n
= VALUE_REPETITIONS (val
);
506 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
507 for (i
= 0; i
< n
&& things_printed
< print_max
; i
++)
511 fprintf_filtered (stream
, ", ");
517 while (rep1
< n
&& !memcmp (VALUE_CONTENTS (val
) + typelen
* i
,
518 VALUE_CONTENTS (val
) + typelen
* rep1
,
525 if (reps
> repeat_count_threshold
)
527 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
528 VALUE_ADDRESS (val
) + typelen
* i
, stream
, format
, 1,
530 fprintf (stream
, " <repeats %u times>", reps
);
532 things_printed
+= repeat_count_threshold
;
536 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
537 VALUE_ADDRESS (val
) + typelen
* i
, stream
, format
, 1,
544 fprintf_filtered (stream
, "...");
550 /* Validate an input or output radix setting, and make sure the user
551 knows what they really did here. Radix setting is confusing, e.g.
552 setting the input radix to "10" never changes it! */
556 set_input_radix (args
, from_tty
, c
)
559 struct cmd_list_element
*c
;
561 unsigned radix
= *(unsigned *)c
->var
;
564 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
565 radix
, radix
, radix
);
571 set_output_radix (args
, from_tty
, c
)
574 struct cmd_list_element
*c
;
576 unsigned radix
= *(unsigned *)c
->var
;
579 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
580 radix
, radix
, radix
);
582 /* FIXME, we really should be able to validate the setting BEFORE
593 output_format
= 'o'; /* octal */
597 error ("Unsupported radix ``decimal %d''; using decimal output",
604 set_radix (arg
, from_tty
, c
)
607 struct cmd_list_element
*c
;
609 unsigned radix
= *(unsigned *)c
->var
;
612 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
613 radix
, radix
, radix
);
616 output_radix
= radix
;
618 set_output_radix (arg
, 0, c
);
623 set_print (arg
, from_tty
)
628 "\"set print\" must be followed by the name of a print subcommand.\n");
629 help_list (setprintlist
, "set print ", -1, stdout
);
634 show_print (args
, from_tty
)
638 cmd_show_list (showprintlist
, from_tty
, "");
642 _initialize_valprint ()
644 struct cmd_list_element
*c
;
646 add_prefix_cmd ("print", no_class
, set_print
,
647 "Generic command for setting how things print.",
648 &setprintlist
, "set print ", 0, &setlist
);
649 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
650 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
); /* prefer set print
652 add_prefix_cmd ("print", no_class
, show_print
,
653 "Generic command for showing print settings.",
654 &showprintlist
, "show print ", 0, &showlist
);
655 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
656 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
659 (add_set_cmd ("elements", no_class
, var_uinteger
, (char *)&print_max
,
660 "Set limit on string chars or array elements to print.\n\
661 \"set print elements 0\" causes there to be no limit.",
666 (add_set_cmd ("repeats", no_class
, var_uinteger
,
667 (char *)&repeat_count_threshold
,
668 "Set threshold for repeated print elements.\n\
669 \"set print repeats 0\" causes all elements to be individually printed.",
674 (add_set_cmd ("pretty", class_support
, var_boolean
,
675 (char *)&prettyprint_structs
,
676 "Set prettyprinting of structures.",
681 (add_set_cmd ("union", class_support
, var_boolean
, (char *)&unionprint
,
682 "Set printing of unions interior to structures.",
687 (add_set_cmd ("array", class_support
, var_boolean
,
688 (char *)&prettyprint_arrays
,
689 "Set prettyprinting of arrays.",
694 (add_set_cmd ("address", class_support
, var_boolean
, (char *)&addressprint
,
695 "Set printing of addresses.",
700 /* The "show radix" cmd isn't good enough to show two separate values.
701 The rest of the code works, but the show part is confusing, so don't
702 let them be set separately 'til we work out "show". */
703 c
= add_set_cmd ("input-radix", class_support
, var_uinteger
,
704 (char *)&input_radix
,
705 "Set default input radix for entering numbers.",
707 add_show_from_set (c
, &showlist
);
708 c
->function
= set_input_radix
;
710 c
= add_set_cmd ("output-radix", class_support
, var_uinteger
,
711 (char *)&output_radix
,
712 "Set default output radix for printing of values.",
714 add_show_from_set (c
, &showlist
);
715 c
->function
= set_output_radix
;
718 c
= add_set_cmd ("radix", class_support
, var_uinteger
,
719 (char *)&output_radix
,
720 "Set default input and output number radix.",
722 add_show_from_set (c
, &showlist
);
723 c
->function
.sfunc
= set_radix
;
725 /* Give people the defaults which they are used to. */
726 prettyprint_structs
= 0;
727 prettyprint_arrays
= 0;