2009-02-05 Thiago Jung Bauermann <bauerman@br.ibm.com>
[deliverable/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 old_chain = make_cleanup (null_cleanup, 0);
1230
1231 if (len > 0)
1232 {
1233 *buffer = (gdb_byte *) xmalloc (len * width);
1234 bufptr = *buffer;
1235 old_chain = make_cleanup (xfree, *buffer);
1236
1237 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1238 / width;
1239 addr += nfetch * width;
1240 bufptr += nfetch * width;
1241 }
1242 else if (len == -1)
1243 {
1244 unsigned long bufsize = 0;
1245
1246 *buffer = NULL;
1247
1248 do
1249 {
1250 QUIT;
1251 nfetch = min (chunksize, fetchlimit - bufsize);
1252
1253 if (*buffer == NULL)
1254 *buffer = (gdb_byte *) xmalloc (nfetch * width);
1255 else
1256 {
1257 discard_cleanups (old_chain);
1258 *buffer = (gdb_byte *) xrealloc (*buffer,
1259 (nfetch + bufsize) * width);
1260 }
1261
1262 old_chain = make_cleanup (xfree, *buffer);
1263 bufptr = *buffer + bufsize * width;
1264 bufsize += nfetch;
1265
1266 /* Read as much as we can. */
1267 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1268 / width;
1269
1270 /* Scan this chunk for the null character that terminates the string
1271 to print. If found, we don't need to fetch any more. Note
1272 that bufptr is explicitly left pointing at the next character
1273 after the null character, or at the next character after the end
1274 of the buffer. */
1275
1276 limit = bufptr + nfetch * width;
1277 while (bufptr < limit)
1278 {
1279 unsigned long c;
1280
1281 c = extract_unsigned_integer (bufptr, width);
1282 addr += width;
1283 bufptr += width;
1284 if (c == 0)
1285 {
1286 /* We don't care about any error which happened after
1287 the NUL terminator. */
1288 errcode = 0;
1289 found_nul = 1;
1290 break;
1291 }
1292 }
1293 }
1294 while (errcode == 0 /* no error */
1295 && bufptr - *buffer < fetchlimit * width /* no overrun */
1296 && !found_nul); /* haven't found NUL yet */
1297 }
1298 else
1299 { /* Length of string is really 0! */
1300 /* We always allocate *buffer. */
1301 *buffer = bufptr = xmalloc (1);
1302 errcode = 0;
1303 }
1304
1305 /* bufptr and addr now point immediately beyond the last byte which we
1306 consider part of the string (including a '\0' which ends the string). */
1307 *bytes_read = bufptr - *buffer;
1308
1309 QUIT;
1310
1311 discard_cleanups (old_chain);
1312
1313 return errcode;
1314 }
1315
1316 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1317 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1318 stops at the first null byte, otherwise printing proceeds (including null
1319 bytes) until either print_max or LEN characters have been printed,
1320 whichever is smaller. */
1321
1322 int
1323 val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream,
1324 const struct value_print_options *options)
1325 {
1326 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1327 int errcode; /* Errno returned from bad reads. */
1328 int found_nul; /* Non-zero if we found the nul char */
1329 unsigned int fetchlimit; /* Maximum number of chars to print. */
1330 int bytes_read;
1331 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
1332 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1333
1334 /* First we need to figure out the limit on the number of characters we are
1335 going to attempt to fetch and print. This is actually pretty simple. If
1336 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1337 LEN is -1, then the limit is print_max. This is true regardless of
1338 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1339 because finding the null byte (or available memory) is what actually
1340 limits the fetch. */
1341
1342 fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
1343
1344 errcode = read_string (addr, len, width, fetchlimit, &buffer, &bytes_read);
1345 old_chain = make_cleanup (xfree, buffer);
1346
1347 addr += bytes_read;
1348
1349 /* We now have either successfully filled the buffer to fetchlimit, or
1350 terminated early due to an error or finding a null char when LEN is -1. */
1351
1352 /* Determine found_nul by looking at the last character read. */
1353 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width) == 0;
1354
1355 if (len == -1 && !found_nul)
1356 {
1357 gdb_byte *peekbuf;
1358
1359 /* We didn't find a NUL terminator we were looking for. Attempt
1360 to peek at the next character. If not successful, or it is not
1361 a null byte, then force ellipsis to be printed. */
1362
1363 peekbuf = (gdb_byte *) alloca (width);
1364
1365 if (target_read_memory (addr, peekbuf, width) == 0
1366 && extract_unsigned_integer (peekbuf, width) != 0)
1367 force_ellipsis = 1;
1368 }
1369 else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
1370 {
1371 /* Getting an error when we have a requested length, or fetching less
1372 than the number of characters actually requested, always make us
1373 print ellipsis. */
1374 force_ellipsis = 1;
1375 }
1376
1377 /* If we get an error before fetching anything, don't print a string.
1378 But if we fetch something and then get an error, print the string
1379 and then the error message. */
1380 if (errcode == 0 || bytes_read > 0)
1381 {
1382 if (options->addressprint)
1383 {
1384 fputs_filtered (" ", stream);
1385 }
1386 LA_PRINT_STRING (stream, buffer, bytes_read / width, width, force_ellipsis, options);
1387 }
1388
1389 if (errcode != 0)
1390 {
1391 if (errcode == EIO)
1392 {
1393 fprintf_filtered (stream, " <Address ");
1394 fputs_filtered (paddress (addr), stream);
1395 fprintf_filtered (stream, " out of bounds>");
1396 }
1397 else
1398 {
1399 fprintf_filtered (stream, " <Error reading address ");
1400 fputs_filtered (paddress (addr), stream);
1401 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1402 }
1403 }
1404
1405 gdb_flush (stream);
1406 do_cleanups (old_chain);
1407
1408 return (bytes_read / width);
1409 }
1410 \f
1411
1412 /* The 'set input-radix' command writes to this auxiliary variable.
1413 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
1414 it is left unchanged. */
1415
1416 static unsigned input_radix_1 = 10;
1417
1418 /* Validate an input or output radix setting, and make sure the user
1419 knows what they really did here. Radix setting is confusing, e.g.
1420 setting the input radix to "10" never changes it! */
1421
1422 static void
1423 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1424 {
1425 set_input_radix_1 (from_tty, input_radix_1);
1426 }
1427
1428 static void
1429 set_input_radix_1 (int from_tty, unsigned radix)
1430 {
1431 /* We don't currently disallow any input radix except 0 or 1, which don't
1432 make any mathematical sense. In theory, we can deal with any input
1433 radix greater than 1, even if we don't have unique digits for every
1434 value from 0 to radix-1, but in practice we lose on large radix values.
1435 We should either fix the lossage or restrict the radix range more.
1436 (FIXME). */
1437
1438 if (radix < 2)
1439 {
1440 input_radix_1 = input_radix;
1441 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
1442 radix);
1443 }
1444 input_radix_1 = input_radix = radix;
1445 if (from_tty)
1446 {
1447 printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
1448 radix, radix, radix);
1449 }
1450 }
1451
1452 /* The 'set output-radix' command writes to this auxiliary variable.
1453 If the requested radix is valid, OUTPUT_RADIX is updated,
1454 otherwise, it is left unchanged. */
1455
1456 static unsigned output_radix_1 = 10;
1457
1458 static void
1459 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1460 {
1461 set_output_radix_1 (from_tty, output_radix_1);
1462 }
1463
1464 static void
1465 set_output_radix_1 (int from_tty, unsigned radix)
1466 {
1467 /* Validate the radix and disallow ones that we aren't prepared to
1468 handle correctly, leaving the radix unchanged. */
1469 switch (radix)
1470 {
1471 case 16:
1472 user_print_options.output_format = 'x'; /* hex */
1473 break;
1474 case 10:
1475 user_print_options.output_format = 0; /* decimal */
1476 break;
1477 case 8:
1478 user_print_options.output_format = 'o'; /* octal */
1479 break;
1480 default:
1481 output_radix_1 = output_radix;
1482 error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
1483 radix);
1484 }
1485 output_radix_1 = output_radix = radix;
1486 if (from_tty)
1487 {
1488 printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
1489 radix, radix, radix);
1490 }
1491 }
1492
1493 /* Set both the input and output radix at once. Try to set the output radix
1494 first, since it has the most restrictive range. An radix that is valid as
1495 an output radix is also valid as an input radix.
1496
1497 It may be useful to have an unusual input radix. If the user wishes to
1498 set an input radix that is not valid as an output radix, he needs to use
1499 the 'set input-radix' command. */
1500
1501 static void
1502 set_radix (char *arg, int from_tty)
1503 {
1504 unsigned radix;
1505
1506 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1507 set_output_radix_1 (0, radix);
1508 set_input_radix_1 (0, radix);
1509 if (from_tty)
1510 {
1511 printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
1512 radix, radix, radix);
1513 }
1514 }
1515
1516 /* Show both the input and output radices. */
1517
1518 static void
1519 show_radix (char *arg, int from_tty)
1520 {
1521 if (from_tty)
1522 {
1523 if (input_radix == output_radix)
1524 {
1525 printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
1526 input_radix, input_radix, input_radix);
1527 }
1528 else
1529 {
1530 printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
1531 input_radix, input_radix, input_radix);
1532 printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
1533 output_radix, output_radix, output_radix);
1534 }
1535 }
1536 }
1537 \f
1538
1539 static void
1540 set_print (char *arg, int from_tty)
1541 {
1542 printf_unfiltered (
1543 "\"set print\" must be followed by the name of a print subcommand.\n");
1544 help_list (setprintlist, "set print ", -1, gdb_stdout);
1545 }
1546
1547 static void
1548 show_print (char *args, int from_tty)
1549 {
1550 cmd_show_list (showprintlist, from_tty, "");
1551 }
1552 \f
1553 void
1554 _initialize_valprint (void)
1555 {
1556 struct cmd_list_element *c;
1557
1558 add_prefix_cmd ("print", no_class, set_print,
1559 _("Generic command for setting how things print."),
1560 &setprintlist, "set print ", 0, &setlist);
1561 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1562 /* prefer set print to set prompt */
1563 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1564
1565 add_prefix_cmd ("print", no_class, show_print,
1566 _("Generic command for showing print settings."),
1567 &showprintlist, "show print ", 0, &showlist);
1568 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1569 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1570
1571 add_setshow_uinteger_cmd ("elements", no_class,
1572 &user_print_options.print_max, _("\
1573 Set limit on string chars or array elements to print."), _("\
1574 Show limit on string chars or array elements to print."), _("\
1575 \"set print elements 0\" causes there to be no limit."),
1576 NULL,
1577 show_print_max,
1578 &setprintlist, &showprintlist);
1579
1580 add_setshow_boolean_cmd ("null-stop", no_class,
1581 &user_print_options.stop_print_at_null, _("\
1582 Set printing of char arrays to stop at first null char."), _("\
1583 Show printing of char arrays to stop at first null char."), NULL,
1584 NULL,
1585 show_stop_print_at_null,
1586 &setprintlist, &showprintlist);
1587
1588 add_setshow_uinteger_cmd ("repeats", no_class,
1589 &user_print_options.repeat_count_threshold, _("\
1590 Set threshold for repeated print elements."), _("\
1591 Show threshold for repeated print elements."), _("\
1592 \"set print repeats 0\" causes all elements to be individually printed."),
1593 NULL,
1594 show_repeat_count_threshold,
1595 &setprintlist, &showprintlist);
1596
1597 add_setshow_boolean_cmd ("pretty", class_support,
1598 &user_print_options.prettyprint_structs, _("\
1599 Set prettyprinting of structures."), _("\
1600 Show prettyprinting of structures."), NULL,
1601 NULL,
1602 show_prettyprint_structs,
1603 &setprintlist, &showprintlist);
1604
1605 add_setshow_boolean_cmd ("union", class_support,
1606 &user_print_options.unionprint, _("\
1607 Set printing of unions interior to structures."), _("\
1608 Show printing of unions interior to structures."), NULL,
1609 NULL,
1610 show_unionprint,
1611 &setprintlist, &showprintlist);
1612
1613 add_setshow_boolean_cmd ("array", class_support,
1614 &user_print_options.prettyprint_arrays, _("\
1615 Set prettyprinting of arrays."), _("\
1616 Show prettyprinting of arrays."), NULL,
1617 NULL,
1618 show_prettyprint_arrays,
1619 &setprintlist, &showprintlist);
1620
1621 add_setshow_boolean_cmd ("address", class_support,
1622 &user_print_options.addressprint, _("\
1623 Set printing of addresses."), _("\
1624 Show printing of addresses."), NULL,
1625 NULL,
1626 show_addressprint,
1627 &setprintlist, &showprintlist);
1628
1629 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
1630 _("\
1631 Set default input radix for entering numbers."), _("\
1632 Show default input radix for entering numbers."), NULL,
1633 set_input_radix,
1634 show_input_radix,
1635 &setlist, &showlist);
1636
1637 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
1638 _("\
1639 Set default output radix for printing of values."), _("\
1640 Show default output radix for printing of values."), NULL,
1641 set_output_radix,
1642 show_output_radix,
1643 &setlist, &showlist);
1644
1645 /* The "set radix" and "show radix" commands are special in that
1646 they are like normal set and show commands but allow two normally
1647 independent variables to be either set or shown with a single
1648 command. So the usual deprecated_add_set_cmd() and [deleted]
1649 add_show_from_set() commands aren't really appropriate. */
1650 /* FIXME: i18n: With the new add_setshow_integer command, that is no
1651 longer true - show can display anything. */
1652 add_cmd ("radix", class_support, set_radix, _("\
1653 Set default input and output number radices.\n\
1654 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1655 Without an argument, sets both radices back to the default value of 10."),
1656 &setlist);
1657 add_cmd ("radix", class_support, show_radix, _("\
1658 Show the default input and output number radices.\n\
1659 Use 'show input-radix' or 'show output-radix' to independently show each."),
1660 &showlist);
1661
1662 add_setshow_boolean_cmd ("array-indexes", class_support,
1663 &user_print_options.print_array_indexes, _("\
1664 Set printing of array indexes."), _("\
1665 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1666 &setprintlist, &showprintlist);
1667 }
This page took 0.064739 seconds and 4 git commands to generate.