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