import gdb-1999-10-18 snapshot
[platform/upstream/binutils.git] / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1998
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
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 "obstack.h"
31 #include "language.h"
32 #include "demangle.h"
33 #include "annotate.h"
34 #include "valprint.h"
35
36 #include <errno.h>
37
38 /* Prototypes for local functions */
39
40 static int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
41                                 int len, int *errnoptr);
42
43 static void print_hex_chars PARAMS ((GDB_FILE *, unsigned char *,
44                                      unsigned int));
45
46 static void show_print PARAMS ((char *, int));
47
48 static void set_print PARAMS ((char *, int));
49
50 static void set_radix PARAMS ((char *, int));
51
52 static void show_radix PARAMS ((char *, int));
53
54 static void set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
55
56 static void set_input_radix_1 PARAMS ((int, unsigned));
57
58 static void set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
59
60 static void set_output_radix_1 PARAMS ((int, unsigned));
61
62 void _initialize_valprint PARAMS ((void));
63
64 /* Maximum number of chars to print for a string pointer value or vector
65    contents, or UINT_MAX for no limit.  Note that "set print elements 0"
66    stores UINT_MAX in print_max, which displays in a show command as
67    "unlimited". */
68
69 unsigned int print_max;
70 #define PRINT_MAX_DEFAULT 200   /* Start print_max off at this value. */
71
72 /* Default input and output radixes, and output format letter.  */
73
74 unsigned input_radix = 10;
75 unsigned output_radix = 10;
76 int output_format = 0;
77
78 /* Print repeat counts if there are more than this many repetitions of an
79    element in an array.  Referenced by the low level language dependent
80    print routines. */
81
82 unsigned int repeat_count_threshold = 10;
83
84 /* If nonzero, stops printing of char arrays at first null. */
85
86 int stop_print_at_null;
87
88 /* Controls pretty printing of structures. */
89
90 int prettyprint_structs;
91
92 /* Controls pretty printing of arrays.  */
93
94 int prettyprint_arrays;
95
96 /* If nonzero, causes unions inside structures or other unions to be
97    printed. */
98
99 int unionprint;                 /* Controls printing of nested unions.  */
100
101 /* If nonzero, causes machine addresses to be printed in certain contexts. */
102
103 int addressprint;               /* Controls printing of machine addresses */
104 \f
105
106 /* Print data of type TYPE located at VALADDR (within GDB), which came from
107    the inferior at address ADDRESS, onto stdio stream STREAM according to
108    FORMAT (a letter, or 0 for natural format using TYPE).
109
110    If DEREF_REF is nonzero, then dereference references, otherwise just print
111    them like pointers.
112
113    The PRETTY parameter controls prettyprinting.
114
115    If the data are a string pointer, returns the number of string characters
116    printed.
117
118    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
119    enhanced to be able to debug more than the single target it was compiled
120    for (specific CPU type and thus specific target byte ordering), then
121    either the print routines are going to have to take this into account,
122    or the data is going to have to be passed into here already converted
123    to the host byte ordering, whichever is more convenient. */
124
125
126 int
127 val_print (type, valaddr, embedded_offset, address,
128            stream, format, deref_ref, recurse, pretty)
129      struct type *type;
130      char *valaddr;
131      int embedded_offset;
132      CORE_ADDR address;
133      GDB_FILE *stream;
134      int format;
135      int deref_ref;
136      int recurse;
137      enum val_prettyprint pretty;
138 {
139   struct type *real_type = check_typedef (type);
140   if (pretty == Val_pretty_default)
141     {
142       pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
143     }
144
145   QUIT;
146
147   /* Ensure that the type is complete and not just a stub.  If the type is
148      only a stub and we can't find and substitute its complete type, then
149      print appropriate string and return.  */
150
151   if (TYPE_FLAGS (real_type) & TYPE_FLAG_STUB)
152     {
153       fprintf_filtered (stream, "<incomplete type>");
154       gdb_flush (stream);
155       return (0);
156     }
157
158   return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
159                         stream, format, deref_ref, recurse, pretty));
160 }
161
162 /* Print the value VAL in C-ish syntax on stream STREAM.
163    FORMAT is a format-letter, or 0 for print in natural format of data type.
164    If the object printed is a string pointer, returns
165    the number of string bytes printed.  */
166
167 int
168 value_print (val, stream, format, pretty)
169      value_ptr val;
170      GDB_FILE *stream;
171      int format;
172      enum val_prettyprint pretty;
173 {
174   if (val == 0)
175     {
176       printf_filtered ("<address of value unknown>");
177       return 0;
178     }
179   if (VALUE_OPTIMIZED_OUT (val))
180     {
181       printf_filtered ("<value optimized out>");
182       return 0;
183     }
184   return LA_VALUE_PRINT (val, stream, format, pretty);
185 }
186
187 /* Called by various <lang>_val_print routines to print
188    TYPE_CODE_INT's.  TYPE is the type.  VALADDR is the address of the
189    value.  STREAM is where to print the value.  */
190
191 void
192 val_print_type_code_int (type, valaddr, stream)
193      struct type *type;
194      char *valaddr;
195      GDB_FILE *stream;
196 {
197   if (TYPE_LENGTH (type) > sizeof (LONGEST))
198     {
199       LONGEST val;
200
201       if (TYPE_UNSIGNED (type)
202           && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
203                                             &val))
204         {
205           print_longest (stream, 'u', 0, val);
206         }
207       else
208         {
209           /* Signed, or we couldn't turn an unsigned value into a
210              LONGEST.  For signed values, one could assume two's
211              complement (a reasonable assumption, I think) and do
212              better than this.  */
213           print_hex_chars (stream, (unsigned char *) valaddr,
214                            TYPE_LENGTH (type));
215         }
216     }
217   else
218     {
219 #ifdef PRINT_TYPELESS_INTEGER
220       PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
221 #else
222       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
223                      unpack_long (type, valaddr));
224 #endif
225     }
226 }
227
228 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
229    The raison d'etre of this function is to consolidate printing of 
230    LONG_LONG's into this one function.  Some platforms have long longs but
231    don't have a printf() that supports "ll" in the format string.  We handle
232    these by seeing if the number is representable as either a signed or
233    unsigned long, depending upon what format is desired, and if not we just
234    bail out and print the number in hex.
235
236    The format chars b,h,w,g are from print_scalar_formatted().  If USE_LOCAL,
237    format it according to the current language (this should be used for most
238    integers which GDB prints, the exception is things like protocols where
239    the format of the integer is a protocol thing, not a user-visible thing).
240  */
241
242 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
243 static void print_decimal PARAMS ((GDB_FILE * stream, char *sign, int use_local, ULONGEST val_ulong));
244 static void
245 print_decimal (stream, sign, use_local, val_ulong)
246      GDB_FILE *stream;
247      char *sign;
248      int use_local;
249      ULONGEST val_ulong;
250 {
251   unsigned long temp[3];
252   int i = 0;
253   do
254     {
255       temp[i] = val_ulong % (1000 * 1000 * 1000);
256       val_ulong /= (1000 * 1000 * 1000);
257       i++;
258     }
259   while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
260   switch (i)
261     {
262     case 1:
263       fprintf_filtered (stream, "%s%lu",
264                         sign, temp[0]);
265       break;
266     case 2:
267       fprintf_filtered (stream, "%s%lu%09lu",
268                         sign, temp[1], temp[0]);
269       break;
270     case 3:
271       fprintf_filtered (stream, "%s%lu%09lu%09lu",
272                         sign, temp[2], temp[1], temp[0]);
273       break;
274     default:
275       abort ();
276     }
277   return;
278 }
279 #endif
280
281 void
282 print_longest (stream, format, use_local, val_long)
283      GDB_FILE *stream;
284      int format;
285      int use_local;
286      LONGEST val_long;
287 {
288 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
289   if (sizeof (long) < sizeof (LONGEST))
290     {
291       switch (format)
292         {
293         case 'd':
294           {
295             /* Print a signed value, that doesn't fit in a long */
296             if ((long) val_long != val_long)
297               {
298                 if (val_long < 0)
299                   print_decimal (stream, "-", use_local, -val_long);
300                 else
301                   print_decimal (stream, "", use_local, val_long);
302                 return;
303               }
304             break;
305           }
306         case 'u':
307           {
308             /* Print an unsigned value, that doesn't fit in a long */
309             if ((unsigned long) val_long != (ULONGEST) val_long)
310               {
311                 print_decimal (stream, "", use_local, val_long);
312                 return;
313               }
314             break;
315           }
316         case 'x':
317         case 'o':
318         case 'b':
319         case 'h':
320         case 'w':
321         case 'g':
322           /* Print as unsigned value, must fit completely in unsigned long */
323           {
324             unsigned long temp = val_long;
325             if (temp != val_long)
326               {
327                 /* Urk, can't represent value in long so print in hex.
328                    Do shift in two operations so that if sizeof (long)
329                    == sizeof (LONGEST) we can avoid warnings from
330                    picky compilers about shifts >= the size of the
331                    shiftee in bits */
332                 unsigned long vbot = (unsigned long) val_long;
333                 LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
334                 unsigned long vtop = temp >> 1;
335                 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
336                 return;
337               }
338             break;
339           }
340         }
341     }
342 #endif
343
344 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
345   switch (format)
346     {
347     case 'd':
348       fprintf_filtered (stream,
349                         use_local ? local_decimal_format_custom ("ll")
350                         : "%lld",
351                         val_long);
352       break;
353     case 'u':
354       fprintf_filtered (stream, "%llu", val_long);
355       break;
356     case 'x':
357       fprintf_filtered (stream,
358                         use_local ? local_hex_format_custom ("ll")
359                         : "%llx",
360                         val_long);
361       break;
362     case 'o':
363       fprintf_filtered (stream,
364                         use_local ? local_octal_format_custom ("ll")
365                         : "%llo",
366                         val_long);
367       break;
368     case 'b':
369       fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
370       break;
371     case 'h':
372       fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
373       break;
374     case 'w':
375       fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
376       break;
377     case 'g':
378       fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
379       break;
380     default:
381       abort ();
382     }
383 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
384   /* In the following it is important to coerce (val_long) to a long. It does
385      nothing if !LONG_LONG, but it will chop off the top half (which we know
386      we can ignore) if the host supports long longs.  */
387
388   switch (format)
389     {
390     case 'd':
391       fprintf_filtered (stream,
392                         use_local ? local_decimal_format_custom ("l")
393                         : "%ld",
394                         (long) val_long);
395       break;
396     case 'u':
397       fprintf_filtered (stream, "%lu", (unsigned long) val_long);
398       break;
399     case 'x':
400       fprintf_filtered (stream,
401                         use_local ? local_hex_format_custom ("l")
402                         : "%lx",
403                         (unsigned long) val_long);
404       break;
405     case 'o':
406       fprintf_filtered (stream,
407                         use_local ? local_octal_format_custom ("l")
408                         : "%lo",
409                         (unsigned long) val_long);
410       break;
411     case 'b':
412       fprintf_filtered (stream, local_hex_format_custom ("02l"),
413                         (unsigned long) val_long);
414       break;
415     case 'h':
416       fprintf_filtered (stream, local_hex_format_custom ("04l"),
417                         (unsigned long) val_long);
418       break;
419     case 'w':
420       fprintf_filtered (stream, local_hex_format_custom ("08l"),
421                         (unsigned long) val_long);
422       break;
423     case 'g':
424       fprintf_filtered (stream, local_hex_format_custom ("016l"),
425                         (unsigned long) val_long);
426       break;
427     default:
428       abort ();
429     }
430 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
431 }
432
433 #if 0
434 void
435 strcat_longest (format, use_local, val_long, buf, buflen)
436      int format;
437      int use_local;
438      LONGEST val_long;
439      char *buf;
440      int buflen;                /* ignored, for now */
441 {
442 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
443   long vtop, vbot;
444
445   vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
446   vbot = (long) val_long;
447
448   if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
449       || ((format == 'u' || format == 'x') && (unsigned long long) val_long > UINT_MAX))
450     {
451       sprintf (buf, "0x%lx%08lx", vtop, vbot);
452       return;
453     }
454 #endif
455
456 #ifdef PRINTF_HAS_LONG_LONG
457   switch (format)
458     {
459     case 'd':
460       sprintf (buf,
461                (use_local ? local_decimal_format_custom ("ll") : "%lld"),
462                val_long);
463       break;
464     case 'u':
465       sprintf (buf, "%llu", val_long);
466       break;
467     case 'x':
468       sprintf (buf,
469                (use_local ? local_hex_format_custom ("ll") : "%llx"),
470
471                val_long);
472       break;
473     case 'o':
474       sprintf (buf,
475                (use_local ? local_octal_format_custom ("ll") : "%llo"),
476                val_long);
477       break;
478     case 'b':
479       sprintf (buf, local_hex_format_custom ("02ll"), val_long);
480       break;
481     case 'h':
482       sprintf (buf, local_hex_format_custom ("04ll"), val_long);
483       break;
484     case 'w':
485       sprintf (buf, local_hex_format_custom ("08ll"), val_long);
486       break;
487     case 'g':
488       sprintf (buf, local_hex_format_custom ("016ll"), val_long);
489       break;
490     default:
491       abort ();
492     }
493 #else /* !PRINTF_HAS_LONG_LONG */
494   /* In the following it is important to coerce (val_long) to a long. It does
495      nothing if !LONG_LONG, but it will chop off the top half (which we know
496      we can ignore) if the host supports long longs.  */
497
498   switch (format)
499     {
500     case 'd':
501       sprintf (buf, (use_local ? local_decimal_format_custom ("l") : "%ld"),
502                ((long) val_long));
503       break;
504     case 'u':
505       sprintf (buf, "%lu", ((unsigned long) val_long));
506       break;
507     case 'x':
508       sprintf (buf, (use_local ? local_hex_format_custom ("l") : "%lx"),
509                ((long) val_long));
510       break;
511     case 'o':
512       sprintf (buf, (use_local ? local_octal_format_custom ("l") : "%lo"),
513                ((long) val_long));
514       break;
515     case 'b':
516       sprintf (buf, local_hex_format_custom ("02l"),
517                ((long) val_long));
518       break;
519     case 'h':
520       sprintf (buf, local_hex_format_custom ("04l"),
521                ((long) val_long));
522       break;
523     case 'w':
524       sprintf (buf, local_hex_format_custom ("08l"),
525                ((long) val_long));
526       break;
527     case 'g':
528       sprintf (buf, local_hex_format_custom ("016l"),
529                ((long) val_long));
530       break;
531     default:
532       abort ();
533     }
534
535 #endif /* !PRINTF_HAS_LONG_LONG */
536 }
537 #endif
538
539 /* This used to be a macro, but I don't think it is called often enough
540    to merit such treatment.  */
541 /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
542    arguments to a function, number in a value history, register number, etc.)
543    where the value must not be larger than can fit in an int.  */
544
545 int
546 longest_to_int (arg)
547      LONGEST arg;
548 {
549   /* Let the compiler do the work */
550   int rtnval = (int) arg;
551
552   /* Check for overflows or underflows */
553   if (sizeof (LONGEST) > sizeof (int))
554     {
555       if (rtnval != arg)
556         {
557           error ("Value out of range.");
558         }
559     }
560   return (rtnval);
561 }
562
563 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
564    on STREAM.  */
565
566 void
567 print_floating (valaddr, type, stream)
568      char *valaddr;
569      struct type *type;
570      GDB_FILE *stream;
571 {
572   DOUBLEST doub;
573   int inv;
574   unsigned len = TYPE_LENGTH (type);
575
576 #if defined (IEEE_FLOAT)
577
578   /* Check for NaN's.  Note that this code does not depend on us being
579      on an IEEE conforming system.  It only depends on the target
580      machine using IEEE representation.  This means (a)
581      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
582      be defined for systems like the 68881, which uses IEEE
583      representation, but is not IEEE conforming.  */
584
585   {
586     unsigned long low, high;
587     /* Is the sign bit 0?  */
588     int nonnegative;
589     /* Is it is a NaN (i.e. the exponent is all ones and
590        the fraction is nonzero)?  */
591     int is_nan;
592
593     /* For lint, initialize these two variables to suppress warning: */
594     low = high = nonnegative = 0;
595     if (len == 4)
596       {
597         /* It's single precision.  */
598         /* Assume that floating point byte order is the same as
599            integer byte order.  */
600         low = extract_unsigned_integer (valaddr, 4);
601         nonnegative = ((low & 0x80000000) == 0);
602         is_nan = ((((low >> 23) & 0xFF) == 0xFF)
603                   && 0 != (low & 0x7FFFFF));
604         low &= 0x7fffff;
605         high = 0;
606       }
607     else if (len == 8)
608       {
609         /* It's double precision.  Get the high and low words.  */
610
611         /* Assume that floating point byte order is the same as
612            integer byte order.  */
613         if (TARGET_BYTE_ORDER == BIG_ENDIAN)
614           {
615             low = extract_unsigned_integer (valaddr + 4, 4);
616             high = extract_unsigned_integer (valaddr, 4);
617           }
618         else
619           {
620             low = extract_unsigned_integer (valaddr, 4);
621             high = extract_unsigned_integer (valaddr + 4, 4);
622           }
623         nonnegative = ((high & 0x80000000) == 0);
624         is_nan = (((high >> 20) & 0x7ff) == 0x7ff
625                   && !((((high & 0xfffff) == 0)) && (low == 0)));
626         high &= 0xfffff;
627       }
628     else
629       {
630 #ifdef TARGET_ANALYZE_FLOATING
631         TARGET_ANALYZE_FLOATING;
632 #else
633         /* Extended.  We can't detect extended NaNs for this target.
634            Also note that currently extendeds get nuked to double in
635            REGISTER_CONVERTIBLE.  */
636         is_nan = 0;
637 #endif 
638       }
639
640     if (is_nan)
641       {
642         /* The meaning of the sign and fraction is not defined by IEEE.
643            But the user might know what they mean.  For example, they
644            (in an implementation-defined manner) distinguish between
645            signaling and quiet NaN's.  */
646         if (high)
647           fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + !!nonnegative,
648                             high, low);
649         else
650           fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
651         return;
652       }
653   }
654 #endif /* IEEE_FLOAT.  */
655
656   doub = unpack_double (type, valaddr, &inv);
657   if (inv)
658     {
659       fprintf_filtered (stream, "<invalid float value>");
660       return;
661     }
662
663   if (len < sizeof (double))
664       fprintf_filtered (stream, "%.9g", (double) doub);
665   else if (len == sizeof (double))
666       fprintf_filtered (stream, "%.17g", (double) doub);
667   else
668 #ifdef PRINTF_HAS_LONG_DOUBLE
669     fprintf_filtered (stream, "%.35Lg", doub);
670 #else
671     /* This at least wins with values that are representable as doubles */
672     fprintf_filtered (stream, "%.17g", (double) doub);
673 #endif
674 }
675
676 void
677 print_binary_chars (stream, valaddr, len)
678      GDB_FILE *stream;
679      unsigned char *valaddr;
680      unsigned len;
681 {
682
683 #define BITS_IN_BYTES 8
684
685   unsigned char *p;
686   int i;
687   int b;
688
689   /* Declared "int" so it will be signed.
690    * This ensures that right shift will shift in zeros.
691    */
692   const int mask = 0x080;
693
694   /* FIXME: We should be not printing leading zeroes in most cases.  */
695
696   fprintf_filtered (stream, local_binary_format_prefix ());
697   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
698     {
699       for (p = valaddr;
700            p < valaddr + len;
701            p++)
702         {
703           /* Every byte has 8 binary characters; peel off
704            * and print from the MSB end.
705            */
706           for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
707             {
708               if (*p & (mask >> i))
709                 b = 1;
710               else
711                 b = 0;
712
713               fprintf_filtered (stream, "%1d", b);
714             }
715         }
716     }
717   else
718     {
719       for (p = valaddr + len - 1;
720            p >= valaddr;
721            p--)
722         {
723           for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
724             {
725               if (*p & (mask >> i))
726                 b = 1;
727               else
728                 b = 0;
729
730               fprintf_filtered (stream, "%1d", b);
731             }
732         }
733     }
734   fprintf_filtered (stream, local_binary_format_suffix ());
735 }
736
737 /* VALADDR points to an integer of LEN bytes.
738  * Print it in octal on stream or format it in buf.
739  */
740 void
741 print_octal_chars (stream, valaddr, len)
742      GDB_FILE *stream;
743      unsigned char *valaddr;
744      unsigned len;
745 {
746   unsigned char *p;
747   unsigned char octa1, octa2, octa3, carry;
748   int cycle;
749
750   /* FIXME: We should be not printing leading zeroes in most cases.  */
751
752
753   /* Octal is 3 bits, which doesn't fit.  Yuk.  So we have to track
754    * the extra bits, which cycle every three bytes:
755    *
756    * Byte side:       0            1             2          3
757    *                         |             |            |            |
758    * bit number   123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
759    *
760    * Octal side:   0   1   carry  3   4  carry ...
761    *
762    * Cycle number:    0             1            2
763    *
764    * But of course we are printing from the high side, so we have to
765    * figure out where in the cycle we are so that we end up with no
766    * left over bits at the end.
767    */
768 #define BITS_IN_OCTAL 3
769 #define HIGH_ZERO     0340
770 #define LOW_ZERO      0016
771 #define CARRY_ZERO    0003
772 #define HIGH_ONE      0200
773 #define MID_ONE       0160
774 #define LOW_ONE       0016
775 #define CARRY_ONE     0001
776 #define HIGH_TWO      0300
777 #define MID_TWO       0070
778 #define LOW_TWO       0007
779
780   /* For 32 we start in cycle 2, with two bits and one bit carry;
781    * for 64 in cycle in cycle 1, with one bit and a two bit carry.
782    */
783   cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
784   carry = 0;
785
786   fprintf_filtered (stream, local_octal_format_prefix ());
787   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
788     {
789       for (p = valaddr;
790            p < valaddr + len;
791            p++)
792         {
793           switch (cycle)
794             {
795             case 0:
796               /* No carry in, carry out two bits.
797                */
798               octa1 = (HIGH_ZERO & *p) >> 5;
799               octa2 = (LOW_ZERO & *p) >> 2;
800               carry = (CARRY_ZERO & *p);
801               fprintf_filtered (stream, "%o", octa1);
802               fprintf_filtered (stream, "%o", octa2);
803               break;
804
805             case 1:
806               /* Carry in two bits, carry out one bit.
807                */
808               octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
809               octa2 = (MID_ONE & *p) >> 4;
810               octa3 = (LOW_ONE & *p) >> 1;
811               carry = (CARRY_ONE & *p);
812               fprintf_filtered (stream, "%o", octa1);
813               fprintf_filtered (stream, "%o", octa2);
814               fprintf_filtered (stream, "%o", octa3);
815               break;
816
817             case 2:
818               /* Carry in one bit, no carry out.
819                */
820               octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
821               octa2 = (MID_TWO & *p) >> 3;
822               octa3 = (LOW_TWO & *p);
823               carry = 0;
824               fprintf_filtered (stream, "%o", octa1);
825               fprintf_filtered (stream, "%o", octa2);
826               fprintf_filtered (stream, "%o", octa3);
827               break;
828
829             default:
830               error ("Internal error in octal conversion;");
831             }
832
833           cycle++;
834           cycle = cycle % BITS_IN_OCTAL;
835         }
836     }
837   else
838     {
839       for (p = valaddr + len - 1;
840            p >= valaddr;
841            p--)
842         {
843           switch (cycle)
844             {
845             case 0:
846               /* Carry out, no carry in */
847               octa1 = (HIGH_ZERO & *p) >> 5;
848               octa2 = (LOW_ZERO & *p) >> 2;
849               carry = (CARRY_ZERO & *p);
850               fprintf_filtered (stream, "%o", octa1);
851               fprintf_filtered (stream, "%o", octa2);
852               break;
853
854             case 1:
855               /* Carry in, carry out */
856               octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
857               octa2 = (MID_ONE & *p) >> 4;
858               octa3 = (LOW_ONE & *p) >> 1;
859               carry = (CARRY_ONE & *p);
860               fprintf_filtered (stream, "%o", octa1);
861               fprintf_filtered (stream, "%o", octa2);
862               fprintf_filtered (stream, "%o", octa3);
863               break;
864
865             case 2:
866               /* Carry in, no carry out */
867               octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
868               octa2 = (MID_TWO & *p) >> 3;
869               octa3 = (LOW_TWO & *p);
870               carry = 0;
871               fprintf_filtered (stream, "%o", octa1);
872               fprintf_filtered (stream, "%o", octa2);
873               fprintf_filtered (stream, "%o", octa3);
874               break;
875
876             default:
877               error ("Internal error in octal conversion;");
878             }
879
880           cycle++;
881           cycle = cycle % BITS_IN_OCTAL;
882         }
883     }
884
885   fprintf_filtered (stream, local_octal_format_suffix ());
886 }
887
888 /* VALADDR points to an integer of LEN bytes.
889  * Print it in decimal on stream or format it in buf.
890  */
891 void
892 print_decimal_chars (stream, valaddr, len)
893      GDB_FILE *stream;
894      unsigned char *valaddr;
895      unsigned len;
896 {
897 #define TEN             10
898 #define TWO_TO_FOURTH   16
899 #define CARRY_OUT(  x ) ((x) / TEN)     /* extend char to int */
900 #define CARRY_LEFT( x ) ((x) % TEN)
901 #define SHIFT( x )      ((x) << 4)
902 #define START_P \
903         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
904 #define NOT_END_P \
905         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
906 #define NEXT_P \
907         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
908 #define LOW_NIBBLE(  x ) ( (x) & 0x00F)
909 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
910
911   unsigned char *p;
912   unsigned char *digits;
913   int carry;
914   int decimal_len;
915   int i, j, decimal_digits;
916   int dummy;
917   int flip;
918
919   /* Base-ten number is less than twice as many digits
920    * as the base 16 number, which is 2 digits per byte.
921    */
922   decimal_len = len * 2 * 2;
923   digits = (unsigned char *) malloc (decimal_len);
924   if (digits == NULL)
925     error ("Can't allocate memory for conversion to decimal.");
926
927   for (i = 0; i < decimal_len; i++)
928     {
929       digits[i] = 0;
930     }
931
932   fprintf_filtered (stream, local_decimal_format_prefix ());
933
934   /* Ok, we have an unknown number of bytes of data to be printed in
935    * decimal.
936    *
937    * Given a hex number (in nibbles) as XYZ, we start by taking X and
938    * decemalizing it as "x1 x2" in two decimal nibbles.  Then we multiply
939    * the nibbles by 16, add Y and re-decimalize.  Repeat with Z.
940    *
941    * The trick is that "digits" holds a base-10 number, but sometimes
942    * the individual digits are > 10. 
943    *
944    * Outer loop is per nibble (hex digit) of input, from MSD end to
945    * LSD end.
946    */
947   decimal_digits = 0;           /* Number of decimal digits so far */
948   p = START_P;
949   flip = 0;
950   while (NOT_END_P)
951     {
952       /*
953        * Multiply current base-ten number by 16 in place.
954        * Each digit was between 0 and 9, now is between
955        * 0 and 144.
956        */
957       for (j = 0; j < decimal_digits; j++)
958         {
959           digits[j] = SHIFT (digits[j]);
960         }
961
962       /* Take the next nibble off the input and add it to what
963        * we've got in the LSB position.  Bottom 'digit' is now
964        * between 0 and 159.
965        *
966        * "flip" is used to run this loop twice for each byte.
967        */
968       if (flip == 0)
969         {
970           /* Take top nibble.
971            */
972           digits[0] += HIGH_NIBBLE (*p);
973           flip = 1;
974         }
975       else
976         {
977           /* Take low nibble and bump our pointer "p".
978            */
979           digits[0] += LOW_NIBBLE (*p);
980           NEXT_P;
981           flip = 0;
982         }
983
984       /* Re-decimalize.  We have to do this often enough
985        * that we don't overflow, but once per nibble is
986        * overkill.  Easier this way, though.  Note that the
987        * carry is often larger than 10 (e.g. max initial
988        * carry out of lowest nibble is 15, could bubble all
989        * the way up greater than 10).  So we have to do
990        * the carrying beyond the last current digit.
991        */
992       carry = 0;
993       for (j = 0; j < decimal_len - 1; j++)
994         {
995           digits[j] += carry;
996
997           /* "/" won't handle an unsigned char with
998            * a value that if signed would be negative.
999            * So extend to longword int via "dummy".
1000            */
1001           dummy = digits[j];
1002           carry = CARRY_OUT (dummy);
1003           digits[j] = CARRY_LEFT (dummy);
1004
1005           if (j >= decimal_digits && carry == 0)
1006             {
1007               /*
1008                * All higher digits are 0 and we
1009                * no longer have a carry.
1010                *
1011                * Note: "j" is 0-based, "decimal_digits" is
1012                *       1-based.
1013                */
1014               decimal_digits = j + 1;
1015               break;
1016             }
1017         }
1018     }
1019
1020   /* Ok, now "digits" is the decimal representation, with
1021    * the "decimal_digits" actual digits.  Print!
1022    */
1023   for (i = decimal_digits - 1; i >= 0; i--)
1024     {
1025       fprintf_filtered (stream, "%1d", digits[i]);
1026     }
1027   free (digits);
1028
1029   fprintf_filtered (stream, local_decimal_format_suffix ());
1030 }
1031
1032 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
1033
1034 static void
1035 print_hex_chars (stream, valaddr, len)
1036      GDB_FILE *stream;
1037      unsigned char *valaddr;
1038      unsigned len;
1039 {
1040   unsigned char *p;
1041
1042   /* FIXME: We should be not printing leading zeroes in most cases.  */
1043
1044   fprintf_filtered (stream, local_hex_format_prefix ());
1045   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1046     {
1047       for (p = valaddr;
1048            p < valaddr + len;
1049            p++)
1050         {
1051           fprintf_filtered (stream, "%02x", *p);
1052         }
1053     }
1054   else
1055     {
1056       for (p = valaddr + len - 1;
1057            p >= valaddr;
1058            p--)
1059         {
1060           fprintf_filtered (stream, "%02x", *p);
1061         }
1062     }
1063   fprintf_filtered (stream, local_hex_format_suffix ());
1064 }
1065
1066 /*  Called by various <lang>_val_print routines to print elements of an
1067    array in the form "<elem1>, <elem2>, <elem3>, ...".
1068
1069    (FIXME?)  Assumes array element separator is a comma, which is correct
1070    for all languages currently handled.
1071    (FIXME?)  Some languages have a notation for repeated array elements,
1072    perhaps we should try to use that notation when appropriate.
1073  */
1074
1075 void
1076 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
1077                           recurse, pretty, i)
1078      struct type *type;
1079      char *valaddr;
1080      CORE_ADDR address;
1081      GDB_FILE *stream;
1082      int format;
1083      int deref_ref;
1084      int recurse;
1085      enum val_prettyprint pretty;
1086      unsigned int i;
1087 {
1088   unsigned int things_printed = 0;
1089   unsigned len;
1090   struct type *elttype;
1091   unsigned eltlen;
1092   /* Position of the array element we are examining to see
1093      whether it is repeated.  */
1094   unsigned int rep1;
1095   /* Number of repetitions we have detected so far.  */
1096   unsigned int reps;
1097
1098   elttype = TYPE_TARGET_TYPE (type);
1099   eltlen = TYPE_LENGTH (check_typedef (elttype));
1100   len = TYPE_LENGTH (type) / eltlen;
1101
1102   annotate_array_section_begin (i, elttype);
1103
1104   for (; i < len && things_printed < print_max; i++)
1105     {
1106       if (i != 0)
1107         {
1108           if (prettyprint_arrays)
1109             {
1110               fprintf_filtered (stream, ",\n");
1111               print_spaces_filtered (2 + 2 * recurse, stream);
1112             }
1113           else
1114             {
1115               fprintf_filtered (stream, ", ");
1116             }
1117         }
1118       wrap_here (n_spaces (2 + 2 * recurse));
1119
1120       rep1 = i + 1;
1121       reps = 1;
1122       while ((rep1 < len) &&
1123              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1124         {
1125           ++reps;
1126           ++rep1;
1127         }
1128
1129       if (reps > repeat_count_threshold)
1130         {
1131           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1132                      deref_ref, recurse + 1, pretty);
1133           annotate_elt_rep (reps);
1134           fprintf_filtered (stream, " <repeats %u times>", reps);
1135           annotate_elt_rep_end ();
1136
1137           i = rep1 - 1;
1138           things_printed += repeat_count_threshold;
1139         }
1140       else
1141         {
1142           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1143                      deref_ref, recurse + 1, pretty);
1144           annotate_elt ();
1145           things_printed++;
1146         }
1147     }
1148   annotate_array_section_end ();
1149   if (i < len)
1150     {
1151       fprintf_filtered (stream, "...");
1152     }
1153 }
1154
1155 /* Read LEN bytes of target memory at address MEMADDR, placing the
1156    results in GDB's memory at MYADDR.  Returns a count of the bytes
1157    actually read, and optionally an errno value in the location
1158    pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1159
1160 /* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
1161    function be eliminated.  */
1162
1163 static int
1164 partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
1165 {
1166   int nread;                    /* Number of bytes actually read. */
1167   int errcode;                  /* Error from last read. */
1168
1169   /* First try a complete read. */
1170   errcode = target_read_memory (memaddr, myaddr, len);
1171   if (errcode == 0)
1172     {
1173       /* Got it all. */
1174       nread = len;
1175     }
1176   else
1177     {
1178       /* Loop, reading one byte at a time until we get as much as we can. */
1179       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1180         {
1181           errcode = target_read_memory (memaddr++, myaddr++, 1);
1182         }
1183       /* If an error, the last read was unsuccessful, so adjust count. */
1184       if (errcode != 0)
1185         {
1186           nread--;
1187         }
1188     }
1189   if (errnoptr != NULL)
1190     {
1191       *errnoptr = errcode;
1192     }
1193   return (nread);
1194 }
1195
1196 /*  Print a string from the inferior, starting at ADDR and printing up to LEN
1197    characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
1198    stops at the first null byte, otherwise printing proceeds (including null
1199    bytes) until either print_max or LEN characters have been printed,
1200    whichever is smaller. */
1201
1202 /* FIXME: Use target_read_string.  */
1203
1204 int
1205 val_print_string (addr, len, width, stream)
1206      CORE_ADDR addr;
1207      int len;
1208      int width;
1209      GDB_FILE *stream;
1210 {
1211   int force_ellipsis = 0;       /* Force ellipsis to be printed if nonzero. */
1212   int errcode;                  /* Errno returned from bad reads. */
1213   unsigned int fetchlimit;      /* Maximum number of chars to print. */
1214   unsigned int nfetch;          /* Chars to fetch / chars fetched. */
1215   unsigned int chunksize;       /* Size of each fetch, in chars. */
1216   char *buffer = NULL;          /* Dynamically growable fetch buffer. */
1217   char *bufptr;                 /* Pointer to next available byte in buffer. */
1218   char *limit;                  /* First location past end of fetch buffer. */
1219   struct cleanup *old_chain = NULL;     /* Top of the old cleanup chain. */
1220   int found_nul;                /* Non-zero if we found the nul char */
1221
1222   /* First we need to figure out the limit on the number of characters we are
1223      going to attempt to fetch and print.  This is actually pretty simple.  If
1224      LEN >= zero, then the limit is the minimum of LEN and print_max.  If
1225      LEN is -1, then the limit is print_max.  This is true regardless of
1226      whether print_max is zero, UINT_MAX (unlimited), or something in between,
1227      because finding the null byte (or available memory) is what actually
1228      limits the fetch. */
1229
1230   fetchlimit = (len == -1 ? print_max : min (len, print_max));
1231
1232   /* Now decide how large of chunks to try to read in one operation.  This
1233      is also pretty simple.  If LEN >= zero, then we want fetchlimit chars,
1234      so we might as well read them all in one operation.  If LEN is -1, we
1235      are looking for a null terminator to end the fetching, so we might as
1236      well read in blocks that are large enough to be efficient, but not so
1237      large as to be slow if fetchlimit happens to be large.  So we choose the
1238      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
1239      200 is way too big for remote debugging over a serial line.  */
1240
1241   chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1242
1243   /* Loop until we either have all the characters to print, or we encounter
1244      some error, such as bumping into the end of the address space. */
1245
1246   found_nul = 0;
1247   old_chain = make_cleanup (null_cleanup, 0);
1248
1249   if (len > 0)
1250     {
1251       buffer = (char *) xmalloc (len * width);
1252       bufptr = buffer;
1253       old_chain = make_cleanup (free, buffer);
1254
1255       nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1256         / width;
1257       addr += nfetch * width;
1258       bufptr += nfetch * width;
1259     }
1260   else if (len == -1)
1261     {
1262       unsigned long bufsize = 0;
1263       do
1264         {
1265           QUIT;
1266           nfetch = min (chunksize, fetchlimit - bufsize);
1267
1268           if (buffer == NULL)
1269             buffer = (char *) xmalloc (nfetch * width);
1270           else
1271             {
1272               discard_cleanups (old_chain);
1273               buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1274             }
1275
1276           old_chain = make_cleanup (free, buffer);
1277           bufptr = buffer + bufsize * width;
1278           bufsize += nfetch;
1279
1280           /* Read as much as we can. */
1281           nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1282             / width;
1283
1284           /* Scan this chunk for the null byte that terminates the string
1285              to print.  If found, we don't need to fetch any more.  Note
1286              that bufptr is explicitly left pointing at the next character
1287              after the null byte, or at the next character after the end of
1288              the buffer. */
1289
1290           limit = bufptr + nfetch * width;
1291           while (bufptr < limit)
1292             {
1293               unsigned long c;
1294
1295               c = extract_unsigned_integer (bufptr, width);
1296               addr += width;
1297               bufptr += width;
1298               if (c == 0)
1299                 {
1300                   /* We don't care about any error which happened after
1301                      the NULL terminator.  */
1302                   errcode = 0;
1303                   found_nul = 1;
1304                   break;
1305                 }
1306             }
1307         }
1308       while (errcode == 0       /* no error */
1309              && bufptr - buffer < fetchlimit * width    /* no overrun */
1310              && !found_nul);    /* haven't found nul yet */
1311     }
1312   else
1313     {                           /* length of string is really 0! */
1314       buffer = bufptr = NULL;
1315       errcode = 0;
1316     }
1317
1318   /* bufptr and addr now point immediately beyond the last byte which we
1319      consider part of the string (including a '\0' which ends the string).  */
1320
1321   /* We now have either successfully filled the buffer to fetchlimit, or
1322      terminated early due to an error or finding a null char when LEN is -1. */
1323
1324   if (len == -1 && !found_nul)
1325     {
1326       char *peekbuf;
1327
1328       /* We didn't find a null terminator we were looking for.  Attempt
1329          to peek at the next character.  If not successful, or it is not
1330          a null byte, then force ellipsis to be printed.  */
1331
1332       peekbuf = (char *) alloca (width);
1333
1334       if (target_read_memory (addr, peekbuf, width) == 0
1335           && extract_unsigned_integer (peekbuf, width) != 0)
1336         force_ellipsis = 1;
1337     }
1338   else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1339     {
1340       /* Getting an error when we have a requested length, or fetching less
1341          than the number of characters actually requested, always make us
1342          print ellipsis. */
1343       force_ellipsis = 1;
1344     }
1345
1346   QUIT;
1347
1348   /* If we get an error before fetching anything, don't print a string.
1349      But if we fetch something and then get an error, print the string
1350      and then the error message.  */
1351   if (errcode == 0 || bufptr > buffer)
1352     {
1353       if (addressprint)
1354         {
1355           fputs_filtered (" ", stream);
1356         }
1357       LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
1358     }
1359
1360   if (errcode != 0)
1361     {
1362       if (errcode == EIO)
1363         {
1364           fprintf_filtered (stream, " <Address ");
1365           print_address_numeric (addr, 1, stream);
1366           fprintf_filtered (stream, " out of bounds>");
1367         }
1368       else
1369         {
1370           fprintf_filtered (stream, " <Error reading address ");
1371           print_address_numeric (addr, 1, stream);
1372           fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1373         }
1374     }
1375   gdb_flush (stream);
1376   do_cleanups (old_chain);
1377   return ((bufptr - buffer) / width);
1378 }
1379 \f
1380
1381 /* Validate an input or output radix setting, and make sure the user
1382    knows what they really did here.  Radix setting is confusing, e.g.
1383    setting the input radix to "10" never changes it!  */
1384
1385 /* ARGSUSED */
1386 static void
1387 set_input_radix (args, from_tty, c)
1388      char *args;
1389      int from_tty;
1390      struct cmd_list_element *c;
1391 {
1392   set_input_radix_1 (from_tty, *(unsigned *) c->var);
1393 }
1394
1395 /* ARGSUSED */
1396 static void
1397 set_input_radix_1 (from_tty, radix)
1398      int from_tty;
1399      unsigned radix;
1400 {
1401   /* We don't currently disallow any input radix except 0 or 1, which don't
1402      make any mathematical sense.  In theory, we can deal with any input
1403      radix greater than 1, even if we don't have unique digits for every
1404      value from 0 to radix-1, but in practice we lose on large radix values.
1405      We should either fix the lossage or restrict the radix range more.
1406      (FIXME). */
1407
1408   if (radix < 2)
1409     {
1410       error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1411              radix);
1412     }
1413   input_radix = radix;
1414   if (from_tty)
1415     {
1416       printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1417                        radix, radix, radix);
1418     }
1419 }
1420
1421 /* ARGSUSED */
1422 static void
1423 set_output_radix (args, from_tty, c)
1424      char *args;
1425      int from_tty;
1426      struct cmd_list_element *c;
1427 {
1428   set_output_radix_1 (from_tty, *(unsigned *) c->var);
1429 }
1430
1431 static void
1432 set_output_radix_1 (from_tty, radix)
1433      int from_tty;
1434      unsigned radix;
1435 {
1436   /* Validate the radix and disallow ones that we aren't prepared to
1437      handle correctly, leaving the radix unchanged. */
1438   switch (radix)
1439     {
1440     case 16:
1441       output_format = 'x';      /* hex */
1442       break;
1443     case 10:
1444       output_format = 0;        /* decimal */
1445       break;
1446     case 8:
1447       output_format = 'o';      /* octal */
1448       break;
1449     default:
1450       error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1451              radix);
1452     }
1453   output_radix = radix;
1454   if (from_tty)
1455     {
1456       printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1457                        radix, radix, radix);
1458     }
1459 }
1460
1461 /* Set both the input and output radix at once.  Try to set the output radix
1462    first, since it has the most restrictive range.  An radix that is valid as
1463    an output radix is also valid as an input radix.
1464
1465    It may be useful to have an unusual input radix.  If the user wishes to
1466    set an input radix that is not valid as an output radix, he needs to use
1467    the 'set input-radix' command. */
1468
1469 static void
1470 set_radix (arg, from_tty)
1471      char *arg;
1472      int from_tty;
1473 {
1474   unsigned radix;
1475
1476   radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
1477   set_output_radix_1 (0, radix);
1478   set_input_radix_1 (0, radix);
1479   if (from_tty)
1480     {
1481       printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1482                        radix, radix, radix);
1483     }
1484 }
1485
1486 /* Show both the input and output radices. */
1487
1488 /*ARGSUSED */
1489 static void
1490 show_radix (arg, from_tty)
1491      char *arg;
1492      int from_tty;
1493 {
1494   if (from_tty)
1495     {
1496       if (input_radix == output_radix)
1497         {
1498           printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1499                            input_radix, input_radix, input_radix);
1500         }
1501       else
1502         {
1503           printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1504                            input_radix, input_radix, input_radix);
1505           printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1506                            output_radix, output_radix, output_radix);
1507         }
1508     }
1509 }
1510 \f
1511
1512 /*ARGSUSED */
1513 static void
1514 set_print (arg, from_tty)
1515      char *arg;
1516      int from_tty;
1517 {
1518   printf_unfiltered (
1519      "\"set print\" must be followed by the name of a print subcommand.\n");
1520   help_list (setprintlist, "set print ", -1, gdb_stdout);
1521 }
1522
1523 /*ARGSUSED */
1524 static void
1525 show_print (args, from_tty)
1526      char *args;
1527      int from_tty;
1528 {
1529   cmd_show_list (showprintlist, from_tty, "");
1530 }
1531 \f
1532 void
1533 _initialize_valprint ()
1534 {
1535   struct cmd_list_element *c;
1536
1537   add_prefix_cmd ("print", no_class, set_print,
1538                   "Generic command for setting how things print.",
1539                   &setprintlist, "set print ", 0, &setlist);
1540   add_alias_cmd ("p", "print", no_class, 1, &setlist);
1541   /* prefer set print to set prompt */
1542   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1543
1544   add_prefix_cmd ("print", no_class, show_print,
1545                   "Generic command for showing print settings.",
1546                   &showprintlist, "show print ", 0, &showlist);
1547   add_alias_cmd ("p", "print", no_class, 1, &showlist);
1548   add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1549
1550   add_show_from_set
1551     (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
1552                   "Set limit on string chars or array elements to print.\n\
1553 \"set print elements 0\" causes there to be no limit.",
1554                   &setprintlist),
1555      &showprintlist);
1556
1557   add_show_from_set
1558     (add_set_cmd ("null-stop", no_class, var_boolean,
1559                   (char *) &stop_print_at_null,
1560                   "Set printing of char arrays to stop at first null char.",
1561                   &setprintlist),
1562      &showprintlist);
1563
1564   add_show_from_set
1565     (add_set_cmd ("repeats", no_class, var_uinteger,
1566                   (char *) &repeat_count_threshold,
1567                   "Set threshold for repeated print elements.\n\
1568 \"set print repeats 0\" causes all elements to be individually printed.",
1569                   &setprintlist),
1570      &showprintlist);
1571
1572   add_show_from_set
1573     (add_set_cmd ("pretty", class_support, var_boolean,
1574                   (char *) &prettyprint_structs,
1575                   "Set prettyprinting of structures.",
1576                   &setprintlist),
1577      &showprintlist);
1578
1579   add_show_from_set
1580     (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
1581                   "Set printing of unions interior to structures.",
1582                   &setprintlist),
1583      &showprintlist);
1584
1585   add_show_from_set
1586     (add_set_cmd ("array", class_support, var_boolean,
1587                   (char *) &prettyprint_arrays,
1588                   "Set prettyprinting of arrays.",
1589                   &setprintlist),
1590      &showprintlist);
1591
1592   add_show_from_set
1593     (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
1594                   "Set printing of addresses.",
1595                   &setprintlist),
1596      &showprintlist);
1597
1598   c = add_set_cmd ("input-radix", class_support, var_uinteger,
1599                    (char *) &input_radix,
1600                    "Set default input radix for entering numbers.",
1601                    &setlist);
1602   add_show_from_set (c, &showlist);
1603   c->function.sfunc = set_input_radix;
1604
1605   c = add_set_cmd ("output-radix", class_support, var_uinteger,
1606                    (char *) &output_radix,
1607                    "Set default output radix for printing of values.",
1608                    &setlist);
1609   add_show_from_set (c, &showlist);
1610   c->function.sfunc = set_output_radix;
1611
1612   /* The "set radix" and "show radix" commands are special in that they are
1613      like normal set and show commands but allow two normally independent
1614      variables to be either set or shown with a single command.  So the
1615      usual add_set_cmd() and add_show_from_set() commands aren't really
1616      appropriate. */
1617   add_cmd ("radix", class_support, set_radix,
1618            "Set default input and output number radices.\n\
1619 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1620 Without an argument, sets both radices back to the default value of 10.",
1621            &setlist);
1622   add_cmd ("radix", class_support, show_radix,
1623            "Show the default input and output number radices.\n\
1624 Use 'show input-radix' or 'show output-radix' to independently show each.",
1625            &showlist);
1626
1627   /* Give people the defaults which they are used to.  */
1628   prettyprint_structs = 0;
1629   prettyprint_arrays = 0;
1630   unionprint = 1;
1631   addressprint = 1;
1632   print_max = PRINT_MAX_DEFAULT;
1633 }