32f9dedd5b23783da9a3c699927a6a8d8898bb8a
[platform/upstream/binutils.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, struct type *type,
923                   const gdb_byte *valaddr,
924                   unsigned len, enum bfd_endian byte_order)
925 {
926   const gdb_byte *p;
927
928   if (byte_order == BFD_ENDIAN_BIG)
929     {
930       p = valaddr;
931       while (p < valaddr + len - 1 && *p == 0)
932         ++p;
933
934       while (p < valaddr + len)
935         {
936           LA_EMIT_CHAR (*p, type, stream, '\'');
937           ++p;
938         }
939     }
940   else
941     {
942       p = valaddr + len - 1;
943       while (p > valaddr && *p == 0)
944         --p;
945
946       while (p >= valaddr)
947         {
948           LA_EMIT_CHAR (*p, type, stream, '\'');
949           --p;
950         }
951     }
952 }
953
954 /* Assuming TYPE is a simple, non-empty array type, compute its upper
955    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
956    Save the high bound into HIGH_BOUND if not NULL.
957
958    Return 1 if the operation was successful. Return zero otherwise,
959    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
960    
961    Computing the array upper and lower bounds is pretty easy, but this
962    function does some additional verifications before returning them.
963    If something incorrect is detected, it is better to return a status
964    rather than throwing an error, making it easier for the caller to
965    implement an error-recovery plan.  For instance, it may decide to
966    warn the user that the bounds were not found and then use some
967    default values instead.  */
968
969 int
970 get_array_bounds (struct type *type, long *low_bound, long *high_bound)
971 {
972   struct type *index = TYPE_INDEX_TYPE (type);
973   long low = 0;
974   long high = 0;
975                                   
976   if (index == NULL)
977     return 0;
978
979   if (TYPE_CODE (index) == TYPE_CODE_RANGE)
980     {
981       low = TYPE_LOW_BOUND (index);
982       high = TYPE_HIGH_BOUND (index);
983     }
984   else if (TYPE_CODE (index) == TYPE_CODE_ENUM)
985     {
986       const int n_enums = TYPE_NFIELDS (index);
987
988       low = TYPE_FIELD_BITPOS (index, 0);
989       high = TYPE_FIELD_BITPOS (index, n_enums - 1);
990     }
991   else
992     return 0;
993
994   /* Abort if the lower bound is greater than the higher bound, except
995      when low = high + 1.  This is a very common idiom used in Ada when
996      defining empty ranges (for instance "range 1 .. 0").  */
997   if (low > high + 1)
998     return 0;
999
1000   if (low_bound)
1001     *low_bound = low;
1002
1003   if (high_bound)
1004     *high_bound = high;
1005
1006   return 1;
1007 }
1008
1009 /* Print on STREAM using the given OPTIONS the index for the element
1010    at INDEX of an array whose index type is INDEX_TYPE.  */
1011     
1012 void  
1013 maybe_print_array_index (struct type *index_type, LONGEST index,
1014                          struct ui_file *stream,
1015                          const struct value_print_options *options)
1016 {
1017   struct value *index_value;
1018
1019   if (!options->print_array_indexes)
1020     return; 
1021     
1022   index_value = value_from_longest (index_type, index);
1023
1024   LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1025 }
1026
1027 /*  Called by various <lang>_val_print routines to print elements of an
1028    array in the form "<elem1>, <elem2>, <elem3>, ...".
1029
1030    (FIXME?)  Assumes array element separator is a comma, which is correct
1031    for all languages currently handled.
1032    (FIXME?)  Some languages have a notation for repeated array elements,
1033    perhaps we should try to use that notation when appropriate.
1034  */
1035
1036 void
1037 val_print_array_elements (struct type *type, const gdb_byte *valaddr,
1038                           CORE_ADDR address, struct ui_file *stream,
1039                           int recurse,
1040                           const struct value_print_options *options,
1041                           unsigned int i)
1042 {
1043   unsigned int things_printed = 0;
1044   unsigned len;
1045   struct type *elttype, *index_type;
1046   unsigned eltlen;
1047   /* Position of the array element we are examining to see
1048      whether it is repeated.  */
1049   unsigned int rep1;
1050   /* Number of repetitions we have detected so far.  */
1051   unsigned int reps;
1052   long low_bound_index = 0;
1053
1054   elttype = TYPE_TARGET_TYPE (type);
1055   eltlen = TYPE_LENGTH (check_typedef (elttype));
1056   index_type = TYPE_INDEX_TYPE (type);
1057
1058   /* Compute the number of elements in the array.  On most arrays,
1059      the size of its elements is not zero, and so the number of elements
1060      is simply the size of the array divided by the size of the elements.
1061      But for arrays of elements whose size is zero, we need to look at
1062      the bounds.  */
1063   if (eltlen != 0)
1064     len = TYPE_LENGTH (type) / eltlen;
1065   else
1066     {
1067       long low, hi;
1068       if (get_array_bounds (type, &low, &hi))
1069         len = hi - low + 1;
1070       else
1071         {
1072           warning (_("unable to get bounds of array, assuming null array"));
1073           len = 0;
1074         }
1075     }
1076
1077   /* Get the array low bound.  This only makes sense if the array
1078      has one or more element in it.  */
1079   if (len > 0 && !get_array_bounds (type, &low_bound_index, NULL))
1080     {
1081       warning (_("unable to get low bound of array, using zero as default"));
1082       low_bound_index = 0;
1083     }
1084
1085   annotate_array_section_begin (i, elttype);
1086
1087   for (; i < len && things_printed < options->print_max; i++)
1088     {
1089       if (i != 0)
1090         {
1091           if (options->prettyprint_arrays)
1092             {
1093               fprintf_filtered (stream, ",\n");
1094               print_spaces_filtered (2 + 2 * recurse, stream);
1095             }
1096           else
1097             {
1098               fprintf_filtered (stream, ", ");
1099             }
1100         }
1101       wrap_here (n_spaces (2 + 2 * recurse));
1102       maybe_print_array_index (index_type, i + low_bound_index,
1103                                stream, options);
1104
1105       rep1 = i + 1;
1106       reps = 1;
1107       while ((rep1 < len) &&
1108              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1109         {
1110           ++reps;
1111           ++rep1;
1112         }
1113
1114       if (reps > options->repeat_count_threshold)
1115         {
1116           val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1117                      stream, recurse + 1, options, current_language);
1118           annotate_elt_rep (reps);
1119           fprintf_filtered (stream, " <repeats %u times>", reps);
1120           annotate_elt_rep_end ();
1121
1122           i = rep1 - 1;
1123           things_printed += options->repeat_count_threshold;
1124         }
1125       else
1126         {
1127           val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1128                      stream, recurse + 1, options, current_language);
1129           annotate_elt ();
1130           things_printed++;
1131         }
1132     }
1133   annotate_array_section_end ();
1134   if (i < len)
1135     {
1136       fprintf_filtered (stream, "...");
1137     }
1138 }
1139
1140 /* Read LEN bytes of target memory at address MEMADDR, placing the
1141    results in GDB's memory at MYADDR.  Returns a count of the bytes
1142    actually read, and optionally an errno value in the location
1143    pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1144
1145 /* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
1146    function be eliminated.  */
1147
1148 static int
1149 partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int *errnoptr)
1150 {
1151   int nread;                    /* Number of bytes actually read. */
1152   int errcode;                  /* Error from last read. */
1153
1154   /* First try a complete read. */
1155   errcode = target_read_memory (memaddr, myaddr, len);
1156   if (errcode == 0)
1157     {
1158       /* Got it all. */
1159       nread = len;
1160     }
1161   else
1162     {
1163       /* Loop, reading one byte at a time until we get as much as we can. */
1164       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1165         {
1166           errcode = target_read_memory (memaddr++, myaddr++, 1);
1167         }
1168       /* If an error, the last read was unsuccessful, so adjust count. */
1169       if (errcode != 0)
1170         {
1171           nread--;
1172         }
1173     }
1174   if (errnoptr != NULL)
1175     {
1176       *errnoptr = errcode;
1177     }
1178   return (nread);
1179 }
1180
1181 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
1182    each.  Fetch at most FETCHLIMIT characters.  BUFFER will be set to a newly
1183    allocated buffer containing the string, which the caller is responsible to
1184    free, and BYTES_READ will be set to the number of bytes read.  Returns 0 on
1185    success, or errno on failure.
1186
1187    If LEN > 0, reads exactly LEN characters (including eventual NULs in
1188    the middle or end of the string).  If LEN is -1, stops at the first
1189    null character (not necessarily the first null byte) up to a maximum
1190    of FETCHLIMIT characters.  Set FETCHLIMIT to UINT_MAX to read as many
1191    characters as possible from the string.
1192
1193    Unless an exception is thrown, BUFFER will always be allocated, even on
1194    failure.  In this case, some characters might have been read before the
1195    failure happened.  Check BYTES_READ to recognize this situation.
1196
1197    Note: There was a FIXME asking to make this code use target_read_string,
1198    but this function is more general (can read past null characters, up to
1199    given LEN). Besides, it is used much more often than target_read_string
1200    so it is more tested.  Perhaps callers of target_read_string should use
1201    this function instead?  */
1202
1203 int
1204 read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
1205              gdb_byte **buffer, int *bytes_read)
1206 {
1207   int found_nul;                /* Non-zero if we found the nul char.  */
1208   int errcode;                  /* Errno returned from bad reads.  */
1209   unsigned int nfetch;          /* Chars to fetch / chars fetched.  */
1210   unsigned int chunksize;       /* Size of each fetch, in chars.  */
1211   gdb_byte *bufptr;             /* Pointer to next available byte in buffer.  */
1212   gdb_byte *limit;              /* First location past end of fetch buffer.  */
1213   struct cleanup *old_chain = NULL;     /* Top of the old cleanup chain.  */
1214
1215   /* Decide how large of chunks to try to read in one operation.  This
1216      is also pretty simple.  If LEN >= zero, then we want fetchlimit chars,
1217      so we might as well read them all in one operation.  If LEN is -1, we
1218      are looking for a NUL terminator to end the fetching, so we might as
1219      well read in blocks that are large enough to be efficient, but not so
1220      large as to be slow if fetchlimit happens to be large.  So we choose the
1221      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
1222      200 is way too big for remote debugging over a serial line.  */
1223
1224   chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1225
1226   /* Loop until we either have all the characters, or we encounter
1227      some error, such as bumping into the end of the address space.  */
1228
1229   found_nul = 0;
1230   *buffer = NULL;
1231
1232   old_chain = make_cleanup (free_current_contents, buffer);
1233
1234   if (len > 0)
1235     {
1236       *buffer = (gdb_byte *) xmalloc (len * width);
1237       bufptr = *buffer;
1238
1239       nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1240         / width;
1241       addr += nfetch * width;
1242       bufptr += nfetch * width;
1243     }
1244   else if (len == -1)
1245     {
1246       unsigned long bufsize = 0;
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             *buffer = (gdb_byte *) xrealloc (*buffer,
1257                                              (nfetch + bufsize) * width);
1258
1259           bufptr = *buffer + bufsize * width;
1260           bufsize += nfetch;
1261
1262           /* Read as much as we can.  */
1263           nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1264                     / width;
1265
1266           /* Scan this chunk for the null character that terminates the string
1267              to print.  If found, we don't need to fetch any more.  Note
1268              that bufptr is explicitly left pointing at the next character
1269              after the null character, or at the next character after the end
1270              of the buffer.  */
1271
1272           limit = bufptr + nfetch * width;
1273           while (bufptr < limit)
1274             {
1275               unsigned long c;
1276
1277               c = extract_unsigned_integer (bufptr, width);
1278               addr += width;
1279               bufptr += width;
1280               if (c == 0)
1281                 {
1282                   /* We don't care about any error which happened after
1283                      the NUL terminator.  */
1284                   errcode = 0;
1285                   found_nul = 1;
1286                   break;
1287                 }
1288             }
1289         }
1290       while (errcode == 0       /* no error */
1291              && bufptr - *buffer < fetchlimit * width   /* no overrun */
1292              && !found_nul);    /* haven't found NUL yet */
1293     }
1294   else
1295     {                           /* Length of string is really 0!  */
1296       /* We always allocate *buffer.  */
1297       *buffer = bufptr = xmalloc (1);
1298       errcode = 0;
1299     }
1300
1301   /* bufptr and addr now point immediately beyond the last byte which we
1302      consider part of the string (including a '\0' which ends the string).  */
1303   *bytes_read = bufptr - *buffer;
1304
1305   QUIT;
1306
1307   discard_cleanups (old_chain);
1308
1309   return errcode;
1310 }
1311
1312 /* Print a string from the inferior, starting at ADDR and printing up to LEN
1313    characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
1314    stops at the first null byte, otherwise printing proceeds (including null
1315    bytes) until either print_max or LEN characters have been printed,
1316    whichever is smaller.  */
1317
1318 int
1319 val_print_string (struct type *elttype, CORE_ADDR addr, int len,
1320                   struct ui_file *stream,
1321                   const struct value_print_options *options)
1322 {
1323   int force_ellipsis = 0;       /* Force ellipsis to be printed if nonzero.  */
1324   int errcode;                  /* Errno returned from bad reads.  */
1325   int found_nul;                /* Non-zero if we found the nul char */
1326   unsigned int fetchlimit;      /* Maximum number of chars to print.  */
1327   int bytes_read;
1328   gdb_byte *buffer = NULL;      /* Dynamically growable fetch buffer.  */
1329   struct cleanup *old_chain = NULL;     /* Top of the old cleanup chain.  */
1330   int width = TYPE_LENGTH (elttype);
1331
1332   /* First we need to figure out the limit on the number of characters we are
1333      going to attempt to fetch and print.  This is actually pretty simple.  If
1334      LEN >= zero, then the limit is the minimum of LEN and print_max.  If
1335      LEN is -1, then the limit is print_max.  This is true regardless of
1336      whether print_max is zero, UINT_MAX (unlimited), or something in between,
1337      because finding the null byte (or available memory) is what actually
1338      limits the fetch.  */
1339
1340   fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
1341
1342   errcode = read_string (addr, len, width, fetchlimit, &buffer, &bytes_read);
1343   old_chain = make_cleanup (xfree, buffer);
1344
1345   addr += bytes_read;
1346
1347   /* We now have either successfully filled the buffer to fetchlimit, or
1348      terminated early due to an error or finding a null char when LEN is -1.  */
1349
1350   /* Determine found_nul by looking at the last character read.  */
1351   found_nul = extract_unsigned_integer (buffer + bytes_read - width, width) == 0;
1352
1353   if (len == -1 && !found_nul)
1354     {
1355       gdb_byte *peekbuf;
1356
1357       /* We didn't find a NUL terminator we were looking for.  Attempt
1358          to peek at the next character.  If not successful, or it is not
1359          a null byte, then force ellipsis to be printed.  */
1360
1361       peekbuf = (gdb_byte *) alloca (width);
1362
1363       if (target_read_memory (addr, peekbuf, width) == 0
1364           && extract_unsigned_integer (peekbuf, width) != 0)
1365         force_ellipsis = 1;
1366     }
1367   else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
1368     {
1369       /* Getting an error when we have a requested length, or fetching less
1370          than the number of characters actually requested, always make us
1371          print ellipsis.  */
1372       force_ellipsis = 1;
1373     }
1374
1375   /* If we get an error before fetching anything, don't print a string.
1376      But if we fetch something and then get an error, print the string
1377      and then the error message.  */
1378   if (errcode == 0 || bytes_read > 0)
1379     {
1380       if (options->addressprint)
1381         {
1382           fputs_filtered (" ", stream);
1383         }
1384       LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width, force_ellipsis, options);
1385     }
1386
1387   if (errcode != 0)
1388     {
1389       if (errcode == EIO)
1390         {
1391           fprintf_filtered (stream, " <Address ");
1392           fputs_filtered (paddress (addr), stream);
1393           fprintf_filtered (stream, " out of bounds>");
1394         }
1395       else
1396         {
1397           fprintf_filtered (stream, " <Error reading address ");
1398           fputs_filtered (paddress (addr), stream);
1399           fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1400         }
1401     }
1402
1403   gdb_flush (stream);
1404   do_cleanups (old_chain);
1405
1406   return (bytes_read / width);
1407 }
1408 \f
1409
1410 /* The 'set input-radix' command writes to this auxiliary variable.
1411    If the requested radix is valid, INPUT_RADIX is updated; otherwise,
1412    it is left unchanged.  */
1413
1414 static unsigned input_radix_1 = 10;
1415
1416 /* Validate an input or output radix setting, and make sure the user
1417    knows what they really did here.  Radix setting is confusing, e.g.
1418    setting the input radix to "10" never changes it!  */
1419
1420 static void
1421 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1422 {
1423   set_input_radix_1 (from_tty, input_radix_1);
1424 }
1425
1426 static void
1427 set_input_radix_1 (int from_tty, unsigned radix)
1428 {
1429   /* We don't currently disallow any input radix except 0 or 1, which don't
1430      make any mathematical sense.  In theory, we can deal with any input
1431      radix greater than 1, even if we don't have unique digits for every
1432      value from 0 to radix-1, but in practice we lose on large radix values.
1433      We should either fix the lossage or restrict the radix range more.
1434      (FIXME). */
1435
1436   if (radix < 2)
1437     {
1438       input_radix_1 = input_radix;
1439       error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
1440              radix);
1441     }
1442   input_radix_1 = input_radix = radix;
1443   if (from_tty)
1444     {
1445       printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
1446                        radix, radix, radix);
1447     }
1448 }
1449
1450 /* The 'set output-radix' command writes to this auxiliary variable.
1451    If the requested radix is valid, OUTPUT_RADIX is updated,
1452    otherwise, it is left unchanged.  */
1453
1454 static unsigned output_radix_1 = 10;
1455
1456 static void
1457 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1458 {
1459   set_output_radix_1 (from_tty, output_radix_1);
1460 }
1461
1462 static void
1463 set_output_radix_1 (int from_tty, unsigned radix)
1464 {
1465   /* Validate the radix and disallow ones that we aren't prepared to
1466      handle correctly, leaving the radix unchanged. */
1467   switch (radix)
1468     {
1469     case 16:
1470       user_print_options.output_format = 'x';   /* hex */
1471       break;
1472     case 10:
1473       user_print_options.output_format = 0;     /* decimal */
1474       break;
1475     case 8:
1476       user_print_options.output_format = 'o';   /* octal */
1477       break;
1478     default:
1479       output_radix_1 = output_radix;
1480       error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
1481              radix);
1482     }
1483   output_radix_1 = output_radix = radix;
1484   if (from_tty)
1485     {
1486       printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
1487                        radix, radix, radix);
1488     }
1489 }
1490
1491 /* Set both the input and output radix at once.  Try to set the output radix
1492    first, since it has the most restrictive range.  An radix that is valid as
1493    an output radix is also valid as an input radix.
1494
1495    It may be useful to have an unusual input radix.  If the user wishes to
1496    set an input radix that is not valid as an output radix, he needs to use
1497    the 'set input-radix' command. */
1498
1499 static void
1500 set_radix (char *arg, int from_tty)
1501 {
1502   unsigned radix;
1503
1504   radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1505   set_output_radix_1 (0, radix);
1506   set_input_radix_1 (0, radix);
1507   if (from_tty)
1508     {
1509       printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
1510                        radix, radix, radix);
1511     }
1512 }
1513
1514 /* Show both the input and output radices. */
1515
1516 static void
1517 show_radix (char *arg, int from_tty)
1518 {
1519   if (from_tty)
1520     {
1521       if (input_radix == output_radix)
1522         {
1523           printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
1524                            input_radix, input_radix, input_radix);
1525         }
1526       else
1527         {
1528           printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
1529                            input_radix, input_radix, input_radix);
1530           printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
1531                            output_radix, output_radix, output_radix);
1532         }
1533     }
1534 }
1535 \f
1536
1537 static void
1538 set_print (char *arg, int from_tty)
1539 {
1540   printf_unfiltered (
1541      "\"set print\" must be followed by the name of a print subcommand.\n");
1542   help_list (setprintlist, "set print ", -1, gdb_stdout);
1543 }
1544
1545 static void
1546 show_print (char *args, int from_tty)
1547 {
1548   cmd_show_list (showprintlist, from_tty, "");
1549 }
1550 \f
1551 void
1552 _initialize_valprint (void)
1553 {
1554   struct cmd_list_element *c;
1555
1556   add_prefix_cmd ("print", no_class, set_print,
1557                   _("Generic command for setting how things print."),
1558                   &setprintlist, "set print ", 0, &setlist);
1559   add_alias_cmd ("p", "print", no_class, 1, &setlist);
1560   /* prefer set print to set prompt */
1561   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1562
1563   add_prefix_cmd ("print", no_class, show_print,
1564                   _("Generic command for showing print settings."),
1565                   &showprintlist, "show print ", 0, &showlist);
1566   add_alias_cmd ("p", "print", no_class, 1, &showlist);
1567   add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1568
1569   add_setshow_uinteger_cmd ("elements", no_class,
1570                             &user_print_options.print_max, _("\
1571 Set limit on string chars or array elements to print."), _("\
1572 Show limit on string chars or array elements to print."), _("\
1573 \"set print elements 0\" causes there to be no limit."),
1574                             NULL,
1575                             show_print_max,
1576                             &setprintlist, &showprintlist);
1577
1578   add_setshow_boolean_cmd ("null-stop", no_class,
1579                            &user_print_options.stop_print_at_null, _("\
1580 Set printing of char arrays to stop at first null char."), _("\
1581 Show printing of char arrays to stop at first null char."), NULL,
1582                            NULL,
1583                            show_stop_print_at_null,
1584                            &setprintlist, &showprintlist);
1585
1586   add_setshow_uinteger_cmd ("repeats", no_class,
1587                             &user_print_options.repeat_count_threshold, _("\
1588 Set threshold for repeated print elements."), _("\
1589 Show threshold for repeated print elements."), _("\
1590 \"set print repeats 0\" causes all elements to be individually printed."),
1591                             NULL,
1592                             show_repeat_count_threshold,
1593                             &setprintlist, &showprintlist);
1594
1595   add_setshow_boolean_cmd ("pretty", class_support,
1596                            &user_print_options.prettyprint_structs, _("\
1597 Set prettyprinting of structures."), _("\
1598 Show prettyprinting of structures."), NULL,
1599                            NULL,
1600                            show_prettyprint_structs,
1601                            &setprintlist, &showprintlist);
1602
1603   add_setshow_boolean_cmd ("union", class_support,
1604                            &user_print_options.unionprint, _("\
1605 Set printing of unions interior to structures."), _("\
1606 Show printing of unions interior to structures."), NULL,
1607                            NULL,
1608                            show_unionprint,
1609                            &setprintlist, &showprintlist);
1610
1611   add_setshow_boolean_cmd ("array", class_support,
1612                            &user_print_options.prettyprint_arrays, _("\
1613 Set prettyprinting of arrays."), _("\
1614 Show prettyprinting of arrays."), NULL,
1615                            NULL,
1616                            show_prettyprint_arrays,
1617                            &setprintlist, &showprintlist);
1618
1619   add_setshow_boolean_cmd ("address", class_support,
1620                            &user_print_options.addressprint, _("\
1621 Set printing of addresses."), _("\
1622 Show printing of addresses."), NULL,
1623                            NULL,
1624                            show_addressprint,
1625                            &setprintlist, &showprintlist);
1626
1627   add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
1628                              _("\
1629 Set default input radix for entering numbers."), _("\
1630 Show default input radix for entering numbers."), NULL,
1631                              set_input_radix,
1632                              show_input_radix,
1633                              &setlist, &showlist);
1634
1635   add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
1636                              _("\
1637 Set default output radix for printing of values."), _("\
1638 Show default output radix for printing of values."), NULL,
1639                              set_output_radix,
1640                              show_output_radix,
1641                              &setlist, &showlist);
1642
1643   /* The "set radix" and "show radix" commands are special in that
1644      they are like normal set and show commands but allow two normally
1645      independent variables to be either set or shown with a single
1646      command.  So the usual deprecated_add_set_cmd() and [deleted]
1647      add_show_from_set() commands aren't really appropriate. */
1648   /* FIXME: i18n: With the new add_setshow_integer command, that is no
1649      longer true - show can display anything.  */
1650   add_cmd ("radix", class_support, set_radix, _("\
1651 Set default input and output number radices.\n\
1652 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1653 Without an argument, sets both radices back to the default value of 10."),
1654            &setlist);
1655   add_cmd ("radix", class_support, show_radix, _("\
1656 Show the default input and output number radices.\n\
1657 Use 'show input-radix' or 'show output-radix' to independently show each."),
1658            &showlist);
1659
1660   add_setshow_boolean_cmd ("array-indexes", class_support,
1661                            &user_print_options.print_array_indexes, _("\
1662 Set printing of array indexes."), _("\
1663 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1664                            &setprintlist, &showprintlist);
1665 }