1 /* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991-1994, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
38 /* Prototypes for local functions */
40 static int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
41 int len, int *errnoptr);
43 static void print_hex_chars (struct ui_file *, unsigned char *,
46 static void show_print PARAMS ((char *, int));
48 static void set_print PARAMS ((char *, int));
50 static void set_radix PARAMS ((char *, int));
52 static void show_radix PARAMS ((char *, int));
54 static void set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
56 static void set_input_radix_1 PARAMS ((int, unsigned));
58 static void set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
60 static void set_output_radix_1 PARAMS ((int, unsigned));
62 void _initialize_valprint PARAMS ((void));
64 /* Maximum number of chars to print for a string pointer value or vector
65 contents, or UINT_MAX for no limit. Note that "set print elements 0"
66 stores UINT_MAX in print_max, which displays in a show command as
69 unsigned int print_max;
70 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
72 /* Default input and output radixes, and output format letter. */
74 unsigned input_radix = 10;
75 unsigned output_radix = 10;
76 int output_format = 0;
78 /* Print repeat counts if there are more than this many repetitions of an
79 element in an array. Referenced by the low level language dependent
82 unsigned int repeat_count_threshold = 10;
84 /* If nonzero, stops printing of char arrays at first null. */
86 int stop_print_at_null;
88 /* Controls pretty printing of structures. */
90 int prettyprint_structs;
92 /* Controls pretty printing of arrays. */
94 int prettyprint_arrays;
96 /* If nonzero, causes unions inside structures or other unions to be
99 int unionprint; /* Controls printing of nested unions. */
101 /* If nonzero, causes machine addresses to be printed in certain contexts. */
103 int addressprint; /* Controls printing of machine addresses */
106 /* Print data of type TYPE located at VALADDR (within GDB), which came from
107 the inferior at address ADDRESS, onto stdio stream STREAM according to
108 FORMAT (a letter, or 0 for natural format using TYPE).
110 If DEREF_REF is nonzero, then dereference references, otherwise just print
113 The PRETTY parameter controls prettyprinting.
115 If the data are a string pointer, returns the number of string characters
118 FIXME: The data at VALADDR is in target byte order. If gdb is ever
119 enhanced to be able to debug more than the single target it was compiled
120 for (specific CPU type and thus specific target byte ordering), then
121 either the print routines are going to have to take this into account,
122 or the data is going to have to be passed into here already converted
123 to the host byte ordering, whichever is more convenient. */
127 val_print (type, valaddr, embedded_offset, address,
128 stream, format, deref_ref, recurse, pretty)
133 struct ui_file *stream;
137 enum val_prettyprint pretty;
139 struct type *real_type = check_typedef (type);
140 if (pretty == Val_pretty_default)
142 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
147 /* Ensure that the type is complete and not just a stub. If the type is
148 only a stub and we can't find and substitute its complete type, then
149 print appropriate string and return. */
151 if (TYPE_FLAGS (real_type) & TYPE_FLAG_STUB)
153 fprintf_filtered (stream, "<incomplete type>");
158 return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
159 stream, format, deref_ref, recurse, pretty));
162 /* Print the value VAL in C-ish syntax on stream STREAM.
163 FORMAT is a format-letter, or 0 for print in natural format of data type.
164 If the object printed is a string pointer, returns
165 the number of string bytes printed. */
168 value_print (val, stream, format, pretty)
170 struct ui_file *stream;
172 enum val_prettyprint pretty;
176 printf_filtered ("<address of value unknown>");
179 if (VALUE_OPTIMIZED_OUT (val))
181 printf_filtered ("<value optimized out>");
184 return LA_VALUE_PRINT (val, stream, format, pretty);
187 /* Called by various <lang>_val_print routines to print
188 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
189 value. STREAM is where to print the value. */
192 val_print_type_code_int (type, valaddr, stream)
195 struct ui_file *stream;
197 if (TYPE_LENGTH (type) > sizeof (LONGEST))
201 if (TYPE_UNSIGNED (type)
202 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
205 print_longest (stream, 'u', 0, val);
209 /* Signed, or we couldn't turn an unsigned value into a
210 LONGEST. For signed values, one could assume two's
211 complement (a reasonable assumption, I think) and do
213 print_hex_chars (stream, (unsigned char *) valaddr,
219 #ifdef PRINT_TYPELESS_INTEGER
220 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
222 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
223 unpack_long (type, valaddr));
228 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
229 The raison d'etre of this function is to consolidate printing of
230 LONG_LONG's into this one function. Some platforms have long longs but
231 don't have a printf() that supports "ll" in the format string. We handle
232 these by seeing if the number is representable as either a signed or
233 unsigned long, depending upon what format is desired, and if not we just
234 bail out and print the number in hex.
236 The format chars b,h,w,g are from print_scalar_formatted(). If USE_LOCAL,
237 format it according to the current language (this should be used for most
238 integers which GDB prints, the exception is things like protocols where
239 the format of the integer is a protocol thing, not a user-visible thing).
242 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
243 static void print_decimal (struct ui_file * stream, char *sign,
244 int use_local, ULONGEST val_ulong);
246 print_decimal (stream, sign, use_local, val_ulong)
247 struct ui_file *stream;
252 unsigned long temp[3];
256 temp[i] = val_ulong % (1000 * 1000 * 1000);
257 val_ulong /= (1000 * 1000 * 1000);
260 while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
264 fprintf_filtered (stream, "%s%lu",
268 fprintf_filtered (stream, "%s%lu%09lu",
269 sign, temp[1], temp[0]);
272 fprintf_filtered (stream, "%s%lu%09lu%09lu",
273 sign, temp[2], temp[1], temp[0]);
283 print_longest (stream, format, use_local, val_long)
284 struct ui_file *stream;
289 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
290 if (sizeof (long) < sizeof (LONGEST))
296 /* Print a signed value, that doesn't fit in a long */
297 if ((long) val_long != val_long)
300 print_decimal (stream, "-", use_local, -val_long);
302 print_decimal (stream, "", use_local, val_long);
309 /* Print an unsigned value, that doesn't fit in a long */
310 if ((unsigned long) val_long != (ULONGEST) val_long)
312 print_decimal (stream, "", use_local, val_long);
323 /* Print as unsigned value, must fit completely in unsigned long */
325 unsigned long temp = val_long;
326 if (temp != val_long)
328 /* Urk, can't represent value in long so print in hex.
329 Do shift in two operations so that if sizeof (long)
330 == sizeof (LONGEST) we can avoid warnings from
331 picky compilers about shifts >= the size of the
333 unsigned long vbot = (unsigned long) val_long;
334 LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
335 unsigned long vtop = temp >> 1;
336 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
345 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
349 fprintf_filtered (stream,
350 use_local ? local_decimal_format_custom ("ll")
355 fprintf_filtered (stream, "%llu", val_long);
358 fprintf_filtered (stream,
359 use_local ? local_hex_format_custom ("ll")
364 fprintf_filtered (stream,
365 use_local ? local_octal_format_custom ("ll")
370 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
373 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
376 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
379 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
384 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
385 /* In the following it is important to coerce (val_long) to a long. It does
386 nothing if !LONG_LONG, but it will chop off the top half (which we know
387 we can ignore) if the host supports long longs. */
392 fprintf_filtered (stream,
393 use_local ? local_decimal_format_custom ("l")
398 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
401 fprintf_filtered (stream,
402 use_local ? local_hex_format_custom ("l")
404 (unsigned long) val_long);
407 fprintf_filtered (stream,
408 use_local ? local_octal_format_custom ("l")
410 (unsigned long) val_long);
413 fprintf_filtered (stream, local_hex_format_custom ("02l"),
414 (unsigned long) val_long);
417 fprintf_filtered (stream, local_hex_format_custom ("04l"),
418 (unsigned long) val_long);
421 fprintf_filtered (stream, local_hex_format_custom ("08l"),
422 (unsigned long) val_long);
425 fprintf_filtered (stream, local_hex_format_custom ("016l"),
426 (unsigned long) val_long);
431 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
436 strcat_longest (format, use_local, val_long, buf, buflen)
441 int buflen; /* ignored, for now */
443 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
446 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
447 vbot = (long) val_long;
449 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
450 || ((format == 'u' || format == 'x') && (unsigned long long) val_long > UINT_MAX))
452 sprintf (buf, "0x%lx%08lx", vtop, vbot);
457 #ifdef PRINTF_HAS_LONG_LONG
462 (use_local ? local_decimal_format_custom ("ll") : "%lld"),
466 sprintf (buf, "%llu", val_long);
470 (use_local ? local_hex_format_custom ("ll") : "%llx"),
476 (use_local ? local_octal_format_custom ("ll") : "%llo"),
480 sprintf (buf, local_hex_format_custom ("02ll"), val_long);
483 sprintf (buf, local_hex_format_custom ("04ll"), val_long);
486 sprintf (buf, local_hex_format_custom ("08ll"), val_long);
489 sprintf (buf, local_hex_format_custom ("016ll"), val_long);
494 #else /* !PRINTF_HAS_LONG_LONG */
495 /* In the following it is important to coerce (val_long) to a long. It does
496 nothing if !LONG_LONG, but it will chop off the top half (which we know
497 we can ignore) if the host supports long longs. */
502 sprintf (buf, (use_local ? local_decimal_format_custom ("l") : "%ld"),
506 sprintf (buf, "%lu", ((unsigned long) val_long));
509 sprintf (buf, (use_local ? local_hex_format_custom ("l") : "%lx"),
513 sprintf (buf, (use_local ? local_octal_format_custom ("l") : "%lo"),
517 sprintf (buf, local_hex_format_custom ("02l"),
521 sprintf (buf, local_hex_format_custom ("04l"),
525 sprintf (buf, local_hex_format_custom ("08l"),
529 sprintf (buf, local_hex_format_custom ("016l"),
536 #endif /* !PRINTF_HAS_LONG_LONG */
540 /* This used to be a macro, but I don't think it is called often enough
541 to merit such treatment. */
542 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
543 arguments to a function, number in a value history, register number, etc.)
544 where the value must not be larger than can fit in an int. */
550 /* Let the compiler do the work */
551 int rtnval = (int) arg;
553 /* Check for overflows or underflows */
554 if (sizeof (LONGEST) > sizeof (int))
558 error ("Value out of range.");
565 /* Provide a default value for IEEE_FLOAT. */
567 #define IEEE_FLOAT (0)
571 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
575 print_floating (valaddr, type, stream)
578 struct ui_file *stream;
582 unsigned len = TYPE_LENGTH (type);
584 /* Check for NaN's. Note that this code does not depend on us being
585 on an IEEE conforming system. It only depends on the target
586 machine using IEEE representation. This means (a)
587 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
588 be non-zero for systems like the 68881, which uses IEEE
589 representation, but is not IEEE conforming. */
592 unsigned long low, high;
593 /* Is the sign bit 0? */
595 /* Is it is a NaN (i.e. the exponent is all ones and
596 the fraction is nonzero)? */
599 /* For lint, initialize these two variables to suppress warning: */
600 low = high = nonnegative = 0;
603 /* It's single precision. */
604 /* Assume that floating point byte order is the same as
605 integer byte order. */
606 low = extract_unsigned_integer (valaddr, 4);
607 nonnegative = ((low & 0x80000000) == 0);
608 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
609 && 0 != (low & 0x7FFFFF));
615 /* It's double precision. Get the high and low words. */
617 /* Assume that floating point byte order is the same as
618 integer byte order. */
619 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
621 low = extract_unsigned_integer (valaddr + 4, 4);
622 high = extract_unsigned_integer (valaddr, 4);
626 low = extract_unsigned_integer (valaddr, 4);
627 high = extract_unsigned_integer (valaddr + 4, 4);
629 nonnegative = ((high & 0x80000000) == 0);
630 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
631 && !((((high & 0xfffff) == 0)) && (low == 0)));
636 #ifdef TARGET_ANALYZE_FLOATING
637 TARGET_ANALYZE_FLOATING;
639 /* Extended. We can't detect extended NaNs for this target.
640 Also note that currently extendeds get nuked to double in
641 REGISTER_CONVERTIBLE. */
648 /* The meaning of the sign and fraction is not defined by IEEE.
649 But the user might know what they mean. For example, they
650 (in an implementation-defined manner) distinguish between
651 signaling and quiet NaN's. */
653 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + !!nonnegative,
656 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
661 doub = unpack_double (type, valaddr, &inv);
664 fprintf_filtered (stream, "<invalid float value>");
668 if (len < sizeof (double))
669 fprintf_filtered (stream, "%.9g", (double) doub);
670 else if (len == sizeof (double))
671 fprintf_filtered (stream, "%.17g", (double) doub);
673 #ifdef PRINTF_HAS_LONG_DOUBLE
674 fprintf_filtered (stream, "%.35Lg", doub);
676 /* This at least wins with values that are representable as doubles */
677 fprintf_filtered (stream, "%.17g", (double) doub);
682 print_binary_chars (stream, valaddr, len)
683 struct ui_file *stream;
684 unsigned char *valaddr;
688 #define BITS_IN_BYTES 8
694 /* Declared "int" so it will be signed.
695 * This ensures that right shift will shift in zeros.
697 const int mask = 0x080;
699 /* FIXME: We should be not printing leading zeroes in most cases. */
701 fprintf_filtered (stream, local_binary_format_prefix ());
702 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
708 /* Every byte has 8 binary characters; peel off
709 * and print from the MSB end.
711 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
713 if (*p & (mask >> i))
718 fprintf_filtered (stream, "%1d", b);
724 for (p = valaddr + len - 1;
728 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
730 if (*p & (mask >> i))
735 fprintf_filtered (stream, "%1d", b);
739 fprintf_filtered (stream, local_binary_format_suffix ());
742 /* VALADDR points to an integer of LEN bytes.
743 * Print it in octal on stream or format it in buf.
746 print_octal_chars (stream, valaddr, len)
747 struct ui_file *stream;
748 unsigned char *valaddr;
752 unsigned char octa1, octa2, octa3, carry;
755 /* FIXME: We should be not printing leading zeroes in most cases. */
758 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
759 * the extra bits, which cycle every three bytes:
763 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
765 * Octal side: 0 1 carry 3 4 carry ...
767 * Cycle number: 0 1 2
769 * But of course we are printing from the high side, so we have to
770 * figure out where in the cycle we are so that we end up with no
771 * left over bits at the end.
773 #define BITS_IN_OCTAL 3
774 #define HIGH_ZERO 0340
775 #define LOW_ZERO 0016
776 #define CARRY_ZERO 0003
777 #define HIGH_ONE 0200
780 #define CARRY_ONE 0001
781 #define HIGH_TWO 0300
785 /* For 32 we start in cycle 2, with two bits and one bit carry;
786 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
788 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
791 fprintf_filtered (stream, local_octal_format_prefix ());
792 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
801 /* No carry in, carry out two bits.
803 octa1 = (HIGH_ZERO & *p) >> 5;
804 octa2 = (LOW_ZERO & *p) >> 2;
805 carry = (CARRY_ZERO & *p);
806 fprintf_filtered (stream, "%o", octa1);
807 fprintf_filtered (stream, "%o", octa2);
811 /* Carry in two bits, carry out one bit.
813 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
814 octa2 = (MID_ONE & *p) >> 4;
815 octa3 = (LOW_ONE & *p) >> 1;
816 carry = (CARRY_ONE & *p);
817 fprintf_filtered (stream, "%o", octa1);
818 fprintf_filtered (stream, "%o", octa2);
819 fprintf_filtered (stream, "%o", octa3);
823 /* Carry in one bit, no carry out.
825 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
826 octa2 = (MID_TWO & *p) >> 3;
827 octa3 = (LOW_TWO & *p);
829 fprintf_filtered (stream, "%o", octa1);
830 fprintf_filtered (stream, "%o", octa2);
831 fprintf_filtered (stream, "%o", octa3);
835 error ("Internal error in octal conversion;");
839 cycle = cycle % BITS_IN_OCTAL;
844 for (p = valaddr + len - 1;
851 /* Carry out, no carry in */
852 octa1 = (HIGH_ZERO & *p) >> 5;
853 octa2 = (LOW_ZERO & *p) >> 2;
854 carry = (CARRY_ZERO & *p);
855 fprintf_filtered (stream, "%o", octa1);
856 fprintf_filtered (stream, "%o", octa2);
860 /* Carry in, carry out */
861 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
862 octa2 = (MID_ONE & *p) >> 4;
863 octa3 = (LOW_ONE & *p) >> 1;
864 carry = (CARRY_ONE & *p);
865 fprintf_filtered (stream, "%o", octa1);
866 fprintf_filtered (stream, "%o", octa2);
867 fprintf_filtered (stream, "%o", octa3);
871 /* Carry in, no carry out */
872 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
873 octa2 = (MID_TWO & *p) >> 3;
874 octa3 = (LOW_TWO & *p);
876 fprintf_filtered (stream, "%o", octa1);
877 fprintf_filtered (stream, "%o", octa2);
878 fprintf_filtered (stream, "%o", octa3);
882 error ("Internal error in octal conversion;");
886 cycle = cycle % BITS_IN_OCTAL;
890 fprintf_filtered (stream, local_octal_format_suffix ());
893 /* VALADDR points to an integer of LEN bytes.
894 * Print it in decimal on stream or format it in buf.
897 print_decimal_chars (stream, valaddr, len)
898 struct ui_file *stream;
899 unsigned char *valaddr;
903 #define TWO_TO_FOURTH 16
904 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
905 #define CARRY_LEFT( x ) ((x) % TEN)
906 #define SHIFT( x ) ((x) << 4)
908 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
910 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
912 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
913 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
914 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
917 unsigned char *digits;
920 int i, j, decimal_digits;
924 /* Base-ten number is less than twice as many digits
925 * as the base 16 number, which is 2 digits per byte.
927 decimal_len = len * 2 * 2;
928 digits = (unsigned char *) malloc (decimal_len);
930 error ("Can't allocate memory for conversion to decimal.");
932 for (i = 0; i < decimal_len; i++)
937 fprintf_filtered (stream, local_decimal_format_prefix ());
939 /* Ok, we have an unknown number of bytes of data to be printed in
942 * Given a hex number (in nibbles) as XYZ, we start by taking X and
943 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
944 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
946 * The trick is that "digits" holds a base-10 number, but sometimes
947 * the individual digits are > 10.
949 * Outer loop is per nibble (hex digit) of input, from MSD end to
952 decimal_digits = 0; /* Number of decimal digits so far */
958 * Multiply current base-ten number by 16 in place.
959 * Each digit was between 0 and 9, now is between
962 for (j = 0; j < decimal_digits; j++)
964 digits[j] = SHIFT (digits[j]);
967 /* Take the next nibble off the input and add it to what
968 * we've got in the LSB position. Bottom 'digit' is now
971 * "flip" is used to run this loop twice for each byte.
977 digits[0] += HIGH_NIBBLE (*p);
982 /* Take low nibble and bump our pointer "p".
984 digits[0] += LOW_NIBBLE (*p);
989 /* Re-decimalize. We have to do this often enough
990 * that we don't overflow, but once per nibble is
991 * overkill. Easier this way, though. Note that the
992 * carry is often larger than 10 (e.g. max initial
993 * carry out of lowest nibble is 15, could bubble all
994 * the way up greater than 10). So we have to do
995 * the carrying beyond the last current digit.
998 for (j = 0; j < decimal_len - 1; j++)
1002 /* "/" won't handle an unsigned char with
1003 * a value that if signed would be negative.
1004 * So extend to longword int via "dummy".
1007 carry = CARRY_OUT (dummy);
1008 digits[j] = CARRY_LEFT (dummy);
1010 if (j >= decimal_digits && carry == 0)
1013 * All higher digits are 0 and we
1014 * no longer have a carry.
1016 * Note: "j" is 0-based, "decimal_digits" is
1019 decimal_digits = j + 1;
1025 /* Ok, now "digits" is the decimal representation, with
1026 * the "decimal_digits" actual digits. Print!
1028 for (i = decimal_digits - 1; i >= 0; i--)
1030 fprintf_filtered (stream, "%1d", digits[i]);
1034 fprintf_filtered (stream, local_decimal_format_suffix ());
1037 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1040 print_hex_chars (stream, valaddr, len)
1041 struct ui_file *stream;
1042 unsigned char *valaddr;
1047 /* FIXME: We should be not printing leading zeroes in most cases. */
1049 fprintf_filtered (stream, local_hex_format_prefix ());
1050 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1056 fprintf_filtered (stream, "%02x", *p);
1061 for (p = valaddr + len - 1;
1065 fprintf_filtered (stream, "%02x", *p);
1068 fprintf_filtered (stream, local_hex_format_suffix ());
1071 /* Called by various <lang>_val_print routines to print elements of an
1072 array in the form "<elem1>, <elem2>, <elem3>, ...".
1074 (FIXME?) Assumes array element separator is a comma, which is correct
1075 for all languages currently handled.
1076 (FIXME?) Some languages have a notation for repeated array elements,
1077 perhaps we should try to use that notation when appropriate.
1081 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
1086 struct ui_file *stream;
1090 enum val_prettyprint pretty;
1093 unsigned int things_printed = 0;
1095 struct type *elttype;
1097 /* Position of the array element we are examining to see
1098 whether it is repeated. */
1100 /* Number of repetitions we have detected so far. */
1103 elttype = TYPE_TARGET_TYPE (type);
1104 eltlen = TYPE_LENGTH (check_typedef (elttype));
1105 len = TYPE_LENGTH (type) / eltlen;
1107 annotate_array_section_begin (i, elttype);
1109 for (; i < len && things_printed < print_max; i++)
1113 if (prettyprint_arrays)
1115 fprintf_filtered (stream, ",\n");
1116 print_spaces_filtered (2 + 2 * recurse, stream);
1120 fprintf_filtered (stream, ", ");
1123 wrap_here (n_spaces (2 + 2 * recurse));
1127 while ((rep1 < len) &&
1128 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1134 if (reps > repeat_count_threshold)
1136 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1137 deref_ref, recurse + 1, pretty);
1138 annotate_elt_rep (reps);
1139 fprintf_filtered (stream, " <repeats %u times>", reps);
1140 annotate_elt_rep_end ();
1143 things_printed += repeat_count_threshold;
1147 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1148 deref_ref, recurse + 1, pretty);
1153 annotate_array_section_end ();
1156 fprintf_filtered (stream, "...");
1160 /* Read LEN bytes of target memory at address MEMADDR, placing the
1161 results in GDB's memory at MYADDR. Returns a count of the bytes
1162 actually read, and optionally an errno value in the location
1163 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1165 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1166 function be eliminated. */
1169 partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
1171 int nread; /* Number of bytes actually read. */
1172 int errcode; /* Error from last read. */
1174 /* First try a complete read. */
1175 errcode = target_read_memory (memaddr, myaddr, len);
1183 /* Loop, reading one byte at a time until we get as much as we can. */
1184 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1186 errcode = target_read_memory (memaddr++, myaddr++, 1);
1188 /* If an error, the last read was unsuccessful, so adjust count. */
1194 if (errnoptr != NULL)
1196 *errnoptr = errcode;
1201 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1202 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1203 stops at the first null byte, otherwise printing proceeds (including null
1204 bytes) until either print_max or LEN characters have been printed,
1205 whichever is smaller. */
1207 /* FIXME: Use target_read_string. */
1210 val_print_string (addr, len, width, stream)
1214 struct ui_file *stream;
1216 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1217 int errcode; /* Errno returned from bad reads. */
1218 unsigned int fetchlimit; /* Maximum number of chars to print. */
1219 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1220 unsigned int chunksize; /* Size of each fetch, in chars. */
1221 char *buffer = NULL; /* Dynamically growable fetch buffer. */
1222 char *bufptr; /* Pointer to next available byte in buffer. */
1223 char *limit; /* First location past end of fetch buffer. */
1224 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1225 int found_nul; /* Non-zero if we found the nul char */
1227 /* First we need to figure out the limit on the number of characters we are
1228 going to attempt to fetch and print. This is actually pretty simple. If
1229 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1230 LEN is -1, then the limit is print_max. This is true regardless of
1231 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1232 because finding the null byte (or available memory) is what actually
1233 limits the fetch. */
1235 fetchlimit = (len == -1 ? print_max : min (len, print_max));
1237 /* Now decide how large of chunks to try to read in one operation. This
1238 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1239 so we might as well read them all in one operation. If LEN is -1, we
1240 are looking for a null terminator to end the fetching, so we might as
1241 well read in blocks that are large enough to be efficient, but not so
1242 large as to be slow if fetchlimit happens to be large. So we choose the
1243 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1244 200 is way too big for remote debugging over a serial line. */
1246 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1248 /* Loop until we either have all the characters to print, or we encounter
1249 some error, such as bumping into the end of the address space. */
1252 old_chain = make_cleanup (null_cleanup, 0);
1256 buffer = (char *) xmalloc (len * width);
1258 old_chain = make_cleanup (free, buffer);
1260 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1262 addr += nfetch * width;
1263 bufptr += nfetch * width;
1267 unsigned long bufsize = 0;
1271 nfetch = min (chunksize, fetchlimit - bufsize);
1274 buffer = (char *) xmalloc (nfetch * width);
1277 discard_cleanups (old_chain);
1278 buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1281 old_chain = make_cleanup (free, buffer);
1282 bufptr = buffer + bufsize * width;
1285 /* Read as much as we can. */
1286 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1289 /* Scan this chunk for the null byte that terminates the string
1290 to print. If found, we don't need to fetch any more. Note
1291 that bufptr is explicitly left pointing at the next character
1292 after the null byte, or at the next character after the end of
1295 limit = bufptr + nfetch * width;
1296 while (bufptr < limit)
1300 c = extract_unsigned_integer (bufptr, width);
1305 /* We don't care about any error which happened after
1306 the NULL terminator. */
1313 while (errcode == 0 /* no error */
1314 && bufptr - buffer < fetchlimit * width /* no overrun */
1315 && !found_nul); /* haven't found nul yet */
1318 { /* length of string is really 0! */
1319 buffer = bufptr = NULL;
1323 /* bufptr and addr now point immediately beyond the last byte which we
1324 consider part of the string (including a '\0' which ends the string). */
1326 /* We now have either successfully filled the buffer to fetchlimit, or
1327 terminated early due to an error or finding a null char when LEN is -1. */
1329 if (len == -1 && !found_nul)
1333 /* We didn't find a null terminator we were looking for. Attempt
1334 to peek at the next character. If not successful, or it is not
1335 a null byte, then force ellipsis to be printed. */
1337 peekbuf = (char *) alloca (width);
1339 if (target_read_memory (addr, peekbuf, width) == 0
1340 && extract_unsigned_integer (peekbuf, width) != 0)
1343 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1345 /* Getting an error when we have a requested length, or fetching less
1346 than the number of characters actually requested, always make us
1353 /* If we get an error before fetching anything, don't print a string.
1354 But if we fetch something and then get an error, print the string
1355 and then the error message. */
1356 if (errcode == 0 || bufptr > buffer)
1360 fputs_filtered (" ", stream);
1362 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
1369 fprintf_filtered (stream, " <Address ");
1370 print_address_numeric (addr, 1, stream);
1371 fprintf_filtered (stream, " out of bounds>");
1375 fprintf_filtered (stream, " <Error reading address ");
1376 print_address_numeric (addr, 1, stream);
1377 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1381 do_cleanups (old_chain);
1382 return ((bufptr - buffer) / width);
1386 /* Validate an input or output radix setting, and make sure the user
1387 knows what they really did here. Radix setting is confusing, e.g.
1388 setting the input radix to "10" never changes it! */
1392 set_input_radix (args, from_tty, c)
1395 struct cmd_list_element *c;
1397 set_input_radix_1 (from_tty, *(unsigned *) c->var);
1402 set_input_radix_1 (from_tty, radix)
1406 /* We don't currently disallow any input radix except 0 or 1, which don't
1407 make any mathematical sense. In theory, we can deal with any input
1408 radix greater than 1, even if we don't have unique digits for every
1409 value from 0 to radix-1, but in practice we lose on large radix values.
1410 We should either fix the lossage or restrict the radix range more.
1415 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1418 input_radix = radix;
1421 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1422 radix, radix, radix);
1428 set_output_radix (args, from_tty, c)
1431 struct cmd_list_element *c;
1433 set_output_radix_1 (from_tty, *(unsigned *) c->var);
1437 set_output_radix_1 (from_tty, radix)
1441 /* Validate the radix and disallow ones that we aren't prepared to
1442 handle correctly, leaving the radix unchanged. */
1446 output_format = 'x'; /* hex */
1449 output_format = 0; /* decimal */
1452 output_format = 'o'; /* octal */
1455 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1458 output_radix = radix;
1461 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1462 radix, radix, radix);
1466 /* Set both the input and output radix at once. Try to set the output radix
1467 first, since it has the most restrictive range. An radix that is valid as
1468 an output radix is also valid as an input radix.
1470 It may be useful to have an unusual input radix. If the user wishes to
1471 set an input radix that is not valid as an output radix, he needs to use
1472 the 'set input-radix' command. */
1475 set_radix (arg, from_tty)
1481 radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
1482 set_output_radix_1 (0, radix);
1483 set_input_radix_1 (0, radix);
1486 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1487 radix, radix, radix);
1491 /* Show both the input and output radices. */
1495 show_radix (arg, from_tty)
1501 if (input_radix == output_radix)
1503 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1504 input_radix, input_radix, input_radix);
1508 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1509 input_radix, input_radix, input_radix);
1510 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1511 output_radix, output_radix, output_radix);
1519 set_print (arg, from_tty)
1524 "\"set print\" must be followed by the name of a print subcommand.\n");
1525 help_list (setprintlist, "set print ", -1, gdb_stdout);
1530 show_print (args, from_tty)
1534 cmd_show_list (showprintlist, from_tty, "");
1538 _initialize_valprint ()
1540 struct cmd_list_element *c;
1542 add_prefix_cmd ("print", no_class, set_print,
1543 "Generic command for setting how things print.",
1544 &setprintlist, "set print ", 0, &setlist);
1545 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1546 /* prefer set print to set prompt */
1547 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1549 add_prefix_cmd ("print", no_class, show_print,
1550 "Generic command for showing print settings.",
1551 &showprintlist, "show print ", 0, &showlist);
1552 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1553 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1556 (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
1557 "Set limit on string chars or array elements to print.\n\
1558 \"set print elements 0\" causes there to be no limit.",
1563 (add_set_cmd ("null-stop", no_class, var_boolean,
1564 (char *) &stop_print_at_null,
1565 "Set printing of char arrays to stop at first null char.",
1570 (add_set_cmd ("repeats", no_class, var_uinteger,
1571 (char *) &repeat_count_threshold,
1572 "Set threshold for repeated print elements.\n\
1573 \"set print repeats 0\" causes all elements to be individually printed.",
1578 (add_set_cmd ("pretty", class_support, var_boolean,
1579 (char *) &prettyprint_structs,
1580 "Set prettyprinting of structures.",
1585 (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
1586 "Set printing of unions interior to structures.",
1591 (add_set_cmd ("array", class_support, var_boolean,
1592 (char *) &prettyprint_arrays,
1593 "Set prettyprinting of arrays.",
1598 (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
1599 "Set printing of addresses.",
1603 c = add_set_cmd ("input-radix", class_support, var_uinteger,
1604 (char *) &input_radix,
1605 "Set default input radix for entering numbers.",
1607 add_show_from_set (c, &showlist);
1608 c->function.sfunc = set_input_radix;
1610 c = add_set_cmd ("output-radix", class_support, var_uinteger,
1611 (char *) &output_radix,
1612 "Set default output radix for printing of values.",
1614 add_show_from_set (c, &showlist);
1615 c->function.sfunc = set_output_radix;
1617 /* The "set radix" and "show radix" commands are special in that they are
1618 like normal set and show commands but allow two normally independent
1619 variables to be either set or shown with a single command. So the
1620 usual add_set_cmd() and add_show_from_set() commands aren't really
1622 add_cmd ("radix", class_support, set_radix,
1623 "Set default input and output number radices.\n\
1624 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1625 Without an argument, sets both radices back to the default value of 10.",
1627 add_cmd ("radix", class_support, show_radix,
1628 "Show the default input and output number radices.\n\
1629 Use 'show input-radix' or 'show output-radix' to independently show each.",
1632 /* Give people the defaults which they are used to. */
1633 prettyprint_structs = 0;
1634 prettyprint_arrays = 0;
1637 print_max = PRINT_MAX_DEFAULT;