1 /* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
35 #include "floatformat.h"
40 /* Prototypes for local functions */
42 static int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
43 int len, int *errnoptr);
45 static void print_hex_chars (struct ui_file *, unsigned char *,
48 static void show_print (char *, int);
50 static void set_print (char *, int);
52 static void set_radix (char *, int);
54 static void show_radix (char *, int);
56 static void set_input_radix (char *, int, struct cmd_list_element *);
58 static void set_input_radix_1 (int, unsigned);
60 static void set_output_radix (char *, int, struct cmd_list_element *);
62 static void set_output_radix_1 (int, unsigned);
64 void _initialize_valprint (void);
66 /* Maximum number of chars to print for a string pointer value or vector
67 contents, or UINT_MAX for no limit. Note that "set print elements 0"
68 stores UINT_MAX in print_max, which displays in a show command as
71 unsigned int print_max;
72 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
74 /* Default input and output radixes, and output format letter. */
76 unsigned input_radix = 10;
77 unsigned output_radix = 10;
78 int output_format = 0;
80 /* Print repeat counts if there are more than this many repetitions of an
81 element in an array. Referenced by the low level language dependent
84 unsigned int repeat_count_threshold = 10;
86 /* If nonzero, stops printing of char arrays at first null. */
88 int stop_print_at_null;
90 /* Controls pretty printing of structures. */
92 int prettyprint_structs;
94 /* Controls pretty printing of arrays. */
96 int prettyprint_arrays;
98 /* If nonzero, causes unions inside structures or other unions to be
101 int unionprint; /* Controls printing of nested unions. */
103 /* If nonzero, causes machine addresses to be printed in certain contexts. */
105 int addressprint; /* Controls printing of machine addresses */
108 /* Print data of type TYPE located at VALADDR (within GDB), which came from
109 the inferior at address ADDRESS, onto stdio stream STREAM according to
110 FORMAT (a letter, or 0 for natural format using TYPE).
112 If DEREF_REF is nonzero, then dereference references, otherwise just print
115 The PRETTY parameter controls prettyprinting.
117 If the data are a string pointer, returns the number of string characters
120 FIXME: The data at VALADDR is in target byte order. If gdb is ever
121 enhanced to be able to debug more than the single target it was compiled
122 for (specific CPU type and thus specific target byte ordering), then
123 either the print routines are going to have to take this into account,
124 or the data is going to have to be passed into here already converted
125 to the host byte ordering, whichever is more convenient. */
129 val_print (struct type *type, char *valaddr, int embedded_offset,
130 CORE_ADDR address, struct ui_file *stream, int format, int deref_ref,
131 int recurse, enum val_prettyprint pretty)
133 struct type *real_type = check_typedef (type);
134 if (pretty == Val_pretty_default)
136 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
141 /* Ensure that the type is complete and not just a stub. If the type is
142 only a stub and we can't find and substitute its complete type, then
143 print appropriate string and return. */
145 if (TYPE_STUB (real_type))
147 fprintf_filtered (stream, "<incomplete type>");
152 return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
153 stream, format, deref_ref, recurse, pretty));
156 /* Print the value VAL in C-ish syntax on stream STREAM.
157 FORMAT is a format-letter, or 0 for print in natural format of data type.
158 If the object printed is a string pointer, returns
159 the number of string bytes printed. */
162 value_print (struct value *val, struct ui_file *stream, int format,
163 enum val_prettyprint pretty)
167 printf_filtered ("<address of value unknown>");
170 if (VALUE_OPTIMIZED_OUT (val))
172 printf_filtered ("<value optimized out>");
175 return LA_VALUE_PRINT (val, stream, format, pretty);
178 /* Called by various <lang>_val_print routines to print
179 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
180 value. STREAM is where to print the value. */
183 val_print_type_code_int (struct type *type, char *valaddr,
184 struct ui_file *stream)
186 if (TYPE_LENGTH (type) > sizeof (LONGEST))
190 if (TYPE_UNSIGNED (type)
191 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
194 print_longest (stream, 'u', 0, val);
198 /* Signed, or we couldn't turn an unsigned value into a
199 LONGEST. For signed values, one could assume two's
200 complement (a reasonable assumption, I think) and do
202 print_hex_chars (stream, (unsigned char *) valaddr,
208 #ifdef PRINT_TYPELESS_INTEGER
209 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
211 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
212 unpack_long (type, valaddr));
217 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
218 The raison d'etre of this function is to consolidate printing of
219 LONG_LONG's into this one function. Some platforms have long longs but
220 don't have a printf() that supports "ll" in the format string. We handle
221 these by seeing if the number is representable as either a signed or
222 unsigned long, depending upon what format is desired, and if not we just
223 bail out and print the number in hex.
225 The format chars b,h,w,g are from print_scalar_formatted(). If USE_LOCAL,
226 format it according to the current language (this should be used for most
227 integers which GDB prints, the exception is things like protocols where
228 the format of the integer is a protocol thing, not a user-visible thing).
231 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
232 static void print_decimal (struct ui_file * stream, char *sign,
233 int use_local, ULONGEST val_ulong);
235 print_decimal (struct ui_file *stream, char *sign, int use_local,
238 unsigned long temp[3];
242 temp[i] = val_ulong % (1000 * 1000 * 1000);
243 val_ulong /= (1000 * 1000 * 1000);
246 while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
250 fprintf_filtered (stream, "%s%lu",
254 fprintf_filtered (stream, "%s%lu%09lu",
255 sign, temp[1], temp[0]);
258 fprintf_filtered (stream, "%s%lu%09lu%09lu",
259 sign, temp[2], temp[1], temp[0]);
262 internal_error (__FILE__, __LINE__, "failed internal consistency check");
269 print_longest (struct ui_file *stream, int format, int use_local,
272 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
273 if (sizeof (long) < sizeof (LONGEST))
279 /* Print a signed value, that doesn't fit in a long */
280 if ((long) val_long != val_long)
283 print_decimal (stream, "-", use_local, -val_long);
285 print_decimal (stream, "", use_local, val_long);
292 /* Print an unsigned value, that doesn't fit in a long */
293 if ((unsigned long) val_long != (ULONGEST) val_long)
295 print_decimal (stream, "", use_local, val_long);
306 /* Print as unsigned value, must fit completely in unsigned long */
308 unsigned long temp = val_long;
309 if (temp != val_long)
311 /* Urk, can't represent value in long so print in hex.
312 Do shift in two operations so that if sizeof (long)
313 == sizeof (LONGEST) we can avoid warnings from
314 picky compilers about shifts >= the size of the
316 unsigned long vbot = (unsigned long) val_long;
317 LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
318 unsigned long vtop = temp >> 1;
319 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
328 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
332 fprintf_filtered (stream,
333 use_local ? local_decimal_format_custom ("ll")
338 fprintf_filtered (stream, "%llu", val_long);
341 fprintf_filtered (stream,
342 use_local ? local_hex_format_custom ("ll")
347 fprintf_filtered (stream,
348 use_local ? local_octal_format_custom ("ll")
353 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
356 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
359 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
362 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
365 internal_error (__FILE__, __LINE__, "failed internal consistency check");
367 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
368 /* In the following it is important to coerce (val_long) to a long. It does
369 nothing if !LONG_LONG, but it will chop off the top half (which we know
370 we can ignore) if the host supports long longs. */
375 fprintf_filtered (stream,
376 use_local ? local_decimal_format_custom ("l")
381 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
384 fprintf_filtered (stream,
385 use_local ? local_hex_format_custom ("l")
387 (unsigned long) val_long);
390 fprintf_filtered (stream,
391 use_local ? local_octal_format_custom ("l")
393 (unsigned long) val_long);
396 fprintf_filtered (stream, local_hex_format_custom ("02l"),
397 (unsigned long) val_long);
400 fprintf_filtered (stream, local_hex_format_custom ("04l"),
401 (unsigned long) val_long);
404 fprintf_filtered (stream, local_hex_format_custom ("08l"),
405 (unsigned long) val_long);
408 fprintf_filtered (stream, local_hex_format_custom ("016l"),
409 (unsigned long) val_long);
412 internal_error (__FILE__, __LINE__, "failed internal consistency check");
414 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
419 strcat_longest (int format, int use_local, LONGEST val_long, char *buf,
422 /* FIXME: Use buflen to avoid buffer overflow. */
423 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
426 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
427 vbot = (long) val_long;
429 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
430 || ((format == 'u' || format == 'x') && (unsigned long long) val_long > UINT_MAX))
432 sprintf (buf, "0x%lx%08lx", vtop, vbot);
437 #ifdef PRINTF_HAS_LONG_LONG
442 (use_local ? local_decimal_format_custom ("ll") : "%lld"),
446 sprintf (buf, "%llu", val_long);
450 (use_local ? local_hex_format_custom ("ll") : "%llx"),
456 (use_local ? local_octal_format_custom ("ll") : "%llo"),
460 sprintf (buf, local_hex_format_custom ("02ll"), val_long);
463 sprintf (buf, local_hex_format_custom ("04ll"), val_long);
466 sprintf (buf, local_hex_format_custom ("08ll"), val_long);
469 sprintf (buf, local_hex_format_custom ("016ll"), val_long);
472 internal_error (__FILE__, __LINE__, "failed internal consistency check");
474 #else /* !PRINTF_HAS_LONG_LONG */
475 /* In the following it is important to coerce (val_long) to a long. It does
476 nothing if !LONG_LONG, but it will chop off the top half (which we know
477 we can ignore) if the host supports long longs. */
482 sprintf (buf, (use_local ? local_decimal_format_custom ("l") : "%ld"),
486 sprintf (buf, "%lu", ((unsigned long) val_long));
489 sprintf (buf, (use_local ? local_hex_format_custom ("l") : "%lx"),
493 sprintf (buf, (use_local ? local_octal_format_custom ("l") : "%lo"),
497 sprintf (buf, local_hex_format_custom ("02l"),
501 sprintf (buf, local_hex_format_custom ("04l"),
505 sprintf (buf, local_hex_format_custom ("08l"),
509 sprintf (buf, local_hex_format_custom ("016l"),
513 internal_error (__FILE__, __LINE__, "failed internal consistency check");
516 #endif /* !PRINTF_HAS_LONG_LONG */
520 /* This used to be a macro, but I don't think it is called often enough
521 to merit such treatment. */
522 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
523 arguments to a function, number in a value history, register number, etc.)
524 where the value must not be larger than can fit in an int. */
527 longest_to_int (LONGEST arg)
529 /* Let the compiler do the work */
530 int rtnval = (int) arg;
532 /* Check for overflows or underflows */
533 if (sizeof (LONGEST) > sizeof (int))
537 error ("Value out of range.");
543 /* Print a floating point value of type TYPE (not always a
544 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
547 print_floating (char *valaddr, struct type *type, struct ui_file *stream)
551 const struct floatformat *fmt = NULL;
552 unsigned len = TYPE_LENGTH (type);
554 /* If it is a floating-point, check for obvious problems. */
555 if (TYPE_CODE (type) == TYPE_CODE_FLT)
556 fmt = floatformat_from_type (type);
557 if (fmt != NULL && floatformat_is_nan (fmt, valaddr))
559 if (floatformat_is_negative (fmt, valaddr))
560 fprintf_filtered (stream, "-");
561 fprintf_filtered (stream, "nan(");
562 fprintf_filtered (stream, local_hex_format_prefix ());
563 fprintf_filtered (stream, floatformat_mantissa (fmt, valaddr));
564 fprintf_filtered (stream, local_hex_format_suffix ());
565 fprintf_filtered (stream, ")");
569 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
570 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
571 needs to be used as that takes care of any necessary type
572 conversions. Such conversions are of course direct to DOUBLEST
573 and disregard any possible target floating point limitations.
574 For instance, a u64 would be converted and displayed exactly on a
575 host with 80 bit DOUBLEST but with loss of information on a host
576 with 64 bit DOUBLEST. */
578 doub = unpack_double (type, valaddr, &inv);
581 fprintf_filtered (stream, "<invalid float value>");
585 /* FIXME: kettenis/2001-01-20: The following code makes too much
586 assumptions about the host and target floating point format. */
588 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
589 not necessarially be a TYPE_CODE_FLT, the below ignores that and
590 instead uses the type's length to determine the precision of the
591 floating-point value being printed. */
593 if (len < sizeof (double))
594 fprintf_filtered (stream, "%.9g", (double) doub);
595 else if (len == sizeof (double))
596 fprintf_filtered (stream, "%.17g", (double) doub);
598 #ifdef PRINTF_HAS_LONG_DOUBLE
599 fprintf_filtered (stream, "%.35Lg", doub);
601 /* This at least wins with values that are representable as
603 fprintf_filtered (stream, "%.17g", (double) doub);
608 print_binary_chars (struct ui_file *stream, unsigned char *valaddr,
612 #define BITS_IN_BYTES 8
618 /* Declared "int" so it will be signed.
619 * This ensures that right shift will shift in zeros.
621 const int mask = 0x080;
623 /* FIXME: We should be not printing leading zeroes in most cases. */
625 fprintf_filtered (stream, local_binary_format_prefix ());
626 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
632 /* Every byte has 8 binary characters; peel off
633 * and print from the MSB end.
635 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
637 if (*p & (mask >> i))
642 fprintf_filtered (stream, "%1d", b);
648 for (p = valaddr + len - 1;
652 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
654 if (*p & (mask >> i))
659 fprintf_filtered (stream, "%1d", b);
663 fprintf_filtered (stream, local_binary_format_suffix ());
666 /* VALADDR points to an integer of LEN bytes.
667 * Print it in octal on stream or format it in buf.
670 print_octal_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
673 unsigned char octa1, octa2, octa3, carry;
676 /* FIXME: We should be not printing leading zeroes in most cases. */
679 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
680 * the extra bits, which cycle every three bytes:
684 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
686 * Octal side: 0 1 carry 3 4 carry ...
688 * Cycle number: 0 1 2
690 * But of course we are printing from the high side, so we have to
691 * figure out where in the cycle we are so that we end up with no
692 * left over bits at the end.
694 #define BITS_IN_OCTAL 3
695 #define HIGH_ZERO 0340
696 #define LOW_ZERO 0016
697 #define CARRY_ZERO 0003
698 #define HIGH_ONE 0200
701 #define CARRY_ONE 0001
702 #define HIGH_TWO 0300
706 /* For 32 we start in cycle 2, with two bits and one bit carry;
707 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
709 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
712 fprintf_filtered (stream, local_octal_format_prefix ());
713 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
722 /* No carry in, carry out two bits.
724 octa1 = (HIGH_ZERO & *p) >> 5;
725 octa2 = (LOW_ZERO & *p) >> 2;
726 carry = (CARRY_ZERO & *p);
727 fprintf_filtered (stream, "%o", octa1);
728 fprintf_filtered (stream, "%o", octa2);
732 /* Carry in two bits, carry out one bit.
734 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
735 octa2 = (MID_ONE & *p) >> 4;
736 octa3 = (LOW_ONE & *p) >> 1;
737 carry = (CARRY_ONE & *p);
738 fprintf_filtered (stream, "%o", octa1);
739 fprintf_filtered (stream, "%o", octa2);
740 fprintf_filtered (stream, "%o", octa3);
744 /* Carry in one bit, no carry out.
746 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
747 octa2 = (MID_TWO & *p) >> 3;
748 octa3 = (LOW_TWO & *p);
750 fprintf_filtered (stream, "%o", octa1);
751 fprintf_filtered (stream, "%o", octa2);
752 fprintf_filtered (stream, "%o", octa3);
756 error ("Internal error in octal conversion;");
760 cycle = cycle % BITS_IN_OCTAL;
765 for (p = valaddr + len - 1;
772 /* Carry out, no carry in */
773 octa1 = (HIGH_ZERO & *p) >> 5;
774 octa2 = (LOW_ZERO & *p) >> 2;
775 carry = (CARRY_ZERO & *p);
776 fprintf_filtered (stream, "%o", octa1);
777 fprintf_filtered (stream, "%o", octa2);
781 /* Carry in, carry out */
782 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
783 octa2 = (MID_ONE & *p) >> 4;
784 octa3 = (LOW_ONE & *p) >> 1;
785 carry = (CARRY_ONE & *p);
786 fprintf_filtered (stream, "%o", octa1);
787 fprintf_filtered (stream, "%o", octa2);
788 fprintf_filtered (stream, "%o", octa3);
792 /* Carry in, no carry out */
793 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
794 octa2 = (MID_TWO & *p) >> 3;
795 octa3 = (LOW_TWO & *p);
797 fprintf_filtered (stream, "%o", octa1);
798 fprintf_filtered (stream, "%o", octa2);
799 fprintf_filtered (stream, "%o", octa3);
803 error ("Internal error in octal conversion;");
807 cycle = cycle % BITS_IN_OCTAL;
811 fprintf_filtered (stream, local_octal_format_suffix ());
814 /* VALADDR points to an integer of LEN bytes.
815 * Print it in decimal on stream or format it in buf.
818 print_decimal_chars (struct ui_file *stream, unsigned char *valaddr,
822 #define TWO_TO_FOURTH 16
823 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
824 #define CARRY_LEFT( x ) ((x) % TEN)
825 #define SHIFT( x ) ((x) << 4)
827 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1)
829 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
831 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? p++ : p-- )
832 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
833 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
836 unsigned char *digits;
839 int i, j, decimal_digits;
843 /* Base-ten number is less than twice as many digits
844 * as the base 16 number, which is 2 digits per byte.
846 decimal_len = len * 2 * 2;
847 digits = xmalloc (decimal_len);
849 for (i = 0; i < decimal_len; i++)
854 fprintf_filtered (stream, local_decimal_format_prefix ());
856 /* Ok, we have an unknown number of bytes of data to be printed in
859 * Given a hex number (in nibbles) as XYZ, we start by taking X and
860 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
861 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
863 * The trick is that "digits" holds a base-10 number, but sometimes
864 * the individual digits are > 10.
866 * Outer loop is per nibble (hex digit) of input, from MSD end to
869 decimal_digits = 0; /* Number of decimal digits so far */
875 * Multiply current base-ten number by 16 in place.
876 * Each digit was between 0 and 9, now is between
879 for (j = 0; j < decimal_digits; j++)
881 digits[j] = SHIFT (digits[j]);
884 /* Take the next nibble off the input and add it to what
885 * we've got in the LSB position. Bottom 'digit' is now
888 * "flip" is used to run this loop twice for each byte.
894 digits[0] += HIGH_NIBBLE (*p);
899 /* Take low nibble and bump our pointer "p".
901 digits[0] += LOW_NIBBLE (*p);
906 /* Re-decimalize. We have to do this often enough
907 * that we don't overflow, but once per nibble is
908 * overkill. Easier this way, though. Note that the
909 * carry is often larger than 10 (e.g. max initial
910 * carry out of lowest nibble is 15, could bubble all
911 * the way up greater than 10). So we have to do
912 * the carrying beyond the last current digit.
915 for (j = 0; j < decimal_len - 1; j++)
919 /* "/" won't handle an unsigned char with
920 * a value that if signed would be negative.
921 * So extend to longword int via "dummy".
924 carry = CARRY_OUT (dummy);
925 digits[j] = CARRY_LEFT (dummy);
927 if (j >= decimal_digits && carry == 0)
930 * All higher digits are 0 and we
931 * no longer have a carry.
933 * Note: "j" is 0-based, "decimal_digits" is
936 decimal_digits = j + 1;
942 /* Ok, now "digits" is the decimal representation, with
943 * the "decimal_digits" actual digits. Print!
945 for (i = decimal_digits - 1; i >= 0; i--)
947 fprintf_filtered (stream, "%1d", digits[i]);
951 fprintf_filtered (stream, local_decimal_format_suffix ());
954 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
957 print_hex_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
961 /* FIXME: We should be not printing leading zeroes in most cases. */
963 fprintf_filtered (stream, local_hex_format_prefix ());
964 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
970 fprintf_filtered (stream, "%02x", *p);
975 for (p = valaddr + len - 1;
979 fprintf_filtered (stream, "%02x", *p);
982 fprintf_filtered (stream, local_hex_format_suffix ());
985 /* Called by various <lang>_val_print routines to print elements of an
986 array in the form "<elem1>, <elem2>, <elem3>, ...".
988 (FIXME?) Assumes array element separator is a comma, which is correct
989 for all languages currently handled.
990 (FIXME?) Some languages have a notation for repeated array elements,
991 perhaps we should try to use that notation when appropriate.
995 val_print_array_elements (struct type *type, char *valaddr, CORE_ADDR address,
996 struct ui_file *stream, int format, int deref_ref,
997 int recurse, enum val_prettyprint pretty,
1000 unsigned int things_printed = 0;
1002 struct type *elttype;
1004 /* Position of the array element we are examining to see
1005 whether it is repeated. */
1007 /* Number of repetitions we have detected so far. */
1010 elttype = TYPE_TARGET_TYPE (type);
1011 eltlen = TYPE_LENGTH (check_typedef (elttype));
1012 len = TYPE_LENGTH (type) / eltlen;
1014 annotate_array_section_begin (i, elttype);
1016 for (; i < len && things_printed < print_max; i++)
1020 if (prettyprint_arrays)
1022 fprintf_filtered (stream, ",\n");
1023 print_spaces_filtered (2 + 2 * recurse, stream);
1027 fprintf_filtered (stream, ", ");
1030 wrap_here (n_spaces (2 + 2 * recurse));
1034 while ((rep1 < len) &&
1035 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1041 if (reps > repeat_count_threshold)
1043 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1044 deref_ref, recurse + 1, pretty);
1045 annotate_elt_rep (reps);
1046 fprintf_filtered (stream, " <repeats %u times>", reps);
1047 annotate_elt_rep_end ();
1050 things_printed += repeat_count_threshold;
1054 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1055 deref_ref, recurse + 1, pretty);
1060 annotate_array_section_end ();
1063 fprintf_filtered (stream, "...");
1067 /* Read LEN bytes of target memory at address MEMADDR, placing the
1068 results in GDB's memory at MYADDR. Returns a count of the bytes
1069 actually read, and optionally an errno value in the location
1070 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1072 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1073 function be eliminated. */
1076 partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
1078 int nread; /* Number of bytes actually read. */
1079 int errcode; /* Error from last read. */
1081 /* First try a complete read. */
1082 errcode = target_read_memory (memaddr, myaddr, len);
1090 /* Loop, reading one byte at a time until we get as much as we can. */
1091 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1093 errcode = target_read_memory (memaddr++, myaddr++, 1);
1095 /* If an error, the last read was unsuccessful, so adjust count. */
1101 if (errnoptr != NULL)
1103 *errnoptr = errcode;
1108 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1109 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1110 stops at the first null byte, otherwise printing proceeds (including null
1111 bytes) until either print_max or LEN characters have been printed,
1112 whichever is smaller. */
1114 /* FIXME: Use target_read_string. */
1117 val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream)
1119 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1120 int errcode; /* Errno returned from bad reads. */
1121 unsigned int fetchlimit; /* Maximum number of chars to print. */
1122 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1123 unsigned int chunksize; /* Size of each fetch, in chars. */
1124 char *buffer = NULL; /* Dynamically growable fetch buffer. */
1125 char *bufptr; /* Pointer to next available byte in buffer. */
1126 char *limit; /* First location past end of fetch buffer. */
1127 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1128 int found_nul; /* Non-zero if we found the nul char */
1130 /* First we need to figure out the limit on the number of characters we are
1131 going to attempt to fetch and print. This is actually pretty simple. If
1132 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1133 LEN is -1, then the limit is print_max. This is true regardless of
1134 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1135 because finding the null byte (or available memory) is what actually
1136 limits the fetch. */
1138 fetchlimit = (len == -1 ? print_max : min (len, print_max));
1140 /* Now decide how large of chunks to try to read in one operation. This
1141 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1142 so we might as well read them all in one operation. If LEN is -1, we
1143 are looking for a null terminator to end the fetching, so we might as
1144 well read in blocks that are large enough to be efficient, but not so
1145 large as to be slow if fetchlimit happens to be large. So we choose the
1146 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1147 200 is way too big for remote debugging over a serial line. */
1149 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1151 /* Loop until we either have all the characters to print, or we encounter
1152 some error, such as bumping into the end of the address space. */
1155 old_chain = make_cleanup (null_cleanup, 0);
1159 buffer = (char *) xmalloc (len * width);
1161 old_chain = make_cleanup (xfree, buffer);
1163 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1165 addr += nfetch * width;
1166 bufptr += nfetch * width;
1170 unsigned long bufsize = 0;
1174 nfetch = min (chunksize, fetchlimit - bufsize);
1177 buffer = (char *) xmalloc (nfetch * width);
1180 discard_cleanups (old_chain);
1181 buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1184 old_chain = make_cleanup (xfree, buffer);
1185 bufptr = buffer + bufsize * width;
1188 /* Read as much as we can. */
1189 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1192 /* Scan this chunk for the null byte that terminates the string
1193 to print. If found, we don't need to fetch any more. Note
1194 that bufptr is explicitly left pointing at the next character
1195 after the null byte, or at the next character after the end of
1198 limit = bufptr + nfetch * width;
1199 while (bufptr < limit)
1203 c = extract_unsigned_integer (bufptr, width);
1208 /* We don't care about any error which happened after
1209 the NULL terminator. */
1216 while (errcode == 0 /* no error */
1217 && bufptr - buffer < fetchlimit * width /* no overrun */
1218 && !found_nul); /* haven't found nul yet */
1221 { /* length of string is really 0! */
1222 buffer = bufptr = NULL;
1226 /* bufptr and addr now point immediately beyond the last byte which we
1227 consider part of the string (including a '\0' which ends the string). */
1229 /* We now have either successfully filled the buffer to fetchlimit, or
1230 terminated early due to an error or finding a null char when LEN is -1. */
1232 if (len == -1 && !found_nul)
1236 /* We didn't find a null terminator we were looking for. Attempt
1237 to peek at the next character. If not successful, or it is not
1238 a null byte, then force ellipsis to be printed. */
1240 peekbuf = (char *) alloca (width);
1242 if (target_read_memory (addr, peekbuf, width) == 0
1243 && extract_unsigned_integer (peekbuf, width) != 0)
1246 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1248 /* Getting an error when we have a requested length, or fetching less
1249 than the number of characters actually requested, always make us
1256 /* If we get an error before fetching anything, don't print a string.
1257 But if we fetch something and then get an error, print the string
1258 and then the error message. */
1259 if (errcode == 0 || bufptr > buffer)
1263 fputs_filtered (" ", stream);
1265 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
1272 fprintf_filtered (stream, " <Address ");
1273 print_address_numeric (addr, 1, stream);
1274 fprintf_filtered (stream, " out of bounds>");
1278 fprintf_filtered (stream, " <Error reading address ");
1279 print_address_numeric (addr, 1, stream);
1280 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1284 do_cleanups (old_chain);
1285 return ((bufptr - buffer) / width);
1289 /* Validate an input or output radix setting, and make sure the user
1290 knows what they really did here. Radix setting is confusing, e.g.
1291 setting the input radix to "10" never changes it! */
1295 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1297 set_input_radix_1 (from_tty, *(unsigned *) c->var);
1302 set_input_radix_1 (int from_tty, unsigned radix)
1304 /* We don't currently disallow any input radix except 0 or 1, which don't
1305 make any mathematical sense. In theory, we can deal with any input
1306 radix greater than 1, even if we don't have unique digits for every
1307 value from 0 to radix-1, but in practice we lose on large radix values.
1308 We should either fix the lossage or restrict the radix range more.
1313 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1316 input_radix = radix;
1319 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1320 radix, radix, radix);
1326 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1328 set_output_radix_1 (from_tty, *(unsigned *) c->var);
1332 set_output_radix_1 (int from_tty, unsigned radix)
1334 /* Validate the radix and disallow ones that we aren't prepared to
1335 handle correctly, leaving the radix unchanged. */
1339 output_format = 'x'; /* hex */
1342 output_format = 0; /* decimal */
1345 output_format = 'o'; /* octal */
1348 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1351 output_radix = radix;
1354 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1355 radix, radix, radix);
1359 /* Set both the input and output radix at once. Try to set the output radix
1360 first, since it has the most restrictive range. An radix that is valid as
1361 an output radix is also valid as an input radix.
1363 It may be useful to have an unusual input radix. If the user wishes to
1364 set an input radix that is not valid as an output radix, he needs to use
1365 the 'set input-radix' command. */
1368 set_radix (char *arg, int from_tty)
1372 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1373 set_output_radix_1 (0, radix);
1374 set_input_radix_1 (0, radix);
1377 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1378 radix, radix, radix);
1382 /* Show both the input and output radices. */
1386 show_radix (char *arg, int from_tty)
1390 if (input_radix == output_radix)
1392 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1393 input_radix, input_radix, input_radix);
1397 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1398 input_radix, input_radix, input_radix);
1399 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1400 output_radix, output_radix, output_radix);
1408 set_print (char *arg, int from_tty)
1411 "\"set print\" must be followed by the name of a print subcommand.\n");
1412 help_list (setprintlist, "set print ", -1, gdb_stdout);
1417 show_print (char *args, int from_tty)
1419 cmd_show_list (showprintlist, from_tty, "");
1423 _initialize_valprint (void)
1425 struct cmd_list_element *c;
1427 add_prefix_cmd ("print", no_class, set_print,
1428 "Generic command for setting how things print.",
1429 &setprintlist, "set print ", 0, &setlist);
1430 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1431 /* prefer set print to set prompt */
1432 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1434 add_prefix_cmd ("print", no_class, show_print,
1435 "Generic command for showing print settings.",
1436 &showprintlist, "show print ", 0, &showlist);
1437 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1438 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1441 (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
1442 "Set limit on string chars or array elements to print.\n\
1443 \"set print elements 0\" causes there to be no limit.",
1448 (add_set_cmd ("null-stop", no_class, var_boolean,
1449 (char *) &stop_print_at_null,
1450 "Set printing of char arrays to stop at first null char.",
1455 (add_set_cmd ("repeats", no_class, var_uinteger,
1456 (char *) &repeat_count_threshold,
1457 "Set threshold for repeated print elements.\n\
1458 \"set print repeats 0\" causes all elements to be individually printed.",
1463 (add_set_cmd ("pretty", class_support, var_boolean,
1464 (char *) &prettyprint_structs,
1465 "Set prettyprinting of structures.",
1470 (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
1471 "Set printing of unions interior to structures.",
1476 (add_set_cmd ("array", class_support, var_boolean,
1477 (char *) &prettyprint_arrays,
1478 "Set prettyprinting of arrays.",
1483 (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
1484 "Set printing of addresses.",
1488 c = add_set_cmd ("input-radix", class_support, var_uinteger,
1489 (char *) &input_radix,
1490 "Set default input radix for entering numbers.",
1492 add_show_from_set (c, &showlist);
1493 c->function.sfunc = set_input_radix;
1495 c = add_set_cmd ("output-radix", class_support, var_uinteger,
1496 (char *) &output_radix,
1497 "Set default output radix for printing of values.",
1499 add_show_from_set (c, &showlist);
1500 c->function.sfunc = set_output_radix;
1502 /* The "set radix" and "show radix" commands are special in that they are
1503 like normal set and show commands but allow two normally independent
1504 variables to be either set or shown with a single command. So the
1505 usual add_set_cmd() and add_show_from_set() commands aren't really
1507 add_cmd ("radix", class_support, set_radix,
1508 "Set default input and output number radices.\n\
1509 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1510 Without an argument, sets both radices back to the default value of 10.",
1512 add_cmd ("radix", class_support, show_radix,
1513 "Show the default input and output number radices.\n\
1514 Use 'show input-radix' or 'show output-radix' to independently show each.",
1517 /* Give people the defaults which they are used to. */
1518 prettyprint_structs = 0;
1519 prettyprint_arrays = 0;
1522 print_max = PRINT_MAX_DEFAULT;