xref: /openbsd-src/gnu/usr.bin/binutils/gdb/valprint.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* Print values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
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, Boston, MA 02111-1307, USA.  */
20 
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "gdbcmd.h"
28 #include "target.h"
29 #include "obstack.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "annotate.h"
33 #include "valprint.h"
34 
35 #include <errno.h>
36 
37 /* Prototypes for local functions */
38 
39 static void
40 print_hex_chars PARAMS ((GDB_FILE *, unsigned char *, unsigned int));
41 
42 static void
43 show_print PARAMS ((char *, int));
44 
45 static void
46 set_print PARAMS ((char *, int));
47 
48 static void
49 set_radix PARAMS ((char *, int));
50 
51 static void
52 show_radix PARAMS ((char *, int));
53 
54 static void
55 set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
56 
57 static void
58 set_input_radix_1 PARAMS ((int, unsigned));
59 
60 static void
61 set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
62 
63 static void
64 set_output_radix_1 PARAMS ((int, unsigned));
65 
66 /* Maximum number of chars to print for a string pointer value or vector
67    contents, or UINT_MAX for no limit.  Note that "set print elements 0"
68    stores UINT_MAX in print_max, which displays in a show command as
69    "unlimited". */
70 
71 unsigned int print_max;
72 #define PRINT_MAX_DEFAULT 200	/* Start print_max off at this value. */
73 
74 /* Default input and output radixes, and output format letter.  */
75 
76 unsigned input_radix = 10;
77 unsigned output_radix = 10;
78 int output_format = 0;
79 
80 /* Print repeat counts if there are more than this many repetitions of an
81    element in an array.  Referenced by the low level language dependent
82    print routines. */
83 
84 unsigned int repeat_count_threshold = 10;
85 
86 /* If nonzero, stops printing of char arrays at first null. */
87 
88 int stop_print_at_null;
89 
90 /* Controls pretty printing of structures. */
91 
92 int prettyprint_structs;
93 
94 /* Controls pretty printing of arrays.  */
95 
96 int prettyprint_arrays;
97 
98 /* If nonzero, causes unions inside structures or other unions to be
99    printed. */
100 
101 int unionprint;			/* Controls printing of nested unions.  */
102 
103 /* If nonzero, causes machine addresses to be printed in certain contexts. */
104 
105 int addressprint;		/* Controls printing of machine addresses */
106 
107 
108 /* Print data of type TYPE located at VALADDR (within GDB), which came from
109    the inferior at address ADDRESS, onto stdio stream STREAM according to
110    FORMAT (a letter, or 0 for natural format using TYPE).
111 
112    If DEREF_REF is nonzero, then dereference references, otherwise just print
113    them like pointers.
114 
115    The PRETTY parameter controls prettyprinting.
116 
117    If the data are a string pointer, returns the number of string characters
118    printed.
119 
120    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
121    enhanced to be able to debug more than the single target it was compiled
122    for (specific CPU type and thus specific target byte ordering), then
123    either the print routines are going to have to take this into account,
124    or the data is going to have to be passed into here already converted
125    to the host byte ordering, whichever is more convenient. */
126 
127 
128 int
129 val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty)
130      struct type *type;
131      char *valaddr;
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, address, stream, format, deref_ref,
159 			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 void
243 print_longest (stream, format, use_local, val_long)
244      GDB_FILE *stream;
245      int format;
246      int use_local;
247      LONGEST val_long;
248 {
249 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
250   if (sizeof (long) < sizeof (LONGEST))
251     {
252       int punt = 0;
253       switch (format)
254 	{
255 	case 'd':
256 	case 'b':
257 	case 'h':
258 	case 'w':
259 	case 'g':
260 	  /* Print as signed value, must fit completely in signed long */
261 	  {
262 	    long temp = val_long;
263 	    if (temp != val_long)
264 	      punt++;
265 	  }
266 	  break;
267 	case 'u':
268 	case 'x':
269 	case 'o':
270 	  /* Print as unsigned value, must fit completely in unsigned long */
271 	  {
272 	    unsigned long temp = val_long;
273 	    if (temp != val_long)
274 	      punt++;
275 	  }
276 	  break;
277 	}
278       if (punt)
279 	{
280 	  /* Urk, can't represent value in long so print in hex.
281 	     Do shift in two operations so that if sizeof (long) == sizeof (LONGEST)
282 	     we can avoid warnings from picky compilers about shifts >= the size of
283 	     the shiftee in bits */
284 	  unsigned long vbot = (unsigned long) val_long;
285 	  unsigned long vtop = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
286 	  vtop >>= 1;
287 	  fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
288 	  return;
289 	}
290       }
291 #endif
292 
293 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
294   switch (format)
295     {
296     case 'd':
297       fprintf_filtered (stream,
298 			use_local ? local_decimal_format_custom ("ll")
299 				  : "%lld",
300 			val_long);
301       break;
302     case 'u':
303       fprintf_filtered (stream, "%llu", val_long);
304       break;
305     case 'x':
306       fprintf_filtered (stream,
307 			use_local ? local_hex_format_custom ("ll")
308 				  : "%llx",
309 			val_long);
310       break;
311     case 'o':
312       fprintf_filtered (stream,
313 			use_local ? local_octal_format_custom ("ll")
314 				  : "%llo",
315 			val_long);
316       break;
317     case 'b':
318       fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
319       break;
320     case 'h':
321       fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
322       break;
323     case 'w':
324       fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
325       break;
326     case 'g':
327       fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
328       break;
329     default:
330       abort ();
331     }
332 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG*/
333   /* In the following it is important to coerce (val_long) to a long. It does
334      nothing if !LONG_LONG, but it will chop off the top half (which we know
335      we can ignore) if the host supports long longs.  */
336 
337   switch (format)
338     {
339     case 'd':
340       fprintf_filtered (stream,
341 			use_local ? local_decimal_format_custom ("l")
342 				  : "%ld",
343 			(long) val_long);
344       break;
345     case 'u':
346       fprintf_filtered (stream, "%lu", (unsigned long) val_long);
347       break;
348     case 'x':
349       fprintf_filtered (stream,
350 			use_local ? local_hex_format_custom ("l")
351 				  : "%lx",
352 			(long) val_long);
353       break;
354     case 'o':
355       fprintf_filtered (stream,
356 			use_local ? local_octal_format_custom ("l")
357 				  : "%lo",
358 			(long) val_long);
359       break;
360     case 'b':
361       fprintf_filtered (stream, local_hex_format_custom ("02l"),
362 			(long) val_long);
363       break;
364     case 'h':
365       fprintf_filtered (stream, local_hex_format_custom ("04l"),
366 			(long) val_long);
367       break;
368     case 'w':
369       fprintf_filtered (stream, local_hex_format_custom ("08l"),
370 			(long) val_long);
371       break;
372     case 'g':
373       fprintf_filtered (stream, local_hex_format_custom ("016l"),
374 			(long) val_long);
375       break;
376     default:
377       abort ();
378     }
379 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
380 }
381 
382 /* This used to be a macro, but I don't think it is called often enough
383    to merit such treatment.  */
384 /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
385    arguments to a function, number in a value history, register number, etc.)
386    where the value must not be larger than can fit in an int.  */
387 
388 int
389 longest_to_int (arg)
390      LONGEST arg;
391 {
392   /* Let the compiler do the work */
393   int rtnval = (int) arg;
394 
395   /* Check for overflows or underflows */
396   if (sizeof (LONGEST) > sizeof (int))
397     {
398       if (rtnval != arg)
399 	{
400 	  error ("Value out of range.");
401 	}
402     }
403   return (rtnval);
404 }
405 
406 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
407    on STREAM.  */
408 
409 void
410 print_floating (valaddr, type, stream)
411      char *valaddr;
412      struct type *type;
413      GDB_FILE *stream;
414 {
415   DOUBLEST doub;
416   int inv;
417   unsigned len = TYPE_LENGTH (type);
418 
419 #if defined (IEEE_FLOAT)
420 
421   /* Check for NaN's.  Note that this code does not depend on us being
422      on an IEEE conforming system.  It only depends on the target
423      machine using IEEE representation.  This means (a)
424      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
425      be defined for systems like the 68881, which uses IEEE
426      representation, but is not IEEE conforming.  */
427 
428   {
429     unsigned long low, high;
430     /* Is the sign bit 0?  */
431     int nonnegative;
432     /* Is it is a NaN (i.e. the exponent is all ones and
433        the fraction is nonzero)?  */
434     int is_nan;
435 
436     if (len == 4)
437       {
438 	/* It's single precision.  */
439 	/* Assume that floating point byte order is the same as
440 	   integer byte order.  */
441 	low = extract_unsigned_integer (valaddr, 4);
442 	nonnegative = ((low & 0x80000000) == 0);
443 	is_nan = ((((low >> 23) & 0xFF) == 0xFF)
444 		  && 0 != (low & 0x7FFFFF));
445 	low &= 0x7fffff;
446 	high = 0;
447       }
448     else if (len == 8)
449       {
450 	/* It's double precision.  Get the high and low words.  */
451 
452 	/* Assume that floating point byte order is the same as
453 	   integer byte order.  */
454 	if (TARGET_BYTE_ORDER == BIG_ENDIAN)
455 	  {
456 	    low = extract_unsigned_integer (valaddr + 4, 4);
457 	    high = extract_unsigned_integer (valaddr, 4);
458 	  }
459 	else
460 	  {
461 	    low = extract_unsigned_integer (valaddr, 4);
462 	    high = extract_unsigned_integer (valaddr + 4, 4);
463 	  }
464 	nonnegative = ((high & 0x80000000) == 0);
465 	is_nan = (((high >> 20) & 0x7ff) == 0x7ff
466 		  && ! ((((high & 0xfffff) == 0)) && (low == 0)));
467 	high &= 0xfffff;
468       }
469     else
470       /* Extended.  We can't detect NaNs for extendeds yet.  Also note
471 	 that currently extendeds get nuked to double in
472 	 REGISTER_CONVERTIBLE.  */
473       is_nan = 0;
474 
475     if (is_nan)
476       {
477 	/* The meaning of the sign and fraction is not defined by IEEE.
478 	   But the user might know what they mean.  For example, they
479 	   (in an implementation-defined manner) distinguish between
480 	   signaling and quiet NaN's.  */
481 	if (high)
482 	  fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
483 			    high, low);
484 	else
485 	  fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
486 	return;
487       }
488   }
489 #endif /* IEEE_FLOAT.  */
490 
491   doub = unpack_double (type, valaddr, &inv);
492   if (inv)
493     {
494       fprintf_filtered (stream, "<invalid float value>");
495       return;
496     }
497 
498   if (len < sizeof (double))
499     fprintf_filtered (stream, "%.9g", (double) doub);
500   else if (len == sizeof (double))
501     fprintf_filtered (stream, "%.17g", (double) doub);
502   else
503 #ifdef PRINTF_HAS_LONG_DOUBLE
504     fprintf_filtered (stream, "%.35Lg", doub);
505 #else
506     /* This at least wins with values that are representable as doubles */
507     fprintf_filtered (stream, "%.17g", (double) doub);
508 #endif
509 }
510 
511 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
512 
513 static void
514 print_hex_chars (stream, valaddr, len)
515      GDB_FILE *stream;
516      unsigned char *valaddr;
517      unsigned len;
518 {
519   unsigned char *p;
520 
521   /* FIXME: We should be not printing leading zeroes in most cases.  */
522 
523   fprintf_filtered (stream, local_hex_format_prefix ());
524   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
525     {
526       for (p = valaddr;
527 	   p < valaddr + len;
528 	   p++)
529 	{
530 	  fprintf_filtered (stream, "%02x", *p);
531 	}
532     }
533   else
534     {
535       for (p = valaddr + len - 1;
536 	   p >= valaddr;
537 	   p--)
538 	{
539 	  fprintf_filtered (stream, "%02x", *p);
540 	}
541     }
542   fprintf_filtered (stream, local_hex_format_suffix ());
543 }
544 
545 /*  Called by various <lang>_val_print routines to print elements of an
546     array in the form "<elem1>, <elem2>, <elem3>, ...".
547 
548     (FIXME?)  Assumes array element separator is a comma, which is correct
549     for all languages currently handled.
550     (FIXME?)  Some languages have a notation for repeated array elements,
551     perhaps we should try to use that notation when appropriate.
552     */
553 
554 void
555 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
556 			  recurse, pretty, i)
557      struct type *type;
558      char *valaddr;
559      CORE_ADDR address;
560      GDB_FILE *stream;
561      int format;
562      int deref_ref;
563      int recurse;
564      enum val_prettyprint pretty;
565      unsigned int i;
566 {
567   unsigned int things_printed = 0;
568   unsigned len;
569   struct type *elttype;
570   unsigned eltlen;
571   /* Position of the array element we are examining to see
572      whether it is repeated.  */
573   unsigned int rep1;
574   /* Number of repetitions we have detected so far.  */
575   unsigned int reps;
576 
577   elttype = TYPE_TARGET_TYPE (type);
578   eltlen = TYPE_LENGTH (check_typedef (elttype));
579   len = TYPE_LENGTH (type) / eltlen;
580 
581   annotate_array_section_begin (i, elttype);
582 
583   for (; i < len && things_printed < print_max; i++)
584     {
585       if (i != 0)
586 	{
587 	  if (prettyprint_arrays)
588 	    {
589 	      fprintf_filtered (stream, ",\n");
590 	      print_spaces_filtered (2 + 2 * recurse, stream);
591 	    }
592 	  else
593 	    {
594 	      fprintf_filtered (stream, ", ");
595 	    }
596 	}
597       wrap_here (n_spaces (2 + 2 * recurse));
598 
599       rep1 = i + 1;
600       reps = 1;
601       while ((rep1 < len) &&
602 	     !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
603 	{
604 	  ++reps;
605 	  ++rep1;
606 	}
607 
608       if (reps > repeat_count_threshold)
609 	{
610 	  val_print (elttype, valaddr + i * eltlen, 0, stream, format,
611 		     deref_ref, recurse + 1, pretty);
612 	  annotate_elt_rep (reps);
613 	  fprintf_filtered (stream, " <repeats %u times>", reps);
614 	  annotate_elt_rep_end ();
615 
616 	  i = rep1 - 1;
617 	  things_printed += repeat_count_threshold;
618 	}
619       else
620 	{
621 	  val_print (elttype, valaddr + i * eltlen, 0, stream, format,
622 		     deref_ref, recurse + 1, pretty);
623 	  annotate_elt ();
624 	  things_printed++;
625 	}
626     }
627   annotate_array_section_end ();
628   if (i < len)
629     {
630       fprintf_filtered (stream, "...");
631     }
632 }
633 
634 /*  Print a string from the inferior, starting at ADDR and printing up to LEN
635     characters, to STREAM.  If LEN is zero, printing stops at the first null
636     byte, otherwise printing proceeds (including null bytes) until either
637     print_max or LEN characters have been printed, whichever is smaller. */
638 
639 /* FIXME: All callers supply LEN of zero.  Supplying a non-zero LEN is
640    pointless, this routine just then becomes a convoluted version of
641    target_read_memory_partial.  Removing all the LEN stuff would simplify
642    this routine enormously.
643 
644    FIXME: Use target_read_string.  */
645 
646 int
647 val_print_string (addr, len, stream)
648     CORE_ADDR addr;
649     unsigned int len;
650     GDB_FILE *stream;
651 {
652   int force_ellipsis = 0;	/* Force ellipsis to be printed if nonzero. */
653   int errcode;			/* Errno returned from bad reads. */
654   unsigned int fetchlimit;	/* Maximum number of bytes to fetch. */
655   unsigned int nfetch;		/* Bytes to fetch / bytes fetched. */
656   unsigned int chunksize;	/* Size of each fetch, in bytes. */
657   unsigned int bufsize;		/* Size of current fetch buffer. */
658   char *buffer = NULL;		/* Dynamically growable fetch buffer. */
659   char *bufptr;			/* Pointer to next available byte in buffer. */
660   char *limit;			/* First location past end of fetch buffer. */
661   struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
662   char peekchar;		/* Place into which we can read one char. */
663 
664   /* First we need to figure out the limit on the number of characters we are
665      going to attempt to fetch and print.  This is actually pretty simple.  If
666      LEN is nonzero, then the limit is the minimum of LEN and print_max.  If
667      LEN is zero, then the limit is print_max.  This is true regardless of
668      whether print_max is zero, UINT_MAX (unlimited), or something in between,
669      because finding the null byte (or available memory) is what actually
670      limits the fetch. */
671 
672   fetchlimit = (len == 0 ? print_max : min (len, print_max));
673 
674   /* Now decide how large of chunks to try to read in one operation.  This
675      is also pretty simple.  If LEN is nonzero, then we want fetchlimit bytes,
676      so we might as well read them all in one operation.  If LEN is zero, we
677      are looking for a null terminator to end the fetching, so we might as
678      well read in blocks that are large enough to be efficient, but not so
679      large as to be slow if fetchlimit happens to be large.  So we choose the
680      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
681      200 is way too big for remote debugging over a serial line.  */
682 
683   chunksize = (len == 0 ? min (8, fetchlimit) : fetchlimit);
684 
685   /* Loop until we either have all the characters to print, or we encounter
686      some error, such as bumping into the end of the address space. */
687 
688   bufsize = 0;
689   do {
690     QUIT;
691     /* Figure out how much to fetch this time, and grow the buffer to fit. */
692     nfetch = min (chunksize, fetchlimit - bufsize);
693     bufsize += nfetch;
694     if (buffer == NULL)
695       {
696 	buffer = (char *) xmalloc (bufsize);
697 	bufptr = buffer;
698       }
699     else
700       {
701 	discard_cleanups (old_chain);
702 	buffer = (char *) xrealloc (buffer, bufsize);
703 	bufptr = buffer + bufsize - nfetch;
704       }
705     old_chain = make_cleanup (free, buffer);
706 
707     /* Read as much as we can. */
708     nfetch = target_read_memory_partial (addr, bufptr, nfetch, &errcode);
709     if (len != 0)
710       {
711 	addr += nfetch;
712 	bufptr += nfetch;
713       }
714     else
715       {
716 	/* Scan this chunk for the null byte that terminates the string
717 	   to print.  If found, we don't need to fetch any more.  Note
718 	   that bufptr is explicitly left pointing at the next character
719 	   after the null byte, or at the next character after the end of
720 	   the buffer. */
721 	limit = bufptr + nfetch;
722 	while (bufptr < limit)
723 	  {
724 	    ++addr;
725 	    ++bufptr;
726 	    if (bufptr[-1] == '\0')
727 	      {
728 		/* We don't care about any error which happened after
729 		   the NULL terminator.  */
730 		errcode = 0;
731 		break;
732 	      }
733 	  }
734       }
735   } while (errcode == 0					/* no error */
736 	   && bufsize < fetchlimit			/* no overrun */
737 	   && !(len == 0 && *(bufptr - 1) == '\0'));	/* no null term */
738 
739   /* bufptr and addr now point immediately beyond the last byte which we
740      consider part of the string (including a '\0' which ends the string).  */
741 
742   /* We now have either successfully filled the buffer to fetchlimit, or
743      terminated early due to an error or finding a null byte when LEN is
744      zero.  */
745 
746   if (len == 0 && bufptr > buffer && *(bufptr - 1) != '\0')
747     {
748       /* We didn't find a null terminator we were looking for.  Attempt
749 	 to peek at the next character.  If not successful, or it is not
750 	 a null byte, then force ellipsis to be printed.  */
751       if (target_read_memory (addr, &peekchar, 1) != 0 || peekchar != '\0')
752 	{
753 	  force_ellipsis = 1;
754 	}
755     }
756   else if ((len != 0 && errcode != 0) || (len > bufptr - buffer))
757     {
758       /* Getting an error when we have a requested length, or fetching less
759 	 than the number of characters actually requested, always make us
760 	 print ellipsis. */
761       force_ellipsis = 1;
762     }
763 
764   QUIT;
765 
766   /* If we get an error before fetching anything, don't print a string.
767      But if we fetch something and then get an error, print the string
768      and then the error message.  */
769   if (errcode == 0 || bufptr > buffer)
770     {
771       if (addressprint)
772 	{
773 	  fputs_filtered (" ", stream);
774 	}
775       LA_PRINT_STRING (stream, buffer, bufptr - buffer, force_ellipsis);
776     }
777 
778   if (errcode != 0)
779     {
780       if (errcode == EIO)
781 	{
782 	  fprintf_filtered (stream, " <Address ");
783 	  print_address_numeric (addr, 1, stream);
784 	  fprintf_filtered (stream, " out of bounds>");
785 	}
786       else
787 	{
788 	  fprintf_filtered (stream, " <Error reading address ");
789 	  print_address_numeric (addr, 1, stream);
790 	  fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
791 	}
792     }
793   gdb_flush (stream);
794   do_cleanups (old_chain);
795   return (bufptr - buffer);
796 }
797 
798 
799 /* Validate an input or output radix setting, and make sure the user
800    knows what they really did here.  Radix setting is confusing, e.g.
801    setting the input radix to "10" never changes it!  */
802 
803 /* ARGSUSED */
804 static void
805 set_input_radix (args, from_tty, c)
806      char *args;
807      int from_tty;
808      struct cmd_list_element *c;
809 {
810   set_input_radix_1 (from_tty, *(unsigned *)c->var);
811 }
812 
813 /* ARGSUSED */
814 static void
815 set_input_radix_1 (from_tty, radix)
816      int from_tty;
817      unsigned radix;
818 {
819   /* We don't currently disallow any input radix except 0 or 1, which don't
820      make any mathematical sense.  In theory, we can deal with any input
821      radix greater than 1, even if we don't have unique digits for every
822      value from 0 to radix-1, but in practice we lose on large radix values.
823      We should either fix the lossage or restrict the radix range more.
824      (FIXME). */
825 
826   if (radix < 2)
827     {
828       error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
829 	     radix);
830     }
831   input_radix = radix;
832   if (from_tty)
833     {
834       printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
835 		       radix, radix, radix);
836     }
837 }
838 
839 /* ARGSUSED */
840 static void
841 set_output_radix (args, from_tty, c)
842      char *args;
843      int from_tty;
844      struct cmd_list_element *c;
845 {
846   set_output_radix_1 (from_tty, *(unsigned *)c->var);
847 }
848 
849 static void
850 set_output_radix_1 (from_tty, radix)
851      int from_tty;
852      unsigned radix;
853 {
854   /* Validate the radix and disallow ones that we aren't prepared to
855      handle correctly, leaving the radix unchanged. */
856   switch (radix)
857     {
858     case 16:
859       output_format = 'x';		/* hex */
860       break;
861     case 10:
862       output_format = 0;		/* decimal */
863       break;
864     case 8:
865       output_format = 'o';		/* octal */
866       break;
867     default:
868       error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
869 	     radix);
870     }
871   output_radix = radix;
872   if (from_tty)
873     {
874       printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
875 		       radix, radix, radix);
876     }
877 }
878 
879 /* Set both the input and output radix at once.  Try to set the output radix
880    first, since it has the most restrictive range.  An radix that is valid as
881    an output radix is also valid as an input radix.
882 
883    It may be useful to have an unusual input radix.  If the user wishes to
884    set an input radix that is not valid as an output radix, he needs to use
885    the 'set input-radix' command. */
886 
887 static void
888 set_radix (arg, from_tty)
889      char *arg;
890      int from_tty;
891 {
892   unsigned radix;
893 
894   radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
895   set_output_radix_1 (0, radix);
896   set_input_radix_1 (0, radix);
897   if (from_tty)
898     {
899       printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
900 		       radix, radix, radix);
901     }
902 }
903 
904 /* Show both the input and output radices. */
905 
906 /*ARGSUSED*/
907 static void
908 show_radix (arg, from_tty)
909      char *arg;
910      int from_tty;
911 {
912   if (from_tty)
913     {
914       if (input_radix == output_radix)
915 	{
916 	  printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
917 			   input_radix, input_radix, input_radix);
918 	}
919       else
920 	{
921 	  printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
922 			   input_radix, input_radix, input_radix);
923 	  printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
924 			   output_radix, output_radix, output_radix);
925 	}
926     }
927 }
928 
929 
930 /*ARGSUSED*/
931 static void
932 set_print (arg, from_tty)
933      char *arg;
934      int from_tty;
935 {
936   printf_unfiltered (
937 "\"set print\" must be followed by the name of a print subcommand.\n");
938   help_list (setprintlist, "set print ", -1, gdb_stdout);
939 }
940 
941 /*ARGSUSED*/
942 static void
943 show_print (args, from_tty)
944      char *args;
945      int from_tty;
946 {
947   cmd_show_list (showprintlist, from_tty, "");
948 }
949 
950 void
951 _initialize_valprint ()
952 {
953   struct cmd_list_element *c;
954 
955   add_prefix_cmd ("print", no_class, set_print,
956 		  "Generic command for setting how things print.",
957 		  &setprintlist, "set print ", 0, &setlist);
958   add_alias_cmd ("p", "print", no_class, 1, &setlist);
959   /* prefer set print to set prompt */
960   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
961 
962   add_prefix_cmd ("print", no_class, show_print,
963 		  "Generic command for showing print settings.",
964 		  &showprintlist, "show print ", 0, &showlist);
965   add_alias_cmd ("p", "print", no_class, 1, &showlist);
966   add_alias_cmd ("pr", "print", no_class, 1, &showlist);
967 
968   add_show_from_set
969     (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
970 		  "Set limit on string chars or array elements to print.\n\
971 \"set print elements 0\" causes there to be no limit.",
972 		  &setprintlist),
973      &showprintlist);
974 
975   add_show_from_set
976     (add_set_cmd ("null-stop", no_class, var_boolean,
977 		  (char *)&stop_print_at_null,
978 		  "Set printing of char arrays to stop at first null char.",
979 		  &setprintlist),
980      &showprintlist);
981 
982   add_show_from_set
983     (add_set_cmd ("repeats", no_class, var_uinteger,
984 		  (char *)&repeat_count_threshold,
985 		  "Set threshold for repeated print elements.\n\
986 \"set print repeats 0\" causes all elements to be individually printed.",
987 		  &setprintlist),
988      &showprintlist);
989 
990   add_show_from_set
991     (add_set_cmd ("pretty", class_support, var_boolean,
992 		  (char *)&prettyprint_structs,
993 		  "Set prettyprinting of structures.",
994 		  &setprintlist),
995      &showprintlist);
996 
997   add_show_from_set
998     (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
999 		  "Set printing of unions interior to structures.",
1000 		  &setprintlist),
1001      &showprintlist);
1002 
1003   add_show_from_set
1004     (add_set_cmd ("array", class_support, var_boolean,
1005 		  (char *)&prettyprint_arrays,
1006 		  "Set prettyprinting of arrays.",
1007 		  &setprintlist),
1008      &showprintlist);
1009 
1010   add_show_from_set
1011     (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
1012 		  "Set printing of addresses.",
1013 		  &setprintlist),
1014      &showprintlist);
1015 
1016   c = add_set_cmd ("input-radix", class_support, var_uinteger,
1017 		   (char *)&input_radix,
1018 		  "Set default input radix for entering numbers.",
1019 		  &setlist);
1020   add_show_from_set (c, &showlist);
1021   c->function.sfunc = set_input_radix;
1022 
1023   c = add_set_cmd ("output-radix", class_support, var_uinteger,
1024 		   (char *)&output_radix,
1025 		  "Set default output radix for printing of values.",
1026 		  &setlist);
1027   add_show_from_set (c, &showlist);
1028   c->function.sfunc = set_output_radix;
1029 
1030   /* The "set radix" and "show radix" commands are special in that they are
1031      like normal set and show commands but allow two normally independent
1032      variables to be either set or shown with a single command.  So the
1033      usual add_set_cmd() and add_show_from_set() commands aren't really
1034      appropriate. */
1035   add_cmd ("radix", class_support, set_radix,
1036 	   "Set default input and output number radices.\n\
1037 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1038 Without an argument, sets both radices back to the default value of 10.",
1039 	   &setlist);
1040   add_cmd ("radix", class_support, show_radix,
1041 	   "Show the default input and output number radices.\n\
1042 Use 'show input-radix' or 'show output-radix' to independently show each.",
1043 	   &showlist);
1044 
1045   /* Give people the defaults which they are used to.  */
1046   prettyprint_structs = 0;
1047   prettyprint_arrays = 0;
1048   unionprint = 1;
1049   addressprint = 1;
1050   print_max = PRINT_MAX_DEFAULT;
1051 }
1052