1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
33 #include "floatformat.h"
35 #include "exceptions.h"
40 /* Prototypes for local functions */
42 static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *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 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
65 struct value_print_options user_print_options =
67 Val_pretty_default, /* pretty */
68 0, /* prettyprint_arrays */
69 0, /* prettyprint_structs */
74 PRINT_MAX_DEFAULT, /* print_max */
75 10, /* repeat_count_threshold */
76 0, /* output_format */
78 0, /* stop_print_at_null */
80 0, /* print_array_indexes */
82 1, /* static_field_print */
83 1 /* pascal_static_field_print */
86 /* Initialize *OPTS to be a copy of the user print options. */
88 get_user_print_options (struct value_print_options *opts)
90 *opts = user_print_options;
93 /* Initialize *OPTS to be a copy of the user print options, but with
94 pretty-printing disabled. */
96 get_raw_print_options (struct value_print_options *opts)
98 *opts = user_print_options;
99 opts->pretty = Val_no_prettyprint;
102 /* Initialize *OPTS to be a copy of the user print options, but using
103 FORMAT as the formatting option. */
105 get_formatted_print_options (struct value_print_options *opts,
108 *opts = user_print_options;
109 opts->format = format;
113 show_print_max (struct ui_file *file, int from_tty,
114 struct cmd_list_element *c, const char *value)
116 fprintf_filtered (file, _("\
117 Limit on string chars or array elements to print is %s.\n"),
122 /* Default input and output radixes, and output format letter. */
124 unsigned input_radix = 10;
126 show_input_radix (struct ui_file *file, int from_tty,
127 struct cmd_list_element *c, const char *value)
129 fprintf_filtered (file, _("\
130 Default input radix for entering numbers is %s.\n"),
134 unsigned output_radix = 10;
136 show_output_radix (struct ui_file *file, int from_tty,
137 struct cmd_list_element *c, const char *value)
139 fprintf_filtered (file, _("\
140 Default output radix for printing of values is %s.\n"),
144 /* By default we print arrays without printing the index of each element in
145 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
148 show_print_array_indexes (struct ui_file *file, int from_tty,
149 struct cmd_list_element *c, const char *value)
151 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
154 /* Print repeat counts if there are more than this many repetitions of an
155 element in an array. Referenced by the low level language dependent
159 show_repeat_count_threshold (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
162 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
166 /* If nonzero, stops printing of char arrays at first null. */
169 show_stop_print_at_null (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
172 fprintf_filtered (file, _("\
173 Printing of char arrays to stop at first null char is %s.\n"),
177 /* Controls pretty printing of structures. */
180 show_prettyprint_structs (struct ui_file *file, int from_tty,
181 struct cmd_list_element *c, const char *value)
183 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
186 /* Controls pretty printing of arrays. */
189 show_prettyprint_arrays (struct ui_file *file, int from_tty,
190 struct cmd_list_element *c, const char *value)
192 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
195 /* If nonzero, causes unions inside structures or other unions to be
199 show_unionprint (struct ui_file *file, int from_tty,
200 struct cmd_list_element *c, const char *value)
202 fprintf_filtered (file, _("\
203 Printing of unions interior to structures is %s.\n"),
207 /* If nonzero, causes machine addresses to be printed in certain contexts. */
210 show_addressprint (struct ui_file *file, int from_tty,
211 struct cmd_list_element *c, const char *value)
213 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
217 /* Print using the given LANGUAGE the data of type TYPE located at VALADDR
218 (within GDB), which came from the inferior at address ADDRESS, onto
219 stdio stream STREAM according to OPTIONS.
221 If the data are a string pointer, returns the number of string characters
224 FIXME: The data at VALADDR is in target byte order. If gdb is ever
225 enhanced to be able to debug more than the single target it was compiled
226 for (specific CPU type and thus specific target byte ordering), then
227 either the print routines are going to have to take this into account,
228 or the data is going to have to be passed into here already converted
229 to the host byte ordering, whichever is more convenient. */
233 val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
234 CORE_ADDR address, struct ui_file *stream, int recurse,
235 const struct value_print_options *options,
236 const struct language_defn *language)
238 volatile struct gdb_exception except;
240 struct value_print_options local_opts = *options;
241 struct type *real_type = check_typedef (type);
243 if (local_opts.pretty == Val_pretty_default)
244 local_opts.pretty = (local_opts.prettyprint_structs
245 ? Val_prettyprint : Val_no_prettyprint);
249 /* Ensure that the type is complete and not just a stub. If the type is
250 only a stub and we can't find and substitute its complete type, then
251 print appropriate string and return. */
253 if (TYPE_STUB (real_type))
255 fprintf_filtered (stream, "<incomplete type>");
260 TRY_CATCH (except, RETURN_MASK_ERROR)
262 ret = language->la_val_print (type, valaddr, embedded_offset, address,
263 stream, recurse, &local_opts);
265 if (except.reason < 0)
266 fprintf_filtered (stream, _("<error reading variable>"));
271 /* Check whether the value VAL is printable. Return 1 if it is;
272 return 0 and print an appropriate error message to STREAM if it
276 value_check_printable (struct value *val, struct ui_file *stream)
280 fprintf_filtered (stream, _("<address of value unknown>"));
284 if (value_optimized_out (val))
286 fprintf_filtered (stream, _("<value optimized out>"));
293 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
296 If the data are a string pointer, returns the number of string characters
299 This is a preferable interface to val_print, above, because it uses
300 GDB's value mechanism. */
303 common_val_print (struct value *val, struct ui_file *stream, int recurse,
304 const struct value_print_options *options,
305 const struct language_defn *language)
307 if (!value_check_printable (val, stream))
310 return val_print (value_type (val), value_contents_all (val),
311 value_embedded_offset (val), VALUE_ADDRESS (val),
312 stream, recurse, options, language);
315 /* Print the value VAL in C-ish syntax on stream STREAM according to
317 If the object printed is a string pointer, returns
318 the number of string bytes printed. */
321 value_print (struct value *val, struct ui_file *stream,
322 const struct value_print_options *options)
324 if (!value_check_printable (val, stream))
327 return LA_VALUE_PRINT (val, stream, options);
330 /* Called by various <lang>_val_print routines to print
331 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
332 value. STREAM is where to print the value. */
335 val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
336 struct ui_file *stream)
338 enum bfd_endian byte_order = gdbarch_byte_order (current_gdbarch);
340 if (TYPE_LENGTH (type) > sizeof (LONGEST))
344 if (TYPE_UNSIGNED (type)
345 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
348 print_longest (stream, 'u', 0, val);
352 /* Signed, or we couldn't turn an unsigned value into a
353 LONGEST. For signed values, one could assume two's
354 complement (a reasonable assumption, I think) and do
356 print_hex_chars (stream, (unsigned char *) valaddr,
357 TYPE_LENGTH (type), byte_order);
362 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
363 unpack_long (type, valaddr));
368 val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
369 struct ui_file *stream)
371 ULONGEST val = unpack_long (type, valaddr);
372 int bitpos, nfields = TYPE_NFIELDS (type);
374 fputs_filtered ("[ ", stream);
375 for (bitpos = 0; bitpos < nfields; bitpos++)
377 if (TYPE_FIELD_BITPOS (type, bitpos) != -1
378 && (val & ((ULONGEST)1 << bitpos)))
380 if (TYPE_FIELD_NAME (type, bitpos))
381 fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
383 fprintf_filtered (stream, "#%d ", bitpos);
386 fputs_filtered ("]", stream);
389 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
390 The raison d'etre of this function is to consolidate printing of
391 LONG_LONG's into this one function. The format chars b,h,w,g are
392 from print_scalar_formatted(). Numbers are printed using C
395 USE_C_FORMAT means to use C format in all cases. Without it,
396 'o' and 'x' format do not include the standard C radix prefix
399 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
400 and was intended to request formating according to the current
401 language and would be used for most integers that GDB prints. The
402 exceptional cases were things like protocols where the format of
403 the integer is a protocol thing, not a user-visible thing). The
404 parameter remains to preserve the information of what things might
405 be printed with language-specific format, should we ever resurrect
409 print_longest (struct ui_file *stream, int format, int use_c_format,
417 val = int_string (val_long, 10, 1, 0, 1); break;
419 val = int_string (val_long, 10, 0, 0, 1); break;
421 val = int_string (val_long, 16, 0, 0, use_c_format); break;
423 val = int_string (val_long, 16, 0, 2, 1); break;
425 val = int_string (val_long, 16, 0, 4, 1); break;
427 val = int_string (val_long, 16, 0, 8, 1); break;
429 val = int_string (val_long, 16, 0, 16, 1); break;
432 val = int_string (val_long, 8, 0, 0, use_c_format); break;
434 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
436 fputs_filtered (val, stream);
439 /* This used to be a macro, but I don't think it is called often enough
440 to merit such treatment. */
441 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
442 arguments to a function, number in a value history, register number, etc.)
443 where the value must not be larger than can fit in an int. */
446 longest_to_int (LONGEST arg)
448 /* Let the compiler do the work */
449 int rtnval = (int) arg;
451 /* Check for overflows or underflows */
452 if (sizeof (LONGEST) > sizeof (int))
456 error (_("Value out of range."));
462 /* Print a floating point value of type TYPE (not always a
463 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
466 print_floating (const gdb_byte *valaddr, struct type *type,
467 struct ui_file *stream)
471 const struct floatformat *fmt = NULL;
472 unsigned len = TYPE_LENGTH (type);
473 enum float_kind kind;
475 /* If it is a floating-point, check for obvious problems. */
476 if (TYPE_CODE (type) == TYPE_CODE_FLT)
477 fmt = floatformat_from_type (type);
480 kind = floatformat_classify (fmt, valaddr);
481 if (kind == float_nan)
483 if (floatformat_is_negative (fmt, valaddr))
484 fprintf_filtered (stream, "-");
485 fprintf_filtered (stream, "nan(");
486 fputs_filtered ("0x", stream);
487 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
488 fprintf_filtered (stream, ")");
491 else if (kind == float_infinite)
493 if (floatformat_is_negative (fmt, valaddr))
494 fputs_filtered ("-", stream);
495 fputs_filtered ("inf", stream);
500 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
501 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
502 needs to be used as that takes care of any necessary type
503 conversions. Such conversions are of course direct to DOUBLEST
504 and disregard any possible target floating point limitations.
505 For instance, a u64 would be converted and displayed exactly on a
506 host with 80 bit DOUBLEST but with loss of information on a host
507 with 64 bit DOUBLEST. */
509 doub = unpack_double (type, valaddr, &inv);
512 fprintf_filtered (stream, "<invalid float value>");
516 /* FIXME: kettenis/2001-01-20: The following code makes too much
517 assumptions about the host and target floating point format. */
519 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
520 not necessarily be a TYPE_CODE_FLT, the below ignores that and
521 instead uses the type's length to determine the precision of the
522 floating-point value being printed. */
524 if (len < sizeof (double))
525 fprintf_filtered (stream, "%.9g", (double) doub);
526 else if (len == sizeof (double))
527 fprintf_filtered (stream, "%.17g", (double) doub);
529 #ifdef PRINTF_HAS_LONG_DOUBLE
530 fprintf_filtered (stream, "%.35Lg", doub);
532 /* This at least wins with values that are representable as
534 fprintf_filtered (stream, "%.17g", (double) doub);
539 print_decimal_floating (const gdb_byte *valaddr, struct type *type,
540 struct ui_file *stream)
542 char decstr[MAX_DECIMAL_STRING];
543 unsigned len = TYPE_LENGTH (type);
545 decimal_to_string (valaddr, len, decstr);
546 fputs_filtered (decstr, stream);
551 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
552 unsigned len, enum bfd_endian byte_order)
555 #define BITS_IN_BYTES 8
561 /* Declared "int" so it will be signed.
562 * This ensures that right shift will shift in zeros.
564 const int mask = 0x080;
566 /* FIXME: We should be not printing leading zeroes in most cases. */
568 if (byte_order == BFD_ENDIAN_BIG)
574 /* Every byte has 8 binary characters; peel off
575 * and print from the MSB end.
577 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
579 if (*p & (mask >> i))
584 fprintf_filtered (stream, "%1d", b);
590 for (p = valaddr + len - 1;
594 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
596 if (*p & (mask >> i))
601 fprintf_filtered (stream, "%1d", b);
607 /* VALADDR points to an integer of LEN bytes.
608 * Print it in octal on stream or format it in buf.
611 print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
612 unsigned len, enum bfd_endian byte_order)
615 unsigned char octa1, octa2, octa3, carry;
618 /* FIXME: We should be not printing leading zeroes in most cases. */
621 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
622 * the extra bits, which cycle every three bytes:
626 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
628 * Octal side: 0 1 carry 3 4 carry ...
630 * Cycle number: 0 1 2
632 * But of course we are printing from the high side, so we have to
633 * figure out where in the cycle we are so that we end up with no
634 * left over bits at the end.
636 #define BITS_IN_OCTAL 3
637 #define HIGH_ZERO 0340
638 #define LOW_ZERO 0016
639 #define CARRY_ZERO 0003
640 #define HIGH_ONE 0200
643 #define CARRY_ONE 0001
644 #define HIGH_TWO 0300
648 /* For 32 we start in cycle 2, with two bits and one bit carry;
649 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
651 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
654 fputs_filtered ("0", stream);
655 if (byte_order == BFD_ENDIAN_BIG)
664 /* No carry in, carry out two bits.
666 octa1 = (HIGH_ZERO & *p) >> 5;
667 octa2 = (LOW_ZERO & *p) >> 2;
668 carry = (CARRY_ZERO & *p);
669 fprintf_filtered (stream, "%o", octa1);
670 fprintf_filtered (stream, "%o", octa2);
674 /* Carry in two bits, carry out one bit.
676 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
677 octa2 = (MID_ONE & *p) >> 4;
678 octa3 = (LOW_ONE & *p) >> 1;
679 carry = (CARRY_ONE & *p);
680 fprintf_filtered (stream, "%o", octa1);
681 fprintf_filtered (stream, "%o", octa2);
682 fprintf_filtered (stream, "%o", octa3);
686 /* Carry in one bit, no carry out.
688 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
689 octa2 = (MID_TWO & *p) >> 3;
690 octa3 = (LOW_TWO & *p);
692 fprintf_filtered (stream, "%o", octa1);
693 fprintf_filtered (stream, "%o", octa2);
694 fprintf_filtered (stream, "%o", octa3);
698 error (_("Internal error in octal conversion;"));
702 cycle = cycle % BITS_IN_OCTAL;
707 for (p = valaddr + len - 1;
714 /* Carry out, no carry in */
715 octa1 = (HIGH_ZERO & *p) >> 5;
716 octa2 = (LOW_ZERO & *p) >> 2;
717 carry = (CARRY_ZERO & *p);
718 fprintf_filtered (stream, "%o", octa1);
719 fprintf_filtered (stream, "%o", octa2);
723 /* Carry in, carry out */
724 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
725 octa2 = (MID_ONE & *p) >> 4;
726 octa3 = (LOW_ONE & *p) >> 1;
727 carry = (CARRY_ONE & *p);
728 fprintf_filtered (stream, "%o", octa1);
729 fprintf_filtered (stream, "%o", octa2);
730 fprintf_filtered (stream, "%o", octa3);
734 /* Carry in, no carry out */
735 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
736 octa2 = (MID_TWO & *p) >> 3;
737 octa3 = (LOW_TWO & *p);
739 fprintf_filtered (stream, "%o", octa1);
740 fprintf_filtered (stream, "%o", octa2);
741 fprintf_filtered (stream, "%o", octa3);
745 error (_("Internal error in octal conversion;"));
749 cycle = cycle % BITS_IN_OCTAL;
755 /* VALADDR points to an integer of LEN bytes.
756 * Print it in decimal on stream or format it in buf.
759 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
760 unsigned len, enum bfd_endian byte_order)
763 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
764 #define CARRY_LEFT( x ) ((x) % TEN)
765 #define SHIFT( x ) ((x) << 4)
766 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
767 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
770 unsigned char *digits;
773 int i, j, decimal_digits;
777 /* Base-ten number is less than twice as many digits
778 * as the base 16 number, which is 2 digits per byte.
780 decimal_len = len * 2 * 2;
781 digits = xmalloc (decimal_len);
783 for (i = 0; i < decimal_len; i++)
788 /* Ok, we have an unknown number of bytes of data to be printed in
791 * Given a hex number (in nibbles) as XYZ, we start by taking X and
792 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
793 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
795 * The trick is that "digits" holds a base-10 number, but sometimes
796 * the individual digits are > 10.
798 * Outer loop is per nibble (hex digit) of input, from MSD end to
801 decimal_digits = 0; /* Number of decimal digits so far */
802 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
804 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
807 * Multiply current base-ten number by 16 in place.
808 * Each digit was between 0 and 9, now is between
811 for (j = 0; j < decimal_digits; j++)
813 digits[j] = SHIFT (digits[j]);
816 /* Take the next nibble off the input and add it to what
817 * we've got in the LSB position. Bottom 'digit' is now
820 * "flip" is used to run this loop twice for each byte.
826 digits[0] += HIGH_NIBBLE (*p);
831 /* Take low nibble and bump our pointer "p".
833 digits[0] += LOW_NIBBLE (*p);
834 if (byte_order == BFD_ENDIAN_BIG)
841 /* Re-decimalize. We have to do this often enough
842 * that we don't overflow, but once per nibble is
843 * overkill. Easier this way, though. Note that the
844 * carry is often larger than 10 (e.g. max initial
845 * carry out of lowest nibble is 15, could bubble all
846 * the way up greater than 10). So we have to do
847 * the carrying beyond the last current digit.
850 for (j = 0; j < decimal_len - 1; j++)
854 /* "/" won't handle an unsigned char with
855 * a value that if signed would be negative.
856 * So extend to longword int via "dummy".
859 carry = CARRY_OUT (dummy);
860 digits[j] = CARRY_LEFT (dummy);
862 if (j >= decimal_digits && carry == 0)
865 * All higher digits are 0 and we
866 * no longer have a carry.
868 * Note: "j" is 0-based, "decimal_digits" is
871 decimal_digits = j + 1;
877 /* Ok, now "digits" is the decimal representation, with
878 * the "decimal_digits" actual digits. Print!
880 for (i = decimal_digits - 1; i >= 0; i--)
882 fprintf_filtered (stream, "%1d", digits[i]);
887 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
890 print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
891 unsigned len, enum bfd_endian byte_order)
895 /* FIXME: We should be not printing leading zeroes in most cases. */
897 fputs_filtered ("0x", stream);
898 if (byte_order == BFD_ENDIAN_BIG)
904 fprintf_filtered (stream, "%02x", *p);
909 for (p = valaddr + len - 1;
913 fprintf_filtered (stream, "%02x", *p);
918 /* VALADDR points to a char integer of LEN bytes. Print it out in appropriate language form on stream.
919 Omit any leading zero chars. */
922 print_char_chars (struct ui_file *stream, const gdb_byte *valaddr,
923 unsigned len, enum bfd_endian byte_order)
927 if (byte_order == BFD_ENDIAN_BIG)
930 while (p < valaddr + len - 1 && *p == 0)
933 while (p < valaddr + len)
935 LA_EMIT_CHAR (*p, stream, '\'');
941 p = valaddr + len - 1;
942 while (p > valaddr && *p == 0)
947 LA_EMIT_CHAR (*p, stream, '\'');
953 /* Assuming TYPE is a simple, non-empty array type, compute its upper
954 and lower bound. Save the low bound into LOW_BOUND if not NULL.
955 Save the high bound into HIGH_BOUND if not NULL.
957 Return 1 if the operation was successful. Return zero otherwise,
958 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
960 Computing the array upper and lower bounds is pretty easy, but this
961 function does some additional verifications before returning them.
962 If something incorrect is detected, it is better to return a status
963 rather than throwing an error, making it easier for the caller to
964 implement an error-recovery plan. For instance, it may decide to
965 warn the user that the bounds were not found and then use some
966 default values instead. */
969 get_array_bounds (struct type *type, long *low_bound, long *high_bound)
971 struct type *index = TYPE_INDEX_TYPE (type);
978 if (TYPE_CODE (index) == TYPE_CODE_RANGE)
980 low = TYPE_LOW_BOUND (index);
981 high = TYPE_HIGH_BOUND (index);
983 else if (TYPE_CODE (index) == TYPE_CODE_ENUM)
985 const int n_enums = TYPE_NFIELDS (index);
987 low = TYPE_FIELD_BITPOS (index, 0);
988 high = TYPE_FIELD_BITPOS (index, n_enums - 1);
993 /* Abort if the lower bound is greater than the higher bound, except
994 when low = high + 1. This is a very common idiom used in Ada when
995 defining empty ranges (for instance "range 1 .. 0"). */
1008 /* Print on STREAM using the given OPTIONS the index for the element
1009 at INDEX of an array whose index type is INDEX_TYPE. */
1012 maybe_print_array_index (struct type *index_type, LONGEST index,
1013 struct ui_file *stream,
1014 const struct value_print_options *options)
1016 struct value *index_value;
1018 if (!options->print_array_indexes)
1021 index_value = value_from_longest (index_type, index);
1023 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1026 /* Called by various <lang>_val_print routines to print elements of an
1027 array in the form "<elem1>, <elem2>, <elem3>, ...".
1029 (FIXME?) Assumes array element separator is a comma, which is correct
1030 for all languages currently handled.
1031 (FIXME?) Some languages have a notation for repeated array elements,
1032 perhaps we should try to use that notation when appropriate.
1036 val_print_array_elements (struct type *type, const gdb_byte *valaddr,
1037 CORE_ADDR address, struct ui_file *stream,
1039 const struct value_print_options *options,
1042 unsigned int things_printed = 0;
1044 struct type *elttype, *index_type;
1046 /* Position of the array element we are examining to see
1047 whether it is repeated. */
1049 /* Number of repetitions we have detected so far. */
1051 long low_bound_index = 0;
1053 elttype = TYPE_TARGET_TYPE (type);
1054 eltlen = TYPE_LENGTH (check_typedef (elttype));
1055 index_type = TYPE_INDEX_TYPE (type);
1057 /* Compute the number of elements in the array. On most arrays,
1058 the size of its elements is not zero, and so the number of elements
1059 is simply the size of the array divided by the size of the elements.
1060 But for arrays of elements whose size is zero, we need to look at
1063 len = TYPE_LENGTH (type) / eltlen;
1067 if (get_array_bounds (type, &low, &hi))
1071 warning (_("unable to get bounds of array, assuming null array"));
1076 /* Get the array low bound. This only makes sense if the array
1077 has one or more element in it. */
1078 if (len > 0 && !get_array_bounds (type, &low_bound_index, NULL))
1080 warning (_("unable to get low bound of array, using zero as default"));
1081 low_bound_index = 0;
1084 annotate_array_section_begin (i, elttype);
1086 for (; i < len && things_printed < options->print_max; i++)
1090 if (options->prettyprint_arrays)
1092 fprintf_filtered (stream, ",\n");
1093 print_spaces_filtered (2 + 2 * recurse, stream);
1097 fprintf_filtered (stream, ", ");
1100 wrap_here (n_spaces (2 + 2 * recurse));
1101 maybe_print_array_index (index_type, i + low_bound_index,
1106 while ((rep1 < len) &&
1107 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1113 if (reps > options->repeat_count_threshold)
1115 val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1116 stream, recurse + 1, options, current_language);
1117 annotate_elt_rep (reps);
1118 fprintf_filtered (stream, " <repeats %u times>", reps);
1119 annotate_elt_rep_end ();
1122 things_printed += options->repeat_count_threshold;
1126 val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1127 stream, recurse + 1, options, current_language);
1132 annotate_array_section_end ();
1135 fprintf_filtered (stream, "...");
1139 /* Read LEN bytes of target memory at address MEMADDR, placing the
1140 results in GDB's memory at MYADDR. Returns a count of the bytes
1141 actually read, and optionally an errno value in the location
1142 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1144 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1145 function be eliminated. */
1148 partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int *errnoptr)
1150 int nread; /* Number of bytes actually read. */
1151 int errcode; /* Error from last read. */
1153 /* First try a complete read. */
1154 errcode = target_read_memory (memaddr, myaddr, len);
1162 /* Loop, reading one byte at a time until we get as much as we can. */
1163 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1165 errcode = target_read_memory (memaddr++, myaddr++, 1);
1167 /* If an error, the last read was unsuccessful, so adjust count. */
1173 if (errnoptr != NULL)
1175 *errnoptr = errcode;
1180 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1181 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1182 stops at the first null byte, otherwise printing proceeds (including null
1183 bytes) until either print_max or LEN characters have been printed,
1184 whichever is smaller. */
1186 /* FIXME: Use target_read_string. */
1189 val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream,
1190 const struct value_print_options *options)
1192 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1193 int errcode; /* Errno returned from bad reads. */
1194 unsigned int fetchlimit; /* Maximum number of chars to print. */
1195 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1196 unsigned int chunksize; /* Size of each fetch, in chars. */
1197 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
1198 gdb_byte *bufptr; /* Pointer to next available byte in buffer. */
1199 gdb_byte *limit; /* First location past end of fetch buffer. */
1200 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1201 int found_nul; /* Non-zero if we found the nul char */
1203 /* First we need to figure out the limit on the number of characters we are
1204 going to attempt to fetch and print. This is actually pretty simple. If
1205 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1206 LEN is -1, then the limit is print_max. This is true regardless of
1207 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1208 because finding the null byte (or available memory) is what actually
1209 limits the fetch. */
1211 fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
1213 /* Now decide how large of chunks to try to read in one operation. This
1214 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1215 so we might as well read them all in one operation. If LEN is -1, we
1216 are looking for a null terminator to end the fetching, so we might as
1217 well read in blocks that are large enough to be efficient, but not so
1218 large as to be slow if fetchlimit happens to be large. So we choose the
1219 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1220 200 is way too big for remote debugging over a serial line. */
1222 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1224 /* Loop until we either have all the characters to print, or we encounter
1225 some error, such as bumping into the end of the address space. */
1228 old_chain = make_cleanup (null_cleanup, 0);
1232 buffer = (gdb_byte *) xmalloc (len * width);
1234 old_chain = make_cleanup (xfree, buffer);
1236 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1238 addr += nfetch * width;
1239 bufptr += nfetch * width;
1243 unsigned long bufsize = 0;
1247 nfetch = min (chunksize, fetchlimit - bufsize);
1250 buffer = (gdb_byte *) xmalloc (nfetch * width);
1253 discard_cleanups (old_chain);
1254 buffer = (gdb_byte *) xrealloc (buffer, (nfetch + bufsize) * width);
1257 old_chain = make_cleanup (xfree, buffer);
1258 bufptr = buffer + bufsize * width;
1261 /* Read as much as we can. */
1262 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1265 /* Scan this chunk for the null byte that terminates the string
1266 to print. If found, we don't need to fetch any more. Note
1267 that bufptr is explicitly left pointing at the next character
1268 after the null byte, or at the next character after the end of
1271 limit = bufptr + nfetch * width;
1272 while (bufptr < limit)
1276 c = extract_unsigned_integer (bufptr, width);
1281 /* We don't care about any error which happened after
1282 the NULL terminator. */
1289 while (errcode == 0 /* no error */
1290 && bufptr - buffer < fetchlimit * width /* no overrun */
1291 && !found_nul); /* haven't found nul yet */
1294 { /* length of string is really 0! */
1295 buffer = bufptr = NULL;
1299 /* bufptr and addr now point immediately beyond the last byte which we
1300 consider part of the string (including a '\0' which ends the string). */
1302 /* We now have either successfully filled the buffer to fetchlimit, or
1303 terminated early due to an error or finding a null char when LEN is -1. */
1305 if (len == -1 && !found_nul)
1309 /* We didn't find a null terminator we were looking for. Attempt
1310 to peek at the next character. If not successful, or it is not
1311 a null byte, then force ellipsis to be printed. */
1313 peekbuf = (gdb_byte *) alloca (width);
1315 if (target_read_memory (addr, peekbuf, width) == 0
1316 && extract_unsigned_integer (peekbuf, width) != 0)
1319 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1321 /* Getting an error when we have a requested length, or fetching less
1322 than the number of characters actually requested, always make us
1329 /* If we get an error before fetching anything, don't print a string.
1330 But if we fetch something and then get an error, print the string
1331 and then the error message. */
1332 if (errcode == 0 || bufptr > buffer)
1334 if (options->addressprint)
1336 fputs_filtered (" ", stream);
1338 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis, options);
1345 fprintf_filtered (stream, " <Address ");
1346 fputs_filtered (paddress (addr), stream);
1347 fprintf_filtered (stream, " out of bounds>");
1351 fprintf_filtered (stream, " <Error reading address ");
1352 fputs_filtered (paddress (addr), stream);
1353 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1357 do_cleanups (old_chain);
1358 return ((bufptr - buffer) / width);
1362 /* The 'set input-radix' command writes to this auxiliary variable.
1363 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
1364 it is left unchanged. */
1366 static unsigned input_radix_1 = 10;
1368 /* Validate an input or output radix setting, and make sure the user
1369 knows what they really did here. Radix setting is confusing, e.g.
1370 setting the input radix to "10" never changes it! */
1373 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1375 set_input_radix_1 (from_tty, input_radix_1);
1379 set_input_radix_1 (int from_tty, unsigned radix)
1381 /* We don't currently disallow any input radix except 0 or 1, which don't
1382 make any mathematical sense. In theory, we can deal with any input
1383 radix greater than 1, even if we don't have unique digits for every
1384 value from 0 to radix-1, but in practice we lose on large radix values.
1385 We should either fix the lossage or restrict the radix range more.
1390 input_radix_1 = input_radix;
1391 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
1394 input_radix_1 = input_radix = radix;
1397 printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
1398 radix, radix, radix);
1402 /* The 'set output-radix' command writes to this auxiliary variable.
1403 If the requested radix is valid, OUTPUT_RADIX is updated,
1404 otherwise, it is left unchanged. */
1406 static unsigned output_radix_1 = 10;
1409 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1411 set_output_radix_1 (from_tty, output_radix_1);
1415 set_output_radix_1 (int from_tty, unsigned radix)
1417 /* Validate the radix and disallow ones that we aren't prepared to
1418 handle correctly, leaving the radix unchanged. */
1422 user_print_options.output_format = 'x'; /* hex */
1425 user_print_options.output_format = 0; /* decimal */
1428 user_print_options.output_format = 'o'; /* octal */
1431 output_radix_1 = output_radix;
1432 error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
1435 output_radix_1 = output_radix = radix;
1438 printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
1439 radix, radix, radix);
1443 /* Set both the input and output radix at once. Try to set the output radix
1444 first, since it has the most restrictive range. An radix that is valid as
1445 an output radix is also valid as an input radix.
1447 It may be useful to have an unusual input radix. If the user wishes to
1448 set an input radix that is not valid as an output radix, he needs to use
1449 the 'set input-radix' command. */
1452 set_radix (char *arg, int from_tty)
1456 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1457 set_output_radix_1 (0, radix);
1458 set_input_radix_1 (0, radix);
1461 printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
1462 radix, radix, radix);
1466 /* Show both the input and output radices. */
1469 show_radix (char *arg, int from_tty)
1473 if (input_radix == output_radix)
1475 printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
1476 input_radix, input_radix, input_radix);
1480 printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
1481 input_radix, input_radix, input_radix);
1482 printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
1483 output_radix, output_radix, output_radix);
1490 set_print (char *arg, int from_tty)
1493 "\"set print\" must be followed by the name of a print subcommand.\n");
1494 help_list (setprintlist, "set print ", -1, gdb_stdout);
1498 show_print (char *args, int from_tty)
1500 cmd_show_list (showprintlist, from_tty, "");
1504 _initialize_valprint (void)
1506 struct cmd_list_element *c;
1508 add_prefix_cmd ("print", no_class, set_print,
1509 _("Generic command for setting how things print."),
1510 &setprintlist, "set print ", 0, &setlist);
1511 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1512 /* prefer set print to set prompt */
1513 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1515 add_prefix_cmd ("print", no_class, show_print,
1516 _("Generic command for showing print settings."),
1517 &showprintlist, "show print ", 0, &showlist);
1518 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1519 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1521 add_setshow_uinteger_cmd ("elements", no_class,
1522 &user_print_options.print_max, _("\
1523 Set limit on string chars or array elements to print."), _("\
1524 Show limit on string chars or array elements to print."), _("\
1525 \"set print elements 0\" causes there to be no limit."),
1528 &setprintlist, &showprintlist);
1530 add_setshow_boolean_cmd ("null-stop", no_class,
1531 &user_print_options.stop_print_at_null, _("\
1532 Set printing of char arrays to stop at first null char."), _("\
1533 Show printing of char arrays to stop at first null char."), NULL,
1535 show_stop_print_at_null,
1536 &setprintlist, &showprintlist);
1538 add_setshow_uinteger_cmd ("repeats", no_class,
1539 &user_print_options.repeat_count_threshold, _("\
1540 Set threshold for repeated print elements."), _("\
1541 Show threshold for repeated print elements."), _("\
1542 \"set print repeats 0\" causes all elements to be individually printed."),
1544 show_repeat_count_threshold,
1545 &setprintlist, &showprintlist);
1547 add_setshow_boolean_cmd ("pretty", class_support,
1548 &user_print_options.prettyprint_structs, _("\
1549 Set prettyprinting of structures."), _("\
1550 Show prettyprinting of structures."), NULL,
1552 show_prettyprint_structs,
1553 &setprintlist, &showprintlist);
1555 add_setshow_boolean_cmd ("union", class_support,
1556 &user_print_options.unionprint, _("\
1557 Set printing of unions interior to structures."), _("\
1558 Show printing of unions interior to structures."), NULL,
1561 &setprintlist, &showprintlist);
1563 add_setshow_boolean_cmd ("array", class_support,
1564 &user_print_options.prettyprint_arrays, _("\
1565 Set prettyprinting of arrays."), _("\
1566 Show prettyprinting of arrays."), NULL,
1568 show_prettyprint_arrays,
1569 &setprintlist, &showprintlist);
1571 add_setshow_boolean_cmd ("address", class_support,
1572 &user_print_options.addressprint, _("\
1573 Set printing of addresses."), _("\
1574 Show printing of addresses."), NULL,
1577 &setprintlist, &showprintlist);
1579 add_setshow_uinteger_cmd ("input-radix", class_support, &input_radix_1,
1581 Set default input radix for entering numbers."), _("\
1582 Show default input radix for entering numbers."), NULL,
1585 &setlist, &showlist);
1587 add_setshow_uinteger_cmd ("output-radix", class_support, &output_radix_1,
1589 Set default output radix for printing of values."), _("\
1590 Show default output radix for printing of values."), NULL,
1593 &setlist, &showlist);
1595 /* The "set radix" and "show radix" commands are special in that
1596 they are like normal set and show commands but allow two normally
1597 independent variables to be either set or shown with a single
1598 command. So the usual deprecated_add_set_cmd() and [deleted]
1599 add_show_from_set() commands aren't really appropriate. */
1600 /* FIXME: i18n: With the new add_setshow_integer command, that is no
1601 longer true - show can display anything. */
1602 add_cmd ("radix", class_support, set_radix, _("\
1603 Set default input and output number radices.\n\
1604 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1605 Without an argument, sets both radices back to the default value of 10."),
1607 add_cmd ("radix", class_support, show_radix, _("\
1608 Show the default input and output number radices.\n\
1609 Use 'show input-radix' or 'show output-radix' to independently show each."),
1612 add_setshow_boolean_cmd ("array-indexes", class_support,
1613 &user_print_options.print_array_indexes, _("\
1614 Set printing of array indexes."), _("\
1615 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1616 &setprintlist, &showprintlist);