Home | History | Annotate | Line # | Download | only in gdb
printcmd.c revision 1.1.1.2
      1      1.1  christos /* Print values for GNU debugger GDB.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 1986-2015 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "defs.h"
     21      1.1  christos #include "frame.h"
     22      1.1  christos #include "symtab.h"
     23      1.1  christos #include "gdbtypes.h"
     24      1.1  christos #include "value.h"
     25      1.1  christos #include "language.h"
     26      1.1  christos #include "expression.h"
     27      1.1  christos #include "gdbcore.h"
     28      1.1  christos #include "gdbcmd.h"
     29      1.1  christos #include "target.h"
     30      1.1  christos #include "breakpoint.h"
     31      1.1  christos #include "demangle.h"
     32      1.1  christos #include "gdb-demangle.h"
     33      1.1  christos #include "valprint.h"
     34      1.1  christos #include "annotate.h"
     35      1.1  christos #include "symfile.h"		/* for overlay functions */
     36      1.1  christos #include "objfiles.h"		/* ditto */
     37      1.1  christos #include "completer.h"		/* for completion functions */
     38      1.1  christos #include "ui-out.h"
     39      1.1  christos #include "block.h"
     40      1.1  christos #include "disasm.h"
     41      1.1  christos #include "dfp.h"
     42      1.1  christos #include "observer.h"
     43      1.1  christos #include "solist.h"
     44      1.1  christos #include "parser-defs.h"
     45      1.1  christos #include "charset.h"
     46      1.1  christos #include "arch-utils.h"
     47      1.1  christos #include "cli/cli-utils.h"
     48      1.1  christos #include "format.h"
     49      1.1  christos #include "source.h"
     50      1.1  christos 
     51      1.1  christos #ifdef TUI
     52      1.1  christos #include "tui/tui.h"		/* For tui_active et al.   */
     53      1.1  christos #endif
     54      1.1  christos 
     55      1.1  christos struct format_data
     56      1.1  christos   {
     57      1.1  christos     int count;
     58      1.1  christos     char format;
     59      1.1  christos     char size;
     60      1.1  christos 
     61      1.1  christos     /* True if the value should be printed raw -- that is, bypassing
     62      1.1  christos        python-based formatters.  */
     63      1.1  christos     unsigned char raw;
     64      1.1  christos   };
     65      1.1  christos 
     66      1.1  christos /* Last specified output format.  */
     67      1.1  christos 
     68      1.1  christos static char last_format = 0;
     69      1.1  christos 
     70      1.1  christos /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
     71      1.1  christos 
     72      1.1  christos static char last_size = 'w';
     73      1.1  christos 
     74      1.1  christos /* Default address to examine next, and associated architecture.  */
     75      1.1  christos 
     76      1.1  christos static struct gdbarch *next_gdbarch;
     77      1.1  christos static CORE_ADDR next_address;
     78      1.1  christos 
     79      1.1  christos /* Number of delay instructions following current disassembled insn.  */
     80      1.1  christos 
     81      1.1  christos static int branch_delay_insns;
     82      1.1  christos 
     83      1.1  christos /* Last address examined.  */
     84      1.1  christos 
     85      1.1  christos static CORE_ADDR last_examine_address;
     86      1.1  christos 
     87      1.1  christos /* Contents of last address examined.
     88      1.1  christos    This is not valid past the end of the `x' command!  */
     89      1.1  christos 
     90      1.1  christos static struct value *last_examine_value;
     91      1.1  christos 
     92      1.1  christos /* Largest offset between a symbolic value and an address, that will be
     93      1.1  christos    printed as `0x1234 <symbol+offset>'.  */
     94      1.1  christos 
     95      1.1  christos static unsigned int max_symbolic_offset = UINT_MAX;
     96      1.1  christos static void
     97      1.1  christos show_max_symbolic_offset (struct ui_file *file, int from_tty,
     98      1.1  christos 			  struct cmd_list_element *c, const char *value)
     99      1.1  christos {
    100      1.1  christos   fprintf_filtered (file,
    101      1.1  christos 		    _("The largest offset that will be "
    102      1.1  christos 		      "printed in <symbol+1234> form is %s.\n"),
    103      1.1  christos 		    value);
    104      1.1  christos }
    105      1.1  christos 
    106      1.1  christos /* Append the source filename and linenumber of the symbol when
    107      1.1  christos    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
    108      1.1  christos static int print_symbol_filename = 0;
    109      1.1  christos static void
    110      1.1  christos show_print_symbol_filename (struct ui_file *file, int from_tty,
    111      1.1  christos 			    struct cmd_list_element *c, const char *value)
    112      1.1  christos {
    113      1.1  christos   fprintf_filtered (file, _("Printing of source filename and "
    114      1.1  christos 			    "line number with <symbol> is %s.\n"),
    115      1.1  christos 		    value);
    116      1.1  christos }
    117      1.1  christos 
    118      1.1  christos /* Number of auto-display expression currently being displayed.
    119      1.1  christos    So that we can disable it if we get a signal within it.
    120      1.1  christos    -1 when not doing one.  */
    121      1.1  christos 
    122      1.1  christos static int current_display_number;
    123      1.1  christos 
    124      1.1  christos struct display
    125      1.1  christos   {
    126      1.1  christos     /* Chain link to next auto-display item.  */
    127      1.1  christos     struct display *next;
    128      1.1  christos 
    129      1.1  christos     /* The expression as the user typed it.  */
    130      1.1  christos     char *exp_string;
    131      1.1  christos 
    132      1.1  christos     /* Expression to be evaluated and displayed.  */
    133      1.1  christos     struct expression *exp;
    134      1.1  christos 
    135      1.1  christos     /* Item number of this auto-display item.  */
    136      1.1  christos     int number;
    137      1.1  christos 
    138      1.1  christos     /* Display format specified.  */
    139      1.1  christos     struct format_data format;
    140      1.1  christos 
    141      1.1  christos     /* Program space associated with `block'.  */
    142      1.1  christos     struct program_space *pspace;
    143      1.1  christos 
    144      1.1  christos     /* Innermost block required by this expression when evaluated.  */
    145      1.1  christos     const struct block *block;
    146      1.1  christos 
    147      1.1  christos     /* Status of this display (enabled or disabled).  */
    148      1.1  christos     int enabled_p;
    149      1.1  christos   };
    150      1.1  christos 
    151      1.1  christos /* Chain of expressions whose values should be displayed
    152      1.1  christos    automatically each time the program stops.  */
    153      1.1  christos 
    154      1.1  christos static struct display *display_chain;
    155      1.1  christos 
    156      1.1  christos static int display_number;
    157      1.1  christos 
    158      1.1  christos /* Walk the following statement or block through all displays.
    159      1.1  christos    ALL_DISPLAYS_SAFE does so even if the statement deletes the current
    160      1.1  christos    display.  */
    161      1.1  christos 
    162      1.1  christos #define ALL_DISPLAYS(B)				\
    163      1.1  christos   for (B = display_chain; B; B = B->next)
    164      1.1  christos 
    165      1.1  christos #define ALL_DISPLAYS_SAFE(B,TMP)		\
    166      1.1  christos   for (B = display_chain;			\
    167      1.1  christos        B ? (TMP = B->next, 1): 0;		\
    168      1.1  christos        B = TMP)
    169      1.1  christos 
    170      1.1  christos /* Prototypes for exported functions.  */
    171      1.1  christos 
    172      1.1  christos void _initialize_printcmd (void);
    173      1.1  christos 
    174      1.1  christos /* Prototypes for local functions.  */
    175      1.1  christos 
    176      1.1  christos static void do_one_display (struct display *);
    177      1.1  christos 
    178      1.1  christos 
    180      1.1  christos /* Decode a format specification.  *STRING_PTR should point to it.
    181      1.1  christos    OFORMAT and OSIZE are used as defaults for the format and size
    182      1.1  christos    if none are given in the format specification.
    183      1.1  christos    If OSIZE is zero, then the size field of the returned value
    184      1.1  christos    should be set only if a size is explicitly specified by the
    185      1.1  christos    user.
    186      1.1  christos    The structure returned describes all the data
    187      1.1  christos    found in the specification.  In addition, *STRING_PTR is advanced
    188      1.1  christos    past the specification and past all whitespace following it.  */
    189      1.1  christos 
    190      1.1  christos static struct format_data
    191      1.1  christos decode_format (const char **string_ptr, int oformat, int osize)
    192      1.1  christos {
    193      1.1  christos   struct format_data val;
    194      1.1  christos   const char *p = *string_ptr;
    195      1.1  christos 
    196      1.1  christos   val.format = '?';
    197      1.1  christos   val.size = '?';
    198      1.1  christos   val.count = 1;
    199      1.1  christos   val.raw = 0;
    200      1.1  christos 
    201      1.1  christos   if (*p >= '0' && *p <= '9')
    202      1.1  christos     val.count = atoi (p);
    203      1.1  christos   while (*p >= '0' && *p <= '9')
    204      1.1  christos     p++;
    205      1.1  christos 
    206      1.1  christos   /* Now process size or format letters that follow.  */
    207      1.1  christos 
    208      1.1  christos   while (1)
    209      1.1  christos     {
    210      1.1  christos       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
    211      1.1  christos 	val.size = *p++;
    212      1.1  christos       else if (*p == 'r')
    213      1.1  christos 	{
    214      1.1  christos 	  val.raw = 1;
    215      1.1  christos 	  p++;
    216      1.1  christos 	}
    217      1.1  christos       else if (*p >= 'a' && *p <= 'z')
    218      1.1  christos 	val.format = *p++;
    219      1.1  christos       else
    220      1.1  christos 	break;
    221      1.1  christos     }
    222      1.1  christos 
    223      1.1  christos   while (*p == ' ' || *p == '\t')
    224      1.1  christos     p++;
    225      1.1  christos   *string_ptr = p;
    226      1.1  christos 
    227      1.1  christos   /* Set defaults for format and size if not specified.  */
    228      1.1  christos   if (val.format == '?')
    229      1.1  christos     {
    230      1.1  christos       if (val.size == '?')
    231      1.1  christos 	{
    232      1.1  christos 	  /* Neither has been specified.  */
    233      1.1  christos 	  val.format = oformat;
    234      1.1  christos 	  val.size = osize;
    235      1.1  christos 	}
    236      1.1  christos       else
    237      1.1  christos 	/* If a size is specified, any format makes a reasonable
    238      1.1  christos 	   default except 'i'.  */
    239      1.1  christos 	val.format = oformat == 'i' ? 'x' : oformat;
    240      1.1  christos     }
    241      1.1  christos   else if (val.size == '?')
    242      1.1  christos     switch (val.format)
    243      1.1  christos       {
    244      1.1  christos       case 'a':
    245      1.1  christos 	/* Pick the appropriate size for an address.  This is deferred
    246      1.1  christos 	   until do_examine when we know the actual architecture to use.
    247      1.1  christos 	   A special size value of 'a' is used to indicate this case.  */
    248      1.1  christos 	val.size = osize ? 'a' : osize;
    249      1.1  christos 	break;
    250      1.1  christos       case 'f':
    251      1.1  christos 	/* Floating point has to be word or giantword.  */
    252      1.1  christos 	if (osize == 'w' || osize == 'g')
    253      1.1  christos 	  val.size = osize;
    254      1.1  christos 	else
    255      1.1  christos 	  /* Default it to giantword if the last used size is not
    256      1.1  christos 	     appropriate.  */
    257      1.1  christos 	  val.size = osize ? 'g' : osize;
    258      1.1  christos 	break;
    259      1.1  christos       case 'c':
    260      1.1  christos 	/* Characters default to one byte.  */
    261      1.1  christos 	val.size = osize ? 'b' : osize;
    262      1.1  christos 	break;
    263      1.1  christos       case 's':
    264      1.1  christos 	/* Display strings with byte size chars unless explicitly
    265      1.1  christos 	   specified.  */
    266      1.1  christos 	val.size = '\0';
    267      1.1  christos 	break;
    268      1.1  christos 
    269      1.1  christos       default:
    270      1.1  christos 	/* The default is the size most recently specified.  */
    271      1.1  christos 	val.size = osize;
    272      1.1  christos       }
    273      1.1  christos 
    274      1.1  christos   return val;
    275      1.1  christos }
    276      1.1  christos 
    277      1.1  christos /* Print value VAL on stream according to OPTIONS.
    279      1.1  christos    Do not end with a newline.
    280      1.1  christos    SIZE is the letter for the size of datum being printed.
    281      1.1  christos    This is used to pad hex numbers so they line up.  SIZE is 0
    282      1.1  christos    for print / output and set for examine.  */
    283      1.1  christos 
    284      1.1  christos static void
    285      1.1  christos print_formatted (struct value *val, int size,
    286      1.1  christos 		 const struct value_print_options *options,
    287      1.1  christos 		 struct ui_file *stream)
    288      1.1  christos {
    289      1.1  christos   struct type *type = check_typedef (value_type (val));
    290      1.1  christos   int len = TYPE_LENGTH (type);
    291      1.1  christos 
    292      1.1  christos   if (VALUE_LVAL (val) == lval_memory)
    293      1.1  christos     next_address = value_address (val) + len;
    294      1.1  christos 
    295      1.1  christos   if (size)
    296      1.1  christos     {
    297      1.1  christos       switch (options->format)
    298      1.1  christos 	{
    299      1.1  christos 	case 's':
    300      1.1  christos 	  {
    301      1.1  christos 	    struct type *elttype = value_type (val);
    302      1.1  christos 
    303      1.1  christos 	    next_address = (value_address (val)
    304      1.1  christos 			    + val_print_string (elttype, NULL,
    305      1.1  christos 						value_address (val), -1,
    306      1.1  christos 						stream, options) * len);
    307      1.1  christos 	  }
    308      1.1  christos 	  return;
    309      1.1  christos 
    310      1.1  christos 	case 'i':
    311      1.1  christos 	  /* We often wrap here if there are long symbolic names.  */
    312      1.1  christos 	  wrap_here ("    ");
    313      1.1  christos 	  next_address = (value_address (val)
    314      1.1  christos 			  + gdb_print_insn (get_type_arch (type),
    315      1.1  christos 					    value_address (val), stream,
    316      1.1  christos 					    &branch_delay_insns));
    317      1.1  christos 	  return;
    318      1.1  christos 	}
    319      1.1  christos     }
    320      1.1  christos 
    321      1.1  christos   if (options->format == 0 || options->format == 's'
    322      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_REF
    323      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_ARRAY
    324      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_STRING
    325      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_STRUCT
    326      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_UNION
    327      1.1  christos       || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
    328      1.1  christos     value_print (val, stream, options);
    329      1.1  christos   else
    330      1.1  christos     /* User specified format, so don't look to the type to tell us
    331      1.1  christos        what to do.  */
    332      1.1  christos     val_print_scalar_formatted (type,
    333      1.1  christos 				value_contents_for_printing (val),
    334      1.1  christos 				value_embedded_offset (val),
    335      1.1  christos 				val,
    336      1.1  christos 				options, size, stream);
    337      1.1  christos }
    338      1.1  christos 
    339      1.1  christos /* Return builtin floating point type of same length as TYPE.
    340      1.1  christos    If no such type is found, return TYPE itself.  */
    341      1.1  christos static struct type *
    342      1.1  christos float_type_from_length (struct type *type)
    343      1.1  christos {
    344      1.1  christos   struct gdbarch *gdbarch = get_type_arch (type);
    345      1.1  christos   const struct builtin_type *builtin = builtin_type (gdbarch);
    346      1.1  christos 
    347      1.1  christos   if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
    348      1.1  christos     type = builtin->builtin_float;
    349      1.1  christos   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
    350      1.1  christos     type = builtin->builtin_double;
    351      1.1  christos   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
    352      1.1  christos     type = builtin->builtin_long_double;
    353      1.1  christos 
    354      1.1  christos   return type;
    355      1.1  christos }
    356      1.1  christos 
    357      1.1  christos /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
    358      1.1  christos    according to OPTIONS and SIZE on STREAM.  Formats s and i are not
    359      1.1  christos    supported at this level.  */
    360      1.1  christos 
    361      1.1  christos void
    362      1.1  christos print_scalar_formatted (const void *valaddr, struct type *type,
    363      1.1  christos 			const struct value_print_options *options,
    364      1.1  christos 			int size, struct ui_file *stream)
    365      1.1  christos {
    366      1.1  christos   struct gdbarch *gdbarch = get_type_arch (type);
    367      1.1  christos   LONGEST val_long = 0;
    368      1.1  christos   unsigned int len = TYPE_LENGTH (type);
    369      1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    370      1.1  christos 
    371      1.1  christos   /* String printing should go through val_print_scalar_formatted.  */
    372      1.1  christos   gdb_assert (options->format != 's');
    373      1.1  christos 
    374      1.1  christos   if (len > sizeof(LONGEST) &&
    375      1.1  christos       (TYPE_CODE (type) == TYPE_CODE_INT
    376      1.1  christos        || TYPE_CODE (type) == TYPE_CODE_ENUM))
    377      1.1  christos     {
    378      1.1  christos       switch (options->format)
    379      1.1  christos 	{
    380      1.1  christos 	case 'o':
    381      1.1  christos 	  print_octal_chars (stream, valaddr, len, byte_order);
    382      1.1  christos 	  return;
    383      1.1  christos 	case 'u':
    384      1.1  christos 	case 'd':
    385      1.1  christos 	  print_decimal_chars (stream, valaddr, len, byte_order);
    386      1.1  christos 	  return;
    387      1.1  christos 	case 't':
    388      1.1  christos 	  print_binary_chars (stream, valaddr, len, byte_order);
    389      1.1  christos 	  return;
    390      1.1  christos 	case 'x':
    391      1.1  christos 	  print_hex_chars (stream, valaddr, len, byte_order);
    392      1.1  christos 	  return;
    393      1.1  christos 	case 'c':
    394      1.1  christos 	  print_char_chars (stream, type, valaddr, len, byte_order);
    395      1.1  christos 	  return;
    396      1.1  christos 	default:
    397      1.1  christos 	  break;
    398      1.1  christos 	};
    399      1.1  christos     }
    400      1.1  christos 
    401      1.1  christos   if (options->format != 'f')
    402      1.1  christos     val_long = unpack_long (type, valaddr);
    403      1.1  christos 
    404      1.1  christos   /* If the value is a pointer, and pointers and addresses are not the
    405      1.1  christos      same, then at this point, the value's length (in target bytes) is
    406      1.1  christos      gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
    407      1.1  christos   if (TYPE_CODE (type) == TYPE_CODE_PTR)
    408      1.1  christos     len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
    409      1.1  christos 
    410      1.1  christos   /* If we are printing it as unsigned, truncate it in case it is actually
    411      1.1  christos      a negative signed value (e.g. "print/u (short)-1" should print 65535
    412      1.1  christos      (if shorts are 16 bits) instead of 4294967295).  */
    413      1.1  christos   if (options->format != 'd' || TYPE_UNSIGNED (type))
    414      1.1  christos     {
    415      1.1  christos       if (len < sizeof (LONGEST))
    416      1.1  christos 	val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
    417      1.1  christos     }
    418      1.1  christos 
    419      1.1  christos   switch (options->format)
    420      1.1  christos     {
    421      1.1  christos     case 'x':
    422      1.1  christos       if (!size)
    423      1.1  christos 	{
    424      1.1  christos 	  /* No size specified, like in print.  Print varying # of digits.  */
    425      1.1  christos 	  print_longest (stream, 'x', 1, val_long);
    426      1.1  christos 	}
    427      1.1  christos       else
    428      1.1  christos 	switch (size)
    429      1.1  christos 	  {
    430      1.1  christos 	  case 'b':
    431      1.1  christos 	  case 'h':
    432      1.1  christos 	  case 'w':
    433      1.1  christos 	  case 'g':
    434      1.1  christos 	    print_longest (stream, size, 1, val_long);
    435      1.1  christos 	    break;
    436      1.1  christos 	  default:
    437      1.1  christos 	    error (_("Undefined output size \"%c\"."), size);
    438      1.1  christos 	  }
    439      1.1  christos       break;
    440      1.1  christos 
    441      1.1  christos     case 'd':
    442      1.1  christos       print_longest (stream, 'd', 1, val_long);
    443      1.1  christos       break;
    444      1.1  christos 
    445      1.1  christos     case 'u':
    446      1.1  christos       print_longest (stream, 'u', 0, val_long);
    447      1.1  christos       break;
    448      1.1  christos 
    449      1.1  christos     case 'o':
    450      1.1  christos       if (val_long)
    451      1.1  christos 	print_longest (stream, 'o', 1, val_long);
    452      1.1  christos       else
    453      1.1  christos 	fprintf_filtered (stream, "0");
    454      1.1  christos       break;
    455      1.1  christos 
    456      1.1  christos     case 'a':
    457      1.1  christos       {
    458      1.1  christos 	CORE_ADDR addr = unpack_pointer (type, valaddr);
    459      1.1  christos 
    460      1.1  christos 	print_address (gdbarch, addr, stream);
    461      1.1  christos       }
    462      1.1  christos       break;
    463      1.1  christos 
    464      1.1  christos     case 'c':
    465      1.1  christos       {
    466      1.1  christos 	struct value_print_options opts = *options;
    467      1.1  christos 
    468      1.1  christos 	opts.format = 0;
    469      1.1  christos 	if (TYPE_UNSIGNED (type))
    470      1.1  christos 	  type = builtin_type (gdbarch)->builtin_true_unsigned_char;
    471      1.1  christos  	else
    472      1.1  christos 	  type = builtin_type (gdbarch)->builtin_true_char;
    473      1.1  christos 
    474      1.1  christos 	value_print (value_from_longest (type, val_long), stream, &opts);
    475      1.1  christos       }
    476      1.1  christos       break;
    477      1.1  christos 
    478      1.1  christos     case 'f':
    479      1.1  christos       type = float_type_from_length (type);
    480      1.1  christos       print_floating (valaddr, type, stream);
    481      1.1  christos       break;
    482      1.1  christos 
    483      1.1  christos     case 0:
    484      1.1  christos       internal_error (__FILE__, __LINE__,
    485      1.1  christos 		      _("failed internal consistency check"));
    486      1.1  christos 
    487      1.1  christos     case 't':
    488      1.1  christos       /* Binary; 't' stands for "two".  */
    489      1.1  christos       {
    490      1.1  christos 	char bits[8 * (sizeof val_long) + 1];
    491      1.1  christos 	char buf[8 * (sizeof val_long) + 32];
    492      1.1  christos 	char *cp = bits;
    493      1.1  christos 	int width;
    494      1.1  christos 
    495      1.1  christos 	if (!size)
    496      1.1  christos 	  width = 8 * (sizeof val_long);
    497      1.1  christos 	else
    498      1.1  christos 	  switch (size)
    499      1.1  christos 	    {
    500      1.1  christos 	    case 'b':
    501      1.1  christos 	      width = 8;
    502      1.1  christos 	      break;
    503      1.1  christos 	    case 'h':
    504      1.1  christos 	      width = 16;
    505      1.1  christos 	      break;
    506      1.1  christos 	    case 'w':
    507      1.1  christos 	      width = 32;
    508      1.1  christos 	      break;
    509      1.1  christos 	    case 'g':
    510      1.1  christos 	      width = 64;
    511      1.1  christos 	      break;
    512      1.1  christos 	    default:
    513      1.1  christos 	      error (_("Undefined output size \"%c\"."), size);
    514      1.1  christos 	    }
    515      1.1  christos 
    516      1.1  christos 	bits[width] = '\0';
    517      1.1  christos 	while (width-- > 0)
    518      1.1  christos 	  {
    519      1.1  christos 	    bits[width] = (val_long & 1) ? '1' : '0';
    520      1.1  christos 	    val_long >>= 1;
    521      1.1  christos 	  }
    522      1.1  christos 	if (!size)
    523      1.1  christos 	  {
    524      1.1  christos 	    while (*cp && *cp == '0')
    525      1.1  christos 	      cp++;
    526      1.1  christos 	    if (*cp == '\0')
    527      1.1  christos 	      cp--;
    528      1.1  christos 	  }
    529      1.1  christos 	strncpy (buf, cp, sizeof (bits));
    530      1.1  christos 	fputs_filtered (buf, stream);
    531      1.1  christos       }
    532      1.1  christos       break;
    533      1.1  christos 
    534      1.1  christos     case 'z':
    535      1.1  christos       print_hex_chars (stream, valaddr, len, byte_order);
    536      1.1  christos       break;
    537      1.1  christos 
    538      1.1  christos     default:
    539      1.1  christos       error (_("Undefined output format \"%c\"."), options->format);
    540      1.1  christos     }
    541      1.1  christos }
    542      1.1  christos 
    543      1.1  christos /* Specify default address for `x' command.
    544      1.1  christos    The `info lines' command uses this.  */
    545      1.1  christos 
    546      1.1  christos void
    547      1.1  christos set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
    548      1.1  christos {
    549      1.1  christos   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
    550      1.1  christos 
    551      1.1  christos   next_gdbarch = gdbarch;
    552      1.1  christos   next_address = addr;
    553      1.1  christos 
    554      1.1  christos   /* Make address available to the user as $_.  */
    555      1.1  christos   set_internalvar (lookup_internalvar ("_"),
    556      1.1  christos 		   value_from_pointer (ptr_type, addr));
    557      1.1  christos }
    558      1.1  christos 
    559      1.1  christos /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
    560      1.1  christos    after LEADIN.  Print nothing if no symbolic name is found nearby.
    561      1.1  christos    Optionally also print source file and line number, if available.
    562      1.1  christos    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
    563      1.1  christos    or to interpret it as a possible C++ name and convert it back to source
    564      1.1  christos    form.  However note that DO_DEMANGLE can be overridden by the specific
    565      1.1  christos    settings of the demangle and asm_demangle variables.  Returns
    566      1.1  christos    non-zero if anything was printed; zero otherwise.  */
    567      1.1  christos 
    568      1.1  christos int
    569      1.1  christos print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
    570      1.1  christos 			struct ui_file *stream,
    571      1.1  christos 			int do_demangle, char *leadin)
    572      1.1  christos {
    573      1.1  christos   char *name = NULL;
    574      1.1  christos   char *filename = NULL;
    575      1.1  christos   int unmapped = 0;
    576      1.1  christos   int offset = 0;
    577      1.1  christos   int line = 0;
    578      1.1  christos 
    579      1.1  christos   /* Throw away both name and filename.  */
    580      1.1  christos   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
    581      1.1  christos   make_cleanup (free_current_contents, &filename);
    582      1.1  christos 
    583      1.1  christos   if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
    584      1.1  christos 			      &filename, &line, &unmapped))
    585      1.1  christos     {
    586      1.1  christos       do_cleanups (cleanup_chain);
    587      1.1  christos       return 0;
    588      1.1  christos     }
    589      1.1  christos 
    590      1.1  christos   fputs_filtered (leadin, stream);
    591      1.1  christos   if (unmapped)
    592      1.1  christos     fputs_filtered ("<*", stream);
    593      1.1  christos   else
    594      1.1  christos     fputs_filtered ("<", stream);
    595      1.1  christos   fputs_filtered (name, stream);
    596      1.1  christos   if (offset != 0)
    597      1.1  christos     fprintf_filtered (stream, "+%u", (unsigned int) offset);
    598      1.1  christos 
    599      1.1  christos   /* Append source filename and line number if desired.  Give specific
    600      1.1  christos      line # of this addr, if we have it; else line # of the nearest symbol.  */
    601      1.1  christos   if (print_symbol_filename && filename != NULL)
    602      1.1  christos     {
    603      1.1  christos       if (line != -1)
    604      1.1  christos 	fprintf_filtered (stream, " at %s:%d", filename, line);
    605      1.1  christos       else
    606      1.1  christos 	fprintf_filtered (stream, " in %s", filename);
    607      1.1  christos     }
    608      1.1  christos   if (unmapped)
    609      1.1  christos     fputs_filtered ("*>", stream);
    610      1.1  christos   else
    611      1.1  christos     fputs_filtered (">", stream);
    612      1.1  christos 
    613      1.1  christos   do_cleanups (cleanup_chain);
    614      1.1  christos   return 1;
    615      1.1  christos }
    616      1.1  christos 
    617      1.1  christos /* Given an address ADDR return all the elements needed to print the
    618      1.1  christos    address in a symbolic form.  NAME can be mangled or not depending
    619      1.1  christos    on DO_DEMANGLE (and also on the asm_demangle global variable,
    620      1.1  christos    manipulated via ''set print asm-demangle'').  Return 0 in case of
    621      1.1  christos    success, when all the info in the OUT paramters is valid.  Return 1
    622      1.1  christos    otherwise.  */
    623      1.1  christos int
    624      1.1  christos build_address_symbolic (struct gdbarch *gdbarch,
    625      1.1  christos 			CORE_ADDR addr,  /* IN */
    626      1.1  christos 			int do_demangle, /* IN */
    627      1.1  christos 			char **name,     /* OUT */
    628      1.1  christos 			int *offset,     /* OUT */
    629      1.1  christos 			char **filename, /* OUT */
    630      1.1  christos 			int *line,       /* OUT */
    631  1.1.1.2  christos 			int *unmapped)   /* OUT */
    632      1.1  christos {
    633      1.1  christos   struct bound_minimal_symbol msymbol;
    634      1.1  christos   struct symbol *symbol;
    635      1.1  christos   CORE_ADDR name_location = 0;
    636      1.1  christos   struct obj_section *section = NULL;
    637      1.1  christos   const char *name_temp = "";
    638      1.1  christos 
    639      1.1  christos   /* Let's say it is mapped (not unmapped).  */
    640      1.1  christos   *unmapped = 0;
    641      1.1  christos 
    642      1.1  christos   /* Determine if the address is in an overlay, and whether it is
    643      1.1  christos      mapped.  */
    644      1.1  christos   if (overlay_debugging)
    645      1.1  christos     {
    646      1.1  christos       section = find_pc_overlay (addr);
    647      1.1  christos       if (pc_in_unmapped_range (addr, section))
    648      1.1  christos 	{
    649      1.1  christos 	  *unmapped = 1;
    650      1.1  christos 	  addr = overlay_mapped_address (addr, section);
    651      1.1  christos 	}
    652      1.1  christos     }
    653      1.1  christos 
    654      1.1  christos   /* First try to find the address in the symbol table, then
    655      1.1  christos      in the minsyms.  Take the closest one.  */
    656      1.1  christos 
    657      1.1  christos   /* This is defective in the sense that it only finds text symbols.  So
    658      1.1  christos      really this is kind of pointless--we should make sure that the
    659      1.1  christos      minimal symbols have everything we need (by changing that we could
    660      1.1  christos      save some memory, but for many debug format--ELF/DWARF or
    661  1.1.1.2  christos      anything/stabs--it would be inconvenient to eliminate those minimal
    662      1.1  christos      symbols anyway).  */
    663      1.1  christos   msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
    664      1.1  christos   symbol = find_pc_sect_function (addr, section);
    665      1.1  christos 
    666      1.1  christos   if (symbol)
    667      1.1  christos     {
    668      1.1  christos       /* If this is a function (i.e. a code address), strip out any
    669      1.1  christos 	 non-address bits.  For instance, display a pointer to the
    670      1.1  christos 	 first instruction of a Thumb function as <function>; the
    671      1.1  christos 	 second instruction will be <function+2>, even though the
    672      1.1  christos 	 pointer is <function+3>.  This matches the ISA behavior.  */
    673      1.1  christos       addr = gdbarch_addr_bits_remove (gdbarch, addr);
    674      1.1  christos 
    675      1.1  christos       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
    676      1.1  christos       if (do_demangle || asm_demangle)
    677      1.1  christos 	name_temp = SYMBOL_PRINT_NAME (symbol);
    678      1.1  christos       else
    679      1.1  christos 	name_temp = SYMBOL_LINKAGE_NAME (symbol);
    680  1.1.1.2  christos     }
    681  1.1.1.2  christos 
    682  1.1.1.2  christos   if (msymbol.minsym != NULL
    683  1.1.1.2  christos       && MSYMBOL_HAS_SIZE (msymbol.minsym)
    684  1.1.1.2  christos       && MSYMBOL_SIZE (msymbol.minsym) == 0
    685  1.1.1.2  christos       && MSYMBOL_TYPE (msymbol.minsym) != mst_text
    686  1.1.1.2  christos       && MSYMBOL_TYPE (msymbol.minsym) != mst_text_gnu_ifunc
    687      1.1  christos       && MSYMBOL_TYPE (msymbol.minsym) != mst_file_text)
    688  1.1.1.2  christos     msymbol.minsym = NULL;
    689      1.1  christos 
    690  1.1.1.2  christos   if (msymbol.minsym != NULL)
    691      1.1  christos     {
    692      1.1  christos       if (BMSYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
    693      1.1  christos 	{
    694      1.1  christos 	  /* If this is a function (i.e. a code address), strip out any
    695      1.1  christos 	     non-address bits.  For instance, display a pointer to the
    696      1.1  christos 	     first instruction of a Thumb function as <function>; the
    697  1.1.1.2  christos 	     second instruction will be <function+2>, even though the
    698  1.1.1.2  christos 	     pointer is <function+3>.  This matches the ISA behavior.  */
    699  1.1.1.2  christos 	  if (MSYMBOL_TYPE (msymbol.minsym) == mst_text
    700  1.1.1.2  christos 	      || MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc
    701      1.1  christos 	      || MSYMBOL_TYPE (msymbol.minsym) == mst_file_text
    702      1.1  christos 	      || MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
    703      1.1  christos 	    addr = gdbarch_addr_bits_remove (gdbarch, addr);
    704      1.1  christos 
    705      1.1  christos 	  /* The msymbol is closer to the address than the symbol;
    706  1.1.1.2  christos 	     use the msymbol instead.  */
    707      1.1  christos 	  symbol = 0;
    708  1.1.1.2  christos 	  name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
    709      1.1  christos 	  if (do_demangle || asm_demangle)
    710  1.1.1.2  christos 	    name_temp = MSYMBOL_PRINT_NAME (msymbol.minsym);
    711      1.1  christos 	  else
    712      1.1  christos 	    name_temp = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
    713  1.1.1.2  christos 	}
    714      1.1  christos     }
    715      1.1  christos   if (symbol == NULL && msymbol.minsym == NULL)
    716      1.1  christos     return 1;
    717      1.1  christos 
    718      1.1  christos   /* If the nearest symbol is too far away, don't print anything symbolic.  */
    719      1.1  christos 
    720      1.1  christos   /* For when CORE_ADDR is larger than unsigned int, we do math in
    721      1.1  christos      CORE_ADDR.  But when we detect unsigned wraparound in the
    722      1.1  christos      CORE_ADDR math, we ignore this test and print the offset,
    723      1.1  christos      because addr+max_symbolic_offset has wrapped through the end
    724      1.1  christos      of the address space back to the beginning, giving bogus comparison.  */
    725      1.1  christos   if (addr > name_location + max_symbolic_offset
    726      1.1  christos       && name_location + max_symbolic_offset > name_location)
    727      1.1  christos     return 1;
    728      1.1  christos 
    729      1.1  christos   *offset = addr - name_location;
    730      1.1  christos 
    731      1.1  christos   *name = xstrdup (name_temp);
    732      1.1  christos 
    733      1.1  christos   if (print_symbol_filename)
    734      1.1  christos     {
    735      1.1  christos       struct symtab_and_line sal;
    736      1.1  christos 
    737      1.1  christos       sal = find_pc_sect_line (addr, section, 0);
    738      1.1  christos 
    739      1.1  christos       if (sal.symtab)
    740      1.1  christos 	{
    741      1.1  christos 	  *filename = xstrdup (symtab_to_filename_for_display (sal.symtab));
    742      1.1  christos 	  *line = sal.line;
    743      1.1  christos 	}
    744      1.1  christos     }
    745      1.1  christos   return 0;
    746      1.1  christos }
    747      1.1  christos 
    748      1.1  christos 
    749      1.1  christos /* Print address ADDR symbolically on STREAM.
    750      1.1  christos    First print it as a number.  Then perhaps print
    751      1.1  christos    <SYMBOL + OFFSET> after the number.  */
    752      1.1  christos 
    753      1.1  christos void
    754      1.1  christos print_address (struct gdbarch *gdbarch,
    755      1.1  christos 	       CORE_ADDR addr, struct ui_file *stream)
    756      1.1  christos {
    757      1.1  christos   fputs_filtered (paddress (gdbarch, addr), stream);
    758      1.1  christos   print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
    759      1.1  christos }
    760      1.1  christos 
    761      1.1  christos /* Return a prefix for instruction address:
    762      1.1  christos    "=> " for current instruction, else "   ".  */
    763      1.1  christos 
    764      1.1  christos const char *
    765      1.1  christos pc_prefix (CORE_ADDR addr)
    766      1.1  christos {
    767      1.1  christos   if (has_stack_frames ())
    768      1.1  christos     {
    769      1.1  christos       struct frame_info *frame;
    770      1.1  christos       CORE_ADDR pc;
    771      1.1  christos 
    772      1.1  christos       frame = get_selected_frame (NULL);
    773      1.1  christos       if (get_frame_pc_if_available (frame, &pc) && pc == addr)
    774      1.1  christos 	return "=> ";
    775      1.1  christos     }
    776      1.1  christos   return "   ";
    777      1.1  christos }
    778      1.1  christos 
    779      1.1  christos /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
    780      1.1  christos    controls whether to print the symbolic name "raw" or demangled.
    781      1.1  christos    Return non-zero if anything was printed; zero otherwise.  */
    782      1.1  christos 
    783      1.1  christos int
    784      1.1  christos print_address_demangle (const struct value_print_options *opts,
    785      1.1  christos 			struct gdbarch *gdbarch, CORE_ADDR addr,
    786      1.1  christos 			struct ui_file *stream, int do_demangle)
    787      1.1  christos {
    788      1.1  christos   if (opts->addressprint)
    789      1.1  christos     {
    790      1.1  christos       fputs_filtered (paddress (gdbarch, addr), stream);
    791      1.1  christos       print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
    792      1.1  christos     }
    793      1.1  christos   else
    794      1.1  christos     {
    795      1.1  christos       return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
    796      1.1  christos     }
    797      1.1  christos   return 1;
    798      1.1  christos }
    799      1.1  christos 
    800      1.1  christos 
    802      1.1  christos /* Examine data at address ADDR in format FMT.
    803      1.1  christos    Fetch it from memory and print on gdb_stdout.  */
    804      1.1  christos 
    805      1.1  christos static void
    806      1.1  christos do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
    807      1.1  christos {
    808      1.1  christos   char format = 0;
    809      1.1  christos   char size;
    810      1.1  christos   int count = 1;
    811      1.1  christos   struct type *val_type = NULL;
    812      1.1  christos   int i;
    813      1.1  christos   int maxelts;
    814      1.1  christos   struct value_print_options opts;
    815      1.1  christos 
    816      1.1  christos   format = fmt.format;
    817      1.1  christos   size = fmt.size;
    818      1.1  christos   count = fmt.count;
    819      1.1  christos   next_gdbarch = gdbarch;
    820      1.1  christos   next_address = addr;
    821      1.1  christos 
    822      1.1  christos   /* Instruction format implies fetch single bytes
    823      1.1  christos      regardless of the specified size.
    824      1.1  christos      The case of strings is handled in decode_format, only explicit
    825      1.1  christos      size operator are not changed to 'b'.  */
    826      1.1  christos   if (format == 'i')
    827      1.1  christos     size = 'b';
    828      1.1  christos 
    829      1.1  christos   if (size == 'a')
    830      1.1  christos     {
    831      1.1  christos       /* Pick the appropriate size for an address.  */
    832      1.1  christos       if (gdbarch_ptr_bit (next_gdbarch) == 64)
    833      1.1  christos 	size = 'g';
    834      1.1  christos       else if (gdbarch_ptr_bit (next_gdbarch) == 32)
    835      1.1  christos 	size = 'w';
    836      1.1  christos       else if (gdbarch_ptr_bit (next_gdbarch) == 16)
    837      1.1  christos 	size = 'h';
    838      1.1  christos       else
    839      1.1  christos 	/* Bad value for gdbarch_ptr_bit.  */
    840      1.1  christos 	internal_error (__FILE__, __LINE__,
    841      1.1  christos 			_("failed internal consistency check"));
    842      1.1  christos     }
    843      1.1  christos 
    844      1.1  christos   if (size == 'b')
    845      1.1  christos     val_type = builtin_type (next_gdbarch)->builtin_int8;
    846      1.1  christos   else if (size == 'h')
    847      1.1  christos     val_type = builtin_type (next_gdbarch)->builtin_int16;
    848      1.1  christos   else if (size == 'w')
    849      1.1  christos     val_type = builtin_type (next_gdbarch)->builtin_int32;
    850      1.1  christos   else if (size == 'g')
    851      1.1  christos     val_type = builtin_type (next_gdbarch)->builtin_int64;
    852      1.1  christos 
    853      1.1  christos   if (format == 's')
    854      1.1  christos     {
    855      1.1  christos       struct type *char_type = NULL;
    856      1.1  christos 
    857      1.1  christos       /* Search for "char16_t"  or "char32_t" types or fall back to 8-bit char
    858      1.1  christos 	 if type is not found.  */
    859      1.1  christos       if (size == 'h')
    860      1.1  christos 	char_type = builtin_type (next_gdbarch)->builtin_char16;
    861      1.1  christos       else if (size == 'w')
    862      1.1  christos 	char_type = builtin_type (next_gdbarch)->builtin_char32;
    863      1.1  christos       if (char_type)
    864      1.1  christos         val_type = char_type;
    865      1.1  christos       else
    866      1.1  christos         {
    867      1.1  christos 	  if (size != '\0' && size != 'b')
    868      1.1  christos 	    warning (_("Unable to display strings with "
    869      1.1  christos 		       "size '%c', using 'b' instead."), size);
    870      1.1  christos 	  size = 'b';
    871      1.1  christos 	  val_type = builtin_type (next_gdbarch)->builtin_int8;
    872      1.1  christos         }
    873      1.1  christos     }
    874      1.1  christos 
    875      1.1  christos   maxelts = 8;
    876      1.1  christos   if (size == 'w')
    877      1.1  christos     maxelts = 4;
    878      1.1  christos   if (size == 'g')
    879      1.1  christos     maxelts = 2;
    880      1.1  christos   if (format == 's' || format == 'i')
    881      1.1  christos     maxelts = 1;
    882      1.1  christos 
    883      1.1  christos   get_formatted_print_options (&opts, format);
    884      1.1  christos 
    885      1.1  christos   /* Print as many objects as specified in COUNT, at most maxelts per line,
    886      1.1  christos      with the address of the next one at the start of each line.  */
    887      1.1  christos 
    888      1.1  christos   while (count > 0)
    889      1.1  christos     {
    890      1.1  christos       QUIT;
    891      1.1  christos       if (format == 'i')
    892      1.1  christos 	fputs_filtered (pc_prefix (next_address), gdb_stdout);
    893      1.1  christos       print_address (next_gdbarch, next_address, gdb_stdout);
    894      1.1  christos       printf_filtered (":");
    895      1.1  christos       for (i = maxelts;
    896      1.1  christos 	   i > 0 && count > 0;
    897      1.1  christos 	   i--, count--)
    898      1.1  christos 	{
    899      1.1  christos 	  printf_filtered ("\t");
    900      1.1  christos 	  /* Note that print_formatted sets next_address for the next
    901      1.1  christos 	     object.  */
    902      1.1  christos 	  last_examine_address = next_address;
    903      1.1  christos 
    904      1.1  christos 	  if (last_examine_value)
    905      1.1  christos 	    value_free (last_examine_value);
    906      1.1  christos 
    907      1.1  christos 	  /* The value to be displayed is not fetched greedily.
    908      1.1  christos 	     Instead, to avoid the possibility of a fetched value not
    909      1.1  christos 	     being used, its retrieval is delayed until the print code
    910      1.1  christos 	     uses it.  When examining an instruction stream, the
    911      1.1  christos 	     disassembler will perform its own memory fetch using just
    912      1.1  christos 	     the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
    913      1.1  christos 	     the disassembler be modified so that LAST_EXAMINE_VALUE
    914      1.1  christos 	     is left with the byte sequence from the last complete
    915      1.1  christos 	     instruction fetched from memory?  */
    916      1.1  christos 	  last_examine_value = value_at_lazy (val_type, next_address);
    917      1.1  christos 
    918      1.1  christos 	  if (last_examine_value)
    919      1.1  christos 	    release_value (last_examine_value);
    920      1.1  christos 
    921      1.1  christos 	  print_formatted (last_examine_value, size, &opts, gdb_stdout);
    922      1.1  christos 
    923      1.1  christos 	  /* Display any branch delay slots following the final insn.  */
    924      1.1  christos 	  if (format == 'i' && count == 1)
    925      1.1  christos 	    count += branch_delay_insns;
    926      1.1  christos 	}
    927      1.1  christos       printf_filtered ("\n");
    928      1.1  christos       gdb_flush (gdb_stdout);
    929      1.1  christos     }
    930      1.1  christos }
    931      1.1  christos 
    932      1.1  christos static void
    934      1.1  christos validate_format (struct format_data fmt, char *cmdname)
    935      1.1  christos {
    936      1.1  christos   if (fmt.size != 0)
    937      1.1  christos     error (_("Size letters are meaningless in \"%s\" command."), cmdname);
    938      1.1  christos   if (fmt.count != 1)
    939      1.1  christos     error (_("Item count other than 1 is meaningless in \"%s\" command."),
    940      1.1  christos 	   cmdname);
    941      1.1  christos   if (fmt.format == 'i')
    942      1.1  christos     error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
    943      1.1  christos 	   fmt.format, cmdname);
    944      1.1  christos }
    945      1.1  christos 
    946      1.1  christos /* Evaluate string EXP as an expression in the current language and
    947      1.1  christos    print the resulting value.  EXP may contain a format specifier as the
    948      1.1  christos    first argument ("/x myvar" for example, to print myvar in hex).  */
    949      1.1  christos 
    950      1.1  christos static void
    951      1.1  christos print_command_1 (const char *exp, int voidprint)
    952      1.1  christos {
    953      1.1  christos   struct expression *expr;
    954      1.1  christos   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
    955      1.1  christos   char format = 0;
    956      1.1  christos   struct value *val;
    957      1.1  christos   struct format_data fmt;
    958      1.1  christos 
    959      1.1  christos   if (exp && *exp == '/')
    960      1.1  christos     {
    961      1.1  christos       exp++;
    962      1.1  christos       fmt = decode_format (&exp, last_format, 0);
    963      1.1  christos       validate_format (fmt, "print");
    964      1.1  christos       last_format = format = fmt.format;
    965      1.1  christos     }
    966      1.1  christos   else
    967      1.1  christos     {
    968      1.1  christos       fmt.count = 1;
    969      1.1  christos       fmt.format = 0;
    970      1.1  christos       fmt.size = 0;
    971      1.1  christos       fmt.raw = 0;
    972      1.1  christos     }
    973      1.1  christos 
    974      1.1  christos   if (exp && *exp)
    975      1.1  christos     {
    976      1.1  christos       expr = parse_expression (exp);
    977      1.1  christos       make_cleanup (free_current_contents, &expr);
    978      1.1  christos       val = evaluate_expression (expr);
    979      1.1  christos     }
    980      1.1  christos   else
    981      1.1  christos     val = access_value_history (0);
    982      1.1  christos 
    983      1.1  christos   if (voidprint || (val && value_type (val) &&
    984      1.1  christos 		    TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
    985  1.1.1.2  christos     {
    986      1.1  christos       struct value_print_options opts;
    987  1.1.1.2  christos       int histindex = record_latest_value (val);
    988      1.1  christos 
    989  1.1.1.2  christos       annotate_value_history_begin (histindex, value_type (val));
    990      1.1  christos 
    991      1.1  christos       printf_filtered ("$%d = ", histindex);
    992      1.1  christos 
    993      1.1  christos       annotate_value_history_value ();
    994      1.1  christos 
    995      1.1  christos       get_formatted_print_options (&opts, format);
    996      1.1  christos       opts.raw = fmt.raw;
    997  1.1.1.2  christos 
    998      1.1  christos       print_formatted (val, fmt.size, &opts, gdb_stdout);
    999      1.1  christos       printf_filtered ("\n");
   1000      1.1  christos 
   1001      1.1  christos       annotate_value_history_end ();
   1002      1.1  christos     }
   1003      1.1  christos 
   1004      1.1  christos   do_cleanups (old_chain);
   1005      1.1  christos }
   1006      1.1  christos 
   1007      1.1  christos static void
   1008      1.1  christos print_command (char *exp, int from_tty)
   1009      1.1  christos {
   1010      1.1  christos   print_command_1 (exp, 1);
   1011      1.1  christos }
   1012      1.1  christos 
   1013      1.1  christos /* Same as print, except it doesn't print void results.  */
   1014      1.1  christos static void
   1015      1.1  christos call_command (char *exp, int from_tty)
   1016      1.1  christos {
   1017      1.1  christos   print_command_1 (exp, 0);
   1018      1.1  christos }
   1019      1.1  christos 
   1020      1.1  christos /* Implementation of the "output" command.  */
   1021      1.1  christos 
   1022      1.1  christos static void
   1023      1.1  christos output_command (char *exp, int from_tty)
   1024      1.1  christos {
   1025      1.1  christos   output_command_const (exp, from_tty);
   1026      1.1  christos }
   1027      1.1  christos 
   1028      1.1  christos /* Like output_command, but takes a const string as argument.  */
   1029      1.1  christos 
   1030      1.1  christos void
   1031      1.1  christos output_command_const (const char *exp, int from_tty)
   1032      1.1  christos {
   1033      1.1  christos   struct expression *expr;
   1034      1.1  christos   struct cleanup *old_chain;
   1035      1.1  christos   char format = 0;
   1036      1.1  christos   struct value *val;
   1037      1.1  christos   struct format_data fmt;
   1038      1.1  christos   struct value_print_options opts;
   1039      1.1  christos 
   1040      1.1  christos   fmt.size = 0;
   1041      1.1  christos   fmt.raw = 0;
   1042      1.1  christos 
   1043      1.1  christos   if (exp && *exp == '/')
   1044      1.1  christos     {
   1045      1.1  christos       exp++;
   1046      1.1  christos       fmt = decode_format (&exp, 0, 0);
   1047      1.1  christos       validate_format (fmt, "output");
   1048      1.1  christos       format = fmt.format;
   1049      1.1  christos     }
   1050      1.1  christos 
   1051      1.1  christos   expr = parse_expression (exp);
   1052      1.1  christos   old_chain = make_cleanup (free_current_contents, &expr);
   1053      1.1  christos 
   1054      1.1  christos   val = evaluate_expression (expr);
   1055      1.1  christos 
   1056      1.1  christos   annotate_value_begin (value_type (val));
   1057      1.1  christos 
   1058      1.1  christos   get_formatted_print_options (&opts, format);
   1059      1.1  christos   opts.raw = fmt.raw;
   1060      1.1  christos   print_formatted (val, fmt.size, &opts, gdb_stdout);
   1061      1.1  christos 
   1062      1.1  christos   annotate_value_end ();
   1063      1.1  christos 
   1064      1.1  christos   wrap_here ("");
   1065      1.1  christos   gdb_flush (gdb_stdout);
   1066      1.1  christos 
   1067      1.1  christos   do_cleanups (old_chain);
   1068      1.1  christos }
   1069      1.1  christos 
   1070      1.1  christos static void
   1071      1.1  christos set_command (char *exp, int from_tty)
   1072      1.1  christos {
   1073      1.1  christos   struct expression *expr = parse_expression (exp);
   1074      1.1  christos   struct cleanup *old_chain =
   1075      1.1  christos     make_cleanup (free_current_contents, &expr);
   1076      1.1  christos 
   1077      1.1  christos   if (expr->nelts >= 1)
   1078      1.1  christos     switch (expr->elts[0].opcode)
   1079      1.1  christos       {
   1080      1.1  christos       case UNOP_PREINCREMENT:
   1081      1.1  christos       case UNOP_POSTINCREMENT:
   1082      1.1  christos       case UNOP_PREDECREMENT:
   1083      1.1  christos       case UNOP_POSTDECREMENT:
   1084      1.1  christos       case BINOP_ASSIGN:
   1085      1.1  christos       case BINOP_ASSIGN_MODIFY:
   1086      1.1  christos       case BINOP_COMMA:
   1087      1.1  christos 	break;
   1088      1.1  christos       default:
   1089      1.1  christos 	warning
   1090      1.1  christos 	  (_("Expression is not an assignment (and might have no effect)"));
   1091      1.1  christos       }
   1092      1.1  christos 
   1093      1.1  christos   evaluate_expression (expr);
   1094      1.1  christos   do_cleanups (old_chain);
   1095      1.1  christos }
   1096      1.1  christos 
   1097      1.1  christos static void
   1098      1.1  christos sym_info (char *arg, int from_tty)
   1099      1.1  christos {
   1100      1.1  christos   struct minimal_symbol *msymbol;
   1101      1.1  christos   struct objfile *objfile;
   1102      1.1  christos   struct obj_section *osect;
   1103      1.1  christos   CORE_ADDR addr, sect_addr;
   1104      1.1  christos   int matches = 0;
   1105      1.1  christos   unsigned int offset;
   1106      1.1  christos 
   1107      1.1  christos   if (!arg)
   1108      1.1  christos     error_no_arg (_("address"));
   1109      1.1  christos 
   1110      1.1  christos   addr = parse_and_eval_address (arg);
   1111      1.1  christos   ALL_OBJSECTIONS (objfile, osect)
   1112      1.1  christos   {
   1113      1.1  christos     /* Only process each object file once, even if there's a separate
   1114      1.1  christos        debug file.  */
   1115      1.1  christos     if (objfile->separate_debug_objfile_backlink)
   1116      1.1  christos       continue;
   1117      1.1  christos 
   1118      1.1  christos     sect_addr = overlay_mapped_address (addr, osect);
   1119      1.1  christos 
   1120      1.1  christos     if (obj_section_addr (osect) <= sect_addr
   1121      1.1  christos 	&& sect_addr < obj_section_endaddr (osect)
   1122      1.1  christos 	&& (msymbol
   1123      1.1  christos 	    = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
   1124      1.1  christos       {
   1125      1.1  christos 	const char *obj_name, *mapped, *sec_name, *msym_name;
   1126  1.1.1.2  christos 	char *loc_string;
   1127      1.1  christos 	struct cleanup *old_chain;
   1128      1.1  christos 
   1129  1.1.1.2  christos 	matches = 1;
   1130      1.1  christos 	offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
   1131      1.1  christos 	mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
   1132      1.1  christos 	sec_name = osect->the_bfd_section->name;
   1133      1.1  christos 	msym_name = MSYMBOL_PRINT_NAME (msymbol);
   1134      1.1  christos 
   1135      1.1  christos 	/* Don't print the offset if it is zero.
   1136      1.1  christos 	   We assume there's no need to handle i18n of "sym + offset".  */
   1137      1.1  christos 	if (offset)
   1138      1.1  christos 	  loc_string = xstrprintf ("%s + %u", msym_name, offset);
   1139      1.1  christos 	else
   1140      1.1  christos 	  loc_string = xstrprintf ("%s", msym_name);
   1141      1.1  christos 
   1142      1.1  christos 	/* Use a cleanup to free loc_string in case the user quits
   1143      1.1  christos 	   a pagination request inside printf_filtered.  */
   1144      1.1  christos 	old_chain = make_cleanup (xfree, loc_string);
   1145      1.1  christos 
   1146      1.1  christos 	gdb_assert (osect->objfile && objfile_name (osect->objfile));
   1147      1.1  christos 	obj_name = objfile_name (osect->objfile);
   1148      1.1  christos 
   1149      1.1  christos 	if (MULTI_OBJFILE_P ())
   1150      1.1  christos 	  if (pc_in_unmapped_range (addr, osect))
   1151      1.1  christos 	    if (section_is_overlay (osect))
   1152      1.1  christos 	      printf_filtered (_("%s in load address range of "
   1153      1.1  christos 				 "%s overlay section %s of %s\n"),
   1154      1.1  christos 			       loc_string, mapped, sec_name, obj_name);
   1155      1.1  christos 	    else
   1156      1.1  christos 	      printf_filtered (_("%s in load address range of "
   1157      1.1  christos 				 "section %s of %s\n"),
   1158      1.1  christos 			       loc_string, sec_name, obj_name);
   1159      1.1  christos 	  else
   1160      1.1  christos 	    if (section_is_overlay (osect))
   1161      1.1  christos 	      printf_filtered (_("%s in %s overlay section %s of %s\n"),
   1162      1.1  christos 			       loc_string, mapped, sec_name, obj_name);
   1163      1.1  christos 	    else
   1164      1.1  christos 	      printf_filtered (_("%s in section %s of %s\n"),
   1165      1.1  christos 			       loc_string, sec_name, obj_name);
   1166      1.1  christos 	else
   1167      1.1  christos 	  if (pc_in_unmapped_range (addr, osect))
   1168      1.1  christos 	    if (section_is_overlay (osect))
   1169      1.1  christos 	      printf_filtered (_("%s in load address range of %s overlay "
   1170      1.1  christos 				 "section %s\n"),
   1171      1.1  christos 			       loc_string, mapped, sec_name);
   1172      1.1  christos 	    else
   1173      1.1  christos 	      printf_filtered (_("%s in load address range of section %s\n"),
   1174      1.1  christos 			       loc_string, sec_name);
   1175      1.1  christos 	  else
   1176      1.1  christos 	    if (section_is_overlay (osect))
   1177      1.1  christos 	      printf_filtered (_("%s in %s overlay section %s\n"),
   1178      1.1  christos 			       loc_string, mapped, sec_name);
   1179      1.1  christos 	    else
   1180      1.1  christos 	      printf_filtered (_("%s in section %s\n"),
   1181      1.1  christos 			       loc_string, sec_name);
   1182      1.1  christos 
   1183      1.1  christos 	do_cleanups (old_chain);
   1184      1.1  christos       }
   1185      1.1  christos   }
   1186      1.1  christos   if (matches == 0)
   1187      1.1  christos     printf_filtered (_("No symbol matches %s.\n"), arg);
   1188      1.1  christos }
   1189      1.1  christos 
   1190      1.1  christos static void
   1191      1.1  christos address_info (char *exp, int from_tty)
   1192      1.1  christos {
   1193      1.1  christos   struct gdbarch *gdbarch;
   1194      1.1  christos   int regno;
   1195      1.1  christos   struct symbol *sym;
   1196      1.1  christos   struct bound_minimal_symbol msymbol;
   1197      1.1  christos   long val;
   1198      1.1  christos   struct obj_section *section;
   1199      1.1  christos   CORE_ADDR load_addr, context_pc = 0;
   1200      1.1  christos   struct field_of_this_result is_a_field_of_this;
   1201      1.1  christos 
   1202      1.1  christos   if (exp == 0)
   1203      1.1  christos     error (_("Argument required."));
   1204      1.1  christos 
   1205      1.1  christos   sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
   1206      1.1  christos 		       &is_a_field_of_this);
   1207      1.1  christos   if (sym == NULL)
   1208      1.1  christos     {
   1209      1.1  christos       if (is_a_field_of_this.type != NULL)
   1210      1.1  christos 	{
   1211      1.1  christos 	  printf_filtered ("Symbol \"");
   1212      1.1  christos 	  fprintf_symbol_filtered (gdb_stdout, exp,
   1213      1.1  christos 				   current_language->la_language, DMGL_ANSI);
   1214      1.1  christos 	  printf_filtered ("\" is a field of the local class variable ");
   1215      1.1  christos 	  if (current_language->la_language == language_objc)
   1216      1.1  christos 	    printf_filtered ("`self'\n");	/* ObjC equivalent of "this" */
   1217      1.1  christos 	  else
   1218      1.1  christos 	    printf_filtered ("`this'\n");
   1219      1.1  christos 	  return;
   1220      1.1  christos 	}
   1221      1.1  christos 
   1222      1.1  christos       msymbol = lookup_bound_minimal_symbol (exp);
   1223      1.1  christos 
   1224      1.1  christos       if (msymbol.minsym != NULL)
   1225  1.1.1.2  christos 	{
   1226      1.1  christos 	  struct objfile *objfile = msymbol.objfile;
   1227      1.1  christos 
   1228      1.1  christos 	  gdbarch = get_objfile_arch (objfile);
   1229      1.1  christos 	  load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
   1230      1.1  christos 
   1231      1.1  christos 	  printf_filtered ("Symbol \"");
   1232      1.1  christos 	  fprintf_symbol_filtered (gdb_stdout, exp,
   1233  1.1.1.2  christos 				   current_language->la_language, DMGL_ANSI);
   1234      1.1  christos 	  printf_filtered ("\" is at ");
   1235      1.1  christos 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1236      1.1  christos 	  printf_filtered (" in a file compiled without debugging");
   1237      1.1  christos 	  section = MSYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
   1238      1.1  christos 	  if (section_is_overlay (section))
   1239      1.1  christos 	    {
   1240      1.1  christos 	      load_addr = overlay_unmapped_address (load_addr, section);
   1241      1.1  christos 	      printf_filtered (",\n -- loaded at ");
   1242      1.1  christos 	      fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1243      1.1  christos 	      printf_filtered (" in overlay section %s",
   1244      1.1  christos 			       section->the_bfd_section->name);
   1245      1.1  christos 	    }
   1246      1.1  christos 	  printf_filtered (".\n");
   1247      1.1  christos 	}
   1248      1.1  christos       else
   1249      1.1  christos 	error (_("No symbol \"%s\" in current context."), exp);
   1250      1.1  christos       return;
   1251      1.1  christos     }
   1252      1.1  christos 
   1253      1.1  christos   printf_filtered ("Symbol \"");
   1254  1.1.1.2  christos   fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
   1255  1.1.1.2  christos 			   current_language->la_language, DMGL_ANSI);
   1256  1.1.1.2  christos   printf_filtered ("\" is ");
   1257  1.1.1.2  christos   val = SYMBOL_VALUE (sym);
   1258  1.1.1.2  christos   if (SYMBOL_OBJFILE_OWNED (sym))
   1259      1.1  christos     section = SYMBOL_OBJ_SECTION (symbol_objfile (sym), sym);
   1260      1.1  christos   else
   1261      1.1  christos     section = NULL;
   1262      1.1  christos   gdbarch = symbol_arch (sym);
   1263      1.1  christos 
   1264      1.1  christos   if (SYMBOL_COMPUTED_OPS (sym) != NULL)
   1265      1.1  christos     {
   1266      1.1  christos       SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
   1267      1.1  christos 						    gdb_stdout);
   1268      1.1  christos       printf_filtered (".\n");
   1269      1.1  christos       return;
   1270      1.1  christos     }
   1271      1.1  christos 
   1272      1.1  christos   switch (SYMBOL_CLASS (sym))
   1273      1.1  christos     {
   1274      1.1  christos     case LOC_CONST:
   1275      1.1  christos     case LOC_CONST_BYTES:
   1276      1.1  christos       printf_filtered ("constant");
   1277      1.1  christos       break;
   1278      1.1  christos 
   1279      1.1  christos     case LOC_LABEL:
   1280      1.1  christos       printf_filtered ("a label at address ");
   1281      1.1  christos       load_addr = SYMBOL_VALUE_ADDRESS (sym);
   1282      1.1  christos       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1283      1.1  christos       if (section_is_overlay (section))
   1284      1.1  christos 	{
   1285      1.1  christos 	  load_addr = overlay_unmapped_address (load_addr, section);
   1286      1.1  christos 	  printf_filtered (",\n -- loaded at ");
   1287      1.1  christos 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1288      1.1  christos 	  printf_filtered (" in overlay section %s",
   1289      1.1  christos 			   section->the_bfd_section->name);
   1290      1.1  christos 	}
   1291      1.1  christos       break;
   1292      1.1  christos 
   1293      1.1  christos     case LOC_COMPUTED:
   1294      1.1  christos       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
   1295      1.1  christos 
   1296      1.1  christos     case LOC_REGISTER:
   1297      1.1  christos       /* GDBARCH is the architecture associated with the objfile the symbol
   1298      1.1  christos 	 is defined in; the target architecture may be different, and may
   1299      1.1  christos 	 provide additional registers.  However, we do not know the target
   1300      1.1  christos 	 architecture at this point.  We assume the objfile architecture
   1301      1.1  christos 	 will contain all the standard registers that occur in debug info
   1302      1.1  christos 	 in that objfile.  */
   1303      1.1  christos       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
   1304      1.1  christos 
   1305      1.1  christos       if (SYMBOL_IS_ARGUMENT (sym))
   1306      1.1  christos 	printf_filtered (_("an argument in register %s"),
   1307      1.1  christos 			 gdbarch_register_name (gdbarch, regno));
   1308      1.1  christos       else
   1309      1.1  christos 	printf_filtered (_("a variable in register %s"),
   1310      1.1  christos 			 gdbarch_register_name (gdbarch, regno));
   1311      1.1  christos       break;
   1312      1.1  christos 
   1313      1.1  christos     case LOC_STATIC:
   1314      1.1  christos       printf_filtered (_("static storage at address "));
   1315      1.1  christos       load_addr = SYMBOL_VALUE_ADDRESS (sym);
   1316      1.1  christos       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1317      1.1  christos       if (section_is_overlay (section))
   1318      1.1  christos 	{
   1319      1.1  christos 	  load_addr = overlay_unmapped_address (load_addr, section);
   1320      1.1  christos 	  printf_filtered (_(",\n -- loaded at "));
   1321      1.1  christos 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1322      1.1  christos 	  printf_filtered (_(" in overlay section %s"),
   1323      1.1  christos 			   section->the_bfd_section->name);
   1324      1.1  christos 	}
   1325      1.1  christos       break;
   1326      1.1  christos 
   1327      1.1  christos     case LOC_REGPARM_ADDR:
   1328      1.1  christos       /* Note comment at LOC_REGISTER.  */
   1329      1.1  christos       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
   1330      1.1  christos       printf_filtered (_("address of an argument in register %s"),
   1331      1.1  christos 		       gdbarch_register_name (gdbarch, regno));
   1332      1.1  christos       break;
   1333      1.1  christos 
   1334      1.1  christos     case LOC_ARG:
   1335      1.1  christos       printf_filtered (_("an argument at offset %ld"), val);
   1336      1.1  christos       break;
   1337      1.1  christos 
   1338      1.1  christos     case LOC_LOCAL:
   1339      1.1  christos       printf_filtered (_("a local variable at frame offset %ld"), val);
   1340      1.1  christos       break;
   1341      1.1  christos 
   1342      1.1  christos     case LOC_REF_ARG:
   1343      1.1  christos       printf_filtered (_("a reference argument at offset %ld"), val);
   1344      1.1  christos       break;
   1345      1.1  christos 
   1346      1.1  christos     case LOC_TYPEDEF:
   1347      1.1  christos       printf_filtered (_("a typedef"));
   1348      1.1  christos       break;
   1349      1.1  christos 
   1350      1.1  christos     case LOC_BLOCK:
   1351      1.1  christos       printf_filtered (_("a function at address "));
   1352      1.1  christos       load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
   1353      1.1  christos       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1354      1.1  christos       if (section_is_overlay (section))
   1355      1.1  christos 	{
   1356      1.1  christos 	  load_addr = overlay_unmapped_address (load_addr, section);
   1357      1.1  christos 	  printf_filtered (_(",\n -- loaded at "));
   1358      1.1  christos 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1359      1.1  christos 	  printf_filtered (_(" in overlay section %s"),
   1360      1.1  christos 			   section->the_bfd_section->name);
   1361      1.1  christos 	}
   1362      1.1  christos       break;
   1363      1.1  christos 
   1364      1.1  christos     case LOC_UNRESOLVED:
   1365      1.1  christos       {
   1366      1.1  christos 	struct bound_minimal_symbol msym;
   1367      1.1  christos 
   1368      1.1  christos 	msym = lookup_minimal_symbol_and_objfile (SYMBOL_LINKAGE_NAME (sym));
   1369  1.1.1.2  christos 	if (msym.minsym == NULL)
   1370  1.1.1.2  christos 	  printf_filtered ("unresolved");
   1371      1.1  christos 	else
   1372      1.1  christos 	  {
   1373      1.1  christos 	    section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
   1374      1.1  christos 	    load_addr = BMSYMBOL_VALUE_ADDRESS (msym);
   1375      1.1  christos 
   1376      1.1  christos 	    if (section
   1377      1.1  christos 		&& (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
   1378      1.1  christos 	      printf_filtered (_("a thread-local variable at offset %s "
   1379      1.1  christos 				 "in the thread-local storage for `%s'"),
   1380      1.1  christos 			       paddress (gdbarch, load_addr),
   1381      1.1  christos 			       objfile_name (section->objfile));
   1382      1.1  christos 	    else
   1383      1.1  christos 	      {
   1384      1.1  christos 		printf_filtered (_("static storage at address "));
   1385      1.1  christos 		fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1386      1.1  christos 		if (section_is_overlay (section))
   1387      1.1  christos 		  {
   1388      1.1  christos 		    load_addr = overlay_unmapped_address (load_addr, section);
   1389      1.1  christos 		    printf_filtered (_(",\n -- loaded at "));
   1390      1.1  christos 		    fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
   1391      1.1  christos 		    printf_filtered (_(" in overlay section %s"),
   1392      1.1  christos 				     section->the_bfd_section->name);
   1393      1.1  christos 		  }
   1394      1.1  christos 	      }
   1395      1.1  christos 	  }
   1396      1.1  christos       }
   1397      1.1  christos       break;
   1398      1.1  christos 
   1399      1.1  christos     case LOC_OPTIMIZED_OUT:
   1400      1.1  christos       printf_filtered (_("optimized out"));
   1401      1.1  christos       break;
   1402      1.1  christos 
   1403      1.1  christos     default:
   1404      1.1  christos       printf_filtered (_("of unknown (botched) type"));
   1405      1.1  christos       break;
   1406      1.1  christos     }
   1407      1.1  christos   printf_filtered (".\n");
   1408      1.1  christos }
   1409      1.1  christos 
   1410      1.1  christos 
   1412      1.1  christos static void
   1413      1.1  christos x_command (char *exp, int from_tty)
   1414      1.1  christos {
   1415      1.1  christos   struct expression *expr;
   1416      1.1  christos   struct format_data fmt;
   1417      1.1  christos   struct cleanup *old_chain;
   1418      1.1  christos   struct value *val;
   1419      1.1  christos 
   1420      1.1  christos   fmt.format = last_format ? last_format : 'x';
   1421      1.1  christos   fmt.size = last_size;
   1422      1.1  christos   fmt.count = 1;
   1423      1.1  christos   fmt.raw = 0;
   1424      1.1  christos 
   1425      1.1  christos   if (exp && *exp == '/')
   1426      1.1  christos     {
   1427      1.1  christos       const char *tmp = exp + 1;
   1428      1.1  christos 
   1429      1.1  christos       fmt = decode_format (&tmp, last_format, last_size);
   1430      1.1  christos       exp = (char *) tmp;
   1431      1.1  christos     }
   1432      1.1  christos 
   1433      1.1  christos   /* If we have an expression, evaluate it and use it as the address.  */
   1434      1.1  christos 
   1435      1.1  christos   if (exp != 0 && *exp != 0)
   1436      1.1  christos     {
   1437      1.1  christos       expr = parse_expression (exp);
   1438      1.1  christos       /* Cause expression not to be there any more if this command is
   1439      1.1  christos          repeated with Newline.  But don't clobber a user-defined
   1440      1.1  christos          command's definition.  */
   1441      1.1  christos       if (from_tty)
   1442      1.1  christos 	*exp = 0;
   1443      1.1  christos       old_chain = make_cleanup (free_current_contents, &expr);
   1444      1.1  christos       val = evaluate_expression (expr);
   1445      1.1  christos       if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
   1446      1.1  christos 	val = coerce_ref (val);
   1447      1.1  christos       /* In rvalue contexts, such as this, functions are coerced into
   1448      1.1  christos          pointers to functions.  This makes "x/i main" work.  */
   1449      1.1  christos       if (/* last_format == 'i'  && */
   1450      1.1  christos 	  TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
   1451      1.1  christos 	   && VALUE_LVAL (val) == lval_memory)
   1452      1.1  christos 	next_address = value_address (val);
   1453      1.1  christos       else
   1454      1.1  christos 	next_address = value_as_address (val);
   1455      1.1  christos 
   1456      1.1  christos       next_gdbarch = expr->gdbarch;
   1457      1.1  christos       do_cleanups (old_chain);
   1458      1.1  christos     }
   1459      1.1  christos 
   1460      1.1  christos   if (!next_gdbarch)
   1461      1.1  christos     error_no_arg (_("starting display address"));
   1462      1.1  christos 
   1463      1.1  christos   do_examine (fmt, next_gdbarch, next_address);
   1464      1.1  christos 
   1465      1.1  christos   /* If the examine succeeds, we remember its size and format for next
   1466      1.1  christos      time.  Set last_size to 'b' for strings.  */
   1467      1.1  christos   if (fmt.format == 's')
   1468      1.1  christos     last_size = 'b';
   1469      1.1  christos   else
   1470      1.1  christos     last_size = fmt.size;
   1471      1.1  christos   last_format = fmt.format;
   1472      1.1  christos 
   1473      1.1  christos   /* Set a couple of internal variables if appropriate.  */
   1474      1.1  christos   if (last_examine_value)
   1475      1.1  christos     {
   1476      1.1  christos       /* Make last address examined available to the user as $_.  Use
   1477      1.1  christos          the correct pointer type.  */
   1478      1.1  christos       struct type *pointer_type
   1479      1.1  christos 	= lookup_pointer_type (value_type (last_examine_value));
   1480      1.1  christos       set_internalvar (lookup_internalvar ("_"),
   1481      1.1  christos 		       value_from_pointer (pointer_type,
   1482      1.1  christos 					   last_examine_address));
   1483      1.1  christos 
   1484      1.1  christos       /* Make contents of last address examined available to the user
   1485      1.1  christos 	 as $__.  If the last value has not been fetched from memory
   1486      1.1  christos 	 then don't fetch it now; instead mark it by voiding the $__
   1487      1.1  christos 	 variable.  */
   1488      1.1  christos       if (value_lazy (last_examine_value))
   1489      1.1  christos 	clear_internalvar (lookup_internalvar ("__"));
   1490      1.1  christos       else
   1491      1.1  christos 	set_internalvar (lookup_internalvar ("__"), last_examine_value);
   1492      1.1  christos     }
   1493      1.1  christos }
   1494      1.1  christos 
   1495      1.1  christos 
   1497      1.1  christos /* Add an expression to the auto-display chain.
   1498      1.1  christos    Specify the expression.  */
   1499      1.1  christos 
   1500      1.1  christos static void
   1501      1.1  christos display_command (char *arg, int from_tty)
   1502      1.1  christos {
   1503      1.1  christos   struct format_data fmt;
   1504      1.1  christos   struct expression *expr;
   1505      1.1  christos   struct display *new;
   1506      1.1  christos   int display_it = 1;
   1507      1.1  christos   const char *exp = arg;
   1508      1.1  christos 
   1509      1.1  christos #if defined(TUI)
   1510      1.1  christos   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
   1511      1.1  christos      `tui_version'.  */
   1512      1.1  christos   if (tui_active && exp != NULL && *exp == '$')
   1513      1.1  christos     display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
   1514      1.1  christos #endif
   1515      1.1  christos 
   1516      1.1  christos   if (display_it)
   1517      1.1  christos     {
   1518      1.1  christos       if (exp == 0)
   1519      1.1  christos 	{
   1520      1.1  christos 	  do_displays ();
   1521      1.1  christos 	  return;
   1522      1.1  christos 	}
   1523      1.1  christos 
   1524      1.1  christos       if (*exp == '/')
   1525      1.1  christos 	{
   1526      1.1  christos 	  exp++;
   1527      1.1  christos 	  fmt = decode_format (&exp, 0, 0);
   1528      1.1  christos 	  if (fmt.size && fmt.format == 0)
   1529      1.1  christos 	    fmt.format = 'x';
   1530      1.1  christos 	  if (fmt.format == 'i' || fmt.format == 's')
   1531      1.1  christos 	    fmt.size = 'b';
   1532      1.1  christos 	}
   1533      1.1  christos       else
   1534      1.1  christos 	{
   1535      1.1  christos 	  fmt.format = 0;
   1536      1.1  christos 	  fmt.size = 0;
   1537      1.1  christos 	  fmt.count = 0;
   1538      1.1  christos 	  fmt.raw = 0;
   1539      1.1  christos 	}
   1540      1.1  christos 
   1541      1.1  christos       innermost_block = NULL;
   1542      1.1  christos       expr = parse_expression (exp);
   1543      1.1  christos 
   1544      1.1  christos       new = (struct display *) xmalloc (sizeof (struct display));
   1545      1.1  christos 
   1546      1.1  christos       new->exp_string = xstrdup (exp);
   1547      1.1  christos       new->exp = expr;
   1548      1.1  christos       new->block = innermost_block;
   1549      1.1  christos       new->pspace = current_program_space;
   1550  1.1.1.2  christos       new->next = display_chain;
   1551      1.1  christos       new->number = ++display_number;
   1552      1.1  christos       new->format = fmt;
   1553      1.1  christos       new->enabled_p = 1;
   1554      1.1  christos       display_chain = new;
   1555      1.1  christos 
   1556      1.1  christos       if (from_tty)
   1557      1.1  christos 	do_one_display (new);
   1558      1.1  christos 
   1559      1.1  christos       dont_repeat ();
   1560      1.1  christos     }
   1561      1.1  christos }
   1562      1.1  christos 
   1563      1.1  christos static void
   1564      1.1  christos free_display (struct display *d)
   1565      1.1  christos {
   1566      1.1  christos   xfree (d->exp_string);
   1567      1.1  christos   xfree (d->exp);
   1568      1.1  christos   xfree (d);
   1569      1.1  christos }
   1570      1.1  christos 
   1571      1.1  christos /* Clear out the display_chain.  Done when new symtabs are loaded,
   1572      1.1  christos    since this invalidates the types stored in many expressions.  */
   1573      1.1  christos 
   1574      1.1  christos void
   1575      1.1  christos clear_displays (void)
   1576      1.1  christos {
   1577      1.1  christos   struct display *d;
   1578      1.1  christos 
   1579      1.1  christos   while ((d = display_chain) != NULL)
   1580      1.1  christos     {
   1581      1.1  christos       display_chain = d->next;
   1582      1.1  christos       free_display (d);
   1583      1.1  christos     }
   1584      1.1  christos }
   1585      1.1  christos 
   1586      1.1  christos /* Delete the auto-display DISPLAY.  */
   1587      1.1  christos 
   1588      1.1  christos static void
   1589      1.1  christos delete_display (struct display *display)
   1590      1.1  christos {
   1591      1.1  christos   struct display *d;
   1592      1.1  christos 
   1593      1.1  christos   gdb_assert (display != NULL);
   1594      1.1  christos 
   1595      1.1  christos   if (display_chain == display)
   1596      1.1  christos     display_chain = display->next;
   1597      1.1  christos 
   1598      1.1  christos   ALL_DISPLAYS (d)
   1599      1.1  christos     if (d->next == display)
   1600      1.1  christos       {
   1601      1.1  christos 	d->next = display->next;
   1602      1.1  christos 	break;
   1603      1.1  christos       }
   1604      1.1  christos 
   1605      1.1  christos   free_display (display);
   1606      1.1  christos }
   1607      1.1  christos 
   1608      1.1  christos /* Call FUNCTION on each of the displays whose numbers are given in
   1609      1.1  christos    ARGS.  DATA is passed unmodified to FUNCTION.  */
   1610      1.1  christos 
   1611      1.1  christos static void
   1612      1.1  christos map_display_numbers (char *args,
   1613      1.1  christos 		     void (*function) (struct display *,
   1614      1.1  christos 				       void *),
   1615      1.1  christos 		     void *data)
   1616      1.1  christos {
   1617      1.1  christos   struct get_number_or_range_state state;
   1618      1.1  christos   int num;
   1619      1.1  christos 
   1620      1.1  christos   if (args == NULL)
   1621  1.1.1.2  christos     error_no_arg (_("one or more display numbers"));
   1622      1.1  christos 
   1623      1.1  christos   init_number_or_range (&state, args);
   1624      1.1  christos 
   1625      1.1  christos   while (!state.finished)
   1626      1.1  christos     {
   1627      1.1  christos       const char *p = state.string;
   1628      1.1  christos 
   1629      1.1  christos       num = get_number_or_range (&state);
   1630      1.1  christos       if (num == 0)
   1631      1.1  christos 	warning (_("bad display number at or near '%s'"), p);
   1632      1.1  christos       else
   1633      1.1  christos 	{
   1634      1.1  christos 	  struct display *d, *tmp;
   1635      1.1  christos 
   1636      1.1  christos 	  ALL_DISPLAYS_SAFE (d, tmp)
   1637      1.1  christos 	    if (d->number == num)
   1638      1.1  christos 	      break;
   1639      1.1  christos 	  if (d == NULL)
   1640      1.1  christos 	    printf_unfiltered (_("No display number %d.\n"), num);
   1641      1.1  christos 	  else
   1642      1.1  christos 	    function (d, data);
   1643      1.1  christos 	}
   1644      1.1  christos     }
   1645      1.1  christos }
   1646      1.1  christos 
   1647      1.1  christos /* Callback for map_display_numbers, that deletes a display.  */
   1648      1.1  christos 
   1649      1.1  christos static void
   1650      1.1  christos do_delete_display (struct display *d, void *data)
   1651      1.1  christos {
   1652      1.1  christos   delete_display (d);
   1653      1.1  christos }
   1654      1.1  christos 
   1655      1.1  christos /* "undisplay" command.  */
   1656      1.1  christos 
   1657      1.1  christos static void
   1658      1.1  christos undisplay_command (char *args, int from_tty)
   1659      1.1  christos {
   1660      1.1  christos   if (args == NULL)
   1661      1.1  christos     {
   1662      1.1  christos       if (query (_("Delete all auto-display expressions? ")))
   1663      1.1  christos 	clear_displays ();
   1664      1.1  christos       dont_repeat ();
   1665      1.1  christos       return;
   1666      1.1  christos     }
   1667      1.1  christos 
   1668      1.1  christos   map_display_numbers (args, do_delete_display, NULL);
   1669      1.1  christos   dont_repeat ();
   1670      1.1  christos }
   1671      1.1  christos 
   1672      1.1  christos /* Display a single auto-display.
   1673      1.1  christos    Do nothing if the display cannot be printed in the current context,
   1674      1.1  christos    or if the display is disabled.  */
   1675      1.1  christos 
   1676      1.1  christos static void
   1677      1.1  christos do_one_display (struct display *d)
   1678      1.1  christos {
   1679      1.1  christos   struct cleanup *old_chain;
   1680      1.1  christos   int within_current_scope;
   1681      1.1  christos 
   1682      1.1  christos   if (d->enabled_p == 0)
   1683      1.1  christos     return;
   1684      1.1  christos 
   1685      1.1  christos   /* The expression carries the architecture that was used at parse time.
   1686      1.1  christos      This is a problem if the expression depends on architecture features
   1687      1.1  christos      (e.g. register numbers), and the current architecture is now different.
   1688      1.1  christos      For example, a display statement like "display/i $pc" is expected to
   1689      1.1  christos      display the PC register of the current architecture, not the arch at
   1690      1.1  christos      the time the display command was given.  Therefore, we re-parse the
   1691      1.1  christos      expression if the current architecture has changed.  */
   1692      1.1  christos   if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
   1693      1.1  christos     {
   1694      1.1  christos       xfree (d->exp);
   1695      1.1  christos       d->exp = NULL;
   1696      1.1  christos       d->block = NULL;
   1697      1.1  christos     }
   1698      1.1  christos 
   1699      1.1  christos   if (d->exp == NULL)
   1700      1.1  christos     {
   1701      1.1  christos       volatile struct gdb_exception ex;
   1702      1.1  christos 
   1703      1.1  christos       TRY_CATCH (ex, RETURN_MASK_ALL)
   1704      1.1  christos 	{
   1705      1.1  christos 	  innermost_block = NULL;
   1706      1.1  christos 	  d->exp = parse_expression (d->exp_string);
   1707      1.1  christos 	  d->block = innermost_block;
   1708      1.1  christos 	}
   1709      1.1  christos       if (ex.reason < 0)
   1710      1.1  christos 	{
   1711      1.1  christos 	  /* Can't re-parse the expression.  Disable this display item.  */
   1712      1.1  christos 	  d->enabled_p = 0;
   1713      1.1  christos 	  warning (_("Unable to display \"%s\": %s"),
   1714      1.1  christos 		   d->exp_string, ex.message);
   1715      1.1  christos 	  return;
   1716      1.1  christos 	}
   1717      1.1  christos     }
   1718      1.1  christos 
   1719      1.1  christos   if (d->block)
   1720      1.1  christos     {
   1721      1.1  christos       if (d->pspace == current_program_space)
   1722      1.1  christos 	within_current_scope = contained_in (get_selected_block (0), d->block);
   1723      1.1  christos       else
   1724      1.1  christos 	within_current_scope = 0;
   1725      1.1  christos     }
   1726      1.1  christos   else
   1727      1.1  christos     within_current_scope = 1;
   1728      1.1  christos   if (!within_current_scope)
   1729      1.1  christos     return;
   1730      1.1  christos 
   1731      1.1  christos   old_chain = make_cleanup_restore_integer (&current_display_number);
   1732      1.1  christos   current_display_number = d->number;
   1733      1.1  christos 
   1734      1.1  christos   annotate_display_begin ();
   1735      1.1  christos   printf_filtered ("%d", d->number);
   1736      1.1  christos   annotate_display_number_end ();
   1737      1.1  christos   printf_filtered (": ");
   1738      1.1  christos   if (d->format.size)
   1739      1.1  christos     {
   1740      1.1  christos       volatile struct gdb_exception ex;
   1741      1.1  christos 
   1742      1.1  christos       annotate_display_format ();
   1743      1.1  christos 
   1744      1.1  christos       printf_filtered ("x/");
   1745      1.1  christos       if (d->format.count != 1)
   1746      1.1  christos 	printf_filtered ("%d", d->format.count);
   1747      1.1  christos       printf_filtered ("%c", d->format.format);
   1748      1.1  christos       if (d->format.format != 'i' && d->format.format != 's')
   1749      1.1  christos 	printf_filtered ("%c", d->format.size);
   1750      1.1  christos       printf_filtered (" ");
   1751      1.1  christos 
   1752      1.1  christos       annotate_display_expression ();
   1753      1.1  christos 
   1754      1.1  christos       puts_filtered (d->exp_string);
   1755      1.1  christos       annotate_display_expression_end ();
   1756      1.1  christos 
   1757      1.1  christos       if (d->format.count != 1 || d->format.format == 'i')
   1758      1.1  christos 	printf_filtered ("\n");
   1759      1.1  christos       else
   1760      1.1  christos 	printf_filtered ("  ");
   1761      1.1  christos 
   1762      1.1  christos       annotate_display_value ();
   1763      1.1  christos 
   1764      1.1  christos       TRY_CATCH (ex, RETURN_MASK_ERROR)
   1765      1.1  christos         {
   1766      1.1  christos 	  struct value *val;
   1767      1.1  christos 	  CORE_ADDR addr;
   1768      1.1  christos 
   1769      1.1  christos 	  val = evaluate_expression (d->exp);
   1770      1.1  christos 	  addr = value_as_address (val);
   1771      1.1  christos 	  if (d->format.format == 'i')
   1772      1.1  christos 	    addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
   1773      1.1  christos 	  do_examine (d->format, d->exp->gdbarch, addr);
   1774      1.1  christos 	}
   1775      1.1  christos       if (ex.reason < 0)
   1776      1.1  christos 	fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
   1777      1.1  christos     }
   1778      1.1  christos   else
   1779      1.1  christos     {
   1780      1.1  christos       struct value_print_options opts;
   1781      1.1  christos       volatile struct gdb_exception ex;
   1782      1.1  christos 
   1783      1.1  christos       annotate_display_format ();
   1784      1.1  christos 
   1785      1.1  christos       if (d->format.format)
   1786      1.1  christos 	printf_filtered ("/%c ", d->format.format);
   1787      1.1  christos 
   1788      1.1  christos       annotate_display_expression ();
   1789      1.1  christos 
   1790      1.1  christos       puts_filtered (d->exp_string);
   1791      1.1  christos       annotate_display_expression_end ();
   1792      1.1  christos 
   1793      1.1  christos       printf_filtered (" = ");
   1794      1.1  christos 
   1795      1.1  christos       annotate_display_expression ();
   1796      1.1  christos 
   1797      1.1  christos       get_formatted_print_options (&opts, d->format.format);
   1798      1.1  christos       opts.raw = d->format.raw;
   1799      1.1  christos 
   1800      1.1  christos       TRY_CATCH (ex, RETURN_MASK_ERROR)
   1801      1.1  christos         {
   1802      1.1  christos 	  struct value *val;
   1803      1.1  christos 
   1804      1.1  christos 	  val = evaluate_expression (d->exp);
   1805      1.1  christos 	  print_formatted (val, d->format.size, &opts, gdb_stdout);
   1806      1.1  christos 	}
   1807      1.1  christos       if (ex.reason < 0)
   1808      1.1  christos 	fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
   1809      1.1  christos       printf_filtered ("\n");
   1810      1.1  christos     }
   1811      1.1  christos 
   1812      1.1  christos   annotate_display_end ();
   1813      1.1  christos 
   1814      1.1  christos   gdb_flush (gdb_stdout);
   1815      1.1  christos   do_cleanups (old_chain);
   1816      1.1  christos }
   1817      1.1  christos 
   1818      1.1  christos /* Display all of the values on the auto-display chain which can be
   1819      1.1  christos    evaluated in the current scope.  */
   1820      1.1  christos 
   1821      1.1  christos void
   1822      1.1  christos do_displays (void)
   1823      1.1  christos {
   1824      1.1  christos   struct display *d;
   1825      1.1  christos 
   1826      1.1  christos   for (d = display_chain; d; d = d->next)
   1827      1.1  christos     do_one_display (d);
   1828      1.1  christos }
   1829      1.1  christos 
   1830      1.1  christos /* Delete the auto-display which we were in the process of displaying.
   1831      1.1  christos    This is done when there is an error or a signal.  */
   1832      1.1  christos 
   1833      1.1  christos void
   1834      1.1  christos disable_display (int num)
   1835      1.1  christos {
   1836      1.1  christos   struct display *d;
   1837      1.1  christos 
   1838      1.1  christos   for (d = display_chain; d; d = d->next)
   1839      1.1  christos     if (d->number == num)
   1840      1.1  christos       {
   1841      1.1  christos 	d->enabled_p = 0;
   1842      1.1  christos 	return;
   1843      1.1  christos       }
   1844      1.1  christos   printf_unfiltered (_("No display number %d.\n"), num);
   1845      1.1  christos }
   1846      1.1  christos 
   1847      1.1  christos void
   1848      1.1  christos disable_current_display (void)
   1849      1.1  christos {
   1850      1.1  christos   if (current_display_number >= 0)
   1851      1.1  christos     {
   1852      1.1  christos       disable_display (current_display_number);
   1853      1.1  christos       fprintf_unfiltered (gdb_stderr,
   1854      1.1  christos 			  _("Disabling display %d to "
   1855      1.1  christos 			    "avoid infinite recursion.\n"),
   1856      1.1  christos 			  current_display_number);
   1857      1.1  christos     }
   1858      1.1  christos   current_display_number = -1;
   1859      1.1  christos }
   1860      1.1  christos 
   1861      1.1  christos static void
   1862      1.1  christos display_info (char *ignore, int from_tty)
   1863      1.1  christos {
   1864      1.1  christos   struct display *d;
   1865      1.1  christos 
   1866      1.1  christos   if (!display_chain)
   1867      1.1  christos     printf_unfiltered (_("There are no auto-display expressions now.\n"));
   1868      1.1  christos   else
   1869      1.1  christos     printf_filtered (_("Auto-display expressions now in effect:\n\
   1870      1.1  christos Num Enb Expression\n"));
   1871      1.1  christos 
   1872      1.1  christos   for (d = display_chain; d; d = d->next)
   1873      1.1  christos     {
   1874      1.1  christos       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
   1875      1.1  christos       if (d->format.size)
   1876      1.1  christos 	printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
   1877      1.1  christos 			 d->format.format);
   1878      1.1  christos       else if (d->format.format)
   1879      1.1  christos 	printf_filtered ("/%c ", d->format.format);
   1880      1.1  christos       puts_filtered (d->exp_string);
   1881      1.1  christos       if (d->block && !contained_in (get_selected_block (0), d->block))
   1882      1.1  christos 	printf_filtered (_(" (cannot be evaluated in the current context)"));
   1883      1.1  christos       printf_filtered ("\n");
   1884      1.1  christos       gdb_flush (gdb_stdout);
   1885      1.1  christos     }
   1886      1.1  christos }
   1887      1.1  christos 
   1888      1.1  christos /* Callback fo map_display_numbers, that enables or disables the
   1889      1.1  christos    passed in display D.  */
   1890      1.1  christos 
   1891      1.1  christos static void
   1892      1.1  christos do_enable_disable_display (struct display *d, void *data)
   1893      1.1  christos {
   1894      1.1  christos   d->enabled_p = *(int *) data;
   1895      1.1  christos }
   1896      1.1  christos 
   1897      1.1  christos /* Implamentation of both the "disable display" and "enable display"
   1898      1.1  christos    commands.  ENABLE decides what to do.  */
   1899      1.1  christos 
   1900      1.1  christos static void
   1901      1.1  christos enable_disable_display_command (char *args, int from_tty, int enable)
   1902      1.1  christos {
   1903      1.1  christos   if (args == NULL)
   1904      1.1  christos     {
   1905      1.1  christos       struct display *d;
   1906      1.1  christos 
   1907      1.1  christos       ALL_DISPLAYS (d)
   1908      1.1  christos 	d->enabled_p = enable;
   1909      1.1  christos       return;
   1910      1.1  christos     }
   1911      1.1  christos 
   1912      1.1  christos   map_display_numbers (args, do_enable_disable_display, &enable);
   1913      1.1  christos }
   1914      1.1  christos 
   1915      1.1  christos /* The "enable display" command.  */
   1916      1.1  christos 
   1917      1.1  christos static void
   1918      1.1  christos enable_display_command (char *args, int from_tty)
   1919      1.1  christos {
   1920      1.1  christos   enable_disable_display_command (args, from_tty, 1);
   1921      1.1  christos }
   1922      1.1  christos 
   1923      1.1  christos /* The "disable display" command.  */
   1924      1.1  christos 
   1925      1.1  christos static void
   1926      1.1  christos disable_display_command (char *args, int from_tty)
   1927      1.1  christos {
   1928      1.1  christos   enable_disable_display_command (args, from_tty, 0);
   1929      1.1  christos }
   1930      1.1  christos 
   1931      1.1  christos /* display_chain items point to blocks and expressions.  Some expressions in
   1932      1.1  christos    turn may point to symbols.
   1933      1.1  christos    Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
   1934      1.1  christos    obstack_free'd when a shared library is unloaded.
   1935      1.1  christos    Clear pointers that are about to become dangling.
   1936      1.1  christos    Both .exp and .block fields will be restored next time we need to display
   1937      1.1  christos    an item by re-parsing .exp_string field in the new execution context.  */
   1938      1.1  christos 
   1939      1.1  christos static void
   1940      1.1  christos clear_dangling_display_expressions (struct objfile *objfile)
   1941      1.1  christos {
   1942      1.1  christos   struct display *d;
   1943      1.1  christos   struct program_space *pspace;
   1944      1.1  christos 
   1945      1.1  christos   /* With no symbol file we cannot have a block or expression from it.  */
   1946      1.1  christos   if (objfile == NULL)
   1947      1.1  christos     return;
   1948      1.1  christos   pspace = objfile->pspace;
   1949      1.1  christos   if (objfile->separate_debug_objfile_backlink)
   1950      1.1  christos     {
   1951      1.1  christos       objfile = objfile->separate_debug_objfile_backlink;
   1952      1.1  christos       gdb_assert (objfile->pspace == pspace);
   1953      1.1  christos     }
   1954      1.1  christos 
   1955      1.1  christos   for (d = display_chain; d != NULL; d = d->next)
   1956      1.1  christos     {
   1957      1.1  christos       if (d->pspace != pspace)
   1958      1.1  christos 	continue;
   1959      1.1  christos 
   1960      1.1  christos       if (lookup_objfile_from_block (d->block) == objfile
   1961      1.1  christos 	  || (d->exp && exp_uses_objfile (d->exp, objfile)))
   1962      1.1  christos       {
   1963      1.1  christos 	xfree (d->exp);
   1964      1.1  christos 	d->exp = NULL;
   1965      1.1  christos 	d->block = NULL;
   1966      1.1  christos       }
   1967      1.1  christos     }
   1968      1.1  christos }
   1969      1.1  christos 
   1970      1.1  christos 
   1972      1.1  christos /* Print the value in stack frame FRAME of a variable specified by a
   1973      1.1  christos    struct symbol.  NAME is the name to print; if NULL then VAR's print
   1974      1.1  christos    name will be used.  STREAM is the ui_file on which to print the
   1975      1.1  christos    value.  INDENT specifies the number of indent levels to print
   1976      1.1  christos    before printing the variable name.
   1977      1.1  christos 
   1978      1.1  christos    This function invalidates FRAME.  */
   1979      1.1  christos 
   1980      1.1  christos void
   1981      1.1  christos print_variable_and_value (const char *name, struct symbol *var,
   1982      1.1  christos 			  struct frame_info *frame,
   1983      1.1  christos 			  struct ui_file *stream, int indent)
   1984      1.1  christos {
   1985      1.1  christos   volatile struct gdb_exception except;
   1986      1.1  christos 
   1987      1.1  christos   if (!name)
   1988      1.1  christos     name = SYMBOL_PRINT_NAME (var);
   1989      1.1  christos 
   1990      1.1  christos   fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
   1991      1.1  christos   TRY_CATCH (except, RETURN_MASK_ERROR)
   1992      1.1  christos     {
   1993      1.1  christos       struct value *val;
   1994      1.1  christos       struct value_print_options opts;
   1995      1.1  christos 
   1996      1.1  christos       val = read_var_value (var, frame);
   1997      1.1  christos       get_user_print_options (&opts);
   1998      1.1  christos       opts.deref_ref = 1;
   1999      1.1  christos       common_val_print (val, stream, indent, &opts, current_language);
   2000      1.1  christos 
   2001      1.1  christos       /* common_val_print invalidates FRAME when a pretty printer calls inferior
   2002      1.1  christos 	 function.  */
   2003      1.1  christos       frame = NULL;
   2004      1.1  christos     }
   2005      1.1  christos   if (except.reason < 0)
   2006      1.1  christos     fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
   2007      1.1  christos 		     except.message);
   2008      1.1  christos   fprintf_filtered (stream, "\n");
   2009      1.1  christos }
   2010      1.1  christos 
   2011      1.1  christos /* Subroutine of ui_printf to simplify it.
   2012      1.1  christos    Print VALUE to STREAM using FORMAT.
   2013      1.1  christos    VALUE is a C-style string on the target.  */
   2014      1.1  christos 
   2015      1.1  christos static void
   2016      1.1  christos printf_c_string (struct ui_file *stream, const char *format,
   2017      1.1  christos 		 struct value *value)
   2018      1.1  christos {
   2019      1.1  christos   gdb_byte *str;
   2020      1.1  christos   CORE_ADDR tem;
   2021      1.1  christos   int j;
   2022      1.1  christos 
   2023      1.1  christos   tem = value_as_address (value);
   2024      1.1  christos 
   2025      1.1  christos   /* This is a %s argument.  Find the length of the string.  */
   2026      1.1  christos   for (j = 0;; j++)
   2027      1.1  christos     {
   2028      1.1  christos       gdb_byte c;
   2029      1.1  christos 
   2030      1.1  christos       QUIT;
   2031      1.1  christos       read_memory (tem + j, &c, 1);
   2032      1.1  christos       if (c == 0)
   2033      1.1  christos 	break;
   2034      1.1  christos     }
   2035      1.1  christos 
   2036      1.1  christos   /* Copy the string contents into a string inside GDB.  */
   2037      1.1  christos   str = (gdb_byte *) alloca (j + 1);
   2038      1.1  christos   if (j != 0)
   2039      1.1  christos     read_memory (tem, str, j);
   2040      1.1  christos   str[j] = 0;
   2041      1.1  christos 
   2042      1.1  christos   fprintf_filtered (stream, format, (char *) str);
   2043      1.1  christos }
   2044      1.1  christos 
   2045      1.1  christos /* Subroutine of ui_printf to simplify it.
   2046      1.1  christos    Print VALUE to STREAM using FORMAT.
   2047      1.1  christos    VALUE is a wide C-style string on the target.  */
   2048      1.1  christos 
   2049      1.1  christos static void
   2050      1.1  christos printf_wide_c_string (struct ui_file *stream, const char *format,
   2051      1.1  christos 		      struct value *value)
   2052      1.1  christos {
   2053      1.1  christos   gdb_byte *str;
   2054      1.1  christos   CORE_ADDR tem;
   2055      1.1  christos   int j;
   2056      1.1  christos   struct gdbarch *gdbarch = get_type_arch (value_type (value));
   2057      1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2058      1.1  christos   struct type *wctype = lookup_typename (current_language, gdbarch,
   2059      1.1  christos 					 "wchar_t", NULL, 0);
   2060      1.1  christos   int wcwidth = TYPE_LENGTH (wctype);
   2061      1.1  christos   gdb_byte *buf = alloca (wcwidth);
   2062      1.1  christos   struct obstack output;
   2063      1.1  christos   struct cleanup *inner_cleanup;
   2064      1.1  christos 
   2065      1.1  christos   tem = value_as_address (value);
   2066      1.1  christos 
   2067      1.1  christos   /* This is a %s argument.  Find the length of the string.  */
   2068      1.1  christos   for (j = 0;; j += wcwidth)
   2069      1.1  christos     {
   2070      1.1  christos       QUIT;
   2071      1.1  christos       read_memory (tem + j, buf, wcwidth);
   2072      1.1  christos       if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
   2073      1.1  christos 	break;
   2074      1.1  christos     }
   2075      1.1  christos 
   2076      1.1  christos   /* Copy the string contents into a string inside GDB.  */
   2077      1.1  christos   str = (gdb_byte *) alloca (j + wcwidth);
   2078      1.1  christos   if (j != 0)
   2079      1.1  christos     read_memory (tem, str, j);
   2080      1.1  christos   memset (&str[j], 0, wcwidth);
   2081      1.1  christos 
   2082      1.1  christos   obstack_init (&output);
   2083      1.1  christos   inner_cleanup = make_cleanup_obstack_free (&output);
   2084      1.1  christos 
   2085      1.1  christos   convert_between_encodings (target_wide_charset (gdbarch),
   2086      1.1  christos 			     host_charset (),
   2087      1.1  christos 			     str, j, wcwidth,
   2088      1.1  christos 			     &output, translit_char);
   2089      1.1  christos   obstack_grow_str0 (&output, "");
   2090      1.1  christos 
   2091      1.1  christos   fprintf_filtered (stream, format, obstack_base (&output));
   2092      1.1  christos   do_cleanups (inner_cleanup);
   2093      1.1  christos }
   2094      1.1  christos 
   2095      1.1  christos /* Subroutine of ui_printf to simplify it.
   2096      1.1  christos    Print VALUE, a decimal floating point value, to STREAM using FORMAT.  */
   2097      1.1  christos 
   2098      1.1  christos static void
   2099      1.1  christos printf_decfloat (struct ui_file *stream, const char *format,
   2100      1.1  christos 		 struct value *value)
   2101      1.1  christos {
   2102      1.1  christos   const gdb_byte *param_ptr = value_contents (value);
   2103      1.1  christos 
   2104      1.1  christos #if defined (PRINTF_HAS_DECFLOAT)
   2105      1.1  christos   /* If we have native support for Decimal floating
   2106      1.1  christos      printing, handle it here.  */
   2107      1.1  christos   fprintf_filtered (stream, format, param_ptr);
   2108      1.1  christos #else
   2109      1.1  christos   /* As a workaround until vasprintf has native support for DFP
   2110      1.1  christos      we convert the DFP values to string and print them using
   2111      1.1  christos      the %s format specifier.  */
   2112      1.1  christos   const char *p;
   2113      1.1  christos 
   2114      1.1  christos   /* Parameter data.  */
   2115      1.1  christos   struct type *param_type = value_type (value);
   2116      1.1  christos   struct gdbarch *gdbarch = get_type_arch (param_type);
   2117      1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2118      1.1  christos 
   2119      1.1  christos   /* DFP output data.  */
   2120      1.1  christos   struct value *dfp_value = NULL;
   2121      1.1  christos   gdb_byte *dfp_ptr;
   2122      1.1  christos   int dfp_len = 16;
   2123      1.1  christos   gdb_byte dec[16];
   2124      1.1  christos   struct type *dfp_type = NULL;
   2125      1.1  christos   char decstr[MAX_DECIMAL_STRING];
   2126      1.1  christos 
   2127      1.1  christos   /* Points to the end of the string so that we can go back
   2128      1.1  christos      and check for DFP length modifiers.  */
   2129      1.1  christos   p = format + strlen (format);
   2130      1.1  christos 
   2131      1.1  christos   /* Look for the float/double format specifier.  */
   2132      1.1  christos   while (*p != 'f' && *p != 'e' && *p != 'E'
   2133      1.1  christos 	 && *p != 'g' && *p != 'G')
   2134      1.1  christos     p--;
   2135      1.1  christos 
   2136      1.1  christos   /* Search for the '%' char and extract the size and type of
   2137      1.1  christos      the output decimal value based on its modifiers
   2138      1.1  christos      (%Hf, %Df, %DDf).  */
   2139      1.1  christos   while (*--p != '%')
   2140      1.1  christos     {
   2141      1.1  christos       if (*p == 'H')
   2142      1.1  christos 	{
   2143      1.1  christos 	  dfp_len = 4;
   2144      1.1  christos 	  dfp_type = builtin_type (gdbarch)->builtin_decfloat;
   2145      1.1  christos 	}
   2146      1.1  christos       else if (*p == 'D' && *(p - 1) == 'D')
   2147      1.1  christos 	{
   2148      1.1  christos 	  dfp_len = 16;
   2149      1.1  christos 	  dfp_type = builtin_type (gdbarch)->builtin_declong;
   2150      1.1  christos 	  p--;
   2151      1.1  christos 	}
   2152      1.1  christos       else
   2153      1.1  christos 	{
   2154      1.1  christos 	  dfp_len = 8;
   2155      1.1  christos 	  dfp_type = builtin_type (gdbarch)->builtin_decdouble;
   2156      1.1  christos 	}
   2157      1.1  christos     }
   2158      1.1  christos 
   2159      1.1  christos   /* Conversion between different DFP types.  */
   2160      1.1  christos   if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
   2161      1.1  christos     decimal_convert (param_ptr, TYPE_LENGTH (param_type),
   2162      1.1  christos 		     byte_order, dec, dfp_len, byte_order);
   2163      1.1  christos   else
   2164      1.1  christos     /* If this is a non-trivial conversion, just output 0.
   2165      1.1  christos        A correct converted value can be displayed by explicitly
   2166      1.1  christos        casting to a DFP type.  */
   2167      1.1  christos     decimal_from_string (dec, dfp_len, byte_order, "0");
   2168      1.1  christos 
   2169      1.1  christos   dfp_value = value_from_decfloat (dfp_type, dec);
   2170      1.1  christos 
   2171      1.1  christos   dfp_ptr = (gdb_byte *) value_contents (dfp_value);
   2172      1.1  christos 
   2173      1.1  christos   decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
   2174      1.1  christos 
   2175      1.1  christos   /* Print the DFP value.  */
   2176      1.1  christos   fprintf_filtered (stream, "%s", decstr);
   2177      1.1  christos #endif
   2178      1.1  christos }
   2179      1.1  christos 
   2180      1.1  christos /* Subroutine of ui_printf to simplify it.
   2181      1.1  christos    Print VALUE, a target pointer, to STREAM using FORMAT.  */
   2182      1.1  christos 
   2183      1.1  christos static void
   2184      1.1  christos printf_pointer (struct ui_file *stream, const char *format,
   2185      1.1  christos 		struct value *value)
   2186      1.1  christos {
   2187      1.1  christos   /* We avoid the host's %p because pointers are too
   2188      1.1  christos      likely to be the wrong size.  The only interesting
   2189      1.1  christos      modifier for %p is a width; extract that, and then
   2190      1.1  christos      handle %p as glibc would: %#x or a literal "(nil)".  */
   2191      1.1  christos 
   2192      1.1  christos   const char *p;
   2193      1.1  christos   char *fmt, *fmt_p;
   2194      1.1  christos #ifdef PRINTF_HAS_LONG_LONG
   2195      1.1  christos   long long val = value_as_long (value);
   2196      1.1  christos #else
   2197      1.1  christos   long val = value_as_long (value);
   2198      1.1  christos #endif
   2199      1.1  christos 
   2200      1.1  christos   fmt = alloca (strlen (format) + 5);
   2201      1.1  christos 
   2202      1.1  christos   /* Copy up to the leading %.  */
   2203      1.1  christos   p = format;
   2204      1.1  christos   fmt_p = fmt;
   2205      1.1  christos   while (*p)
   2206      1.1  christos     {
   2207      1.1  christos       int is_percent = (*p == '%');
   2208      1.1  christos 
   2209      1.1  christos       *fmt_p++ = *p++;
   2210      1.1  christos       if (is_percent)
   2211      1.1  christos 	{
   2212      1.1  christos 	  if (*p == '%')
   2213      1.1  christos 	    *fmt_p++ = *p++;
   2214      1.1  christos 	  else
   2215      1.1  christos 	    break;
   2216      1.1  christos 	}
   2217      1.1  christos     }
   2218      1.1  christos 
   2219      1.1  christos   if (val != 0)
   2220      1.1  christos     *fmt_p++ = '#';
   2221      1.1  christos 
   2222      1.1  christos   /* Copy any width.  */
   2223      1.1  christos   while (*p >= '0' && *p < '9')
   2224      1.1  christos     *fmt_p++ = *p++;
   2225      1.1  christos 
   2226      1.1  christos   gdb_assert (*p == 'p' && *(p + 1) == '\0');
   2227      1.1  christos   if (val != 0)
   2228      1.1  christos     {
   2229      1.1  christos #ifdef PRINTF_HAS_LONG_LONG
   2230      1.1  christos       *fmt_p++ = 'l';
   2231      1.1  christos #endif
   2232      1.1  christos       *fmt_p++ = 'l';
   2233      1.1  christos       *fmt_p++ = 'x';
   2234      1.1  christos       *fmt_p++ = '\0';
   2235      1.1  christos       fprintf_filtered (stream, fmt, val);
   2236      1.1  christos     }
   2237      1.1  christos   else
   2238      1.1  christos     {
   2239      1.1  christos       *fmt_p++ = 's';
   2240      1.1  christos       *fmt_p++ = '\0';
   2241      1.1  christos       fprintf_filtered (stream, fmt, "(nil)");
   2242      1.1  christos     }
   2243      1.1  christos }
   2244      1.1  christos 
   2245      1.1  christos /* printf "printf format string" ARG to STREAM.  */
   2246      1.1  christos 
   2247      1.1  christos static void
   2248      1.1  christos ui_printf (const char *arg, struct ui_file *stream)
   2249      1.1  christos {
   2250      1.1  christos   struct format_piece *fpieces;
   2251      1.1  christos   const char *s = arg;
   2252      1.1  christos   struct value **val_args;
   2253      1.1  christos   int allocated_args = 20;
   2254      1.1  christos   struct cleanup *old_cleanups;
   2255      1.1  christos 
   2256      1.1  christos   val_args = xmalloc (allocated_args * sizeof (struct value *));
   2257      1.1  christos   old_cleanups = make_cleanup (free_current_contents, &val_args);
   2258      1.1  christos 
   2259      1.1  christos   if (s == 0)
   2260      1.1  christos     error_no_arg (_("format-control string and values to print"));
   2261      1.1  christos 
   2262      1.1  christos   s = skip_spaces_const (s);
   2263      1.1  christos 
   2264      1.1  christos   /* A format string should follow, enveloped in double quotes.  */
   2265      1.1  christos   if (*s++ != '"')
   2266      1.1  christos     error (_("Bad format string, missing '\"'."));
   2267      1.1  christos 
   2268      1.1  christos   fpieces = parse_format_string (&s);
   2269      1.1  christos 
   2270      1.1  christos   make_cleanup (free_format_pieces_cleanup, &fpieces);
   2271      1.1  christos 
   2272      1.1  christos   if (*s++ != '"')
   2273      1.1  christos     error (_("Bad format string, non-terminated '\"'."));
   2274      1.1  christos 
   2275      1.1  christos   s = skip_spaces_const (s);
   2276      1.1  christos 
   2277      1.1  christos   if (*s != ',' && *s != 0)
   2278      1.1  christos     error (_("Invalid argument syntax"));
   2279      1.1  christos 
   2280      1.1  christos   if (*s == ',')
   2281      1.1  christos     s++;
   2282      1.1  christos   s = skip_spaces_const (s);
   2283      1.1  christos 
   2284      1.1  christos   {
   2285      1.1  christos     int nargs = 0;
   2286      1.1  christos     int nargs_wanted;
   2287      1.1  christos     int i, fr;
   2288      1.1  christos     char *current_substring;
   2289      1.1  christos 
   2290      1.1  christos     nargs_wanted = 0;
   2291      1.1  christos     for (fr = 0; fpieces[fr].string != NULL; fr++)
   2292      1.1  christos       if (fpieces[fr].argclass != literal_piece)
   2293      1.1  christos 	++nargs_wanted;
   2294      1.1  christos 
   2295      1.1  christos     /* Now, parse all arguments and evaluate them.
   2296      1.1  christos        Store the VALUEs in VAL_ARGS.  */
   2297      1.1  christos 
   2298      1.1  christos     while (*s != '\0')
   2299      1.1  christos       {
   2300      1.1  christos 	const char *s1;
   2301      1.1  christos 
   2302      1.1  christos 	if (nargs == allocated_args)
   2303      1.1  christos 	  val_args = (struct value **) xrealloc ((char *) val_args,
   2304      1.1  christos 						 (allocated_args *= 2)
   2305      1.1  christos 						 * sizeof (struct value *));
   2306      1.1  christos 	s1 = s;
   2307      1.1  christos 	val_args[nargs] = parse_to_comma_and_eval (&s1);
   2308      1.1  christos 
   2309      1.1  christos 	nargs++;
   2310      1.1  christos 	s = s1;
   2311      1.1  christos 	if (*s == ',')
   2312      1.1  christos 	  s++;
   2313      1.1  christos       }
   2314      1.1  christos 
   2315      1.1  christos     if (nargs != nargs_wanted)
   2316      1.1  christos       error (_("Wrong number of arguments for specified format-string"));
   2317      1.1  christos 
   2318      1.1  christos     /* Now actually print them.  */
   2319      1.1  christos     i = 0;
   2320      1.1  christos     for (fr = 0; fpieces[fr].string != NULL; fr++)
   2321      1.1  christos       {
   2322      1.1  christos 	current_substring = fpieces[fr].string;
   2323      1.1  christos 	switch (fpieces[fr].argclass)
   2324      1.1  christos 	  {
   2325      1.1  christos 	  case string_arg:
   2326      1.1  christos 	    printf_c_string (stream, current_substring, val_args[i]);
   2327      1.1  christos 	    break;
   2328      1.1  christos 	  case wide_string_arg:
   2329      1.1  christos 	    printf_wide_c_string (stream, current_substring, val_args[i]);
   2330      1.1  christos 	    break;
   2331      1.1  christos 	  case wide_char_arg:
   2332      1.1  christos 	    {
   2333      1.1  christos 	      struct gdbarch *gdbarch
   2334      1.1  christos 		= get_type_arch (value_type (val_args[i]));
   2335      1.1  christos 	      struct type *wctype = lookup_typename (current_language, gdbarch,
   2336      1.1  christos 						     "wchar_t", NULL, 0);
   2337      1.1  christos 	      struct type *valtype;
   2338      1.1  christos 	      struct obstack output;
   2339      1.1  christos 	      struct cleanup *inner_cleanup;
   2340      1.1  christos 	      const gdb_byte *bytes;
   2341      1.1  christos 
   2342      1.1  christos 	      valtype = value_type (val_args[i]);
   2343      1.1  christos 	      if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
   2344      1.1  christos 		  || TYPE_CODE (valtype) != TYPE_CODE_INT)
   2345      1.1  christos 		error (_("expected wchar_t argument for %%lc"));
   2346      1.1  christos 
   2347      1.1  christos 	      bytes = value_contents (val_args[i]);
   2348      1.1  christos 
   2349      1.1  christos 	      obstack_init (&output);
   2350      1.1  christos 	      inner_cleanup = make_cleanup_obstack_free (&output);
   2351      1.1  christos 
   2352      1.1  christos 	      convert_between_encodings (target_wide_charset (gdbarch),
   2353      1.1  christos 					 host_charset (),
   2354      1.1  christos 					 bytes, TYPE_LENGTH (valtype),
   2355      1.1  christos 					 TYPE_LENGTH (valtype),
   2356      1.1  christos 					 &output, translit_char);
   2357      1.1  christos 	      obstack_grow_str0 (&output, "");
   2358      1.1  christos 
   2359      1.1  christos 	      fprintf_filtered (stream, current_substring,
   2360      1.1  christos                                 obstack_base (&output));
   2361      1.1  christos 	      do_cleanups (inner_cleanup);
   2362      1.1  christos 	    }
   2363      1.1  christos 	    break;
   2364      1.1  christos 	  case double_arg:
   2365      1.1  christos 	    {
   2366      1.1  christos 	      struct type *type = value_type (val_args[i]);
   2367      1.1  christos 	      DOUBLEST val;
   2368      1.1  christos 	      int inv;
   2369      1.1  christos 
   2370      1.1  christos 	      /* If format string wants a float, unchecked-convert the value
   2371      1.1  christos 		 to floating point of the same size.  */
   2372      1.1  christos 	      type = float_type_from_length (type);
   2373      1.1  christos 	      val = unpack_double (type, value_contents (val_args[i]), &inv);
   2374      1.1  christos 	      if (inv)
   2375      1.1  christos 		error (_("Invalid floating value found in program."));
   2376      1.1  christos 
   2377      1.1  christos               fprintf_filtered (stream, current_substring, (double) val);
   2378      1.1  christos 	      break;
   2379      1.1  christos 	    }
   2380      1.1  christos 	  case long_double_arg:
   2381      1.1  christos #ifdef HAVE_LONG_DOUBLE
   2382      1.1  christos 	    {
   2383      1.1  christos 	      struct type *type = value_type (val_args[i]);
   2384      1.1  christos 	      DOUBLEST val;
   2385      1.1  christos 	      int inv;
   2386      1.1  christos 
   2387      1.1  christos 	      /* If format string wants a float, unchecked-convert the value
   2388      1.1  christos 		 to floating point of the same size.  */
   2389      1.1  christos 	      type = float_type_from_length (type);
   2390      1.1  christos 	      val = unpack_double (type, value_contents (val_args[i]), &inv);
   2391      1.1  christos 	      if (inv)
   2392      1.1  christos 		error (_("Invalid floating value found in program."));
   2393      1.1  christos 
   2394      1.1  christos 	      fprintf_filtered (stream, current_substring,
   2395      1.1  christos                                 (long double) val);
   2396      1.1  christos 	      break;
   2397      1.1  christos 	    }
   2398      1.1  christos #else
   2399      1.1  christos 	    error (_("long double not supported in printf"));
   2400      1.1  christos #endif
   2401      1.1  christos 	  case long_long_arg:
   2402      1.1  christos #ifdef PRINTF_HAS_LONG_LONG
   2403      1.1  christos 	    {
   2404      1.1  christos 	      long long val = value_as_long (val_args[i]);
   2405      1.1  christos 
   2406      1.1  christos               fprintf_filtered (stream, current_substring, val);
   2407      1.1  christos 	      break;
   2408      1.1  christos 	    }
   2409      1.1  christos #else
   2410      1.1  christos 	    error (_("long long not supported in printf"));
   2411      1.1  christos #endif
   2412      1.1  christos 	  case int_arg:
   2413      1.1  christos 	    {
   2414      1.1  christos 	      int val = value_as_long (val_args[i]);
   2415      1.1  christos 
   2416      1.1  christos               fprintf_filtered (stream, current_substring, val);
   2417      1.1  christos 	      break;
   2418      1.1  christos 	    }
   2419      1.1  christos 	  case long_arg:
   2420      1.1  christos 	    {
   2421      1.1  christos 	      long val = value_as_long (val_args[i]);
   2422      1.1  christos 
   2423      1.1  christos               fprintf_filtered (stream, current_substring, val);
   2424      1.1  christos 	      break;
   2425      1.1  christos 	    }
   2426      1.1  christos 	  /* Handles decimal floating values.  */
   2427      1.1  christos 	  case decfloat_arg:
   2428      1.1  christos 	    printf_decfloat (stream, current_substring, val_args[i]);
   2429      1.1  christos 	    break;
   2430      1.1  christos 	  case ptr_arg:
   2431      1.1  christos 	    printf_pointer (stream, current_substring, val_args[i]);
   2432      1.1  christos 	    break;
   2433      1.1  christos 	  case literal_piece:
   2434      1.1  christos 	    /* Print a portion of the format string that has no
   2435      1.1  christos 	       directives.  Note that this will not include any
   2436      1.1  christos 	       ordinary %-specs, but it might include "%%".  That is
   2437      1.1  christos 	       why we use printf_filtered and not puts_filtered here.
   2438      1.1  christos 	       Also, we pass a dummy argument because some platforms
   2439      1.1  christos 	       have modified GCC to include -Wformat-security by
   2440      1.1  christos 	       default, which will warn here if there is no
   2441      1.1  christos 	       argument.  */
   2442      1.1  christos 	    fprintf_filtered (stream, current_substring, 0);
   2443      1.1  christos 	    break;
   2444      1.1  christos 	  default:
   2445      1.1  christos 	    internal_error (__FILE__, __LINE__,
   2446      1.1  christos 			    _("failed internal consistency check"));
   2447      1.1  christos 	  }
   2448      1.1  christos 	/* Maybe advance to the next argument.  */
   2449      1.1  christos 	if (fpieces[fr].argclass != literal_piece)
   2450      1.1  christos 	  ++i;
   2451      1.1  christos       }
   2452      1.1  christos   }
   2453      1.1  christos   do_cleanups (old_cleanups);
   2454      1.1  christos }
   2455      1.1  christos 
   2456      1.1  christos /* Implement the "printf" command.  */
   2457      1.1  christos 
   2458      1.1  christos static void
   2459      1.1  christos printf_command (char *arg, int from_tty)
   2460      1.1  christos {
   2461      1.1  christos   ui_printf (arg, gdb_stdout);
   2462      1.1  christos   gdb_flush (gdb_stdout);
   2463      1.1  christos }
   2464      1.1  christos 
   2465      1.1  christos /* Implement the "eval" command.  */
   2466      1.1  christos 
   2467      1.1  christos static void
   2468      1.1  christos eval_command (char *arg, int from_tty)
   2469      1.1  christos {
   2470      1.1  christos   struct ui_file *ui_out = mem_fileopen ();
   2471      1.1  christos   struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
   2472      1.1  christos   char *expanded;
   2473      1.1  christos 
   2474      1.1  christos   ui_printf (arg, ui_out);
   2475      1.1  christos 
   2476      1.1  christos   expanded = ui_file_xstrdup (ui_out, NULL);
   2477      1.1  christos   make_cleanup (xfree, expanded);
   2478      1.1  christos 
   2479      1.1  christos   execute_command (expanded, from_tty);
   2480      1.1  christos 
   2481      1.1  christos   do_cleanups (cleanups);
   2482      1.1  christos }
   2483      1.1  christos 
   2484      1.1  christos void
   2485      1.1  christos _initialize_printcmd (void)
   2486      1.1  christos {
   2487      1.1  christos   struct cmd_list_element *c;
   2488      1.1  christos 
   2489      1.1  christos   current_display_number = -1;
   2490      1.1  christos 
   2491      1.1  christos   observer_attach_free_objfile (clear_dangling_display_expressions);
   2492      1.1  christos 
   2493      1.1  christos   add_info ("address", address_info,
   2494      1.1  christos 	    _("Describe where symbol SYM is stored."));
   2495      1.1  christos 
   2496      1.1  christos   add_info ("symbol", sym_info, _("\
   2497      1.1  christos Describe what symbol is at location ADDR.\n\
   2498      1.1  christos Only for symbols with fixed locations (global or static scope)."));
   2499      1.1  christos 
   2500      1.1  christos   add_com ("x", class_vars, x_command, _("\
   2501      1.1  christos Examine memory: x/FMT ADDRESS.\n\
   2502      1.1  christos ADDRESS is an expression for the memory address to examine.\n\
   2503      1.1  christos FMT is a repeat count followed by a format letter and a size letter.\n\
   2504      1.1  christos Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
   2505      1.1  christos   t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
   2506      1.1  christos   and z(hex, zero padded on the left).\n\
   2507      1.1  christos Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
   2508      1.1  christos The specified number of objects of the specified size are printed\n\
   2509      1.1  christos according to the format.\n\n\
   2510      1.1  christos Defaults for format and size letters are those previously used.\n\
   2511      1.1  christos Default count is 1.  Default address is following last thing printed\n\
   2512      1.1  christos with this command or \"print\"."));
   2513      1.1  christos 
   2514      1.1  christos #if 0
   2515      1.1  christos   add_com ("whereis", class_vars, whereis_command,
   2516      1.1  christos 	   _("Print line number and file of definition of variable."));
   2517      1.1  christos #endif
   2518      1.1  christos 
   2519      1.1  christos   add_info ("display", display_info, _("\
   2520      1.1  christos Expressions to display when program stops, with code numbers."));
   2521      1.1  christos 
   2522      1.1  christos   add_cmd ("undisplay", class_vars, undisplay_command, _("\
   2523      1.1  christos Cancel some expressions to be displayed when program stops.\n\
   2524      1.1  christos Arguments are the code numbers of the expressions to stop displaying.\n\
   2525      1.1  christos No argument means cancel all automatic-display expressions.\n\
   2526      1.1  christos \"delete display\" has the same effect as this command.\n\
   2527      1.1  christos Do \"info display\" to see current list of code numbers."),
   2528      1.1  christos 	   &cmdlist);
   2529      1.1  christos 
   2530      1.1  christos   add_com ("display", class_vars, display_command, _("\
   2531      1.1  christos Print value of expression EXP each time the program stops.\n\
   2532      1.1  christos /FMT may be used before EXP as in the \"print\" command.\n\
   2533      1.1  christos /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
   2534      1.1  christos as in the \"x\" command, and then EXP is used to get the address to examine\n\
   2535      1.1  christos and examining is done as in the \"x\" command.\n\n\
   2536      1.1  christos With no argument, display all currently requested auto-display expressions.\n\
   2537      1.1  christos Use \"undisplay\" to cancel display requests previously made."));
   2538      1.1  christos 
   2539      1.1  christos   add_cmd ("display", class_vars, enable_display_command, _("\
   2540      1.1  christos Enable some expressions to be displayed when program stops.\n\
   2541      1.1  christos Arguments are the code numbers of the expressions to resume displaying.\n\
   2542      1.1  christos No argument means enable all automatic-display expressions.\n\
   2543      1.1  christos Do \"info display\" to see current list of code numbers."), &enablelist);
   2544      1.1  christos 
   2545      1.1  christos   add_cmd ("display", class_vars, disable_display_command, _("\
   2546      1.1  christos Disable some expressions to be displayed when program stops.\n\
   2547      1.1  christos Arguments are the code numbers of the expressions to stop displaying.\n\
   2548      1.1  christos No argument means disable all automatic-display expressions.\n\
   2549      1.1  christos Do \"info display\" to see current list of code numbers."), &disablelist);
   2550      1.1  christos 
   2551      1.1  christos   add_cmd ("display", class_vars, undisplay_command, _("\
   2552      1.1  christos Cancel some expressions to be displayed when program stops.\n\
   2553      1.1  christos Arguments are the code numbers of the expressions to stop displaying.\n\
   2554      1.1  christos No argument means cancel all automatic-display expressions.\n\
   2555      1.1  christos Do \"info display\" to see current list of code numbers."), &deletelist);
   2556      1.1  christos 
   2557      1.1  christos   add_com ("printf", class_vars, printf_command, _("\
   2558      1.1  christos printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
   2559      1.1  christos This is useful for formatted output in user-defined commands."));
   2560      1.1  christos 
   2561      1.1  christos   add_com ("output", class_vars, output_command, _("\
   2562      1.1  christos Like \"print\" but don't put in value history and don't print newline.\n\
   2563      1.1  christos This is useful in user-defined commands."));
   2564      1.1  christos 
   2565      1.1  christos   add_prefix_cmd ("set", class_vars, set_command, _("\
   2566      1.1  christos Evaluate expression EXP and assign result to variable VAR, using assignment\n\
   2567      1.1  christos syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
   2568      1.1  christos example).  VAR may be a debugger \"convenience\" variable (names starting\n\
   2569      1.1  christos with $), a register (a few standard names starting with $), or an actual\n\
   2570      1.1  christos variable in the program being debugged.  EXP is any valid expression.\n\
   2571      1.1  christos Use \"set variable\" for variables with names identical to set subcommands.\n\
   2572      1.1  christos \n\
   2573      1.1  christos With a subcommand, this command modifies parts of the gdb environment.\n\
   2574      1.1  christos You can see these environment settings with the \"show\" command."),
   2575      1.1  christos 		  &setlist, "set ", 1, &cmdlist);
   2576      1.1  christos   if (dbx_commands)
   2577      1.1  christos     add_com ("assign", class_vars, set_command, _("\
   2578      1.1  christos Evaluate expression EXP and assign result to variable VAR, using assignment\n\
   2579      1.1  christos syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
   2580      1.1  christos example).  VAR may be a debugger \"convenience\" variable (names starting\n\
   2581      1.1  christos with $), a register (a few standard names starting with $), or an actual\n\
   2582      1.1  christos variable in the program being debugged.  EXP is any valid expression.\n\
   2583      1.1  christos Use \"set variable\" for variables with names identical to set subcommands.\n\
   2584      1.1  christos \nWith a subcommand, this command modifies parts of the gdb environment.\n\
   2585      1.1  christos You can see these environment settings with the \"show\" command."));
   2586      1.1  christos 
   2587      1.1  christos   /* "call" is the same as "set", but handy for dbx users to call fns.  */
   2588      1.1  christos   c = add_com ("call", class_vars, call_command, _("\
   2589      1.1  christos Call a function in the program.\n\
   2590      1.1  christos The argument is the function name and arguments, in the notation of the\n\
   2591      1.1  christos current working language.  The result is printed and saved in the value\n\
   2592      1.1  christos history, if it is not void."));
   2593      1.1  christos   set_cmd_completer (c, expression_completer);
   2594      1.1  christos 
   2595      1.1  christos   add_cmd ("variable", class_vars, set_command, _("\
   2596      1.1  christos Evaluate expression EXP and assign result to variable VAR, using assignment\n\
   2597      1.1  christos syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
   2598      1.1  christos example).  VAR may be a debugger \"convenience\" variable (names starting\n\
   2599      1.1  christos with $), a register (a few standard names starting with $), or an actual\n\
   2600      1.1  christos variable in the program being debugged.  EXP is any valid expression.\n\
   2601      1.1  christos This may usually be abbreviated to simply \"set\"."),
   2602      1.1  christos 	   &setlist);
   2603      1.1  christos 
   2604      1.1  christos   c = add_com ("print", class_vars, print_command, _("\
   2605      1.1  christos Print value of expression EXP.\n\
   2606      1.1  christos Variables accessible are those of the lexical environment of the selected\n\
   2607      1.1  christos stack frame, plus all those whose scope is global or an entire file.\n\
   2608      1.1  christos \n\
   2609      1.1  christos $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
   2610      1.1  christos $$NUM refers to NUM'th value back from the last one.\n\
   2611      1.1  christos Names starting with $ refer to registers (with the values they would have\n\
   2612      1.1  christos if the program were to return to the stack frame now selected, restoring\n\
   2613      1.1  christos all registers saved by frames farther in) or else to debugger\n\
   2614      1.1  christos \"convenience\" variables (any such name not a known register).\n\
   2615      1.1  christos Use assignment expressions to give values to convenience variables.\n\
   2616      1.1  christos \n\
   2617      1.1  christos {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
   2618      1.1  christos @ is a binary operator for treating consecutive data objects\n\
   2619      1.1  christos anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
   2620      1.1  christos element is FOO, whose second element is stored in the space following\n\
   2621      1.1  christos where FOO is stored, etc.  FOO must be an expression whose value\n\
   2622      1.1  christos resides in memory.\n\
   2623      1.1  christos \n\
   2624      1.1  christos EXP may be preceded with /FMT, where FMT is a format letter\n\
   2625      1.1  christos but no count or size letter (see \"x\" command)."));
   2626      1.1  christos   set_cmd_completer (c, expression_completer);
   2627      1.1  christos   add_com_alias ("p", "print", class_vars, 1);
   2628      1.1  christos   add_com_alias ("inspect", "print", class_vars, 1);
   2629      1.1  christos 
   2630      1.1  christos   add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
   2631      1.1  christos 			    &max_symbolic_offset, _("\
   2632      1.1  christos Set the largest offset that will be printed in <symbol+1234> form."), _("\
   2633      1.1  christos Show the largest offset that will be printed in <symbol+1234> form."), _("\
   2634      1.1  christos Tell GDB to only display the symbolic form of an address if the\n\
   2635      1.1  christos offset between the closest earlier symbol and the address is less than\n\
   2636      1.1  christos the specified maximum offset.  The default is \"unlimited\", which tells GDB\n\
   2637      1.1  christos to always print the symbolic form of an address if any symbol precedes\n\
   2638      1.1  christos it.  Zero is equivalent to \"unlimited\"."),
   2639      1.1  christos 			    NULL,
   2640      1.1  christos 			    show_max_symbolic_offset,
   2641      1.1  christos 			    &setprintlist, &showprintlist);
   2642      1.1  christos   add_setshow_boolean_cmd ("symbol-filename", no_class,
   2643      1.1  christos 			   &print_symbol_filename, _("\
   2644      1.1  christos Set printing of source filename and line number with <symbol>."), _("\
   2645      1.1  christos Show printing of source filename and line number with <symbol>."), NULL,
   2646      1.1  christos 			   NULL,
   2647                    			   show_print_symbol_filename,
   2648                    			   &setprintlist, &showprintlist);
   2649                    
   2650                      add_com ("eval", no_class, eval_command, _("\
   2651                    Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
   2652                    a command line, and call it."));
   2653                    }
   2654