]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/valprint.c
2009-02-27 Phil Muldoon <pmuldoon@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2
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 2009 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "target.h"
30 #include "language.h"
31 #include "annotate.h"
32 #include "valprint.h"
33 #include "floatformat.h"
34 #include "doublest.h"
35 #include "exceptions.h"
36 #include "dfp.h"
37
38 #include <errno.h>
39
40 /* Prototypes for local functions */
41
42 static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
43 int len, int *errnoptr);
44
45 static void show_print (char *, int);
46
47 static void set_print (char *, int);
48
49 static void set_radix (char *, int);
50
51 static void show_radix (char *, int);
52
53 static void set_input_radix (char *, int, struct cmd_list_element *);
54
55 static void set_input_radix_1 (int, unsigned);
56
57 static void set_output_radix (char *, int, struct cmd_list_element *);
58
59 static void set_output_radix_1 (int, unsigned);
60
61 void _initialize_valprint (void);
62
63 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
64
65 struct value_print_options user_print_options =
66 {
67 Val_pretty_default, /* pretty */
68 0, /* prettyprint_arrays */
69 0, /* prettyprint_structs */
70 0, /* vtblprint */
71 1, /* unionprint */
72 1, /* addressprint */
73 0, /* objectprint */
74 PRINT_MAX_DEFAULT, /* print_max */
75 10, /* repeat_count_threshold */
76 0, /* output_format */
77 0, /* format */
78 0, /* stop_print_at_null */
79 0, /* inspect_it */
80 0, /* print_array_indexes */
81 0, /* deref_ref */
82 1, /* static_field_print */
83 1 /* pascal_static_field_print */
84 };
85
86 /* Initialize *OPTS to be a copy of the user print options. */
87 void
88 get_user_print_options (struct value_print_options *opts)
89 {
90 *opts = user_print_options;
91 }
92
93 /* Initialize *OPTS to be a copy of the user print options, but with
94 pretty-printing disabled. */
95 void
96 get_raw_print_options (struct value_print_options *opts)
97 {
98 *opts = user_print_options;
99 opts->pretty = Val_no_prettyprint;
100 }
101
102 /* Initialize *OPTS to be a copy of the user print options, but using
103 FORMAT as the formatting option. */
104 void
105 get_formatted_print_options (struct value_print_options *opts,
106 char format)
107 {
108 *opts = user_print_options;
109 opts->format = format;
110 }
111
112 static void
113 show_print_max (struct ui_file *file, int from_tty,
114 struct cmd_list_element *c, const char *value)
115 {
116 fprintf_filtered (file, _("\
117 Limit on string chars or array elements to print is %s.\n"),
118 value);
119 }
120
121
122 /* Default input and output radixes, and output format letter. */
123
124 unsigned input_radix = 10;
125 static void
126 show_input_radix (struct ui_file *file, int from_tty,
127 struct cmd_list_element *c, const char *value)
128 {
129 fprintf_filtered (file, _("\
130 Default input radix for entering numbers is %s.\n"),
131 value);
132 }
133
134 unsigned output_radix = 10;
135 static void
136 show_output_radix (struct ui_file *file, int from_tty,
137 struct cmd_list_element *c, const char *value)
138 {
139 fprintf_filtered (file, _("\
140 Default output radix for printing of values is %s.\n"),
141 value);
142 }
143
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. */
146
147 static void
148 show_print_array_indexes (struct ui_file *file, int from_tty,
149 struct cmd_list_element *c, const char *value)
150 {
151 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
152 }
153
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
156 print routines. */
157
158 static void
159 show_repeat_count_threshold (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
161 {
162 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
163 value);
164 }
165
166 /* If nonzero, stops printing of char arrays at first null. */
167
168 static void
169 show_stop_print_at_null (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
171 {
172 fprintf_filtered (file, _("\
173 Printing of char arrays to stop at first null char is %s.\n"),
174 value);
175 }
176
177 /* Controls pretty printing of structures. */
178
179 static void
180 show_prettyprint_structs (struct ui_file *file, int from_tty,
181 struct cmd_list_element *c, const char *value)
182 {
183 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
184 }
185
186 /* Controls pretty printing of arrays. */
187
188 static void
189 show_prettyprint_arrays (struct ui_file *file, int from_tty,
190 struct cmd_list_element *c, const char *value)
191 {
192 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
193 }
194
195 /* If nonzero, causes unions inside structures or other unions to be
196 printed. */
197
198 static void
199 show_unionprint (struct ui_file *file, int from_tty,
200 struct cmd_list_element *c, const char *value)
201 {
202 fprintf_filtered (file, _("\
203 Printing of unions interior to structures is %s.\n"),
204 value);
205 }
206
207 /* If nonzero, causes machine addresses to be printed in certain contexts. */
208
209 static void
210 show_addressprint (struct ui_file *file, int from_tty,
211 struct cmd_list_element *c, const char *value)
212 {
213 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
214 }
215 \f
216
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.
220
221 If the data are a string pointer, returns the number of string characters
222 printed.
223
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. */
230
231
232 int
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)
237 {
238 volatile struct gdb_exception except;
239 int ret = 0;
240 struct value_print_options local_opts = *options;
241 struct type *real_type = check_typedef (type);
242
243 if (local_opts.pretty == Val_pretty_default)
244 local_opts.pretty = (local_opts.prettyprint_structs
245 ? Val_prettyprint : Val_no_prettyprint);
246
247 QUIT;
248
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. */
252
253 if (TYPE_STUB (real_type))
254 {
255 fprintf_filtered (stream, "<incomplete type>");
256 gdb_flush (stream);
257 return (0);
258 }
259
260 TRY_CATCH (except, RETURN_MASK_ERROR)
261 {
262 ret = language->la_val_print (type, valaddr, embedded_offset, address,
263 stream, recurse, &local_opts);
264 }
265 if (except.reason < 0)
266 fprintf_filtered (stream, _("<error reading variable>"));
267
268 return ret;
269 }
270
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
273 is not. */
274
275 static int
276 value_check_printable (struct value *val, struct ui_file *stream)
277 {
278 if (val == 0)
279 {
280 fprintf_filtered (stream, _("<address of value unknown>"));
281 return 0;
282 }
283
284 if (value_optimized_out (val))
285 {
286 fprintf_filtered (stream, _("<value optimized out>"));
287 return 0;
288 }
289
290 return 1;
291 }
292
293 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
294 to OPTIONS.
295
296 If the data are a string pointer, returns the number of string characters
297 printed.
298
299 This is a preferable interface to val_print, above, because it uses
300 GDB's value mechanism. */
301
302 int
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)
306 {
307 if (!value_check_printable (val, stream))
308 return 0;
309
310 return val_print (value_type (val), value_contents_all (val),
311 value_embedded_offset (val), VALUE_ADDRESS (val),
312 stream, recurse, options, language);
313 }
314
315 /* Print the value VAL in C-ish syntax on stream STREAM according to
316 OPTIONS.
317 If the object printed is a string pointer, returns
318 the number of string bytes printed. */
319
320 int
321 value_print (struct value *val, struct ui_file *stream,
322 const struct value_print_options *options)
323 {
324 if (!value_check_printable (val, stream))
325 return 0;
326
327 return LA_VALUE_PRINT (val, stream, options);
328 }
329
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. */
333
334 void
335 val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
336 struct ui_file *stream)
337 {
338 enum bfd_endian byte_order = gdbarch_byte_order (current_gdbarch);
339
340 if (TYPE_LENGTH (type) > sizeof (LONGEST))
341 {
342 LONGEST val;
343
344 if (TYPE_UNSIGNED (type)
345 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
346 &val))
347 {
348 print_longest (stream, 'u', 0, val);
349 }
350 else
351 {
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
355 better than this. */
356 print_hex_chars (stream, (unsigned char *) valaddr,
357 TYPE_LENGTH (type), byte_order);
358 }
359 }
360 else
361 {
362 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
363 unpack_long (type, valaddr));
364 }
365 }
366
367 void
368 val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
369 struct ui_file *stream)
370 {
371 ULONGEST val = unpack_long (type, valaddr);
372 int bitpos, nfields = TYPE_NFIELDS (type);
373
374 fputs_filtered ("[ ", stream);
375 for (bitpos = 0; bitpos < nfields; bitpos++)
376 {
377 if (TYPE_FIELD_BITPOS (type, bitpos) != -1
378 && (val & ((ULONGEST)1 << bitpos)))
379 {
380 if (TYPE_FIELD_NAME (type, bitpos))
381 fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
382 else
383 fprintf_filtered (stream, "#%d ", bitpos);
384 }
385 }
386 fputs_filtered ("]", stream);
387 }
388
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
393 format.
394
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
397 (leading 0 or 0x).
398
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
406 that capability. */
407
408 void
409 print_longest (struct ui_file *stream, int format, int use_c_format,
410 LONGEST val_long)
411 {
412 const char *val;
413
414 switch (format)
415 {
416 case 'd':
417 val = int_string (val_long, 10, 1, 0, 1); break;
418 case 'u':
419 val = int_string (val_long, 10, 0, 0, 1); break;
420 case 'x':
421 val = int_string (val_long, 16, 0, 0, use_c_format); break;
422 case 'b':
423 val = int_string (val_long, 16, 0, 2, 1); break;
424 case 'h':
425 val = int_string (val_long, 16, 0, 4, 1); break;
426 case 'w':
427 val = int_string (val_long, 16, 0, 8, 1); break;
428 case 'g':
429 val = int_string (val_long, 16, 0, 16, 1); break;
430 break;
431 case 'o':
432 val = int_string (val_long, 8, 0, 0, use_c_format); break;
433 default:
434 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
435 }
436 fputs_filtered (val, stream);
437 }
438
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. */
444
445 int
446 longest_to_int (LONGEST arg)
447 {
448 /* Let the compiler do the work */
449 int rtnval = (int) arg;
450
451 /* Check for overflows or underflows */
452 if (sizeof (LONGEST) > sizeof (int))
453 {
454 if (rtnval != arg)
455 {
456 error (_("Value out of range."));
457 }
458 }
459 return (rtnval);
460 }
461
462 /* Print a floating point value of type TYPE (not always a
463 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
464
465 void
466 print_floating (const gdb_byte *valaddr, struct type *type,
467 struct ui_file *stream)
468 {
469 DOUBLEST doub;
470 int inv;
471 const struct floatformat *fmt = NULL;
472 unsigned len = TYPE_LENGTH (type);
473 enum float_kind kind;
474
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);
478 if (fmt != NULL)
479 {
480 kind = floatformat_classify (fmt, valaddr);
481 if (kind == float_nan)
482 {
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, ")");
489 return;
490 }
491 else if (kind == float_infinite)
492 {
493 if (floatformat_is_negative (fmt, valaddr))
494 fputs_filtered ("-", stream);
495 fputs_filtered ("inf", stream);
496 return;
497 }
498 }
499
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. */
508
509 doub = unpack_double (type, valaddr, &inv);
510 if (inv)
511 {
512 fprintf_filtered (stream, "<invalid float value>");
513 return;
514 }
515
516 /* FIXME: kettenis/2001-01-20: The following code makes too much
517 assumptions about the host and target floating point format. */
518
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. */
523
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);
528 else
529 #ifdef PRINTF_HAS_LONG_DOUBLE
530 fprintf_filtered (stream, "%.35Lg", doub);
531 #else
532 /* This at least wins with values that are representable as
533 doubles. */
534 fprintf_filtered (stream, "%.17g", (double) doub);
535 #endif
536 }
537
538 void
539 print_decimal_floating (const gdb_byte *valaddr, struct type *type,
540 struct ui_file *stream)
541 {
542 char decstr[MAX_DECIMAL_STRING];
543 unsigned len = TYPE_LENGTH (type);
544
545 decimal_to_string (valaddr, len, decstr);
546 fputs_filtered (decstr, stream);
547 return;
548 }
549
550 void
551 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
552 unsigned len, enum bfd_endian byte_order)
553 {
554
555 #define BITS_IN_BYTES 8
556
557 const gdb_byte *p;
558 unsigned int i;
559 int b;
560
561 /* Declared "int" so it will be signed.
562 * This ensures that right shift will shift in zeros.
563 */
564 const int mask = 0x080;
565
566 /* FIXME: We should be not printing leading zeroes in most cases. */
567
568 if (byte_order == BFD_ENDIAN_BIG)
569 {
570 for (p = valaddr;
571 p < valaddr + len;
572 p++)
573 {
574 /* Every byte has 8 binary characters; peel off
575 * and print from the MSB end.
576 */
577 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
578 {
579 if (*p & (mask >> i))
580 b = 1;
581 else
582 b = 0;
583
584 fprintf_filtered (stream, "%1d", b);
585 }
586 }
587 }
588 else
589 {
590 for (p = valaddr + len - 1;
591 p >= valaddr;
592 p--)
593 {
594 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
595 {
596 if (*p & (mask >> i))
597 b = 1;
598 else
599 b = 0;
600
601 fprintf_filtered (stream, "%1d", b);
602 }
603 }
604 }
605 }
606
607 /* VALADDR points to an integer of LEN bytes.
608 * Print it in octal on stream or format it in buf.
609 */
610 void
611 print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
612 unsigned len, enum bfd_endian byte_order)
613 {
614 const gdb_byte *p;
615 unsigned char octa1, octa2, octa3, carry;
616 int cycle;
617
618 /* FIXME: We should be not printing leading zeroes in most cases. */
619
620
621 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
622 * the extra bits, which cycle every three bytes:
623 *
624 * Byte side: 0 1 2 3
625 * | | | |
626 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
627 *
628 * Octal side: 0 1 carry 3 4 carry ...
629 *
630 * Cycle number: 0 1 2
631 *
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.
635 */
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
641 #define MID_ONE 0160
642 #define LOW_ONE 0016
643 #define CARRY_ONE 0001
644 #define HIGH_TWO 0300
645 #define MID_TWO 0070
646 #define LOW_TWO 0007
647
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.
650 */
651 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
652 carry = 0;
653
654 fputs_filtered ("0", stream);
655 if (byte_order == BFD_ENDIAN_BIG)
656 {
657 for (p = valaddr;
658 p < valaddr + len;
659 p++)
660 {
661 switch (cycle)
662 {
663 case 0:
664 /* No carry in, carry out two bits.
665 */
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);
671 break;
672
673 case 1:
674 /* Carry in two bits, carry out one bit.
675 */
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);
683 break;
684
685 case 2:
686 /* Carry in one bit, no carry out.
687 */
688 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
689 octa2 = (MID_TWO & *p) >> 3;
690 octa3 = (LOW_TWO & *p);
691 carry = 0;
692 fprintf_filtered (stream, "%o", octa1);
693 fprintf_filtered (stream, "%o", octa2);
694 fprintf_filtered (stream, "%o", octa3);
695 break;
696
697 default:
698 error (_("Internal error in octal conversion;"));
699 }
700
701 cycle++;
702 cycle = cycle % BITS_IN_OCTAL;
703 }
704 }
705 else
706 {
707 for (p = valaddr + len - 1;
708 p >= valaddr;
709 p--)
710 {
711 switch (cycle)
712 {
713 case 0:
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);
720 break;
721
722 case 1:
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);
731 break;
732
733 case 2:
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);
738 carry = 0;
739 fprintf_filtered (stream, "%o", octa1);
740 fprintf_filtered (stream, "%o", octa2);
741 fprintf_filtered (stream, "%o", octa3);
742 break;
743
744 default:
745 error (_("Internal error in octal conversion;"));
746 }
747
748 cycle++;
749 cycle = cycle % BITS_IN_OCTAL;
750 }
751 }
752
753 }
754
755 /* VALADDR points to an integer of LEN bytes.
756 * Print it in decimal on stream or format it in buf.
757 */
758 void
759 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
760 unsigned len, enum bfd_endian byte_order)
761 {
762 #define TEN 10
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)
768
769 const gdb_byte *p;
770 unsigned char *digits;
771 int carry;
772 int decimal_len;
773 int i, j, decimal_digits;
774 int dummy;
775 int flip;
776
777 /* Base-ten number is less than twice as many digits
778 * as the base 16 number, which is 2 digits per byte.
779 */
780 decimal_len = len * 2 * 2;
781 digits = xmalloc (decimal_len);
782
783 for (i = 0; i < decimal_len; i++)
784 {
785 digits[i] = 0;
786 }
787
788 /* Ok, we have an unknown number of bytes of data to be printed in
789 * decimal.
790 *
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.
794 *
795 * The trick is that "digits" holds a base-10 number, but sometimes
796 * the individual digits are > 10.
797 *
798 * Outer loop is per nibble (hex digit) of input, from MSD end to
799 * LSD end.
800 */
801 decimal_digits = 0; /* Number of decimal digits so far */
802 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
803 flip = 0;
804 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
805 {
806 /*
807 * Multiply current base-ten number by 16 in place.
808 * Each digit was between 0 and 9, now is between
809 * 0 and 144.
810 */
811 for (j = 0; j < decimal_digits; j++)
812 {
813 digits[j] = SHIFT (digits[j]);
814 }
815
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
818 * between 0 and 159.
819 *
820 * "flip" is used to run this loop twice for each byte.
821 */
822 if (flip == 0)
823 {
824 /* Take top nibble.
825 */
826 digits[0] += HIGH_NIBBLE (*p);
827 flip = 1;
828 }
829 else
830 {
831 /* Take low nibble and bump our pointer "p".
832 */
833 digits[0] += LOW_NIBBLE (*p);
834 if (byte_order == BFD_ENDIAN_BIG)
835 p++;
836 else
837 p--;
838 flip = 0;
839 }
840
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.
848 */
849 carry = 0;
850 for (j = 0; j < decimal_len - 1; j++)
851 {
852 digits[j] += carry;
853
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".
857 */
858 dummy = digits[j];
859 carry = CARRY_OUT (dummy);
860 digits[j] = CARRY_LEFT (dummy);
861
862 if (j >= decimal_digits && carry == 0)
863 {
864 /*
865 * All higher digits are 0 and we
866 * no longer have a carry.
867 *
868 * Note: "j" is 0-based, "decimal_digits" is
869 * 1-based.
870 */
871 decimal_digits = j + 1;
872 break;
873 }
874 }
875 }
876
877 /* Ok, now "digits" is the decimal representation, with
878 * the "decimal_digits" actual digits. Print!
879 */
880 for (i = decimal_digits - 1; i >= 0; i--)
881 {
882 fprintf_filtered (stream, "%1d", digits[i]);
883 }
884 xfree (digits);
885 }
886
887 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
888
889 void
890 print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
891 unsigned len, enum bfd_endian byte_order)
892 {
893 const gdb_byte *p;
894
895 /* FIXME: We should be not printing leading zeroes in most cases. */
896
897 fputs_filtered ("0x", stream);
898 if (byte_order == BFD_ENDIAN_BIG)
899 {
900 for (p = valaddr;
901 p < valaddr + len;
902 p++)
903 {
904 fprintf_filtered (stream, "%02x", *p);
905 }
906 }
907 else
908 {
909 for (p = valaddr + len - 1;
910 p >= valaddr;
911 p--)
912 {
913 fprintf_filtered (stream, "%02x", *p);
914 }
915 }
916 }
917
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. */
920
921 void
922 print_char_chars (struct ui_file *stream, const gdb_byte *valaddr,
923 unsigned len, enum bfd_endian byte_order)
924 {
925 const gdb_byte *p;
926
927 if (byte_order == BFD_ENDIAN_BIG)
928 {
929 p = valaddr;
930 while (p < valaddr + len - 1 && *p == 0)
931 ++p;
932
933 while (p < valaddr + len)
934 {
935 LA_EMIT_CHAR (*p, stream, '\'');
936 ++p;
937 }
938 }
939 else
940 {
941 p = valaddr + len - 1;
942 while (p > valaddr && *p == 0)
943 --p;
944
945 while (p >= valaddr)
946 {
947 LA_EMIT_CHAR (*p, stream, '\'');
948 --p;
949 }
950 }
951 }
952
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.
956
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.
959
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. */
967
968 int
969 get_array_bounds (struct type *type, long *low_bound, long *high_bound)
970 {
971 struct type *index = TYPE_INDEX_TYPE (type);
972 long low = 0;
973 long high = 0;
974
975 if (index == NULL)
976 return 0;
977
978 if (TYPE_CODE (index) == TYPE_CODE_RANGE)
979 {
980 low = TYPE_LOW_BOUND (index);
981 high = TYPE_HIGH_BOUND (index);
982 }
983 else if (TYPE_CODE (index) == TYPE_CODE_ENUM)
984 {
985 const int n_enums = TYPE_NFIELDS (index);
986
987 low = TYPE_FIELD_BITPOS (index, 0);
988 high = TYPE_FIELD_BITPOS (index, n_enums - 1);
989 }
990 else
991 return 0;
992
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"). */
996 if (low > high + 1)
997 return 0;
998
999 if (low_bound)
1000 *low_bound = low;
1001
1002 if (high_bound)
1003 *high_bound = high;
1004
1005 return 1;
1006 }
1007
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. */
1010
1011 void
1012 maybe_print_array_index (struct type *index_type, LONGEST index,
1013 struct ui_file *stream,
1014 const struct value_print_options *options)
1015 {
1016 struct value *index_value;
1017
1018 if (!options->print_array_indexes)
1019 return;
1020
1021 index_value = value_from_longest (index_type, index);
1022
1023 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1024 }
1025
1026 /* Called by various <lang>_val_print routines to print elements of an
1027 array in the form "<elem1>, <elem2>, <elem3>, ...".
1028
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.
1033 */
1034
1035 void
1036 val_print_array_elements (struct type *type, const gdb_byte *valaddr,
1037 CORE_ADDR address, struct ui_file *stream,
1038 int recurse,
1039 const struct value_print_options *options,
1040 unsigned int i)
1041 {
1042 unsigned int things_printed = 0;
1043 unsigned len;
1044 struct type *elttype, *index_type;
1045 unsigned eltlen;
1046 /* Position of the array element we are examining to see
1047 whether it is repeated. */
1048 unsigned int rep1;
1049 /* Number of repetitions we have detected so far. */
1050 unsigned int reps;
1051 long low_bound_index = 0;
1052
1053 elttype = TYPE_TARGET_TYPE (type);
1054 eltlen = TYPE_LENGTH (check_typedef (elttype));
1055 index_type = TYPE_INDEX_TYPE (type);
1056
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
1061 the bounds. */
1062 if (eltlen != 0)
1063 len = TYPE_LENGTH (type) / eltlen;
1064 else
1065 {
1066 long low, hi;
1067 if (get_array_bounds (type, &low, &hi))
1068 len = hi - low + 1;
1069 else
1070 {
1071 warning (_("unable to get bounds of array, assuming null array"));
1072 len = 0;
1073 }
1074 }
1075
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))
1079 {
1080 warning (_("unable to get low bound of array, using zero as default"));
1081 low_bound_index = 0;
1082 }
1083
1084 annotate_array_section_begin (i, elttype);
1085
1086 for (; i < len && things_printed < options->print_max; i++)
1087 {
1088 if (i != 0)
1089 {
1090 if (options->prettyprint_arrays)
1091 {
1092 fprintf_filtered (stream, ",\n");
1093 print_spaces_filtered (2 + 2 * recurse, stream);
1094 }
1095 else
1096 {
1097 fprintf_filtered (stream, ", ");
1098 }
1099 }
1100 wrap_here (n_spaces (2 + 2 * recurse));
1101 maybe_print_array_index (index_type, i + low_bound_index,
1102 stream, options);
1103
1104 rep1 = i + 1;
1105 reps = 1;
1106 while ((rep1 < len) &&
1107 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1108 {
1109 ++reps;
1110 ++rep1;
1111 }
1112
1113 if (reps > options->repeat_count_threshold)
1114 {
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 ();
1120
1121 i = rep1 - 1;
1122 things_printed += options->repeat_count_threshold;
1123 }
1124 else
1125 {
1126 val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1127 stream, recurse + 1, options, current_language);
1128 annotate_elt ();
1129 things_printed++;
1130 }
1131 }
1132 annotate_array_section_end ();
1133 if (i < len)
1134 {
1135 fprintf_filtered (stream, "...");
1136 }
1137 }
1138
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. */
1143
1144 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1145 function be eliminated. */
1146
1147 static int
1148 partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int *errnoptr)
1149 {
1150 int nread; /* Number of bytes actually read. */
1151 int errcode; /* Error from last read. */
1152
1153 /* First try a complete read. */
1154 errcode = target_read_memory (memaddr, myaddr, len);
1155 if (errcode == 0)
1156 {
1157 /* Got it all. */
1158 nread = len;
1159 }
1160 else
1161 {
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--)
1164 {
1165 errcode = target_read_memory (memaddr++, myaddr++, 1);
1166 }
1167 /* If an error, the last read was unsuccessful, so adjust count. */
1168 if (errcode != 0)
1169 {
1170 nread--;
1171 }
1172 }
1173 if (errnoptr != NULL)
1174 {
1175 *errnoptr = errcode;
1176 }
1177 return (nread);
1178 }
1179
1180 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
1181 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
1182 allocated buffer containing the string, which the caller is responsible to
1183 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
1184 success, or errno on failure.
1185
1186 If LEN > 0, reads exactly LEN characters (including eventual NULs in
1187 the middle or end of the string). If LEN is -1, stops at the first
1188 null character (not necessarily the first null byte) up to a maximum
1189 of FETCHLIMIT characters. Set FETCHLIMIT to UINT_MAX to read as many
1190 characters as possible from the string.
1191
1192 Unless an exception is thrown, BUFFER will always be allocated, even on
1193 failure. In this case, some characters might have been read before the
1194 failure happened. Check BYTES_READ to recognize this situation.
1195
1196 Note: There was a FIXME asking to make this code use target_read_string,
1197 but this function is more general (can read past null characters, up to
1198 given LEN). Besides, it is used much more often than target_read_string
1199 so it is more tested. Perhaps callers of target_read_string should use
1200 this function instead? */
1201
1202 int
1203 read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
1204 gdb_byte **buffer, int *bytes_read)
1205 {
1206 int found_nul; /* Non-zero if we found the nul char. */
1207 int errcode; /* Errno returned from bad reads. */
1208 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1209 unsigned int chunksize; /* Size of each fetch, in chars. */
1210 gdb_byte *bufptr; /* Pointer to next available byte in buffer. */
1211 gdb_byte *limit; /* First location past end of fetch buffer. */
1212 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1213
1214 /* Decide how large of chunks to try to read in one operation. This
1215 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1216 so we might as well read them all in one operation. If LEN is -1, we
1217 are looking for a NUL terminator to end the fetching, so we might as
1218 well read in blocks that are large enough to be efficient, but not so
1219 large as to be slow if fetchlimit happens to be large. So we choose the
1220 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1221 200 is way too big for remote debugging over a serial line. */
1222
1223 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1224
1225 /* Loop until we either have all the characters, or we encounter
1226 some error, such as bumping into the end of the address space. */
1227
1228 found_nul = 0;
1229 *buffer = NULL;
1230
1231 old_chain = make_cleanup (free_current_contents, buffer);
1232
1233 if (len > 0)
1234 {
1235 *buffer = (gdb_byte *) xmalloc (len * width);
1236 bufptr = *buffer;
1237
1238 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1239 / width;
1240 addr += nfetch * width;
1241 bufptr += nfetch * width;
1242 }
1243 else if (len == -1)
1244 {
1245 unsigned long bufsize = 0;
1246
1247 do
1248 {
1249 QUIT;
1250 nfetch = min (chunksize, fetchlimit - bufsize);
1251
1252 if (*buffer == NULL)
1253 *buffer = (gdb_byte *) xmalloc (nfetch * width);
1254 else
1255 *buffer = (gdb_byte *) xrealloc (*buffer,
1256 (nfetch + bufsize) * width);
1257
1258 bufptr = *buffer + bufsize * width;
1259 bufsize += nfetch;
1260
1261 /* Read as much as we can. */
1262 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1263 / width;
1264
1265 /* Scan this chunk for the null character 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 character, or at the next character after the end
1269 of the buffer. */
1270
1271 limit = bufptr + nfetch * width;
1272 while (bufptr < limit)
1273 {
1274 unsigned long c;
1275
1276 c = extract_unsigned_integer (bufptr, width);
1277 addr += width;
1278 bufptr += width;
1279 if (c == 0)
1280 {
1281 /* We don't care about any error which happened after
1282 the NUL terminator. */
1283 errcode = 0;
1284 found_nul = 1;
1285 break;
1286 }
1287 }
1288 }
1289 while (errcode == 0 /* no error */
1290 && bufptr - *buffer < fetchlimit * width /* no overrun */
1291 && !found_nul); /* haven't found NUL yet */
1292 }
1293 else
1294 { /* Length of string is really 0! */
1295 /* We always allocate *buffer. */
1296 *buffer = bufptr = xmalloc (1);
1297 errcode = 0;
1298 }
1299
1300 /* bufptr and addr now point immediately beyond the last byte which we
1301 consider part of the string (including a '\0' which ends the string). */
1302 *bytes_read = bufptr - *buffer;
1303
1304 QUIT;
1305
1306 discard_cleanups (old_chain);
1307
1308 return errcode;
1309 }
1310
1311 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1312 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1313 stops at the first null byte, otherwise printing proceeds (including null
1314 bytes) until either print_max or LEN characters have been printed,
1315 whichever is smaller. */
1316
1317 int
1318 val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream,
1319 const struct value_print_options *options)
1320 {
1321 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1322 int errcode; /* Errno returned from bad reads. */
1323 int found_nul; /* Non-zero if we found the nul char */
1324 unsigned int fetchlimit; /* Maximum number of chars to print. */
1325 int bytes_read;
1326 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
1327 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1328
1329 /* First we need to figure out the limit on the number of characters we are
1330 going to attempt to fetch and print. This is actually pretty simple. If
1331 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1332 LEN is -1, then the limit is print_max. This is true regardless of
1333 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1334 because finding the null byte (or available memory) is what actually
1335 limits the fetch. */
1336
1337 fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
1338
1339 errcode = read_string (addr, len, width, fetchlimit, &buffer, &bytes_read);
1340 old_chain = make_cleanup (xfree, buffer);
1341
1342 addr += bytes_read;
1343
1344 /* We now have either successfully filled the buffer to fetchlimit, or
1345 terminated early due to an error or finding a null char when LEN is -1. */
1346
1347 /* Determine found_nul by looking at the last character read. */
1348 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width) == 0;
1349
1350 if (len == -1 && !found_nul)
1351 {
1352 gdb_byte *peekbuf;
1353
1354 /* We didn't find a NUL terminator we were looking for. Attempt
1355 to peek at the next character. If not successful, or it is not
1356 a null byte, then force ellipsis to be printed. */
1357
1358 peekbuf = (gdb_byte *) alloca (width);
1359
1360 if (target_read_memory (addr, peekbuf, width) == 0
1361 && extract_unsigned_integer (peekbuf, width) != 0)
1362 force_ellipsis = 1;
1363 }
1364 else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
1365 {
1366 /* Getting an error when we have a requested length, or fetching less
1367 than the number of characters actually requested, always make us
1368 print ellipsis. */
1369 force_ellipsis = 1;
1370 }
1371
1372 /* If we get an error before fetching anything, don't print a string.
1373 But if we fetch something and then get an error, print the string
1374 and then the error message. */
1375 if (errcode == 0 || bytes_read > 0)
1376 {
1377 if (options->addressprint)
1378 {
1379 fputs_filtered (" ", stream);
1380 }
1381 LA_PRINT_STRING (stream, buffer, bytes_read / width, width, force_ellipsis, options);
1382 }
1383
1384 if (errcode != 0)
1385 {
1386 if (errcode == EIO)
1387 {
1388 fprintf_filtered (stream, " <Address ");
1389 fputs_filtered (paddress (addr), stream);
1390 fprintf_filtered (stream, " out of bounds>");
1391 }
1392 else
1393 {
1394 fprintf_filtered (stream, " <Error reading address ");
1395 fputs_filtered (paddress (addr), stream);
1396 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1397 }
1398 }
1399
1400 gdb_flush (stream);
1401 do_cleanups (old_chain);
1402
1403 return (bytes_read / width);
1404 }
1405 \f
1406
1407 /* The 'set input-radix' command writes to this auxiliary variable.
1408 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
1409 it is left unchanged. */
1410
1411 static unsigned input_radix_1 = 10;
1412
1413 /* Validate an input or output radix setting, and make sure the user
1414 knows what they really did here. Radix setting is confusing, e.g.
1415 setting the input radix to "10" never changes it! */
1416
1417 static void
1418 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1419 {
1420 set_input_radix_1 (from_tty, input_radix_1);
1421 }
1422
1423 static void
1424 set_input_radix_1 (int from_tty, unsigned radix)
1425 {
1426 /* We don't currently disallow any input radix except 0 or 1, which don't
1427 make any mathematical sense. In theory, we can deal with any input
1428 radix greater than 1, even if we don't have unique digits for every
1429 value from 0 to radix-1, but in practice we lose on large radix values.
1430 We should either fix the lossage or restrict the radix range more.
1431 (FIXME). */
1432
1433 if (radix < 2)
1434 {
1435 input_radix_1 = input_radix;
1436 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
1437 radix);
1438 }
1439 input_radix_1 = input_radix = radix;
1440 if (from_tty)
1441 {
1442 printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
1443 radix, radix, radix);
1444 }
1445 }
1446
1447 /* The 'set output-radix' command writes to this auxiliary variable.
1448 If the requested radix is valid, OUTPUT_RADIX is updated,
1449 otherwise, it is left unchanged. */
1450
1451 static unsigned output_radix_1 = 10;
1452
1453 static void
1454 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1455 {
1456 set_output_radix_1 (from_tty, output_radix_1);
1457 }
1458
1459 static void
1460 set_output_radix_1 (int from_tty, unsigned radix)
1461 {
1462 /* Validate the radix and disallow ones that we aren't prepared to
1463 handle correctly, leaving the radix unchanged. */
1464 switch (radix)
1465 {
1466 case 16:
1467 user_print_options.output_format = 'x'; /* hex */
1468 break;
1469 case 10:
1470 user_print_options.output_format = 0; /* decimal */
1471 break;
1472 case 8:
1473 user_print_options.output_format = 'o'; /* octal */
1474 break;
1475 default:
1476 output_radix_1 = output_radix;
1477 error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
1478 radix);
1479 }
1480 output_radix_1 = output_radix = radix;
1481 if (from_tty)
1482 {
1483 printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
1484 radix, radix, radix);
1485 }
1486 }
1487
1488 /* Set both the input and output radix at once. Try to set the output radix
1489 first, since it has the most restrictive range. An radix that is valid as
1490 an output radix is also valid as an input radix.
1491
1492 It may be useful to have an unusual input radix. If the user wishes to
1493 set an input radix that is not valid as an output radix, he needs to use
1494 the 'set input-radix' command. */
1495
1496 static void
1497 set_radix (char *arg, int from_tty)
1498 {
1499 unsigned radix;
1500
1501 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1502 set_output_radix_1 (0, radix);
1503 set_input_radix_1 (0, radix);
1504 if (from_tty)
1505 {
1506 printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
1507 radix, radix, radix);
1508 }
1509 }
1510
1511 /* Show both the input and output radices. */
1512
1513 static void
1514 show_radix (char *arg, int from_tty)
1515 {
1516 if (from_tty)
1517 {
1518 if (input_radix == output_radix)
1519 {
1520 printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
1521 input_radix, input_radix, input_radix);
1522 }
1523 else
1524 {
1525 printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
1526 input_radix, input_radix, input_radix);
1527 printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
1528 output_radix, output_radix, output_radix);
1529 }
1530 }
1531 }
1532 \f
1533
1534 static void
1535 set_print (char *arg, int from_tty)
1536 {
1537 printf_unfiltered (
1538 "\"set print\" must be followed by the name of a print subcommand.\n");
1539 help_list (setprintlist, "set print ", -1, gdb_stdout);
1540 }
1541
1542 static void
1543 show_print (char *args, int from_tty)
1544 {
1545 cmd_show_list (showprintlist, from_tty, "");
1546 }
1547 \f
1548 void
1549 _initialize_valprint (void)
1550 {
1551 struct cmd_list_element *c;
1552
1553 add_prefix_cmd ("print", no_class, set_print,
1554 _("Generic command for setting how things print."),
1555 &setprintlist, "set print ", 0, &setlist);
1556 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1557 /* prefer set print to set prompt */
1558 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1559
1560 add_prefix_cmd ("print", no_class, show_print,
1561 _("Generic command for showing print settings."),
1562 &showprintlist, "show print ", 0, &showlist);
1563 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1564 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1565
1566 add_setshow_uinteger_cmd ("elements", no_class,
1567 &user_print_options.print_max, _("\
1568 Set limit on string chars or array elements to print."), _("\
1569 Show limit on string chars or array elements to print."), _("\
1570 \"set print elements 0\" causes there to be no limit."),
1571 NULL,
1572 show_print_max,
1573 &setprintlist, &showprintlist);
1574
1575 add_setshow_boolean_cmd ("null-stop", no_class,
1576 &user_print_options.stop_print_at_null, _("\
1577 Set printing of char arrays to stop at first null char."), _("\
1578 Show printing of char arrays to stop at first null char."), NULL,
1579 NULL,
1580 show_stop_print_at_null,
1581 &setprintlist, &showprintlist);
1582
1583 add_setshow_uinteger_cmd ("repeats", no_class,
1584 &user_print_options.repeat_count_threshold, _("\
1585 Set threshold for repeated print elements."), _("\
1586 Show threshold for repeated print elements."), _("\
1587 \"set print repeats 0\" causes all elements to be individually printed."),
1588 NULL,
1589 show_repeat_count_threshold,
1590 &setprintlist, &showprintlist);
1591
1592 add_setshow_boolean_cmd ("pretty", class_support,
1593 &user_print_options.prettyprint_structs, _("\
1594 Set prettyprinting of structures."), _("\
1595 Show prettyprinting of structures."), NULL,
1596 NULL,
1597 show_prettyprint_structs,
1598 &setprintlist, &showprintlist);
1599
1600 add_setshow_boolean_cmd ("union", class_support,
1601 &user_print_options.unionprint, _("\
1602 Set printing of unions interior to structures."), _("\
1603 Show printing of unions interior to structures."), NULL,
1604 NULL,
1605 show_unionprint,
1606 &setprintlist, &showprintlist);
1607
1608 add_setshow_boolean_cmd ("array", class_support,
1609 &user_print_options.prettyprint_arrays, _("\
1610 Set prettyprinting of arrays."), _("\
1611 Show prettyprinting of arrays."), NULL,
1612 NULL,
1613 show_prettyprint_arrays,
1614 &setprintlist, &showprintlist);
1615
1616 add_setshow_boolean_cmd ("address", class_support,
1617 &user_print_options.addressprint, _("\
1618 Set printing of addresses."), _("\
1619 Show printing of addresses."), NULL,
1620 NULL,
1621 show_addressprint,
1622 &setprintlist, &showprintlist);
1623
1624 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
1625 _("\
1626 Set default input radix for entering numbers."), _("\
1627 Show default input radix for entering numbers."), NULL,
1628 set_input_radix,
1629 show_input_radix,
1630 &setlist, &showlist);
1631
1632 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
1633 _("\
1634 Set default output radix for printing of values."), _("\
1635 Show default output radix for printing of values."), NULL,
1636 set_output_radix,
1637 show_output_radix,
1638 &setlist, &showlist);
1639
1640 /* The "set radix" and "show radix" commands are special in that
1641 they are like normal set and show commands but allow two normally
1642 independent variables to be either set or shown with a single
1643 command. So the usual deprecated_add_set_cmd() and [deleted]
1644 add_show_from_set() commands aren't really appropriate. */
1645 /* FIXME: i18n: With the new add_setshow_integer command, that is no
1646 longer true - show can display anything. */
1647 add_cmd ("radix", class_support, set_radix, _("\
1648 Set default input and output number radices.\n\
1649 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1650 Without an argument, sets both radices back to the default value of 10."),
1651 &setlist);
1652 add_cmd ("radix", class_support, show_radix, _("\
1653 Show the default input and output number radices.\n\
1654 Use 'show input-radix' or 'show output-radix' to independently show each."),
1655 &showlist);
1656
1657 add_setshow_boolean_cmd ("array-indexes", class_support,
1658 &user_print_options.print_array_indexes, _("\
1659 Set printing of array indexes."), _("\
1660 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1661 &setprintlist, &showprintlist);
1662 }