]> Git Repo - binutils.git/blob - gdb/valprint.c
* Makefile.in, as.texinfo: Renamed asdoc-config.texi to asconfig.texi.
[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 ((GDB_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));
47
48 static void
49 show_radix PARAMS ((char *, int));
50
51 static void
52 set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
53
54 static void
55 set_input_radix_1 PARAMS ((int, unsigned));
56
57 static void
58 set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
59
60 static void
61 set_output_radix_1 PARAMS ((int, unsigned));
62
63 static void value_print_array_elements PARAMS ((value_ptr, GDB_FILE *, int,
64                                                 enum val_prettyprint));
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 int prettyprint_structs;        /* Controls pretty printing of structures */
87 int prettyprint_arrays;         /* Controls pretty printing of arrays.  */
88
89 /* If nonzero, causes unions inside structures or other unions to be
90    printed. */
91
92 int unionprint;                 /* Controls printing of nested unions.  */
93
94 /* If nonzero, causes machine addresses to be printed in certain contexts. */
95
96 int addressprint;               /* Controls printing of machine addresses */
97
98 \f
99 /* Print data of type TYPE located at VALADDR (within GDB), which came from
100    the inferior at address ADDRESS, onto stdio stream STREAM according to
101    FORMAT (a letter, or 0 for natural format using TYPE).
102
103    If DEREF_REF is nonzero, then dereference references, otherwise just print
104    them like pointers.
105
106    The PRETTY parameter controls prettyprinting.
107
108    If the data are a string pointer, returns the number of string characters
109    printed.
110
111    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
112    enhanced to be able to debug more than the single target it was compiled
113    for (specific CPU type and thus specific target byte ordering), then
114    either the print routines are going to have to take this into account,
115    or the data is going to have to be passed into here already converted
116    to the host byte ordering, whichever is more convenient. */
117
118
119 int
120 val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty)
121      struct type *type;
122      char *valaddr;
123      CORE_ADDR address;
124      GDB_FILE *stream;
125      int format;
126      int deref_ref;
127      int recurse;
128      enum val_prettyprint pretty;
129 {
130   if (pretty == Val_pretty_default)
131     {
132       pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
133     }
134   
135   QUIT;
136
137   /* Ensure that the type is complete and not just a stub.  If the type is
138      only a stub and we can't find and substitute its complete type, then
139      print appropriate string and return.  */
140
141   check_stub_type (type);
142   if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
143     {
144       fprintf_filtered (stream, "<incomplete type>");
145       gdb_flush (stream);
146       return (0);
147     }
148   
149   return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref,
150                         recurse, pretty));
151 }
152
153 /* Print the value VAL in C-ish syntax on stream STREAM.
154    FORMAT is a format-letter, or 0 for print in natural format of data type.
155    If the object printed is a string pointer, returns
156    the number of string bytes printed.  */
157
158 int
159 value_print (val, stream, format, pretty)
160      value_ptr val;
161      GDB_FILE *stream;
162      int format;
163      enum val_prettyprint pretty;
164 {
165   register unsigned int n, typelen;
166
167   if (val == 0)
168     {
169       printf_filtered ("<address of value unknown>");
170       return 0;
171     }
172   if (VALUE_OPTIMIZED_OUT (val))
173     {
174       printf_filtered ("<value optimized out>");
175       return 0;
176     }
177
178   /* A "repeated" value really contains several values in a row.
179      They are made by the @ operator.
180      Print such values as if they were arrays.  */
181
182   if (VALUE_REPEATED (val))
183     {
184       n = VALUE_REPETITIONS (val);
185       typelen = TYPE_LENGTH (VALUE_TYPE (val));
186       fprintf_filtered (stream, "{");
187       /* Print arrays of characters using string syntax.  */
188       if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
189           && format == 0)
190         LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
191       else
192         {
193           value_print_array_elements (val, stream, format, pretty);
194         }
195       fprintf_filtered (stream, "}");
196       return (n * typelen);
197     }
198   else
199     {
200       struct type *type = VALUE_TYPE (val);
201
202       /* If it is a pointer, indicate what it points to.
203
204          Print type also if it is a reference.
205
206          C++: if it is a member pointer, we will take care
207          of that when we print it.  */
208       if (TYPE_CODE (type) == TYPE_CODE_PTR ||
209           TYPE_CODE (type) == TYPE_CODE_REF)
210         {
211           /* Hack:  remove (char *) for char strings.  Their
212              type is indicated by the quoted string anyway. */
213           if (TYPE_CODE (type) == TYPE_CODE_PTR &&
214               TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) &&
215               TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
216               !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
217             {
218                 /* Print nothing */
219             }
220           else
221             {
222               fprintf_filtered (stream, "(");
223               type_print (type, "", stream, -1);
224               fprintf_filtered (stream, ") ");
225             }
226         }
227       return (val_print (type, VALUE_CONTENTS (val),
228                          VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
229     }
230 }
231
232 /*  Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
233
234 void
235 val_print_type_code_int (type, valaddr, stream)
236      struct type *type;
237      char *valaddr;
238      GDB_FILE *stream;
239 {
240   char *p;
241   /* Pointer to first (i.e. lowest address) nonzero character.  */
242   char *first_addr;
243   unsigned int len;
244
245   if (TYPE_LENGTH (type) > sizeof (LONGEST))
246     {
247       if (TYPE_UNSIGNED (type))
248         {
249           /* First figure out whether the number in fact has zeros
250              in all its bytes more significant than least significant
251              sizeof (LONGEST) ones.  */
252           len = TYPE_LENGTH (type);
253           
254 #if TARGET_BYTE_ORDER == BIG_ENDIAN
255           for (p = valaddr;
256                len > sizeof (LONGEST) && p < valaddr + TYPE_LENGTH (type);
257                p++)
258 #else           /* Little endian.  */
259           first_addr = valaddr;
260           for (p = valaddr + TYPE_LENGTH (type) - 1;
261                len > sizeof (LONGEST) && p >= valaddr;
262                p--)
263 #endif          /* Little endian.  */
264             {
265               if (*p == 0)
266                 {
267                   len--;
268                 }
269               else
270                 {
271                   break;
272                 }
273             }
274 #if TARGET_BYTE_ORDER == BIG_ENDIAN
275           first_addr = p;
276 #endif
277           if (len <= sizeof (LONGEST))
278             {
279               /* The most significant bytes are zero, so we can just get
280                  the least significant sizeof (LONGEST) bytes and print it
281                  in decimal.  */
282               print_longest (stream, 'u', 0,
283                              extract_unsigned_integer (first_addr,
284                                                        sizeof (LONGEST)));
285             }
286           else
287             {
288               /* It is big, so print it in hex.  */
289               print_hex_chars (stream, (unsigned char *) first_addr, len);
290             }
291         }
292       else
293         {
294           /* Signed.  One could assume two's complement (a reasonable
295              assumption, I think) and do better than this.  */
296           print_hex_chars (stream, (unsigned char *) valaddr,
297                            TYPE_LENGTH (type));
298         }
299     }
300   else
301     {
302 #ifdef PRINT_TYPELESS_INTEGER
303       PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
304 #else
305       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
306                      unpack_long (type, valaddr));
307 #endif
308     }
309 }
310
311 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
312    The raison d'etre of this function is to consolidate printing of LONG_LONG's
313    into this one function.  Some platforms have long longs but don't have a
314    printf() that supports "ll" in the format string.  We handle these by seeing
315    if the number is actually a long, and if not we just bail out and print the
316    number in hex.  The format chars b,h,w,g are from
317    print_scalar_formatted().  USE_LOCAL says whether or not to call the
318    local formatting routine to get the format.  */
319
320 void
321 print_longest (stream, format, use_local, val_long)
322      GDB_FILE *stream;
323      int format;
324      int use_local;
325      LONGEST val_long;
326 {
327 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
328   long vtop, vbot;
329
330   vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
331   vbot = (long) val_long;
332
333   if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
334       || ((format == 'u' || format == 'x') && (unsigned long long)val_long > UINT_MAX))
335     {
336       fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
337       return;
338     }
339 #endif
340
341 #ifdef PRINTF_HAS_LONG_LONG
342   switch (format)
343     {
344     case 'd':
345       fprintf_filtered (stream,
346                         use_local ? local_decimal_format_custom ("ll")
347                                   : "%lld",
348                         val_long);
349       break;
350     case 'u':
351       fprintf_filtered (stream, "%llu", val_long);
352       break;
353     case 'x':
354       fprintf_filtered (stream,
355                         use_local ? local_hex_format_custom ("ll")
356                                   : "%llx",
357                         val_long);
358       break;
359     case 'o':
360       fprintf_filtered (stream,
361                         use_local ? local_octal_format_custom ("ll")
362                                   : "%llo",
363       break;
364     case 'b':
365       fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
366       break;
367     case 'h':
368       fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
369       break;
370     case 'w':
371       fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
372       break;
373     case 'g':
374       fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
375       break;
376     default:
377       abort ();
378     }
379 #else /* !PRINTF_HAS_LONG_LONG */
380   /* In the following it is important to coerce (val_long) to a long. It does
381      nothing if !LONG_LONG, but it will chop off the top half (which we know
382      we can ignore) if the host supports long longs.  */
383
384   switch (format)
385     {
386     case 'd':
387       fprintf_filtered (stream,
388                         use_local ? local_decimal_format_custom ("l")
389                                   : "%ld",
390                         (long) val_long);
391       break;
392     case 'u':
393       fprintf_filtered (stream, "%lu", (unsigned long) val_long);
394       break;
395     case 'x':
396       fprintf_filtered (stream,
397                         use_local ? local_hex_format_custom ("l")
398                                   : "%lx",
399                         (long) val_long);
400       break;
401     case 'o':
402       fprintf_filtered (stream,
403                         use_local ? local_octal_format_custom ("l")
404                                   : "%lo",
405                         (long) val_long);
406       break;
407     case 'b':
408       fprintf_filtered (stream, local_hex_format_custom ("02l"),
409                         (long) val_long);
410       break;
411     case 'h':
412       fprintf_filtered (stream, local_hex_format_custom ("04l"),
413                         (long) val_long);
414       break;
415     case 'w':
416       fprintf_filtered (stream, local_hex_format_custom ("08l"),
417                         (long) val_long);
418       break;
419     case 'g':
420       fprintf_filtered (stream, local_hex_format_custom ("016l"),
421                         (long) val_long);
422       break;
423     default:
424       abort ();
425     }
426 #endif /* !PRINTF_HAS_LONG_LONG */
427 }
428
429 /* This used to be a macro, but I don't think it is called often enough
430    to merit such treatment.  */
431 /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
432    arguments to a function, number in a value history, register number, etc.)
433    where the value must not be larger than can fit in an int.  */
434
435 int
436 longest_to_int (arg)
437      LONGEST arg;
438 {
439
440   /* This check is in case a system header has botched the
441      definition of INT_MIN, like on BSDI.  */
442   if (sizeof (LONGEST) <= sizeof (int))
443     return arg;
444
445   if (arg > INT_MAX || arg < INT_MIN)
446     error ("Value out of range.");
447
448   return arg;
449 }
450
451 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
452    on STREAM.  */
453
454 void
455 print_floating (valaddr, type, stream)
456      char *valaddr;
457      struct type *type;
458      GDB_FILE *stream;
459 {
460   double doub;
461   int inv;
462   unsigned len = TYPE_LENGTH (type);
463   
464 #if defined (IEEE_FLOAT)
465
466   /* Check for NaN's.  Note that this code does not depend on us being
467      on an IEEE conforming system.  It only depends on the target
468      machine using IEEE representation.  This means (a)
469      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
470      be defined for systems like the 68881, which uses IEEE
471      representation, but is not IEEE conforming.  */
472
473   {
474     unsigned long low, high;
475     /* Is the sign bit 0?  */
476     int nonnegative;
477     /* Is it is a NaN (i.e. the exponent is all ones and
478        the fraction is nonzero)?  */
479     int is_nan;
480
481     if (len == 4)
482       {
483         /* It's single precision.  */
484         /* Assume that floating point byte order is the same as
485            integer byte order.  */
486         low = extract_unsigned_integer (valaddr, 4);
487         nonnegative = ((low & 0x80000000) == 0);
488         is_nan = ((((low >> 23) & 0xFF) == 0xFF) 
489                   && 0 != (low & 0x7FFFFF));
490         low &= 0x7fffff;
491         high = 0;
492       }
493     else if (len == 8)
494       {
495         /* It's double precision.  Get the high and low words.  */
496
497         /* Assume that floating point byte order is the same as
498            integer byte order.  */
499 #if TARGET_BYTE_ORDER == BIG_ENDIAN
500         low = extract_unsigned_integer (valaddr + 4, 4);
501         high = extract_unsigned_integer (valaddr, 4);
502 #else
503         low = extract_unsigned_integer (valaddr, 4);
504         high = extract_unsigned_integer (valaddr + 4, 4);
505 #endif
506         nonnegative = ((high & 0x80000000) == 0);
507         is_nan = (((high >> 20) & 0x7ff) == 0x7ff
508                   && ! ((((high & 0xfffff) == 0)) && (low == 0)));
509         high &= 0xfffff;
510       }
511     else
512       /* Extended.  We can't detect NaNs for extendeds yet.  Also note
513          that currently extendeds get nuked to double in
514          REGISTER_CONVERTIBLE.  */
515       is_nan = 0;
516
517     if (is_nan)
518       {
519         /* The meaning of the sign and fraction is not defined by IEEE.
520            But the user might know what they mean.  For example, they
521            (in an implementation-defined manner) distinguish between
522            signaling and quiet NaN's.  */
523         if (high)
524           fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
525                             high, low);
526         else
527           fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
528         return;
529       }
530   }
531 #endif /* IEEE_FLOAT.  */
532
533   doub = unpack_double (type, valaddr, &inv);
534   if (inv)
535     fprintf_filtered (stream, "<invalid float value>");
536   else
537     fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
538 }
539
540 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
541
542 static void
543 print_hex_chars (stream, valaddr, len)
544      GDB_FILE *stream;
545      unsigned char *valaddr;
546      unsigned len;
547 {
548   unsigned char *p;
549
550   /* FIXME: We should be not printing leading zeroes in most cases.  */
551
552   fprintf_filtered (stream, local_hex_format_prefix ());
553 #if TARGET_BYTE_ORDER == BIG_ENDIAN
554   for (p = valaddr;
555        p < valaddr + len;
556        p++)
557 #else /* Little endian.  */
558   for (p = valaddr + len - 1;
559        p >= valaddr;
560        p--)
561 #endif
562     {
563       fprintf_filtered (stream, "%02x", *p);
564     }
565   fprintf_filtered (stream, local_hex_format_suffix ());
566 }
567
568 /*  Called by various <lang>_val_print routines to print elements of an
569     array in the form "<elem1>, <elem2>, <elem3>, ...".
570
571     (FIXME?)  Assumes array element separator is a comma, which is correct
572     for all languages currently handled.
573     (FIXME?)  Some languages have a notation for repeated array elements,
574     perhaps we should try to use that notation when appropriate.
575     */
576
577 void
578 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
579                           recurse, pretty, i)
580      struct type *type;
581      char *valaddr;
582      CORE_ADDR address;
583      GDB_FILE *stream;
584      int format;
585      int deref_ref;
586      int recurse;
587      enum val_prettyprint pretty;
588      unsigned int i;
589 {
590   unsigned int things_printed = 0;
591   unsigned len;
592   struct type *elttype;
593   unsigned eltlen;
594   /* Position of the array element we are examining to see
595      whether it is repeated.  */
596   unsigned int rep1;
597   /* Number of repetitions we have detected so far.  */
598   unsigned int reps;
599       
600   elttype = TYPE_TARGET_TYPE (type);
601   eltlen = TYPE_LENGTH (elttype);
602   len = TYPE_LENGTH (type) / eltlen;
603               
604   for (; i < len && things_printed < print_max; i++)
605     {
606       if (i != 0)
607         {
608           if (prettyprint_arrays)
609             {
610               fprintf_filtered (stream, ",\n");
611               print_spaces_filtered (2 + 2 * recurse, stream);
612             }
613           else
614             {
615               fprintf_filtered (stream, ", ");
616             }
617         }
618       wrap_here (n_spaces (2 + 2 * recurse));
619       
620       rep1 = i + 1;
621       reps = 1;
622       while ((rep1 < len) && 
623              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
624         {
625           ++reps;
626           ++rep1;
627         }
628       
629       if (reps > repeat_count_threshold)
630         {
631           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
632                      deref_ref, recurse + 1, pretty);
633           fprintf_filtered (stream, " <repeats %u times>", reps);
634           i = rep1 - 1;
635           things_printed += repeat_count_threshold;
636         }
637       else
638         {
639           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
640                      deref_ref, recurse + 1, pretty);
641           things_printed++;
642         }
643     }
644   if (i < len)
645     {
646       fprintf_filtered (stream, "...");
647     }
648 }
649
650 static void
651 value_print_array_elements (val, stream, format, pretty)
652      value_ptr val;
653      GDB_FILE *stream;
654      int format;
655      enum val_prettyprint pretty;
656 {
657   unsigned int things_printed = 0;
658   register unsigned int i, n, typelen;
659   /* Position of the array elem we are examining to see if it is repeated.  */
660   unsigned int rep1;
661   /* Number of repetitions we have detected so far.  */
662   unsigned int reps;
663     
664   n = VALUE_REPETITIONS (val);
665   typelen = TYPE_LENGTH (VALUE_TYPE (val));
666   for (i = 0; i < n && things_printed < print_max; i++)
667     {
668       if (i != 0)
669         {
670           fprintf_filtered (stream, ", ");
671         }
672       wrap_here ("");
673       
674       rep1 = i + 1;
675       reps = 1;
676       while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
677                                   VALUE_CONTENTS (val) + typelen * rep1,
678                                   typelen))
679         {
680           ++reps;
681           ++rep1;
682         }
683       
684       if (reps > repeat_count_threshold)
685         {
686           val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
687                      VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
688                      0, pretty);
689           fprintf_unfiltered (stream, " <repeats %u times>", reps);
690           i = rep1 - 1;
691           things_printed += repeat_count_threshold;
692         }
693       else
694         {
695           val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
696                      VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
697                      0, pretty);
698           things_printed++;
699         }
700     }
701   if (i < n)
702     {
703       fprintf_filtered (stream, "...");
704     }
705 }
706
707 /*  Print a string from the inferior, starting at ADDR and printing up to LEN
708     characters, to STREAM.  If LEN is zero, printing stops at the first null
709     byte, otherwise printing proceeds (including null bytes) until either
710     print_max or LEN characters have been printed, whichever is smaller. */
711
712 /* FIXME: All callers supply LEN of zero.  Supplying a non-zero LEN is
713    pointless, this routine just then becomes a convoluted version of
714    target_read_memory_partial.  Removing all the LEN stuff would simplify
715    this routine enormously.
716
717    FIXME: Use target_read_string.  */
718
719 int
720 val_print_string (addr, len, stream)
721     CORE_ADDR addr;
722     unsigned int len;
723     GDB_FILE *stream;
724 {
725   int force_ellipsis = 0;       /* Force ellipsis to be printed if nonzero. */
726   int errcode;                  /* Errno returned from bad reads. */
727   unsigned int fetchlimit;      /* Maximum number of bytes to fetch. */
728   unsigned int nfetch;          /* Bytes to fetch / bytes fetched. */
729   unsigned int chunksize;       /* Size of each fetch, in bytes. */
730   int bufsize;                  /* Size of current fetch buffer. */
731   char *buffer = NULL;          /* Dynamically growable fetch buffer. */
732   char *bufptr;                 /* Pointer to next available byte in buffer. */
733   char *limit;                  /* First location past end of fetch buffer. */
734   struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
735   char peekchar;                /* Place into which we can read one char. */
736
737   /* First we need to figure out the limit on the number of characters we are
738      going to attempt to fetch and print.  This is actually pretty simple.  If
739      LEN is nonzero, then the limit is the minimum of LEN and print_max.  If
740      LEN is zero, then the limit is print_max.  This is true regardless of
741      whether print_max is zero, UINT_MAX (unlimited), or something in between,
742      because finding the null byte (or available memory) is what actually
743      limits the fetch. */
744
745   fetchlimit = (len == 0 ? print_max : min (len, print_max));
746
747   /* Now decide how large of chunks to try to read in one operation.  This
748      is also pretty simple.  If LEN is nonzero, then we want fetchlimit bytes,
749      so we might as well read them all in one operation.  If LEN is zero, we
750      are looking for a null terminator to end the fetching, so we might as
751      well read in blocks that are large enough to be efficient, but not so
752      large as to be slow if fetchlimit happens to be large.  So we choose the
753      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
754      200 is way too big for remote debugging over a serial line.  */
755
756   chunksize = (len == 0 ? min (8, fetchlimit) : fetchlimit);
757
758   /* Loop until we either have all the characters to print, or we encounter
759      some error, such as bumping into the end of the address space. */
760
761   bufsize = 0;
762   do {
763     QUIT;
764     /* Figure out how much to fetch this time, and grow the buffer to fit. */
765     nfetch = min (chunksize, fetchlimit - bufsize);
766     bufsize += nfetch;
767     if (buffer == NULL)
768       {
769         buffer = (char *) xmalloc (bufsize);
770         bufptr = buffer;
771       }
772     else
773       {
774         discard_cleanups (old_chain);
775         buffer = (char *) xrealloc (buffer, bufsize);
776         bufptr = buffer + bufsize - nfetch;
777       }
778     old_chain = make_cleanup (free, buffer);
779
780     /* Read as much as we can. */
781     nfetch = target_read_memory_partial (addr, bufptr, nfetch, &errcode);
782     if (len != 0)
783       {
784         addr += nfetch;
785         bufptr += nfetch;
786       }
787     else
788       {
789         /* Scan this chunk for the null byte that terminates the string
790            to print.  If found, we don't need to fetch any more.  Note
791            that bufptr is explicitly left pointing at the next character
792            after the null byte, or at the next character after the end of
793            the buffer. */
794         limit = bufptr + nfetch;
795         while (bufptr < limit)
796           {
797             ++addr;
798             ++bufptr;
799             if (bufptr[-1] == '\0')
800               {
801                 /* We don't care about any error which happened after
802                    the NULL terminator.  */
803                 errcode = 0;
804                 break;
805               }
806           }
807       }
808   } while (errcode == 0                                 /* no error */
809            && bufsize < fetchlimit                      /* no overrun */
810            && !(len == 0 && *(bufptr - 1) == '\0'));    /* no null term */
811
812   /* bufptr and addr now point immediately beyond the last byte which we
813      consider part of the string (including a '\0' which ends the string).  */
814
815   /* We now have either successfully filled the buffer to fetchlimit, or
816      terminated early due to an error or finding a null byte when LEN is
817      zero.  */
818
819   if (len == 0 && bufptr > buffer && *(bufptr - 1) != '\0')
820     {
821       /* We didn't find a null terminator we were looking for.  Attempt
822          to peek at the next character.  If not successful, or it is not
823          a null byte, then force ellipsis to be printed.  */
824       if (target_read_memory (addr, &peekchar, 1) != 0 || peekchar != '\0')
825         {
826           force_ellipsis = 1;
827         }
828     }
829   else if ((len != 0 && errcode != 0) || (len > bufptr - buffer))
830     {
831       /* Getting an error when we have a requested length, or fetching less
832          than the number of characters actually requested, always make us
833          print ellipsis. */
834       force_ellipsis = 1;
835     }
836
837   QUIT;
838
839   /* If we get an error before fetching anything, don't print a string.
840      But if we fetch something and then get an error, print the string
841      and then the error message.  */
842   if (errcode == 0 || bufptr > buffer)
843     {
844       if (addressprint)
845         {
846           fputs_filtered (" ", stream);
847         }
848       LA_PRINT_STRING (stream, buffer, bufptr - buffer, force_ellipsis);
849     }
850
851   if (errcode != 0)
852     {
853       if (errcode == EIO)
854         {
855           fprintf_filtered (stream, " <Address ");
856           print_address_numeric (addr, stream);
857           fprintf_filtered (stream, " out of bounds>");
858         }
859       else
860         {
861           fprintf_filtered (stream, " <Error reading address ");
862           print_address_numeric (addr, stream);
863           fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
864         }
865     }
866   gdb_flush (stream);
867   do_cleanups (old_chain);
868   return (bufptr - buffer);
869 }
870
871 \f
872 /* Validate an input or output radix setting, and make sure the user
873    knows what they really did here.  Radix setting is confusing, e.g.
874    setting the input radix to "10" never changes it!  */
875
876 /* ARGSUSED */
877 static void
878 set_input_radix (args, from_tty, c)
879      char *args;
880      int from_tty;
881      struct cmd_list_element *c;
882 {
883   set_input_radix_1 (from_tty, *(unsigned *)c->var);
884 }
885
886 /* ARGSUSED */
887 static void
888 set_input_radix_1 (from_tty, radix)
889      int from_tty;
890      unsigned radix;
891 {
892   /* We don't currently disallow any input radix except 0 or 1, which don't
893      make any mathematical sense.  In theory, we can deal with any input
894      radix greater than 1, even if we don't have unique digits for every
895      value from 0 to radix-1, but in practice we lose on large radix values.
896      We should either fix the lossage or restrict the radix range more.
897      (FIXME). */
898
899   if (radix < 2)
900     {
901       error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
902              radix);
903     }
904   input_radix = radix;
905   if (from_tty)
906     {
907       printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
908                        radix, radix, radix);
909     }
910 }
911
912 /* ARGSUSED */
913 static void
914 set_output_radix (args, from_tty, c)
915      char *args;
916      int from_tty;
917      struct cmd_list_element *c;
918 {
919   set_output_radix_1 (from_tty, *(unsigned *)c->var);
920 }
921
922 static void
923 set_output_radix_1 (from_tty, radix)
924      int from_tty;
925      unsigned radix;
926 {
927   /* Validate the radix and disallow ones that we aren't prepared to
928      handle correctly, leaving the radix unchanged. */
929   switch (radix)
930     {
931     case 16:
932       output_format = 'x';              /* hex */
933       break;
934     case 10:
935       output_format = 0;                /* decimal */
936       break;
937     case 8:
938       output_format = 'o';              /* octal */
939       break;
940     default:
941       error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
942              radix);
943     }
944   output_radix = radix;
945   if (from_tty)
946     {
947       printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
948                        radix, radix, radix);
949     }
950 }
951
952 /* Set both the input and output radix at once.  Try to set the output radix
953    first, since it has the most restrictive range.  An radix that is valid as
954    an output radix is also valid as an input radix.
955
956    It may be useful to have an unusual input radix.  If the user wishes to
957    set an input radix that is not valid as an output radix, he needs to use
958    the 'set input-radix' command. */
959
960 static void
961 set_radix (arg, from_tty)
962      char *arg;
963      int from_tty;
964 {
965   unsigned radix;
966
967   radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
968   set_output_radix_1 (0, radix);
969   set_input_radix_1 (0, radix);
970   if (from_tty)
971     {
972       printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
973                        radix, radix, radix);
974     }
975 }
976
977 /* Show both the input and output radices. */
978
979 /*ARGSUSED*/
980 static void
981 show_radix (arg, from_tty)
982      char *arg;
983      int from_tty;
984 {
985   if (from_tty)
986     {
987       if (input_radix == output_radix)
988         {
989           printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
990                            input_radix, input_radix, input_radix);
991         }
992       else
993         {
994           printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
995                            input_radix, input_radix, input_radix);
996           printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
997                            output_radix, output_radix, output_radix);
998         }
999     }
1000 }
1001
1002 \f
1003 /*ARGSUSED*/
1004 static void
1005 set_print (arg, from_tty)
1006      char *arg;
1007      int from_tty;
1008 {
1009   printf_unfiltered (
1010 "\"set print\" must be followed by the name of a print subcommand.\n");
1011   help_list (setprintlist, "set print ", -1, gdb_stdout);
1012 }
1013
1014 /*ARGSUSED*/
1015 static void
1016 show_print (args, from_tty)
1017      char *args;
1018      int from_tty;
1019 {
1020   cmd_show_list (showprintlist, from_tty, "");
1021 }
1022 \f
1023 void
1024 _initialize_valprint ()
1025 {
1026   struct cmd_list_element *c;
1027
1028   add_prefix_cmd ("print", no_class, set_print,
1029                   "Generic command for setting how things print.",
1030                   &setprintlist, "set print ", 0, &setlist);
1031   add_alias_cmd ("p", "print", no_class, 1, &setlist); 
1032   /* prefer set print to set prompt */ 
1033   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1034
1035   add_prefix_cmd ("print", no_class, show_print,
1036                   "Generic command for showing print settings.",
1037                   &showprintlist, "show print ", 0, &showlist);
1038   add_alias_cmd ("p", "print", no_class, 1, &showlist); 
1039   add_alias_cmd ("pr", "print", no_class, 1, &showlist); 
1040
1041   add_show_from_set
1042     (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
1043                   "Set limit on string chars or array elements to print.\n\
1044 \"set print elements 0\" causes there to be no limit.",
1045                   &setprintlist),
1046      &showprintlist);
1047
1048   add_show_from_set
1049     (add_set_cmd ("repeats", no_class, var_uinteger,
1050                   (char *)&repeat_count_threshold,
1051                   "Set threshold for repeated print elements.\n\
1052 \"set print repeats 0\" causes all elements to be individually printed.",
1053                   &setprintlist),
1054      &showprintlist);
1055
1056   add_show_from_set
1057     (add_set_cmd ("pretty", class_support, var_boolean,
1058                   (char *)&prettyprint_structs,
1059                   "Set prettyprinting of structures.",
1060                   &setprintlist),
1061      &showprintlist);
1062
1063   add_show_from_set
1064     (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
1065                   "Set printing of unions interior to structures.",
1066                   &setprintlist),
1067      &showprintlist);
1068   
1069   add_show_from_set
1070     (add_set_cmd ("array", class_support, var_boolean,
1071                   (char *)&prettyprint_arrays,
1072                   "Set prettyprinting of arrays.",
1073                   &setprintlist),
1074      &showprintlist);
1075
1076   add_show_from_set
1077     (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
1078                   "Set printing of addresses.",
1079                   &setprintlist),
1080      &showprintlist);
1081
1082   c = add_set_cmd ("input-radix", class_support, var_uinteger,
1083                    (char *)&input_radix,
1084                   "Set default input radix for entering numbers.",
1085                   &setlist);
1086   add_show_from_set (c, &showlist);
1087   c->function.sfunc = set_input_radix;
1088
1089   c = add_set_cmd ("output-radix", class_support, var_uinteger,
1090                    (char *)&output_radix,
1091                   "Set default output radix for printing of values.",
1092                   &setlist);
1093   add_show_from_set (c, &showlist);
1094   c->function.sfunc = set_output_radix;
1095
1096   /* The "set radix" and "show radix" commands are special in that they are
1097      like normal set and show commands but allow two normally independent
1098      variables to be either set or shown with a single command.  So the
1099      usual add_set_cmd() and add_show_from_set() commands aren't really
1100      appropriate. */
1101   add_cmd ("radix", class_support, set_radix,
1102            "Set default input and output number radices.\n\
1103 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1104 Without an argument, sets both radices back to the default value of 10.",
1105            &setlist);
1106   add_cmd ("radix", class_support, show_radix,
1107            "Show the default input and output number radices.\n\
1108 Use 'show input-radix' or 'show output-radix' to independently show each.",
1109            &showlist);
1110
1111   /* Give people the defaults which they are used to.  */
1112   prettyprint_structs = 0;
1113   prettyprint_arrays = 0;
1114   unionprint = 1;
1115   addressprint = 1;
1116   print_max = PRINT_MAX_DEFAULT;
1117 }
This page took 0.087866 seconds and 4 git commands to generate.