1 /* Print values for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005 Free Software
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #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 show_print (char *, int);
47 static void set_print (char *, int);
49 static void set_radix (char *, int);
51 static void show_radix (char *, int);
53 static void set_input_radix (char *, int, struct cmd_list_element *);
55 static void set_input_radix_1 (int, unsigned);
57 static void set_output_radix (char *, int, struct cmd_list_element *);
59 static void set_output_radix_1 (int, unsigned);
61 void _initialize_valprint (void);
63 /* Maximum number of chars to print for a string pointer value or vector
64 contents, or UINT_MAX for no limit. Note that "set print elements 0"
65 stores UINT_MAX in print_max, which displays in a show command as
68 unsigned int print_max;
69 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
71 show_print_max (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c, const char *value)
74 fprintf_filtered (file, _("\
75 Limit on string chars or array elements to print is %s.\n"),
80 /* Default input and output radixes, and output format letter. */
82 unsigned input_radix = 10;
84 show_input_radix (struct ui_file *file, int from_tty,
85 struct cmd_list_element *c, const char *value)
87 fprintf_filtered (file, _("\
88 Default input radix for entering numbers is %s.\n"),
92 unsigned output_radix = 10;
94 show_output_radix (struct ui_file *file, int from_tty,
95 struct cmd_list_element *c, const char *value)
97 fprintf_filtered (file, _("\
98 Default output radix for printing of values is %s.\n"),
101 int output_format = 0;
103 /* By default we print arrays without printing the index of each element in
104 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
106 static int print_array_indexes = 0;
108 show_print_array_indexes (struct ui_file *file, int from_tty,
109 struct cmd_list_element *c, const char *value)
111 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
114 /* Print repeat counts if there are more than this many repetitions of an
115 element in an array. Referenced by the low level language dependent
118 unsigned int repeat_count_threshold = 10;
120 show_repeat_count_threshold (struct ui_file *file, int from_tty,
121 struct cmd_list_element *c, const char *value)
123 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
127 /* If nonzero, stops printing of char arrays at first null. */
129 int stop_print_at_null;
131 show_stop_print_at_null (struct ui_file *file, int from_tty,
132 struct cmd_list_element *c, const char *value)
134 fprintf_filtered (file, _("\
135 Printing of char arrays to stop at first null char is %s.\n"),
139 /* Controls pretty printing of structures. */
141 int prettyprint_structs;
143 show_prettyprint_structs (struct ui_file *file, int from_tty,
144 struct cmd_list_element *c, const char *value)
146 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
149 /* Controls pretty printing of arrays. */
151 int prettyprint_arrays;
153 show_prettyprint_arrays (struct ui_file *file, int from_tty,
154 struct cmd_list_element *c, const char *value)
156 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
159 /* If nonzero, causes unions inside structures or other unions to be
162 int unionprint; /* Controls printing of nested unions. */
164 show_unionprint (struct ui_file *file, int from_tty,
165 struct cmd_list_element *c, const char *value)
167 fprintf_filtered (file, _("\
168 Printing of unions interior to structures is %s.\n"),
172 /* If nonzero, causes machine addresses to be printed in certain contexts. */
174 int addressprint; /* Controls printing of machine addresses */
176 show_addressprint (struct ui_file *file, int from_tty,
177 struct cmd_list_element *c, const char *value)
179 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
183 /* Print data of type TYPE located at VALADDR (within GDB), which came from
184 the inferior at address ADDRESS, onto stdio stream STREAM according to
185 FORMAT (a letter, or 0 for natural format using TYPE).
187 If DEREF_REF is nonzero, then dereference references, otherwise just print
190 The PRETTY parameter controls prettyprinting.
192 If the data are a string pointer, returns the number of string characters
195 FIXME: The data at VALADDR is in target byte order. If gdb is ever
196 enhanced to be able to debug more than the single target it was compiled
197 for (specific CPU type and thus specific target byte ordering), then
198 either the print routines are going to have to take this into account,
199 or the data is going to have to be passed into here already converted
200 to the host byte ordering, whichever is more convenient. */
204 val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
205 CORE_ADDR address, struct ui_file *stream, int format,
206 int deref_ref, int recurse, enum val_prettyprint pretty)
208 struct type *real_type = check_typedef (type);
209 if (pretty == Val_pretty_default)
211 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
216 /* Ensure that the type is complete and not just a stub. If the type is
217 only a stub and we can't find and substitute its complete type, then
218 print appropriate string and return. */
220 if (TYPE_STUB (real_type))
222 fprintf_filtered (stream, "<incomplete type>");
227 return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
228 stream, format, deref_ref, recurse, pretty));
231 /* Check whether the value VAL is printable. Return 1 if it is;
232 return 0 and print an appropriate error message to STREAM if it
236 value_check_printable (struct value *val, struct ui_file *stream)
240 fprintf_filtered (stream, _("<address of value unknown>"));
244 if (value_optimized_out (val))
246 fprintf_filtered (stream, _("<value optimized out>"));
253 /* Print the value VAL onto stream STREAM according to FORMAT (a
254 letter, or 0 for natural format using TYPE).
256 If DEREF_REF is nonzero, then dereference references, otherwise just print
259 The PRETTY parameter controls prettyprinting.
261 If the data are a string pointer, returns the number of string characters
264 This is a preferable interface to val_print, above, because it uses
265 GDB's value mechanism. */
268 common_val_print (struct value *val, struct ui_file *stream, int format,
269 int deref_ref, int recurse, enum val_prettyprint pretty)
271 if (!value_check_printable (val, stream))
274 return val_print (value_type (val), value_contents_all (val),
275 value_embedded_offset (val), VALUE_ADDRESS (val),
276 stream, format, deref_ref, recurse, pretty);
279 /* Print the value VAL in C-ish syntax on stream STREAM.
280 FORMAT is a format-letter, or 0 for print in natural format of data type.
281 If the object printed is a string pointer, returns
282 the number of string bytes printed. */
285 value_print (struct value *val, struct ui_file *stream, int format,
286 enum val_prettyprint pretty)
288 if (!value_check_printable (val, stream))
291 return LA_VALUE_PRINT (val, stream, format, pretty);
294 /* Called by various <lang>_val_print routines to print
295 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
296 value. STREAM is where to print the value. */
299 val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
300 struct ui_file *stream)
302 if (TYPE_LENGTH (type) > sizeof (LONGEST))
306 if (TYPE_UNSIGNED (type)
307 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
310 print_longest (stream, 'u', 0, val);
314 /* Signed, or we couldn't turn an unsigned value into a
315 LONGEST. For signed values, one could assume two's
316 complement (a reasonable assumption, I think) and do
318 print_hex_chars (stream, (unsigned char *) valaddr,
324 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
325 unpack_long (type, valaddr));
329 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
330 The raison d'etre of this function is to consolidate printing of
331 LONG_LONG's into this one function. The format chars b,h,w,g are
332 from print_scalar_formatted(). Numbers are printed using C
335 USE_C_FORMAT means to use C format in all cases. Without it,
336 'o' and 'x' format do not include the standard C radix prefix
339 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
340 and was intended to request formating according to the current
341 language and would be used for most integers that GDB prints. The
342 exceptional cases were things like protocols where the format of
343 the integer is a protocol thing, not a user-visible thing). The
344 parameter remains to preserve the information of what things might
345 be printed with language-specific format, should we ever resurrect
349 print_longest (struct ui_file *stream, int format, int use_c_format,
357 val = int_string (val_long, 10, 1, 0, 1); break;
359 val = int_string (val_long, 10, 0, 0, 1); break;
361 val = int_string (val_long, 16, 0, 0, use_c_format); break;
363 val = int_string (val_long, 16, 0, 2, 1); break;
365 val = int_string (val_long, 16, 0, 4, 1); break;
367 val = int_string (val_long, 16, 0, 8, 1); break;
369 val = int_string (val_long, 16, 0, 16, 1); break;
372 val = int_string (val_long, 8, 0, 0, use_c_format); break;
374 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
376 fputs_filtered (val, stream);
379 /* This used to be a macro, but I don't think it is called often enough
380 to merit such treatment. */
381 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
382 arguments to a function, number in a value history, register number, etc.)
383 where the value must not be larger than can fit in an int. */
386 longest_to_int (LONGEST arg)
388 /* Let the compiler do the work */
389 int rtnval = (int) arg;
391 /* Check for overflows or underflows */
392 if (sizeof (LONGEST) > sizeof (int))
396 error (_("Value out of range."));
402 /* Print a floating point value of type TYPE (not always a
403 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
406 print_floating (const gdb_byte *valaddr, struct type *type,
407 struct ui_file *stream)
411 const struct floatformat *fmt = NULL;
412 unsigned len = TYPE_LENGTH (type);
414 /* If it is a floating-point, check for obvious problems. */
415 if (TYPE_CODE (type) == TYPE_CODE_FLT)
416 fmt = floatformat_from_type (type);
417 if (fmt != NULL && floatformat_is_nan (fmt, valaddr))
419 if (floatformat_is_negative (fmt, valaddr))
420 fprintf_filtered (stream, "-");
421 fprintf_filtered (stream, "nan(");
422 fputs_filtered ("0x", stream);
423 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
424 fprintf_filtered (stream, ")");
428 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
429 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
430 needs to be used as that takes care of any necessary type
431 conversions. Such conversions are of course direct to DOUBLEST
432 and disregard any possible target floating point limitations.
433 For instance, a u64 would be converted and displayed exactly on a
434 host with 80 bit DOUBLEST but with loss of information on a host
435 with 64 bit DOUBLEST. */
437 doub = unpack_double (type, valaddr, &inv);
440 fprintf_filtered (stream, "<invalid float value>");
444 /* FIXME: kettenis/2001-01-20: The following code makes too much
445 assumptions about the host and target floating point format. */
447 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
448 not necessarily be a TYPE_CODE_FLT, the below ignores that and
449 instead uses the type's length to determine the precision of the
450 floating-point value being printed. */
452 if (len < sizeof (double))
453 fprintf_filtered (stream, "%.9g", (double) doub);
454 else if (len == sizeof (double))
455 fprintf_filtered (stream, "%.17g", (double) doub);
457 #ifdef PRINTF_HAS_LONG_DOUBLE
458 fprintf_filtered (stream, "%.35Lg", doub);
460 /* This at least wins with values that are representable as
462 fprintf_filtered (stream, "%.17g", (double) doub);
467 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
471 #define BITS_IN_BYTES 8
477 /* Declared "int" so it will be signed.
478 * This ensures that right shift will shift in zeros.
480 const int mask = 0x080;
482 /* FIXME: We should be not printing leading zeroes in most cases. */
484 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
490 /* Every byte has 8 binary characters; peel off
491 * and print from the MSB end.
493 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
495 if (*p & (mask >> i))
500 fprintf_filtered (stream, "%1d", b);
506 for (p = valaddr + len - 1;
510 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
512 if (*p & (mask >> i))
517 fprintf_filtered (stream, "%1d", b);
523 /* VALADDR points to an integer of LEN bytes.
524 * Print it in octal on stream or format it in buf.
527 print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
531 unsigned char octa1, octa2, octa3, carry;
534 /* FIXME: We should be not printing leading zeroes in most cases. */
537 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
538 * the extra bits, which cycle every three bytes:
542 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
544 * Octal side: 0 1 carry 3 4 carry ...
546 * Cycle number: 0 1 2
548 * But of course we are printing from the high side, so we have to
549 * figure out where in the cycle we are so that we end up with no
550 * left over bits at the end.
552 #define BITS_IN_OCTAL 3
553 #define HIGH_ZERO 0340
554 #define LOW_ZERO 0016
555 #define CARRY_ZERO 0003
556 #define HIGH_ONE 0200
559 #define CARRY_ONE 0001
560 #define HIGH_TWO 0300
564 /* For 32 we start in cycle 2, with two bits and one bit carry;
565 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
567 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
570 fputs_filtered ("0", stream);
571 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
580 /* No carry in, carry out two bits.
582 octa1 = (HIGH_ZERO & *p) >> 5;
583 octa2 = (LOW_ZERO & *p) >> 2;
584 carry = (CARRY_ZERO & *p);
585 fprintf_filtered (stream, "%o", octa1);
586 fprintf_filtered (stream, "%o", octa2);
590 /* Carry in two bits, carry out one bit.
592 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
593 octa2 = (MID_ONE & *p) >> 4;
594 octa3 = (LOW_ONE & *p) >> 1;
595 carry = (CARRY_ONE & *p);
596 fprintf_filtered (stream, "%o", octa1);
597 fprintf_filtered (stream, "%o", octa2);
598 fprintf_filtered (stream, "%o", octa3);
602 /* Carry in one bit, no carry out.
604 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
605 octa2 = (MID_TWO & *p) >> 3;
606 octa3 = (LOW_TWO & *p);
608 fprintf_filtered (stream, "%o", octa1);
609 fprintf_filtered (stream, "%o", octa2);
610 fprintf_filtered (stream, "%o", octa3);
614 error (_("Internal error in octal conversion;"));
618 cycle = cycle % BITS_IN_OCTAL;
623 for (p = valaddr + len - 1;
630 /* Carry out, no carry in */
631 octa1 = (HIGH_ZERO & *p) >> 5;
632 octa2 = (LOW_ZERO & *p) >> 2;
633 carry = (CARRY_ZERO & *p);
634 fprintf_filtered (stream, "%o", octa1);
635 fprintf_filtered (stream, "%o", octa2);
639 /* Carry in, carry out */
640 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
641 octa2 = (MID_ONE & *p) >> 4;
642 octa3 = (LOW_ONE & *p) >> 1;
643 carry = (CARRY_ONE & *p);
644 fprintf_filtered (stream, "%o", octa1);
645 fprintf_filtered (stream, "%o", octa2);
646 fprintf_filtered (stream, "%o", octa3);
650 /* Carry in, no carry out */
651 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
652 octa2 = (MID_TWO & *p) >> 3;
653 octa3 = (LOW_TWO & *p);
655 fprintf_filtered (stream, "%o", octa1);
656 fprintf_filtered (stream, "%o", octa2);
657 fprintf_filtered (stream, "%o", octa3);
661 error (_("Internal error in octal conversion;"));
665 cycle = cycle % BITS_IN_OCTAL;
671 /* VALADDR points to an integer of LEN bytes.
672 * Print it in decimal on stream or format it in buf.
675 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
679 #define TWO_TO_FOURTH 16
680 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
681 #define CARRY_LEFT( x ) ((x) % TEN)
682 #define SHIFT( x ) ((x) << 4)
684 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1)
686 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
688 ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? p++ : p-- )
689 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
690 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
693 unsigned char *digits;
696 int i, j, decimal_digits;
700 /* Base-ten number is less than twice as many digits
701 * as the base 16 number, which is 2 digits per byte.
703 decimal_len = len * 2 * 2;
704 digits = xmalloc (decimal_len);
706 for (i = 0; i < decimal_len; i++)
711 /* Ok, we have an unknown number of bytes of data to be printed in
714 * Given a hex number (in nibbles) as XYZ, we start by taking X and
715 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
716 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
718 * The trick is that "digits" holds a base-10 number, but sometimes
719 * the individual digits are > 10.
721 * Outer loop is per nibble (hex digit) of input, from MSD end to
724 decimal_digits = 0; /* Number of decimal digits so far */
730 * Multiply current base-ten number by 16 in place.
731 * Each digit was between 0 and 9, now is between
734 for (j = 0; j < decimal_digits; j++)
736 digits[j] = SHIFT (digits[j]);
739 /* Take the next nibble off the input and add it to what
740 * we've got in the LSB position. Bottom 'digit' is now
743 * "flip" is used to run this loop twice for each byte.
749 digits[0] += HIGH_NIBBLE (*p);
754 /* Take low nibble and bump our pointer "p".
756 digits[0] += LOW_NIBBLE (*p);
761 /* Re-decimalize. We have to do this often enough
762 * that we don't overflow, but once per nibble is
763 * overkill. Easier this way, though. Note that the
764 * carry is often larger than 10 (e.g. max initial
765 * carry out of lowest nibble is 15, could bubble all
766 * the way up greater than 10). So we have to do
767 * the carrying beyond the last current digit.
770 for (j = 0; j < decimal_len - 1; j++)
774 /* "/" won't handle an unsigned char with
775 * a value that if signed would be negative.
776 * So extend to longword int via "dummy".
779 carry = CARRY_OUT (dummy);
780 digits[j] = CARRY_LEFT (dummy);
782 if (j >= decimal_digits && carry == 0)
785 * All higher digits are 0 and we
786 * no longer have a carry.
788 * Note: "j" is 0-based, "decimal_digits" is
791 decimal_digits = j + 1;
797 /* Ok, now "digits" is the decimal representation, with
798 * the "decimal_digits" actual digits. Print!
800 for (i = decimal_digits - 1; i >= 0; i--)
802 fprintf_filtered (stream, "%1d", digits[i]);
807 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
810 print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
815 /* FIXME: We should be not printing leading zeroes in most cases. */
817 fputs_filtered ("0x", stream);
818 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
824 fprintf_filtered (stream, "%02x", *p);
829 for (p = valaddr + len - 1;
833 fprintf_filtered (stream, "%02x", *p);
838 /* VALADDR points to a char integer of LEN bytes. Print it out in appropriate language form on stream.
839 Omit any leading zero chars. */
842 print_char_chars (struct ui_file *stream, const gdb_byte *valaddr,
847 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
850 while (p < valaddr + len - 1 && *p == 0)
853 while (p < valaddr + len)
855 LA_EMIT_CHAR (*p, stream, '\'');
861 p = valaddr + len - 1;
862 while (p > valaddr && *p == 0)
867 LA_EMIT_CHAR (*p, stream, '\'');
873 /* Return non-zero if the debugger should print the index of each element
874 when printing array values. */
877 print_array_indexes_p (void)
879 return print_array_indexes;
882 /* Assuming TYPE is a simple, non-empty array type, compute its lower bound.
883 Save it into LOW_BOUND if not NULL.
885 Return 1 if the operation was successful. Return zero otherwise,
886 in which case the value of LOW_BOUND is unmodified.
888 Computing the array lower bound is pretty easy, but this function
889 does some additional verifications before returning the low bound.
890 If something incorrect is detected, it is better to return a status
891 rather than throwing an error, making it easier for the caller to
892 implement an error-recovery plan. For instance, it may decide to
893 warn the user that the bound was not found and then use a default
897 get_array_low_bound (struct type *type, long *low_bound)
899 struct type *index = TYPE_INDEX_TYPE (type);
905 if (TYPE_CODE (index) != TYPE_CODE_RANGE
906 && TYPE_CODE (index) != TYPE_CODE_ENUM)
909 low = TYPE_LOW_BOUND (index);
910 if (low > TYPE_HIGH_BOUND (index))
919 /* Print on STREAM using the given FORMAT the index for the element
920 at INDEX of an array whose index type is INDEX_TYPE. */
923 maybe_print_array_index (struct type *index_type, LONGEST index,
924 struct ui_file *stream, int format,
925 enum val_prettyprint pretty)
927 struct value *index_value;
929 if (!print_array_indexes)
932 index_value = value_from_longest (index_type, index);
934 LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty);
937 /* Called by various <lang>_val_print routines to print elements of an
938 array in the form "<elem1>, <elem2>, <elem3>, ...".
940 (FIXME?) Assumes array element separator is a comma, which is correct
941 for all languages currently handled.
942 (FIXME?) Some languages have a notation for repeated array elements,
943 perhaps we should try to use that notation when appropriate.
947 val_print_array_elements (struct type *type, const gdb_byte *valaddr,
948 CORE_ADDR address, struct ui_file *stream,
949 int format, int deref_ref,
950 int recurse, enum val_prettyprint pretty,
953 unsigned int things_printed = 0;
955 struct type *elttype, *index_type;
957 /* Position of the array element we are examining to see
958 whether it is repeated. */
960 /* Number of repetitions we have detected so far. */
962 long low_bound_index = 0;
964 elttype = TYPE_TARGET_TYPE (type);
965 eltlen = TYPE_LENGTH (check_typedef (elttype));
966 len = TYPE_LENGTH (type) / eltlen;
967 index_type = TYPE_INDEX_TYPE (type);
969 /* Get the array low bound. This only makes sense if the array
970 has one or more element in it. */
971 if (len > 0 && !get_array_low_bound (type, &low_bound_index))
973 warning ("unable to get low bound of array, using zero as default");
977 annotate_array_section_begin (i, elttype);
979 for (; i < len && things_printed < print_max; i++)
983 if (prettyprint_arrays)
985 fprintf_filtered (stream, ",\n");
986 print_spaces_filtered (2 + 2 * recurse, stream);
990 fprintf_filtered (stream, ", ");
993 wrap_here (n_spaces (2 + 2 * recurse));
994 maybe_print_array_index (index_type, i + low_bound_index,
995 stream, format, pretty);
999 while ((rep1 < len) &&
1000 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1006 if (reps > repeat_count_threshold)
1008 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1009 deref_ref, recurse + 1, pretty);
1010 annotate_elt_rep (reps);
1011 fprintf_filtered (stream, " <repeats %u times>", reps);
1012 annotate_elt_rep_end ();
1015 things_printed += repeat_count_threshold;
1019 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1020 deref_ref, recurse + 1, pretty);
1025 annotate_array_section_end ();
1028 fprintf_filtered (stream, "...");
1032 /* Read LEN bytes of target memory at address MEMADDR, placing the
1033 results in GDB's memory at MYADDR. Returns a count of the bytes
1034 actually read, and optionally an errno value in the location
1035 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1037 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1038 function be eliminated. */
1041 partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
1043 int nread; /* Number of bytes actually read. */
1044 int errcode; /* Error from last read. */
1046 /* First try a complete read. */
1047 errcode = target_read_memory (memaddr, myaddr, len);
1055 /* Loop, reading one byte at a time until we get as much as we can. */
1056 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1058 errcode = target_read_memory (memaddr++, myaddr++, 1);
1060 /* If an error, the last read was unsuccessful, so adjust count. */
1066 if (errnoptr != NULL)
1068 *errnoptr = errcode;
1073 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1074 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1075 stops at the first null byte, otherwise printing proceeds (including null
1076 bytes) until either print_max or LEN characters have been printed,
1077 whichever is smaller. */
1079 /* FIXME: Use target_read_string. */
1082 val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream)
1084 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1085 int errcode; /* Errno returned from bad reads. */
1086 unsigned int fetchlimit; /* Maximum number of chars to print. */
1087 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1088 unsigned int chunksize; /* Size of each fetch, in chars. */
1089 char *buffer = NULL; /* Dynamically growable fetch buffer. */
1090 char *bufptr; /* Pointer to next available byte in buffer. */
1091 char *limit; /* First location past end of fetch buffer. */
1092 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1093 int found_nul; /* Non-zero if we found the nul char */
1095 /* First we need to figure out the limit on the number of characters we are
1096 going to attempt to fetch and print. This is actually pretty simple. If
1097 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1098 LEN is -1, then the limit is print_max. This is true regardless of
1099 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1100 because finding the null byte (or available memory) is what actually
1101 limits the fetch. */
1103 fetchlimit = (len == -1 ? print_max : min (len, print_max));
1105 /* Now decide how large of chunks to try to read in one operation. This
1106 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1107 so we might as well read them all in one operation. If LEN is -1, we
1108 are looking for a null terminator to end the fetching, so we might as
1109 well read in blocks that are large enough to be efficient, but not so
1110 large as to be slow if fetchlimit happens to be large. So we choose the
1111 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1112 200 is way too big for remote debugging over a serial line. */
1114 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1116 /* Loop until we either have all the characters to print, or we encounter
1117 some error, such as bumping into the end of the address space. */
1120 old_chain = make_cleanup (null_cleanup, 0);
1124 buffer = (char *) xmalloc (len * width);
1126 old_chain = make_cleanup (xfree, buffer);
1128 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1130 addr += nfetch * width;
1131 bufptr += nfetch * width;
1135 unsigned long bufsize = 0;
1139 nfetch = min (chunksize, fetchlimit - bufsize);
1142 buffer = (char *) xmalloc (nfetch * width);
1145 discard_cleanups (old_chain);
1146 buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1149 old_chain = make_cleanup (xfree, buffer);
1150 bufptr = buffer + bufsize * width;
1153 /* Read as much as we can. */
1154 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1157 /* Scan this chunk for the null byte that terminates the string
1158 to print. If found, we don't need to fetch any more. Note
1159 that bufptr is explicitly left pointing at the next character
1160 after the null byte, or at the next character after the end of
1163 limit = bufptr + nfetch * width;
1164 while (bufptr < limit)
1168 c = extract_unsigned_integer (bufptr, width);
1173 /* We don't care about any error which happened after
1174 the NULL terminator. */
1181 while (errcode == 0 /* no error */
1182 && bufptr - buffer < fetchlimit * width /* no overrun */
1183 && !found_nul); /* haven't found nul yet */
1186 { /* length of string is really 0! */
1187 buffer = bufptr = NULL;
1191 /* bufptr and addr now point immediately beyond the last byte which we
1192 consider part of the string (including a '\0' which ends the string). */
1194 /* We now have either successfully filled the buffer to fetchlimit, or
1195 terminated early due to an error or finding a null char when LEN is -1. */
1197 if (len == -1 && !found_nul)
1201 /* We didn't find a null terminator we were looking for. Attempt
1202 to peek at the next character. If not successful, or it is not
1203 a null byte, then force ellipsis to be printed. */
1205 peekbuf = (char *) alloca (width);
1207 if (target_read_memory (addr, peekbuf, width) == 0
1208 && extract_unsigned_integer (peekbuf, width) != 0)
1211 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1213 /* Getting an error when we have a requested length, or fetching less
1214 than the number of characters actually requested, always make us
1221 /* If we get an error before fetching anything, don't print a string.
1222 But if we fetch something and then get an error, print the string
1223 and then the error message. */
1224 if (errcode == 0 || bufptr > buffer)
1228 fputs_filtered (" ", stream);
1230 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
1237 fprintf_filtered (stream, " <Address ");
1238 deprecated_print_address_numeric (addr, 1, stream);
1239 fprintf_filtered (stream, " out of bounds>");
1243 fprintf_filtered (stream, " <Error reading address ");
1244 deprecated_print_address_numeric (addr, 1, stream);
1245 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1249 do_cleanups (old_chain);
1250 return ((bufptr - buffer) / width);
1254 /* Validate an input or output radix setting, and make sure the user
1255 knows what they really did here. Radix setting is confusing, e.g.
1256 setting the input radix to "10" never changes it! */
1259 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1261 set_input_radix_1 (from_tty, input_radix);
1265 set_input_radix_1 (int from_tty, unsigned radix)
1267 /* We don't currently disallow any input radix except 0 or 1, which don't
1268 make any mathematical sense. In theory, we can deal with any input
1269 radix greater than 1, even if we don't have unique digits for every
1270 value from 0 to radix-1, but in practice we lose on large radix values.
1271 We should either fix the lossage or restrict the radix range more.
1276 /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1278 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
1281 input_radix = radix;
1284 printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
1285 radix, radix, radix);
1290 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1292 set_output_radix_1 (from_tty, output_radix);
1296 set_output_radix_1 (int from_tty, unsigned radix)
1298 /* Validate the radix and disallow ones that we aren't prepared to
1299 handle correctly, leaving the radix unchanged. */
1303 output_format = 'x'; /* hex */
1306 output_format = 0; /* decimal */
1309 output_format = 'o'; /* octal */
1312 /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1314 error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
1317 output_radix = radix;
1320 printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
1321 radix, radix, radix);
1325 /* Set both the input and output radix at once. Try to set the output radix
1326 first, since it has the most restrictive range. An radix that is valid as
1327 an output radix is also valid as an input radix.
1329 It may be useful to have an unusual input radix. If the user wishes to
1330 set an input radix that is not valid as an output radix, he needs to use
1331 the 'set input-radix' command. */
1334 set_radix (char *arg, int from_tty)
1338 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1339 set_output_radix_1 (0, radix);
1340 set_input_radix_1 (0, radix);
1343 printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
1344 radix, radix, radix);
1348 /* Show both the input and output radices. */
1351 show_radix (char *arg, int from_tty)
1355 if (input_radix == output_radix)
1357 printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
1358 input_radix, input_radix, input_radix);
1362 printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
1363 input_radix, input_radix, input_radix);
1364 printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
1365 output_radix, output_radix, output_radix);
1372 set_print (char *arg, int from_tty)
1375 "\"set print\" must be followed by the name of a print subcommand.\n");
1376 help_list (setprintlist, "set print ", -1, gdb_stdout);
1380 show_print (char *args, int from_tty)
1382 cmd_show_list (showprintlist, from_tty, "");
1386 _initialize_valprint (void)
1388 struct cmd_list_element *c;
1390 add_prefix_cmd ("print", no_class, set_print,
1391 _("Generic command for setting how things print."),
1392 &setprintlist, "set print ", 0, &setlist);
1393 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1394 /* prefer set print to set prompt */
1395 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1397 add_prefix_cmd ("print", no_class, show_print,
1398 _("Generic command for showing print settings."),
1399 &showprintlist, "show print ", 0, &showlist);
1400 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1401 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1403 add_setshow_uinteger_cmd ("elements", no_class, &print_max, _("\
1404 Set limit on string chars or array elements to print."), _("\
1405 Show limit on string chars or array elements to print."), _("\
1406 \"set print elements 0\" causes there to be no limit."),
1409 &setprintlist, &showprintlist);
1411 add_setshow_boolean_cmd ("null-stop", no_class, &stop_print_at_null, _("\
1412 Set printing of char arrays to stop at first null char."), _("\
1413 Show printing of char arrays to stop at first null char."), NULL,
1415 show_stop_print_at_null,
1416 &setprintlist, &showprintlist);
1418 add_setshow_uinteger_cmd ("repeats", no_class,
1419 &repeat_count_threshold, _("\
1420 Set threshold for repeated print elements."), _("\
1421 Show threshold for repeated print elements."), _("\
1422 \"set print repeats 0\" causes all elements to be individually printed."),
1424 show_repeat_count_threshold,
1425 &setprintlist, &showprintlist);
1427 add_setshow_boolean_cmd ("pretty", class_support, &prettyprint_structs, _("\
1428 Set prettyprinting of structures."), _("\
1429 Show prettyprinting of structures."), NULL,
1431 show_prettyprint_structs,
1432 &setprintlist, &showprintlist);
1434 add_setshow_boolean_cmd ("union", class_support, &unionprint, _("\
1435 Set printing of unions interior to structures."), _("\
1436 Show printing of unions interior to structures."), NULL,
1439 &setprintlist, &showprintlist);
1441 add_setshow_boolean_cmd ("array", class_support, &prettyprint_arrays, _("\
1442 Set prettyprinting of arrays."), _("\
1443 Show prettyprinting of arrays."), NULL,
1445 show_prettyprint_arrays,
1446 &setprintlist, &showprintlist);
1448 add_setshow_boolean_cmd ("address", class_support, &addressprint, _("\
1449 Set printing of addresses."), _("\
1450 Show printing of addresses."), NULL,
1453 &setprintlist, &showprintlist);
1455 add_setshow_uinteger_cmd ("input-radix", class_support, &input_radix, _("\
1456 Set default input radix for entering numbers."), _("\
1457 Show default input radix for entering numbers."), NULL,
1460 &setlist, &showlist);
1462 add_setshow_uinteger_cmd ("output-radix", class_support, &output_radix, _("\
1463 Set default output radix for printing of values."), _("\
1464 Show default output radix for printing of values."), NULL,
1467 &setlist, &showlist);
1469 /* The "set radix" and "show radix" commands are special in that
1470 they are like normal set and show commands but allow two normally
1471 independent variables to be either set or shown with a single
1472 command. So the usual deprecated_add_set_cmd() and [deleted]
1473 add_show_from_set() commands aren't really appropriate. */
1474 /* FIXME: i18n: With the new add_setshow_integer command, that is no
1475 longer true - show can display anything. */
1476 add_cmd ("radix", class_support, set_radix, _("\
1477 Set default input and output number radices.\n\
1478 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1479 Without an argument, sets both radices back to the default value of 10."),
1481 add_cmd ("radix", class_support, show_radix, _("\
1482 Show the default input and output number radices.\n\
1483 Use 'show input-radix' or 'show output-radix' to independently show each."),
1486 add_setshow_boolean_cmd ("array-indexes", class_support,
1487 &print_array_indexes, _("\
1488 Set printing of array indexes."), _("\
1489 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1490 &setprintlist, &showprintlist);
1492 /* Give people the defaults which they are used to. */
1493 prettyprint_structs = 0;
1494 prettyprint_arrays = 0;
1497 print_max = PRINT_MAX_DEFAULT;