* defs.h (CC_HAS_LONG_LONG): Set up to define CC_HAS_LONG_LONG
[platform/upstream/binutils.git] / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include <string.h>
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "target.h"
28 #include "obstack.h"
29 #include "language.h"
30 #include "demangle.h"
31
32 #include <errno.h>
33
34 /* Prototypes for local functions */
35
36 static void
37 print_hex_chars PARAMS ((FILE *, unsigned char *, unsigned int));
38
39 static void
40 show_print PARAMS ((char *, int));
41
42 static void
43 set_print PARAMS ((char *, int));
44
45 static void
46 set_radix PARAMS ((char *, int, struct cmd_list_element *));
47
48 static void
49 set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
50
51 static void
52 value_print_array_elements PARAMS ((value, FILE *, int, enum val_prettyprint));
53
54 /* Maximum number of chars to print for a string pointer value
55    or vector contents, or UINT_MAX for no limit.  */
56
57 unsigned int print_max;
58
59 /* Default input and output radixes, and output format letter.  */
60
61 unsigned input_radix = 10;
62 unsigned output_radix = 10;
63 int output_format = 0;
64
65 /* Print repeat counts if there are more than this many repetitions of an
66    element in an array.  Referenced by the low level language dependent
67    print routines. */
68
69 unsigned int repeat_count_threshold = 10;
70
71 int prettyprint_structs;        /* Controls pretty printing of structures */
72 int prettyprint_arrays;         /* Controls pretty printing of arrays.  */
73
74 /* If nonzero, causes unions inside structures or other unions to be
75    printed. */
76
77 int unionprint;                 /* Controls printing of nested unions.  */
78
79 /* If nonzero, causes machine addresses to be printed in certain contexts. */
80
81 int addressprint;               /* Controls printing of machine addresses */
82
83 \f
84 /* Print data of type TYPE located at VALADDR (within GDB), which came from
85    the inferior at address ADDRESS, onto stdio stream STREAM according to
86    FORMAT (a letter, or 0 for natural format using TYPE).
87
88    If DEREF_REF is nonzero, then dereference references, otherwise just print
89    them like pointers.
90
91    The PRETTY parameter controls prettyprinting.
92
93    If the data are a string pointer, returns the number of string characters
94    printed.
95
96    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
97    enhanced to be able to debug more than the single target it was compiled
98    for (specific CPU type and thus specific target byte ordering), then
99    either the print routines are going to have to take this into account,
100    or the data is going to have to be passed into here already converted
101    to the host byte ordering, whichever is more convenient. */
102
103
104 int
105 val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty)
106      struct type *type;
107      char *valaddr;
108      CORE_ADDR address;
109      FILE *stream;
110      int format;
111      int deref_ref;
112      int recurse;
113      enum val_prettyprint pretty;
114 {
115   if (pretty == Val_pretty_default)
116     {
117       pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
118     }
119   
120   QUIT;
121
122   /* Ensure that the type is complete and not just a stub.  If the type is
123      only a stub and we can't find and substitute its complete type, then
124      print appropriate string and return.  Typical types that my be stubs
125      are structs, unions, and C++ methods. */
126
127   check_stub_type (type);
128   if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
129     {
130       fprintf_filtered (stream, "<incomplete type>");
131       fflush (stream);
132       return (0);
133     }
134   
135   return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref,
136                         recurse, pretty));
137 }
138
139 /* Print the value VAL in C-ish syntax on stream STREAM.
140    FORMAT is a format-letter, or 0 for print in natural format of data type.
141    If the object printed is a string pointer, returns
142    the number of string bytes printed.  */
143
144 int
145 value_print (val, stream, format, pretty)
146      value val;
147      FILE *stream;
148      int format;
149      enum val_prettyprint pretty;
150 {
151   register unsigned int n, typelen;
152
153   if (val == 0)
154     {
155       printf_filtered ("<address of value unknown>");
156       return 0;
157     }
158   if (VALUE_OPTIMIZED_OUT (val))
159     {
160       printf_filtered ("<value optimized out>");
161       return 0;
162     }
163
164   /* A "repeated" value really contains several values in a row.
165      They are made by the @ operator.
166      Print such values as if they were arrays.  */
167
168   if (VALUE_REPEATED (val))
169     {
170       n = VALUE_REPETITIONS (val);
171       typelen = TYPE_LENGTH (VALUE_TYPE (val));
172       fprintf_filtered (stream, "{");
173       /* Print arrays of characters using string syntax.  */
174       if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
175           && format == 0)
176         LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
177       else
178         {
179           value_print_array_elements (val, stream, format, pretty);
180         }
181       fprintf_filtered (stream, "}");
182       return (n * typelen);
183     }
184   else
185     {
186       struct type *type = VALUE_TYPE (val);
187
188       /* If it is a pointer, indicate what it points to.
189
190          Print type also if it is a reference.
191
192          C++: if it is a member pointer, we will take care
193          of that when we print it.  */
194       if (TYPE_CODE (type) == TYPE_CODE_PTR ||
195           TYPE_CODE (type) == TYPE_CODE_REF)
196         {
197           /* Hack:  remove (char *) for char strings.  Their
198              type is indicated by the quoted string anyway. */
199           if (TYPE_CODE (type) == TYPE_CODE_PTR &&
200               TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) &&
201               TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
202               !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
203             {
204                 /* Print nothing */
205             }
206           else
207             {
208               fprintf_filtered (stream, "(");
209               type_print (type, "", stream, -1);
210               fprintf_filtered (stream, ") ");
211             }
212         }
213       return (val_print (type, VALUE_CONTENTS (val),
214                          VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
215     }
216 }
217
218 /*  Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
219
220 void
221 val_print_type_code_int (type, valaddr, stream)
222      struct type *type;
223      char *valaddr;
224      FILE *stream;
225 {
226   char *p;
227   /* Pointer to first (i.e. lowest address) nonzero character.  */
228   char *first_addr;
229   unsigned int len;
230
231   if (TYPE_LENGTH (type) > sizeof (LONGEST))
232     {
233       if (TYPE_UNSIGNED (type))
234         {
235           /* First figure out whether the number in fact has zeros
236              in all its bytes more significant than least significant
237              sizeof (LONGEST) ones.  */
238           len = TYPE_LENGTH (type);
239           
240 #if TARGET_BYTE_ORDER == BIG_ENDIAN
241           for (p = valaddr;
242                len > sizeof (LONGEST) && p < valaddr + TYPE_LENGTH (type);
243                p++)
244 #else           /* Little endian.  */
245           first_addr = valaddr;
246           for (p = valaddr + TYPE_LENGTH (type);
247                len > sizeof (LONGEST) && p >= valaddr;
248                p--)
249 #endif          /* Little endian.  */
250             {
251               if (*p == 0)
252                 {
253                   len--;
254                 }
255               else
256                 {
257                   break;
258                 }
259             }
260 #if TARGET_BYTE_ORDER == BIG_ENDIAN
261           first_addr = p;
262 #endif
263           if (len <= sizeof (LONGEST))
264             {
265               /* We can print it in decimal.  */
266               print_longest (stream, 'u', 0,
267                             unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
268             }
269           else
270             {
271               /* It is big, so print it in hex.  */
272               print_hex_chars (stream, (unsigned char *) first_addr, len);
273             }
274         }
275       else
276         {
277           /* Signed.  One could assume two's complement (a reasonable
278              assumption, I think) and do better than this.  */
279           print_hex_chars (stream, (unsigned char *) valaddr,
280                            TYPE_LENGTH (type));
281         }
282     }
283   else
284     {
285 #ifdef PRINT_TYPELESS_INTEGER
286       PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
287 #else
288       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
289                      unpack_long (type, valaddr));
290 #endif
291     }
292 }                       
293
294 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
295    The raison d'etre of this function is to consolidate printing of LONG_LONG's
296    into this one function.  Some platforms have long longs but don't have a
297    printf() that supports "ll" in the format string.  We handle these by seeing
298    if the number is actually a long, and if not we just bail out and print the
299    number in hex.  The format chars b,h,w,g are from
300    print_scalar_formatted().  USE_LOCAL says whether or not to call the
301    local formatting routine to get the format.  */
302
303 void
304 print_longest (stream, format, use_local, val_long)
305      FILE *stream;
306      char format;
307      int use_local;
308      LONGEST val_long;
309 {
310 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
311   long vtop, vbot;
312
313   vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
314   vbot = (long) val_long;
315
316   if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
317       || ((format == 'u' || format == 'x') && val_long > UINT_MAX))
318     {
319       fprintf_filtered (stream, "0x%x%08x", vtop, vbot);
320       return;
321     }
322 #endif
323
324 #ifdef PRINTF_HAS_LONG_LONG
325   switch (format)
326     {
327     case 'd':
328       fprintf_filtered (stream,
329                         use_local ? local_decimal_format_custom ("ll")
330                                   : "%lld",
331                         val_long);
332       break;
333     case 'u':
334       fprintf_filtered (stream, "%llu", val_long);
335       break;
336     case 'x':
337       fprintf_filtered (stream,
338                         use_local ? local_hex_format_custom ("ll")
339                                   : "%llx",
340                         val_long);
341       break;
342     case 'o':
343       fprintf_filtered (stream,
344                         use_local ? local_octal_format_custom ("ll")
345                                   : "%llo",
346       break;
347     case 'b':
348       fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
349       break;
350     case 'h':
351       fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
352       break;
353     case 'w':
354       fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
355       break;
356     case 'g':
357       fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
358       break;
359     default:
360       abort ();
361     }
362 #else /* !PRINTF_HAS_LONG_LONG */
363   /* In the following it is important to coerce (val_long) to a long. It does
364      nothing if !LONG_LONG, but it will chop off the top half (which we know
365      we can ignore) if the host supports long longs.  */
366
367   switch (format)
368     {
369     case 'd':
370       fprintf_filtered (stream,
371                         use_local ? local_decimal_format_custom ("l")
372                                   : "%ld",
373                         (long) val_long);
374       break;
375     case 'u':
376       fprintf_filtered (stream, "%lu", (unsigned long) val_long);
377       break;
378     case 'x':
379       fprintf_filtered (stream,
380                         use_local ? local_hex_format_custom ("l")
381                                   : "%lx",
382                         (long) val_long);
383       break;
384     case 'o':
385       fprintf_filtered (stream,
386                         use_local ? local_octal_format_custom ("l")
387                                   : "%lo",
388                         (long) val_long);
389       break;
390     case 'b':
391       fprintf_filtered (stream, local_hex_format_custom ("02l"),
392                         (long) val_long);
393       break;
394     case 'h':
395       fprintf_filtered (stream, local_hex_format_custom ("04l"),
396                         (long) val_long);
397       break;
398     case 'w':
399       fprintf_filtered (stream, local_hex_format_custom ("08l"),
400                         (long) val_long);
401       break;
402     case 'g':
403       fprintf_filtered (stream, local_hex_format_custom ("016l"),
404                         (long) val_long);
405       break;
406     default:
407       abort ();
408     }
409 #endif /* !PRINTF_HAS_LONG_LONG */
410 }
411
412 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
413    on STREAM.  */
414
415 void
416 print_floating (valaddr, type, stream)
417      char *valaddr;
418      struct type *type;
419      FILE *stream;
420 {
421   double doub;
422   int inv;
423   unsigned len = TYPE_LENGTH (type);
424   
425 #if defined (IEEE_FLOAT)
426
427   /* Check for NaN's.  Note that this code does not depend on us being
428      on an IEEE conforming system.  It only depends on the target
429      machine using IEEE representation.  This means (a)
430      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
431      be defined for systems like the 68881, which uses IEEE
432      representation, but is not IEEE conforming.  */
433
434   {
435     long low, high;
436     /* Is the sign bit 0?  */
437     int nonnegative;
438     /* Is it is a NaN (i.e. the exponent is all ones and
439        the fraction is nonzero)?  */
440     int is_nan;
441
442     if (len == sizeof (float))
443       {
444         /* It's single precision. */
445         memcpy ((char *) &low, valaddr, sizeof (low));
446         /* target -> host.  */
447         SWAP_TARGET_AND_HOST (&low, sizeof (float));
448         nonnegative = low >= 0;
449         is_nan = ((((low >> 23) & 0xFF) == 0xFF) 
450                   && 0 != (low & 0x7FFFFF));
451         low &= 0x7fffff;
452         high = 0;
453       }
454     else
455       {
456         /* It's double precision.  Get the high and low words.  */
457
458 #if TARGET_BYTE_ORDER == BIG_ENDIAN
459         memcpy (&low, valaddr+4,  sizeof (low));
460         memcpy (&high, valaddr+0, sizeof (high));
461 #else
462         memcpy (&low, valaddr+0,  sizeof (low));
463         memcpy (&high, valaddr+4, sizeof (high));
464 #endif
465         SWAP_TARGET_AND_HOST (&low, sizeof (low));
466         SWAP_TARGET_AND_HOST (&high, sizeof (high));
467         nonnegative = high >= 0;
468         is_nan = (((high >> 20) & 0x7ff) == 0x7ff
469                   && ! ((((high & 0xfffff) == 0)) && (low == 0)));
470         high &= 0xfffff;
471       }
472
473     if (is_nan)
474       {
475         /* The meaning of the sign and fraction is not defined by IEEE.
476            But the user might know what they mean.  For example, they
477            (in an implementation-defined manner) distinguish between
478            signaling and quiet NaN's.  */
479         if (high)
480           fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
481                             high, low);
482         else
483           fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
484         return;
485       }
486   }
487 #endif /* IEEE_FLOAT.  */
488
489   doub = unpack_double (type, valaddr, &inv);
490   if (inv)
491     fprintf_filtered (stream, "<invalid float value>");
492   else
493     fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
494 }
495
496 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
497
498 static void
499 print_hex_chars (stream, valaddr, len)
500      FILE *stream;
501      unsigned char *valaddr;
502      unsigned len;
503 {
504   unsigned char *p;
505   
506   fprintf_filtered (stream, "0x");
507 #if TARGET_BYTE_ORDER == BIG_ENDIAN
508   for (p = valaddr;
509        p < valaddr + len;
510        p++)
511 #else /* Little endian.  */
512   for (p = valaddr + len - 1;
513        p >= valaddr;
514        p--)
515 #endif
516     {
517       fprintf_filtered (stream, "%02x", *p);
518     }
519 }
520
521 /*  Called by various <lang>_val_print routines to print elements of an
522     array in the form "<elem1>, <elem2>, <elem3>, ...".
523
524     (FIXME?)  Assumes array element separator is a comma, which is correct
525     for all languages currently handled.
526     (FIXME?)  Some languages have a notation for repeated array elements,
527     perhaps we should try to use that notation when appropriate.
528     */
529
530 void
531 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
532                           recurse, pretty, i)
533      struct type *type;
534      char *valaddr;
535      CORE_ADDR address;
536      FILE *stream;
537      int format;
538      int deref_ref;
539      int recurse;
540      enum val_prettyprint pretty;
541      unsigned int i;
542 {
543   unsigned int things_printed = 0;
544   unsigned len;
545   struct type *elttype;
546   unsigned eltlen;
547   /* Position of the array element we are examining to see
548      whether it is repeated.  */
549   unsigned int rep1;
550   /* Number of repetitions we have detected so far.  */
551   unsigned int reps;
552       
553   elttype = TYPE_TARGET_TYPE (type);
554   eltlen = TYPE_LENGTH (elttype);
555   len = TYPE_LENGTH (type) / eltlen;
556               
557   for (; i < len && things_printed < print_max; i++)
558     {
559       if (i != 0)
560         {
561           if (prettyprint_arrays)
562             {
563               fprintf_filtered (stream, ",\n");
564               print_spaces_filtered (2 + 2 * recurse, stream);
565             }
566           else
567             {
568               fprintf_filtered (stream, ", ");
569             }
570         }
571       wrap_here (n_spaces (2 + 2 * recurse));
572       
573       rep1 = i + 1;
574       reps = 1;
575       while ((rep1 < len) && 
576              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
577         {
578           ++reps;
579           ++rep1;
580         }
581       
582       if (reps > repeat_count_threshold)
583         {
584           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
585                      deref_ref, recurse + 1, pretty);
586           fprintf_filtered (stream, " <repeats %u times>", reps);
587           i = rep1 - 1;
588           things_printed += repeat_count_threshold;
589         }
590       else
591         {
592           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
593                      deref_ref, recurse + 1, pretty);
594           things_printed++;
595         }
596     }
597   if (i < len)
598     {
599       fprintf_filtered (stream, "...");
600     }
601 }
602
603 static void
604 value_print_array_elements (val, stream, format, pretty)
605      value val;
606      FILE *stream;
607      int format;
608      enum val_prettyprint pretty;
609 {
610   unsigned int things_printed = 0;
611   register unsigned int i, n, typelen;
612   /* Position of the array elem we are examining to see if it is repeated.  */
613   unsigned int rep1;
614   /* Number of repetitions we have detected so far.  */
615   unsigned int reps;
616     
617   n = VALUE_REPETITIONS (val);
618   typelen = TYPE_LENGTH (VALUE_TYPE (val));
619   for (i = 0; i < n && things_printed < print_max; i++)
620     {
621       if (i != 0)
622         {
623           fprintf_filtered (stream, ", ");
624         }
625       wrap_here ("");
626       
627       rep1 = i + 1;
628       reps = 1;
629       while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
630                                   VALUE_CONTENTS (val) + typelen * rep1,
631                                   typelen))
632         {
633           ++reps;
634           ++rep1;
635         }
636       
637       if (reps > repeat_count_threshold)
638         {
639           val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
640                      VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
641                      0, pretty);
642           fprintf (stream, " <repeats %u times>", reps);
643           i = rep1 - 1;
644           things_printed += repeat_count_threshold;
645         }
646       else
647         {
648           val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
649                      VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
650                      0, pretty);
651           things_printed++;
652         }
653     }
654   if (i < n)
655     {
656       fprintf_filtered (stream, "...");
657     }
658 }
659
660 /*  Print a string from the inferior, starting at ADDR and printing up to LEN
661     characters, to STREAM.  If LEN is zero, printing stops at the first null
662     byte, otherwise printing proceeds (including null bytes) until either
663     print_max or LEN characters have been printed.
664
665     Always fetch print_max+1 characters, even though LA_PRINT_STRING might want
666     to print more or fewer (with repeated characters).  This is so that we
667     don't spend forever fetching if we print a long string consisting of the
668     same character repeated.  Also so we can do it all in one memory operation,
669     which is faster.  However, this will be slower if print_max is set high,
670     e.g. if you set print_max to 1000, not only will it take a long time to
671     fetch short strings, but if you are near the end of the address space, it
672     might not work.
673
674     If the number of characters we actually print is limited because of hitting
675     print_max, when LEN would have explicitly or implicitly (in the case of a
676     null terminated string with another non-null character available to print)
677     allowed us to print more, we print ellipsis ("...") after the printed string
678     to indicate that more characters were available to print but that we were
679     limited by print_max.  To do this correctly requires that we always fetch
680     one more than the number of characters we could potentially print, so that
681     if we do print the maximum number, we can tell whether or not a null byte
682     would have been the next character, in the case of C style strings.
683     For non-C style strings, only the value of LEN is pertinent in deciding
684     whether or not to print ellipsis.
685
686     FIXME:  If LEN is nonzero and less than print_max, we could get away
687     with only fetching the specified number of characters from the inferior. */
688
689 int
690 val_print_string (addr, len, stream)
691     CORE_ADDR addr;
692     unsigned int len;
693     FILE *stream;
694 {
695   int first_addr_err = 0;       /* Nonzero if first address out of bounds */
696   int force_ellipsis = 0;       /* Force ellipsis to be printed if nonzero */
697   int errcode;
698   unsigned char c;
699   char *string;
700
701   /* Get first character.  */
702   errcode = target_read_memory (addr, (char *)&c, 1);
703   if (errcode != 0)
704     {
705       /* First address out of bounds.  */
706       first_addr_err = 1;
707     }
708   else if (print_max < UINT_MAX)
709     {
710       string = (char *) alloca (print_max + 1);
711       memset (string, 0, print_max + 1);
712       
713       QUIT;
714       errcode = target_read_memory (addr, string, print_max + 1);
715       if (errcode != 0)
716         {
717           /* Try reading just one character.  If that succeeds, assume we hit
718              the end of the address space, but the initial part of the string
719              is probably safe. */
720           char x[1];
721           errcode = target_read_memory (addr, x, 1);
722         }
723       if (len == 0)
724         {
725           /* When the length is unspecified, such as when printing C style
726              null byte terminated strings, then scan the string looking for
727              the terminator in the first print_max characters.  If a terminator
728              is found, then it determines the length, otherwise print_max
729              determines the length. */
730           for (;len < print_max; len++)
731             {
732               if (string[len] == '\0')
733                 {
734                   break;
735                 }
736             }
737           /* If the first unprinted character is not the null terminator, set
738              the flag to force ellipses.  This is true whether or not we broke
739              out of the above loop because we found a terminator, or whether
740              we simply hit the limit on how many characters to print. */
741           if (string[len] != '\0')
742             {
743               force_ellipsis = 1;
744             }
745         }
746       else if (len > print_max)
747         {
748           /* Printing less than the number of characters actually requested
749              always makes us print ellipsis. */
750           len = print_max;
751           force_ellipsis = 1;
752         }
753       QUIT;
754       
755       if (addressprint)
756         {
757           fputs_filtered (" ", stream);
758         }
759       LA_PRINT_STRING (stream, string, len, force_ellipsis);
760     }
761   
762   if (errcode != 0)
763     {
764       if (errcode == EIO)
765         {
766           fprintf_filtered (stream,
767                             (" <Address 0x%x out of bounds>" + first_addr_err),
768                             addr + len);
769         }
770       else
771         {
772           error ("Error reading memory address 0x%x: %s.", addr + len,
773                  safe_strerror (errcode));
774         }
775     }
776   fflush (stream);
777   return (len);
778 }
779 \f
780 #if 0
781 /* Validate an input or output radix setting, and make sure the user
782    knows what they really did here.  Radix setting is confusing, e.g.
783    setting the input radix to "10" never changes it!  */
784
785 /* ARGSUSED */
786 static void
787 set_input_radix (args, from_tty, c)
788      char *args;
789      int from_tty;
790      struct cmd_list_element *c;
791 {
792   unsigned radix = *(unsigned *)c->var;
793
794   if (from_tty)
795     printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
796         radix, radix, radix);
797 }
798 #endif
799
800 /* ARGSUSED */
801 static void
802 set_output_radix (args, from_tty, c)
803      char *args;
804      int from_tty;
805      struct cmd_list_element *c;
806 {
807   unsigned radix = *(unsigned *)c->var;
808
809   if (from_tty)
810     printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
811         radix, radix, radix);
812
813   /* FIXME, we really should be able to validate the setting BEFORE
814      it takes effect.  */
815   switch (radix)
816     {
817     case 16:
818       output_format = 'x';
819       break;
820     case 10:
821       output_format = 0;
822       break;
823     case 8:
824       output_format = 'o';              /* octal */
825       break;
826     default:
827       output_format = 0;
828       error ("Unsupported radix ``decimal %d''; using decimal output",
829               radix);
830     }
831 }
832
833 /* Both at once */
834 static void
835 set_radix (arg, from_tty, c)
836      char *arg;
837      int from_tty;
838      struct cmd_list_element *c;
839 {
840   unsigned radix = *(unsigned *)c->var;
841
842   if (from_tty)
843     printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
844         radix, radix, radix);
845
846   input_radix = radix;
847   output_radix = radix;
848
849   set_output_radix (arg, 0, c);
850 }
851 \f
852 /*ARGSUSED*/
853 static void
854 set_print (arg, from_tty)
855      char *arg;
856      int from_tty;
857 {
858   printf (
859 "\"set print\" must be followed by the name of a print subcommand.\n");
860   help_list (setprintlist, "set print ", -1, stdout);
861 }
862
863 /*ARGSUSED*/
864 static void
865 show_print (args, from_tty)
866      char *args;
867      int from_tty;
868 {
869   cmd_show_list (showprintlist, from_tty, "");
870 }
871 \f
872 void
873 _initialize_valprint ()
874 {
875   struct cmd_list_element *c;
876
877   add_prefix_cmd ("print", no_class, set_print,
878                   "Generic command for setting how things print.",
879                   &setprintlist, "set print ", 0, &setlist);
880   add_alias_cmd ("p", "print", no_class, 1, &setlist); 
881   add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
882                                                                                                                    to     set prompt */
883   add_prefix_cmd ("print", no_class, show_print,
884                   "Generic command for showing print settings.",
885                   &showprintlist, "show print ", 0, &showlist);
886   add_alias_cmd ("p", "print", no_class, 1, &showlist); 
887   add_alias_cmd ("pr", "print", no_class, 1, &showlist); 
888
889   add_show_from_set
890     (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
891                   "Set limit on string chars or array elements to print.\n\
892 \"set print elements 0\" causes there to be no limit.",
893                   &setprintlist),
894      &showprintlist);
895
896   add_show_from_set
897     (add_set_cmd ("repeats", no_class, var_uinteger,
898                   (char *)&repeat_count_threshold,
899                   "Set threshold for repeated print elements.\n\
900 \"set print repeats 0\" causes all elements to be individually printed.",
901                   &setprintlist),
902      &showprintlist);
903
904   add_show_from_set
905     (add_set_cmd ("pretty", class_support, var_boolean,
906                   (char *)&prettyprint_structs,
907                   "Set prettyprinting of structures.",
908                   &setprintlist),
909      &showprintlist);
910
911   add_show_from_set
912     (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
913                   "Set printing of unions interior to structures.",
914                   &setprintlist),
915      &showprintlist);
916   
917   add_show_from_set
918     (add_set_cmd ("array", class_support, var_boolean,
919                   (char *)&prettyprint_arrays,
920                   "Set prettyprinting of arrays.",
921                   &setprintlist),
922      &showprintlist);
923
924   add_show_from_set
925     (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
926                   "Set printing of addresses.",
927                   &setprintlist),
928      &showprintlist);
929
930 #if 0
931   /* The "show radix" cmd isn't good enough to show two separate values.
932      The rest of the code works, but the show part is confusing, so don't
933      let them be set separately 'til we work out "show".  */
934   c = add_set_cmd ("input-radix", class_support, var_uinteger,
935                    (char *)&input_radix,
936                   "Set default input radix for entering numbers.",
937                   &setlist);
938   add_show_from_set (c, &showlist);
939   c->function = set_input_radix;
940
941   c = add_set_cmd ("output-radix", class_support, var_uinteger,
942                    (char *)&output_radix,
943                   "Set default output radix for printing of values.",
944                   &setlist);
945   add_show_from_set (c, &showlist);
946   c->function = set_output_radix;
947 #endif 
948
949   c = add_set_cmd ("radix", class_support, var_uinteger,
950                    (char *)&output_radix,
951                   "Set default input and output number radix.",
952                   &setlist);
953   add_show_from_set (c, &showlist);
954   c->function.sfunc = set_radix;
955
956   /* Give people the defaults which they are used to.  */
957   prettyprint_structs = 0;
958   prettyprint_arrays = 0;
959   unionprint = 1;
960   addressprint = 1;
961   print_max = 200;
962 }