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. */
266 print_longest (stream, 'u', 0,
267 unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
271 /* It is big, so print it in hex. */
272 print_hex_chars (stream, (unsigned char *) first_addr, len);
277 /* Signed. One could assume two's complement (a reasonable
278 assumption, I think) and do better than this. */
279 print_hex_chars (stream, (unsigned char *) valaddr,
285 #ifdef PRINT_TYPELESS_INTEGER
286 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
288 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
289 unpack_long (type, valaddr));
294 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
295 The raison d'etre of this function is to consolidate printing of LONG_LONG's
296 into this one function. Some platforms have long longs but don't have a
297 printf() that supports "ll" in the format string. We handle these by seeing
298 if the number is actually a long, and if not we just bail out and print the
299 number in hex. The format chars b,h,w,g are from
300 print_scalar_formatted(). USE_LOCAL says whether or not to call the
301 local formatting routine to get the format. */
304 print_longest (stream, format, use_local, val_long)
310 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
313 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
314 vbot = (long) val_long;
316 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
317 || ((format == 'u' || format == 'x') && val_long > UINT_MAX))
319 fprintf_filtered (stream, "0x%x%08x", vtop, vbot);
324 #ifdef PRINTF_HAS_LONG_LONG
328 fprintf_filtered (stream,
329 use_local ? local_decimal_format_custom ("ll")
334 fprintf_filtered (stream, "%llu", val_long);
337 fprintf_filtered (stream,
338 use_local ? local_hex_format_custom ("ll")
343 fprintf_filtered (stream,
344 use_local ? local_octal_format_custom ("ll")
348 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
351 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
354 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
357 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
362 #else /* !PRINTF_HAS_LONG_LONG */
363 /* In the following it is important to coerce (val_long) to a long. It does
364 nothing if !LONG_LONG, but it will chop off the top half (which we know
365 we can ignore) if the host supports long longs. */
370 fprintf_filtered (stream,
371 use_local ? local_decimal_format_custom ("l")
376 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
379 fprintf_filtered (stream,
380 use_local ? local_hex_format_custom ("l")
385 fprintf_filtered (stream,
386 use_local ? local_octal_format_custom ("l")
391 fprintf_filtered (stream, local_hex_format_custom ("02l"),
395 fprintf_filtered (stream, local_hex_format_custom ("04l"),
399 fprintf_filtered (stream, local_hex_format_custom ("08l"),
403 fprintf_filtered (stream, local_hex_format_custom ("016l"),
409 #endif /* !PRINTF_HAS_LONG_LONG */
412 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
416 print_floating (valaddr, type, stream)
423 unsigned len = TYPE_LENGTH (type);
425 #if defined (IEEE_FLOAT)
427 /* Check for NaN's. Note that this code does not depend on us being
428 on an IEEE conforming system. It only depends on the target
429 machine using IEEE representation. This means (a)
430 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
431 be defined for systems like the 68881, which uses IEEE
432 representation, but is not IEEE conforming. */
436 /* Is the sign bit 0? */
438 /* Is it is a NaN (i.e. the exponent is all ones and
439 the fraction is nonzero)? */
442 if (len == sizeof (float))
444 /* It's single precision. */
445 memcpy ((char *) &low, valaddr, sizeof (low));
446 /* target -> host. */
447 SWAP_TARGET_AND_HOST (&low, sizeof (float));
448 nonnegative = low >= 0;
449 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
450 && 0 != (low & 0x7FFFFF));
456 /* It's double precision. Get the high and low words. */
458 #if TARGET_BYTE_ORDER == BIG_ENDIAN
459 memcpy (&low, valaddr+4, sizeof (low));
460 memcpy (&high, valaddr+0, sizeof (high));
462 memcpy (&low, valaddr+0, sizeof (low));
463 memcpy (&high, valaddr+4, sizeof (high));
465 SWAP_TARGET_AND_HOST (&low, sizeof (low));
466 SWAP_TARGET_AND_HOST (&high, sizeof (high));
467 nonnegative = high >= 0;
468 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
469 && ! ((((high & 0xfffff) == 0)) && (low == 0)));
475 /* The meaning of the sign and fraction is not defined by IEEE.
476 But the user might know what they mean. For example, they
477 (in an implementation-defined manner) distinguish between
478 signaling and quiet NaN's. */
480 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
483 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
487 #endif /* IEEE_FLOAT. */
489 doub = unpack_double (type, valaddr, &inv);
491 fprintf_filtered (stream, "<invalid float value>");
493 fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
496 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
499 print_hex_chars (stream, valaddr, len)
501 unsigned char *valaddr;
506 fprintf_filtered (stream, "0x");
507 #if TARGET_BYTE_ORDER == BIG_ENDIAN
511 #else /* Little endian. */
512 for (p = valaddr + len - 1;
517 fprintf_filtered (stream, "%02x", *p);
521 /* Called by various <lang>_val_print routines to print elements of an
522 array in the form "<elem1>, <elem2>, <elem3>, ...".
524 (FIXME?) Assumes array element separator is a comma, which is correct
525 for all languages currently handled.
526 (FIXME?) Some languages have a notation for repeated array elements,
527 perhaps we should try to use that notation when appropriate.
531 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
540 enum val_prettyprint pretty;
543 unsigned int things_printed = 0;
545 struct type *elttype;
547 /* Position of the array element we are examining to see
548 whether it is repeated. */
550 /* Number of repetitions we have detected so far. */
553 elttype = TYPE_TARGET_TYPE (type);
554 eltlen = TYPE_LENGTH (elttype);
555 len = TYPE_LENGTH (type) / eltlen;
557 for (; i < len && things_printed < print_max; i++)
561 if (prettyprint_arrays)
563 fprintf_filtered (stream, ",\n");
564 print_spaces_filtered (2 + 2 * recurse, stream);
568 fprintf_filtered (stream, ", ");
571 wrap_here (n_spaces (2 + 2 * recurse));
575 while ((rep1 < len) &&
576 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
582 if (reps > repeat_count_threshold)
584 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
585 deref_ref, recurse + 1, pretty);
586 fprintf_filtered (stream, " <repeats %u times>", reps);
588 things_printed += repeat_count_threshold;
592 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
593 deref_ref, recurse + 1, pretty);
599 fprintf_filtered (stream, "...");
604 value_print_array_elements (val, stream, format, pretty)
608 enum val_prettyprint pretty;
610 unsigned int things_printed = 0;
611 register unsigned int i, n, typelen;
612 /* Position of the array elem we are examining to see if it is repeated. */
614 /* Number of repetitions we have detected so far. */
617 n = VALUE_REPETITIONS (val);
618 typelen = TYPE_LENGTH (VALUE_TYPE (val));
619 for (i = 0; i < n && things_printed < print_max; i++)
623 fprintf_filtered (stream, ", ");
629 while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
630 VALUE_CONTENTS (val) + typelen * rep1,
637 if (reps > repeat_count_threshold)
639 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
640 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
642 fprintf (stream, " <repeats %u times>", reps);
644 things_printed += repeat_count_threshold;
648 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
649 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
656 fprintf_filtered (stream, "...");
660 /* Print a string from the inferior, starting at ADDR and printing up to LEN
661 characters, to STREAM. If LEN is zero, printing stops at the first null
662 byte, otherwise printing proceeds (including null bytes) until either
663 print_max or LEN characters have been printed.
665 Always fetch print_max+1 characters, even though LA_PRINT_STRING might want
666 to print more or fewer (with repeated characters). This is so that we
667 don't spend forever fetching if we print a long string consisting of the
668 same character repeated. Also so we can do it all in one memory operation,
669 which is faster. However, this will be slower if print_max is set high,
670 e.g. if you set print_max to 1000, not only will it take a long time to
671 fetch short strings, but if you are near the end of the address space, it
674 If the number of characters we actually print is limited because of hitting
675 print_max, when LEN would have explicitly or implicitly (in the case of a
676 null terminated string with another non-null character available to print)
677 allowed us to print more, we print ellipsis ("...") after the printed string
678 to indicate that more characters were available to print but that we were
679 limited by print_max. To do this correctly requires that we always fetch
680 one more than the number of characters we could potentially print, so that
681 if we do print the maximum number, we can tell whether or not a null byte
682 would have been the next character, in the case of C style strings.
683 For non-C style strings, only the value of LEN is pertinent in deciding
684 whether or not to print ellipsis.
686 FIXME: If LEN is nonzero and less than print_max, we could get away
687 with only fetching the specified number of characters from the inferior. */
690 val_print_string (addr, len, stream)
695 int first_addr_err = 0; /* Nonzero if first address out of bounds */
696 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero */
701 /* Get first character. */
702 errcode = target_read_memory (addr, (char *)&c, 1);
705 /* First address out of bounds. */
708 else if (print_max < UINT_MAX)
710 string = (char *) alloca (print_max + 1);
711 memset (string, 0, print_max + 1);
714 errcode = target_read_memory (addr, string, print_max + 1);
717 /* Try reading just one character. If that succeeds, assume we hit
718 the end of the address space, but the initial part of the string
721 errcode = target_read_memory (addr, x, 1);
725 /* When the length is unspecified, such as when printing C style
726 null byte terminated strings, then scan the string looking for
727 the terminator in the first print_max characters. If a terminator
728 is found, then it determines the length, otherwise print_max
729 determines the length. */
730 for (;len < print_max; len++)
732 if (string[len] == '\0')
737 /* If the first unprinted character is not the null terminator, set
738 the flag to force ellipses. This is true whether or not we broke
739 out of the above loop because we found a terminator, or whether
740 we simply hit the limit on how many characters to print. */
741 if (string[len] != '\0')
746 else if (len > print_max)
748 /* Printing less than the number of characters actually requested
749 always makes us print ellipsis. */
757 fputs_filtered (" ", stream);
759 LA_PRINT_STRING (stream, string, len, force_ellipsis);
766 fprintf_filtered (stream,
767 (" <Address 0x%x out of bounds>" + first_addr_err),
772 error ("Error reading memory address 0x%x: %s.", addr + len,
773 safe_strerror (errcode));
781 /* Validate an input or output radix setting, and make sure the user
782 knows what they really did here. Radix setting is confusing, e.g.
783 setting the input radix to "10" never changes it! */
787 set_input_radix (args, from_tty, c)
790 struct cmd_list_element *c;
792 unsigned radix = *(unsigned *)c->var;
795 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
796 radix, radix, radix);
802 set_output_radix (args, from_tty, c)
805 struct cmd_list_element *c;
807 unsigned radix = *(unsigned *)c->var;
810 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
811 radix, radix, radix);
813 /* FIXME, we really should be able to validate the setting BEFORE
824 output_format = 'o'; /* octal */
828 error ("Unsupported radix ``decimal %d''; using decimal output",
835 set_radix (arg, from_tty, c)
838 struct cmd_list_element *c;
840 unsigned radix = *(unsigned *)c->var;
843 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
844 radix, radix, radix);
847 output_radix = radix;
849 set_output_radix (arg, 0, c);
854 set_print (arg, from_tty)
859 "\"set print\" must be followed by the name of a print subcommand.\n");
860 help_list (setprintlist, "set print ", -1, stdout);
865 show_print (args, from_tty)
869 cmd_show_list (showprintlist, from_tty, "");
873 _initialize_valprint ()
875 struct cmd_list_element *c;
877 add_prefix_cmd ("print", no_class, set_print,
878 "Generic command for setting how things print.",
879 &setprintlist, "set print ", 0, &setlist);
880 add_alias_cmd ("p", "print", no_class, 1, &setlist);
881 add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
883 add_prefix_cmd ("print", no_class, show_print,
884 "Generic command for showing print settings.",
885 &showprintlist, "show print ", 0, &showlist);
886 add_alias_cmd ("p", "print", no_class, 1, &showlist);
887 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
890 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
891 "Set limit on string chars or array elements to print.\n\
892 \"set print elements 0\" causes there to be no limit.",
897 (add_set_cmd ("repeats", no_class, var_uinteger,
898 (char *)&repeat_count_threshold,
899 "Set threshold for repeated print elements.\n\
900 \"set print repeats 0\" causes all elements to be individually printed.",
905 (add_set_cmd ("pretty", class_support, var_boolean,
906 (char *)&prettyprint_structs,
907 "Set prettyprinting of structures.",
912 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
913 "Set printing of unions interior to structures.",
918 (add_set_cmd ("array", class_support, var_boolean,
919 (char *)&prettyprint_arrays,
920 "Set prettyprinting of arrays.",
925 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
926 "Set printing of addresses.",
931 /* The "show radix" cmd isn't good enough to show two separate values.
932 The rest of the code works, but the show part is confusing, so don't
933 let them be set separately 'til we work out "show". */
934 c = add_set_cmd ("input-radix", class_support, var_uinteger,
935 (char *)&input_radix,
936 "Set default input radix for entering numbers.",
938 add_show_from_set (c, &showlist);
939 c->function = set_input_radix;
941 c = add_set_cmd ("output-radix", class_support, var_uinteger,
942 (char *)&output_radix,
943 "Set default output radix for printing of values.",
945 add_show_from_set (c, &showlist);
946 c->function = set_output_radix;
949 c = add_set_cmd ("radix", class_support, var_uinteger,
950 (char *)&output_radix,
951 "Set default input and output number radix.",
953 add_show_from_set (c, &showlist);
954 c->function.sfunc = set_radix;
956 /* Give people the defaults which they are used to. */
957 prettyprint_structs = 0;
958 prettyprint_arrays = 0;