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), which came from
85 the inferior at address ADDRESS, onto stdio stream STREAM according to
86 FORMAT (a letter, or 0 for natural format using TYPE).
88 If DEREF_REF is nonzero, then dereference references, otherwise just print
91 The PRETTY parameter controls prettyprinting.
93 If the data are a string pointer, returns the number of string characters
96 FIXME: The data at VALADDR is in target byte order. If gdb is ever
97 enhanced to be able to debug more than the single target it was compiled
98 for (specific CPU type and thus specific target byte ordering), then
99 either the print routines are going to have to take this into account,
100 or the data is going to have to be passed into here already converted
101 to the host byte ordering, whichever is more convenient. */
105 val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
, pretty
)
113 enum val_prettyprint pretty
;
115 if (pretty
== Val_pretty_default
)
117 pretty
= prettyprint_structs
? Val_prettyprint
: Val_no_prettyprint
;
122 /* Ensure that the type is complete and not just a stub. If the type is
123 only a stub and we can't find and substitute its complete type, then
124 print appropriate string and return. Typical types that my be stubs
125 are structs, unions, and C++ methods. */
127 check_stub_type (type
);
128 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
130 fprintf_filtered (stream
, "<incomplete type>");
135 return (LA_VAL_PRINT (type
, valaddr
, address
, stream
, format
, deref_ref
,
139 /* Print the value VAL in C-ish syntax on stream STREAM.
140 FORMAT is a format-letter, or 0 for print in natural format of data type.
141 If the object printed is a string pointer, returns
142 the number of string bytes printed. */
145 value_print (val
, stream
, format
, pretty
)
149 enum val_prettyprint pretty
;
151 register unsigned int n
, typelen
;
155 printf_filtered ("<address of value unknown>");
158 if (VALUE_OPTIMIZED_OUT (val
))
160 printf_filtered ("<value optimized out>");
164 /* A "repeated" value really contains several values in a row.
165 They are made by the @ operator.
166 Print such values as if they were arrays. */
168 if (VALUE_REPEATED (val
))
170 n
= VALUE_REPETITIONS (val
);
171 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
172 fprintf_filtered (stream
, "{");
173 /* Print arrays of characters using string syntax. */
174 if (typelen
== 1 && TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_INT
176 LA_PRINT_STRING (stream
, VALUE_CONTENTS (val
), n
, 0);
179 value_print_array_elements (val
, stream
, format
, pretty
);
181 fprintf_filtered (stream
, "}");
182 return (n
* typelen
);
186 struct type
*type
= VALUE_TYPE (val
);
188 /* If it is a pointer, indicate what it points to.
190 Print type also if it is a reference.
192 C++: if it is a member pointer, we will take care
193 of that when we print it. */
194 if (TYPE_CODE (type
) == TYPE_CODE_PTR
||
195 TYPE_CODE (type
) == TYPE_CODE_REF
)
197 /* Hack: remove (char *) for char strings. Their
198 type is indicated by the quoted string anyway. */
199 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&&
200 TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) == sizeof(char) &&
201 TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_INT
&&
202 !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type
)))
208 fprintf_filtered (stream
, "(");
209 type_print (type
, "", stream
, -1);
210 fprintf_filtered (stream
, ") ");
213 return (val_print (type
, VALUE_CONTENTS (val
),
214 VALUE_ADDRESS (val
), stream
, format
, 1, 0, pretty
));
218 /* Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
221 val_print_type_code_int (type
, valaddr
, stream
)
227 /* Pointer to first (i.e. lowest address) nonzero character. */
231 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
233 if (TYPE_UNSIGNED (type
))
235 /* First figure out whether the number in fact has zeros
236 in all its bytes more significant than least significant
237 sizeof (LONGEST) ones. */
238 len
= TYPE_LENGTH (type
);
240 #if TARGET_BYTE_ORDER == BIG_ENDIAN
242 len
> sizeof (LONGEST
) && p
< valaddr
+ TYPE_LENGTH (type
);
244 #else /* Little endian. */
245 first_addr
= valaddr
;
246 for (p
= valaddr
+ TYPE_LENGTH (type
);
247 len
> sizeof (LONGEST
) && p
>= valaddr
;
249 #endif /* Little endian. */
260 #if TARGET_BYTE_ORDER == BIG_ENDIAN
263 if (len
<= sizeof (LONGEST
))
265 /* We can print it in decimal. */
268 #if defined (LONG_LONG)
273 unpack_long (BUILTIN_TYPE_LONGEST
, first_addr
));
277 /* It is big, so print it in hex. */
278 print_hex_chars (stream
, (unsigned char *) first_addr
, len
);
283 /* Signed. One could assume two's complement (a reasonable
284 assumption, I think) and do better than this. */
285 print_hex_chars (stream
, (unsigned char *) valaddr
,
291 #ifdef PRINT_TYPELESS_INTEGER
292 PRINT_TYPELESS_INTEGER (stream
, type
, unpack_long (type
, valaddr
));
294 fprintf_filtered (stream
, TYPE_UNSIGNED (type
) ?
295 #if defined (LONG_LONG)
300 unpack_long (type
, valaddr
));
305 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
309 print_floating (valaddr
, type
, stream
)
316 unsigned len
= TYPE_LENGTH (type
);
318 #if defined (IEEE_FLOAT)
320 /* Check for NaN's. Note that this code does not depend on us being
321 on an IEEE conforming system. It only depends on the target
322 machine using IEEE representation. This means (a)
323 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
324 be defined for systems like the 68881, which uses IEEE
325 representation, but is not IEEE conforming. */
329 /* Is the sign bit 0? */
331 /* Is it is a NaN (i.e. the exponent is all ones and
332 the fraction is nonzero)? */
335 if (len
== sizeof (float))
337 /* It's single precision. */
338 memcpy ((char *) &low
, valaddr
, sizeof (low
));
339 /* target -> host. */
340 SWAP_TARGET_AND_HOST (&low
, sizeof (float));
341 nonnegative
= low
>= 0;
342 is_nan
= ((((low
>> 23) & 0xFF) == 0xFF)
343 && 0 != (low
& 0x7FFFFF));
349 /* It's double precision. Get the high and low words. */
351 #if TARGET_BYTE_ORDER == BIG_ENDIAN
352 memcpy (&low
, valaddr
+4, sizeof (low
));
353 memcpy (&high
, valaddr
+0, sizeof (high
));
355 memcpy (&low
, valaddr
+0, sizeof (low
));
356 memcpy (&high
, valaddr
+4, sizeof (high
));
358 SWAP_TARGET_AND_HOST (&low
, sizeof (low
));
359 SWAP_TARGET_AND_HOST (&high
, sizeof (high
));
360 nonnegative
= high
>= 0;
361 is_nan
= (((high
>> 20) & 0x7ff) == 0x7ff
362 && ! ((((high
& 0xfffff) == 0)) && (low
== 0)));
368 /* The meaning of the sign and fraction is not defined by IEEE.
369 But the user might know what they mean. For example, they
370 (in an implementation-defined manner) distinguish between
371 signaling and quiet NaN's. */
373 fprintf_filtered (stream
, "-NaN(0x%lx%.8lx)" + nonnegative
,
376 fprintf_filtered (stream
, "-NaN(0x%lx)" + nonnegative
, low
);
380 #endif /* IEEE_FLOAT. */
382 doub
= unpack_double (type
, valaddr
, &inv
);
384 fprintf_filtered (stream
, "<invalid float value>");
386 fprintf_filtered (stream
, len
<= sizeof(float) ? "%.9g" : "%.17g", doub
);
389 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
392 print_hex_chars (stream
, valaddr
, len
)
394 unsigned char *valaddr
;
399 fprintf_filtered (stream
, "0x");
400 #if TARGET_BYTE_ORDER == BIG_ENDIAN
404 #else /* Little endian. */
405 for (p
= valaddr
+ len
- 1;
410 fprintf_filtered (stream
, "%02x", *p
);
414 /* Called by various <lang>_val_print routines to print elements of an
415 array in the form "<elem1>, <elem2>, <elem3>, ...".
417 (FIXME?) Assumes array element separator is a comma, which is correct
418 for all languages currently handled.
419 (FIXME?) Some languages have a notation for repeated array elements,
420 perhaps we should try to use that notation when appropriate.
424 val_print_array_elements (type
, valaddr
, address
, stream
, format
, deref_ref
,
433 enum val_prettyprint pretty
;
436 unsigned int things_printed
= 0;
438 struct type
*elttype
;
440 /* Position of the array element we are examining to see
441 whether it is repeated. */
443 /* Number of repetitions we have detected so far. */
446 elttype
= TYPE_TARGET_TYPE (type
);
447 eltlen
= TYPE_LENGTH (elttype
);
448 len
= TYPE_LENGTH (type
) / eltlen
;
450 for (; i
< len
&& things_printed
< print_max
; i
++)
454 if (prettyprint_arrays
)
456 fprintf_filtered (stream
, ",\n");
457 print_spaces_filtered (2 + 2 * recurse
, stream
);
461 fprintf_filtered (stream
, ", ");
464 wrap_here (n_spaces (2 + 2 * recurse
));
468 while ((rep1
< len
) &&
469 !memcmp (valaddr
+ i
* eltlen
, valaddr
+ rep1
* eltlen
, eltlen
))
475 if (reps
> repeat_count_threshold
)
477 val_print (elttype
, valaddr
+ i
* eltlen
, 0, stream
, format
,
478 deref_ref
, recurse
+ 1, pretty
);
479 fprintf_filtered (stream
, " <repeats %u times>", reps
);
481 things_printed
+= repeat_count_threshold
;
485 val_print (elttype
, valaddr
+ i
* eltlen
, 0, stream
, format
,
486 deref_ref
, recurse
+ 1, pretty
);
492 fprintf_filtered (stream
, "...");
497 value_print_array_elements (val
, stream
, format
, pretty
)
501 enum val_prettyprint pretty
;
503 unsigned int things_printed
= 0;
504 register unsigned int i
, n
, typelen
;
505 /* Position of the array elem we are examining to see if it is repeated. */
507 /* Number of repetitions we have detected so far. */
510 n
= VALUE_REPETITIONS (val
);
511 typelen
= TYPE_LENGTH (VALUE_TYPE (val
));
512 for (i
= 0; i
< n
&& things_printed
< print_max
; i
++)
516 fprintf_filtered (stream
, ", ");
522 while (rep1
< n
&& !memcmp (VALUE_CONTENTS (val
) + typelen
* i
,
523 VALUE_CONTENTS (val
) + typelen
* rep1
,
530 if (reps
> repeat_count_threshold
)
532 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
533 VALUE_ADDRESS (val
) + typelen
* i
, stream
, format
, 1,
535 fprintf (stream
, " <repeats %u times>", reps
);
537 things_printed
+= repeat_count_threshold
;
541 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
) + typelen
* i
,
542 VALUE_ADDRESS (val
) + typelen
* i
, stream
, format
, 1,
549 fprintf_filtered (stream
, "...");
554 val_print_string (addr
, stream
)
563 unsigned int i
= 0; /* Number of characters printed. */
565 /* Get first character. */
566 errcode
= target_read_memory (addr
, (char *)&c
, 1);
569 /* First address out of bounds. */
576 string
= (char *) alloca (print_max
);
578 /* If the loop ends by us hitting print_max characters,
579 we need to have elipses at the end. */
582 /* This loop always fetches print_max characters, even
583 though LA_PRINT_STRING might want to print more or fewer
584 (with repeated characters). This is so that
585 we don't spend forever fetching if we print
586 a long string consisting of the same character
587 repeated. Also so we can do it all in one memory
588 operation, which is faster. However, this will be
589 slower if print_max is set high, e.g. if you set
590 print_max to 1000, not only will it take a long
591 time to fetch short strings, but if you are near
592 the end of the address space, it might not work. */
594 errcode
= target_read_memory (addr
, string
, print_max
);
597 /* Try reading just one character. If that succeeds,
598 assume we hit the end of the address space, but
599 the initial part of the string is probably safe. */
601 errcode
= target_read_memory (addr
, x
, 1);
606 for (i
= 0; i
< print_max
; i
++)
607 if (string
[i
] == '\0')
616 fputs_filtered (" ", stream
);
618 LA_PRINT_STRING (stream
, string
, i
, force_ellipses
);
625 fprintf_filtered (stream
,
626 (" <Address 0x%x out of bounds>" + first_addr_err
),
631 error ("Error reading memory address 0x%x: %s.", addr
+ i
,
632 safe_strerror (errcode
));
640 /* Validate an input or output radix setting, and make sure the user
641 knows what they really did here. Radix setting is confusing, e.g.
642 setting the input radix to "10" never changes it! */
646 set_input_radix (args
, from_tty
, c
)
649 struct cmd_list_element
*c
;
651 unsigned radix
= *(unsigned *)c
->var
;
654 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
655 radix
, radix
, radix
);
661 set_output_radix (args
, from_tty
, c
)
664 struct cmd_list_element
*c
;
666 unsigned radix
= *(unsigned *)c
->var
;
669 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
670 radix
, radix
, radix
);
672 /* FIXME, we really should be able to validate the setting BEFORE
683 output_format
= 'o'; /* octal */
687 error ("Unsupported radix ``decimal %d''; using decimal output",
694 set_radix (arg
, from_tty
, c
)
697 struct cmd_list_element
*c
;
699 unsigned radix
= *(unsigned *)c
->var
;
702 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
703 radix
, radix
, radix
);
706 output_radix
= radix
;
708 set_output_radix (arg
, 0, c
);
713 set_print (arg
, from_tty
)
718 "\"set print\" must be followed by the name of a print subcommand.\n");
719 help_list (setprintlist
, "set print ", -1, stdout
);
724 show_print (args
, from_tty
)
728 cmd_show_list (showprintlist
, from_tty
, "");
732 _initialize_valprint ()
734 struct cmd_list_element
*c
;
736 add_prefix_cmd ("print", no_class
, set_print
,
737 "Generic command for setting how things print.",
738 &setprintlist
, "set print ", 0, &setlist
);
739 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
740 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
); /* prefer set print
742 add_prefix_cmd ("print", no_class
, show_print
,
743 "Generic command for showing print settings.",
744 &showprintlist
, "show print ", 0, &showlist
);
745 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
746 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
749 (add_set_cmd ("elements", no_class
, var_uinteger
, (char *)&print_max
,
750 "Set limit on string chars or array elements to print.\n\
751 \"set print elements 0\" causes there to be no limit.",
756 (add_set_cmd ("repeats", no_class
, var_uinteger
,
757 (char *)&repeat_count_threshold
,
758 "Set threshold for repeated print elements.\n\
759 \"set print repeats 0\" causes all elements to be individually printed.",
764 (add_set_cmd ("pretty", class_support
, var_boolean
,
765 (char *)&prettyprint_structs
,
766 "Set prettyprinting of structures.",
771 (add_set_cmd ("union", class_support
, var_boolean
, (char *)&unionprint
,
772 "Set printing of unions interior to structures.",
777 (add_set_cmd ("array", class_support
, var_boolean
,
778 (char *)&prettyprint_arrays
,
779 "Set prettyprinting of arrays.",
784 (add_set_cmd ("address", class_support
, var_boolean
, (char *)&addressprint
,
785 "Set printing of addresses.",
790 /* The "show radix" cmd isn't good enough to show two separate values.
791 The rest of the code works, but the show part is confusing, so don't
792 let them be set separately 'til we work out "show". */
793 c
= add_set_cmd ("input-radix", class_support
, var_uinteger
,
794 (char *)&input_radix
,
795 "Set default input radix for entering numbers.",
797 add_show_from_set (c
, &showlist
);
798 c
->function
= set_input_radix
;
800 c
= add_set_cmd ("output-radix", class_support
, var_uinteger
,
801 (char *)&output_radix
,
802 "Set default output radix for printing of values.",
804 add_show_from_set (c
, &showlist
);
805 c
->function
= set_output_radix
;
808 c
= add_set_cmd ("radix", class_support
, var_uinteger
,
809 (char *)&output_radix
,
810 "Set default input and output number radix.",
812 add_show_from_set (c
, &showlist
);
813 c
->function
.sfunc
= set_radix
;
815 /* Give people the defaults which they are used to. */
816 prettyprint_structs
= 0;
817 prettyprint_arrays
= 0;