1 /* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1998
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 void print_hex_chars
PARAMS ((GDB_FILE
*, unsigned char *,
43 static void show_print
PARAMS ((char *, int));
45 static void set_print
PARAMS ((char *, int));
47 static void set_radix
PARAMS ((char *, int));
49 static void show_radix
PARAMS ((char *, int));
51 static void set_input_radix
PARAMS ((char *, int, struct cmd_list_element
*));
53 static void set_input_radix_1
PARAMS ((int, unsigned));
55 static void set_output_radix
PARAMS ((char *, int, struct cmd_list_element
*));
57 static void set_output_radix_1
PARAMS ((int, unsigned));
59 void _initialize_valprint
PARAMS ((void));
61 /* Maximum number of chars to print for a string pointer value or vector
62 contents, or UINT_MAX for no limit. Note that "set print elements 0"
63 stores UINT_MAX in print_max, which displays in a show command as
66 unsigned int print_max
;
67 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
69 /* Default input and output radixes, and output format letter. */
71 unsigned input_radix
= 10;
72 unsigned output_radix
= 10;
73 int output_format
= 0;
75 /* Print repeat counts if there are more than this many repetitions of an
76 element in an array. Referenced by the low level language dependent
79 unsigned int repeat_count_threshold
= 10;
81 /* If nonzero, stops printing of char arrays at first null. */
83 int stop_print_at_null
;
85 /* Controls pretty printing of structures. */
87 int prettyprint_structs
;
89 /* Controls pretty printing of arrays. */
91 int prettyprint_arrays
;
93 /* If nonzero, causes unions inside structures or other unions to be
96 int unionprint
; /* Controls printing of nested unions. */
98 /* If nonzero, causes machine addresses to be printed in certain contexts. */
100 int addressprint
; /* Controls printing of machine addresses */
103 /* Print data of type TYPE located at VALADDR (within GDB), which came from
104 the inferior at address ADDRESS, onto stdio stream STREAM according to
105 FORMAT (a letter, or 0 for natural format using TYPE).
107 If DEREF_REF is nonzero, then dereference references, otherwise just print
110 The PRETTY parameter controls prettyprinting.
112 If the data are a string pointer, returns the number of string characters
115 FIXME: The data at VALADDR is in target byte order. If gdb is ever
116 enhanced to be able to debug more than the single target it was compiled
117 for (specific CPU type and thus specific target byte ordering), then
118 either the print routines are going to have to take this into account,
119 or the data is going to have to be passed into here already converted
120 to the host byte ordering, whichever is more convenient. */
124 val_print (type
, valaddr
, embedded_offset
, address
,
125 stream
, format
, deref_ref
, recurse
, pretty
)
134 enum val_prettyprint pretty
;
136 struct type
*real_type
= check_typedef (type
);
137 if (pretty
== Val_pretty_default
)
139 pretty
= prettyprint_structs
? Val_prettyprint
: Val_no_prettyprint
;
144 /* Ensure that the type is complete and not just a stub. If the type is
145 only a stub and we can't find and substitute its complete type, then
146 print appropriate string and return. */
148 if (TYPE_FLAGS (real_type
) & TYPE_FLAG_STUB
)
150 fprintf_filtered (stream
, "<incomplete type>");
155 return (LA_VAL_PRINT (type
, valaddr
, embedded_offset
, address
,
156 stream
, format
, deref_ref
, recurse
, pretty
));
159 /* Print the value VAL in C-ish syntax on stream STREAM.
160 FORMAT is a format-letter, or 0 for print in natural format of data type.
161 If the object printed is a string pointer, returns
162 the number of string bytes printed. */
165 value_print (val
, stream
, format
, pretty
)
169 enum val_prettyprint pretty
;
173 printf_filtered ("<address of value unknown>");
176 if (VALUE_OPTIMIZED_OUT (val
))
178 printf_filtered ("<value optimized out>");
181 return LA_VALUE_PRINT (val
, stream
, format
, pretty
);
184 /* Called by various <lang>_val_print routines to print
185 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
186 value. STREAM is where to print the value. */
189 val_print_type_code_int (type
, valaddr
, stream
)
194 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
198 if (TYPE_UNSIGNED (type
)
199 && extract_long_unsigned_integer (valaddr
, TYPE_LENGTH (type
),
202 print_longest (stream
, 'u', 0, val
);
206 /* Signed, or we couldn't turn an unsigned value into a
207 LONGEST. For signed values, one could assume two's
208 complement (a reasonable assumption, I think) and do
210 print_hex_chars (stream
, (unsigned char *) valaddr
,
216 #ifdef PRINT_TYPELESS_INTEGER
217 PRINT_TYPELESS_INTEGER (stream
, type
, unpack_long (type
, valaddr
));
219 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0,
220 unpack_long (type
, valaddr
));
225 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
226 The raison d'etre of this function is to consolidate printing of
227 LONG_LONG's into this one function. Some platforms have long longs but
228 don't have a printf() that supports "ll" in the format string. We handle
229 these by seeing if the number is representable as either a signed or
230 unsigned long, depending upon what format is desired, and if not we just
231 bail out and print the number in hex.
233 The format chars b,h,w,g are from print_scalar_formatted(). If USE_LOCAL,
234 format it according to the current language (this should be used for most
235 integers which GDB prints, the exception is things like protocols where
236 the format of the integer is a protocol thing, not a user-visible thing).
239 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
240 static void print_decimal
PARAMS ((GDB_FILE
* stream
, char *sign
, int use_local
, ULONGEST val_ulong
));
242 print_decimal (stream
, sign
, use_local
, val_ulong
)
248 unsigned long temp
[3];
252 temp
[i
] = val_ulong
% (1000 * 1000 * 1000);
253 val_ulong
/= (1000 * 1000 * 1000);
256 while (val_ulong
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
260 fprintf_filtered (stream
, "%s%lu",
264 fprintf_filtered (stream
, "%s%lu%09lu",
265 sign
, temp
[1], temp
[0]);
268 fprintf_filtered (stream
, "%s%lu%09lu%09lu",
269 sign
, temp
[2], temp
[1], temp
[0]);
279 print_longest (stream
, format
, use_local
, val_long
)
285 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
286 if (sizeof (long) < sizeof (LONGEST
))
292 /* Print a signed value, that doesn't fit in a long */
293 if ((long) val_long
!= val_long
)
296 print_decimal (stream
, "-", use_local
, -val_long
);
298 print_decimal (stream
, "", use_local
, val_long
);
305 /* Print an unsigned value, that doesn't fit in a long */
306 if ((unsigned long) val_long
!= (ULONGEST
) val_long
)
308 print_decimal (stream
, "", use_local
, val_long
);
319 /* Print as unsigned value, must fit completely in unsigned long */
321 unsigned long temp
= val_long
;
322 if (temp
!= val_long
)
324 /* Urk, can't represent value in long so print in hex.
325 Do shift in two operations so that if sizeof (long)
326 == sizeof (LONGEST) we can avoid warnings from
327 picky compilers about shifts >= the size of the
329 unsigned long vbot
= (unsigned long) val_long
;
330 LONGEST temp
= (val_long
>> (sizeof (long) * HOST_CHAR_BIT
- 1));
331 unsigned long vtop
= temp
>> 1;
332 fprintf_filtered (stream
, "0x%lx%08lx", vtop
, vbot
);
341 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
345 fprintf_filtered (stream
,
346 use_local
? local_decimal_format_custom ("ll")
351 fprintf_filtered (stream
, "%llu", val_long
);
354 fprintf_filtered (stream
,
355 use_local
? local_hex_format_custom ("ll")
360 fprintf_filtered (stream
,
361 use_local
? local_octal_format_custom ("ll")
366 fprintf_filtered (stream
, local_hex_format_custom ("02ll"), val_long
);
369 fprintf_filtered (stream
, local_hex_format_custom ("04ll"), val_long
);
372 fprintf_filtered (stream
, local_hex_format_custom ("08ll"), val_long
);
375 fprintf_filtered (stream
, local_hex_format_custom ("016ll"), val_long
);
380 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
381 /* In the following it is important to coerce (val_long) to a long. It does
382 nothing if !LONG_LONG, but it will chop off the top half (which we know
383 we can ignore) if the host supports long longs. */
388 fprintf_filtered (stream
,
389 use_local
? local_decimal_format_custom ("l")
394 fprintf_filtered (stream
, "%lu", (unsigned long) val_long
);
397 fprintf_filtered (stream
,
398 use_local
? local_hex_format_custom ("l")
400 (unsigned long) val_long
);
403 fprintf_filtered (stream
,
404 use_local
? local_octal_format_custom ("l")
406 (unsigned long) val_long
);
409 fprintf_filtered (stream
, local_hex_format_custom ("02l"),
410 (unsigned long) val_long
);
413 fprintf_filtered (stream
, local_hex_format_custom ("04l"),
414 (unsigned long) val_long
);
417 fprintf_filtered (stream
, local_hex_format_custom ("08l"),
418 (unsigned long) val_long
);
421 fprintf_filtered (stream
, local_hex_format_custom ("016l"),
422 (unsigned long) val_long
);
427 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
432 strcat_longest (format
, use_local
, val_long
, buf
, buflen
)
437 int buflen
; /* ignored, for now */
439 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
442 vtop
= val_long
>> (sizeof (long) * HOST_CHAR_BIT
);
443 vbot
= (long) val_long
;
445 if ((format
== 'd' && (val_long
< INT_MIN
|| val_long
> INT_MAX
))
446 || ((format
== 'u' || format
== 'x') && (unsigned long long) val_long
> UINT_MAX
))
448 sprintf (buf
, "0x%lx%08lx", vtop
, vbot
);
453 #ifdef PRINTF_HAS_LONG_LONG
458 (use_local
? local_decimal_format_custom ("ll") : "%lld"),
462 sprintf (buf
, "%llu", val_long
);
466 (use_local
? local_hex_format_custom ("ll") : "%llx"),
472 (use_local
? local_octal_format_custom ("ll") : "%llo"),
476 sprintf (buf
, local_hex_format_custom ("02ll"), val_long
);
479 sprintf (buf
, local_hex_format_custom ("04ll"), val_long
);
482 sprintf (buf
, local_hex_format_custom ("08ll"), val_long
);
485 sprintf (buf
, local_hex_format_custom ("016ll"), val_long
);
490 #else /* !PRINTF_HAS_LONG_LONG */
491 /* In the following it is important to coerce (val_long) to a long. It does
492 nothing if !LONG_LONG, but it will chop off the top half (which we know
493 we can ignore) if the host supports long longs. */
498 sprintf (buf
, (use_local
? local_decimal_format_custom ("l") : "%ld"),
502 sprintf (buf
, "%lu", ((unsigned long) val_long
));
505 sprintf (buf
, (use_local
? local_hex_format_custom ("l") : "%lx"),
509 sprintf (buf
, (use_local
? local_octal_format_custom ("l") : "%lo"),
513 sprintf (buf
, local_hex_format_custom ("02l"),
517 sprintf (buf
, local_hex_format_custom ("04l"),
521 sprintf (buf
, local_hex_format_custom ("08l"),
525 sprintf (buf
, local_hex_format_custom ("016l"),
532 #endif /* !PRINTF_HAS_LONG_LONG */
536 /* This used to be a macro, but I don't think it is called often enough
537 to merit such treatment. */
538 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
539 arguments to a function, number in a value history, register number, etc.)
540 where the value must not be larger than can fit in an int. */
546 /* Let the compiler do the work */
547 int rtnval
= (int) arg
;
549 /* Check for overflows or underflows */
550 if (sizeof (LONGEST
) > sizeof (int))
554 error ("Value out of range.");
560 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
564 print_floating (valaddr
, type
, stream
)
571 unsigned len
= TYPE_LENGTH (type
);
573 #if defined (IEEE_FLOAT)
575 /* Check for NaN's. Note that this code does not depend on us being
576 on an IEEE conforming system. It only depends on the target
577 machine using IEEE representation. This means (a)
578 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
579 be defined for systems like the 68881, which uses IEEE
580 representation, but is not IEEE conforming. */
583 unsigned long low
, high
;
584 /* Is the sign bit 0? */
586 /* Is it is a NaN (i.e. the exponent is all ones and
587 the fraction is nonzero)? */
590 /* For lint, initialize these two variables to suppress warning: */
591 low
= high
= nonnegative
= 0;
594 /* It's single precision. */
595 /* Assume that floating point byte order is the same as
596 integer byte order. */
597 low
= extract_unsigned_integer (valaddr
, 4);
598 nonnegative
= ((low
& 0x80000000) == 0);
599 is_nan
= ((((low
>> 23) & 0xFF) == 0xFF)
600 && 0 != (low
& 0x7FFFFF));
606 /* It's double precision. Get the high and low words. */
608 /* Assume that floating point byte order is the same as
609 integer byte order. */
610 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
612 low
= extract_unsigned_integer (valaddr
+ 4, 4);
613 high
= extract_unsigned_integer (valaddr
, 4);
617 low
= extract_unsigned_integer (valaddr
, 4);
618 high
= extract_unsigned_integer (valaddr
+ 4, 4);
620 nonnegative
= ((high
& 0x80000000) == 0);
621 is_nan
= (((high
>> 20) & 0x7ff) == 0x7ff
622 && !((((high
& 0xfffff) == 0)) && (low
== 0)));
626 /* Extended. We can't detect NaNs for extendeds yet. Also note
627 that currently extendeds get nuked to double in
628 REGISTER_CONVERTIBLE. */
633 /* The meaning of the sign and fraction is not defined by IEEE.
634 But the user might know what they mean. For example, they
635 (in an implementation-defined manner) distinguish between
636 signaling and quiet NaN's. */
638 fprintf_filtered (stream
, "-NaN(0x%lx%.8lx)" + nonnegative
,
641 fprintf_filtered (stream
, "-NaN(0x%lx)" + nonnegative
, low
);
645 #endif /* IEEE_FLOAT. */
647 doub
= unpack_double (type
, valaddr
, &inv
);
650 fprintf_filtered (stream
, "<invalid float value>");
654 if (len
< sizeof (double))
655 fprintf_filtered (stream
, "%.9g", (double) doub
);
656 else if (len
== sizeof (double))
657 fprintf_filtered (stream
, "%.17g", (double) doub
);
659 #ifdef PRINTF_HAS_LONG_DOUBLE
660 fprintf_filtered (stream
, "%.35Lg", doub
);
662 /* This at least wins with values that are representable as doubles */
663 fprintf_filtered (stream
, "%.17g", (double) doub
);
668 print_binary_chars (stream
, valaddr
, len
)
670 unsigned char *valaddr
;
674 #define BITS_IN_BYTES 8
680 /* Declared "int" so it will be signed.
681 * This ensures that right shift will shift in zeros.
683 const int mask
= 0x080;
685 /* FIXME: We should be not printing leading zeroes in most cases. */
687 fprintf_filtered (stream
, local_binary_format_prefix ());
688 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
694 /* Every byte has 8 binary characters; peel off
695 * and print from the MSB end.
697 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
699 if (*p
& (mask
>> i
))
704 fprintf_filtered (stream
, "%1d", b
);
710 for (p
= valaddr
+ len
- 1;
714 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
716 if (*p
& (mask
>> i
))
721 fprintf_filtered (stream
, "%1d", b
);
725 fprintf_filtered (stream
, local_binary_format_suffix ());
728 /* VALADDR points to an integer of LEN bytes.
729 * Print it in octal on stream or format it in buf.
732 print_octal_chars (stream
, valaddr
, len
)
734 unsigned char *valaddr
;
738 unsigned char octa1
, octa2
, octa3
, carry
;
741 /* FIXME: We should be not printing leading zeroes in most cases. */
744 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
745 * the extra bits, which cycle every three bytes:
749 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
751 * Octal side: 0 1 carry 3 4 carry ...
753 * Cycle number: 0 1 2
755 * But of course we are printing from the high side, so we have to
756 * figure out where in the cycle we are so that we end up with no
757 * left over bits at the end.
759 #define BITS_IN_OCTAL 3
760 #define HIGH_ZERO 0340
761 #define LOW_ZERO 0016
762 #define CARRY_ZERO 0003
763 #define HIGH_ONE 0200
766 #define CARRY_ONE 0001
767 #define HIGH_TWO 0300
771 /* For 32 we start in cycle 2, with two bits and one bit carry;
772 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
774 cycle
= (len
* BITS_IN_BYTES
) % BITS_IN_OCTAL
;
777 fprintf_filtered (stream
, local_octal_format_prefix ());
778 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
787 /* No carry in, carry out two bits.
789 octa1
= (HIGH_ZERO
& *p
) >> 5;
790 octa2
= (LOW_ZERO
& *p
) >> 2;
791 carry
= (CARRY_ZERO
& *p
);
792 fprintf_filtered (stream
, "%o", octa1
);
793 fprintf_filtered (stream
, "%o", octa2
);
797 /* Carry in two bits, carry out one bit.
799 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
800 octa2
= (MID_ONE
& *p
) >> 4;
801 octa3
= (LOW_ONE
& *p
) >> 1;
802 carry
= (CARRY_ONE
& *p
);
803 fprintf_filtered (stream
, "%o", octa1
);
804 fprintf_filtered (stream
, "%o", octa2
);
805 fprintf_filtered (stream
, "%o", octa3
);
809 /* Carry in one bit, no carry out.
811 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
812 octa2
= (MID_TWO
& *p
) >> 3;
813 octa3
= (LOW_TWO
& *p
);
815 fprintf_filtered (stream
, "%o", octa1
);
816 fprintf_filtered (stream
, "%o", octa2
);
817 fprintf_filtered (stream
, "%o", octa3
);
821 error ("Internal error in octal conversion;");
825 cycle
= cycle
% BITS_IN_OCTAL
;
830 for (p
= valaddr
+ len
- 1;
837 /* Carry out, no carry in */
838 octa1
= (HIGH_ZERO
& *p
) >> 5;
839 octa2
= (LOW_ZERO
& *p
) >> 2;
840 carry
= (CARRY_ZERO
& *p
);
841 fprintf_filtered (stream
, "%o", octa1
);
842 fprintf_filtered (stream
, "%o", octa2
);
846 /* Carry in, carry out */
847 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
848 octa2
= (MID_ONE
& *p
) >> 4;
849 octa3
= (LOW_ONE
& *p
) >> 1;
850 carry
= (CARRY_ONE
& *p
);
851 fprintf_filtered (stream
, "%o", octa1
);
852 fprintf_filtered (stream
, "%o", octa2
);
853 fprintf_filtered (stream
, "%o", octa3
);
857 /* Carry in, no carry out */
858 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
859 octa2
= (MID_TWO
& *p
) >> 3;
860 octa3
= (LOW_TWO
& *p
);
862 fprintf_filtered (stream
, "%o", octa1
);
863 fprintf_filtered (stream
, "%o", octa2
);
864 fprintf_filtered (stream
, "%o", octa3
);
868 error ("Internal error in octal conversion;");
872 cycle
= cycle
% BITS_IN_OCTAL
;
876 fprintf_filtered (stream
, local_octal_format_suffix ());
879 /* VALADDR points to an integer of LEN bytes.
880 * Print it in decimal on stream or format it in buf.
883 print_decimal_chars (stream
, valaddr
, len
)
885 unsigned char *valaddr
;
889 #define TWO_TO_FOURTH 16
890 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
891 #define CARRY_LEFT( x ) ((x) % TEN)
892 #define SHIFT( x ) ((x) << 4)
894 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
896 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
898 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
899 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
900 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
903 unsigned char *digits
;
906 int i
, j
, decimal_digits
;
910 /* Base-ten number is less than twice as many digits
911 * as the base 16 number, which is 2 digits per byte.
913 decimal_len
= len
* 2 * 2;
914 digits
= (unsigned char *) malloc (decimal_len
);
916 error ("Can't allocate memory for conversion to decimal.");
918 for (i
= 0; i
< decimal_len
; i
++)
923 fprintf_filtered (stream
, local_decimal_format_prefix ());
925 /* Ok, we have an unknown number of bytes of data to be printed in
928 * Given a hex number (in nibbles) as XYZ, we start by taking X and
929 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
930 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
932 * The trick is that "digits" holds a base-10 number, but sometimes
933 * the individual digits are > 10.
935 * Outer loop is per nibble (hex digit) of input, from MSD end to
938 decimal_digits
= 0; /* Number of decimal digits so far */
944 * Multiply current base-ten number by 16 in place.
945 * Each digit was between 0 and 9, now is between
948 for (j
= 0; j
< decimal_digits
; j
++)
950 digits
[j
] = SHIFT (digits
[j
]);
953 /* Take the next nibble off the input and add it to what
954 * we've got in the LSB position. Bottom 'digit' is now
957 * "flip" is used to run this loop twice for each byte.
963 digits
[0] += HIGH_NIBBLE (*p
);
968 /* Take low nibble and bump our pointer "p".
970 digits
[0] += LOW_NIBBLE (*p
);
975 /* Re-decimalize. We have to do this often enough
976 * that we don't overflow, but once per nibble is
977 * overkill. Easier this way, though. Note that the
978 * carry is often larger than 10 (e.g. max initial
979 * carry out of lowest nibble is 15, could bubble all
980 * the way up greater than 10). So we have to do
981 * the carrying beyond the last current digit.
984 for (j
= 0; j
< decimal_len
- 1; j
++)
988 /* "/" won't handle an unsigned char with
989 * a value that if signed would be negative.
990 * So extend to longword int via "dummy".
993 carry
= CARRY_OUT (dummy
);
994 digits
[j
] = CARRY_LEFT (dummy
);
996 if (j
>= decimal_digits
&& carry
== 0)
999 * All higher digits are 0 and we
1000 * no longer have a carry.
1002 * Note: "j" is 0-based, "decimal_digits" is
1005 decimal_digits
= j
+ 1;
1011 /* Ok, now "digits" is the decimal representation, with
1012 * the "decimal_digits" actual digits. Print!
1014 for (i
= decimal_digits
- 1; i
>= 0; i
--)
1016 fprintf_filtered (stream
, "%1d", digits
[i
]);
1020 fprintf_filtered (stream
, local_decimal_format_suffix ());
1023 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1026 print_hex_chars (stream
, valaddr
, len
)
1028 unsigned char *valaddr
;
1033 /* FIXME: We should be not printing leading zeroes in most cases. */
1035 fprintf_filtered (stream
, local_hex_format_prefix ());
1036 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1042 fprintf_filtered (stream
, "%02x", *p
);
1047 for (p
= valaddr
+ len
- 1;
1051 fprintf_filtered (stream
, "%02x", *p
);
1054 fprintf_filtered (stream
, local_hex_format_suffix ());
1057 /* Called by various <lang>_val_print routines to print elements of an
1058 array in the form "<elem1>, <elem2>, <elem3>, ...".
1060 (FIXME?) Assumes array element separator is a comma, which is correct
1061 for all languages currently handled.
1062 (FIXME?) Some languages have a notation for repeated array elements,
1063 perhaps we should try to use that notation when appropriate.
1067 val_print_array_elements (type
, valaddr
, address
, stream
, format
, deref_ref
,
1076 enum val_prettyprint pretty
;
1079 unsigned int things_printed
= 0;
1081 struct type
*elttype
;
1083 /* Position of the array element we are examining to see
1084 whether it is repeated. */
1086 /* Number of repetitions we have detected so far. */
1089 elttype
= TYPE_TARGET_TYPE (type
);
1090 eltlen
= TYPE_LENGTH (check_typedef (elttype
));
1091 len
= TYPE_LENGTH (type
) / eltlen
;
1093 annotate_array_section_begin (i
, elttype
);
1095 for (; i
< len
&& things_printed
< print_max
; i
++)
1099 if (prettyprint_arrays
)
1101 fprintf_filtered (stream
, ",\n");
1102 print_spaces_filtered (2 + 2 * recurse
, stream
);
1106 fprintf_filtered (stream
, ", ");
1109 wrap_here (n_spaces (2 + 2 * recurse
));
1113 while ((rep1
< len
) &&
1114 !memcmp (valaddr
+ i
* eltlen
, valaddr
+ rep1
* eltlen
, eltlen
))
1120 if (reps
> repeat_count_threshold
)
1122 val_print (elttype
, valaddr
+ i
* eltlen
, 0, 0, stream
, format
,
1123 deref_ref
, recurse
+ 1, pretty
);
1124 annotate_elt_rep (reps
);
1125 fprintf_filtered (stream
, " <repeats %u times>", reps
);
1126 annotate_elt_rep_end ();
1129 things_printed
+= repeat_count_threshold
;
1133 val_print (elttype
, valaddr
+ i
* eltlen
, 0, 0, stream
, format
,
1134 deref_ref
, recurse
+ 1, pretty
);
1139 annotate_array_section_end ();
1142 fprintf_filtered (stream
, "...");
1146 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1147 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1148 stops at the first null byte, otherwise printing proceeds (including null
1149 bytes) until either print_max or LEN characters have been printed,
1150 whichever is smaller. */
1152 /* FIXME: Use target_read_string. */
1155 val_print_string (addr
, len
, width
, stream
)
1161 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
1162 int errcode
; /* Errno returned from bad reads. */
1163 unsigned int fetchlimit
; /* Maximum number of chars to print. */
1164 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
1165 unsigned int chunksize
; /* Size of each fetch, in chars. */
1166 char *buffer
= NULL
; /* Dynamically growable fetch buffer. */
1167 char *bufptr
; /* Pointer to next available byte in buffer. */
1168 char *limit
; /* First location past end of fetch buffer. */
1169 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
1170 int found_nul
; /* Non-zero if we found the nul char */
1172 /* First we need to figure out the limit on the number of characters we are
1173 going to attempt to fetch and print. This is actually pretty simple. If
1174 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1175 LEN is -1, then the limit is print_max. This is true regardless of
1176 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1177 because finding the null byte (or available memory) is what actually
1178 limits the fetch. */
1180 fetchlimit
= (len
== -1 ? print_max
: min (len
, print_max
));
1182 /* Now decide how large of chunks to try to read in one operation. This
1183 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1184 so we might as well read them all in one operation. If LEN is -1, we
1185 are looking for a null terminator to end the fetching, so we might as
1186 well read in blocks that are large enough to be efficient, but not so
1187 large as to be slow if fetchlimit happens to be large. So we choose the
1188 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1189 200 is way too big for remote debugging over a serial line. */
1191 chunksize
= (len
== -1 ? min (8, fetchlimit
) : fetchlimit
);
1193 /* Loop until we either have all the characters to print, or we encounter
1194 some error, such as bumping into the end of the address space. */
1197 old_chain
= make_cleanup (null_cleanup
, 0);
1201 buffer
= (char *) xmalloc (len
* width
);
1203 old_chain
= make_cleanup (free
, buffer
);
1205 nfetch
= target_read_memory_partial (addr
, bufptr
, len
* width
, &errcode
)
1207 addr
+= nfetch
* width
;
1208 bufptr
+= nfetch
* width
;
1212 unsigned long bufsize
= 0;
1216 nfetch
= min (chunksize
, fetchlimit
- bufsize
);
1219 buffer
= (char *) xmalloc (nfetch
* width
);
1222 discard_cleanups (old_chain
);
1223 buffer
= (char *) xrealloc (buffer
, (nfetch
+ bufsize
) * width
);
1226 old_chain
= make_cleanup (free
, buffer
);
1227 bufptr
= buffer
+ bufsize
* width
;
1230 /* Read as much as we can. */
1231 nfetch
= target_read_memory_partial (addr
, bufptr
, nfetch
* width
, &errcode
)
1234 /* Scan this chunk for the null byte that terminates the string
1235 to print. If found, we don't need to fetch any more. Note
1236 that bufptr is explicitly left pointing at the next character
1237 after the null byte, or at the next character after the end of
1240 limit
= bufptr
+ nfetch
* width
;
1241 while (bufptr
< limit
)
1245 c
= extract_unsigned_integer (bufptr
, width
);
1250 /* We don't care about any error which happened after
1251 the NULL terminator. */
1258 while (errcode
== 0 /* no error */
1259 && bufptr
- buffer
< fetchlimit
* width
/* no overrun */
1260 && !found_nul
); /* haven't found nul yet */
1263 { /* length of string is really 0! */
1264 buffer
= bufptr
= NULL
;
1268 /* bufptr and addr now point immediately beyond the last byte which we
1269 consider part of the string (including a '\0' which ends the string). */
1271 /* We now have either successfully filled the buffer to fetchlimit, or
1272 terminated early due to an error or finding a null char when LEN is -1. */
1274 if (len
== -1 && !found_nul
)
1278 /* We didn't find a null terminator we were looking for. Attempt
1279 to peek at the next character. If not successful, or it is not
1280 a null byte, then force ellipsis to be printed. */
1282 peekbuf
= (char *) alloca (width
);
1284 if (target_read_memory (addr
, peekbuf
, width
) == 0
1285 && extract_unsigned_integer (peekbuf
, width
) != 0)
1288 else if ((len
>= 0 && errcode
!= 0) || (len
> (bufptr
- buffer
) / width
))
1290 /* Getting an error when we have a requested length, or fetching less
1291 than the number of characters actually requested, always make us
1298 /* If we get an error before fetching anything, don't print a string.
1299 But if we fetch something and then get an error, print the string
1300 and then the error message. */
1301 if (errcode
== 0 || bufptr
> buffer
)
1305 fputs_filtered (" ", stream
);
1307 LA_PRINT_STRING (stream
, buffer
, (bufptr
- buffer
) / width
, width
, force_ellipsis
);
1314 fprintf_filtered (stream
, " <Address ");
1315 print_address_numeric (addr
, 1, stream
);
1316 fprintf_filtered (stream
, " out of bounds>");
1320 fprintf_filtered (stream
, " <Error reading address ");
1321 print_address_numeric (addr
, 1, stream
);
1322 fprintf_filtered (stream
, ": %s>", safe_strerror (errcode
));
1326 do_cleanups (old_chain
);
1327 return ((bufptr
- buffer
) / width
);
1331 /* Validate an input or output radix setting, and make sure the user
1332 knows what they really did here. Radix setting is confusing, e.g.
1333 setting the input radix to "10" never changes it! */
1337 set_input_radix (args
, from_tty
, c
)
1340 struct cmd_list_element
*c
;
1342 set_input_radix_1 (from_tty
, *(unsigned *) c
->var
);
1347 set_input_radix_1 (from_tty
, radix
)
1351 /* We don't currently disallow any input radix except 0 or 1, which don't
1352 make any mathematical sense. In theory, we can deal with any input
1353 radix greater than 1, even if we don't have unique digits for every
1354 value from 0 to radix-1, but in practice we lose on large radix values.
1355 We should either fix the lossage or restrict the radix range more.
1360 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1363 input_radix
= radix
;
1366 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1367 radix
, radix
, radix
);
1373 set_output_radix (args
, from_tty
, c
)
1376 struct cmd_list_element
*c
;
1378 set_output_radix_1 (from_tty
, *(unsigned *) c
->var
);
1382 set_output_radix_1 (from_tty
, radix
)
1386 /* Validate the radix and disallow ones that we aren't prepared to
1387 handle correctly, leaving the radix unchanged. */
1391 output_format
= 'x'; /* hex */
1394 output_format
= 0; /* decimal */
1397 output_format
= 'o'; /* octal */
1400 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1403 output_radix
= radix
;
1406 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1407 radix
, radix
, radix
);
1411 /* Set both the input and output radix at once. Try to set the output radix
1412 first, since it has the most restrictive range. An radix that is valid as
1413 an output radix is also valid as an input radix.
1415 It may be useful to have an unusual input radix. If the user wishes to
1416 set an input radix that is not valid as an output radix, he needs to use
1417 the 'set input-radix' command. */
1420 set_radix (arg
, from_tty
)
1426 radix
= (arg
== NULL
) ? 10 : parse_and_eval_address (arg
);
1427 set_output_radix_1 (0, radix
);
1428 set_input_radix_1 (0, radix
);
1431 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1432 radix
, radix
, radix
);
1436 /* Show both the input and output radices. */
1440 show_radix (arg
, from_tty
)
1446 if (input_radix
== output_radix
)
1448 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1449 input_radix
, input_radix
, input_radix
);
1453 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1454 input_radix
, input_radix
, input_radix
);
1455 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1456 output_radix
, output_radix
, output_radix
);
1464 set_print (arg
, from_tty
)
1469 "\"set print\" must be followed by the name of a print subcommand.\n");
1470 help_list (setprintlist
, "set print ", -1, gdb_stdout
);
1475 show_print (args
, from_tty
)
1479 cmd_show_list (showprintlist
, from_tty
, "");
1483 _initialize_valprint ()
1485 struct cmd_list_element
*c
;
1487 add_prefix_cmd ("print", no_class
, set_print
,
1488 "Generic command for setting how things print.",
1489 &setprintlist
, "set print ", 0, &setlist
);
1490 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
1491 /* prefer set print to set prompt */
1492 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
1494 add_prefix_cmd ("print", no_class
, show_print
,
1495 "Generic command for showing print settings.",
1496 &showprintlist
, "show print ", 0, &showlist
);
1497 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
1498 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
1501 (add_set_cmd ("elements", no_class
, var_uinteger
, (char *) &print_max
,
1502 "Set limit on string chars or array elements to print.\n\
1503 \"set print elements 0\" causes there to be no limit.",
1508 (add_set_cmd ("null-stop", no_class
, var_boolean
,
1509 (char *) &stop_print_at_null
,
1510 "Set printing of char arrays to stop at first null char.",
1515 (add_set_cmd ("repeats", no_class
, var_uinteger
,
1516 (char *) &repeat_count_threshold
,
1517 "Set threshold for repeated print elements.\n\
1518 \"set print repeats 0\" causes all elements to be individually printed.",
1523 (add_set_cmd ("pretty", class_support
, var_boolean
,
1524 (char *) &prettyprint_structs
,
1525 "Set prettyprinting of structures.",
1530 (add_set_cmd ("union", class_support
, var_boolean
, (char *) &unionprint
,
1531 "Set printing of unions interior to structures.",
1536 (add_set_cmd ("array", class_support
, var_boolean
,
1537 (char *) &prettyprint_arrays
,
1538 "Set prettyprinting of arrays.",
1543 (add_set_cmd ("address", class_support
, var_boolean
, (char *) &addressprint
,
1544 "Set printing of addresses.",
1548 c
= add_set_cmd ("input-radix", class_support
, var_uinteger
,
1549 (char *) &input_radix
,
1550 "Set default input radix for entering numbers.",
1552 add_show_from_set (c
, &showlist
);
1553 c
->function
.sfunc
= set_input_radix
;
1555 c
= add_set_cmd ("output-radix", class_support
, var_uinteger
,
1556 (char *) &output_radix
,
1557 "Set default output radix for printing of values.",
1559 add_show_from_set (c
, &showlist
);
1560 c
->function
.sfunc
= set_output_radix
;
1562 /* The "set radix" and "show radix" commands are special in that they are
1563 like normal set and show commands but allow two normally independent
1564 variables to be either set or shown with a single command. So the
1565 usual add_set_cmd() and add_show_from_set() commands aren't really
1567 add_cmd ("radix", class_support
, set_radix
,
1568 "Set default input and output number radices.\n\
1569 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1570 Without an argument, sets both radices back to the default value of 10.",
1572 add_cmd ("radix", class_support
, show_radix
,
1573 "Show the default input and output number radices.\n\
1574 Use 'show input-radix' or 'show output-radix' to independently show each.",
1577 /* Give people the defaults which they are used to. */
1578 prettyprint_structs
= 0;
1579 prettyprint_arrays
= 0;
1582 print_max
= PRINT_MAX_DEFAULT
;
This page took 0.06178 seconds and 5 git commands to generate.