gdb/valprint.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Print values for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "value.h"
  18. #include "gdbcore.h"
  19. #include "gdbcmd.h"
  20. #include "target.h"
  21. #include "language.h"
  22. #include "annotate.h"
  23. #include "valprint.h"
  24. #include "floatformat.h"
  25. #include "doublest.h"
  26. #include "dfp.h"
  27. #include "extension.h"
  28. #include "ada-lang.h"
  29. #include "gdb_obstack.h"
  30. #include "charset.h"
  31. #include <ctype.h>

  32. /* Maximum number of wchars returned from wchar_iterate.  */
  33. #define MAX_WCHARS 4

  34. /* A convenience macro to compute the size of a wchar_t buffer containing X
  35.    characters.  */
  36. #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))

  37. /* Character buffer size saved while iterating over wchars.  */
  38. #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)

  39. /* A structure to encapsulate state information from iterated
  40.    character conversions.  */
  41. struct converted_character
  42. {
  43.   /* The number of characters converted.  */
  44.   int num_chars;

  45.   /* The result of the conversion.  See charset.h for more.  */
  46.   enum wchar_iterate_result result;

  47.   /* The (saved) converted character(s).  */
  48.   gdb_wchar_t chars[WCHAR_BUFLEN_MAX];

  49.   /* The first converted target byte.  */
  50.   const gdb_byte *buf;

  51.   /* The number of bytes converted.  */
  52.   size_t buflen;

  53.   /* How many times this character(s) is repeated.  */
  54.   int repeat_count;
  55. };

  56. typedef struct converted_character converted_character_d;
  57. DEF_VEC_O (converted_character_d);

  58. /* Command lists for set/show print raw.  */
  59. struct cmd_list_element *setprintrawlist;
  60. struct cmd_list_element *showprintrawlist;

  61. /* Prototypes for local functions */

  62. static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
  63.                                 int len, int *errptr);

  64. static void show_print (char *, int);

  65. static void set_print (char *, int);

  66. static void set_radix (char *, int);

  67. static void show_radix (char *, int);

  68. static void set_input_radix (char *, int, struct cmd_list_element *);

  69. static void set_input_radix_1 (int, unsigned);

  70. static void set_output_radix (char *, int, struct cmd_list_element *);

  71. static void set_output_radix_1 (int, unsigned);

  72. void _initialize_valprint (void);

  73. #define PRINT_MAX_DEFAULT 200        /* Start print_max off at this value.  */

  74. struct value_print_options user_print_options =
  75. {
  76.   Val_prettyformat_default,        /* prettyformat */
  77.   0,                                /* prettyformat_arrays */
  78.   0,                                /* prettyformat_structs */
  79.   0,                                /* vtblprint */
  80.   1,                                /* unionprint */
  81.   1,                                /* addressprint */
  82.   0,                                /* objectprint */
  83.   PRINT_MAX_DEFAULT,                /* print_max */
  84.   10,                                /* repeat_count_threshold */
  85.   0,                                /* output_format */
  86.   0,                                /* format */
  87.   0,                                /* stop_print_at_null */
  88.   0,                                /* print_array_indexes */
  89.   0,                                /* deref_ref */
  90.   1,                                /* static_field_print */
  91.   1,                                /* pascal_static_field_print */
  92.   0,                                /* raw */
  93.   0,                                /* summary */
  94.   1                                /* symbol_print */
  95. };

  96. /* Initialize *OPTS to be a copy of the user print options.  */
  97. void
  98. get_user_print_options (struct value_print_options *opts)
  99. {
  100.   *opts = user_print_options;
  101. }

  102. /* Initialize *OPTS to be a copy of the user print options, but with
  103.    pretty-formatting disabled.  */
  104. void
  105. get_no_prettyformat_print_options (struct value_print_options *opts)
  106. {
  107.   *opts = user_print_options;
  108.   opts->prettyformat = Val_no_prettyformat;
  109. }

  110. /* Initialize *OPTS to be a copy of the user print options, but using
  111.    FORMAT as the formatting option.  */
  112. void
  113. get_formatted_print_options (struct value_print_options *opts,
  114.                              char format)
  115. {
  116.   *opts = user_print_options;
  117.   opts->format = format;
  118. }

  119. static void
  120. show_print_max (struct ui_file *file, int from_tty,
  121.                 struct cmd_list_element *c, const char *value)
  122. {
  123.   fprintf_filtered (file,
  124.                     _("Limit on string chars or array "
  125.                       "elements to print is %s.\n"),
  126.                     value);
  127. }


  128. /* Default input and output radixes, and output format letter.  */

  129. unsigned input_radix = 10;
  130. static void
  131. show_input_radix (struct ui_file *file, int from_tty,
  132.                   struct cmd_list_element *c, const char *value)
  133. {
  134.   fprintf_filtered (file,
  135.                     _("Default input radix for entering numbers is %s.\n"),
  136.                     value);
  137. }

  138. unsigned output_radix = 10;
  139. static void
  140. show_output_radix (struct ui_file *file, int from_tty,
  141.                    struct cmd_list_element *c, const char *value)
  142. {
  143.   fprintf_filtered (file,
  144.                     _("Default output radix for printing of values is %s.\n"),
  145.                     value);
  146. }

  147. /* By default we print arrays without printing the index of each element in
  148.    the array.  This behavior can be changed by setting PRINT_ARRAY_INDEXES.  */

  149. static void
  150. show_print_array_indexes (struct ui_file *file, int from_tty,
  151.                           struct cmd_list_element *c, const char *value)
  152. {
  153.   fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
  154. }

  155. /* Print repeat counts if there are more than this many repetitions of an
  156.    element in an array.  Referenced by the low level language dependent
  157.    print routines.  */

  158. static void
  159. show_repeat_count_threshold (struct ui_file *file, int from_tty,
  160.                              struct cmd_list_element *c, const char *value)
  161. {
  162.   fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
  163.                     value);
  164. }

  165. /* If nonzero, stops printing of char arrays at first null.  */

  166. static void
  167. show_stop_print_at_null (struct ui_file *file, int from_tty,
  168.                          struct cmd_list_element *c, const char *value)
  169. {
  170.   fprintf_filtered (file,
  171.                     _("Printing of char arrays to stop "
  172.                       "at first null char is %s.\n"),
  173.                     value);
  174. }

  175. /* Controls pretty printing of structures.  */

  176. static void
  177. show_prettyformat_structs (struct ui_file *file, int from_tty,
  178.                           struct cmd_list_element *c, const char *value)
  179. {
  180.   fprintf_filtered (file, _("Pretty formatting of structures is %s.\n"), value);
  181. }

  182. /* Controls pretty printing of arrays.  */

  183. static void
  184. show_prettyformat_arrays (struct ui_file *file, int from_tty,
  185.                          struct cmd_list_element *c, const char *value)
  186. {
  187.   fprintf_filtered (file, _("Pretty formatting of arrays is %s.\n"), value);
  188. }

  189. /* If nonzero, causes unions inside structures or other unions to be
  190.    printed.  */

  191. static void
  192. show_unionprint (struct ui_file *file, int from_tty,
  193.                  struct cmd_list_element *c, const char *value)
  194. {
  195.   fprintf_filtered (file,
  196.                     _("Printing of unions interior to structures is %s.\n"),
  197.                     value);
  198. }

  199. /* If nonzero, causes machine addresses to be printed in certain contexts.  */

  200. static void
  201. show_addressprint (struct ui_file *file, int from_tty,
  202.                    struct cmd_list_element *c, const char *value)
  203. {
  204.   fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
  205. }

  206. static void
  207. show_symbol_print (struct ui_file *file, int from_tty,
  208.                    struct cmd_list_element *c, const char *value)
  209. {
  210.   fprintf_filtered (file,
  211.                     _("Printing of symbols when printing pointers is %s.\n"),
  212.                     value);
  213. }



  214. /* A helper function for val_print.  When printing in "summary" mode,
  215.    we want to print scalar arguments, but not aggregate arguments.
  216.    This function distinguishes between the two.  */

  217. int
  218. val_print_scalar_type_p (struct type *type)
  219. {
  220.   CHECK_TYPEDEF (type);
  221.   while (TYPE_CODE (type) == TYPE_CODE_REF)
  222.     {
  223.       type = TYPE_TARGET_TYPE (type);
  224.       CHECK_TYPEDEF (type);
  225.     }
  226.   switch (TYPE_CODE (type))
  227.     {
  228.     case TYPE_CODE_ARRAY:
  229.     case TYPE_CODE_STRUCT:
  230.     case TYPE_CODE_UNION:
  231.     case TYPE_CODE_SET:
  232.     case TYPE_CODE_STRING:
  233.       return 0;
  234.     default:
  235.       return 1;
  236.     }
  237. }

  238. /* See its definition in value.h.  */

  239. int
  240. valprint_check_validity (struct ui_file *stream,
  241.                          struct type *type,
  242.                          int embedded_offset,
  243.                          const struct value *val)
  244. {
  245.   CHECK_TYPEDEF (type);

  246.   if (TYPE_CODE (type) != TYPE_CODE_UNION
  247.       && TYPE_CODE (type) != TYPE_CODE_STRUCT
  248.       && TYPE_CODE (type) != TYPE_CODE_ARRAY)
  249.     {
  250.       if (value_bits_any_optimized_out (val,
  251.                                         TARGET_CHAR_BIT * embedded_offset,
  252.                                         TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  253.         {
  254.           val_print_optimized_out (val, stream);
  255.           return 0;
  256.         }

  257.       if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
  258.                                         TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  259.         {
  260.           fputs_filtered (_("<synthetic pointer>"), stream);
  261.           return 0;
  262.         }

  263.       if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
  264.         {
  265.           val_print_unavailable (stream);
  266.           return 0;
  267.         }
  268.     }

  269.   return 1;
  270. }

  271. void
  272. val_print_optimized_out (const struct value *val, struct ui_file *stream)
  273. {
  274.   if (val != NULL && value_lval_const (val) == lval_register)
  275.     val_print_not_saved (stream);
  276.   else
  277.     fprintf_filtered (stream, _("<optimized out>"));
  278. }

  279. void
  280. val_print_not_saved (struct ui_file *stream)
  281. {
  282.   fprintf_filtered (stream, _("<not saved>"));
  283. }

  284. void
  285. val_print_unavailable (struct ui_file *stream)
  286. {
  287.   fprintf_filtered (stream, _("<unavailable>"));
  288. }

  289. void
  290. val_print_invalid_address (struct ui_file *stream)
  291. {
  292.   fprintf_filtered (stream, _("<invalid address>"));
  293. }

  294. /* A generic val_print that is suitable for use by language
  295.    implementations of the la_val_print method.  This function can
  296.    handle most type codes, though not all, notably exception
  297.    TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
  298.    the caller.

  299.    Most arguments are as to val_print.

  300.    The additional DECORATIONS argument can be used to customize the
  301.    output in some small, language-specific ways.  */

  302. void
  303. generic_val_print (struct type *type, const gdb_byte *valaddr,
  304.                    int embedded_offset, CORE_ADDR address,
  305.                    struct ui_file *stream, int recurse,
  306.                    const struct value *original_value,
  307.                    const struct value_print_options *options,
  308.                    const struct generic_val_print_decorations *decorations)
  309. {
  310.   struct gdbarch *gdbarch = get_type_arch (type);
  311.   unsigned int i = 0;        /* Number of characters printed.  */
  312.   unsigned len;
  313.   struct type *elttype, *unresolved_elttype;
  314.   struct type *unresolved_type = type;
  315.   LONGEST val;
  316.   CORE_ADDR addr;

  317.   CHECK_TYPEDEF (type);
  318.   switch (TYPE_CODE (type))
  319.     {
  320.     case TYPE_CODE_ARRAY:
  321.       unresolved_elttype = TYPE_TARGET_TYPE (type);
  322.       elttype = check_typedef (unresolved_elttype);
  323.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
  324.         {
  325.           LONGEST low_bound, high_bound;

  326.           if (!get_array_bounds (type, &low_bound, &high_bound))
  327.             error (_("Could not determine the array high bound"));

  328.           if (options->prettyformat_arrays)
  329.             {
  330.               print_spaces_filtered (2 + 2 * recurse, stream);
  331.             }

  332.           fprintf_filtered (stream, "{");
  333.           val_print_array_elements (type, valaddr, embedded_offset,
  334.                                     address, stream,
  335.                                     recurse, original_value, options, 0);
  336.           fprintf_filtered (stream, "}");
  337.           break;
  338.         }
  339.       /* Array of unspecified length: treat like pointer to first
  340.          elt.  */
  341.       addr = address + embedded_offset;
  342.       goto print_unpacked_pointer;

  343.     case TYPE_CODE_MEMBERPTR:
  344.       val_print_scalar_formatted (type, valaddr, embedded_offset,
  345.                                   original_value, options, 0, stream);
  346.       break;

  347.     case TYPE_CODE_PTR:
  348.       if (options->format && options->format != 's')
  349.         {
  350.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  351.                                       original_value, options, 0, stream);
  352.           break;
  353.         }
  354.       unresolved_elttype = TYPE_TARGET_TYPE (type);
  355.       elttype = check_typedef (unresolved_elttype);
  356.         {
  357.           addr = unpack_pointer (type, valaddr + embedded_offset);
  358.         print_unpacked_pointer:

  359.           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  360.             {
  361.               /* Try to print what function it points to.  */
  362.               print_function_pointer_address (options, gdbarch, addr, stream);
  363.               return;
  364.             }

  365.           if (options->symbol_print)
  366.             print_address_demangle (options, gdbarch, addr, stream, demangle);
  367.           else if (options->addressprint)
  368.             fputs_filtered (paddress (gdbarch, addr), stream);
  369.         }
  370.       break;

  371.     case TYPE_CODE_REF:
  372.       elttype = check_typedef (TYPE_TARGET_TYPE (type));
  373.       if (options->addressprint)
  374.         {
  375.           CORE_ADDR addr
  376.             = extract_typed_address (valaddr + embedded_offset, type);

  377.           fprintf_filtered (stream, "@");
  378.           fputs_filtered (paddress (gdbarch, addr), stream);
  379.           if (options->deref_ref)
  380.             fputs_filtered (": ", stream);
  381.         }
  382.       /* De-reference the reference.  */
  383.       if (options->deref_ref)
  384.         {
  385.           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
  386.             {
  387.               struct value *deref_val;

  388.               deref_val = coerce_ref_if_computed (original_value);
  389.               if (deref_val != NULL)
  390.                 {
  391.                   /* More complicated computed references are not supported.  */
  392.                   gdb_assert (embedded_offset == 0);
  393.                 }
  394.               else
  395.                 deref_val = value_at (TYPE_TARGET_TYPE (type),
  396.                                       unpack_pointer (type,
  397.                                                       (valaddr
  398.                                                        + embedded_offset)));

  399.               common_val_print (deref_val, stream, recurse, options,
  400.                                 current_language);
  401.             }
  402.           else
  403.             fputs_filtered ("???", stream);
  404.         }
  405.       break;

  406.     case TYPE_CODE_ENUM:
  407.       if (options->format)
  408.         {
  409.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  410.                                       original_value, options, 0, stream);
  411.           break;
  412.         }
  413.       len = TYPE_NFIELDS (type);
  414.       val = unpack_long (type, valaddr + embedded_offset);
  415.       for (i = 0; i < len; i++)
  416.         {
  417.           QUIT;
  418.           if (val == TYPE_FIELD_ENUMVAL (type, i))
  419.             {
  420.               break;
  421.             }
  422.         }
  423.       if (i < len)
  424.         {
  425.           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  426.         }
  427.       else if (TYPE_FLAG_ENUM (type))
  428.         {
  429.           int first = 1;

  430.           /* We have a "flag" enum, so we try to decompose it into
  431.              pieces as appropriate.  A flag enum has disjoint
  432.              constants by definition.  */
  433.           fputs_filtered ("(", stream);
  434.           for (i = 0; i < len; ++i)
  435.             {
  436.               QUIT;

  437.               if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
  438.                 {
  439.                   if (!first)
  440.                     fputs_filtered (" | ", stream);
  441.                   first = 0;

  442.                   val &= ~TYPE_FIELD_ENUMVAL (type, i);
  443.                   fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  444.                 }
  445.             }

  446.           if (first || val != 0)
  447.             {
  448.               if (!first)
  449.                 fputs_filtered (" | ", stream);
  450.               fputs_filtered ("unknown: ", stream);
  451.               print_longest (stream, 'd', 0, val);
  452.             }

  453.           fputs_filtered (")", stream);
  454.         }
  455.       else
  456.         print_longest (stream, 'd', 0, val);
  457.       break;

  458.     case TYPE_CODE_FLAGS:
  459.       if (options->format)
  460.         val_print_scalar_formatted (type, valaddr, embedded_offset,
  461.                                     original_value, options, 0, stream);
  462.       else
  463.         val_print_type_code_flags (type, valaddr + embedded_offset,
  464.                                    stream);
  465.       break;

  466.     case TYPE_CODE_FUNC:
  467.     case TYPE_CODE_METHOD:
  468.       if (options->format)
  469.         {
  470.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  471.                                       original_value, options, 0, stream);
  472.           break;
  473.         }
  474.       /* FIXME, we should consider, at least for ANSI C language,
  475.          eliminating the distinction made between FUNCs and POINTERs
  476.          to FUNCs.  */
  477.       fprintf_filtered (stream, "{");
  478.       type_print (type, "", stream, -1);
  479.       fprintf_filtered (stream, "} ");
  480.       /* Try to print what function it points to, and its address.  */
  481.       print_address_demangle (options, gdbarch, address, stream, demangle);
  482.       break;

  483.     case TYPE_CODE_BOOL:
  484.       if (options->format || options->output_format)
  485.         {
  486.           struct value_print_options opts = *options;
  487.           opts.format = (options->format ? options->format
  488.                          : options->output_format);
  489.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  490.                                       original_value, &opts, 0, stream);
  491.         }
  492.       else
  493.         {
  494.           val = unpack_long (type, valaddr + embedded_offset);
  495.           if (val == 0)
  496.             fputs_filtered (decorations->false_name, stream);
  497.           else if (val == 1)
  498.             fputs_filtered (decorations->true_name, stream);
  499.           else
  500.             print_longest (stream, 'd', 0, val);
  501.         }
  502.       break;

  503.     case TYPE_CODE_RANGE:
  504.       /* FIXME: create_static_range_type does not set the unsigned bit in a
  505.          range type (I think it probably should copy it from the
  506.          target type), so we won't print values which are too large to
  507.          fit in a signed integer correctly.  */
  508.       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
  509.          print with the target type, though, because the size of our
  510.          type and the target type might differ).  */

  511.       /* FALLTHROUGH */

  512.     case TYPE_CODE_INT:
  513.       if (options->format || options->output_format)
  514.         {
  515.           struct value_print_options opts = *options;

  516.           opts.format = (options->format ? options->format
  517.                          : options->output_format);
  518.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  519.                                       original_value, &opts, 0, stream);
  520.         }
  521.       else
  522.         val_print_type_code_int (type, valaddr + embedded_offset, stream);
  523.       break;

  524.     case TYPE_CODE_CHAR:
  525.       if (options->format || options->output_format)
  526.         {
  527.           struct value_print_options opts = *options;

  528.           opts.format = (options->format ? options->format
  529.                          : options->output_format);
  530.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  531.                                       original_value, &opts, 0, stream);
  532.         }
  533.       else
  534.         {
  535.           val = unpack_long (type, valaddr + embedded_offset);
  536.           if (TYPE_UNSIGNED (type))
  537.             fprintf_filtered (stream, "%u", (unsigned int) val);
  538.           else
  539.             fprintf_filtered (stream, "%d", (int) val);
  540.           fputs_filtered (" ", stream);
  541.           LA_PRINT_CHAR (val, unresolved_type, stream);
  542.         }
  543.       break;

  544.     case TYPE_CODE_FLT:
  545.       if (options->format)
  546.         {
  547.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  548.                                       original_value, options, 0, stream);
  549.         }
  550.       else
  551.         {
  552.           print_floating (valaddr + embedded_offset, type, stream);
  553.         }
  554.       break;

  555.     case TYPE_CODE_DECFLOAT:
  556.       if (options->format)
  557.         val_print_scalar_formatted (type, valaddr, embedded_offset,
  558.                                     original_value, options, 0, stream);
  559.       else
  560.         print_decimal_floating (valaddr + embedded_offset,
  561.                                 type, stream);
  562.       break;

  563.     case TYPE_CODE_VOID:
  564.       fputs_filtered (decorations->void_name, stream);
  565.       break;

  566.     case TYPE_CODE_ERROR:
  567.       fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
  568.       break;

  569.     case TYPE_CODE_UNDEF:
  570.       /* This happens (without TYPE_FLAG_STUB set) on systems which
  571.          don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
  572.          "struct foo *bar" and no complete type for struct foo in that
  573.          file.  */
  574.       fprintf_filtered (stream, _("<incomplete type>"));
  575.       break;

  576.     case TYPE_CODE_COMPLEX:
  577.       fprintf_filtered (stream, "%s", decorations->complex_prefix);
  578.       if (options->format)
  579.         val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
  580.                                     valaddr, embedded_offset,
  581.                                     original_value, options, 0, stream);
  582.       else
  583.         print_floating (valaddr + embedded_offset,
  584.                         TYPE_TARGET_TYPE (type),
  585.                         stream);
  586.       fprintf_filtered (stream, "%s", decorations->complex_infix);
  587.       if (options->format)
  588.         val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
  589.                                     valaddr,
  590.                                     embedded_offset
  591.                                     + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
  592.                                     original_value,
  593.                                     options, 0, stream);
  594.       else
  595.         print_floating (valaddr + embedded_offset
  596.                         + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
  597.                         TYPE_TARGET_TYPE (type),
  598.                         stream);
  599.       fprintf_filtered (stream, "%s", decorations->complex_suffix);
  600.       break;

  601.     case TYPE_CODE_UNION:
  602.     case TYPE_CODE_STRUCT:
  603.     case TYPE_CODE_METHODPTR:
  604.     default:
  605.       error (_("Unhandled type code %d in symbol table."),
  606.              TYPE_CODE (type));
  607.     }
  608.   gdb_flush (stream);
  609. }

  610. /* Print using the given LANGUAGE the data of type TYPE located at
  611.    VALADDR + EMBEDDED_OFFSET (within GDB), which came from the
  612.    inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream
  613.    STREAM according to OPTIONS.  VAL is the whole object that came
  614.    from ADDRESS.  VALADDR must point to the head of VAL's contents
  615.    buffer.

  616.    The language printers will pass down an adjusted EMBEDDED_OFFSET to
  617.    further helper subroutines as subfields of TYPE are printed.  In
  618.    such cases, VALADDR is passed down unadjusted, as well as VAL, so
  619.    that VAL can be queried for metadata about the contents data being
  620.    printed, using EMBEDDED_OFFSET as an offset into VAL's contents
  621.    buffer.  For example: "has this field been optimized out", or "I'm
  622.    printing an object while inspecting a traceframe; has this
  623.    particular piece of data been collected?".

  624.    RECURSE indicates the amount of indentation to supply before
  625.    continuation lines; this amount is roughly twice the value of
  626.    RECURSE.  */

  627. void
  628. val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
  629.            CORE_ADDR address, struct ui_file *stream, int recurse,
  630.            const struct value *val,
  631.            const struct value_print_options *options,
  632.            const struct language_defn *language)
  633. {
  634.   volatile struct gdb_exception except;
  635.   int ret = 0;
  636.   struct value_print_options local_opts = *options;
  637.   struct type *real_type = check_typedef (type);

  638.   if (local_opts.prettyformat == Val_prettyformat_default)
  639.     local_opts.prettyformat = (local_opts.prettyformat_structs
  640.                                ? Val_prettyformat : Val_no_prettyformat);

  641.   QUIT;

  642.   /* Ensure that the type is complete and not just a stub.  If the type is
  643.      only a stub and we can't find and substitute its complete type, then
  644.      print appropriate string and return.  */

  645.   if (TYPE_STUB (real_type))
  646.     {
  647.       fprintf_filtered (stream, _("<incomplete type>"));
  648.       gdb_flush (stream);
  649.       return;
  650.     }

  651.   if (!valprint_check_validity (stream, real_type, embedded_offset, val))
  652.     return;

  653.   if (!options->raw)
  654.     {
  655.       ret = apply_ext_lang_val_pretty_printer (type, valaddr, embedded_offset,
  656.                                                address, stream, recurse,
  657.                                                val, options, language);
  658.       if (ret)
  659.         return;
  660.     }

  661.   /* Handle summary mode.  If the value is a scalar, print it;
  662.      otherwise, print an ellipsis.  */
  663.   if (options->summary && !val_print_scalar_type_p (type))
  664.     {
  665.       fprintf_filtered (stream, "...");
  666.       return;
  667.     }

  668.   TRY_CATCH (except, RETURN_MASK_ERROR)
  669.     {
  670.       language->la_val_print (type, valaddr, embedded_offset, address,
  671.                               stream, recurse, val,
  672.                               &local_opts);
  673.     }
  674.   if (except.reason < 0)
  675.     fprintf_filtered (stream, _("<error reading variable>"));
  676. }

  677. /* Check whether the value VAL is printable.  Return 1 if it is;
  678.    return 0 and print an appropriate error message to STREAM according to
  679.    OPTIONS if it is not.  */

  680. static int
  681. value_check_printable (struct value *val, struct ui_file *stream,
  682.                        const struct value_print_options *options)
  683. {
  684.   if (val == 0)
  685.     {
  686.       fprintf_filtered (stream, _("<address of value unknown>"));
  687.       return 0;
  688.     }

  689.   if (value_entirely_optimized_out (val))
  690.     {
  691.       if (options->summary && !val_print_scalar_type_p (value_type (val)))
  692.         fprintf_filtered (stream, "...");
  693.       else
  694.         val_print_optimized_out (val, stream);
  695.       return 0;
  696.     }

  697.   if (value_entirely_unavailable (val))
  698.     {
  699.       if (options->summary && !val_print_scalar_type_p (value_type (val)))
  700.         fprintf_filtered (stream, "...");
  701.       else
  702.         val_print_unavailable (stream);
  703.       return 0;
  704.     }

  705.   if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
  706.     {
  707.       fprintf_filtered (stream, _("<internal function %s>"),
  708.                         value_internal_function_name (val));
  709.       return 0;
  710.     }

  711.   return 1;
  712. }

  713. /* Print using the given LANGUAGE the value VAL onto stream STREAM according
  714.    to OPTIONS.

  715.    This is a preferable interface to val_print, above, because it uses
  716.    GDB's value mechanism.  */

  717. void
  718. common_val_print (struct value *val, struct ui_file *stream, int recurse,
  719.                   const struct value_print_options *options,
  720.                   const struct language_defn *language)
  721. {
  722.   if (!value_check_printable (val, stream, options))
  723.     return;

  724.   if (language->la_language == language_ada)
  725.     /* The value might have a dynamic type, which would cause trouble
  726.        below when trying to extract the value contents (since the value
  727.        size is determined from the type size which is unknown).  So
  728.        get a fixed representation of our value.  */
  729.     val = ada_to_fixed_value (val);

  730.   val_print (value_type (val), value_contents_for_printing (val),
  731.              value_embedded_offset (val), value_address (val),
  732.              stream, recurse,
  733.              val, options, language);
  734. }

  735. /* Print on stream STREAM the value VAL according to OPTIONS.  The value
  736.    is printed using the current_language syntax.  */

  737. void
  738. value_print (struct value *val, struct ui_file *stream,
  739.              const struct value_print_options *options)
  740. {
  741.   if (!value_check_printable (val, stream, options))
  742.     return;

  743.   if (!options->raw)
  744.     {
  745.       int r
  746.         = apply_ext_lang_val_pretty_printer (value_type (val),
  747.                                              value_contents_for_printing (val),
  748.                                              value_embedded_offset (val),
  749.                                              value_address (val),
  750.                                              stream, 0,
  751.                                              val, options, current_language);

  752.       if (r)
  753.         return;
  754.     }

  755.   LA_VALUE_PRINT (val, stream, options);
  756. }

  757. /* Called by various <lang>_val_print routines to print
  758.    TYPE_CODE_INT'sTYPE is the type.  VALADDR is the address of the
  759.    value.  STREAM is where to print the value.  */

  760. void
  761. val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
  762.                          struct ui_file *stream)
  763. {
  764.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));

  765.   if (TYPE_LENGTH (type) > sizeof (LONGEST))
  766.     {
  767.       LONGEST val;

  768.       if (TYPE_UNSIGNED (type)
  769.           && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
  770.                                             byte_order, &val))
  771.         {
  772.           print_longest (stream, 'u', 0, val);
  773.         }
  774.       else
  775.         {
  776.           /* Signed, or we couldn't turn an unsigned value into a
  777.              LONGEST.  For signed values, one could assume two's
  778.              complement (a reasonable assumption, I think) and do
  779.              better than this.  */
  780.           print_hex_chars (stream, (unsigned char *) valaddr,
  781.                            TYPE_LENGTH (type), byte_order);
  782.         }
  783.     }
  784.   else
  785.     {
  786.       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
  787.                      unpack_long (type, valaddr));
  788.     }
  789. }

  790. void
  791. val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
  792.                            struct ui_file *stream)
  793. {
  794.   ULONGEST val = unpack_long (type, valaddr);
  795.   int bitpos, nfields = TYPE_NFIELDS (type);

  796.   fputs_filtered ("[ ", stream);
  797.   for (bitpos = 0; bitpos < nfields; bitpos++)
  798.     {
  799.       if (TYPE_FIELD_BITPOS (type, bitpos) != -1
  800.           && (val & ((ULONGEST)1 << bitpos)))
  801.         {
  802.           if (TYPE_FIELD_NAME (type, bitpos))
  803.             fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
  804.           else
  805.             fprintf_filtered (stream, "#%d ", bitpos);
  806.         }
  807.     }
  808.   fputs_filtered ("]", stream);
  809. }

  810. /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
  811.    according to OPTIONS and SIZE on STREAM.  Format i is not supported
  812.    at this level.

  813.    This is how the elements of an array or structure are printed
  814.    with a format.  */

  815. void
  816. val_print_scalar_formatted (struct type *type,
  817.                             const gdb_byte *valaddr, int embedded_offset,
  818.                             const struct value *val,
  819.                             const struct value_print_options *options,
  820.                             int size,
  821.                             struct ui_file *stream)
  822. {
  823.   gdb_assert (val != NULL);
  824.   gdb_assert (valaddr == value_contents_for_printing_const (val));

  825.   /* If we get here with a string format, try again without it.  Go
  826.      all the way back to the language printers, which may call us
  827.      again.  */
  828.   if (options->format == 's')
  829.     {
  830.       struct value_print_options opts = *options;
  831.       opts.format = 0;
  832.       opts.deref_ref = 0;
  833.       val_print (type, valaddr, embedded_offset, 0, stream, 0, val, &opts,
  834.                  current_language);
  835.       return;
  836.     }

  837.   /* A scalar object that does not have all bits available can't be
  838.      printed, because all bits contribute to its representation.  */
  839.   if (value_bits_any_optimized_out (val,
  840.                                     TARGET_CHAR_BIT * embedded_offset,
  841.                                     TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  842.     val_print_optimized_out (val, stream);
  843.   else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
  844.     val_print_unavailable (stream);
  845.   else
  846.     print_scalar_formatted (valaddr + embedded_offset, type,
  847.                             options, size, stream);
  848. }

  849. /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
  850.    The raison d'etre of this function is to consolidate printing of
  851.    LONG_LONG's into this one function.  The format chars b,h,w,g are
  852.    from print_scalar_formatted().  Numbers are printed using C
  853.    format.

  854.    USE_C_FORMAT means to use C format in all cases.  Without it,
  855.    'o' and 'x' format do not include the standard C radix prefix
  856.    (leading 0 or 0x).

  857.    Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
  858.    and was intended to request formating according to the current
  859.    language and would be used for most integers that GDB prints.  The
  860.    exceptional cases were things like protocols where the format of
  861.    the integer is a protocol thing, not a user-visible thing).  The
  862.    parameter remains to preserve the information of what things might
  863.    be printed with language-specific format, should we ever resurrect
  864.    that capability.  */

  865. void
  866. print_longest (struct ui_file *stream, int format, int use_c_format,
  867.                LONGEST val_long)
  868. {
  869.   const char *val;

  870.   switch (format)
  871.     {
  872.     case 'd':
  873.       val = int_string (val_long, 10, 1, 0, 1); break;
  874.     case 'u':
  875.       val = int_string (val_long, 10, 0, 0, 1); break;
  876.     case 'x':
  877.       val = int_string (val_long, 16, 0, 0, use_c_format); break;
  878.     case 'b':
  879.       val = int_string (val_long, 16, 0, 2, 1); break;
  880.     case 'h':
  881.       val = int_string (val_long, 16, 0, 4, 1); break;
  882.     case 'w':
  883.       val = int_string (val_long, 16, 0, 8, 1); break;
  884.     case 'g':
  885.       val = int_string (val_long, 16, 0, 16, 1); break;
  886.       break;
  887.     case 'o':
  888.       val = int_string (val_long, 8, 0, 0, use_c_format); break;
  889.     default:
  890.       internal_error (__FILE__, __LINE__,
  891.                       _("failed internal consistency check"));
  892.     }
  893.   fputs_filtered (val, stream);
  894. }

  895. /* This used to be a macro, but I don't think it is called often enough
  896.    to merit such treatment.  */
  897. /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
  898.    arguments to a function, number in a value history, register number, etc.)
  899.    where the value must not be larger than can fit in an int.  */

  900. int
  901. longest_to_int (LONGEST arg)
  902. {
  903.   /* Let the compiler do the work.  */
  904.   int rtnval = (int) arg;

  905.   /* Check for overflows or underflows.  */
  906.   if (sizeof (LONGEST) > sizeof (int))
  907.     {
  908.       if (rtnval != arg)
  909.         {
  910.           error (_("Value out of range."));
  911.         }
  912.     }
  913.   return (rtnval);
  914. }

  915. /* Print a floating point value of type TYPE (not always a
  916.    TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM.  */

  917. void
  918. print_floating (const gdb_byte *valaddr, struct type *type,
  919.                 struct ui_file *stream)
  920. {
  921.   DOUBLEST doub;
  922.   int inv;
  923.   const struct floatformat *fmt = NULL;
  924.   unsigned len = TYPE_LENGTH (type);
  925.   enum float_kind kind;

  926.   /* If it is a floating-point, check for obvious problems.  */
  927.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  928.     fmt = floatformat_from_type (type);
  929.   if (fmt != NULL)
  930.     {
  931.       kind = floatformat_classify (fmt, valaddr);
  932.       if (kind == float_nan)
  933.         {
  934.           if (floatformat_is_negative (fmt, valaddr))
  935.             fprintf_filtered (stream, "-");
  936.           fprintf_filtered (stream, "nan(");
  937.           fputs_filtered ("0x", stream);
  938.           fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
  939.           fprintf_filtered (stream, ")");
  940.           return;
  941.         }
  942.       else if (kind == float_infinite)
  943.         {
  944.           if (floatformat_is_negative (fmt, valaddr))
  945.             fputs_filtered ("-", stream);
  946.           fputs_filtered ("inf", stream);
  947.           return;
  948.         }
  949.     }

  950.   /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
  951.      isn't necessarily a TYPE_CODE_FLT.  Consequently, unpack_double
  952.      needs to be used as that takes care of any necessary type
  953.      conversions.  Such conversions are of course direct to DOUBLEST
  954.      and disregard any possible target floating point limitations.
  955.      For instance, a u64 would be converted and displayed exactly on a
  956.      host with 80 bit DOUBLEST but with loss of information on a host
  957.      with 64 bit DOUBLEST.  */

  958.   doub = unpack_double (type, valaddr, &inv);
  959.   if (inv)
  960.     {
  961.       fprintf_filtered (stream, "<invalid float value>");
  962.       return;
  963.     }

  964.   /* FIXME: kettenis/2001-01-20: The following code makes too much
  965.      assumptions about the host and target floating point format.  */

  966.   /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
  967.      not necessarily be a TYPE_CODE_FLT, the below ignores that and
  968.      instead uses the type's length to determine the precision of the
  969.      floating-point value being printed.  */

  970.   if (len < sizeof (double))
  971.       fprintf_filtered (stream, "%.9g", (double) doub);
  972.   else if (len == sizeof (double))
  973.       fprintf_filtered (stream, "%.17g", (double) doub);
  974.   else
  975. #ifdef PRINTF_HAS_LONG_DOUBLE
  976.     fprintf_filtered (stream, "%.35Lg", doub);
  977. #else
  978.     /* This at least wins with values that are representable as
  979.        doubles.  */
  980.     fprintf_filtered (stream, "%.17g", (double) doub);
  981. #endif
  982. }

  983. void
  984. print_decimal_floating (const gdb_byte *valaddr, struct type *type,
  985.                         struct ui_file *stream)
  986. {
  987.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  988.   char decstr[MAX_DECIMAL_STRING];
  989.   unsigned len = TYPE_LENGTH (type);

  990.   decimal_to_string (valaddr, len, byte_order, decstr);
  991.   fputs_filtered (decstr, stream);
  992.   return;
  993. }

  994. void
  995. print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
  996.                     unsigned len, enum bfd_endian byte_order)
  997. {

  998. #define BITS_IN_BYTES 8

  999.   const gdb_byte *p;
  1000.   unsigned int i;
  1001.   int b;

  1002.   /* Declared "int" so it will be signed.
  1003.      This ensures that right shift will shift in zeros.  */

  1004.   const int mask = 0x080;

  1005.   /* FIXME: We should be not printing leading zeroes in most cases.  */

  1006.   if (byte_order == BFD_ENDIAN_BIG)
  1007.     {
  1008.       for (p = valaddr;
  1009.            p < valaddr + len;
  1010.            p++)
  1011.         {
  1012.           /* Every byte has 8 binary characters; peel off
  1013.              and print from the MSB end.  */

  1014.           for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
  1015.             {
  1016.               if (*p & (mask >> i))
  1017.                 b = 1;
  1018.               else
  1019.                 b = 0;

  1020.               fprintf_filtered (stream, "%1d", b);
  1021.             }
  1022.         }
  1023.     }
  1024.   else
  1025.     {
  1026.       for (p = valaddr + len - 1;
  1027.            p >= valaddr;
  1028.            p--)
  1029.         {
  1030.           for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
  1031.             {
  1032.               if (*p & (mask >> i))
  1033.                 b = 1;
  1034.               else
  1035.                 b = 0;

  1036.               fprintf_filtered (stream, "%1d", b);
  1037.             }
  1038.         }
  1039.     }
  1040. }

  1041. /* VALADDR points to an integer of LEN bytes.
  1042.    Print it in octal on stream or format it in buf.  */

  1043. void
  1044. print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
  1045.                    unsigned len, enum bfd_endian byte_order)
  1046. {
  1047.   const gdb_byte *p;
  1048.   unsigned char octa1, octa2, octa3, carry;
  1049.   int cycle;

  1050.   /* FIXME: We should be not printing leading zeroes in most cases.  */


  1051.   /* Octal is 3 bits, which doesn't fit.  Yuk.  So we have to track
  1052.    * the extra bits, which cycle every three bytes:
  1053.    *
  1054.    * Byte side:       0            1             2          3
  1055.    *                         |             |            |            |
  1056.    * bit number   123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
  1057.    *
  1058.    * Octal side:   0   1   carry  3   4  carry ...
  1059.    *
  1060.    * Cycle number:    0             1            2
  1061.    *
  1062.    * But of course we are printing from the high side, so we have to
  1063.    * figure out where in the cycle we are so that we end up with no
  1064.    * left over bits at the end.
  1065.    */
  1066. #define BITS_IN_OCTAL 3
  1067. #define HIGH_ZERO     0340
  1068. #define LOW_ZERO      0016
  1069. #define CARRY_ZERO    0003
  1070. #define HIGH_ONE      0200
  1071. #define MID_ONE       0160
  1072. #define LOW_ONE       0016
  1073. #define CARRY_ONE     0001
  1074. #define HIGH_TWO      0300
  1075. #define MID_TWO       0070
  1076. #define LOW_TWO       0007

  1077.   /* For 32 we start in cycle 2, with two bits and one bit carry;
  1078.      for 64 in cycle in cycle 1, with one bit and a two bit carry.  */

  1079.   cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
  1080.   carry = 0;

  1081.   fputs_filtered ("0", stream);
  1082.   if (byte_order == BFD_ENDIAN_BIG)
  1083.     {
  1084.       for (p = valaddr;
  1085.            p < valaddr + len;
  1086.            p++)
  1087.         {
  1088.           switch (cycle)
  1089.             {
  1090.             case 0:
  1091.               /* No carry in, carry out two bits.  */

  1092.               octa1 = (HIGH_ZERO & *p) >> 5;
  1093.               octa2 = (LOW_ZERO & *p) >> 2;
  1094.               carry = (CARRY_ZERO & *p);
  1095.               fprintf_filtered (stream, "%o", octa1);
  1096.               fprintf_filtered (stream, "%o", octa2);
  1097.               break;

  1098.             case 1:
  1099.               /* Carry in two bits, carry out one bit.  */

  1100.               octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
  1101.               octa2 = (MID_ONE & *p) >> 4;
  1102.               octa3 = (LOW_ONE & *p) >> 1;
  1103.               carry = (CARRY_ONE & *p);
  1104.               fprintf_filtered (stream, "%o", octa1);
  1105.               fprintf_filtered (stream, "%o", octa2);
  1106.               fprintf_filtered (stream, "%o", octa3);
  1107.               break;

  1108.             case 2:
  1109.               /* Carry in one bit, no carry out.  */

  1110.               octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
  1111.               octa2 = (MID_TWO & *p) >> 3;
  1112.               octa3 = (LOW_TWO & *p);
  1113.               carry = 0;
  1114.               fprintf_filtered (stream, "%o", octa1);
  1115.               fprintf_filtered (stream, "%o", octa2);
  1116.               fprintf_filtered (stream, "%o", octa3);
  1117.               break;

  1118.             default:
  1119.               error (_("Internal error in octal conversion;"));
  1120.             }

  1121.           cycle++;
  1122.           cycle = cycle % BITS_IN_OCTAL;
  1123.         }
  1124.     }
  1125.   else
  1126.     {
  1127.       for (p = valaddr + len - 1;
  1128.            p >= valaddr;
  1129.            p--)
  1130.         {
  1131.           switch (cycle)
  1132.             {
  1133.             case 0:
  1134.               /* Carry out, no carry in */

  1135.               octa1 = (HIGH_ZERO & *p) >> 5;
  1136.               octa2 = (LOW_ZERO & *p) >> 2;
  1137.               carry = (CARRY_ZERO & *p);
  1138.               fprintf_filtered (stream, "%o", octa1);
  1139.               fprintf_filtered (stream, "%o", octa2);
  1140.               break;

  1141.             case 1:
  1142.               /* Carry in, carry out */

  1143.               octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
  1144.               octa2 = (MID_ONE & *p) >> 4;
  1145.               octa3 = (LOW_ONE & *p) >> 1;
  1146.               carry = (CARRY_ONE & *p);
  1147.               fprintf_filtered (stream, "%o", octa1);
  1148.               fprintf_filtered (stream, "%o", octa2);
  1149.               fprintf_filtered (stream, "%o", octa3);
  1150.               break;

  1151.             case 2:
  1152.               /* Carry in, no carry out */

  1153.               octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
  1154.               octa2 = (MID_TWO & *p) >> 3;
  1155.               octa3 = (LOW_TWO & *p);
  1156.               carry = 0;
  1157.               fprintf_filtered (stream, "%o", octa1);
  1158.               fprintf_filtered (stream, "%o", octa2);
  1159.               fprintf_filtered (stream, "%o", octa3);
  1160.               break;

  1161.             default:
  1162.               error (_("Internal error in octal conversion;"));
  1163.             }

  1164.           cycle++;
  1165.           cycle = cycle % BITS_IN_OCTAL;
  1166.         }
  1167.     }

  1168. }

  1169. /* VALADDR points to an integer of LEN bytes.
  1170.    Print it in decimal on stream or format it in buf.  */

  1171. void
  1172. print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
  1173.                      unsigned len, enum bfd_endian byte_order)
  1174. {
  1175. #define TEN             10
  1176. #define CARRY_OUTx ) ((x) / TEN)        /* extend char to int */
  1177. #define CARRY_LEFT( x ) ((x) % TEN)
  1178. #define SHIFT( x )      ((x) << 4)
  1179. #define LOW_NIBBLEx ) ( (x) & 0x00F)
  1180. #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)

  1181.   const gdb_byte *p;
  1182.   unsigned char *digits;
  1183.   int carry;
  1184.   int decimal_len;
  1185.   int i, j, decimal_digits;
  1186.   int dummy;
  1187.   int flip;

  1188.   /* Base-ten number is less than twice as many digits
  1189.      as the base 16 number, which is 2 digits per byte.  */

  1190.   decimal_len = len * 2 * 2;
  1191.   digits = xmalloc (decimal_len);

  1192.   for (i = 0; i < decimal_len; i++)
  1193.     {
  1194.       digits[i] = 0;
  1195.     }

  1196.   /* Ok, we have an unknown number of bytes of data to be printed in
  1197.    * decimal.
  1198.    *
  1199.    * Given a hex number (in nibbles) as XYZ, we start by taking X and
  1200.    * decemalizing it as "x1 x2" in two decimal nibbles.  Then we multiply
  1201.    * the nibbles by 16, add Y and re-decimalize.  Repeat with Z.
  1202.    *
  1203.    * The trick is that "digits" holds a base-10 number, but sometimes
  1204.    * the individual digits are > 10.
  1205.    *
  1206.    * Outer loop is per nibble (hex digit) of input, from MSD end to
  1207.    * LSD end.
  1208.    */
  1209.   decimal_digits = 0;                /* Number of decimal digits so far */
  1210.   p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
  1211.   flip = 0;
  1212.   while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
  1213.     {
  1214.       /*
  1215.        * Multiply current base-ten number by 16 in place.
  1216.        * Each digit was between 0 and 9, now is between
  1217.        * 0 and 144.
  1218.        */
  1219.       for (j = 0; j < decimal_digits; j++)
  1220.         {
  1221.           digits[j] = SHIFT (digits[j]);
  1222.         }

  1223.       /* Take the next nibble off the input and add it to what
  1224.        * we've got in the LSB position.  Bottom 'digit' is now
  1225.        * between 0 and 159.
  1226.        *
  1227.        * "flip" is used to run this loop twice for each byte.
  1228.        */
  1229.       if (flip == 0)
  1230.         {
  1231.           /* Take top nibble.  */

  1232.           digits[0] += HIGH_NIBBLE (*p);
  1233.           flip = 1;
  1234.         }
  1235.       else
  1236.         {
  1237.           /* Take low nibble and bump our pointer "p".  */

  1238.           digits[0] += LOW_NIBBLE (*p);
  1239.           if (byte_order == BFD_ENDIAN_BIG)
  1240.             p++;
  1241.           else
  1242.             p--;
  1243.           flip = 0;
  1244.         }

  1245.       /* Re-decimalize.  We have to do this often enough
  1246.        * that we don't overflow, but once per nibble is
  1247.        * overkill.  Easier this way, though.  Note that the
  1248.        * carry is often larger than 10 (e.g. max initial
  1249.        * carry out of lowest nibble is 15, could bubble all
  1250.        * the way up greater than 10).  So we have to do
  1251.        * the carrying beyond the last current digit.
  1252.        */
  1253.       carry = 0;
  1254.       for (j = 0; j < decimal_len - 1; j++)
  1255.         {
  1256.           digits[j] += carry;

  1257.           /* "/" won't handle an unsigned char with
  1258.            * a value that if signed would be negative.
  1259.            * So extend to longword int via "dummy".
  1260.            */
  1261.           dummy = digits[j];
  1262.           carry = CARRY_OUT (dummy);
  1263.           digits[j] = CARRY_LEFT (dummy);

  1264.           if (j >= decimal_digits && carry == 0)
  1265.             {
  1266.               /*
  1267.                * All higher digits are 0 and we
  1268.                * no longer have a carry.
  1269.                *
  1270.                * Note: "j" is 0-based, "decimal_digits" is
  1271.                *       1-based.
  1272.                */
  1273.               decimal_digits = j + 1;
  1274.               break;
  1275.             }
  1276.         }
  1277.     }

  1278.   /* Ok, now "digits" is the decimal representation, with
  1279.      the "decimal_digits" actual digits.  Print!  */

  1280.   for (i = decimal_digits - 1; i >= 0; i--)
  1281.     {
  1282.       fprintf_filtered (stream, "%1d", digits[i]);
  1283.     }
  1284.   xfree (digits);
  1285. }

  1286. /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */

  1287. void
  1288. print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
  1289.                  unsigned len, enum bfd_endian byte_order)
  1290. {
  1291.   const gdb_byte *p;

  1292.   /* FIXME: We should be not printing leading zeroes in most cases.  */

  1293.   fputs_filtered ("0x", stream);
  1294.   if (byte_order == BFD_ENDIAN_BIG)
  1295.     {
  1296.       for (p = valaddr;
  1297.            p < valaddr + len;
  1298.            p++)
  1299.         {
  1300.           fprintf_filtered (stream, "%02x", *p);
  1301.         }
  1302.     }
  1303.   else
  1304.     {
  1305.       for (p = valaddr + len - 1;
  1306.            p >= valaddr;
  1307.            p--)
  1308.         {
  1309.           fprintf_filtered (stream, "%02x", *p);
  1310.         }
  1311.     }
  1312. }

  1313. /* VALADDR points to a char integer of LEN bytes.
  1314.    Print it out in appropriate language form on stream.
  1315.    Omit any leading zero chars.  */

  1316. void
  1317. print_char_chars (struct ui_file *stream, struct type *type,
  1318.                   const gdb_byte *valaddr,
  1319.                   unsigned len, enum bfd_endian byte_order)
  1320. {
  1321.   const gdb_byte *p;

  1322.   if (byte_order == BFD_ENDIAN_BIG)
  1323.     {
  1324.       p = valaddr;
  1325.       while (p < valaddr + len - 1 && *p == 0)
  1326.         ++p;

  1327.       while (p < valaddr + len)
  1328.         {
  1329.           LA_EMIT_CHAR (*p, type, stream, '\'');
  1330.           ++p;
  1331.         }
  1332.     }
  1333.   else
  1334.     {
  1335.       p = valaddr + len - 1;
  1336.       while (p > valaddr && *p == 0)
  1337.         --p;

  1338.       while (p >= valaddr)
  1339.         {
  1340.           LA_EMIT_CHAR (*p, type, stream, '\'');
  1341.           --p;
  1342.         }
  1343.     }
  1344. }

  1345. /* Print function pointer with inferior address ADDRESS onto stdio
  1346.    stream STREAM.  */

  1347. void
  1348. print_function_pointer_address (const struct value_print_options *options,
  1349.                                 struct gdbarch *gdbarch,
  1350.                                 CORE_ADDR address,
  1351.                                 struct ui_file *stream)
  1352. {
  1353.   CORE_ADDR func_addr
  1354.     = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
  1355.                                           &current_target);

  1356.   /* If the function pointer is represented by a description, print
  1357.      the address of the description.  */
  1358.   if (options->addressprint && func_addr != address)
  1359.     {
  1360.       fputs_filtered ("@", stream);
  1361.       fputs_filtered (paddress (gdbarch, address), stream);
  1362.       fputs_filtered (": ", stream);
  1363.     }
  1364.   print_address_demangle (options, gdbarch, func_addr, stream, demangle);
  1365. }


  1366. /* Print on STREAM using the given OPTIONS the index for the element
  1367.    at INDEX of an array whose index type is INDEX_TYPE.  */

  1368. void
  1369. maybe_print_array_index (struct type *index_type, LONGEST index,
  1370.                          struct ui_file *stream,
  1371.                          const struct value_print_options *options)
  1372. {
  1373.   struct value *index_value;

  1374.   if (!options->print_array_indexes)
  1375.     return;

  1376.   index_value = value_from_longest (index_type, index);

  1377.   LA_PRINT_ARRAY_INDEX (index_value, stream, options);
  1378. }

  1379. /*  Called by various <lang>_val_print routines to print elements of an
  1380.    array in the form "<elem1>, <elem2>, <elem3>, ...".

  1381.    (FIXME?)  Assumes array element separator is a comma, which is correct
  1382.    for all languages currently handled.
  1383.    (FIXME?)  Some languages have a notation for repeated array elements,
  1384.    perhaps we should try to use that notation when appropriate.  */

  1385. void
  1386. val_print_array_elements (struct type *type,
  1387.                           const gdb_byte *valaddr, int embedded_offset,
  1388.                           CORE_ADDR address, struct ui_file *stream,
  1389.                           int recurse,
  1390.                           const struct value *val,
  1391.                           const struct value_print_options *options,
  1392.                           unsigned int i)
  1393. {
  1394.   unsigned int things_printed = 0;
  1395.   unsigned len;
  1396.   struct type *elttype, *index_type;
  1397.   unsigned eltlen;
  1398.   /* Position of the array element we are examining to see
  1399.      whether it is repeated.  */
  1400.   unsigned int rep1;
  1401.   /* Number of repetitions we have detected so far.  */
  1402.   unsigned int reps;
  1403.   LONGEST low_bound, high_bound;

  1404.   elttype = TYPE_TARGET_TYPE (type);
  1405.   eltlen = TYPE_LENGTH (check_typedef (elttype));
  1406.   index_type = TYPE_INDEX_TYPE (type);

  1407.   if (get_array_bounds (type, &low_bound, &high_bound))
  1408.     {
  1409.       /* The array length should normally be HIGH_BOUND - LOW_BOUND + 1.
  1410.          But we have to be a little extra careful, because some languages
  1411.          such as Ada allow LOW_BOUND to be greater than HIGH_BOUND for
  1412.          empty arrays.  In that situation, the array length is just zero,
  1413.          not negative!  */
  1414.       if (low_bound > high_bound)
  1415.         len = 0;
  1416.       else
  1417.         len = high_bound - low_bound + 1;
  1418.     }
  1419.   else
  1420.     {
  1421.       warning (_("unable to get bounds of array, assuming null array"));
  1422.       low_bound = 0;
  1423.       len = 0;
  1424.     }

  1425.   annotate_array_section_begin (i, elttype);

  1426.   for (; i < len && things_printed < options->print_max; i++)
  1427.     {
  1428.       if (i != 0)
  1429.         {
  1430.           if (options->prettyformat_arrays)
  1431.             {
  1432.               fprintf_filtered (stream, ",\n");
  1433.               print_spaces_filtered (2 + 2 * recurse, stream);
  1434.             }
  1435.           else
  1436.             {
  1437.               fprintf_filtered (stream, ", ");
  1438.             }
  1439.         }
  1440.       wrap_here (n_spaces (2 + 2 * recurse));
  1441.       maybe_print_array_index (index_type, i + low_bound,
  1442.                                stream, options);

  1443.       rep1 = i + 1;
  1444.       reps = 1;
  1445.       /* Only check for reps if repeat_count_threshold is not set to
  1446.          UINT_MAX (unlimited).  */
  1447.       if (options->repeat_count_threshold < UINT_MAX)
  1448.         {
  1449.           while (rep1 < len
  1450.                  && value_contents_eq (val,
  1451.                                        embedded_offset + i * eltlen,
  1452.                                        val,
  1453.                                        (embedded_offset
  1454.                                         + rep1 * eltlen),
  1455.                                        eltlen))
  1456.             {
  1457.               ++reps;
  1458.               ++rep1;
  1459.             }
  1460.         }

  1461.       if (reps > options->repeat_count_threshold)
  1462.         {
  1463.           val_print (elttype, valaddr, embedded_offset + i * eltlen,
  1464.                      address, stream, recurse + 1, val, options,
  1465.                      current_language);
  1466.           annotate_elt_rep (reps);
  1467.           fprintf_filtered (stream, " <repeats %u times>", reps);
  1468.           annotate_elt_rep_end ();

  1469.           i = rep1 - 1;
  1470.           things_printed += options->repeat_count_threshold;
  1471.         }
  1472.       else
  1473.         {
  1474.           val_print (elttype, valaddr, embedded_offset + i * eltlen,
  1475.                      address,
  1476.                      stream, recurse + 1, val, options, current_language);
  1477.           annotate_elt ();
  1478.           things_printed++;
  1479.         }
  1480.     }
  1481.   annotate_array_section_end ();
  1482.   if (i < len)
  1483.     {
  1484.       fprintf_filtered (stream, "...");
  1485.     }
  1486. }

  1487. /* Read LEN bytes of target memory at address MEMADDR, placing the
  1488.    results in GDB's memory at MYADDR.  Returns a count of the bytes
  1489.    actually read, and optionally a target_xfer_status value in the
  1490.    location pointed to by ERRPTR if ERRPTR is non-null.  */

  1491. /* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
  1492.    function be eliminated.  */

  1493. static int
  1494. partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
  1495.                      int len, int *errptr)
  1496. {
  1497.   int nread;                        /* Number of bytes actually read.  */
  1498.   int errcode;                        /* Error from last read.  */

  1499.   /* First try a complete read.  */
  1500.   errcode = target_read_memory (memaddr, myaddr, len);
  1501.   if (errcode == 0)
  1502.     {
  1503.       /* Got it all.  */
  1504.       nread = len;
  1505.     }
  1506.   else
  1507.     {
  1508.       /* Loop, reading one byte at a time until we get as much as we can.  */
  1509.       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
  1510.         {
  1511.           errcode = target_read_memory (memaddr++, myaddr++, 1);
  1512.         }
  1513.       /* If an error, the last read was unsuccessful, so adjust count.  */
  1514.       if (errcode != 0)
  1515.         {
  1516.           nread--;
  1517.         }
  1518.     }
  1519.   if (errptr != NULL)
  1520.     {
  1521.       *errptr = errcode;
  1522.     }
  1523.   return (nread);
  1524. }

  1525. /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
  1526.    each.  Fetch at most FETCHLIMIT characters.  BUFFER will be set to a newly
  1527.    allocated buffer containing the string, which the caller is responsible to
  1528.    free, and BYTES_READ will be set to the number of bytes read.  Returns 0 on
  1529.    success, or a target_xfer_status on failure.

  1530.    If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
  1531.    (including eventual NULs in the middle or end of the string).

  1532.    If LEN is -1, stops at the first null character (not necessarily
  1533.    the first null byte) up to a maximum of FETCHLIMIT characters.  Set
  1534.    FETCHLIMIT to UINT_MAX to read as many characters as possible from
  1535.    the string.

  1536.    Unless an exception is thrown, BUFFER will always be allocated, even on
  1537.    failure.  In this case, some characters might have been read before the
  1538.    failure happened.  Check BYTES_READ to recognize this situation.

  1539.    Note: There was a FIXME asking to make this code use target_read_string,
  1540.    but this function is more general (can read past null characters, up to
  1541.    given LEN).  Besides, it is used much more often than target_read_string
  1542.    so it is more tested.  Perhaps callers of target_read_string should use
  1543.    this function instead?  */

  1544. int
  1545. read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
  1546.              enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
  1547. {
  1548.   int errcode;                        /* Errno returned from bad reads.  */
  1549.   unsigned int nfetch;                /* Chars to fetch / chars fetched.  */
  1550.   gdb_byte *bufptr;                /* Pointer to next available byte in
  1551.                                    buffer.  */
  1552.   struct cleanup *old_chain = NULL;        /* Top of the old cleanup chain.  */

  1553.   /* Loop until we either have all the characters, or we encounter
  1554.      some error, such as bumping into the end of the address space.  */

  1555.   *buffer = NULL;

  1556.   old_chain = make_cleanup (free_current_contents, buffer);

  1557.   if (len > 0)
  1558.     {
  1559.       /* We want fetchlimit chars, so we might as well read them all in
  1560.          one operation.  */
  1561.       unsigned int fetchlen = min (len, fetchlimit);

  1562.       *buffer = (gdb_byte *) xmalloc (fetchlen * width);
  1563.       bufptr = *buffer;

  1564.       nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode)
  1565.         / width;
  1566.       addr += nfetch * width;
  1567.       bufptr += nfetch * width;
  1568.     }
  1569.   else if (len == -1)
  1570.     {
  1571.       unsigned long bufsize = 0;
  1572.       unsigned int chunksize;        /* Size of each fetch, in chars.  */
  1573.       int found_nul;                /* Non-zero if we found the nul char.  */
  1574.       gdb_byte *limit;                /* First location past end of fetch buffer.  */

  1575.       found_nul = 0;
  1576.       /* We are looking for a NUL terminator to end the fetching, so we
  1577.          might as well read in blocks that are large enough to be efficient,
  1578.          but not so large as to be slow if fetchlimit happens to be large.
  1579.          So we choose the minimum of 8 and fetchlimit.  We used to use 200
  1580.          instead of 8 but 200 is way too big for remote debugging over a
  1581.           serial line.  */
  1582.       chunksize = min (8, fetchlimit);

  1583.       do
  1584.         {
  1585.           QUIT;
  1586.           nfetch = min (chunksize, fetchlimit - bufsize);

  1587.           if (*buffer == NULL)
  1588.             *buffer = (gdb_byte *) xmalloc (nfetch * width);
  1589.           else
  1590.             *buffer = (gdb_byte *) xrealloc (*buffer,
  1591.                                              (nfetch + bufsize) * width);

  1592.           bufptr = *buffer + bufsize * width;
  1593.           bufsize += nfetch;

  1594.           /* Read as much as we can.  */
  1595.           nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
  1596.                     / width;

  1597.           /* Scan this chunk for the null character that terminates the string
  1598.              to print.  If found, we don't need to fetch any more.  Note
  1599.              that bufptr is explicitly left pointing at the next character
  1600.              after the null character, or at the next character after the end
  1601.              of the buffer.  */

  1602.           limit = bufptr + nfetch * width;
  1603.           while (bufptr < limit)
  1604.             {
  1605.               unsigned long c;

  1606.               c = extract_unsigned_integer (bufptr, width, byte_order);
  1607.               addr += width;
  1608.               bufptr += width;
  1609.               if (c == 0)
  1610.                 {
  1611.                   /* We don't care about any error which happened after
  1612.                      the NUL terminator.  */
  1613.                   errcode = 0;
  1614.                   found_nul = 1;
  1615.                   break;
  1616.                 }
  1617.             }
  1618.         }
  1619.       while (errcode == 0        /* no error */
  1620.              && bufptr - *buffer < fetchlimit * width        /* no overrun */
  1621.              && !found_nul);        /* haven't found NUL yet */
  1622.     }
  1623.   else
  1624.     {                                /* Length of string is really 0!  */
  1625.       /* We always allocate *buffer.  */
  1626.       *buffer = bufptr = xmalloc (1);
  1627.       errcode = 0;
  1628.     }

  1629.   /* bufptr and addr now point immediately beyond the last byte which we
  1630.      consider part of the string (including a '\0' which ends the string).  */
  1631.   *bytes_read = bufptr - *buffer;

  1632.   QUIT;

  1633.   discard_cleanups (old_chain);

  1634.   return errcode;
  1635. }

  1636. /* Return true if print_wchar can display W without resorting to a
  1637.    numeric escape, false otherwise.  */

  1638. static int
  1639. wchar_printable (gdb_wchar_t w)
  1640. {
  1641.   return (gdb_iswprint (w)
  1642.           || w == LCST ('\a') || w == LCST ('\b')
  1643.           || w == LCST ('\f') || w == LCST ('\n')
  1644.           || w == LCST ('\r') || w == LCST ('\t')
  1645.           || w == LCST ('\v'));
  1646. }

  1647. /* A helper function that converts the contents of STRING to wide
  1648.    characters and then appends them to OUTPUT.  */

  1649. static void
  1650. append_string_as_wide (const char *string,
  1651.                        struct obstack *output)
  1652. {
  1653.   for (; *string; ++string)
  1654.     {
  1655.       gdb_wchar_t w = gdb_btowc (*string);
  1656.       obstack_grow (output, &w, sizeof (gdb_wchar_t));
  1657.     }
  1658. }

  1659. /* Print a wide character W to OUTPUT.  ORIG is a pointer to the
  1660.    original (target) bytes representing the character, ORIG_LEN is the
  1661.    number of valid bytes.  WIDTH is the number of bytes in a base
  1662.    characters of the type.  OUTPUT is an obstack to which wide
  1663.    characters are emitted.  QUOTER is a (narrow) character indicating
  1664.    the style of quotes surrounding the character to be printed.
  1665.    NEED_ESCAPE is an in/out flag which is used to track numeric
  1666.    escapes across calls.  */

  1667. static void
  1668. print_wchar (gdb_wint_t w, const gdb_byte *orig,
  1669.              int orig_len, int width,
  1670.              enum bfd_endian byte_order,
  1671.              struct obstack *output,
  1672.              int quoter, int *need_escapep)
  1673. {
  1674.   int need_escape = *need_escapep;

  1675.   *need_escapep = 0;

  1676.   /* iswprint implementation on Windows returns 1 for tab character.
  1677.      In order to avoid different printout on this host, we explicitly
  1678.      use wchar_printable function.  */
  1679.   switch (w)
  1680.     {
  1681.       case LCST ('\a'):
  1682.         obstack_grow_wstr (output, LCST ("\\a"));
  1683.         break;
  1684.       case LCST ('\b'):
  1685.         obstack_grow_wstr (output, LCST ("\\b"));
  1686.         break;
  1687.       case LCST ('\f'):
  1688.         obstack_grow_wstr (output, LCST ("\\f"));
  1689.         break;
  1690.       case LCST ('\n'):
  1691.         obstack_grow_wstr (output, LCST ("\\n"));
  1692.         break;
  1693.       case LCST ('\r'):
  1694.         obstack_grow_wstr (output, LCST ("\\r"));
  1695.         break;
  1696.       case LCST ('\t'):
  1697.         obstack_grow_wstr (output, LCST ("\\t"));
  1698.         break;
  1699.       case LCST ('\v'):
  1700.         obstack_grow_wstr (output, LCST ("\\v"));
  1701.         break;
  1702.       default:
  1703.         {
  1704.           if (wchar_printable (w) && (!need_escape || (!gdb_iswdigit (w)
  1705.                                                        && w != LCST ('8')
  1706.                                                        && w != LCST ('9'))))
  1707.             {
  1708.               gdb_wchar_t wchar = w;

  1709.               if (w == gdb_btowc (quoter) || w == LCST ('\\'))
  1710.                 obstack_grow_wstr (output, LCST ("\\"));
  1711.               obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
  1712.             }
  1713.           else
  1714.             {
  1715.               int i;

  1716.               for (i = 0; i + width <= orig_len; i += width)
  1717.                 {
  1718.                   char octal[30];
  1719.                   ULONGEST value;

  1720.                   value = extract_unsigned_integer (&orig[i], width,
  1721.                                                   byte_order);
  1722.                   /* If the value fits in 3 octal digits, print it that
  1723.                      way.  Otherwise, print it as a hex escape.  */
  1724.                   if (value <= 0777)
  1725.                     xsnprintf (octal, sizeof (octal), "\\%.3o",
  1726.                                (int) (value & 0777));
  1727.                   else
  1728.                     xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
  1729.                   append_string_as_wide (octal, output);
  1730.                 }
  1731.               /* If we somehow have extra bytes, print them now.  */
  1732.               while (i < orig_len)
  1733.                 {
  1734.                   char octal[5];

  1735.                   xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
  1736.                   append_string_as_wide (octal, output);
  1737.                   ++i;
  1738.                 }

  1739.               *need_escapep = 1;
  1740.             }
  1741.           break;
  1742.         }
  1743.     }
  1744. }

  1745. /* Print the character C on STREAM as part of the contents of a
  1746.    literal string whose delimiter is QUOTER.  ENCODING names the
  1747.    encoding of C.  */

  1748. void
  1749. generic_emit_char (int c, struct type *type, struct ui_file *stream,
  1750.                    int quoter, const char *encoding)
  1751. {
  1752.   enum bfd_endian byte_order
  1753.     = gdbarch_byte_order (get_type_arch (type));
  1754.   struct obstack wchar_buf, output;
  1755.   struct cleanup *cleanups;
  1756.   gdb_byte *buf;
  1757.   struct wchar_iterator *iter;
  1758.   int need_escape = 0;

  1759.   buf = alloca (TYPE_LENGTH (type));
  1760.   pack_long (buf, type, c);

  1761.   iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
  1762.                               encoding, TYPE_LENGTH (type));
  1763.   cleanups = make_cleanup_wchar_iterator (iter);

  1764.   /* This holds the printable form of the wchar_t data.  */
  1765.   obstack_init (&wchar_buf);
  1766.   make_cleanup_obstack_free (&wchar_buf);

  1767.   while (1)
  1768.     {
  1769.       int num_chars;
  1770.       gdb_wchar_t *chars;
  1771.       const gdb_byte *buf;
  1772.       size_t buflen;
  1773.       int print_escape = 1;
  1774.       enum wchar_iterate_result result;

  1775.       num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
  1776.       if (num_chars < 0)
  1777.         break;
  1778.       if (num_chars > 0)
  1779.         {
  1780.           /* If all characters are printable, print them.  Otherwise,
  1781.              we're going to have to print an escape sequence.  We
  1782.              check all characters because we want to print the target
  1783.              bytes in the escape sequence, and we don't know character
  1784.              boundaries there.  */
  1785.           int i;

  1786.           print_escape = 0;
  1787.           for (i = 0; i < num_chars; ++i)
  1788.             if (!wchar_printable (chars[i]))
  1789.               {
  1790.                 print_escape = 1;
  1791.                 break;
  1792.               }

  1793.           if (!print_escape)
  1794.             {
  1795.               for (i = 0; i < num_chars; ++i)
  1796.                 print_wchar (chars[i], buf, buflen,
  1797.                              TYPE_LENGTH (type), byte_order,
  1798.                              &wchar_buf, quoter, &need_escape);
  1799.             }
  1800.         }

  1801.       /* This handles the NUM_CHARS == 0 case as well.  */
  1802.       if (print_escape)
  1803.         print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
  1804.                      byte_order, &wchar_buf, quoter, &need_escape);
  1805.     }

  1806.   /* The output in the host encoding.  */
  1807.   obstack_init (&output);
  1808.   make_cleanup_obstack_free (&output);

  1809.   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
  1810.                              (gdb_byte *) obstack_base (&wchar_buf),
  1811.                              obstack_object_size (&wchar_buf),
  1812.                              sizeof (gdb_wchar_t), &output, translit_char);
  1813.   obstack_1grow (&output, '\0');

  1814.   fputs_filtered (obstack_base (&output), stream);

  1815.   do_cleanups (cleanups);
  1816. }

  1817. /* Return the repeat count of the next character/byte in ITER,
  1818.    storing the result in VEC.  */

  1819. static int
  1820. count_next_character (struct wchar_iterator *iter,
  1821.                       VEC (converted_character_d) **vec)
  1822. {
  1823.   struct converted_character *current;

  1824.   if (VEC_empty (converted_character_d, *vec))
  1825.     {
  1826.       struct converted_character tmp;
  1827.       gdb_wchar_t *chars;

  1828.       tmp.num_chars
  1829.         = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
  1830.       if (tmp.num_chars > 0)
  1831.         {
  1832.           gdb_assert (tmp.num_chars < MAX_WCHARS);
  1833.           memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
  1834.         }
  1835.       VEC_safe_push (converted_character_d, *vec, &tmp);
  1836.     }

  1837.   current = VEC_last (converted_character_d, *vec);

  1838.   /* Count repeated characters or bytes.  */
  1839.   current->repeat_count = 1;
  1840.   if (current->num_chars == -1)
  1841.     {
  1842.       /* EOF  */
  1843.       return -1;
  1844.     }
  1845.   else
  1846.     {
  1847.       gdb_wchar_t *chars;
  1848.       struct converted_character d;
  1849.       int repeat;

  1850.       d.repeat_count = 0;

  1851.       while (1)
  1852.         {
  1853.           /* Get the next character.  */
  1854.           d.num_chars
  1855.             = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);

  1856.           /* If a character was successfully converted, save the character
  1857.              into the converted character.  */
  1858.           if (d.num_chars > 0)
  1859.             {
  1860.               gdb_assert (d.num_chars < MAX_WCHARS);
  1861.               memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
  1862.             }

  1863.           /* Determine if the current character is the same as this
  1864.              new character.  */
  1865.           if (d.num_chars == current->num_chars && d.result == current->result)
  1866.             {
  1867.               /* There are two cases to consider:

  1868.                  1) Equality of converted character (num_chars > 0)
  1869.                  2) Equality of non-converted character (num_chars == 0)  */
  1870.               if ((current->num_chars > 0
  1871.                    && memcmp (current->chars, d.chars,
  1872.                               WCHAR_BUFLEN (current->num_chars)) == 0)
  1873.                   || (current->num_chars == 0
  1874.                       && current->buflen == d.buflen
  1875.                       && memcmp (current->buf, d.buf, current->buflen) == 0))
  1876.                 ++current->repeat_count;
  1877.               else
  1878.                 break;
  1879.             }
  1880.           else
  1881.             break;
  1882.         }

  1883.       /* Push this next converted character onto the result vector.  */
  1884.       repeat = current->repeat_count;
  1885.       VEC_safe_push (converted_character_d, *vec, &d);
  1886.       return repeat;
  1887.     }
  1888. }

  1889. /* Print the characters in CHARS to the OBSTACK.  QUOTE_CHAR is the quote
  1890.    character to use with string output.  WIDTH is the size of the output
  1891.    character type.  BYTE_ORDER is the the target byte order.  OPTIONS
  1892.    is the user's print options.  */

  1893. static void
  1894. print_converted_chars_to_obstack (struct obstack *obstack,
  1895.                                   VEC (converted_character_d) *chars,
  1896.                                   int quote_char, int width,
  1897.                                   enum bfd_endian byte_order,
  1898.                                   const struct value_print_options *options)
  1899. {
  1900.   unsigned int idx;
  1901.   struct converted_character *elem;
  1902.   enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
  1903.   gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
  1904.   int need_escape = 0;

  1905.   /* Set the start state.  */
  1906.   idx = 0;
  1907.   last = state = START;
  1908.   elem = NULL;

  1909.   while (1)
  1910.     {
  1911.       switch (state)
  1912.         {
  1913.         case START:
  1914.           /* Nothing to do.  */
  1915.           break;

  1916.         case SINGLE:
  1917.           {
  1918.             int j;

  1919.             /* We are outputting a single character
  1920.                (< options->repeat_count_threshold).  */

  1921.             if (last != SINGLE)
  1922.               {
  1923.                 /* We were outputting some other type of content, so we
  1924.                    must output and a comma and a quote.  */
  1925.                 if (last != START)
  1926.                   obstack_grow_wstr (obstack, LCST (", "));
  1927.                 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
  1928.               }
  1929.             /* Output the character.  */
  1930.             for (j = 0; j < elem->repeat_count; ++j)
  1931.               {
  1932.                 if (elem->result == wchar_iterate_ok)
  1933.                   print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
  1934.                                byte_order, obstack, quote_char, &need_escape);
  1935.                 else
  1936.                   print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
  1937.                                byte_order, obstack, quote_char, &need_escape);
  1938.               }
  1939.           }
  1940.           break;

  1941.         case REPEAT:
  1942.           {
  1943.             int j;
  1944.             char *s;

  1945.             /* We are outputting a character with a repeat count
  1946.                greater than options->repeat_count_threshold.  */

  1947.             if (last == SINGLE)
  1948.               {
  1949.                 /* We were outputting a single string.  Terminate the
  1950.                    string.  */
  1951.                 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
  1952.               }
  1953.             if (last != START)
  1954.               obstack_grow_wstr (obstack, LCST (", "));

  1955.             /* Output the character and repeat string.  */
  1956.             obstack_grow_wstr (obstack, LCST ("'"));
  1957.             if (elem->result == wchar_iterate_ok)
  1958.               print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
  1959.                            byte_order, obstack, quote_char, &need_escape);
  1960.             else
  1961.               print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
  1962.                            byte_order, obstack, quote_char, &need_escape);
  1963.             obstack_grow_wstr (obstack, LCST ("'"));
  1964.             s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
  1965.             for (j = 0; s[j]; ++j)
  1966.               {
  1967.                 gdb_wchar_t w = gdb_btowc (s[j]);
  1968.                 obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
  1969.               }
  1970.             xfree (s);
  1971.           }
  1972.           break;

  1973.         case INCOMPLETE:
  1974.           /* We are outputting an incomplete sequence.  */
  1975.           if (last == SINGLE)
  1976.             {
  1977.               /* If we were outputting a string of SINGLE characters,
  1978.                  terminate the quote.  */
  1979.               obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
  1980.             }
  1981.           if (last != START)
  1982.             obstack_grow_wstr (obstack, LCST (", "));

  1983.           /* Output the incomplete sequence string.  */
  1984.           obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
  1985.           print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
  1986.                        obstack, 0, &need_escape);
  1987.           obstack_grow_wstr (obstack, LCST (">"));

  1988.           /* We do not attempt to outupt anything after this.  */
  1989.           state = FINISH;
  1990.           break;

  1991.         case FINISH:
  1992.           /* All done.  If we were outputting a string of SINGLE
  1993.              characters, the string must be terminated.  Otherwise,
  1994.              REPEAT and INCOMPLETE are always left properly terminated.  */
  1995.           if (last == SINGLE)
  1996.             obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));

  1997.           return;
  1998.         }

  1999.       /* Get the next element and state.  */
  2000.       last = state;
  2001.       if (state != FINISH)
  2002.         {
  2003.           elem = VEC_index (converted_character_d, chars, idx++);
  2004.           switch (elem->result)
  2005.             {
  2006.             case wchar_iterate_ok:
  2007.             case wchar_iterate_invalid:
  2008.               if (elem->repeat_count > options->repeat_count_threshold)
  2009.                 state = REPEAT;
  2010.               else
  2011.                 state = SINGLE;
  2012.               break;

  2013.             case wchar_iterate_incomplete:
  2014.               state = INCOMPLETE;
  2015.               break;

  2016.             case wchar_iterate_eof:
  2017.               state = FINISH;
  2018.               break;
  2019.             }
  2020.         }
  2021.     }
  2022. }

  2023. /* Print the character string STRING, printing at most LENGTH
  2024.    characters.  LENGTH is -1 if the string is nul terminated.  TYPE is
  2025.    the type of each character.  OPTIONS holds the printing options;
  2026.    printing stops early if the number hits print_max; repeat counts
  2027.    are printed as appropriate.  Print ellipses at the end if we had to
  2028.    stop before printing LENGTH characters, or if FORCE_ELLIPSES.
  2029.    QUOTE_CHAR is the character to print at each end of the string.  If
  2030.    C_STYLE_TERMINATOR is true, and the last character is 0, then it is
  2031.    omitted.  */

  2032. void
  2033. generic_printstr (struct ui_file *stream, struct type *type,
  2034.                   const gdb_byte *string, unsigned int length,
  2035.                   const char *encoding, int force_ellipses,
  2036.                   int quote_char, int c_style_terminator,
  2037.                   const struct value_print_options *options)
  2038. {
  2039.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  2040.   unsigned int i;
  2041.   int width = TYPE_LENGTH (type);
  2042.   struct obstack wchar_buf, output;
  2043.   struct cleanup *cleanup;
  2044.   struct wchar_iterator *iter;
  2045.   int finished = 0;
  2046.   struct converted_character *last;
  2047.   VEC (converted_character_d) *converted_chars;

  2048.   if (length == -1)
  2049.     {
  2050.       unsigned long current_char = 1;

  2051.       for (i = 0; current_char; ++i)
  2052.         {
  2053.           QUIT;
  2054.           current_char = extract_unsigned_integer (string + i * width,
  2055.                                                    width, byte_order);
  2056.         }
  2057.       length = i;
  2058.     }

  2059.   /* If the string was not truncated due to `set print elements', and
  2060.      the last byte of it is a null, we don't print that, in
  2061.      traditional C style.  */
  2062.   if (c_style_terminator
  2063.       && !force_ellipses
  2064.       && length > 0
  2065.       && (extract_unsigned_integer (string + (length - 1) * width,
  2066.                                     width, byte_order) == 0))
  2067.     length--;

  2068.   if (length == 0)
  2069.     {
  2070.       fputs_filtered ("\"\"", stream);
  2071.       return;
  2072.     }

  2073.   /* Arrange to iterate over the characters, in wchar_t form.  */
  2074.   iter = make_wchar_iterator (string, length * width, encoding, width);
  2075.   cleanup = make_cleanup_wchar_iterator (iter);
  2076.   converted_chars = NULL;
  2077.   make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);

  2078.   /* Convert characters until the string is over or the maximum
  2079.      number of printed characters has been reached.  */
  2080.   i = 0;
  2081.   while (i < options->print_max)
  2082.     {
  2083.       int r;

  2084.       QUIT;

  2085.       /* Grab the next character and repeat count.  */
  2086.       r = count_next_character (iter, &converted_chars);

  2087.       /* If less than zero, the end of the input string was reached.  */
  2088.       if (r < 0)
  2089.         break;

  2090.       /* Otherwise, add the count to the total print count and get
  2091.          the next character.  */
  2092.       i += r;
  2093.     }

  2094.   /* Get the last element and determine if the entire string was
  2095.      processed.  */
  2096.   last = VEC_last (converted_character_d, converted_chars);
  2097.   finished = (last->result == wchar_iterate_eof);

  2098.   /* Ensure that CONVERTED_CHARS is terminated.  */
  2099.   last->result = wchar_iterate_eof;

  2100.   /* WCHAR_BUF is the obstack we use to represent the string in
  2101.      wchar_t form.  */
  2102.   obstack_init (&wchar_buf);
  2103.   make_cleanup_obstack_free (&wchar_buf);

  2104.   /* Print the output string to the obstack.  */
  2105.   print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
  2106.                                     width, byte_order, options);

  2107.   if (force_ellipses || !finished)
  2108.     obstack_grow_wstr (&wchar_buf, LCST ("..."));

  2109.   /* OUTPUT is where we collect `char's for printing.  */
  2110.   obstack_init (&output);
  2111.   make_cleanup_obstack_free (&output);

  2112.   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
  2113.                              (gdb_byte *) obstack_base (&wchar_buf),
  2114.                              obstack_object_size (&wchar_buf),
  2115.                              sizeof (gdb_wchar_t), &output, translit_char);
  2116.   obstack_1grow (&output, '\0');

  2117.   fputs_filtered (obstack_base (&output), stream);

  2118.   do_cleanups (cleanup);
  2119. }

  2120. /* Print a string from the inferior, starting at ADDR and printing up to LEN
  2121.    characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
  2122.    stops at the first null byte, otherwise printing proceeds (including null
  2123.    bytes) until either print_max or LEN characters have been printed,
  2124.    whichever is smaller.  ENCODING is the name of the string's
  2125.    encoding.  It can be NULL, in which case the target encoding is
  2126.    assumed.  */

  2127. int
  2128. val_print_string (struct type *elttype, const char *encoding,
  2129.                   CORE_ADDR addr, int len,
  2130.                   struct ui_file *stream,
  2131.                   const struct value_print_options *options)
  2132. {
  2133.   int force_ellipsis = 0;        /* Force ellipsis to be printed if nonzero.  */
  2134.   int errcode;                        /* Errno returned from bad reads.  */
  2135.   int found_nul;                /* Non-zero if we found the nul char.  */
  2136.   unsigned int fetchlimit;        /* Maximum number of chars to print.  */
  2137.   int bytes_read;
  2138.   gdb_byte *buffer = NULL;        /* Dynamically growable fetch buffer.  */
  2139.   struct cleanup *old_chain = NULL;        /* Top of the old cleanup chain.  */
  2140.   struct gdbarch *gdbarch = get_type_arch (elttype);
  2141.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2142.   int width = TYPE_LENGTH (elttype);

  2143.   /* First we need to figure out the limit on the number of characters we are
  2144.      going to attempt to fetch and print.  This is actually pretty simple.  If
  2145.      LEN >= zero, then the limit is the minimum of LEN and print_max.  If
  2146.      LEN is -1, then the limit is print_max.  This is true regardless of
  2147.      whether print_max is zero, UINT_MAX (unlimited), or something in between,
  2148.      because finding the null byte (or available memory) is what actually
  2149.      limits the fetch.  */

  2150.   fetchlimit = (len == -1 ? options->print_max : min (len,
  2151.                                                       options->print_max));

  2152.   errcode = read_string (addr, len, width, fetchlimit, byte_order,
  2153.                          &buffer, &bytes_read);
  2154.   old_chain = make_cleanup (xfree, buffer);

  2155.   addr += bytes_read;

  2156.   /* We now have either successfully filled the buffer to fetchlimit,
  2157.      or terminated early due to an error or finding a null char when
  2158.      LEN is -1.  */

  2159.   /* Determine found_nul by looking at the last character read.  */
  2160.   found_nul = 0;
  2161.   if (bytes_read >= width)
  2162.     found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
  2163.                                           byte_order) == 0;
  2164.   if (len == -1 && !found_nul)
  2165.     {
  2166.       gdb_byte *peekbuf;

  2167.       /* We didn't find a NUL terminator we were looking for.  Attempt
  2168.          to peek at the next character.  If not successful, or it is not
  2169.          a null byte, then force ellipsis to be printed.  */

  2170.       peekbuf = (gdb_byte *) alloca (width);

  2171.       if (target_read_memory (addr, peekbuf, width) == 0
  2172.           && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
  2173.         force_ellipsis = 1;
  2174.     }
  2175.   else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
  2176.     {
  2177.       /* Getting an error when we have a requested length, or fetching less
  2178.          than the number of characters actually requested, always make us
  2179.          print ellipsis.  */
  2180.       force_ellipsis = 1;
  2181.     }

  2182.   /* If we get an error before fetching anything, don't print a string.
  2183.      But if we fetch something and then get an error, print the string
  2184.      and then the error message.  */
  2185.   if (errcode == 0 || bytes_read > 0)
  2186.     {
  2187.       LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
  2188.                        encoding, force_ellipsis, options);
  2189.     }

  2190.   if (errcode != 0)
  2191.     {
  2192.       char *str;

  2193.       str = memory_error_message (errcode, gdbarch, addr);
  2194.       make_cleanup (xfree, str);

  2195.       fprintf_filtered (stream, "<error: ");
  2196.       fputs_filtered (str, stream);
  2197.       fprintf_filtered (stream, ">");
  2198.     }

  2199.   gdb_flush (stream);
  2200.   do_cleanups (old_chain);

  2201.   return (bytes_read / width);
  2202. }


  2203. /* The 'set input-radix' command writes to this auxiliary variable.
  2204.    If the requested radix is valid, INPUT_RADIX is updated; otherwise,
  2205.    it is left unchanged.  */

  2206. static unsigned input_radix_1 = 10;

  2207. /* Validate an input or output radix setting, and make sure the user
  2208.    knows what they really did here.  Radix setting is confusing, e.g.
  2209.    setting the input radix to "10" never changes it!  */

  2210. static void
  2211. set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
  2212. {
  2213.   set_input_radix_1 (from_tty, input_radix_1);
  2214. }

  2215. static void
  2216. set_input_radix_1 (int from_tty, unsigned radix)
  2217. {
  2218.   /* We don't currently disallow any input radix except 0 or 1, which don't
  2219.      make any mathematical sense.  In theory, we can deal with any input
  2220.      radix greater than 1, even if we don't have unique digits for every
  2221.      value from 0 to radix-1, but in practice we lose on large radix values.
  2222.      We should either fix the lossage or restrict the radix range more.
  2223.      (FIXME).  */

  2224.   if (radix < 2)
  2225.     {
  2226.       input_radix_1 = input_radix;
  2227.       error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
  2228.              radix);
  2229.     }
  2230.   input_radix_1 = input_radix = radix;
  2231.   if (from_tty)
  2232.     {
  2233.       printf_filtered (_("Input radix now set to "
  2234.                          "decimal %u, hex %x, octal %o.\n"),
  2235.                        radix, radix, radix);
  2236.     }
  2237. }

  2238. /* The 'set output-radix' command writes to this auxiliary variable.
  2239.    If the requested radix is valid, OUTPUT_RADIX is updated,
  2240.    otherwise, it is left unchanged.  */

  2241. static unsigned output_radix_1 = 10;

  2242. static void
  2243. set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
  2244. {
  2245.   set_output_radix_1 (from_tty, output_radix_1);
  2246. }

  2247. static void
  2248. set_output_radix_1 (int from_tty, unsigned radix)
  2249. {
  2250.   /* Validate the radix and disallow ones that we aren't prepared to
  2251.      handle correctly, leaving the radix unchanged.  */
  2252.   switch (radix)
  2253.     {
  2254.     case 16:
  2255.       user_print_options.output_format = 'x';        /* hex */
  2256.       break;
  2257.     case 10:
  2258.       user_print_options.output_format = 0;        /* decimal */
  2259.       break;
  2260.     case 8:
  2261.       user_print_options.output_format = 'o';        /* octal */
  2262.       break;
  2263.     default:
  2264.       output_radix_1 = output_radix;
  2265.       error (_("Unsupported output radix ``decimal %u''; "
  2266.                "output radix unchanged."),
  2267.              radix);
  2268.     }
  2269.   output_radix_1 = output_radix = radix;
  2270.   if (from_tty)
  2271.     {
  2272.       printf_filtered (_("Output radix now set to "
  2273.                          "decimal %u, hex %x, octal %o.\n"),
  2274.                        radix, radix, radix);
  2275.     }
  2276. }

  2277. /* Set both the input and output radix at once.  Try to set the output radix
  2278.    first, since it has the most restrictive range.  An radix that is valid as
  2279.    an output radix is also valid as an input radix.

  2280.    It may be useful to have an unusual input radix.  If the user wishes to
  2281.    set an input radix that is not valid as an output radix, he needs to use
  2282.    the 'set input-radix' command.  */

  2283. static void
  2284. set_radix (char *arg, int from_tty)
  2285. {
  2286.   unsigned radix;

  2287.   radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
  2288.   set_output_radix_1 (0, radix);
  2289.   set_input_radix_1 (0, radix);
  2290.   if (from_tty)
  2291.     {
  2292.       printf_filtered (_("Input and output radices now set to "
  2293.                          "decimal %u, hex %x, octal %o.\n"),
  2294.                        radix, radix, radix);
  2295.     }
  2296. }

  2297. /* Show both the input and output radices.  */

  2298. static void
  2299. show_radix (char *arg, int from_tty)
  2300. {
  2301.   if (from_tty)
  2302.     {
  2303.       if (input_radix == output_radix)
  2304.         {
  2305.           printf_filtered (_("Input and output radices set to "
  2306.                              "decimal %u, hex %x, octal %o.\n"),
  2307.                            input_radix, input_radix, input_radix);
  2308.         }
  2309.       else
  2310.         {
  2311.           printf_filtered (_("Input radix set to decimal "
  2312.                              "%u, hex %x, octal %o.\n"),
  2313.                            input_radix, input_radix, input_radix);
  2314.           printf_filtered (_("Output radix set to decimal "
  2315.                              "%u, hex %x, octal %o.\n"),
  2316.                            output_radix, output_radix, output_radix);
  2317.         }
  2318.     }
  2319. }


  2320. static void
  2321. set_print (char *arg, int from_tty)
  2322. {
  2323.   printf_unfiltered (
  2324.      "\"set print\" must be followed by the name of a print subcommand.\n");
  2325.   help_list (setprintlist, "set print ", all_commands, gdb_stdout);
  2326. }

  2327. static void
  2328. show_print (char *args, int from_tty)
  2329. {
  2330.   cmd_show_list (showprintlist, from_tty, "");
  2331. }

  2332. static void
  2333. set_print_raw (char *arg, int from_tty)
  2334. {
  2335.   printf_unfiltered (
  2336.      "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
  2337.   help_list (setprintrawlist, "set print raw ", all_commands, gdb_stdout);
  2338. }

  2339. static void
  2340. show_print_raw (char *args, int from_tty)
  2341. {
  2342.   cmd_show_list (showprintrawlist, from_tty, "");
  2343. }


  2344. void
  2345. _initialize_valprint (void)
  2346. {
  2347.   add_prefix_cmd ("print", no_class, set_print,
  2348.                   _("Generic command for setting how things print."),
  2349.                   &setprintlist, "set print ", 0, &setlist);
  2350.   add_alias_cmd ("p", "print", no_class, 1, &setlist);
  2351.   /* Prefer set print to set prompt.  */
  2352.   add_alias_cmd ("pr", "print", no_class, 1, &setlist);

  2353.   add_prefix_cmd ("print", no_class, show_print,
  2354.                   _("Generic command for showing print settings."),
  2355.                   &showprintlist, "show print ", 0, &showlist);
  2356.   add_alias_cmd ("p", "print", no_class, 1, &showlist);
  2357.   add_alias_cmd ("pr", "print", no_class, 1, &showlist);

  2358.   add_prefix_cmd ("raw", no_class, set_print_raw,
  2359.                   _("\
  2360. Generic command for setting what things to print in \"raw\" mode."),
  2361.                   &setprintrawlist, "set print raw ", 0, &setprintlist);
  2362.   add_prefix_cmd ("raw", no_class, show_print_raw,
  2363.                   _("Generic command for showing \"print raw\" settings."),
  2364.                   &showprintrawlist, "show print raw ", 0, &showprintlist);

  2365.   add_setshow_uinteger_cmd ("elements", no_class,
  2366.                             &user_print_options.print_max, _("\
  2367. Set limit on string chars or array elements to print."), _("\
  2368. Show limit on string chars or array elements to print."), _("\
  2369. \"set print elements unlimited\" causes there to be no limit."),
  2370.                             NULL,
  2371.                             show_print_max,
  2372.                             &setprintlist, &showprintlist);

  2373.   add_setshow_boolean_cmd ("null-stop", no_class,
  2374.                            &user_print_options.stop_print_at_null, _("\
  2375. Set printing of char arrays to stop at first null char."), _("\
  2376. Show printing of char arrays to stop at first null char."), NULL,
  2377.                            NULL,
  2378.                            show_stop_print_at_null,
  2379.                            &setprintlist, &showprintlist);

  2380.   add_setshow_uinteger_cmd ("repeats", no_class,
  2381.                             &user_print_options.repeat_count_threshold, _("\
  2382. Set threshold for repeated print elements."), _("\
  2383. Show threshold for repeated print elements."), _("\
  2384. \"set print repeats unlimited\" causes all elements to be individually printed."),
  2385.                             NULL,
  2386.                             show_repeat_count_threshold,
  2387.                             &setprintlist, &showprintlist);

  2388.   add_setshow_boolean_cmd ("pretty", class_support,
  2389.                            &user_print_options.prettyformat_structs, _("\
  2390. Set pretty formatting of structures."), _("\
  2391. Show pretty formatting of structures."), NULL,
  2392.                            NULL,
  2393.                            show_prettyformat_structs,
  2394.                            &setprintlist, &showprintlist);

  2395.   add_setshow_boolean_cmd ("union", class_support,
  2396.                            &user_print_options.unionprint, _("\
  2397. Set printing of unions interior to structures."), _("\
  2398. Show printing of unions interior to structures."), NULL,
  2399.                            NULL,
  2400.                            show_unionprint,
  2401.                            &setprintlist, &showprintlist);

  2402.   add_setshow_boolean_cmd ("array", class_support,
  2403.                            &user_print_options.prettyformat_arrays, _("\
  2404. Set pretty formatting of arrays."), _("\
  2405. Show pretty formatting of arrays."), NULL,
  2406.                            NULL,
  2407.                            show_prettyformat_arrays,
  2408.                            &setprintlist, &showprintlist);

  2409.   add_setshow_boolean_cmd ("address", class_support,
  2410.                            &user_print_options.addressprint, _("\
  2411. Set printing of addresses."), _("\
  2412. Show printing of addresses."), NULL,
  2413.                            NULL,
  2414.                            show_addressprint,
  2415.                            &setprintlist, &showprintlist);

  2416.   add_setshow_boolean_cmd ("symbol", class_support,
  2417.                            &user_print_options.symbol_print, _("\
  2418. Set printing of symbol names when printing pointers."), _("\
  2419. Show printing of symbol names when printing pointers."),
  2420.                            NULL, NULL,
  2421.                            show_symbol_print,
  2422.                            &setprintlist, &showprintlist);

  2423.   add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
  2424.                              _("\
  2425. Set default input radix for entering numbers."), _("\
  2426. Show default input radix for entering numbers."), NULL,
  2427.                              set_input_radix,
  2428.                              show_input_radix,
  2429.                              &setlist, &showlist);

  2430.   add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
  2431.                              _("\
  2432. Set default output radix for printing of values."), _("\
  2433. Show default output radix for printing of values."), NULL,
  2434.                              set_output_radix,
  2435.                              show_output_radix,
  2436.                              &setlist, &showlist);

  2437.   /* The "set radix" and "show radix" commands are special in that
  2438.      they are like normal set and show commands but allow two normally
  2439.      independent variables to be either set or shown with a single
  2440.      command.  So the usual deprecated_add_set_cmd() and [deleted]
  2441.      add_show_from_set() commands aren't really appropriate.  */
  2442.   /* FIXME: i18n: With the new add_setshow_integer command, that is no
  2443.      longer true - show can display anything.  */
  2444.   add_cmd ("radix", class_support, set_radix, _("\
  2445. Set default input and output number radices.\n\
  2446. Use 'set input-radix' or 'set output-radix' to independently set each.\n\
  2447. Without an argument, sets both radices back to the default value of 10."),
  2448.            &setlist);
  2449.   add_cmd ("radix", class_support, show_radix, _("\
  2450. Show the default input and output number radices.\n\
  2451. Use 'show input-radix' or 'show output-radix' to independently show each."),
  2452.            &showlist);

  2453.   add_setshow_boolean_cmd ("array-indexes", class_support,
  2454.                            &user_print_options.print_array_indexes, _("\
  2455. Set printing of array indexes."), _("\
  2456. Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
  2457.                            &setprintlist, &showprintlist);
  2458. }