home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / valprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-06  |  31.1 KB  |  1,109 lines

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