gdb/printcmd.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Print values for GNU debugger GDB.

  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 "frame.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "value.h"
  19. #include "language.h"
  20. #include "expression.h"
  21. #include "gdbcore.h"
  22. #include "gdbcmd.h"
  23. #include "target.h"
  24. #include "breakpoint.h"
  25. #include "demangle.h"
  26. #include "gdb-demangle.h"
  27. #include "valprint.h"
  28. #include "annotate.h"
  29. #include "symfile.h"                /* for overlay functions */
  30. #include "objfiles.h"                /* ditto */
  31. #include "completer.h"                /* for completion functions */
  32. #include "ui-out.h"
  33. #include "block.h"
  34. #include "disasm.h"
  35. #include "dfp.h"
  36. #include "observer.h"
  37. #include "solist.h"
  38. #include "parser-defs.h"
  39. #include "charset.h"
  40. #include "arch-utils.h"
  41. #include "cli/cli-utils.h"
  42. #include "format.h"
  43. #include "source.h"

  44. #ifdef TUI
  45. #include "tui/tui.h"                /* For tui_active et al.   */
  46. #endif

  47. struct format_data
  48.   {
  49.     int count;
  50.     char format;
  51.     char size;

  52.     /* True if the value should be printed raw -- that is, bypassing
  53.        python-based formatters.  */
  54.     unsigned char raw;
  55.   };

  56. /* Last specified output format.  */

  57. static char last_format = 0;

  58. /* Last specified examination size.  'b', 'h', 'w' or `q'.  */

  59. static char last_size = 'w';

  60. /* Default address to examine next, and associated architecture.  */

  61. static struct gdbarch *next_gdbarch;
  62. static CORE_ADDR next_address;

  63. /* Number of delay instructions following current disassembled insn.  */

  64. static int branch_delay_insns;

  65. /* Last address examined.  */

  66. static CORE_ADDR last_examine_address;

  67. /* Contents of last address examined.
  68.    This is not valid past the end of the `x' command!  */

  69. static struct value *last_examine_value;

  70. /* Largest offset between a symbolic value and an address, that will be
  71.    printed as `0x1234 <symbol+offset>'.  */

  72. static unsigned int max_symbolic_offset = UINT_MAX;
  73. static void
  74. show_max_symbolic_offset (struct ui_file *file, int from_tty,
  75.                           struct cmd_list_element *c, const char *value)
  76. {
  77.   fprintf_filtered (file,
  78.                     _("The largest offset that will be "
  79.                       "printed in <symbol+1234> form is %s.\n"),
  80.                     value);
  81. }

  82. /* Append the source filename and linenumber of the symbol when
  83.    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
  84. static int print_symbol_filename = 0;
  85. static void
  86. show_print_symbol_filename (struct ui_file *file, int from_tty,
  87.                             struct cmd_list_element *c, const char *value)
  88. {
  89.   fprintf_filtered (file, _("Printing of source filename and "
  90.                             "line number with <symbol> is %s.\n"),
  91.                     value);
  92. }

  93. /* Number of auto-display expression currently being displayed.
  94.    So that we can disable it if we get a signal within it.
  95.    -1 when not doing one.  */

  96. static int current_display_number;

  97. struct display
  98.   {
  99.     /* Chain link to next auto-display item.  */
  100.     struct display *next;

  101.     /* The expression as the user typed it.  */
  102.     char *exp_string;

  103.     /* Expression to be evaluated and displayed.  */
  104.     struct expression *exp;

  105.     /* Item number of this auto-display item.  */
  106.     int number;

  107.     /* Display format specified.  */
  108.     struct format_data format;

  109.     /* Program space associated with `block'.  */
  110.     struct program_space *pspace;

  111.     /* Innermost block required by this expression when evaluated.  */
  112.     const struct block *block;

  113.     /* Status of this display (enabled or disabled).  */
  114.     int enabled_p;
  115.   };

  116. /* Chain of expressions whose values should be displayed
  117.    automatically each time the program stops.  */

  118. static struct display *display_chain;

  119. static int display_number;

  120. /* Walk the following statement or block through all displays.
  121.    ALL_DISPLAYS_SAFE does so even if the statement deletes the current
  122.    display.  */

  123. #define ALL_DISPLAYS(B)                                \
  124.   for (B = display_chain; B; B = B->next)

  125. #define ALL_DISPLAYS_SAFE(B,TMP)                \
  126.   for (B = display_chain;                        \
  127.        B ? (TMP = B->next, 1): 0;                \
  128.        B = TMP)

  129. /* Prototypes for exported functions.  */

  130. void _initialize_printcmd (void);

  131. /* Prototypes for local functions.  */

  132. static void do_one_display (struct display *);


  133. /* Decode a format specification.  *STRING_PTR should point to it.
  134.    OFORMAT and OSIZE are used as defaults for the format and size
  135.    if none are given in the format specification.
  136.    If OSIZE is zero, then the size field of the returned value
  137.    should be set only if a size is explicitly specified by the
  138.    user.
  139.    The structure returned describes all the data
  140.    found in the specification.  In addition, *STRING_PTR is advanced
  141.    past the specification and past all whitespace following it.  */

  142. static struct format_data
  143. decode_format (const char **string_ptr, int oformat, int osize)
  144. {
  145.   struct format_data val;
  146.   const char *p = *string_ptr;

  147.   val.format = '?';
  148.   val.size = '?';
  149.   val.count = 1;
  150.   val.raw = 0;

  151.   if (*p >= '0' && *p <= '9')
  152.     val.count = atoi (p);
  153.   while (*p >= '0' && *p <= '9')
  154.     p++;

  155.   /* Now process size or format letters that follow.  */

  156.   while (1)
  157.     {
  158.       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
  159.         val.size = *p++;
  160.       else if (*p == 'r')
  161.         {
  162.           val.raw = 1;
  163.           p++;
  164.         }
  165.       else if (*p >= 'a' && *p <= 'z')
  166.         val.format = *p++;
  167.       else
  168.         break;
  169.     }

  170.   while (*p == ' ' || *p == '\t')
  171.     p++;
  172.   *string_ptr = p;

  173.   /* Set defaults for format and size if not specified.  */
  174.   if (val.format == '?')
  175.     {
  176.       if (val.size == '?')
  177.         {
  178.           /* Neither has been specified.  */
  179.           val.format = oformat;
  180.           val.size = osize;
  181.         }
  182.       else
  183.         /* If a size is specified, any format makes a reasonable
  184.            default except 'i'.  */
  185.         val.format = oformat == 'i' ? 'x' : oformat;
  186.     }
  187.   else if (val.size == '?')
  188.     switch (val.format)
  189.       {
  190.       case 'a':
  191.         /* Pick the appropriate size for an address.  This is deferred
  192.            until do_examine when we know the actual architecture to use.
  193.            A special size value of 'a' is used to indicate this case.  */
  194.         val.size = osize ? 'a' : osize;
  195.         break;
  196.       case 'f':
  197.         /* Floating point has to be word or giantword.  */
  198.         if (osize == 'w' || osize == 'g')
  199.           val.size = osize;
  200.         else
  201.           /* Default it to giantword if the last used size is not
  202.              appropriate.  */
  203.           val.size = osize ? 'g' : osize;
  204.         break;
  205.       case 'c':
  206.         /* Characters default to one byte.  */
  207.         val.size = osize ? 'b' : osize;
  208.         break;
  209.       case 's':
  210.         /* Display strings with byte size chars unless explicitly
  211.            specified.  */
  212.         val.size = '\0';
  213.         break;

  214.       default:
  215.         /* The default is the size most recently specified.  */
  216.         val.size = osize;
  217.       }

  218.   return val;
  219. }

  220. /* Print value VAL on stream according to OPTIONS.
  221.    Do not end with a newline.
  222.    SIZE is the letter for the size of datum being printed.
  223.    This is used to pad hex numbers so they line up.  SIZE is 0
  224.    for print / output and set for examine.  */

  225. static void
  226. print_formatted (struct value *val, int size,
  227.                  const struct value_print_options *options,
  228.                  struct ui_file *stream)
  229. {
  230.   struct type *type = check_typedef (value_type (val));
  231.   int len = TYPE_LENGTH (type);

  232.   if (VALUE_LVAL (val) == lval_memory)
  233.     next_address = value_address (val) + len;

  234.   if (size)
  235.     {
  236.       switch (options->format)
  237.         {
  238.         case 's':
  239.           {
  240.             struct type *elttype = value_type (val);

  241.             next_address = (value_address (val)
  242.                             + val_print_string (elttype, NULL,
  243.                                                 value_address (val), -1,
  244.                                                 stream, options) * len);
  245.           }
  246.           return;

  247.         case 'i':
  248.           /* We often wrap here if there are long symbolic names.  */
  249.           wrap_here ("    ");
  250.           next_address = (value_address (val)
  251.                           + gdb_print_insn (get_type_arch (type),
  252.                                             value_address (val), stream,
  253.                                             &branch_delay_insns));
  254.           return;
  255.         }
  256.     }

  257.   if (options->format == 0 || options->format == 's'
  258.       || TYPE_CODE (type) == TYPE_CODE_REF
  259.       || TYPE_CODE (type) == TYPE_CODE_ARRAY
  260.       || TYPE_CODE (type) == TYPE_CODE_STRING
  261.       || TYPE_CODE (type) == TYPE_CODE_STRUCT
  262.       || TYPE_CODE (type) == TYPE_CODE_UNION
  263.       || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
  264.     value_print (val, stream, options);
  265.   else
  266.     /* User specified format, so don't look to the type to tell us
  267.        what to do.  */
  268.     val_print_scalar_formatted (type,
  269.                                 value_contents_for_printing (val),
  270.                                 value_embedded_offset (val),
  271.                                 val,
  272.                                 options, size, stream);
  273. }

  274. /* Return builtin floating point type of same length as TYPE.
  275.    If no such type is found, return TYPE itself.  */
  276. static struct type *
  277. float_type_from_length (struct type *type)
  278. {
  279.   struct gdbarch *gdbarch = get_type_arch (type);
  280.   const struct builtin_type *builtin = builtin_type (gdbarch);

  281.   if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
  282.     type = builtin->builtin_float;
  283.   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
  284.     type = builtin->builtin_double;
  285.   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
  286.     type = builtin->builtin_long_double;

  287.   return type;
  288. }

  289. /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
  290.    according to OPTIONS and SIZE on STREAM.  Formats s and i are not
  291.    supported at this level.  */

  292. void
  293. print_scalar_formatted (const void *valaddr, struct type *type,
  294.                         const struct value_print_options *options,
  295.                         int size, struct ui_file *stream)
  296. {
  297.   struct gdbarch *gdbarch = get_type_arch (type);
  298.   LONGEST val_long = 0;
  299.   unsigned int len = TYPE_LENGTH (type);
  300.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  301.   /* String printing should go through val_print_scalar_formatted.  */
  302.   gdb_assert (options->format != 's');

  303.   if (len > sizeof(LONGEST) &&
  304.       (TYPE_CODE (type) == TYPE_CODE_INT
  305.        || TYPE_CODE (type) == TYPE_CODE_ENUM))
  306.     {
  307.       switch (options->format)
  308.         {
  309.         case 'o':
  310.           print_octal_chars (stream, valaddr, len, byte_order);
  311.           return;
  312.         case 'u':
  313.         case 'd':
  314.           print_decimal_chars (stream, valaddr, len, byte_order);
  315.           return;
  316.         case 't':
  317.           print_binary_chars (stream, valaddr, len, byte_order);
  318.           return;
  319.         case 'x':
  320.           print_hex_chars (stream, valaddr, len, byte_order);
  321.           return;
  322.         case 'c':
  323.           print_char_chars (stream, type, valaddr, len, byte_order);
  324.           return;
  325.         default:
  326.           break;
  327.         };
  328.     }

  329.   if (options->format != 'f')
  330.     val_long = unpack_long (type, valaddr);

  331.   /* If the value is a pointer, and pointers and addresses are not the
  332.      same, then at this point, the value's length (in target bytes) is
  333.      gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
  334.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  335.     len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;

  336.   /* If we are printing it as unsigned, truncate it in case it is actually
  337.      a negative signed value (e.g. "print/u (short)-1" should print 65535
  338.      (if shorts are 16 bits) instead of 4294967295).  */
  339.   if (options->format != 'd' || TYPE_UNSIGNED (type))
  340.     {
  341.       if (len < sizeof (LONGEST))
  342.         val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
  343.     }

  344.   switch (options->format)
  345.     {
  346.     case 'x':
  347.       if (!size)
  348.         {
  349.           /* No size specified, like in print.  Print varying # of digits.  */
  350.           print_longest (stream, 'x', 1, val_long);
  351.         }
  352.       else
  353.         switch (size)
  354.           {
  355.           case 'b':
  356.           case 'h':
  357.           case 'w':
  358.           case 'g':
  359.             print_longest (stream, size, 1, val_long);
  360.             break;
  361.           default:
  362.             error (_("Undefined output size \"%c\"."), size);
  363.           }
  364.       break;

  365.     case 'd':
  366.       print_longest (stream, 'd', 1, val_long);
  367.       break;

  368.     case 'u':
  369.       print_longest (stream, 'u', 0, val_long);
  370.       break;

  371.     case 'o':
  372.       if (val_long)
  373.         print_longest (stream, 'o', 1, val_long);
  374.       else
  375.         fprintf_filtered (stream, "0");
  376.       break;

  377.     case 'a':
  378.       {
  379.         CORE_ADDR addr = unpack_pointer (type, valaddr);

  380.         print_address (gdbarch, addr, stream);
  381.       }
  382.       break;

  383.     case 'c':
  384.       {
  385.         struct value_print_options opts = *options;

  386.         opts.format = 0;
  387.         if (TYPE_UNSIGNED (type))
  388.           type = builtin_type (gdbarch)->builtin_true_unsigned_char;
  389.          else
  390.           type = builtin_type (gdbarch)->builtin_true_char;

  391.         value_print (value_from_longest (type, val_long), stream, &opts);
  392.       }
  393.       break;

  394.     case 'f':
  395.       type = float_type_from_length (type);
  396.       print_floating (valaddr, type, stream);
  397.       break;

  398.     case 0:
  399.       internal_error (__FILE__, __LINE__,
  400.                       _("failed internal consistency check"));

  401.     case 't':
  402.       /* Binary; 't' stands for "two".  */
  403.       {
  404.         char bits[8 * (sizeof val_long) + 1];
  405.         char buf[8 * (sizeof val_long) + 32];
  406.         char *cp = bits;
  407.         int width;

  408.         if (!size)
  409.           width = 8 * (sizeof val_long);
  410.         else
  411.           switch (size)
  412.             {
  413.             case 'b':
  414.               width = 8;
  415.               break;
  416.             case 'h':
  417.               width = 16;
  418.               break;
  419.             case 'w':
  420.               width = 32;
  421.               break;
  422.             case 'g':
  423.               width = 64;
  424.               break;
  425.             default:
  426.               error (_("Undefined output size \"%c\"."), size);
  427.             }

  428.         bits[width] = '\0';
  429.         while (width-- > 0)
  430.           {
  431.             bits[width] = (val_long & 1) ? '1' : '0';
  432.             val_long >>= 1;
  433.           }
  434.         if (!size)
  435.           {
  436.             while (*cp && *cp == '0')
  437.               cp++;
  438.             if (*cp == '\0')
  439.               cp--;
  440.           }
  441.         strncpy (buf, cp, sizeof (bits));
  442.         fputs_filtered (buf, stream);
  443.       }
  444.       break;

  445.     case 'z':
  446.       print_hex_chars (stream, valaddr, len, byte_order);
  447.       break;

  448.     default:
  449.       error (_("Undefined output format \"%c\"."), options->format);
  450.     }
  451. }

  452. /* Specify default address for `x' command.
  453.    The `info lines' command uses this.  */

  454. void
  455. set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
  456. {
  457.   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;

  458.   next_gdbarch = gdbarch;
  459.   next_address = addr;

  460.   /* Make address available to the user as $_.  */
  461.   set_internalvar (lookup_internalvar ("_"),
  462.                    value_from_pointer (ptr_type, addr));
  463. }

  464. /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
  465.    after LEADIN.  Print nothing if no symbolic name is found nearby.
  466.    Optionally also print source file and line number, if available.
  467.    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
  468.    or to interpret it as a possible C++ name and convert it back to source
  469.    form.  However note that DO_DEMANGLE can be overridden by the specific
  470.    settings of the demangle and asm_demangle variables.  Returns
  471.    non-zero if anything was printed; zero otherwise.  */

  472. int
  473. print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
  474.                         struct ui_file *stream,
  475.                         int do_demangle, char *leadin)
  476. {
  477.   char *name = NULL;
  478.   char *filename = NULL;
  479.   int unmapped = 0;
  480.   int offset = 0;
  481.   int line = 0;

  482.   /* Throw away both name and filename.  */
  483.   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
  484.   make_cleanup (free_current_contents, &filename);

  485.   if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
  486.                               &filename, &line, &unmapped))
  487.     {
  488.       do_cleanups (cleanup_chain);
  489.       return 0;
  490.     }

  491.   fputs_filtered (leadin, stream);
  492.   if (unmapped)
  493.     fputs_filtered ("<*", stream);
  494.   else
  495.     fputs_filtered ("<", stream);
  496.   fputs_filtered (name, stream);
  497.   if (offset != 0)
  498.     fprintf_filtered (stream, "+%u", (unsigned int) offset);

  499.   /* Append source filename and line number if desired.  Give specific
  500.      line # of this addr, if we have it; else line # of the nearest symbol.  */
  501.   if (print_symbol_filename && filename != NULL)
  502.     {
  503.       if (line != -1)
  504.         fprintf_filtered (stream, " at %s:%d", filename, line);
  505.       else
  506.         fprintf_filtered (stream, " in %s", filename);
  507.     }
  508.   if (unmapped)
  509.     fputs_filtered ("*>", stream);
  510.   else
  511.     fputs_filtered (">", stream);

  512.   do_cleanups (cleanup_chain);
  513.   return 1;
  514. }

  515. /* Given an address ADDR return all the elements needed to print the
  516.    address in a symbolic form.  NAME can be mangled or not depending
  517.    on DO_DEMANGLE (and also on the asm_demangle global variable,
  518.    manipulated via ''set print asm-demangle'').  Return 0 in case of
  519.    success, when all the info in the OUT paramters is valid.  Return 1
  520.    otherwise.  */
  521. int
  522. build_address_symbolic (struct gdbarch *gdbarch,
  523.                         CORE_ADDR addr/* IN */
  524.                         int do_demangle, /* IN */
  525.                         char **name,     /* OUT */
  526.                         int *offset,     /* OUT */
  527.                         char **filename, /* OUT */
  528.                         int *line,       /* OUT */
  529.                         int *unmapped)   /* OUT */
  530. {
  531.   struct bound_minimal_symbol msymbol;
  532.   struct symbol *symbol;
  533.   CORE_ADDR name_location = 0;
  534.   struct obj_section *section = NULL;
  535.   const char *name_temp = "";

  536.   /* Let's say it is mapped (not unmapped).  */
  537.   *unmapped = 0;

  538.   /* Determine if the address is in an overlay, and whether it is
  539.      mapped.  */
  540.   if (overlay_debugging)
  541.     {
  542.       section = find_pc_overlay (addr);
  543.       if (pc_in_unmapped_range (addr, section))
  544.         {
  545.           *unmapped = 1;
  546.           addr = overlay_mapped_address (addr, section);
  547.         }
  548.     }

  549.   /* First try to find the address in the symbol table, then
  550.      in the minsyms.  Take the closest one.  */

  551.   /* This is defective in the sense that it only finds text symbols.  So
  552.      really this is kind of pointless--we should make sure that the
  553.      minimal symbols have everything we need (by changing that we could
  554.      save some memory, but for many debug format--ELF/DWARF or
  555.      anything/stabs--it would be inconvenient to eliminate those minimal
  556.      symbols anyway).  */
  557.   msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
  558.   symbol = find_pc_sect_function (addr, section);

  559.   if (symbol)
  560.     {
  561.       /* If this is a function (i.e. a code address), strip out any
  562.          non-address bits.  For instance, display a pointer to the
  563.          first instruction of a Thumb function as <function>; the
  564.          second instruction will be <function+2>, even though the
  565.          pointer is <function+3>.  This matches the ISA behavior.  */
  566.       addr = gdbarch_addr_bits_remove (gdbarch, addr);

  567.       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
  568.       if (do_demangle || asm_demangle)
  569.         name_temp = SYMBOL_PRINT_NAME (symbol);
  570.       else
  571.         name_temp = SYMBOL_LINKAGE_NAME (symbol);
  572.     }

  573.   if (msymbol.minsym != NULL
  574.       && MSYMBOL_HAS_SIZE (msymbol.minsym)
  575.       && MSYMBOL_SIZE (msymbol.minsym) == 0
  576.       && MSYMBOL_TYPE (msymbol.minsym) != mst_text
  577.       && MSYMBOL_TYPE (msymbol.minsym) != mst_text_gnu_ifunc
  578.       && MSYMBOL_TYPE (msymbol.minsym) != mst_file_text)
  579.     msymbol.minsym = NULL;

  580.   if (msymbol.minsym != NULL)
  581.     {
  582.       if (BMSYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
  583.         {
  584.           /* If this is a function (i.e. a code address), strip out any
  585.              non-address bits.  For instance, display a pointer to the
  586.              first instruction of a Thumb function as <function>; the
  587.              second instruction will be <function+2>, even though the
  588.              pointer is <function+3>.  This matches the ISA behavior.  */
  589.           if (MSYMBOL_TYPE (msymbol.minsym) == mst_text
  590.               || MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc
  591.               || MSYMBOL_TYPE (msymbol.minsym) == mst_file_text
  592.               || MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  593.             addr = gdbarch_addr_bits_remove (gdbarch, addr);

  594.           /* The msymbol is closer to the address than the symbol;
  595.              use the msymbol instead.  */
  596.           symbol = 0;
  597.           name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
  598.           if (do_demangle || asm_demangle)
  599.             name_temp = MSYMBOL_PRINT_NAME (msymbol.minsym);
  600.           else
  601.             name_temp = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
  602.         }
  603.     }
  604.   if (symbol == NULL && msymbol.minsym == NULL)
  605.     return 1;

  606.   /* If the nearest symbol is too far away, don't print anything symbolic.  */

  607.   /* For when CORE_ADDR is larger than unsigned int, we do math in
  608.      CORE_ADDR.  But when we detect unsigned wraparound in the
  609.      CORE_ADDR math, we ignore this test and print the offset,
  610.      because addr+max_symbolic_offset has wrapped through the end
  611.      of the address space back to the beginning, giving bogus comparison.  */
  612.   if (addr > name_location + max_symbolic_offset
  613.       && name_location + max_symbolic_offset > name_location)
  614.     return 1;

  615.   *offset = addr - name_location;

  616.   *name = xstrdup (name_temp);

  617.   if (print_symbol_filename)
  618.     {
  619.       struct symtab_and_line sal;

  620.       sal = find_pc_sect_line (addr, section, 0);

  621.       if (sal.symtab)
  622.         {
  623.           *filename = xstrdup (symtab_to_filename_for_display (sal.symtab));
  624.           *line = sal.line;
  625.         }
  626.     }
  627.   return 0;
  628. }


  629. /* Print address ADDR symbolically on STREAM.
  630.    First print it as a number.  Then perhaps print
  631.    <SYMBOL + OFFSET> after the number.  */

  632. void
  633. print_address (struct gdbarch *gdbarch,
  634.                CORE_ADDR addr, struct ui_file *stream)
  635. {
  636.   fputs_filtered (paddress (gdbarch, addr), stream);
  637.   print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
  638. }

  639. /* Return a prefix for instruction address:
  640.    "=> " for current instruction, else "   ".  */

  641. const char *
  642. pc_prefix (CORE_ADDR addr)
  643. {
  644.   if (has_stack_frames ())
  645.     {
  646.       struct frame_info *frame;
  647.       CORE_ADDR pc;

  648.       frame = get_selected_frame (NULL);
  649.       if (get_frame_pc_if_available (frame, &pc) && pc == addr)
  650.         return "=> ";
  651.     }
  652.   return "   ";
  653. }

  654. /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
  655.    controls whether to print the symbolic name "raw" or demangled.
  656.    Return non-zero if anything was printed; zero otherwise.  */

  657. int
  658. print_address_demangle (const struct value_print_options *opts,
  659.                         struct gdbarch *gdbarch, CORE_ADDR addr,
  660.                         struct ui_file *stream, int do_demangle)
  661. {
  662.   if (opts->addressprint)
  663.     {
  664.       fputs_filtered (paddress (gdbarch, addr), stream);
  665.       print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
  666.     }
  667.   else
  668.     {
  669.       return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
  670.     }
  671.   return 1;
  672. }


  673. /* Examine data at address ADDR in format FMT.
  674.    Fetch it from memory and print on gdb_stdout.  */

  675. static void
  676. do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
  677. {
  678.   char format = 0;
  679.   char size;
  680.   int count = 1;
  681.   struct type *val_type = NULL;
  682.   int i;
  683.   int maxelts;
  684.   struct value_print_options opts;

  685.   format = fmt.format;
  686.   size = fmt.size;
  687.   count = fmt.count;
  688.   next_gdbarch = gdbarch;
  689.   next_address = addr;

  690.   /* Instruction format implies fetch single bytes
  691.      regardless of the specified size.
  692.      The case of strings is handled in decode_format, only explicit
  693.      size operator are not changed to 'b'.  */
  694.   if (format == 'i')
  695.     size = 'b';

  696.   if (size == 'a')
  697.     {
  698.       /* Pick the appropriate size for an address.  */
  699.       if (gdbarch_ptr_bit (next_gdbarch) == 64)
  700.         size = 'g';
  701.       else if (gdbarch_ptr_bit (next_gdbarch) == 32)
  702.         size = 'w';
  703.       else if (gdbarch_ptr_bit (next_gdbarch) == 16)
  704.         size = 'h';
  705.       else
  706.         /* Bad value for gdbarch_ptr_bit.  */
  707.         internal_error (__FILE__, __LINE__,
  708.                         _("failed internal consistency check"));
  709.     }

  710.   if (size == 'b')
  711.     val_type = builtin_type (next_gdbarch)->builtin_int8;
  712.   else if (size == 'h')
  713.     val_type = builtin_type (next_gdbarch)->builtin_int16;
  714.   else if (size == 'w')
  715.     val_type = builtin_type (next_gdbarch)->builtin_int32;
  716.   else if (size == 'g')
  717.     val_type = builtin_type (next_gdbarch)->builtin_int64;

  718.   if (format == 's')
  719.     {
  720.       struct type *char_type = NULL;

  721.       /* Search for "char16_t"  or "char32_t" types or fall back to 8-bit char
  722.          if type is not found.  */
  723.       if (size == 'h')
  724.         char_type = builtin_type (next_gdbarch)->builtin_char16;
  725.       else if (size == 'w')
  726.         char_type = builtin_type (next_gdbarch)->builtin_char32;
  727.       if (char_type)
  728.         val_type = char_type;
  729.       else
  730.         {
  731.           if (size != '\0' && size != 'b')
  732.             warning (_("Unable to display strings with "
  733.                        "size '%c', using 'b' instead."), size);
  734.           size = 'b';
  735.           val_type = builtin_type (next_gdbarch)->builtin_int8;
  736.         }
  737.     }

  738.   maxelts = 8;
  739.   if (size == 'w')
  740.     maxelts = 4;
  741.   if (size == 'g')
  742.     maxelts = 2;
  743.   if (format == 's' || format == 'i')
  744.     maxelts = 1;

  745.   get_formatted_print_options (&opts, format);

  746.   /* Print as many objects as specified in COUNT, at most maxelts per line,
  747.      with the address of the next one at the start of each line.  */

  748.   while (count > 0)
  749.     {
  750.       QUIT;
  751.       if (format == 'i')
  752.         fputs_filtered (pc_prefix (next_address), gdb_stdout);
  753.       print_address (next_gdbarch, next_address, gdb_stdout);
  754.       printf_filtered (":");
  755.       for (i = maxelts;
  756.            i > 0 && count > 0;
  757.            i--, count--)
  758.         {
  759.           printf_filtered ("\t");
  760.           /* Note that print_formatted sets next_address for the next
  761.              object.  */
  762.           last_examine_address = next_address;

  763.           if (last_examine_value)
  764.             value_free (last_examine_value);

  765.           /* The value to be displayed is not fetched greedily.
  766.              Instead, to avoid the possibility of a fetched value not
  767.              being used, its retrieval is delayed until the print code
  768.              uses it.  When examining an instruction stream, the
  769.              disassembler will perform its own memory fetch using just
  770.              the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
  771.              the disassembler be modified so that LAST_EXAMINE_VALUE
  772.              is left with the byte sequence from the last complete
  773.              instruction fetched from memory?  */
  774.           last_examine_value = value_at_lazy (val_type, next_address);

  775.           if (last_examine_value)
  776.             release_value (last_examine_value);

  777.           print_formatted (last_examine_value, size, &opts, gdb_stdout);

  778.           /* Display any branch delay slots following the final insn.  */
  779.           if (format == 'i' && count == 1)
  780.             count += branch_delay_insns;
  781.         }
  782.       printf_filtered ("\n");
  783.       gdb_flush (gdb_stdout);
  784.     }
  785. }

  786. static void
  787. validate_format (struct format_data fmt, char *cmdname)
  788. {
  789.   if (fmt.size != 0)
  790.     error (_("Size letters are meaningless in \"%s\" command."), cmdname);
  791.   if (fmt.count != 1)
  792.     error (_("Item count other than 1 is meaningless in \"%s\" command."),
  793.            cmdname);
  794.   if (fmt.format == 'i')
  795.     error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
  796.            fmt.format, cmdname);
  797. }

  798. /* Evaluate string EXP as an expression in the current language and
  799.    print the resulting value.  EXP may contain a format specifier as the
  800.    first argument ("/x myvar" for example, to print myvar in hex).  */

  801. static void
  802. print_command_1 (const char *exp, int voidprint)
  803. {
  804.   struct expression *expr;
  805.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
  806.   char format = 0;
  807.   struct value *val;
  808.   struct format_data fmt;

  809.   if (exp && *exp == '/')
  810.     {
  811.       exp++;
  812.       fmt = decode_format (&exp, last_format, 0);
  813.       validate_format (fmt, "print");
  814.       last_format = format = fmt.format;
  815.     }
  816.   else
  817.     {
  818.       fmt.count = 1;
  819.       fmt.format = 0;
  820.       fmt.size = 0;
  821.       fmt.raw = 0;
  822.     }

  823.   if (exp && *exp)
  824.     {
  825.       expr = parse_expression (exp);
  826.       make_cleanup (free_current_contents, &expr);
  827.       val = evaluate_expression (expr);
  828.     }
  829.   else
  830.     val = access_value_history (0);

  831.   if (voidprint || (val && value_type (val) &&
  832.                     TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
  833.     {
  834.       struct value_print_options opts;
  835.       int histindex = record_latest_value (val);

  836.       annotate_value_history_begin (histindex, value_type (val));

  837.       printf_filtered ("$%d = ", histindex);

  838.       annotate_value_history_value ();

  839.       get_formatted_print_options (&opts, format);
  840.       opts.raw = fmt.raw;

  841.       print_formatted (val, fmt.size, &opts, gdb_stdout);
  842.       printf_filtered ("\n");

  843.       annotate_value_history_end ();
  844.     }

  845.   do_cleanups (old_chain);
  846. }

  847. static void
  848. print_command (char *exp, int from_tty)
  849. {
  850.   print_command_1 (exp, 1);
  851. }

  852. /* Same as print, except it doesn't print void results.  */
  853. static void
  854. call_command (char *exp, int from_tty)
  855. {
  856.   print_command_1 (exp, 0);
  857. }

  858. /* Implementation of the "output" command.  */

  859. static void
  860. output_command (char *exp, int from_tty)
  861. {
  862.   output_command_const (exp, from_tty);
  863. }

  864. /* Like output_command, but takes a const string as argument.  */

  865. void
  866. output_command_const (const char *exp, int from_tty)
  867. {
  868.   struct expression *expr;
  869.   struct cleanup *old_chain;
  870.   char format = 0;
  871.   struct value *val;
  872.   struct format_data fmt;
  873.   struct value_print_options opts;

  874.   fmt.size = 0;
  875.   fmt.raw = 0;

  876.   if (exp && *exp == '/')
  877.     {
  878.       exp++;
  879.       fmt = decode_format (&exp, 0, 0);
  880.       validate_format (fmt, "output");
  881.       format = fmt.format;
  882.     }

  883.   expr = parse_expression (exp);
  884.   old_chain = make_cleanup (free_current_contents, &expr);

  885.   val = evaluate_expression (expr);

  886.   annotate_value_begin (value_type (val));

  887.   get_formatted_print_options (&opts, format);
  888.   opts.raw = fmt.raw;
  889.   print_formatted (val, fmt.size, &opts, gdb_stdout);

  890.   annotate_value_end ();

  891.   wrap_here ("");
  892.   gdb_flush (gdb_stdout);

  893.   do_cleanups (old_chain);
  894. }

  895. static void
  896. set_command (char *exp, int from_tty)
  897. {
  898.   struct expression *expr = parse_expression (exp);
  899.   struct cleanup *old_chain =
  900.     make_cleanup (free_current_contents, &expr);

  901.   if (expr->nelts >= 1)
  902.     switch (expr->elts[0].opcode)
  903.       {
  904.       case UNOP_PREINCREMENT:
  905.       case UNOP_POSTINCREMENT:
  906.       case UNOP_PREDECREMENT:
  907.       case UNOP_POSTDECREMENT:
  908.       case BINOP_ASSIGN:
  909.       case BINOP_ASSIGN_MODIFY:
  910.       case BINOP_COMMA:
  911.         break;
  912.       default:
  913.         warning
  914.           (_("Expression is not an assignment (and might have no effect)"));
  915.       }

  916.   evaluate_expression (expr);
  917.   do_cleanups (old_chain);
  918. }

  919. static void
  920. sym_info (char *arg, int from_tty)
  921. {
  922.   struct minimal_symbol *msymbol;
  923.   struct objfile *objfile;
  924.   struct obj_section *osect;
  925.   CORE_ADDR addr, sect_addr;
  926.   int matches = 0;
  927.   unsigned int offset;

  928.   if (!arg)
  929.     error_no_arg (_("address"));

  930.   addr = parse_and_eval_address (arg);
  931.   ALL_OBJSECTIONS (objfile, osect)
  932.   {
  933.     /* Only process each object file once, even if there's a separate
  934.        debug file.  */
  935.     if (objfile->separate_debug_objfile_backlink)
  936.       continue;

  937.     sect_addr = overlay_mapped_address (addr, osect);

  938.     if (obj_section_addr (osect) <= sect_addr
  939.         && sect_addr < obj_section_endaddr (osect)
  940.         && (msymbol
  941.             = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
  942.       {
  943.         const char *obj_name, *mapped, *sec_name, *msym_name;
  944.         char *loc_string;
  945.         struct cleanup *old_chain;

  946.         matches = 1;
  947.         offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
  948.         mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
  949.         sec_name = osect->the_bfd_section->name;
  950.         msym_name = MSYMBOL_PRINT_NAME (msymbol);

  951.         /* Don't print the offset if it is zero.
  952.            We assume there's no need to handle i18n of "sym + offset".  */
  953.         if (offset)
  954.           loc_string = xstrprintf ("%s + %u", msym_name, offset);
  955.         else
  956.           loc_string = xstrprintf ("%s", msym_name);

  957.         /* Use a cleanup to free loc_string in case the user quits
  958.            a pagination request inside printf_filtered.  */
  959.         old_chain = make_cleanup (xfree, loc_string);

  960.         gdb_assert (osect->objfile && objfile_name (osect->objfile));
  961.         obj_name = objfile_name (osect->objfile);

  962.         if (MULTI_OBJFILE_P ())
  963.           if (pc_in_unmapped_range (addr, osect))
  964.             if (section_is_overlay (osect))
  965.               printf_filtered (_("%s in load address range of "
  966.                                  "%s overlay section %s of %s\n"),
  967.                                loc_string, mapped, sec_name, obj_name);
  968.             else
  969.               printf_filtered (_("%s in load address range of "
  970.                                  "section %s of %s\n"),
  971.                                loc_string, sec_name, obj_name);
  972.           else
  973.             if (section_is_overlay (osect))
  974.               printf_filtered (_("%s in %s overlay section %s of %s\n"),
  975.                                loc_string, mapped, sec_name, obj_name);
  976.             else
  977.               printf_filtered (_("%s in section %s of %s\n"),
  978.                                loc_string, sec_name, obj_name);
  979.         else
  980.           if (pc_in_unmapped_range (addr, osect))
  981.             if (section_is_overlay (osect))
  982.               printf_filtered (_("%s in load address range of %s overlay "
  983.                                  "section %s\n"),
  984.                                loc_string, mapped, sec_name);
  985.             else
  986.               printf_filtered (_("%s in load address range of section %s\n"),
  987.                                loc_string, sec_name);
  988.           else
  989.             if (section_is_overlay (osect))
  990.               printf_filtered (_("%s in %s overlay section %s\n"),
  991.                                loc_string, mapped, sec_name);
  992.             else
  993.               printf_filtered (_("%s in section %s\n"),
  994.                                loc_string, sec_name);

  995.         do_cleanups (old_chain);
  996.       }
  997.   }
  998.   if (matches == 0)
  999.     printf_filtered (_("No symbol matches %s.\n"), arg);
  1000. }

  1001. static void
  1002. address_info (char *exp, int from_tty)
  1003. {
  1004.   struct gdbarch *gdbarch;
  1005.   int regno;
  1006.   struct symbol *sym;
  1007.   struct bound_minimal_symbol msymbol;
  1008.   long val;
  1009.   struct obj_section *section;
  1010.   CORE_ADDR load_addr, context_pc = 0;
  1011.   struct field_of_this_result is_a_field_of_this;

  1012.   if (exp == 0)
  1013.     error (_("Argument required."));

  1014.   sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
  1015.                        &is_a_field_of_this);
  1016.   if (sym == NULL)
  1017.     {
  1018.       if (is_a_field_of_this.type != NULL)
  1019.         {
  1020.           printf_filtered ("Symbol \"");
  1021.           fprintf_symbol_filtered (gdb_stdout, exp,
  1022.                                    current_language->la_language, DMGL_ANSI);
  1023.           printf_filtered ("\" is a field of the local class variable ");
  1024.           if (current_language->la_language == language_objc)
  1025.             printf_filtered ("`self'\n");        /* ObjC equivalent of "this" */
  1026.           else
  1027.             printf_filtered ("`this'\n");
  1028.           return;
  1029.         }

  1030.       msymbol = lookup_bound_minimal_symbol (exp);

  1031.       if (msymbol.minsym != NULL)
  1032.         {
  1033.           struct objfile *objfile = msymbol.objfile;

  1034.           gdbarch = get_objfile_arch (objfile);
  1035.           load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);

  1036.           printf_filtered ("Symbol \"");
  1037.           fprintf_symbol_filtered (gdb_stdout, exp,
  1038.                                    current_language->la_language, DMGL_ANSI);
  1039.           printf_filtered ("\" is at ");
  1040.           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1041.           printf_filtered (" in a file compiled without debugging");
  1042.           section = MSYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
  1043.           if (section_is_overlay (section))
  1044.             {
  1045.               load_addr = overlay_unmapped_address (load_addr, section);
  1046.               printf_filtered (",\n -- loaded at ");
  1047.               fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1048.               printf_filtered (" in overlay section %s",
  1049.                                section->the_bfd_section->name);
  1050.             }
  1051.           printf_filtered (".\n");
  1052.         }
  1053.       else
  1054.         error (_("No symbol \"%s\" in current context."), exp);
  1055.       return;
  1056.     }

  1057.   printf_filtered ("Symbol \"");
  1058.   fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
  1059.                            current_language->la_language, DMGL_ANSI);
  1060.   printf_filtered ("\" is ");
  1061.   val = SYMBOL_VALUE (sym);
  1062.   if (SYMBOL_OBJFILE_OWNED (sym))
  1063.     section = SYMBOL_OBJ_SECTION (symbol_objfile (sym), sym);
  1064.   else
  1065.     section = NULL;
  1066.   gdbarch = symbol_arch (sym);

  1067.   if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  1068.     {
  1069.       SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
  1070.                                                     gdb_stdout);
  1071.       printf_filtered (".\n");
  1072.       return;
  1073.     }

  1074.   switch (SYMBOL_CLASS (sym))
  1075.     {
  1076.     case LOC_CONST:
  1077.     case LOC_CONST_BYTES:
  1078.       printf_filtered ("constant");
  1079.       break;

  1080.     case LOC_LABEL:
  1081.       printf_filtered ("a label at address ");
  1082.       load_addr = SYMBOL_VALUE_ADDRESS (sym);
  1083.       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1084.       if (section_is_overlay (section))
  1085.         {
  1086.           load_addr = overlay_unmapped_address (load_addr, section);
  1087.           printf_filtered (",\n -- loaded at ");
  1088.           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1089.           printf_filtered (" in overlay section %s",
  1090.                            section->the_bfd_section->name);
  1091.         }
  1092.       break;

  1093.     case LOC_COMPUTED:
  1094.       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));

  1095.     case LOC_REGISTER:
  1096.       /* GDBARCH is the architecture associated with the objfile the symbol
  1097.          is defined in; the target architecture may be different, and may
  1098.          provide additional registers.  However, we do not know the target
  1099.          architecture at this point.  We assume the objfile architecture
  1100.          will contain all the standard registers that occur in debug info
  1101.          in that objfile.  */
  1102.       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);

  1103.       if (SYMBOL_IS_ARGUMENT (sym))
  1104.         printf_filtered (_("an argument in register %s"),
  1105.                          gdbarch_register_name (gdbarch, regno));
  1106.       else
  1107.         printf_filtered (_("a variable in register %s"),
  1108.                          gdbarch_register_name (gdbarch, regno));
  1109.       break;

  1110.     case LOC_STATIC:
  1111.       printf_filtered (_("static storage at address "));
  1112.       load_addr = SYMBOL_VALUE_ADDRESS (sym);
  1113.       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1114.       if (section_is_overlay (section))
  1115.         {
  1116.           load_addr = overlay_unmapped_address (load_addr, section);
  1117.           printf_filtered (_(",\n -- loaded at "));
  1118.           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1119.           printf_filtered (_(" in overlay section %s"),
  1120.                            section->the_bfd_section->name);
  1121.         }
  1122.       break;

  1123.     case LOC_REGPARM_ADDR:
  1124.       /* Note comment at LOC_REGISTER.  */
  1125.       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
  1126.       printf_filtered (_("address of an argument in register %s"),
  1127.                        gdbarch_register_name (gdbarch, regno));
  1128.       break;

  1129.     case LOC_ARG:
  1130.       printf_filtered (_("an argument at offset %ld"), val);
  1131.       break;

  1132.     case LOC_LOCAL:
  1133.       printf_filtered (_("a local variable at frame offset %ld"), val);
  1134.       break;

  1135.     case LOC_REF_ARG:
  1136.       printf_filtered (_("a reference argument at offset %ld"), val);
  1137.       break;

  1138.     case LOC_TYPEDEF:
  1139.       printf_filtered (_("a typedef"));
  1140.       break;

  1141.     case LOC_BLOCK:
  1142.       printf_filtered (_("a function at address "));
  1143.       load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  1144.       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1145.       if (section_is_overlay (section))
  1146.         {
  1147.           load_addr = overlay_unmapped_address (load_addr, section);
  1148.           printf_filtered (_(",\n -- loaded at "));
  1149.           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1150.           printf_filtered (_(" in overlay section %s"),
  1151.                            section->the_bfd_section->name);
  1152.         }
  1153.       break;

  1154.     case LOC_UNRESOLVED:
  1155.       {
  1156.         struct bound_minimal_symbol msym;

  1157.         msym = lookup_minimal_symbol_and_objfile (SYMBOL_LINKAGE_NAME (sym));
  1158.         if (msym.minsym == NULL)
  1159.           printf_filtered ("unresolved");
  1160.         else
  1161.           {
  1162.             section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
  1163.             load_addr = BMSYMBOL_VALUE_ADDRESS (msym);

  1164.             if (section
  1165.                 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
  1166.               printf_filtered (_("a thread-local variable at offset %s "
  1167.                                  "in the thread-local storage for `%s'"),
  1168.                                paddress (gdbarch, load_addr),
  1169.                                objfile_name (section->objfile));
  1170.             else
  1171.               {
  1172.                 printf_filtered (_("static storage at address "));
  1173.                 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1174.                 if (section_is_overlay (section))
  1175.                   {
  1176.                     load_addr = overlay_unmapped_address (load_addr, section);
  1177.                     printf_filtered (_(",\n -- loaded at "));
  1178.                     fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
  1179.                     printf_filtered (_(" in overlay section %s"),
  1180.                                      section->the_bfd_section->name);
  1181.                   }
  1182.               }
  1183.           }
  1184.       }
  1185.       break;

  1186.     case LOC_OPTIMIZED_OUT:
  1187.       printf_filtered (_("optimized out"));
  1188.       break;

  1189.     default:
  1190.       printf_filtered (_("of unknown (botched) type"));
  1191.       break;
  1192.     }
  1193.   printf_filtered (".\n");
  1194. }


  1195. static void
  1196. x_command (char *exp, int from_tty)
  1197. {
  1198.   struct expression *expr;
  1199.   struct format_data fmt;
  1200.   struct cleanup *old_chain;
  1201.   struct value *val;

  1202.   fmt.format = last_format ? last_format : 'x';
  1203.   fmt.size = last_size;
  1204.   fmt.count = 1;
  1205.   fmt.raw = 0;

  1206.   if (exp && *exp == '/')
  1207.     {
  1208.       const char *tmp = exp + 1;

  1209.       fmt = decode_format (&tmp, last_format, last_size);
  1210.       exp = (char *) tmp;
  1211.     }

  1212.   /* If we have an expression, evaluate it and use it as the address.  */

  1213.   if (exp != 0 && *exp != 0)
  1214.     {
  1215.       expr = parse_expression (exp);
  1216.       /* Cause expression not to be there any more if this command is
  1217.          repeated with Newline.  But don't clobber a user-defined
  1218.          command's definition.  */
  1219.       if (from_tty)
  1220.         *exp = 0;
  1221.       old_chain = make_cleanup (free_current_contents, &expr);
  1222.       val = evaluate_expression (expr);
  1223.       if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
  1224.         val = coerce_ref (val);
  1225.       /* In rvalue contexts, such as this, functions are coerced into
  1226.          pointers to functions.  This makes "x/i main" work.  */
  1227.       if (/* last_format == 'i'  && */
  1228.           TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
  1229.            && VALUE_LVAL (val) == lval_memory)
  1230.         next_address = value_address (val);
  1231.       else
  1232.         next_address = value_as_address (val);

  1233.       next_gdbarch = expr->gdbarch;
  1234.       do_cleanups (old_chain);
  1235.     }

  1236.   if (!next_gdbarch)
  1237.     error_no_arg (_("starting display address"));

  1238.   do_examine (fmt, next_gdbarch, next_address);

  1239.   /* If the examine succeeds, we remember its size and format for next
  1240.      time.  Set last_size to 'b' for strings.  */
  1241.   if (fmt.format == 's')
  1242.     last_size = 'b';
  1243.   else
  1244.     last_size = fmt.size;
  1245.   last_format = fmt.format;

  1246.   /* Set a couple of internal variables if appropriate.  */
  1247.   if (last_examine_value)
  1248.     {
  1249.       /* Make last address examined available to the user as $_.  Use
  1250.          the correct pointer type.  */
  1251.       struct type *pointer_type
  1252.         = lookup_pointer_type (value_type (last_examine_value));
  1253.       set_internalvar (lookup_internalvar ("_"),
  1254.                        value_from_pointer (pointer_type,
  1255.                                            last_examine_address));

  1256.       /* Make contents of last address examined available to the user
  1257.          as $__.  If the last value has not been fetched from memory
  1258.          then don't fetch it now; instead mark it by voiding the $__
  1259.          variable.  */
  1260.       if (value_lazy (last_examine_value))
  1261.         clear_internalvar (lookup_internalvar ("__"));
  1262.       else
  1263.         set_internalvar (lookup_internalvar ("__"), last_examine_value);
  1264.     }
  1265. }


  1266. /* Add an expression to the auto-display chain.
  1267.    Specify the expression.  */

  1268. static void
  1269. display_command (char *arg, int from_tty)
  1270. {
  1271.   struct format_data fmt;
  1272.   struct expression *expr;
  1273.   struct display *new;
  1274.   int display_it = 1;
  1275.   const char *exp = arg;

  1276. #if defined(TUI)
  1277.   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
  1278.      `tui_version'.  */
  1279.   if (tui_active && exp != NULL && *exp == '$')
  1280.     display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
  1281. #endif

  1282.   if (display_it)
  1283.     {
  1284.       if (exp == 0)
  1285.         {
  1286.           do_displays ();
  1287.           return;
  1288.         }

  1289.       if (*exp == '/')
  1290.         {
  1291.           exp++;
  1292.           fmt = decode_format (&exp, 0, 0);
  1293.           if (fmt.size && fmt.format == 0)
  1294.             fmt.format = 'x';
  1295.           if (fmt.format == 'i' || fmt.format == 's')
  1296.             fmt.size = 'b';
  1297.         }
  1298.       else
  1299.         {
  1300.           fmt.format = 0;
  1301.           fmt.size = 0;
  1302.           fmt.count = 0;
  1303.           fmt.raw = 0;
  1304.         }

  1305.       innermost_block = NULL;
  1306.       expr = parse_expression (exp);

  1307.       new = (struct display *) xmalloc (sizeof (struct display));

  1308.       new->exp_string = xstrdup (exp);
  1309.       new->exp = expr;
  1310.       new->block = innermost_block;
  1311.       new->pspace = current_program_space;
  1312.       new->next = display_chain;
  1313.       new->number = ++display_number;
  1314.       new->format = fmt;
  1315.       new->enabled_p = 1;
  1316.       display_chain = new;

  1317.       if (from_tty)
  1318.         do_one_display (new);

  1319.       dont_repeat ();
  1320.     }
  1321. }

  1322. static void
  1323. free_display (struct display *d)
  1324. {
  1325.   xfree (d->exp_string);
  1326.   xfree (d->exp);
  1327.   xfree (d);
  1328. }

  1329. /* Clear out the display_chain.  Done when new symtabs are loaded,
  1330.    since this invalidates the types stored in many expressions.  */

  1331. void
  1332. clear_displays (void)
  1333. {
  1334.   struct display *d;

  1335.   while ((d = display_chain) != NULL)
  1336.     {
  1337.       display_chain = d->next;
  1338.       free_display (d);
  1339.     }
  1340. }

  1341. /* Delete the auto-display DISPLAY.  */

  1342. static void
  1343. delete_display (struct display *display)
  1344. {
  1345.   struct display *d;

  1346.   gdb_assert (display != NULL);

  1347.   if (display_chain == display)
  1348.     display_chain = display->next;

  1349.   ALL_DISPLAYS (d)
  1350.     if (d->next == display)
  1351.       {
  1352.         d->next = display->next;
  1353.         break;
  1354.       }

  1355.   free_display (display);
  1356. }

  1357. /* Call FUNCTION on each of the displays whose numbers are given in
  1358.    ARGS.  DATA is passed unmodified to FUNCTION.  */

  1359. static void
  1360. map_display_numbers (char *args,
  1361.                      void (*function) (struct display *,
  1362.                                        void *),
  1363.                      void *data)
  1364. {
  1365.   struct get_number_or_range_state state;
  1366.   int num;

  1367.   if (args == NULL)
  1368.     error_no_arg (_("one or more display numbers"));

  1369.   init_number_or_range (&state, args);

  1370.   while (!state.finished)
  1371.     {
  1372.       const char *p = state.string;

  1373.       num = get_number_or_range (&state);
  1374.       if (num == 0)
  1375.         warning (_("bad display number at or near '%s'"), p);
  1376.       else
  1377.         {
  1378.           struct display *d, *tmp;

  1379.           ALL_DISPLAYS_SAFE (d, tmp)
  1380.             if (d->number == num)
  1381.               break;
  1382.           if (d == NULL)
  1383.             printf_unfiltered (_("No display number %d.\n"), num);
  1384.           else
  1385.             function (d, data);
  1386.         }
  1387.     }
  1388. }

  1389. /* Callback for map_display_numbers, that deletes a display.  */

  1390. static void
  1391. do_delete_display (struct display *d, void *data)
  1392. {
  1393.   delete_display (d);
  1394. }

  1395. /* "undisplay" command.  */

  1396. static void
  1397. undisplay_command (char *args, int from_tty)
  1398. {
  1399.   if (args == NULL)
  1400.     {
  1401.       if (query (_("Delete all auto-display expressions? ")))
  1402.         clear_displays ();
  1403.       dont_repeat ();
  1404.       return;
  1405.     }

  1406.   map_display_numbers (args, do_delete_display, NULL);
  1407.   dont_repeat ();
  1408. }

  1409. /* Display a single auto-display.
  1410.    Do nothing if the display cannot be printed in the current context,
  1411.    or if the display is disabled.  */

  1412. static void
  1413. do_one_display (struct display *d)
  1414. {
  1415.   struct cleanup *old_chain;
  1416.   int within_current_scope;

  1417.   if (d->enabled_p == 0)
  1418.     return;

  1419.   /* The expression carries the architecture that was used at parse time.
  1420.      This is a problem if the expression depends on architecture features
  1421.      (e.g. register numbers), and the current architecture is now different.
  1422.      For example, a display statement like "display/i $pc" is expected to
  1423.      display the PC register of the current architecture, not the arch at
  1424.      the time the display command was given.  Therefore, we re-parse the
  1425.      expression if the current architecture has changed.  */
  1426.   if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
  1427.     {
  1428.       xfree (d->exp);
  1429.       d->exp = NULL;
  1430.       d->block = NULL;
  1431.     }

  1432.   if (d->exp == NULL)
  1433.     {
  1434.       volatile struct gdb_exception ex;

  1435.       TRY_CATCH (ex, RETURN_MASK_ALL)
  1436.         {
  1437.           innermost_block = NULL;
  1438.           d->exp = parse_expression (d->exp_string);
  1439.           d->block = innermost_block;
  1440.         }
  1441.       if (ex.reason < 0)
  1442.         {
  1443.           /* Can't re-parse the expression.  Disable this display item.  */
  1444.           d->enabled_p = 0;
  1445.           warning (_("Unable to display \"%s\": %s"),
  1446.                    d->exp_string, ex.message);
  1447.           return;
  1448.         }
  1449.     }

  1450.   if (d->block)
  1451.     {
  1452.       if (d->pspace == current_program_space)
  1453.         within_current_scope = contained_in (get_selected_block (0), d->block);
  1454.       else
  1455.         within_current_scope = 0;
  1456.     }
  1457.   else
  1458.     within_current_scope = 1;
  1459.   if (!within_current_scope)
  1460.     return;

  1461.   old_chain = make_cleanup_restore_integer (&current_display_number);
  1462.   current_display_number = d->number;

  1463.   annotate_display_begin ();
  1464.   printf_filtered ("%d", d->number);
  1465.   annotate_display_number_end ();
  1466.   printf_filtered (": ");
  1467.   if (d->format.size)
  1468.     {
  1469.       volatile struct gdb_exception ex;

  1470.       annotate_display_format ();

  1471.       printf_filtered ("x/");
  1472.       if (d->format.count != 1)
  1473.         printf_filtered ("%d", d->format.count);
  1474.       printf_filtered ("%c", d->format.format);
  1475.       if (d->format.format != 'i' && d->format.format != 's')
  1476.         printf_filtered ("%c", d->format.size);
  1477.       printf_filtered (" ");

  1478.       annotate_display_expression ();

  1479.       puts_filtered (d->exp_string);
  1480.       annotate_display_expression_end ();

  1481.       if (d->format.count != 1 || d->format.format == 'i')
  1482.         printf_filtered ("\n");
  1483.       else
  1484.         printf_filtered ("  ");

  1485.       annotate_display_value ();

  1486.       TRY_CATCH (ex, RETURN_MASK_ERROR)
  1487.         {
  1488.           struct value *val;
  1489.           CORE_ADDR addr;

  1490.           val = evaluate_expression (d->exp);
  1491.           addr = value_as_address (val);
  1492.           if (d->format.format == 'i')
  1493.             addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
  1494.           do_examine (d->format, d->exp->gdbarch, addr);
  1495.         }
  1496.       if (ex.reason < 0)
  1497.         fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
  1498.     }
  1499.   else
  1500.     {
  1501.       struct value_print_options opts;
  1502.       volatile struct gdb_exception ex;

  1503.       annotate_display_format ();

  1504.       if (d->format.format)
  1505.         printf_filtered ("/%c ", d->format.format);

  1506.       annotate_display_expression ();

  1507.       puts_filtered (d->exp_string);
  1508.       annotate_display_expression_end ();

  1509.       printf_filtered (" = ");

  1510.       annotate_display_expression ();

  1511.       get_formatted_print_options (&opts, d->format.format);
  1512.       opts.raw = d->format.raw;

  1513.       TRY_CATCH (ex, RETURN_MASK_ERROR)
  1514.         {
  1515.           struct value *val;

  1516.           val = evaluate_expression (d->exp);
  1517.           print_formatted (val, d->format.size, &opts, gdb_stdout);
  1518.         }
  1519.       if (ex.reason < 0)
  1520.         fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
  1521.       printf_filtered ("\n");
  1522.     }

  1523.   annotate_display_end ();

  1524.   gdb_flush (gdb_stdout);
  1525.   do_cleanups (old_chain);
  1526. }

  1527. /* Display all of the values on the auto-display chain which can be
  1528.    evaluated in the current scope.  */

  1529. void
  1530. do_displays (void)
  1531. {
  1532.   struct display *d;

  1533.   for (d = display_chain; d; d = d->next)
  1534.     do_one_display (d);
  1535. }

  1536. /* Delete the auto-display which we were in the process of displaying.
  1537.    This is done when there is an error or a signal.  */

  1538. void
  1539. disable_display (int num)
  1540. {
  1541.   struct display *d;

  1542.   for (d = display_chain; d; d = d->next)
  1543.     if (d->number == num)
  1544.       {
  1545.         d->enabled_p = 0;
  1546.         return;
  1547.       }
  1548.   printf_unfiltered (_("No display number %d.\n"), num);
  1549. }

  1550. void
  1551. disable_current_display (void)
  1552. {
  1553.   if (current_display_number >= 0)
  1554.     {
  1555.       disable_display (current_display_number);
  1556.       fprintf_unfiltered (gdb_stderr,
  1557.                           _("Disabling display %d to "
  1558.                             "avoid infinite recursion.\n"),
  1559.                           current_display_number);
  1560.     }
  1561.   current_display_number = -1;
  1562. }

  1563. static void
  1564. display_info (char *ignore, int from_tty)
  1565. {
  1566.   struct display *d;

  1567.   if (!display_chain)
  1568.     printf_unfiltered (_("There are no auto-display expressions now.\n"));
  1569.   else
  1570.     printf_filtered (_("Auto-display expressions now in effect:\n\
  1571. Num Enb Expression\n"));

  1572.   for (d = display_chain; d; d = d->next)
  1573.     {
  1574.       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
  1575.       if (d->format.size)
  1576.         printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
  1577.                          d->format.format);
  1578.       else if (d->format.format)
  1579.         printf_filtered ("/%c ", d->format.format);
  1580.       puts_filtered (d->exp_string);
  1581.       if (d->block && !contained_in (get_selected_block (0), d->block))
  1582.         printf_filtered (_(" (cannot be evaluated in the current context)"));
  1583.       printf_filtered ("\n");
  1584.       gdb_flush (gdb_stdout);
  1585.     }
  1586. }

  1587. /* Callback fo map_display_numbers, that enables or disables the
  1588.    passed in display D.  */

  1589. static void
  1590. do_enable_disable_display (struct display *d, void *data)
  1591. {
  1592.   d->enabled_p = *(int *) data;
  1593. }

  1594. /* Implamentation of both the "disable display" and "enable display"
  1595.    commands.  ENABLE decides what to do.  */

  1596. static void
  1597. enable_disable_display_command (char *args, int from_tty, int enable)
  1598. {
  1599.   if (args == NULL)
  1600.     {
  1601.       struct display *d;

  1602.       ALL_DISPLAYS (d)
  1603.         d->enabled_p = enable;
  1604.       return;
  1605.     }

  1606.   map_display_numbers (args, do_enable_disable_display, &enable);
  1607. }

  1608. /* The "enable display" command.  */

  1609. static void
  1610. enable_display_command (char *args, int from_tty)
  1611. {
  1612.   enable_disable_display_command (args, from_tty, 1);
  1613. }

  1614. /* The "disable display" command.  */

  1615. static void
  1616. disable_display_command (char *args, int from_tty)
  1617. {
  1618.   enable_disable_display_command (args, from_tty, 0);
  1619. }

  1620. /* display_chain items point to blocks and expressions.  Some expressions in
  1621.    turn may point to symbols.
  1622.    Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
  1623.    obstack_free'd when a shared library is unloaded.
  1624.    Clear pointers that are about to become dangling.
  1625.    Both .exp and .block fields will be restored next time we need to display
  1626.    an item by re-parsing .exp_string field in the new execution context.  */

  1627. static void
  1628. clear_dangling_display_expressions (struct objfile *objfile)
  1629. {
  1630.   struct display *d;
  1631.   struct program_space *pspace;

  1632.   /* With no symbol file we cannot have a block or expression from it.  */
  1633.   if (objfile == NULL)
  1634.     return;
  1635.   pspace = objfile->pspace;
  1636.   if (objfile->separate_debug_objfile_backlink)
  1637.     {
  1638.       objfile = objfile->separate_debug_objfile_backlink;
  1639.       gdb_assert (objfile->pspace == pspace);
  1640.     }

  1641.   for (d = display_chain; d != NULL; d = d->next)
  1642.     {
  1643.       if (d->pspace != pspace)
  1644.         continue;

  1645.       if (lookup_objfile_from_block (d->block) == objfile
  1646.           || (d->exp && exp_uses_objfile (d->exp, objfile)))
  1647.       {
  1648.         xfree (d->exp);
  1649.         d->exp = NULL;
  1650.         d->block = NULL;
  1651.       }
  1652.     }
  1653. }


  1654. /* Print the value in stack frame FRAME of a variable specified by a
  1655.    struct symbolNAME is the name to print; if NULL then VAR's print
  1656.    name will be used.  STREAM is the ui_file on which to print the
  1657.    value.  INDENT specifies the number of indent levels to print
  1658.    before printing the variable name.

  1659.    This function invalidates FRAME.  */

  1660. void
  1661. print_variable_and_value (const char *name, struct symbol *var,
  1662.                           struct frame_info *frame,
  1663.                           struct ui_file *stream, int indent)
  1664. {
  1665.   volatile struct gdb_exception except;

  1666.   if (!name)
  1667.     name = SYMBOL_PRINT_NAME (var);

  1668.   fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
  1669.   TRY_CATCH (except, RETURN_MASK_ERROR)
  1670.     {
  1671.       struct value *val;
  1672.       struct value_print_options opts;

  1673.       val = read_var_value (var, frame);
  1674.       get_user_print_options (&opts);
  1675.       opts.deref_ref = 1;
  1676.       common_val_print (val, stream, indent, &opts, current_language);

  1677.       /* common_val_print invalidates FRAME when a pretty printer calls inferior
  1678.          function.  */
  1679.       frame = NULL;
  1680.     }
  1681.   if (except.reason < 0)
  1682.     fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
  1683.                      except.message);
  1684.   fprintf_filtered (stream, "\n");
  1685. }

  1686. /* Subroutine of ui_printf to simplify it.
  1687.    Print VALUE to STREAM using FORMAT.
  1688.    VALUE is a C-style string on the target.  */

  1689. static void
  1690. printf_c_string (struct ui_file *stream, const char *format,
  1691.                  struct value *value)
  1692. {
  1693.   gdb_byte *str;
  1694.   CORE_ADDR tem;
  1695.   int j;

  1696.   tem = value_as_address (value);

  1697.   /* This is a %s argument.  Find the length of the string.  */
  1698.   for (j = 0;; j++)
  1699.     {
  1700.       gdb_byte c;

  1701.       QUIT;
  1702.       read_memory (tem + j, &c, 1);
  1703.       if (c == 0)
  1704.         break;
  1705.     }

  1706.   /* Copy the string contents into a string inside GDB.  */
  1707.   str = (gdb_byte *) alloca (j + 1);
  1708.   if (j != 0)
  1709.     read_memory (tem, str, j);
  1710.   str[j] = 0;

  1711.   fprintf_filtered (stream, format, (char *) str);
  1712. }

  1713. /* Subroutine of ui_printf to simplify it.
  1714.    Print VALUE to STREAM using FORMAT.
  1715.    VALUE is a wide C-style string on the target.  */

  1716. static void
  1717. printf_wide_c_string (struct ui_file *stream, const char *format,
  1718.                       struct value *value)
  1719. {
  1720.   gdb_byte *str;
  1721.   CORE_ADDR tem;
  1722.   int j;
  1723.   struct gdbarch *gdbarch = get_type_arch (value_type (value));
  1724.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1725.   struct type *wctype = lookup_typename (current_language, gdbarch,
  1726.                                          "wchar_t", NULL, 0);
  1727.   int wcwidth = TYPE_LENGTH (wctype);
  1728.   gdb_byte *buf = alloca (wcwidth);
  1729.   struct obstack output;
  1730.   struct cleanup *inner_cleanup;

  1731.   tem = value_as_address (value);

  1732.   /* This is a %s argument.  Find the length of the string.  */
  1733.   for (j = 0;; j += wcwidth)
  1734.     {
  1735.       QUIT;
  1736.       read_memory (tem + j, buf, wcwidth);
  1737.       if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
  1738.         break;
  1739.     }

  1740.   /* Copy the string contents into a string inside GDB.  */
  1741.   str = (gdb_byte *) alloca (j + wcwidth);
  1742.   if (j != 0)
  1743.     read_memory (tem, str, j);
  1744.   memset (&str[j], 0, wcwidth);

  1745.   obstack_init (&output);
  1746.   inner_cleanup = make_cleanup_obstack_free (&output);

  1747.   convert_between_encodings (target_wide_charset (gdbarch),
  1748.                              host_charset (),
  1749.                              str, j, wcwidth,
  1750.                              &output, translit_char);
  1751.   obstack_grow_str0 (&output, "");

  1752.   fprintf_filtered (stream, format, obstack_base (&output));
  1753.   do_cleanups (inner_cleanup);
  1754. }

  1755. /* Subroutine of ui_printf to simplify it.
  1756.    Print VALUE, a decimal floating point value, to STREAM using FORMAT.  */

  1757. static void
  1758. printf_decfloat (struct ui_file *stream, const char *format,
  1759.                  struct value *value)
  1760. {
  1761.   const gdb_byte *param_ptr = value_contents (value);

  1762. #if defined (PRINTF_HAS_DECFLOAT)
  1763.   /* If we have native support for Decimal floating
  1764.      printing, handle it here.  */
  1765.   fprintf_filtered (stream, format, param_ptr);
  1766. #else
  1767.   /* As a workaround until vasprintf has native support for DFP
  1768.      we convert the DFP values to string and print them using
  1769.      the %s format specifier.  */
  1770.   const char *p;

  1771.   /* Parameter data.  */
  1772.   struct type *param_type = value_type (value);
  1773.   struct gdbarch *gdbarch = get_type_arch (param_type);
  1774.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1775.   /* DFP output data.  */
  1776.   struct value *dfp_value = NULL;
  1777.   gdb_byte *dfp_ptr;
  1778.   int dfp_len = 16;
  1779.   gdb_byte dec[16];
  1780.   struct type *dfp_type = NULL;
  1781.   char decstr[MAX_DECIMAL_STRING];

  1782.   /* Points to the end of the string so that we can go back
  1783.      and check for DFP length modifiers.  */
  1784.   p = format + strlen (format);

  1785.   /* Look for the float/double format specifier.  */
  1786.   while (*p != 'f' && *p != 'e' && *p != 'E'
  1787.          && *p != 'g' && *p != 'G')
  1788.     p--;

  1789.   /* Search for the '%' char and extract the size and type of
  1790.      the output decimal value based on its modifiers
  1791.      (%Hf, %Df, %DDf).  */
  1792.   while (*--p != '%')
  1793.     {
  1794.       if (*p == 'H')
  1795.         {
  1796.           dfp_len = 4;
  1797.           dfp_type = builtin_type (gdbarch)->builtin_decfloat;
  1798.         }
  1799.       else if (*p == 'D' && *(p - 1) == 'D')
  1800.         {
  1801.           dfp_len = 16;
  1802.           dfp_type = builtin_type (gdbarch)->builtin_declong;
  1803.           p--;
  1804.         }
  1805.       else
  1806.         {
  1807.           dfp_len = 8;
  1808.           dfp_type = builtin_type (gdbarch)->builtin_decdouble;
  1809.         }
  1810.     }

  1811.   /* Conversion between different DFP types.  */
  1812.   if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
  1813.     decimal_convert (param_ptr, TYPE_LENGTH (param_type),
  1814.                      byte_order, dec, dfp_len, byte_order);
  1815.   else
  1816.     /* If this is a non-trivial conversion, just output 0.
  1817.        A correct converted value can be displayed by explicitly
  1818.        casting to a DFP type.  */
  1819.     decimal_from_string (dec, dfp_len, byte_order, "0");

  1820.   dfp_value = value_from_decfloat (dfp_type, dec);

  1821.   dfp_ptr = (gdb_byte *) value_contents (dfp_value);

  1822.   decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);

  1823.   /* Print the DFP value.  */
  1824.   fprintf_filtered (stream, "%s", decstr);
  1825. #endif
  1826. }

  1827. /* Subroutine of ui_printf to simplify it.
  1828.    Print VALUE, a target pointer, to STREAM using FORMAT.  */

  1829. static void
  1830. printf_pointer (struct ui_file *stream, const char *format,
  1831.                 struct value *value)
  1832. {
  1833.   /* We avoid the host's %p because pointers are too
  1834.      likely to be the wrong size.  The only interesting
  1835.      modifier for %p is a width; extract that, and then
  1836.      handle %p as glibc would: %#x or a literal "(nil)".  */

  1837.   const char *p;
  1838.   char *fmt, *fmt_p;
  1839. #ifdef PRINTF_HAS_LONG_LONG
  1840.   long long val = value_as_long (value);
  1841. #else
  1842.   long val = value_as_long (value);
  1843. #endif

  1844.   fmt = alloca (strlen (format) + 5);

  1845.   /* Copy up to the leading %.  */
  1846.   p = format;
  1847.   fmt_p = fmt;
  1848.   while (*p)
  1849.     {
  1850.       int is_percent = (*p == '%');

  1851.       *fmt_p++ = *p++;
  1852.       if (is_percent)
  1853.         {
  1854.           if (*p == '%')
  1855.             *fmt_p++ = *p++;
  1856.           else
  1857.             break;
  1858.         }
  1859.     }

  1860.   if (val != 0)
  1861.     *fmt_p++ = '#';

  1862.   /* Copy any width.  */
  1863.   while (*p >= '0' && *p < '9')
  1864.     *fmt_p++ = *p++;

  1865.   gdb_assert (*p == 'p' && *(p + 1) == '\0');
  1866.   if (val != 0)
  1867.     {
  1868. #ifdef PRINTF_HAS_LONG_LONG
  1869.       *fmt_p++ = 'l';
  1870. #endif
  1871.       *fmt_p++ = 'l';
  1872.       *fmt_p++ = 'x';
  1873.       *fmt_p++ = '\0';
  1874.       fprintf_filtered (stream, fmt, val);
  1875.     }
  1876.   else
  1877.     {
  1878.       *fmt_p++ = 's';
  1879.       *fmt_p++ = '\0';
  1880.       fprintf_filtered (stream, fmt, "(nil)");
  1881.     }
  1882. }

  1883. /* printf "printf format string" ARG to STREAM.  */

  1884. static void
  1885. ui_printf (const char *arg, struct ui_file *stream)
  1886. {
  1887.   struct format_piece *fpieces;
  1888.   const char *s = arg;
  1889.   struct value **val_args;
  1890.   int allocated_args = 20;
  1891.   struct cleanup *old_cleanups;

  1892.   val_args = xmalloc (allocated_args * sizeof (struct value *));
  1893.   old_cleanups = make_cleanup (free_current_contents, &val_args);

  1894.   if (s == 0)
  1895.     error_no_arg (_("format-control string and values to print"));

  1896.   s = skip_spaces_const (s);

  1897.   /* A format string should follow, enveloped in double quotes.  */
  1898.   if (*s++ != '"')
  1899.     error (_("Bad format string, missing '\"'."));

  1900.   fpieces = parse_format_string (&s);

  1901.   make_cleanup (free_format_pieces_cleanup, &fpieces);

  1902.   if (*s++ != '"')
  1903.     error (_("Bad format string, non-terminated '\"'."));

  1904.   s = skip_spaces_const (s);

  1905.   if (*s != ',' && *s != 0)
  1906.     error (_("Invalid argument syntax"));

  1907.   if (*s == ',')
  1908.     s++;
  1909.   s = skip_spaces_const (s);

  1910.   {
  1911.     int nargs = 0;
  1912.     int nargs_wanted;
  1913.     int i, fr;
  1914.     char *current_substring;

  1915.     nargs_wanted = 0;
  1916.     for (fr = 0; fpieces[fr].string != NULL; fr++)
  1917.       if (fpieces[fr].argclass != literal_piece)
  1918.         ++nargs_wanted;

  1919.     /* Now, parse all arguments and evaluate them.
  1920.        Store the VALUEs in VAL_ARGS.  */

  1921.     while (*s != '\0')
  1922.       {
  1923.         const char *s1;

  1924.         if (nargs == allocated_args)
  1925.           val_args = (struct value **) xrealloc ((char *) val_args,
  1926.                                                  (allocated_args *= 2)
  1927.                                                  * sizeof (struct value *));
  1928.         s1 = s;
  1929.         val_args[nargs] = parse_to_comma_and_eval (&s1);

  1930.         nargs++;
  1931.         s = s1;
  1932.         if (*s == ',')
  1933.           s++;
  1934.       }

  1935.     if (nargs != nargs_wanted)
  1936.       error (_("Wrong number of arguments for specified format-string"));

  1937.     /* Now actually print them.  */
  1938.     i = 0;
  1939.     for (fr = 0; fpieces[fr].string != NULL; fr++)
  1940.       {
  1941.         current_substring = fpieces[fr].string;
  1942.         switch (fpieces[fr].argclass)
  1943.           {
  1944.           case string_arg:
  1945.             printf_c_string (stream, current_substring, val_args[i]);
  1946.             break;
  1947.           case wide_string_arg:
  1948.             printf_wide_c_string (stream, current_substring, val_args[i]);
  1949.             break;
  1950.           case wide_char_arg:
  1951.             {
  1952.               struct gdbarch *gdbarch
  1953.                 = get_type_arch (value_type (val_args[i]));
  1954.               struct type *wctype = lookup_typename (current_language, gdbarch,
  1955.                                                      "wchar_t", NULL, 0);
  1956.               struct type *valtype;
  1957.               struct obstack output;
  1958.               struct cleanup *inner_cleanup;
  1959.               const gdb_byte *bytes;

  1960.               valtype = value_type (val_args[i]);
  1961.               if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
  1962.                   || TYPE_CODE (valtype) != TYPE_CODE_INT)
  1963.                 error (_("expected wchar_t argument for %%lc"));

  1964.               bytes = value_contents (val_args[i]);

  1965.               obstack_init (&output);
  1966.               inner_cleanup = make_cleanup_obstack_free (&output);

  1967.               convert_between_encodings (target_wide_charset (gdbarch),
  1968.                                          host_charset (),
  1969.                                          bytes, TYPE_LENGTH (valtype),
  1970.                                          TYPE_LENGTH (valtype),
  1971.                                          &output, translit_char);
  1972.               obstack_grow_str0 (&output, "");

  1973.               fprintf_filtered (stream, current_substring,
  1974.                                 obstack_base (&output));
  1975.               do_cleanups (inner_cleanup);
  1976.             }
  1977.             break;
  1978.           case double_arg:
  1979.             {
  1980.               struct type *type = value_type (val_args[i]);
  1981.               DOUBLEST val;
  1982.               int inv;

  1983.               /* If format string wants a float, unchecked-convert the value
  1984.                  to floating point of the same size.  */
  1985.               type = float_type_from_length (type);
  1986.               val = unpack_double (type, value_contents (val_args[i]), &inv);
  1987.               if (inv)
  1988.                 error (_("Invalid floating value found in program."));

  1989.               fprintf_filtered (stream, current_substring, (double) val);
  1990.               break;
  1991.             }
  1992.           case long_double_arg:
  1993. #ifdef HAVE_LONG_DOUBLE
  1994.             {
  1995.               struct type *type = value_type (val_args[i]);
  1996.               DOUBLEST val;
  1997.               int inv;

  1998.               /* If format string wants a float, unchecked-convert the value
  1999.                  to floating point of the same size.  */
  2000.               type = float_type_from_length (type);
  2001.               val = unpack_double (type, value_contents (val_args[i]), &inv);
  2002.               if (inv)
  2003.                 error (_("Invalid floating value found in program."));

  2004.               fprintf_filtered (stream, current_substring,
  2005.                                 (long double) val);
  2006.               break;
  2007.             }
  2008. #else
  2009.             error (_("long double not supported in printf"));
  2010. #endif
  2011.           case long_long_arg:
  2012. #ifdef PRINTF_HAS_LONG_LONG
  2013.             {
  2014.               long long val = value_as_long (val_args[i]);

  2015.               fprintf_filtered (stream, current_substring, val);
  2016.               break;
  2017.             }
  2018. #else
  2019.             error (_("long long not supported in printf"));
  2020. #endif
  2021.           case int_arg:
  2022.             {
  2023.               int val = value_as_long (val_args[i]);

  2024.               fprintf_filtered (stream, current_substring, val);
  2025.               break;
  2026.             }
  2027.           case long_arg:
  2028.             {
  2029.               long val = value_as_long (val_args[i]);

  2030.               fprintf_filtered (stream, current_substring, val);
  2031.               break;
  2032.             }
  2033.           /* Handles decimal floating values.  */
  2034.           case decfloat_arg:
  2035.             printf_decfloat (stream, current_substring, val_args[i]);
  2036.             break;
  2037.           case ptr_arg:
  2038.             printf_pointer (stream, current_substring, val_args[i]);
  2039.             break;
  2040.           case literal_piece:
  2041.             /* Print a portion of the format string that has no
  2042.                directives.  Note that this will not include any
  2043.                ordinary %-specs, but it might include "%%".  That is
  2044.                why we use printf_filtered and not puts_filtered here.
  2045.                Also, we pass a dummy argument because some platforms
  2046.                have modified GCC to include -Wformat-security by
  2047.                default, which will warn here if there is no
  2048.                argument.  */
  2049.             fprintf_filtered (stream, current_substring, 0);
  2050.             break;
  2051.           default:
  2052.             internal_error (__FILE__, __LINE__,
  2053.                             _("failed internal consistency check"));
  2054.           }
  2055.         /* Maybe advance to the next argument.  */
  2056.         if (fpieces[fr].argclass != literal_piece)
  2057.           ++i;
  2058.       }
  2059.   }
  2060.   do_cleanups (old_cleanups);
  2061. }

  2062. /* Implement the "printf" command.  */

  2063. static void
  2064. printf_command (char *arg, int from_tty)
  2065. {
  2066.   ui_printf (arg, gdb_stdout);
  2067.   gdb_flush (gdb_stdout);
  2068. }

  2069. /* Implement the "eval" command.  */

  2070. static void
  2071. eval_command (char *arg, int from_tty)
  2072. {
  2073.   struct ui_file *ui_out = mem_fileopen ();
  2074.   struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
  2075.   char *expanded;

  2076.   ui_printf (arg, ui_out);

  2077.   expanded = ui_file_xstrdup (ui_out, NULL);
  2078.   make_cleanup (xfree, expanded);

  2079.   execute_command (expanded, from_tty);

  2080.   do_cleanups (cleanups);
  2081. }

  2082. void
  2083. _initialize_printcmd (void)
  2084. {
  2085.   struct cmd_list_element *c;

  2086.   current_display_number = -1;

  2087.   observer_attach_free_objfile (clear_dangling_display_expressions);

  2088.   add_info ("address", address_info,
  2089.             _("Describe where symbol SYM is stored."));

  2090.   add_info ("symbol", sym_info, _("\
  2091. Describe what symbol is at location ADDR.\n\
  2092. Only for symbols with fixed locations (global or static scope)."));

  2093.   add_com ("x", class_vars, x_command, _("\
  2094. Examine memory: x/FMT ADDRESS.\n\
  2095. ADDRESS is an expression for the memory address to examine.\n\
  2096. FMT is a repeat count followed by a format letter and a size letter.\n\
  2097. Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
  2098.   t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
  2099.   and z(hex, zero padded on the left).\n\
  2100. Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
  2101. The specified number of objects of the specified size are printed\n\
  2102. according to the format.\n\n\
  2103. Defaults for format and size letters are those previously used.\n\
  2104. Default count is 1.  Default address is following last thing printed\n\
  2105. with this command or \"print\"."));

  2106. #if 0
  2107.   add_com ("whereis", class_vars, whereis_command,
  2108.            _("Print line number and file of definition of variable."));
  2109. #endif

  2110.   add_info ("display", display_info, _("\
  2111. Expressions to display when program stops, with code numbers."));

  2112.   add_cmd ("undisplay", class_vars, undisplay_command, _("\
  2113. Cancel some expressions to be displayed when program stops.\n\
  2114. Arguments are the code numbers of the expressions to stop displaying.\n\
  2115. No argument means cancel all automatic-display expressions.\n\
  2116. \"delete display\" has the same effect as this command.\n\
  2117. Do \"info display\" to see current list of code numbers."),
  2118.            &cmdlist);

  2119.   add_com ("display", class_vars, display_command, _("\
  2120. Print value of expression EXP each time the program stops.\n\
  2121. /FMT may be used before EXP as in the \"print\" command.\n\
  2122. /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
  2123. as in the \"x\" command, and then EXP is used to get the address to examine\n\
  2124. and examining is done as in the \"x\" command.\n\n\
  2125. With no argument, display all currently requested auto-display expressions.\n\
  2126. Use \"undisplay\" to cancel display requests previously made."));

  2127.   add_cmd ("display", class_vars, enable_display_command, _("\
  2128. Enable some expressions to be displayed when program stops.\n\
  2129. Arguments are the code numbers of the expressions to resume displaying.\n\
  2130. No argument means enable all automatic-display expressions.\n\
  2131. Do \"info display\" to see current list of code numbers."), &enablelist);

  2132.   add_cmd ("display", class_vars, disable_display_command, _("\
  2133. Disable some expressions to be displayed when program stops.\n\
  2134. Arguments are the code numbers of the expressions to stop displaying.\n\
  2135. No argument means disable all automatic-display expressions.\n\
  2136. Do \"info display\" to see current list of code numbers."), &disablelist);

  2137.   add_cmd ("display", class_vars, undisplay_command, _("\
  2138. Cancel some expressions to be displayed when program stops.\n\
  2139. Arguments are the code numbers of the expressions to stop displaying.\n\
  2140. No argument means cancel all automatic-display expressions.\n\
  2141. Do \"info display\" to see current list of code numbers."), &deletelist);

  2142.   add_com ("printf", class_vars, printf_command, _("\
  2143. printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
  2144. This is useful for formatted output in user-defined commands."));

  2145.   add_com ("output", class_vars, output_command, _("\
  2146. Like \"print\" but don't put in value history and don't print newline.\n\
  2147. This is useful in user-defined commands."));

  2148.   add_prefix_cmd ("set", class_vars, set_command, _("\
  2149. Evaluate expression EXP and assign result to variable VAR, using assignment\n\
  2150. syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
  2151. example).  VAR may be a debugger \"convenience\" variable (names starting\n\
  2152. with $), a register (a few standard names starting with $), or an actual\n\
  2153. variable in the program being debugged.  EXP is any valid expression.\n\
  2154. Use \"set variable\" for variables with names identical to set subcommands.\n\
  2155. \n\
  2156. With a subcommand, this command modifies parts of the gdb environment.\n\
  2157. You can see these environment settings with the \"show\" command."),
  2158.                   &setlist, "set ", 1, &cmdlist);
  2159.   if (dbx_commands)
  2160.     add_com ("assign", class_vars, set_command, _("\
  2161. Evaluate expression EXP and assign result to variable VAR, using assignment\n\
  2162. syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
  2163. example).  VAR may be a debugger \"convenience\" variable (names starting\n\
  2164. with $), a register (a few standard names starting with $), or an actual\n\
  2165. variable in the program being debugged.  EXP is any valid expression.\n\
  2166. Use \"set variable\" for variables with names identical to set subcommands.\n\
  2167. \nWith a subcommand, this command modifies parts of the gdb environment.\n\
  2168. You can see these environment settings with the \"show\" command."));

  2169.   /* "call" is the same as "set", but handy for dbx users to call fns.  */
  2170.   c = add_com ("call", class_vars, call_command, _("\
  2171. Call a function in the program.\n\
  2172. The argument is the function name and arguments, in the notation of the\n\
  2173. current working language.  The result is printed and saved in the value\n\
  2174. history, if it is not void."));
  2175.   set_cmd_completer (c, expression_completer);

  2176.   add_cmd ("variable", class_vars, set_command, _("\
  2177. Evaluate expression EXP and assign result to variable VAR, using assignment\n\
  2178. syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
  2179. example).  VAR may be a debugger \"convenience\" variable (names starting\n\
  2180. with $), a register (a few standard names starting with $), or an actual\n\
  2181. variable in the program being debugged.  EXP is any valid expression.\n\
  2182. This may usually be abbreviated to simply \"set\"."),
  2183.            &setlist);

  2184.   c = add_com ("print", class_vars, print_command, _("\
  2185. Print value of expression EXP.\n\
  2186. Variables accessible are those of the lexical environment of the selected\n\
  2187. stack frame, plus all those whose scope is global or an entire file.\n\
  2188. \n\
  2189. $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
  2190. $$NUM refers to NUM'th value back from the last one.\n\
  2191. Names starting with $ refer to registers (with the values they would have\n\
  2192. if the program were to return to the stack frame now selected, restoring\n\
  2193. all registers saved by frames farther in) or else to debugger\n\
  2194. \"convenience\" variables (any such name not a known register).\n\
  2195. Use assignment expressions to give values to convenience variables.\n\
  2196. \n\
  2197. {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
  2198. @ is a binary operator for treating consecutive data objects\n\
  2199. anywhere in memory as an arrayFOO@NUM gives an array whose first\n\
  2200. element is FOO, whose second element is stored in the space following\n\
  2201. where FOO is stored, etc.  FOO must be an expression whose value\n\
  2202. resides in memory.\n\
  2203. \n\
  2204. EXP may be preceded with /FMT, where FMT is a format letter\n\
  2205. but no count or size letter (see \"x\" command)."));
  2206.   set_cmd_completer (c, expression_completer);
  2207.   add_com_alias ("p", "print", class_vars, 1);
  2208.   add_com_alias ("inspect", "print", class_vars, 1);

  2209.   add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
  2210.                             &max_symbolic_offset, _("\
  2211. Set the largest offset that will be printed in <symbol+1234> form."), _("\
  2212. Show the largest offset that will be printed in <symbol+1234> form."), _("\
  2213. Tell GDB to only display the symbolic form of an address if the\n\
  2214. offset between the closest earlier symbol and the address is less than\n\
  2215. the specified maximum offset.  The default is \"unlimited\", which tells GDB\n\
  2216. to always print the symbolic form of an address if any symbol precedes\n\
  2217. it.  Zero is equivalent to \"unlimited\"."),
  2218.                             NULL,
  2219.                             show_max_symbolic_offset,
  2220.                             &setprintlist, &showprintlist);
  2221.   add_setshow_boolean_cmd ("symbol-filename", no_class,
  2222.                            &print_symbol_filename, _("\
  2223. Set printing of source filename and line number with <symbol>."), _("\
  2224. Show printing of source filename and line number with <symbol>."), NULL,
  2225.                            NULL,
  2226.                            show_print_symbol_filename,
  2227.                            &setprintlist, &showprintlist);

  2228.   add_com ("eval", no_class, eval_command, _("\
  2229. Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
  2230. a command line, and call it."));
  2231. }