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"
39 /* Prototypes for local functions */
41 static int partial_memory_read (CORE_ADDR memaddr
, char *myaddr
,
42 int len
, int *errnoptr
);
44 static void print_hex_chars (struct ui_file
*, unsigned char *,
47 static void show_print (char *, int);
49 static void set_print (char *, int);
51 static void set_radix (char *, int);
53 static void show_radix (char *, int);
55 static void set_input_radix (char *, int, struct cmd_list_element
*);
57 static void set_input_radix_1 (int, unsigned);
59 static void set_output_radix (char *, int, struct cmd_list_element
*);
61 static void set_output_radix_1 (int, unsigned);
63 void _initialize_valprint (void);
65 /* Maximum number of chars to print for a string pointer value or vector
66 contents, or UINT_MAX for no limit. Note that "set print elements 0"
67 stores UINT_MAX in print_max, which displays in a show command as
70 unsigned int print_max
;
71 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
73 /* Default input and output radixes, and output format letter. */
75 unsigned input_radix
= 10;
76 unsigned output_radix
= 10;
77 int output_format
= 0;
79 /* Print repeat counts if there are more than this many repetitions of an
80 element in an array. Referenced by the low level language dependent
83 unsigned int repeat_count_threshold
= 10;
85 /* If nonzero, stops printing of char arrays at first null. */
87 int stop_print_at_null
;
89 /* Controls pretty printing of structures. */
91 int prettyprint_structs
;
93 /* Controls pretty printing of arrays. */
95 int prettyprint_arrays
;
97 /* If nonzero, causes unions inside structures or other unions to be
100 int unionprint
; /* Controls printing of nested unions. */
102 /* If nonzero, causes machine addresses to be printed in certain contexts. */
104 int addressprint
; /* Controls printing of machine addresses */
107 /* Print data of type TYPE located at VALADDR (within GDB), which came from
108 the inferior at address ADDRESS, onto stdio stream STREAM according to
109 FORMAT (a letter, or 0 for natural format using TYPE).
111 If DEREF_REF is nonzero, then dereference references, otherwise just print
114 The PRETTY parameter controls prettyprinting.
116 If the data are a string pointer, returns the number of string characters
119 FIXME: The data at VALADDR is in target byte order. If gdb is ever
120 enhanced to be able to debug more than the single target it was compiled
121 for (specific CPU type and thus specific target byte ordering), then
122 either the print routines are going to have to take this into account,
123 or the data is going to have to be passed into here already converted
124 to the host byte ordering, whichever is more convenient. */
128 val_print (struct type
*type
, char *valaddr
, int embedded_offset
,
129 CORE_ADDR address
, struct ui_file
*stream
, int format
, int deref_ref
,
130 int recurse
, enum val_prettyprint pretty
)
132 struct type
*real_type
= check_typedef (type
);
133 if (pretty
== Val_pretty_default
)
135 pretty
= prettyprint_structs
? Val_prettyprint
: Val_no_prettyprint
;
140 /* Ensure that the type is complete and not just a stub. If the type is
141 only a stub and we can't find and substitute its complete type, then
142 print appropriate string and return. */
144 if (TYPE_FLAGS (real_type
) & TYPE_FLAG_STUB
)
146 fprintf_filtered (stream
, "<incomplete type>");
151 return (LA_VAL_PRINT (type
, valaddr
, embedded_offset
, address
,
152 stream
, format
, deref_ref
, recurse
, pretty
));
155 /* Print the value VAL in C-ish syntax on stream STREAM.
156 FORMAT is a format-letter, or 0 for print in natural format of data type.
157 If the object printed is a string pointer, returns
158 the number of string bytes printed. */
161 value_print (value_ptr val
, struct ui_file
*stream
, int format
,
162 enum val_prettyprint pretty
)
166 printf_filtered ("<address of value unknown>");
169 if (VALUE_OPTIMIZED_OUT (val
))
171 printf_filtered ("<value optimized out>");
174 return LA_VALUE_PRINT (val
, stream
, format
, pretty
);
177 /* Called by various <lang>_val_print routines to print
178 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
179 value. STREAM is where to print the value. */
182 val_print_type_code_int (struct type
*type
, char *valaddr
,
183 struct ui_file
*stream
)
185 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
189 if (TYPE_UNSIGNED (type
)
190 && extract_long_unsigned_integer (valaddr
, TYPE_LENGTH (type
),
193 print_longest (stream
, 'u', 0, val
);
197 /* Signed, or we couldn't turn an unsigned value into a
198 LONGEST. For signed values, one could assume two's
199 complement (a reasonable assumption, I think) and do
201 print_hex_chars (stream
, (unsigned char *) valaddr
,
207 #ifdef PRINT_TYPELESS_INTEGER
208 PRINT_TYPELESS_INTEGER (stream
, type
, unpack_long (type
, valaddr
));
210 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0,
211 unpack_long (type
, valaddr
));
216 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
217 The raison d'etre of this function is to consolidate printing of
218 LONG_LONG's into this one function. Some platforms have long longs but
219 don't have a printf() that supports "ll" in the format string. We handle
220 these by seeing if the number is representable as either a signed or
221 unsigned long, depending upon what format is desired, and if not we just
222 bail out and print the number in hex.
224 The format chars b,h,w,g are from print_scalar_formatted(). If USE_LOCAL,
225 format it according to the current language (this should be used for most
226 integers which GDB prints, the exception is things like protocols where
227 the format of the integer is a protocol thing, not a user-visible thing).
230 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
231 static void print_decimal (struct ui_file
* stream
, char *sign
,
232 int use_local
, ULONGEST val_ulong
);
234 print_decimal (struct ui_file
*stream
, char *sign
, int use_local
,
237 unsigned long temp
[3];
241 temp
[i
] = val_ulong
% (1000 * 1000 * 1000);
242 val_ulong
/= (1000 * 1000 * 1000);
245 while (val_ulong
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
249 fprintf_filtered (stream
, "%s%lu",
253 fprintf_filtered (stream
, "%s%lu%09lu",
254 sign
, temp
[1], temp
[0]);
257 fprintf_filtered (stream
, "%s%lu%09lu%09lu",
258 sign
, temp
[2], temp
[1], temp
[0]);
261 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
268 print_longest (struct ui_file
*stream
, int format
, int use_local
,
271 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
272 if (sizeof (long) < sizeof (LONGEST
))
278 /* Print a signed value, that doesn't fit in a long */
279 if ((long) val_long
!= val_long
)
282 print_decimal (stream
, "-", use_local
, -val_long
);
284 print_decimal (stream
, "", use_local
, val_long
);
291 /* Print an unsigned value, that doesn't fit in a long */
292 if ((unsigned long) val_long
!= (ULONGEST
) val_long
)
294 print_decimal (stream
, "", use_local
, val_long
);
305 /* Print as unsigned value, must fit completely in unsigned long */
307 unsigned long temp
= val_long
;
308 if (temp
!= val_long
)
310 /* Urk, can't represent value in long so print in hex.
311 Do shift in two operations so that if sizeof (long)
312 == sizeof (LONGEST) we can avoid warnings from
313 picky compilers about shifts >= the size of the
315 unsigned long vbot
= (unsigned long) val_long
;
316 LONGEST temp
= (val_long
>> (sizeof (long) * HOST_CHAR_BIT
- 1));
317 unsigned long vtop
= temp
>> 1;
318 fprintf_filtered (stream
, "0x%lx%08lx", vtop
, vbot
);
327 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
331 fprintf_filtered (stream
,
332 use_local
? local_decimal_format_custom ("ll")
337 fprintf_filtered (stream
, "%llu", val_long
);
340 fprintf_filtered (stream
,
341 use_local
? local_hex_format_custom ("ll")
346 fprintf_filtered (stream
,
347 use_local
? local_octal_format_custom ("ll")
352 fprintf_filtered (stream
, local_hex_format_custom ("02ll"), val_long
);
355 fprintf_filtered (stream
, local_hex_format_custom ("04ll"), val_long
);
358 fprintf_filtered (stream
, local_hex_format_custom ("08ll"), val_long
);
361 fprintf_filtered (stream
, local_hex_format_custom ("016ll"), val_long
);
364 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
366 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
367 /* In the following it is important to coerce (val_long) to a long. It does
368 nothing if !LONG_LONG, but it will chop off the top half (which we know
369 we can ignore) if the host supports long longs. */
374 fprintf_filtered (stream
,
375 use_local
? local_decimal_format_custom ("l")
380 fprintf_filtered (stream
, "%lu", (unsigned long) val_long
);
383 fprintf_filtered (stream
,
384 use_local
? local_hex_format_custom ("l")
386 (unsigned long) val_long
);
389 fprintf_filtered (stream
,
390 use_local
? local_octal_format_custom ("l")
392 (unsigned long) val_long
);
395 fprintf_filtered (stream
, local_hex_format_custom ("02l"),
396 (unsigned long) val_long
);
399 fprintf_filtered (stream
, local_hex_format_custom ("04l"),
400 (unsigned long) val_long
);
403 fprintf_filtered (stream
, local_hex_format_custom ("08l"),
404 (unsigned long) val_long
);
407 fprintf_filtered (stream
, local_hex_format_custom ("016l"),
408 (unsigned long) val_long
);
411 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
413 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
418 strcat_longest (int format
, int use_local
, LONGEST val_long
, char *buf
,
421 /* FIXME: Use buflen to avoid buffer overflow. */
422 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
425 vtop
= val_long
>> (sizeof (long) * HOST_CHAR_BIT
);
426 vbot
= (long) val_long
;
428 if ((format
== 'd' && (val_long
< INT_MIN
|| val_long
> INT_MAX
))
429 || ((format
== 'u' || format
== 'x') && (unsigned long long) val_long
> UINT_MAX
))
431 sprintf (buf
, "0x%lx%08lx", vtop
, vbot
);
436 #ifdef PRINTF_HAS_LONG_LONG
441 (use_local
? local_decimal_format_custom ("ll") : "%lld"),
445 sprintf (buf
, "%llu", val_long
);
449 (use_local
? local_hex_format_custom ("ll") : "%llx"),
455 (use_local
? local_octal_format_custom ("ll") : "%llo"),
459 sprintf (buf
, local_hex_format_custom ("02ll"), val_long
);
462 sprintf (buf
, local_hex_format_custom ("04ll"), val_long
);
465 sprintf (buf
, local_hex_format_custom ("08ll"), val_long
);
468 sprintf (buf
, local_hex_format_custom ("016ll"), val_long
);
471 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
473 #else /* !PRINTF_HAS_LONG_LONG */
474 /* In the following it is important to coerce (val_long) to a long. It does
475 nothing if !LONG_LONG, but it will chop off the top half (which we know
476 we can ignore) if the host supports long longs. */
481 sprintf (buf
, (use_local
? local_decimal_format_custom ("l") : "%ld"),
485 sprintf (buf
, "%lu", ((unsigned long) val_long
));
488 sprintf (buf
, (use_local
? local_hex_format_custom ("l") : "%lx"),
492 sprintf (buf
, (use_local
? local_octal_format_custom ("l") : "%lo"),
496 sprintf (buf
, local_hex_format_custom ("02l"),
500 sprintf (buf
, local_hex_format_custom ("04l"),
504 sprintf (buf
, local_hex_format_custom ("08l"),
508 sprintf (buf
, local_hex_format_custom ("016l"),
512 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
515 #endif /* !PRINTF_HAS_LONG_LONG */
519 /* This used to be a macro, but I don't think it is called often enough
520 to merit such treatment. */
521 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
522 arguments to a function, number in a value history, register number, etc.)
523 where the value must not be larger than can fit in an int. */
526 longest_to_int (LONGEST arg
)
528 /* Let the compiler do the work */
529 int rtnval
= (int) arg
;
531 /* Check for overflows or underflows */
532 if (sizeof (LONGEST
) > sizeof (int))
536 error ("Value out of range.");
542 /* Print a floating point value of type TYPE, pointed to in GDB by
543 VALADDR, on STREAM. */
546 print_floating (char *valaddr
, struct type
*type
, struct ui_file
*stream
)
550 const struct floatformat
*fmt
= &floatformat_unknown
;
551 unsigned len
= TYPE_LENGTH (type
);
553 /* FIXME: kettenis/2001-01-20: The check for IEEE_FLOAT is probably
554 still necessary since GDB by default assumes that the target uses
555 the IEEE 754 representation for its floats and doubles. Of
556 course this is all crock and should be cleaned up. */
558 if (len
== TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
&& IEEE_FLOAT
)
559 fmt
= TARGET_FLOAT_FORMAT
;
560 else if (len
== TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
&& IEEE_FLOAT
)
561 fmt
= TARGET_DOUBLE_FORMAT
;
562 else if (len
== TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
)
563 fmt
= TARGET_LONG_DOUBLE_FORMAT
;
565 if (floatformat_is_nan (fmt
, valaddr
))
567 if (floatformat_is_negative (fmt
, valaddr
))
568 fprintf_filtered (stream
, "-");
569 fprintf_filtered (stream
, "nan(");
570 fprintf_filtered (stream
, local_hex_format_prefix ());
571 fprintf_filtered (stream
, floatformat_mantissa (fmt
, valaddr
));
572 fprintf_filtered (stream
, local_hex_format_suffix ());
573 fprintf_filtered (stream
, ")");
577 doub
= unpack_double (type
, valaddr
, &inv
);
580 fprintf_filtered (stream
, "<invalid float value>");
584 /* FIXME: kettenis/2001-01-20: The following code makes too much
585 assumptions about the host and target floating point format. */
587 if (len
< sizeof (double))
588 fprintf_filtered (stream
, "%.9g", (double) doub
);
589 else if (len
== sizeof (double))
590 fprintf_filtered (stream
, "%.17g", (double) doub
);
592 #ifdef PRINTF_HAS_LONG_DOUBLE
593 fprintf_filtered (stream
, "%.35Lg", doub
);
595 /* This at least wins with values that are representable as
597 fprintf_filtered (stream
, "%.17g", (double) doub
);
602 print_binary_chars (struct ui_file
*stream
, unsigned char *valaddr
,
606 #define BITS_IN_BYTES 8
612 /* Declared "int" so it will be signed.
613 * This ensures that right shift will shift in zeros.
615 const int mask
= 0x080;
617 /* FIXME: We should be not printing leading zeroes in most cases. */
619 fprintf_filtered (stream
, local_binary_format_prefix ());
620 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
626 /* Every byte has 8 binary characters; peel off
627 * and print from the MSB end.
629 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
631 if (*p
& (mask
>> i
))
636 fprintf_filtered (stream
, "%1d", b
);
642 for (p
= valaddr
+ len
- 1;
646 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
648 if (*p
& (mask
>> i
))
653 fprintf_filtered (stream
, "%1d", b
);
657 fprintf_filtered (stream
, local_binary_format_suffix ());
660 /* VALADDR points to an integer of LEN bytes.
661 * Print it in octal on stream or format it in buf.
664 print_octal_chars (struct ui_file
*stream
, unsigned char *valaddr
, unsigned len
)
667 unsigned char octa1
, octa2
, octa3
, carry
;
670 /* FIXME: We should be not printing leading zeroes in most cases. */
673 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
674 * the extra bits, which cycle every three bytes:
678 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
680 * Octal side: 0 1 carry 3 4 carry ...
682 * Cycle number: 0 1 2
684 * But of course we are printing from the high side, so we have to
685 * figure out where in the cycle we are so that we end up with no
686 * left over bits at the end.
688 #define BITS_IN_OCTAL 3
689 #define HIGH_ZERO 0340
690 #define LOW_ZERO 0016
691 #define CARRY_ZERO 0003
692 #define HIGH_ONE 0200
695 #define CARRY_ONE 0001
696 #define HIGH_TWO 0300
700 /* For 32 we start in cycle 2, with two bits and one bit carry;
701 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
703 cycle
= (len
* BITS_IN_BYTES
) % BITS_IN_OCTAL
;
706 fprintf_filtered (stream
, local_octal_format_prefix ());
707 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
716 /* No carry in, carry out two bits.
718 octa1
= (HIGH_ZERO
& *p
) >> 5;
719 octa2
= (LOW_ZERO
& *p
) >> 2;
720 carry
= (CARRY_ZERO
& *p
);
721 fprintf_filtered (stream
, "%o", octa1
);
722 fprintf_filtered (stream
, "%o", octa2
);
726 /* Carry in two bits, carry out one bit.
728 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
729 octa2
= (MID_ONE
& *p
) >> 4;
730 octa3
= (LOW_ONE
& *p
) >> 1;
731 carry
= (CARRY_ONE
& *p
);
732 fprintf_filtered (stream
, "%o", octa1
);
733 fprintf_filtered (stream
, "%o", octa2
);
734 fprintf_filtered (stream
, "%o", octa3
);
738 /* Carry in one bit, no carry out.
740 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
741 octa2
= (MID_TWO
& *p
) >> 3;
742 octa3
= (LOW_TWO
& *p
);
744 fprintf_filtered (stream
, "%o", octa1
);
745 fprintf_filtered (stream
, "%o", octa2
);
746 fprintf_filtered (stream
, "%o", octa3
);
750 error ("Internal error in octal conversion;");
754 cycle
= cycle
% BITS_IN_OCTAL
;
759 for (p
= valaddr
+ len
- 1;
766 /* Carry out, no carry in */
767 octa1
= (HIGH_ZERO
& *p
) >> 5;
768 octa2
= (LOW_ZERO
& *p
) >> 2;
769 carry
= (CARRY_ZERO
& *p
);
770 fprintf_filtered (stream
, "%o", octa1
);
771 fprintf_filtered (stream
, "%o", octa2
);
775 /* Carry in, carry out */
776 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
777 octa2
= (MID_ONE
& *p
) >> 4;
778 octa3
= (LOW_ONE
& *p
) >> 1;
779 carry
= (CARRY_ONE
& *p
);
780 fprintf_filtered (stream
, "%o", octa1
);
781 fprintf_filtered (stream
, "%o", octa2
);
782 fprintf_filtered (stream
, "%o", octa3
);
786 /* Carry in, no carry out */
787 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
788 octa2
= (MID_TWO
& *p
) >> 3;
789 octa3
= (LOW_TWO
& *p
);
791 fprintf_filtered (stream
, "%o", octa1
);
792 fprintf_filtered (stream
, "%o", octa2
);
793 fprintf_filtered (stream
, "%o", octa3
);
797 error ("Internal error in octal conversion;");
801 cycle
= cycle
% BITS_IN_OCTAL
;
805 fprintf_filtered (stream
, local_octal_format_suffix ());
808 /* VALADDR points to an integer of LEN bytes.
809 * Print it in decimal on stream or format it in buf.
812 print_decimal_chars (struct ui_file
*stream
, unsigned char *valaddr
,
816 #define TWO_TO_FOURTH 16
817 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
818 #define CARRY_LEFT( x ) ((x) % TEN)
819 #define SHIFT( x ) ((x) << 4)
821 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
823 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
825 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
826 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
827 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
830 unsigned char *digits
;
833 int i
, j
, decimal_digits
;
837 /* Base-ten number is less than twice as many digits
838 * as the base 16 number, which is 2 digits per byte.
840 decimal_len
= len
* 2 * 2;
841 digits
= xmalloc (decimal_len
);
843 for (i
= 0; i
< decimal_len
; i
++)
848 fprintf_filtered (stream
, local_decimal_format_prefix ());
850 /* Ok, we have an unknown number of bytes of data to be printed in
853 * Given a hex number (in nibbles) as XYZ, we start by taking X and
854 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
855 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
857 * The trick is that "digits" holds a base-10 number, but sometimes
858 * the individual digits are > 10.
860 * Outer loop is per nibble (hex digit) of input, from MSD end to
863 decimal_digits
= 0; /* Number of decimal digits so far */
869 * Multiply current base-ten number by 16 in place.
870 * Each digit was between 0 and 9, now is between
873 for (j
= 0; j
< decimal_digits
; j
++)
875 digits
[j
] = SHIFT (digits
[j
]);
878 /* Take the next nibble off the input and add it to what
879 * we've got in the LSB position. Bottom 'digit' is now
882 * "flip" is used to run this loop twice for each byte.
888 digits
[0] += HIGH_NIBBLE (*p
);
893 /* Take low nibble and bump our pointer "p".
895 digits
[0] += LOW_NIBBLE (*p
);
900 /* Re-decimalize. We have to do this often enough
901 * that we don't overflow, but once per nibble is
902 * overkill. Easier this way, though. Note that the
903 * carry is often larger than 10 (e.g. max initial
904 * carry out of lowest nibble is 15, could bubble all
905 * the way up greater than 10). So we have to do
906 * the carrying beyond the last current digit.
909 for (j
= 0; j
< decimal_len
- 1; j
++)
913 /* "/" won't handle an unsigned char with
914 * a value that if signed would be negative.
915 * So extend to longword int via "dummy".
918 carry
= CARRY_OUT (dummy
);
919 digits
[j
] = CARRY_LEFT (dummy
);
921 if (j
>= decimal_digits
&& carry
== 0)
924 * All higher digits are 0 and we
925 * no longer have a carry.
927 * Note: "j" is 0-based, "decimal_digits" is
930 decimal_digits
= j
+ 1;
936 /* Ok, now "digits" is the decimal representation, with
937 * the "decimal_digits" actual digits. Print!
939 for (i
= decimal_digits
- 1; i
>= 0; i
--)
941 fprintf_filtered (stream
, "%1d", digits
[i
]);
945 fprintf_filtered (stream
, local_decimal_format_suffix ());
948 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
951 print_hex_chars (struct ui_file
*stream
, unsigned char *valaddr
, unsigned len
)
955 /* FIXME: We should be not printing leading zeroes in most cases. */
957 fprintf_filtered (stream
, local_hex_format_prefix ());
958 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
964 fprintf_filtered (stream
, "%02x", *p
);
969 for (p
= valaddr
+ len
- 1;
973 fprintf_filtered (stream
, "%02x", *p
);
976 fprintf_filtered (stream
, local_hex_format_suffix ());
979 /* Called by various <lang>_val_print routines to print elements of an
980 array in the form "<elem1>, <elem2>, <elem3>, ...".
982 (FIXME?) Assumes array element separator is a comma, which is correct
983 for all languages currently handled.
984 (FIXME?) Some languages have a notation for repeated array elements,
985 perhaps we should try to use that notation when appropriate.
989 val_print_array_elements (struct type
*type
, char *valaddr
, CORE_ADDR address
,
990 struct ui_file
*stream
, int format
, int deref_ref
,
991 int recurse
, enum val_prettyprint pretty
,
994 unsigned int things_printed
= 0;
996 struct type
*elttype
;
998 /* Position of the array element we are examining to see
999 whether it is repeated. */
1001 /* Number of repetitions we have detected so far. */
1004 elttype
= TYPE_TARGET_TYPE (type
);
1005 eltlen
= TYPE_LENGTH (check_typedef (elttype
));
1006 len
= TYPE_LENGTH (type
) / eltlen
;
1008 annotate_array_section_begin (i
, elttype
);
1010 for (; i
< len
&& things_printed
< print_max
; i
++)
1014 if (prettyprint_arrays
)
1016 fprintf_filtered (stream
, ",\n");
1017 print_spaces_filtered (2 + 2 * recurse
, stream
);
1021 fprintf_filtered (stream
, ", ");
1024 wrap_here (n_spaces (2 + 2 * recurse
));
1028 while ((rep1
< len
) &&
1029 !memcmp (valaddr
+ i
* eltlen
, valaddr
+ rep1
* eltlen
, eltlen
))
1035 if (reps
> repeat_count_threshold
)
1037 val_print (elttype
, valaddr
+ i
* eltlen
, 0, 0, stream
, format
,
1038 deref_ref
, recurse
+ 1, pretty
);
1039 annotate_elt_rep (reps
);
1040 fprintf_filtered (stream
, " <repeats %u times>", reps
);
1041 annotate_elt_rep_end ();
1044 things_printed
+= repeat_count_threshold
;
1048 val_print (elttype
, valaddr
+ i
* eltlen
, 0, 0, stream
, format
,
1049 deref_ref
, recurse
+ 1, pretty
);
1054 annotate_array_section_end ();
1057 fprintf_filtered (stream
, "...");
1061 /* Read LEN bytes of target memory at address MEMADDR, placing the
1062 results in GDB's memory at MYADDR. Returns a count of the bytes
1063 actually read, and optionally an errno value in the location
1064 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1066 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1067 function be eliminated. */
1070 partial_memory_read (CORE_ADDR memaddr
, char *myaddr
, int len
, int *errnoptr
)
1072 int nread
; /* Number of bytes actually read. */
1073 int errcode
; /* Error from last read. */
1075 /* First try a complete read. */
1076 errcode
= target_read_memory (memaddr
, myaddr
, len
);
1084 /* Loop, reading one byte at a time until we get as much as we can. */
1085 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
1087 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
1089 /* If an error, the last read was unsuccessful, so adjust count. */
1095 if (errnoptr
!= NULL
)
1097 *errnoptr
= errcode
;
1102 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1103 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1104 stops at the first null byte, otherwise printing proceeds (including null
1105 bytes) until either print_max or LEN characters have been printed,
1106 whichever is smaller. */
1108 /* FIXME: Use target_read_string. */
1111 val_print_string (CORE_ADDR addr
, int len
, int width
, struct ui_file
*stream
)
1113 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
1114 int errcode
; /* Errno returned from bad reads. */
1115 unsigned int fetchlimit
; /* Maximum number of chars to print. */
1116 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
1117 unsigned int chunksize
; /* Size of each fetch, in chars. */
1118 char *buffer
= NULL
; /* Dynamically growable fetch buffer. */
1119 char *bufptr
; /* Pointer to next available byte in buffer. */
1120 char *limit
; /* First location past end of fetch buffer. */
1121 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
1122 int found_nul
; /* Non-zero if we found the nul char */
1124 /* First we need to figure out the limit on the number of characters we are
1125 going to attempt to fetch and print. This is actually pretty simple. If
1126 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1127 LEN is -1, then the limit is print_max. This is true regardless of
1128 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1129 because finding the null byte (or available memory) is what actually
1130 limits the fetch. */
1132 fetchlimit
= (len
== -1 ? print_max
: min (len
, print_max
));
1134 /* Now decide how large of chunks to try to read in one operation. This
1135 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1136 so we might as well read them all in one operation. If LEN is -1, we
1137 are looking for a null terminator to end the fetching, so we might as
1138 well read in blocks that are large enough to be efficient, but not so
1139 large as to be slow if fetchlimit happens to be large. So we choose the
1140 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1141 200 is way too big for remote debugging over a serial line. */
1143 chunksize
= (len
== -1 ? min (8, fetchlimit
) : fetchlimit
);
1145 /* Loop until we either have all the characters to print, or we encounter
1146 some error, such as bumping into the end of the address space. */
1149 old_chain
= make_cleanup (null_cleanup
, 0);
1153 buffer
= (char *) xmalloc (len
* width
);
1155 old_chain
= make_cleanup (xfree
, buffer
);
1157 nfetch
= partial_memory_read (addr
, bufptr
, len
* width
, &errcode
)
1159 addr
+= nfetch
* width
;
1160 bufptr
+= nfetch
* width
;
1164 unsigned long bufsize
= 0;
1168 nfetch
= min (chunksize
, fetchlimit
- bufsize
);
1171 buffer
= (char *) xmalloc (nfetch
* width
);
1174 discard_cleanups (old_chain
);
1175 buffer
= (char *) xrealloc (buffer
, (nfetch
+ bufsize
) * width
);
1178 old_chain
= make_cleanup (xfree
, buffer
);
1179 bufptr
= buffer
+ bufsize
* width
;
1182 /* Read as much as we can. */
1183 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
1186 /* Scan this chunk for the null byte that terminates the string
1187 to print. If found, we don't need to fetch any more. Note
1188 that bufptr is explicitly left pointing at the next character
1189 after the null byte, or at the next character after the end of
1192 limit
= bufptr
+ nfetch
* width
;
1193 while (bufptr
< limit
)
1197 c
= extract_unsigned_integer (bufptr
, width
);
1202 /* We don't care about any error which happened after
1203 the NULL terminator. */
1210 while (errcode
== 0 /* no error */
1211 && bufptr
- buffer
< fetchlimit
* width
/* no overrun */
1212 && !found_nul
); /* haven't found nul yet */
1215 { /* length of string is really 0! */
1216 buffer
= bufptr
= NULL
;
1220 /* bufptr and addr now point immediately beyond the last byte which we
1221 consider part of the string (including a '\0' which ends the string). */
1223 /* We now have either successfully filled the buffer to fetchlimit, or
1224 terminated early due to an error or finding a null char when LEN is -1. */
1226 if (len
== -1 && !found_nul
)
1230 /* We didn't find a null terminator we were looking for. Attempt
1231 to peek at the next character. If not successful, or it is not
1232 a null byte, then force ellipsis to be printed. */
1234 peekbuf
= (char *) alloca (width
);
1236 if (target_read_memory (addr
, peekbuf
, width
) == 0
1237 && extract_unsigned_integer (peekbuf
, width
) != 0)
1240 else if ((len
>= 0 && errcode
!= 0) || (len
> (bufptr
- buffer
) / width
))
1242 /* Getting an error when we have a requested length, or fetching less
1243 than the number of characters actually requested, always make us
1250 /* If we get an error before fetching anything, don't print a string.
1251 But if we fetch something and then get an error, print the string
1252 and then the error message. */
1253 if (errcode
== 0 || bufptr
> buffer
)
1257 fputs_filtered (" ", stream
);
1259 LA_PRINT_STRING (stream
, buffer
, (bufptr
- buffer
) / width
, width
, force_ellipsis
);
1266 fprintf_filtered (stream
, " <Address ");
1267 print_address_numeric (addr
, 1, stream
);
1268 fprintf_filtered (stream
, " out of bounds>");
1272 fprintf_filtered (stream
, " <Error reading address ");
1273 print_address_numeric (addr
, 1, stream
);
1274 fprintf_filtered (stream
, ": %s>", safe_strerror (errcode
));
1278 do_cleanups (old_chain
);
1279 return ((bufptr
- buffer
) / width
);
1283 /* Validate an input or output radix setting, and make sure the user
1284 knows what they really did here. Radix setting is confusing, e.g.
1285 setting the input radix to "10" never changes it! */
1289 set_input_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
1291 set_input_radix_1 (from_tty
, *(unsigned *) c
->var
);
1296 set_input_radix_1 (int from_tty
, unsigned radix
)
1298 /* We don't currently disallow any input radix except 0 or 1, which don't
1299 make any mathematical sense. In theory, we can deal with any input
1300 radix greater than 1, even if we don't have unique digits for every
1301 value from 0 to radix-1, but in practice we lose on large radix values.
1302 We should either fix the lossage or restrict the radix range more.
1307 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1310 input_radix
= radix
;
1313 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1314 radix
, radix
, radix
);
1320 set_output_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
1322 set_output_radix_1 (from_tty
, *(unsigned *) c
->var
);
1326 set_output_radix_1 (int from_tty
, unsigned radix
)
1328 /* Validate the radix and disallow ones that we aren't prepared to
1329 handle correctly, leaving the radix unchanged. */
1333 output_format
= 'x'; /* hex */
1336 output_format
= 0; /* decimal */
1339 output_format
= 'o'; /* octal */
1342 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1345 output_radix
= radix
;
1348 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1349 radix
, radix
, radix
);
1353 /* Set both the input and output radix at once. Try to set the output radix
1354 first, since it has the most restrictive range. An radix that is valid as
1355 an output radix is also valid as an input radix.
1357 It may be useful to have an unusual input radix. If the user wishes to
1358 set an input radix that is not valid as an output radix, he needs to use
1359 the 'set input-radix' command. */
1362 set_radix (char *arg
, int from_tty
)
1366 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
1367 set_output_radix_1 (0, radix
);
1368 set_input_radix_1 (0, radix
);
1371 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1372 radix
, radix
, radix
);
1376 /* Show both the input and output radices. */
1380 show_radix (char *arg
, int from_tty
)
1384 if (input_radix
== output_radix
)
1386 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1387 input_radix
, input_radix
, input_radix
);
1391 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1392 input_radix
, input_radix
, input_radix
);
1393 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1394 output_radix
, output_radix
, output_radix
);
1402 set_print (char *arg
, int from_tty
)
1405 "\"set print\" must be followed by the name of a print subcommand.\n");
1406 help_list (setprintlist
, "set print ", -1, gdb_stdout
);
1411 show_print (char *args
, int from_tty
)
1413 cmd_show_list (showprintlist
, from_tty
, "");
1417 _initialize_valprint (void)
1419 struct cmd_list_element
*c
;
1421 add_prefix_cmd ("print", no_class
, set_print
,
1422 "Generic command for setting how things print.",
1423 &setprintlist
, "set print ", 0, &setlist
);
1424 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
1425 /* prefer set print to set prompt */
1426 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
1428 add_prefix_cmd ("print", no_class
, show_print
,
1429 "Generic command for showing print settings.",
1430 &showprintlist
, "show print ", 0, &showlist
);
1431 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
1432 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
1435 (add_set_cmd ("elements", no_class
, var_uinteger
, (char *) &print_max
,
1436 "Set limit on string chars or array elements to print.\n\
1437 \"set print elements 0\" causes there to be no limit.",
1442 (add_set_cmd ("null-stop", no_class
, var_boolean
,
1443 (char *) &stop_print_at_null
,
1444 "Set printing of char arrays to stop at first null char.",
1449 (add_set_cmd ("repeats", no_class
, var_uinteger
,
1450 (char *) &repeat_count_threshold
,
1451 "Set threshold for repeated print elements.\n\
1452 \"set print repeats 0\" causes all elements to be individually printed.",
1457 (add_set_cmd ("pretty", class_support
, var_boolean
,
1458 (char *) &prettyprint_structs
,
1459 "Set prettyprinting of structures.",
1464 (add_set_cmd ("union", class_support
, var_boolean
, (char *) &unionprint
,
1465 "Set printing of unions interior to structures.",
1470 (add_set_cmd ("array", class_support
, var_boolean
,
1471 (char *) &prettyprint_arrays
,
1472 "Set prettyprinting of arrays.",
1477 (add_set_cmd ("address", class_support
, var_boolean
, (char *) &addressprint
,
1478 "Set printing of addresses.",
1482 c
= add_set_cmd ("input-radix", class_support
, var_uinteger
,
1483 (char *) &input_radix
,
1484 "Set default input radix for entering numbers.",
1486 add_show_from_set (c
, &showlist
);
1487 c
->function
.sfunc
= set_input_radix
;
1489 c
= add_set_cmd ("output-radix", class_support
, var_uinteger
,
1490 (char *) &output_radix
,
1491 "Set default output radix for printing of values.",
1493 add_show_from_set (c
, &showlist
);
1494 c
->function
.sfunc
= set_output_radix
;
1496 /* The "set radix" and "show radix" commands are special in that they are
1497 like normal set and show commands but allow two normally independent
1498 variables to be either set or shown with a single command. So the
1499 usual add_set_cmd() and add_show_from_set() commands aren't really
1501 add_cmd ("radix", class_support
, set_radix
,
1502 "Set default input and output number radices.\n\
1503 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1504 Without an argument, sets both radices back to the default value of 10.",
1506 add_cmd ("radix", class_support
, show_radix
,
1507 "Show the default input and output number radices.\n\
1508 Use 'show input-radix' or 'show output-radix' to independently show each.",
1511 /* Give people the defaults which they are used to. */
1512 prettyprint_structs
= 0;
1513 prettyprint_arrays
= 0;
1516 print_max
= PRINT_MAX_DEFAULT
;