xref: /dflybsd-src/contrib/gdb-7/gdb/printcmd.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Print values for GNU debugger GDB.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1986-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdb_string.h"
225796c8dcSSimon Schubert #include "frame.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "value.h"
265796c8dcSSimon Schubert #include "language.h"
275796c8dcSSimon Schubert #include "expression.h"
285796c8dcSSimon Schubert #include "gdbcore.h"
295796c8dcSSimon Schubert #include "gdbcmd.h"
305796c8dcSSimon Schubert #include "target.h"
315796c8dcSSimon Schubert #include "breakpoint.h"
325796c8dcSSimon Schubert #include "demangle.h"
33*a45ae5f8SJohn Marino #include "gdb-demangle.h"
345796c8dcSSimon Schubert #include "valprint.h"
355796c8dcSSimon Schubert #include "annotate.h"
365796c8dcSSimon Schubert #include "symfile.h"		/* for overlay functions */
375796c8dcSSimon Schubert #include "objfiles.h"		/* ditto */
385796c8dcSSimon Schubert #include "completer.h"		/* for completion functions */
395796c8dcSSimon Schubert #include "ui-out.h"
405796c8dcSSimon Schubert #include "gdb_assert.h"
415796c8dcSSimon Schubert #include "block.h"
425796c8dcSSimon Schubert #include "disasm.h"
435796c8dcSSimon Schubert #include "dfp.h"
445796c8dcSSimon Schubert #include "valprint.h"
455796c8dcSSimon Schubert #include "exceptions.h"
465796c8dcSSimon Schubert #include "observer.h"
475796c8dcSSimon Schubert #include "solist.h"
485796c8dcSSimon Schubert #include "parser-defs.h"
495796c8dcSSimon Schubert #include "charset.h"
50cf7f2e2dSJohn Marino #include "arch-utils.h"
51c50c785cSJohn Marino #include "cli/cli-utils.h"
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert #ifdef TUI
54c50c785cSJohn Marino #include "tui/tui.h"		/* For tui_active et al.   */
555796c8dcSSimon Schubert #endif
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert #if defined(__MINGW32__) && !defined(PRINTF_HAS_LONG_LONG)
585796c8dcSSimon Schubert # define USE_PRINTF_I64 1
595796c8dcSSimon Schubert # define PRINTF_HAS_LONG_LONG
605796c8dcSSimon Schubert #else
615796c8dcSSimon Schubert # define USE_PRINTF_I64 0
625796c8dcSSimon Schubert #endif
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert struct format_data
655796c8dcSSimon Schubert   {
665796c8dcSSimon Schubert     int count;
675796c8dcSSimon Schubert     char format;
685796c8dcSSimon Schubert     char size;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert     /* True if the value should be printed raw -- that is, bypassing
715796c8dcSSimon Schubert        python-based formatters.  */
725796c8dcSSimon Schubert     unsigned char raw;
735796c8dcSSimon Schubert   };
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert /* Last specified output format.  */
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert static char last_format = 0;
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static char last_size = 'w';
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert /* Default address to examine next, and associated architecture.  */
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert static struct gdbarch *next_gdbarch;
865796c8dcSSimon Schubert static CORE_ADDR next_address;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* Number of delay instructions following current disassembled insn.  */
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static int branch_delay_insns;
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert /* Last address examined.  */
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert static CORE_ADDR last_examine_address;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert /* Contents of last address examined.
975796c8dcSSimon Schubert    This is not valid past the end of the `x' command!  */
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert static struct value *last_examine_value;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert /* Largest offset between a symbolic value and an address, that will be
1025796c8dcSSimon Schubert    printed as `0x1234 <symbol+offset>'.  */
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert static unsigned int max_symbolic_offset = UINT_MAX;
1055796c8dcSSimon Schubert static void
1065796c8dcSSimon Schubert show_max_symbolic_offset (struct ui_file *file, int from_tty,
1075796c8dcSSimon Schubert 			  struct cmd_list_element *c, const char *value)
1085796c8dcSSimon Schubert {
109c50c785cSJohn Marino   fprintf_filtered (file,
110c50c785cSJohn Marino 		    _("The largest offset that will be "
111c50c785cSJohn Marino 		      "printed in <symbol+1234> form is %s.\n"),
1125796c8dcSSimon Schubert 		    value);
1135796c8dcSSimon Schubert }
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert /* Append the source filename and linenumber of the symbol when
1165796c8dcSSimon Schubert    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
1175796c8dcSSimon Schubert static int print_symbol_filename = 0;
1185796c8dcSSimon Schubert static void
1195796c8dcSSimon Schubert show_print_symbol_filename (struct ui_file *file, int from_tty,
1205796c8dcSSimon Schubert 			    struct cmd_list_element *c, const char *value)
1215796c8dcSSimon Schubert {
122c50c785cSJohn Marino   fprintf_filtered (file, _("Printing of source filename and "
123c50c785cSJohn Marino 			    "line number with <symbol> is %s.\n"),
1245796c8dcSSimon Schubert 		    value);
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert /* Number of auto-display expression currently being displayed.
128*a45ae5f8SJohn Marino    So that we can disable it if we get a signal within it.
1295796c8dcSSimon Schubert    -1 when not doing one.  */
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert int current_display_number;
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert struct display
1345796c8dcSSimon Schubert   {
1355796c8dcSSimon Schubert     /* Chain link to next auto-display item.  */
1365796c8dcSSimon Schubert     struct display *next;
137cf7f2e2dSJohn Marino 
1385796c8dcSSimon Schubert     /* The expression as the user typed it.  */
1395796c8dcSSimon Schubert     char *exp_string;
140cf7f2e2dSJohn Marino 
1415796c8dcSSimon Schubert     /* Expression to be evaluated and displayed.  */
1425796c8dcSSimon Schubert     struct expression *exp;
143cf7f2e2dSJohn Marino 
1445796c8dcSSimon Schubert     /* Item number of this auto-display item.  */
1455796c8dcSSimon Schubert     int number;
146cf7f2e2dSJohn Marino 
1475796c8dcSSimon Schubert     /* Display format specified.  */
1485796c8dcSSimon Schubert     struct format_data format;
149cf7f2e2dSJohn Marino 
150cf7f2e2dSJohn Marino     /* Program space associated with `block'.  */
151cf7f2e2dSJohn Marino     struct program_space *pspace;
152cf7f2e2dSJohn Marino 
153c50c785cSJohn Marino     /* Innermost block required by this expression when evaluated.  */
1545796c8dcSSimon Schubert     struct block *block;
155cf7f2e2dSJohn Marino 
156c50c785cSJohn Marino     /* Status of this display (enabled or disabled).  */
1575796c8dcSSimon Schubert     int enabled_p;
1585796c8dcSSimon Schubert   };
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert /* Chain of expressions whose values should be displayed
1615796c8dcSSimon Schubert    automatically each time the program stops.  */
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert static struct display *display_chain;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert static int display_number;
1665796c8dcSSimon Schubert 
167c50c785cSJohn Marino /* Walk the following statement or block through all displays.
168c50c785cSJohn Marino    ALL_DISPLAYS_SAFE does so even if the statement deletes the current
169c50c785cSJohn Marino    display.  */
170c50c785cSJohn Marino 
171c50c785cSJohn Marino #define ALL_DISPLAYS(B)				\
172c50c785cSJohn Marino   for (B = display_chain; B; B = B->next)
173c50c785cSJohn Marino 
174c50c785cSJohn Marino #define ALL_DISPLAYS_SAFE(B,TMP)		\
175c50c785cSJohn Marino   for (B = display_chain;			\
176c50c785cSJohn Marino        B ? (TMP = B->next, 1): 0;		\
177c50c785cSJohn Marino        B = TMP)
178c50c785cSJohn Marino 
1795796c8dcSSimon Schubert /* Prototypes for exported functions.  */
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert void output_command (char *, int);
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert void _initialize_printcmd (void);
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert /* Prototypes for local functions.  */
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert static void do_one_display (struct display *);
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert /* Decode a format specification.  *STRING_PTR should point to it.
1915796c8dcSSimon Schubert    OFORMAT and OSIZE are used as defaults for the format and size
1925796c8dcSSimon Schubert    if none are given in the format specification.
1935796c8dcSSimon Schubert    If OSIZE is zero, then the size field of the returned value
1945796c8dcSSimon Schubert    should be set only if a size is explicitly specified by the
1955796c8dcSSimon Schubert    user.
1965796c8dcSSimon Schubert    The structure returned describes all the data
1975796c8dcSSimon Schubert    found in the specification.  In addition, *STRING_PTR is advanced
1985796c8dcSSimon Schubert    past the specification and past all whitespace following it.  */
1995796c8dcSSimon Schubert 
2005796c8dcSSimon Schubert static struct format_data
2015796c8dcSSimon Schubert decode_format (char **string_ptr, int oformat, int osize)
2025796c8dcSSimon Schubert {
2035796c8dcSSimon Schubert   struct format_data val;
2045796c8dcSSimon Schubert   char *p = *string_ptr;
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert   val.format = '?';
2075796c8dcSSimon Schubert   val.size = '?';
2085796c8dcSSimon Schubert   val.count = 1;
2095796c8dcSSimon Schubert   val.raw = 0;
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert   if (*p >= '0' && *p <= '9')
2125796c8dcSSimon Schubert     val.count = atoi (p);
2135796c8dcSSimon Schubert   while (*p >= '0' && *p <= '9')
2145796c8dcSSimon Schubert     p++;
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert   /* Now process size or format letters that follow.  */
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert   while (1)
2195796c8dcSSimon Schubert     {
2205796c8dcSSimon Schubert       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
2215796c8dcSSimon Schubert 	val.size = *p++;
2225796c8dcSSimon Schubert       else if (*p == 'r')
2235796c8dcSSimon Schubert 	{
2245796c8dcSSimon Schubert 	  val.raw = 1;
2255796c8dcSSimon Schubert 	  p++;
2265796c8dcSSimon Schubert 	}
2275796c8dcSSimon Schubert       else if (*p >= 'a' && *p <= 'z')
2285796c8dcSSimon Schubert 	val.format = *p++;
2295796c8dcSSimon Schubert       else
2305796c8dcSSimon Schubert 	break;
2315796c8dcSSimon Schubert     }
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert   while (*p == ' ' || *p == '\t')
2345796c8dcSSimon Schubert     p++;
2355796c8dcSSimon Schubert   *string_ptr = p;
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert   /* Set defaults for format and size if not specified.  */
2385796c8dcSSimon Schubert   if (val.format == '?')
2395796c8dcSSimon Schubert     {
2405796c8dcSSimon Schubert       if (val.size == '?')
2415796c8dcSSimon Schubert 	{
2425796c8dcSSimon Schubert 	  /* Neither has been specified.  */
2435796c8dcSSimon Schubert 	  val.format = oformat;
2445796c8dcSSimon Schubert 	  val.size = osize;
2455796c8dcSSimon Schubert 	}
2465796c8dcSSimon Schubert       else
2475796c8dcSSimon Schubert 	/* If a size is specified, any format makes a reasonable
2485796c8dcSSimon Schubert 	   default except 'i'.  */
2495796c8dcSSimon Schubert 	val.format = oformat == 'i' ? 'x' : oformat;
2505796c8dcSSimon Schubert     }
2515796c8dcSSimon Schubert   else if (val.size == '?')
2525796c8dcSSimon Schubert     switch (val.format)
2535796c8dcSSimon Schubert       {
2545796c8dcSSimon Schubert       case 'a':
2555796c8dcSSimon Schubert 	/* Pick the appropriate size for an address.  This is deferred
2565796c8dcSSimon Schubert 	   until do_examine when we know the actual architecture to use.
2575796c8dcSSimon Schubert 	   A special size value of 'a' is used to indicate this case.  */
2585796c8dcSSimon Schubert 	val.size = osize ? 'a' : osize;
2595796c8dcSSimon Schubert 	break;
2605796c8dcSSimon Schubert       case 'f':
2615796c8dcSSimon Schubert 	/* Floating point has to be word or giantword.  */
2625796c8dcSSimon Schubert 	if (osize == 'w' || osize == 'g')
2635796c8dcSSimon Schubert 	  val.size = osize;
2645796c8dcSSimon Schubert 	else
2655796c8dcSSimon Schubert 	  /* Default it to giantword if the last used size is not
2665796c8dcSSimon Schubert 	     appropriate.  */
2675796c8dcSSimon Schubert 	  val.size = osize ? 'g' : osize;
2685796c8dcSSimon Schubert 	break;
2695796c8dcSSimon Schubert       case 'c':
2705796c8dcSSimon Schubert 	/* Characters default to one byte.  */
2715796c8dcSSimon Schubert 	val.size = osize ? 'b' : osize;
2725796c8dcSSimon Schubert 	break;
273cf7f2e2dSJohn Marino       case 's':
274c50c785cSJohn Marino 	/* Display strings with byte size chars unless explicitly
275c50c785cSJohn Marino 	   specified.  */
276cf7f2e2dSJohn Marino 	val.size = '\0';
277cf7f2e2dSJohn Marino 	break;
278cf7f2e2dSJohn Marino 
2795796c8dcSSimon Schubert       default:
2805796c8dcSSimon Schubert 	/* The default is the size most recently specified.  */
2815796c8dcSSimon Schubert 	val.size = osize;
2825796c8dcSSimon Schubert       }
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert   return val;
2855796c8dcSSimon Schubert }
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert /* Print value VAL on stream according to OPTIONS.
2885796c8dcSSimon Schubert    Do not end with a newline.
2895796c8dcSSimon Schubert    SIZE is the letter for the size of datum being printed.
2905796c8dcSSimon Schubert    This is used to pad hex numbers so they line up.  SIZE is 0
2915796c8dcSSimon Schubert    for print / output and set for examine.  */
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert static void
2945796c8dcSSimon Schubert print_formatted (struct value *val, int size,
2955796c8dcSSimon Schubert 		 const struct value_print_options *options,
2965796c8dcSSimon Schubert 		 struct ui_file *stream)
2975796c8dcSSimon Schubert {
2985796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (val));
2995796c8dcSSimon Schubert   int len = TYPE_LENGTH (type);
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   if (VALUE_LVAL (val) == lval_memory)
3025796c8dcSSimon Schubert     next_address = value_address (val) + len;
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert   if (size)
3055796c8dcSSimon Schubert     {
3065796c8dcSSimon Schubert       switch (options->format)
3075796c8dcSSimon Schubert 	{
3085796c8dcSSimon Schubert 	case 's':
3095796c8dcSSimon Schubert 	  {
3105796c8dcSSimon Schubert 	    struct type *elttype = value_type (val);
311cf7f2e2dSJohn Marino 
3125796c8dcSSimon Schubert 	    next_address = (value_address (val)
313c50c785cSJohn Marino 			    + val_print_string (elttype, NULL,
3145796c8dcSSimon Schubert 						value_address (val), -1,
315cf7f2e2dSJohn Marino 						stream, options) * len);
3165796c8dcSSimon Schubert 	  }
3175796c8dcSSimon Schubert 	  return;
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert 	case 'i':
3205796c8dcSSimon Schubert 	  /* We often wrap here if there are long symbolic names.  */
3215796c8dcSSimon Schubert 	  wrap_here ("    ");
3225796c8dcSSimon Schubert 	  next_address = (value_address (val)
3235796c8dcSSimon Schubert 			  + gdb_print_insn (get_type_arch (type),
3245796c8dcSSimon Schubert 					    value_address (val), stream,
3255796c8dcSSimon Schubert 					    &branch_delay_insns));
3265796c8dcSSimon Schubert 	  return;
3275796c8dcSSimon Schubert 	}
3285796c8dcSSimon Schubert     }
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   if (options->format == 0 || options->format == 's'
3315796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_REF
3325796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_ARRAY
3335796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_STRING
3345796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_STRUCT
3355796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_UNION
3365796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3375796c8dcSSimon Schubert     value_print (val, stream, options);
3385796c8dcSSimon Schubert   else
339c50c785cSJohn Marino     /* User specified format, so don't look to the type to tell us
340c50c785cSJohn Marino        what to do.  */
341c50c785cSJohn Marino     val_print_scalar_formatted (type,
342c50c785cSJohn Marino 				value_contents_for_printing (val),
343c50c785cSJohn Marino 				value_embedded_offset (val),
344c50c785cSJohn Marino 				val,
3455796c8dcSSimon Schubert 				options, size, stream);
3465796c8dcSSimon Schubert }
3475796c8dcSSimon Schubert 
3485796c8dcSSimon Schubert /* Return builtin floating point type of same length as TYPE.
3495796c8dcSSimon Schubert    If no such type is found, return TYPE itself.  */
3505796c8dcSSimon Schubert static struct type *
3515796c8dcSSimon Schubert float_type_from_length (struct type *type)
3525796c8dcSSimon Schubert {
3535796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
3545796c8dcSSimon Schubert   const struct builtin_type *builtin = builtin_type (gdbarch);
3555796c8dcSSimon Schubert   unsigned int len = TYPE_LENGTH (type);
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   if (len == TYPE_LENGTH (builtin->builtin_float))
3585796c8dcSSimon Schubert     type = builtin->builtin_float;
3595796c8dcSSimon Schubert   else if (len == TYPE_LENGTH (builtin->builtin_double))
3605796c8dcSSimon Schubert     type = builtin->builtin_double;
3615796c8dcSSimon Schubert   else if (len == TYPE_LENGTH (builtin->builtin_long_double))
3625796c8dcSSimon Schubert     type = builtin->builtin_long_double;
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   return type;
3655796c8dcSSimon Schubert }
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
368c50c785cSJohn Marino    according to OPTIONS and SIZE on STREAM.  Formats s and i are not
369c50c785cSJohn Marino    supported at this level.  */
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert void
3725796c8dcSSimon Schubert print_scalar_formatted (const void *valaddr, struct type *type,
3735796c8dcSSimon Schubert 			const struct value_print_options *options,
3745796c8dcSSimon Schubert 			int size, struct ui_file *stream)
3755796c8dcSSimon Schubert {
3765796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
3775796c8dcSSimon Schubert   LONGEST val_long = 0;
3785796c8dcSSimon Schubert   unsigned int len = TYPE_LENGTH (type);
3795796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3805796c8dcSSimon Schubert 
381c50c785cSJohn Marino   /* String printing should go through val_print_scalar_formatted.  */
382c50c785cSJohn Marino   gdb_assert (options->format != 's');
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert   if (len > sizeof(LONGEST) &&
3855796c8dcSSimon Schubert       (TYPE_CODE (type) == TYPE_CODE_INT
3865796c8dcSSimon Schubert        || TYPE_CODE (type) == TYPE_CODE_ENUM))
3875796c8dcSSimon Schubert     {
3885796c8dcSSimon Schubert       switch (options->format)
3895796c8dcSSimon Schubert 	{
3905796c8dcSSimon Schubert 	case 'o':
3915796c8dcSSimon Schubert 	  print_octal_chars (stream, valaddr, len, byte_order);
3925796c8dcSSimon Schubert 	  return;
3935796c8dcSSimon Schubert 	case 'u':
3945796c8dcSSimon Schubert 	case 'd':
3955796c8dcSSimon Schubert 	  print_decimal_chars (stream, valaddr, len, byte_order);
3965796c8dcSSimon Schubert 	  return;
3975796c8dcSSimon Schubert 	case 't':
3985796c8dcSSimon Schubert 	  print_binary_chars (stream, valaddr, len, byte_order);
3995796c8dcSSimon Schubert 	  return;
4005796c8dcSSimon Schubert 	case 'x':
4015796c8dcSSimon Schubert 	  print_hex_chars (stream, valaddr, len, byte_order);
4025796c8dcSSimon Schubert 	  return;
4035796c8dcSSimon Schubert 	case 'c':
4045796c8dcSSimon Schubert 	  print_char_chars (stream, type, valaddr, len, byte_order);
4055796c8dcSSimon Schubert 	  return;
4065796c8dcSSimon Schubert 	default:
4075796c8dcSSimon Schubert 	  break;
4085796c8dcSSimon Schubert 	};
4095796c8dcSSimon Schubert     }
4105796c8dcSSimon Schubert 
4115796c8dcSSimon Schubert   if (options->format != 'f')
4125796c8dcSSimon Schubert     val_long = unpack_long (type, valaddr);
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   /* If the value is a pointer, and pointers and addresses are not the
4155796c8dcSSimon Schubert      same, then at this point, the value's length (in target bytes) is
4165796c8dcSSimon Schubert      gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
4175796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_PTR)
4185796c8dcSSimon Schubert     len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   /* If we are printing it as unsigned, truncate it in case it is actually
4215796c8dcSSimon Schubert      a negative signed value (e.g. "print/u (short)-1" should print 65535
4225796c8dcSSimon Schubert      (if shorts are 16 bits) instead of 4294967295).  */
4235796c8dcSSimon Schubert   if (options->format != 'd' || TYPE_UNSIGNED (type))
4245796c8dcSSimon Schubert     {
4255796c8dcSSimon Schubert       if (len < sizeof (LONGEST))
4265796c8dcSSimon Schubert 	val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
4275796c8dcSSimon Schubert     }
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   switch (options->format)
4305796c8dcSSimon Schubert     {
4315796c8dcSSimon Schubert     case 'x':
4325796c8dcSSimon Schubert       if (!size)
4335796c8dcSSimon Schubert 	{
4345796c8dcSSimon Schubert 	  /* No size specified, like in print.  Print varying # of digits.  */
4355796c8dcSSimon Schubert 	  print_longest (stream, 'x', 1, val_long);
4365796c8dcSSimon Schubert 	}
4375796c8dcSSimon Schubert       else
4385796c8dcSSimon Schubert 	switch (size)
4395796c8dcSSimon Schubert 	  {
4405796c8dcSSimon Schubert 	  case 'b':
4415796c8dcSSimon Schubert 	  case 'h':
4425796c8dcSSimon Schubert 	  case 'w':
4435796c8dcSSimon Schubert 	  case 'g':
4445796c8dcSSimon Schubert 	    print_longest (stream, size, 1, val_long);
4455796c8dcSSimon Schubert 	    break;
4465796c8dcSSimon Schubert 	  default:
4475796c8dcSSimon Schubert 	    error (_("Undefined output size \"%c\"."), size);
4485796c8dcSSimon Schubert 	  }
4495796c8dcSSimon Schubert       break;
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert     case 'd':
4525796c8dcSSimon Schubert       print_longest (stream, 'd', 1, val_long);
4535796c8dcSSimon Schubert       break;
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert     case 'u':
4565796c8dcSSimon Schubert       print_longest (stream, 'u', 0, val_long);
4575796c8dcSSimon Schubert       break;
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert     case 'o':
4605796c8dcSSimon Schubert       if (val_long)
4615796c8dcSSimon Schubert 	print_longest (stream, 'o', 1, val_long);
4625796c8dcSSimon Schubert       else
4635796c8dcSSimon Schubert 	fprintf_filtered (stream, "0");
4645796c8dcSSimon Schubert       break;
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert     case 'a':
4675796c8dcSSimon Schubert       {
4685796c8dcSSimon Schubert 	CORE_ADDR addr = unpack_pointer (type, valaddr);
469cf7f2e2dSJohn Marino 
4705796c8dcSSimon Schubert 	print_address (gdbarch, addr, stream);
4715796c8dcSSimon Schubert       }
4725796c8dcSSimon Schubert       break;
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert     case 'c':
4755796c8dcSSimon Schubert       {
4765796c8dcSSimon Schubert 	struct value_print_options opts = *options;
4775796c8dcSSimon Schubert 
478cf7f2e2dSJohn Marino 	opts.format = 0;
4795796c8dcSSimon Schubert 	if (TYPE_UNSIGNED (type))
4805796c8dcSSimon Schubert 	  type = builtin_type (gdbarch)->builtin_true_unsigned_char;
4815796c8dcSSimon Schubert  	else
4825796c8dcSSimon Schubert 	  type = builtin_type (gdbarch)->builtin_true_char;
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert 	value_print (value_from_longest (type, val_long), stream, &opts);
4855796c8dcSSimon Schubert       }
4865796c8dcSSimon Schubert       break;
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert     case 'f':
4895796c8dcSSimon Schubert       type = float_type_from_length (type);
4905796c8dcSSimon Schubert       print_floating (valaddr, type, stream);
4915796c8dcSSimon Schubert       break;
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert     case 0:
4945796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
4955796c8dcSSimon Schubert 		      _("failed internal consistency check"));
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert     case 't':
4985796c8dcSSimon Schubert       /* Binary; 't' stands for "two".  */
4995796c8dcSSimon Schubert       {
5005796c8dcSSimon Schubert 	char bits[8 * (sizeof val_long) + 1];
5015796c8dcSSimon Schubert 	char buf[8 * (sizeof val_long) + 32];
5025796c8dcSSimon Schubert 	char *cp = bits;
5035796c8dcSSimon Schubert 	int width;
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert 	if (!size)
5065796c8dcSSimon Schubert 	  width = 8 * (sizeof val_long);
5075796c8dcSSimon Schubert 	else
5085796c8dcSSimon Schubert 	  switch (size)
5095796c8dcSSimon Schubert 	    {
5105796c8dcSSimon Schubert 	    case 'b':
5115796c8dcSSimon Schubert 	      width = 8;
5125796c8dcSSimon Schubert 	      break;
5135796c8dcSSimon Schubert 	    case 'h':
5145796c8dcSSimon Schubert 	      width = 16;
5155796c8dcSSimon Schubert 	      break;
5165796c8dcSSimon Schubert 	    case 'w':
5175796c8dcSSimon Schubert 	      width = 32;
5185796c8dcSSimon Schubert 	      break;
5195796c8dcSSimon Schubert 	    case 'g':
5205796c8dcSSimon Schubert 	      width = 64;
5215796c8dcSSimon Schubert 	      break;
5225796c8dcSSimon Schubert 	    default:
5235796c8dcSSimon Schubert 	      error (_("Undefined output size \"%c\"."), size);
5245796c8dcSSimon Schubert 	    }
5255796c8dcSSimon Schubert 
5265796c8dcSSimon Schubert 	bits[width] = '\0';
5275796c8dcSSimon Schubert 	while (width-- > 0)
5285796c8dcSSimon Schubert 	  {
5295796c8dcSSimon Schubert 	    bits[width] = (val_long & 1) ? '1' : '0';
5305796c8dcSSimon Schubert 	    val_long >>= 1;
5315796c8dcSSimon Schubert 	  }
5325796c8dcSSimon Schubert 	if (!size)
5335796c8dcSSimon Schubert 	  {
5345796c8dcSSimon Schubert 	    while (*cp && *cp == '0')
5355796c8dcSSimon Schubert 	      cp++;
5365796c8dcSSimon Schubert 	    if (*cp == '\0')
5375796c8dcSSimon Schubert 	      cp--;
5385796c8dcSSimon Schubert 	  }
539c50c785cSJohn Marino 	strncpy (buf, cp, sizeof (bits));
5405796c8dcSSimon Schubert 	fputs_filtered (buf, stream);
5415796c8dcSSimon Schubert       }
5425796c8dcSSimon Schubert       break;
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert     default:
5455796c8dcSSimon Schubert       error (_("Undefined output format \"%c\"."), options->format);
5465796c8dcSSimon Schubert     }
5475796c8dcSSimon Schubert }
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert /* Specify default address for `x' command.
5505796c8dcSSimon Schubert    The `info lines' command uses this.  */
5515796c8dcSSimon Schubert 
5525796c8dcSSimon Schubert void
5535796c8dcSSimon Schubert set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
5545796c8dcSSimon Schubert {
5555796c8dcSSimon Schubert   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert   next_gdbarch = gdbarch;
5585796c8dcSSimon Schubert   next_address = addr;
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   /* Make address available to the user as $_.  */
5615796c8dcSSimon Schubert   set_internalvar (lookup_internalvar ("_"),
5625796c8dcSSimon Schubert 		   value_from_pointer (ptr_type, addr));
5635796c8dcSSimon Schubert }
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
5665796c8dcSSimon Schubert    after LEADIN.  Print nothing if no symbolic name is found nearby.
5675796c8dcSSimon Schubert    Optionally also print source file and line number, if available.
5685796c8dcSSimon Schubert    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
5695796c8dcSSimon Schubert    or to interpret it as a possible C++ name and convert it back to source
5705796c8dcSSimon Schubert    form.  However note that DO_DEMANGLE can be overridden by the specific
5715796c8dcSSimon Schubert    settings of the demangle and asm_demangle variables.  */
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert void
574cf7f2e2dSJohn Marino print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
575cf7f2e2dSJohn Marino 			struct ui_file *stream,
5765796c8dcSSimon Schubert 			int do_demangle, char *leadin)
5775796c8dcSSimon Schubert {
5785796c8dcSSimon Schubert   char *name = NULL;
5795796c8dcSSimon Schubert   char *filename = NULL;
5805796c8dcSSimon Schubert   int unmapped = 0;
5815796c8dcSSimon Schubert   int offset = 0;
5825796c8dcSSimon Schubert   int line = 0;
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert   /* Throw away both name and filename.  */
5855796c8dcSSimon Schubert   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
5865796c8dcSSimon Schubert   make_cleanup (free_current_contents, &filename);
5875796c8dcSSimon Schubert 
588cf7f2e2dSJohn Marino   if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
5895796c8dcSSimon Schubert 			      &filename, &line, &unmapped))
5905796c8dcSSimon Schubert     {
5915796c8dcSSimon Schubert       do_cleanups (cleanup_chain);
5925796c8dcSSimon Schubert       return;
5935796c8dcSSimon Schubert     }
5945796c8dcSSimon Schubert 
5955796c8dcSSimon Schubert   fputs_filtered (leadin, stream);
5965796c8dcSSimon Schubert   if (unmapped)
5975796c8dcSSimon Schubert     fputs_filtered ("<*", stream);
5985796c8dcSSimon Schubert   else
5995796c8dcSSimon Schubert     fputs_filtered ("<", stream);
6005796c8dcSSimon Schubert   fputs_filtered (name, stream);
6015796c8dcSSimon Schubert   if (offset != 0)
6025796c8dcSSimon Schubert     fprintf_filtered (stream, "+%u", (unsigned int) offset);
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert   /* Append source filename and line number if desired.  Give specific
6055796c8dcSSimon Schubert      line # of this addr, if we have it; else line # of the nearest symbol.  */
6065796c8dcSSimon Schubert   if (print_symbol_filename && filename != NULL)
6075796c8dcSSimon Schubert     {
6085796c8dcSSimon Schubert       if (line != -1)
6095796c8dcSSimon Schubert 	fprintf_filtered (stream, " at %s:%d", filename, line);
6105796c8dcSSimon Schubert       else
6115796c8dcSSimon Schubert 	fprintf_filtered (stream, " in %s", filename);
6125796c8dcSSimon Schubert     }
6135796c8dcSSimon Schubert   if (unmapped)
6145796c8dcSSimon Schubert     fputs_filtered ("*>", stream);
6155796c8dcSSimon Schubert   else
6165796c8dcSSimon Schubert     fputs_filtered (">", stream);
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
6195796c8dcSSimon Schubert }
6205796c8dcSSimon Schubert 
6215796c8dcSSimon Schubert /* Given an address ADDR return all the elements needed to print the
6225796c8dcSSimon Schubert    address in a symbolic form.  NAME can be mangled or not depending
6235796c8dcSSimon Schubert    on DO_DEMANGLE (and also on the asm_demangle global variable,
6245796c8dcSSimon Schubert    manipulated via ''set print asm-demangle'').  Return 0 in case of
6255796c8dcSSimon Schubert    success, when all the info in the OUT paramters is valid.  Return 1
6265796c8dcSSimon Schubert    otherwise.  */
6275796c8dcSSimon Schubert int
628cf7f2e2dSJohn Marino build_address_symbolic (struct gdbarch *gdbarch,
629cf7f2e2dSJohn Marino 			CORE_ADDR addr,  /* IN */
6305796c8dcSSimon Schubert 			int do_demangle, /* IN */
6315796c8dcSSimon Schubert 			char **name,     /* OUT */
6325796c8dcSSimon Schubert 			int *offset,     /* OUT */
6335796c8dcSSimon Schubert 			char **filename, /* OUT */
6345796c8dcSSimon Schubert 			int *line,       /* OUT */
6355796c8dcSSimon Schubert 			int *unmapped)   /* OUT */
6365796c8dcSSimon Schubert {
6375796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
6385796c8dcSSimon Schubert   struct symbol *symbol;
6395796c8dcSSimon Schubert   CORE_ADDR name_location = 0;
6405796c8dcSSimon Schubert   struct obj_section *section = NULL;
6415796c8dcSSimon Schubert   char *name_temp = "";
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert   /* Let's say it is mapped (not unmapped).  */
6445796c8dcSSimon Schubert   *unmapped = 0;
6455796c8dcSSimon Schubert 
6465796c8dcSSimon Schubert   /* Determine if the address is in an overlay, and whether it is
6475796c8dcSSimon Schubert      mapped.  */
6485796c8dcSSimon Schubert   if (overlay_debugging)
6495796c8dcSSimon Schubert     {
6505796c8dcSSimon Schubert       section = find_pc_overlay (addr);
6515796c8dcSSimon Schubert       if (pc_in_unmapped_range (addr, section))
6525796c8dcSSimon Schubert 	{
6535796c8dcSSimon Schubert 	  *unmapped = 1;
6545796c8dcSSimon Schubert 	  addr = overlay_mapped_address (addr, section);
6555796c8dcSSimon Schubert 	}
6565796c8dcSSimon Schubert     }
6575796c8dcSSimon Schubert 
6585796c8dcSSimon Schubert   /* First try to find the address in the symbol table, then
6595796c8dcSSimon Schubert      in the minsyms.  Take the closest one.  */
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert   /* This is defective in the sense that it only finds text symbols.  So
6625796c8dcSSimon Schubert      really this is kind of pointless--we should make sure that the
6635796c8dcSSimon Schubert      minimal symbols have everything we need (by changing that we could
6645796c8dcSSimon Schubert      save some memory, but for many debug format--ELF/DWARF or
6655796c8dcSSimon Schubert      anything/stabs--it would be inconvenient to eliminate those minimal
6665796c8dcSSimon Schubert      symbols anyway).  */
6675796c8dcSSimon Schubert   msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
6685796c8dcSSimon Schubert   symbol = find_pc_sect_function (addr, section);
6695796c8dcSSimon Schubert 
6705796c8dcSSimon Schubert   if (symbol)
6715796c8dcSSimon Schubert     {
672cf7f2e2dSJohn Marino       /* If this is a function (i.e. a code address), strip out any
673cf7f2e2dSJohn Marino 	 non-address bits.  For instance, display a pointer to the
674cf7f2e2dSJohn Marino 	 first instruction of a Thumb function as <function>; the
675cf7f2e2dSJohn Marino 	 second instruction will be <function+2>, even though the
676cf7f2e2dSJohn Marino 	 pointer is <function+3>.  This matches the ISA behavior.  */
677cf7f2e2dSJohn Marino       addr = gdbarch_addr_bits_remove (gdbarch, addr);
678cf7f2e2dSJohn Marino 
6795796c8dcSSimon Schubert       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
6805796c8dcSSimon Schubert       if (do_demangle || asm_demangle)
6815796c8dcSSimon Schubert 	name_temp = SYMBOL_PRINT_NAME (symbol);
6825796c8dcSSimon Schubert       else
6835796c8dcSSimon Schubert 	name_temp = SYMBOL_LINKAGE_NAME (symbol);
6845796c8dcSSimon Schubert     }
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert   if (msymbol != NULL)
6875796c8dcSSimon Schubert     {
6885796c8dcSSimon Schubert       if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
6895796c8dcSSimon Schubert 	{
6905796c8dcSSimon Schubert 	  /* The msymbol is closer to the address than the symbol;
6915796c8dcSSimon Schubert 	     use the msymbol instead.  */
6925796c8dcSSimon Schubert 	  symbol = 0;
6935796c8dcSSimon Schubert 	  name_location = SYMBOL_VALUE_ADDRESS (msymbol);
6945796c8dcSSimon Schubert 	  if (do_demangle || asm_demangle)
6955796c8dcSSimon Schubert 	    name_temp = SYMBOL_PRINT_NAME (msymbol);
6965796c8dcSSimon Schubert 	  else
6975796c8dcSSimon Schubert 	    name_temp = SYMBOL_LINKAGE_NAME (msymbol);
6985796c8dcSSimon Schubert 	}
6995796c8dcSSimon Schubert     }
7005796c8dcSSimon Schubert   if (symbol == NULL && msymbol == NULL)
7015796c8dcSSimon Schubert     return 1;
7025796c8dcSSimon Schubert 
7035796c8dcSSimon Schubert   /* If the nearest symbol is too far away, don't print anything symbolic.  */
7045796c8dcSSimon Schubert 
7055796c8dcSSimon Schubert   /* For when CORE_ADDR is larger than unsigned int, we do math in
7065796c8dcSSimon Schubert      CORE_ADDR.  But when we detect unsigned wraparound in the
7075796c8dcSSimon Schubert      CORE_ADDR math, we ignore this test and print the offset,
7085796c8dcSSimon Schubert      because addr+max_symbolic_offset has wrapped through the end
7095796c8dcSSimon Schubert      of the address space back to the beginning, giving bogus comparison.  */
7105796c8dcSSimon Schubert   if (addr > name_location + max_symbolic_offset
7115796c8dcSSimon Schubert       && name_location + max_symbolic_offset > name_location)
7125796c8dcSSimon Schubert     return 1;
7135796c8dcSSimon Schubert 
7145796c8dcSSimon Schubert   *offset = addr - name_location;
7155796c8dcSSimon Schubert 
7165796c8dcSSimon Schubert   *name = xstrdup (name_temp);
7175796c8dcSSimon Schubert 
7185796c8dcSSimon Schubert   if (print_symbol_filename)
7195796c8dcSSimon Schubert     {
7205796c8dcSSimon Schubert       struct symtab_and_line sal;
7215796c8dcSSimon Schubert 
7225796c8dcSSimon Schubert       sal = find_pc_sect_line (addr, section, 0);
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert       if (sal.symtab)
7255796c8dcSSimon Schubert 	{
7265796c8dcSSimon Schubert 	  *filename = xstrdup (sal.symtab->filename);
7275796c8dcSSimon Schubert 	  *line = sal.line;
7285796c8dcSSimon Schubert 	}
7295796c8dcSSimon Schubert     }
7305796c8dcSSimon Schubert   return 0;
7315796c8dcSSimon Schubert }
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert /* Print address ADDR symbolically on STREAM.
7355796c8dcSSimon Schubert    First print it as a number.  Then perhaps print
7365796c8dcSSimon Schubert    <SYMBOL + OFFSET> after the number.  */
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert void
7395796c8dcSSimon Schubert print_address (struct gdbarch *gdbarch,
7405796c8dcSSimon Schubert 	       CORE_ADDR addr, struct ui_file *stream)
7415796c8dcSSimon Schubert {
7425796c8dcSSimon Schubert   fputs_filtered (paddress (gdbarch, addr), stream);
743cf7f2e2dSJohn Marino   print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
744cf7f2e2dSJohn Marino }
745cf7f2e2dSJohn Marino 
746cf7f2e2dSJohn Marino /* Return a prefix for instruction address:
747cf7f2e2dSJohn Marino    "=> " for current instruction, else "   ".  */
748cf7f2e2dSJohn Marino 
749cf7f2e2dSJohn Marino const char *
750cf7f2e2dSJohn Marino pc_prefix (CORE_ADDR addr)
751cf7f2e2dSJohn Marino {
752cf7f2e2dSJohn Marino   if (has_stack_frames ())
753cf7f2e2dSJohn Marino     {
754cf7f2e2dSJohn Marino       struct frame_info *frame;
755cf7f2e2dSJohn Marino       CORE_ADDR pc;
756cf7f2e2dSJohn Marino 
757cf7f2e2dSJohn Marino       frame = get_selected_frame (NULL);
758c50c785cSJohn Marino       if (get_frame_pc_if_available (frame, &pc) && pc == addr)
759cf7f2e2dSJohn Marino 	return "=> ";
760cf7f2e2dSJohn Marino     }
761cf7f2e2dSJohn Marino   return "   ";
7625796c8dcSSimon Schubert }
7635796c8dcSSimon Schubert 
7645796c8dcSSimon Schubert /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
7655796c8dcSSimon Schubert    controls whether to print the symbolic name "raw" or demangled.
7665796c8dcSSimon Schubert    Global setting "addressprint" controls whether to print hex address
7675796c8dcSSimon Schubert    or not.  */
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert void
7705796c8dcSSimon Schubert print_address_demangle (struct gdbarch *gdbarch, CORE_ADDR addr,
7715796c8dcSSimon Schubert 			struct ui_file *stream, int do_demangle)
7725796c8dcSSimon Schubert {
7735796c8dcSSimon Schubert   struct value_print_options opts;
774cf7f2e2dSJohn Marino 
7755796c8dcSSimon Schubert   get_user_print_options (&opts);
7765796c8dcSSimon Schubert   if (addr == 0)
7775796c8dcSSimon Schubert     {
7785796c8dcSSimon Schubert       fprintf_filtered (stream, "0");
7795796c8dcSSimon Schubert     }
7805796c8dcSSimon Schubert   else if (opts.addressprint)
7815796c8dcSSimon Schubert     {
7825796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, addr), stream);
783cf7f2e2dSJohn Marino       print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
7845796c8dcSSimon Schubert     }
7855796c8dcSSimon Schubert   else
7865796c8dcSSimon Schubert     {
787cf7f2e2dSJohn Marino       print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
7885796c8dcSSimon Schubert     }
7895796c8dcSSimon Schubert }
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert /* Examine data at address ADDR in format FMT.
7935796c8dcSSimon Schubert    Fetch it from memory and print on gdb_stdout.  */
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert static void
7965796c8dcSSimon Schubert do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
7975796c8dcSSimon Schubert {
7985796c8dcSSimon Schubert   char format = 0;
7995796c8dcSSimon Schubert   char size;
8005796c8dcSSimon Schubert   int count = 1;
8015796c8dcSSimon Schubert   struct type *val_type = NULL;
8025796c8dcSSimon Schubert   int i;
8035796c8dcSSimon Schubert   int maxelts;
8045796c8dcSSimon Schubert   struct value_print_options opts;
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert   format = fmt.format;
8075796c8dcSSimon Schubert   size = fmt.size;
8085796c8dcSSimon Schubert   count = fmt.count;
8095796c8dcSSimon Schubert   next_gdbarch = gdbarch;
8105796c8dcSSimon Schubert   next_address = addr;
8115796c8dcSSimon Schubert 
812cf7f2e2dSJohn Marino   /* Instruction format implies fetch single bytes
813cf7f2e2dSJohn Marino      regardless of the specified size.
814cf7f2e2dSJohn Marino      The case of strings is handled in decode_format, only explicit
815cf7f2e2dSJohn Marino      size operator are not changed to 'b'.  */
816cf7f2e2dSJohn Marino   if (format == 'i')
8175796c8dcSSimon Schubert     size = 'b';
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert   if (size == 'a')
8205796c8dcSSimon Schubert     {
8215796c8dcSSimon Schubert       /* Pick the appropriate size for an address.  */
8225796c8dcSSimon Schubert       if (gdbarch_ptr_bit (next_gdbarch) == 64)
8235796c8dcSSimon Schubert 	size = 'g';
8245796c8dcSSimon Schubert       else if (gdbarch_ptr_bit (next_gdbarch) == 32)
8255796c8dcSSimon Schubert 	size = 'w';
8265796c8dcSSimon Schubert       else if (gdbarch_ptr_bit (next_gdbarch) == 16)
8275796c8dcSSimon Schubert 	size = 'h';
8285796c8dcSSimon Schubert       else
8295796c8dcSSimon Schubert 	/* Bad value for gdbarch_ptr_bit.  */
8305796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__,
8315796c8dcSSimon Schubert 			_("failed internal consistency check"));
8325796c8dcSSimon Schubert     }
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert   if (size == 'b')
8355796c8dcSSimon Schubert     val_type = builtin_type (next_gdbarch)->builtin_int8;
8365796c8dcSSimon Schubert   else if (size == 'h')
8375796c8dcSSimon Schubert     val_type = builtin_type (next_gdbarch)->builtin_int16;
8385796c8dcSSimon Schubert   else if (size == 'w')
8395796c8dcSSimon Schubert     val_type = builtin_type (next_gdbarch)->builtin_int32;
8405796c8dcSSimon Schubert   else if (size == 'g')
8415796c8dcSSimon Schubert     val_type = builtin_type (next_gdbarch)->builtin_int64;
8425796c8dcSSimon Schubert 
843cf7f2e2dSJohn Marino   if (format == 's')
844cf7f2e2dSJohn Marino     {
845cf7f2e2dSJohn Marino       struct type *char_type = NULL;
846cf7f2e2dSJohn Marino 
847cf7f2e2dSJohn Marino       /* Search for "char16_t"  or "char32_t" types or fall back to 8-bit char
848cf7f2e2dSJohn Marino 	 if type is not found.  */
849cf7f2e2dSJohn Marino       if (size == 'h')
850cf7f2e2dSJohn Marino 	char_type = builtin_type (next_gdbarch)->builtin_char16;
851cf7f2e2dSJohn Marino       else if (size == 'w')
852cf7f2e2dSJohn Marino 	char_type = builtin_type (next_gdbarch)->builtin_char32;
853cf7f2e2dSJohn Marino       if (char_type)
854cf7f2e2dSJohn Marino         val_type = char_type;
855cf7f2e2dSJohn Marino       else
856cf7f2e2dSJohn Marino         {
857cf7f2e2dSJohn Marino 	  if (size != '\0' && size != 'b')
858c50c785cSJohn Marino 	    warning (_("Unable to display strings with "
859c50c785cSJohn Marino 		       "size '%c', using 'b' instead."), size);
860cf7f2e2dSJohn Marino 	  size = 'b';
861cf7f2e2dSJohn Marino 	  val_type = builtin_type (next_gdbarch)->builtin_int8;
862cf7f2e2dSJohn Marino         }
863cf7f2e2dSJohn Marino     }
864cf7f2e2dSJohn Marino 
8655796c8dcSSimon Schubert   maxelts = 8;
8665796c8dcSSimon Schubert   if (size == 'w')
8675796c8dcSSimon Schubert     maxelts = 4;
8685796c8dcSSimon Schubert   if (size == 'g')
8695796c8dcSSimon Schubert     maxelts = 2;
8705796c8dcSSimon Schubert   if (format == 's' || format == 'i')
8715796c8dcSSimon Schubert     maxelts = 1;
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   get_formatted_print_options (&opts, format);
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert   /* Print as many objects as specified in COUNT, at most maxelts per line,
8765796c8dcSSimon Schubert      with the address of the next one at the start of each line.  */
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert   while (count > 0)
8795796c8dcSSimon Schubert     {
8805796c8dcSSimon Schubert       QUIT;
881cf7f2e2dSJohn Marino       if (format == 'i')
882cf7f2e2dSJohn Marino 	fputs_filtered (pc_prefix (next_address), gdb_stdout);
8835796c8dcSSimon Schubert       print_address (next_gdbarch, next_address, gdb_stdout);
8845796c8dcSSimon Schubert       printf_filtered (":");
8855796c8dcSSimon Schubert       for (i = maxelts;
8865796c8dcSSimon Schubert 	   i > 0 && count > 0;
8875796c8dcSSimon Schubert 	   i--, count--)
8885796c8dcSSimon Schubert 	{
8895796c8dcSSimon Schubert 	  printf_filtered ("\t");
8905796c8dcSSimon Schubert 	  /* Note that print_formatted sets next_address for the next
8915796c8dcSSimon Schubert 	     object.  */
8925796c8dcSSimon Schubert 	  last_examine_address = next_address;
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert 	  if (last_examine_value)
8955796c8dcSSimon Schubert 	    value_free (last_examine_value);
8965796c8dcSSimon Schubert 
8975796c8dcSSimon Schubert 	  /* The value to be displayed is not fetched greedily.
8985796c8dcSSimon Schubert 	     Instead, to avoid the possibility of a fetched value not
8995796c8dcSSimon Schubert 	     being used, its retrieval is delayed until the print code
9005796c8dcSSimon Schubert 	     uses it.  When examining an instruction stream, the
9015796c8dcSSimon Schubert 	     disassembler will perform its own memory fetch using just
9025796c8dcSSimon Schubert 	     the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
9035796c8dcSSimon Schubert 	     the disassembler be modified so that LAST_EXAMINE_VALUE
9045796c8dcSSimon Schubert 	     is left with the byte sequence from the last complete
9055796c8dcSSimon Schubert 	     instruction fetched from memory?  */
9065796c8dcSSimon Schubert 	  last_examine_value = value_at_lazy (val_type, next_address);
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert 	  if (last_examine_value)
9095796c8dcSSimon Schubert 	    release_value (last_examine_value);
9105796c8dcSSimon Schubert 
9115796c8dcSSimon Schubert 	  print_formatted (last_examine_value, size, &opts, gdb_stdout);
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert 	  /* Display any branch delay slots following the final insn.  */
9145796c8dcSSimon Schubert 	  if (format == 'i' && count == 1)
9155796c8dcSSimon Schubert 	    count += branch_delay_insns;
9165796c8dcSSimon Schubert 	}
9175796c8dcSSimon Schubert       printf_filtered ("\n");
9185796c8dcSSimon Schubert       gdb_flush (gdb_stdout);
9195796c8dcSSimon Schubert     }
9205796c8dcSSimon Schubert }
9215796c8dcSSimon Schubert 
9225796c8dcSSimon Schubert static void
9235796c8dcSSimon Schubert validate_format (struct format_data fmt, char *cmdname)
9245796c8dcSSimon Schubert {
9255796c8dcSSimon Schubert   if (fmt.size != 0)
9265796c8dcSSimon Schubert     error (_("Size letters are meaningless in \"%s\" command."), cmdname);
9275796c8dcSSimon Schubert   if (fmt.count != 1)
9285796c8dcSSimon Schubert     error (_("Item count other than 1 is meaningless in \"%s\" command."),
9295796c8dcSSimon Schubert 	   cmdname);
9305796c8dcSSimon Schubert   if (fmt.format == 'i')
9315796c8dcSSimon Schubert     error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
9325796c8dcSSimon Schubert 	   fmt.format, cmdname);
9335796c8dcSSimon Schubert }
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert /* Evaluate string EXP as an expression in the current language and
9365796c8dcSSimon Schubert    print the resulting value.  EXP may contain a format specifier as the
9375796c8dcSSimon Schubert    first argument ("/x myvar" for example, to print myvar in hex).  */
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert static void
9405796c8dcSSimon Schubert print_command_1 (char *exp, int inspect, int voidprint)
9415796c8dcSSimon Schubert {
9425796c8dcSSimon Schubert   struct expression *expr;
9435796c8dcSSimon Schubert   struct cleanup *old_chain = 0;
9445796c8dcSSimon Schubert   char format = 0;
9455796c8dcSSimon Schubert   struct value *val;
9465796c8dcSSimon Schubert   struct format_data fmt;
9475796c8dcSSimon Schubert   int cleanup = 0;
9485796c8dcSSimon Schubert 
9495796c8dcSSimon Schubert   if (exp && *exp == '/')
9505796c8dcSSimon Schubert     {
9515796c8dcSSimon Schubert       exp++;
9525796c8dcSSimon Schubert       fmt = decode_format (&exp, last_format, 0);
9535796c8dcSSimon Schubert       validate_format (fmt, "print");
9545796c8dcSSimon Schubert       last_format = format = fmt.format;
9555796c8dcSSimon Schubert     }
9565796c8dcSSimon Schubert   else
9575796c8dcSSimon Schubert     {
9585796c8dcSSimon Schubert       fmt.count = 1;
9595796c8dcSSimon Schubert       fmt.format = 0;
9605796c8dcSSimon Schubert       fmt.size = 0;
9615796c8dcSSimon Schubert       fmt.raw = 0;
9625796c8dcSSimon Schubert     }
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert   if (exp && *exp)
9655796c8dcSSimon Schubert     {
9665796c8dcSSimon Schubert       expr = parse_expression (exp);
9675796c8dcSSimon Schubert       old_chain = make_cleanup (free_current_contents, &expr);
9685796c8dcSSimon Schubert       cleanup = 1;
9695796c8dcSSimon Schubert       val = evaluate_expression (expr);
9705796c8dcSSimon Schubert     }
9715796c8dcSSimon Schubert   else
9725796c8dcSSimon Schubert     val = access_value_history (0);
9735796c8dcSSimon Schubert 
9745796c8dcSSimon Schubert   if (voidprint || (val && value_type (val) &&
9755796c8dcSSimon Schubert 		    TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
9765796c8dcSSimon Schubert     {
9775796c8dcSSimon Schubert       struct value_print_options opts;
9785796c8dcSSimon Schubert       int histindex = record_latest_value (val);
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert       if (histindex >= 0)
9815796c8dcSSimon Schubert 	annotate_value_history_begin (histindex, value_type (val));
9825796c8dcSSimon Schubert       else
9835796c8dcSSimon Schubert 	annotate_value_begin (value_type (val));
9845796c8dcSSimon Schubert 
9855796c8dcSSimon Schubert       if (inspect)
9865796c8dcSSimon Schubert 	printf_unfiltered ("\031(gdb-makebuffer \"%s\"  %d '(\"",
9875796c8dcSSimon Schubert 			   exp, histindex);
9885796c8dcSSimon Schubert       else if (histindex >= 0)
9895796c8dcSSimon Schubert 	printf_filtered ("$%d = ", histindex);
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert       if (histindex >= 0)
9925796c8dcSSimon Schubert 	annotate_value_history_value ();
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert       get_formatted_print_options (&opts, format);
9955796c8dcSSimon Schubert       opts.inspect_it = inspect;
9965796c8dcSSimon Schubert       opts.raw = fmt.raw;
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert       print_formatted (val, fmt.size, &opts, gdb_stdout);
9995796c8dcSSimon Schubert       printf_filtered ("\n");
10005796c8dcSSimon Schubert 
10015796c8dcSSimon Schubert       if (histindex >= 0)
10025796c8dcSSimon Schubert 	annotate_value_history_end ();
10035796c8dcSSimon Schubert       else
10045796c8dcSSimon Schubert 	annotate_value_end ();
10055796c8dcSSimon Schubert 
10065796c8dcSSimon Schubert       if (inspect)
10075796c8dcSSimon Schubert 	printf_unfiltered ("\") )\030");
10085796c8dcSSimon Schubert     }
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert   if (cleanup)
10115796c8dcSSimon Schubert     do_cleanups (old_chain);
10125796c8dcSSimon Schubert }
10135796c8dcSSimon Schubert 
10145796c8dcSSimon Schubert static void
10155796c8dcSSimon Schubert print_command (char *exp, int from_tty)
10165796c8dcSSimon Schubert {
10175796c8dcSSimon Schubert   print_command_1 (exp, 0, 1);
10185796c8dcSSimon Schubert }
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert /* Same as print, except in epoch, it gets its own window.  */
10215796c8dcSSimon Schubert static void
10225796c8dcSSimon Schubert inspect_command (char *exp, int from_tty)
10235796c8dcSSimon Schubert {
10245796c8dcSSimon Schubert   extern int epoch_interface;
10255796c8dcSSimon Schubert 
10265796c8dcSSimon Schubert   print_command_1 (exp, epoch_interface, 1);
10275796c8dcSSimon Schubert }
10285796c8dcSSimon Schubert 
10295796c8dcSSimon Schubert /* Same as print, except it doesn't print void results.  */
10305796c8dcSSimon Schubert static void
10315796c8dcSSimon Schubert call_command (char *exp, int from_tty)
10325796c8dcSSimon Schubert {
10335796c8dcSSimon Schubert   print_command_1 (exp, 0, 0);
10345796c8dcSSimon Schubert }
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert void
10375796c8dcSSimon Schubert output_command (char *exp, int from_tty)
10385796c8dcSSimon Schubert {
10395796c8dcSSimon Schubert   struct expression *expr;
10405796c8dcSSimon Schubert   struct cleanup *old_chain;
10415796c8dcSSimon Schubert   char format = 0;
10425796c8dcSSimon Schubert   struct value *val;
10435796c8dcSSimon Schubert   struct format_data fmt;
10445796c8dcSSimon Schubert   struct value_print_options opts;
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert   fmt.size = 0;
10475796c8dcSSimon Schubert   fmt.raw = 0;
10485796c8dcSSimon Schubert 
10495796c8dcSSimon Schubert   if (exp && *exp == '/')
10505796c8dcSSimon Schubert     {
10515796c8dcSSimon Schubert       exp++;
10525796c8dcSSimon Schubert       fmt = decode_format (&exp, 0, 0);
10535796c8dcSSimon Schubert       validate_format (fmt, "output");
10545796c8dcSSimon Schubert       format = fmt.format;
10555796c8dcSSimon Schubert     }
10565796c8dcSSimon Schubert 
10575796c8dcSSimon Schubert   expr = parse_expression (exp);
10585796c8dcSSimon Schubert   old_chain = make_cleanup (free_current_contents, &expr);
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert   val = evaluate_expression (expr);
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert   annotate_value_begin (value_type (val));
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert   get_formatted_print_options (&opts, format);
10655796c8dcSSimon Schubert   opts.raw = fmt.raw;
10665796c8dcSSimon Schubert   print_formatted (val, fmt.size, &opts, gdb_stdout);
10675796c8dcSSimon Schubert 
10685796c8dcSSimon Schubert   annotate_value_end ();
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert   wrap_here ("");
10715796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
10725796c8dcSSimon Schubert 
10735796c8dcSSimon Schubert   do_cleanups (old_chain);
10745796c8dcSSimon Schubert }
10755796c8dcSSimon Schubert 
10765796c8dcSSimon Schubert static void
10775796c8dcSSimon Schubert set_command (char *exp, int from_tty)
10785796c8dcSSimon Schubert {
10795796c8dcSSimon Schubert   struct expression *expr = parse_expression (exp);
10805796c8dcSSimon Schubert   struct cleanup *old_chain =
10815796c8dcSSimon Schubert     make_cleanup (free_current_contents, &expr);
1082cf7f2e2dSJohn Marino 
10835796c8dcSSimon Schubert   evaluate_expression (expr);
10845796c8dcSSimon Schubert   do_cleanups (old_chain);
10855796c8dcSSimon Schubert }
10865796c8dcSSimon Schubert 
10875796c8dcSSimon Schubert static void
10885796c8dcSSimon Schubert sym_info (char *arg, int from_tty)
10895796c8dcSSimon Schubert {
10905796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
10915796c8dcSSimon Schubert   struct objfile *objfile;
10925796c8dcSSimon Schubert   struct obj_section *osect;
10935796c8dcSSimon Schubert   CORE_ADDR addr, sect_addr;
10945796c8dcSSimon Schubert   int matches = 0;
10955796c8dcSSimon Schubert   unsigned int offset;
10965796c8dcSSimon Schubert 
10975796c8dcSSimon Schubert   if (!arg)
10985796c8dcSSimon Schubert     error_no_arg (_("address"));
10995796c8dcSSimon Schubert 
11005796c8dcSSimon Schubert   addr = parse_and_eval_address (arg);
11015796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, osect)
11025796c8dcSSimon Schubert   {
11035796c8dcSSimon Schubert     /* Only process each object file once, even if there's a separate
11045796c8dcSSimon Schubert        debug file.  */
11055796c8dcSSimon Schubert     if (objfile->separate_debug_objfile_backlink)
11065796c8dcSSimon Schubert       continue;
11075796c8dcSSimon Schubert 
11085796c8dcSSimon Schubert     sect_addr = overlay_mapped_address (addr, osect);
11095796c8dcSSimon Schubert 
11105796c8dcSSimon Schubert     if (obj_section_addr (osect) <= sect_addr
11115796c8dcSSimon Schubert 	&& sect_addr < obj_section_endaddr (osect)
11125796c8dcSSimon Schubert 	&& (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, osect)))
11135796c8dcSSimon Schubert       {
11145796c8dcSSimon Schubert 	const char *obj_name, *mapped, *sec_name, *msym_name;
11155796c8dcSSimon Schubert 	char *loc_string;
11165796c8dcSSimon Schubert 	struct cleanup *old_chain;
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert 	matches = 1;
11195796c8dcSSimon Schubert 	offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
11205796c8dcSSimon Schubert 	mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
11215796c8dcSSimon Schubert 	sec_name = osect->the_bfd_section->name;
11225796c8dcSSimon Schubert 	msym_name = SYMBOL_PRINT_NAME (msymbol);
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert 	/* Don't print the offset if it is zero.
11255796c8dcSSimon Schubert 	   We assume there's no need to handle i18n of "sym + offset".  */
11265796c8dcSSimon Schubert 	if (offset)
11275796c8dcSSimon Schubert 	  loc_string = xstrprintf ("%s + %u", msym_name, offset);
11285796c8dcSSimon Schubert 	else
11295796c8dcSSimon Schubert 	  loc_string = xstrprintf ("%s", msym_name);
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert 	/* Use a cleanup to free loc_string in case the user quits
11325796c8dcSSimon Schubert 	   a pagination request inside printf_filtered.  */
11335796c8dcSSimon Schubert 	old_chain = make_cleanup (xfree, loc_string);
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert 	gdb_assert (osect->objfile && osect->objfile->name);
11365796c8dcSSimon Schubert 	obj_name = osect->objfile->name;
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert 	if (MULTI_OBJFILE_P ())
11395796c8dcSSimon Schubert 	  if (pc_in_unmapped_range (addr, osect))
11405796c8dcSSimon Schubert 	    if (section_is_overlay (osect))
11415796c8dcSSimon Schubert 	      printf_filtered (_("%s in load address range of "
11425796c8dcSSimon Schubert 				 "%s overlay section %s of %s\n"),
11435796c8dcSSimon Schubert 			       loc_string, mapped, sec_name, obj_name);
11445796c8dcSSimon Schubert 	    else
11455796c8dcSSimon Schubert 	      printf_filtered (_("%s in load address range of "
11465796c8dcSSimon Schubert 				 "section %s of %s\n"),
11475796c8dcSSimon Schubert 			       loc_string, sec_name, obj_name);
11485796c8dcSSimon Schubert 	  else
11495796c8dcSSimon Schubert 	    if (section_is_overlay (osect))
11505796c8dcSSimon Schubert 	      printf_filtered (_("%s in %s overlay section %s of %s\n"),
11515796c8dcSSimon Schubert 			       loc_string, mapped, sec_name, obj_name);
11525796c8dcSSimon Schubert 	    else
11535796c8dcSSimon Schubert 	      printf_filtered (_("%s in section %s of %s\n"),
11545796c8dcSSimon Schubert 			       loc_string, sec_name, obj_name);
11555796c8dcSSimon Schubert 	else
11565796c8dcSSimon Schubert 	  if (pc_in_unmapped_range (addr, osect))
11575796c8dcSSimon Schubert 	    if (section_is_overlay (osect))
11585796c8dcSSimon Schubert 	      printf_filtered (_("%s in load address range of %s overlay "
11595796c8dcSSimon Schubert 				 "section %s\n"),
11605796c8dcSSimon Schubert 			       loc_string, mapped, sec_name);
11615796c8dcSSimon Schubert 	    else
11625796c8dcSSimon Schubert 	      printf_filtered (_("%s in load address range of section %s\n"),
11635796c8dcSSimon Schubert 			       loc_string, sec_name);
11645796c8dcSSimon Schubert 	  else
11655796c8dcSSimon Schubert 	    if (section_is_overlay (osect))
11665796c8dcSSimon Schubert 	      printf_filtered (_("%s in %s overlay section %s\n"),
11675796c8dcSSimon Schubert 			       loc_string, mapped, sec_name);
11685796c8dcSSimon Schubert 	    else
11695796c8dcSSimon Schubert 	      printf_filtered (_("%s in section %s\n"),
11705796c8dcSSimon Schubert 			       loc_string, sec_name);
11715796c8dcSSimon Schubert 
11725796c8dcSSimon Schubert 	do_cleanups (old_chain);
11735796c8dcSSimon Schubert       }
11745796c8dcSSimon Schubert   }
11755796c8dcSSimon Schubert   if (matches == 0)
11765796c8dcSSimon Schubert     printf_filtered (_("No symbol matches %s.\n"), arg);
11775796c8dcSSimon Schubert }
11785796c8dcSSimon Schubert 
11795796c8dcSSimon Schubert static void
11805796c8dcSSimon Schubert address_info (char *exp, int from_tty)
11815796c8dcSSimon Schubert {
11825796c8dcSSimon Schubert   struct gdbarch *gdbarch;
11835796c8dcSSimon Schubert   int regno;
11845796c8dcSSimon Schubert   struct symbol *sym;
11855796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
11865796c8dcSSimon Schubert   long val;
11875796c8dcSSimon Schubert   struct obj_section *section;
1188cf7f2e2dSJohn Marino   CORE_ADDR load_addr, context_pc = 0;
11895796c8dcSSimon Schubert   int is_a_field_of_this;	/* C++: lookup_symbol sets this to nonzero
11905796c8dcSSimon Schubert 				   if exp is a field of `this'.  */
11915796c8dcSSimon Schubert 
11925796c8dcSSimon Schubert   if (exp == 0)
11935796c8dcSSimon Schubert     error (_("Argument required."));
11945796c8dcSSimon Schubert 
1195cf7f2e2dSJohn Marino   sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
11965796c8dcSSimon Schubert 		       &is_a_field_of_this);
11975796c8dcSSimon Schubert   if (sym == NULL)
11985796c8dcSSimon Schubert     {
11995796c8dcSSimon Schubert       if (is_a_field_of_this)
12005796c8dcSSimon Schubert 	{
12015796c8dcSSimon Schubert 	  printf_filtered ("Symbol \"");
12025796c8dcSSimon Schubert 	  fprintf_symbol_filtered (gdb_stdout, exp,
12035796c8dcSSimon Schubert 				   current_language->la_language, DMGL_ANSI);
12045796c8dcSSimon Schubert 	  printf_filtered ("\" is a field of the local class variable ");
12055796c8dcSSimon Schubert 	  if (current_language->la_language == language_objc)
12065796c8dcSSimon Schubert 	    printf_filtered ("`self'\n");	/* ObjC equivalent of "this" */
12075796c8dcSSimon Schubert 	  else
12085796c8dcSSimon Schubert 	    printf_filtered ("`this'\n");
12095796c8dcSSimon Schubert 	  return;
12105796c8dcSSimon Schubert 	}
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert       msymbol = lookup_minimal_symbol (exp, NULL, NULL);
12135796c8dcSSimon Schubert 
12145796c8dcSSimon Schubert       if (msymbol != NULL)
12155796c8dcSSimon Schubert 	{
12165796c8dcSSimon Schubert 	  gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
12175796c8dcSSimon Schubert 	  load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
12185796c8dcSSimon Schubert 
12195796c8dcSSimon Schubert 	  printf_filtered ("Symbol \"");
12205796c8dcSSimon Schubert 	  fprintf_symbol_filtered (gdb_stdout, exp,
12215796c8dcSSimon Schubert 				   current_language->la_language, DMGL_ANSI);
12225796c8dcSSimon Schubert 	  printf_filtered ("\" is at ");
12235796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
12245796c8dcSSimon Schubert 	  printf_filtered (" in a file compiled without debugging");
12255796c8dcSSimon Schubert 	  section = SYMBOL_OBJ_SECTION (msymbol);
12265796c8dcSSimon Schubert 	  if (section_is_overlay (section))
12275796c8dcSSimon Schubert 	    {
12285796c8dcSSimon Schubert 	      load_addr = overlay_unmapped_address (load_addr, section);
12295796c8dcSSimon Schubert 	      printf_filtered (",\n -- loaded at ");
12305796c8dcSSimon Schubert 	      fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
12315796c8dcSSimon Schubert 	      printf_filtered (" in overlay section %s",
12325796c8dcSSimon Schubert 			       section->the_bfd_section->name);
12335796c8dcSSimon Schubert 	    }
12345796c8dcSSimon Schubert 	  printf_filtered (".\n");
12355796c8dcSSimon Schubert 	}
12365796c8dcSSimon Schubert       else
12375796c8dcSSimon Schubert 	error (_("No symbol \"%s\" in current context."), exp);
12385796c8dcSSimon Schubert       return;
12395796c8dcSSimon Schubert     }
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert   printf_filtered ("Symbol \"");
12425796c8dcSSimon Schubert   fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
12435796c8dcSSimon Schubert 			   current_language->la_language, DMGL_ANSI);
12445796c8dcSSimon Schubert   printf_filtered ("\" is ");
12455796c8dcSSimon Schubert   val = SYMBOL_VALUE (sym);
12465796c8dcSSimon Schubert   section = SYMBOL_OBJ_SECTION (sym);
12475796c8dcSSimon Schubert   gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
12485796c8dcSSimon Schubert 
12495796c8dcSSimon Schubert   switch (SYMBOL_CLASS (sym))
12505796c8dcSSimon Schubert     {
12515796c8dcSSimon Schubert     case LOC_CONST:
12525796c8dcSSimon Schubert     case LOC_CONST_BYTES:
12535796c8dcSSimon Schubert       printf_filtered ("constant");
12545796c8dcSSimon Schubert       break;
12555796c8dcSSimon Schubert 
12565796c8dcSSimon Schubert     case LOC_LABEL:
12575796c8dcSSimon Schubert       printf_filtered ("a label at address ");
12585796c8dcSSimon Schubert       load_addr = SYMBOL_VALUE_ADDRESS (sym);
12595796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
12605796c8dcSSimon Schubert       if (section_is_overlay (section))
12615796c8dcSSimon Schubert 	{
12625796c8dcSSimon Schubert 	  load_addr = overlay_unmapped_address (load_addr, section);
12635796c8dcSSimon Schubert 	  printf_filtered (",\n -- loaded at ");
12645796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
12655796c8dcSSimon Schubert 	  printf_filtered (" in overlay section %s",
12665796c8dcSSimon Schubert 			   section->the_bfd_section->name);
12675796c8dcSSimon Schubert 	}
12685796c8dcSSimon Schubert       break;
12695796c8dcSSimon Schubert 
12705796c8dcSSimon Schubert     case LOC_COMPUTED:
12715796c8dcSSimon Schubert       /* FIXME: cagney/2004-01-26: It should be possible to
12725796c8dcSSimon Schubert 	 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
12735796c8dcSSimon Schubert 	 Unfortunately DWARF 2 stores the frame-base (instead of the
12745796c8dcSSimon Schubert 	 function) location in a function's symbol.  Oops!  For the
12755796c8dcSSimon Schubert 	 moment enable this when/where applicable.  */
1276c50c785cSJohn Marino       SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1277c50c785cSJohn Marino 						    gdb_stdout);
12785796c8dcSSimon Schubert       break;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert     case LOC_REGISTER:
12815796c8dcSSimon Schubert       /* GDBARCH is the architecture associated with the objfile the symbol
12825796c8dcSSimon Schubert 	 is defined in; the target architecture may be different, and may
12835796c8dcSSimon Schubert 	 provide additional registers.  However, we do not know the target
12845796c8dcSSimon Schubert 	 architecture at this point.  We assume the objfile architecture
12855796c8dcSSimon Schubert 	 will contain all the standard registers that occur in debug info
12865796c8dcSSimon Schubert 	 in that objfile.  */
12875796c8dcSSimon Schubert       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
12885796c8dcSSimon Schubert 
12895796c8dcSSimon Schubert       if (SYMBOL_IS_ARGUMENT (sym))
12905796c8dcSSimon Schubert 	printf_filtered (_("an argument in register %s"),
12915796c8dcSSimon Schubert 			 gdbarch_register_name (gdbarch, regno));
12925796c8dcSSimon Schubert       else
12935796c8dcSSimon Schubert 	printf_filtered (_("a variable in register %s"),
12945796c8dcSSimon Schubert 			 gdbarch_register_name (gdbarch, regno));
12955796c8dcSSimon Schubert       break;
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert     case LOC_STATIC:
12985796c8dcSSimon Schubert       printf_filtered (_("static storage at address "));
12995796c8dcSSimon Schubert       load_addr = SYMBOL_VALUE_ADDRESS (sym);
13005796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13015796c8dcSSimon Schubert       if (section_is_overlay (section))
13025796c8dcSSimon Schubert 	{
13035796c8dcSSimon Schubert 	  load_addr = overlay_unmapped_address (load_addr, section);
13045796c8dcSSimon Schubert 	  printf_filtered (_(",\n -- loaded at "));
13055796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13065796c8dcSSimon Schubert 	  printf_filtered (_(" in overlay section %s"),
13075796c8dcSSimon Schubert 			   section->the_bfd_section->name);
13085796c8dcSSimon Schubert 	}
13095796c8dcSSimon Schubert       break;
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert     case LOC_REGPARM_ADDR:
13125796c8dcSSimon Schubert       /* Note comment at LOC_REGISTER.  */
13135796c8dcSSimon Schubert       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
13145796c8dcSSimon Schubert       printf_filtered (_("address of an argument in register %s"),
13155796c8dcSSimon Schubert 		       gdbarch_register_name (gdbarch, regno));
13165796c8dcSSimon Schubert       break;
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert     case LOC_ARG:
13195796c8dcSSimon Schubert       printf_filtered (_("an argument at offset %ld"), val);
13205796c8dcSSimon Schubert       break;
13215796c8dcSSimon Schubert 
13225796c8dcSSimon Schubert     case LOC_LOCAL:
13235796c8dcSSimon Schubert       printf_filtered (_("a local variable at frame offset %ld"), val);
13245796c8dcSSimon Schubert       break;
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert     case LOC_REF_ARG:
13275796c8dcSSimon Schubert       printf_filtered (_("a reference argument at offset %ld"), val);
13285796c8dcSSimon Schubert       break;
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert     case LOC_TYPEDEF:
13315796c8dcSSimon Schubert       printf_filtered (_("a typedef"));
13325796c8dcSSimon Schubert       break;
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert     case LOC_BLOCK:
13355796c8dcSSimon Schubert       printf_filtered (_("a function at address "));
13365796c8dcSSimon Schubert       load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
13375796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13385796c8dcSSimon Schubert       if (section_is_overlay (section))
13395796c8dcSSimon Schubert 	{
13405796c8dcSSimon Schubert 	  load_addr = overlay_unmapped_address (load_addr, section);
13415796c8dcSSimon Schubert 	  printf_filtered (_(",\n -- loaded at "));
13425796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13435796c8dcSSimon Schubert 	  printf_filtered (_(" in overlay section %s"),
13445796c8dcSSimon Schubert 			   section->the_bfd_section->name);
13455796c8dcSSimon Schubert 	}
13465796c8dcSSimon Schubert       break;
13475796c8dcSSimon Schubert 
13485796c8dcSSimon Schubert     case LOC_UNRESOLVED:
13495796c8dcSSimon Schubert       {
13505796c8dcSSimon Schubert 	struct minimal_symbol *msym;
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert 	msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym), NULL, NULL);
13535796c8dcSSimon Schubert 	if (msym == NULL)
13545796c8dcSSimon Schubert 	  printf_filtered ("unresolved");
13555796c8dcSSimon Schubert 	else
13565796c8dcSSimon Schubert 	  {
13575796c8dcSSimon Schubert 	    section = SYMBOL_OBJ_SECTION (msym);
13585796c8dcSSimon Schubert 	    load_addr = SYMBOL_VALUE_ADDRESS (msym);
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert 	    if (section
13615796c8dcSSimon Schubert 		&& (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
13625796c8dcSSimon Schubert 	      printf_filtered (_("a thread-local variable at offset %s "
13635796c8dcSSimon Schubert 				 "in the thread-local storage for `%s'"),
13645796c8dcSSimon Schubert 			       paddress (gdbarch, load_addr),
13655796c8dcSSimon Schubert 			       section->objfile->name);
13665796c8dcSSimon Schubert 	    else
13675796c8dcSSimon Schubert 	      {
13685796c8dcSSimon Schubert 		printf_filtered (_("static storage at address "));
13695796c8dcSSimon Schubert 		fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13705796c8dcSSimon Schubert 		if (section_is_overlay (section))
13715796c8dcSSimon Schubert 		  {
13725796c8dcSSimon Schubert 		    load_addr = overlay_unmapped_address (load_addr, section);
13735796c8dcSSimon Schubert 		    printf_filtered (_(",\n -- loaded at "));
13745796c8dcSSimon Schubert 		    fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
13755796c8dcSSimon Schubert 		    printf_filtered (_(" in overlay section %s"),
13765796c8dcSSimon Schubert 				     section->the_bfd_section->name);
13775796c8dcSSimon Schubert 		  }
13785796c8dcSSimon Schubert 	      }
13795796c8dcSSimon Schubert 	  }
13805796c8dcSSimon Schubert       }
13815796c8dcSSimon Schubert       break;
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert     case LOC_OPTIMIZED_OUT:
13845796c8dcSSimon Schubert       printf_filtered (_("optimized out"));
13855796c8dcSSimon Schubert       break;
13865796c8dcSSimon Schubert 
13875796c8dcSSimon Schubert     default:
13885796c8dcSSimon Schubert       printf_filtered (_("of unknown (botched) type"));
13895796c8dcSSimon Schubert       break;
13905796c8dcSSimon Schubert     }
13915796c8dcSSimon Schubert   printf_filtered (".\n");
13925796c8dcSSimon Schubert }
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert 
13955796c8dcSSimon Schubert static void
13965796c8dcSSimon Schubert x_command (char *exp, int from_tty)
13975796c8dcSSimon Schubert {
13985796c8dcSSimon Schubert   struct expression *expr;
13995796c8dcSSimon Schubert   struct format_data fmt;
14005796c8dcSSimon Schubert   struct cleanup *old_chain;
14015796c8dcSSimon Schubert   struct value *val;
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert   fmt.format = last_format ? last_format : 'x';
14045796c8dcSSimon Schubert   fmt.size = last_size;
14055796c8dcSSimon Schubert   fmt.count = 1;
14065796c8dcSSimon Schubert   fmt.raw = 0;
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert   if (exp && *exp == '/')
14095796c8dcSSimon Schubert     {
14105796c8dcSSimon Schubert       exp++;
14115796c8dcSSimon Schubert       fmt = decode_format (&exp, last_format, last_size);
14125796c8dcSSimon Schubert     }
14135796c8dcSSimon Schubert 
14145796c8dcSSimon Schubert   /* If we have an expression, evaluate it and use it as the address.  */
14155796c8dcSSimon Schubert 
14165796c8dcSSimon Schubert   if (exp != 0 && *exp != 0)
14175796c8dcSSimon Schubert     {
14185796c8dcSSimon Schubert       expr = parse_expression (exp);
14195796c8dcSSimon Schubert       /* Cause expression not to be there any more if this command is
14205796c8dcSSimon Schubert          repeated with Newline.  But don't clobber a user-defined
14215796c8dcSSimon Schubert          command's definition.  */
14225796c8dcSSimon Schubert       if (from_tty)
14235796c8dcSSimon Schubert 	*exp = 0;
14245796c8dcSSimon Schubert       old_chain = make_cleanup (free_current_contents, &expr);
14255796c8dcSSimon Schubert       val = evaluate_expression (expr);
14265796c8dcSSimon Schubert       if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1427cf7f2e2dSJohn Marino 	val = coerce_ref (val);
14285796c8dcSSimon Schubert       /* In rvalue contexts, such as this, functions are coerced into
14295796c8dcSSimon Schubert          pointers to functions.  This makes "x/i main" work.  */
14305796c8dcSSimon Schubert       if (/* last_format == 'i'  && */
14315796c8dcSSimon Schubert 	  TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
14325796c8dcSSimon Schubert 	   && VALUE_LVAL (val) == lval_memory)
14335796c8dcSSimon Schubert 	next_address = value_address (val);
14345796c8dcSSimon Schubert       else
14355796c8dcSSimon Schubert 	next_address = value_as_address (val);
14365796c8dcSSimon Schubert 
14375796c8dcSSimon Schubert       next_gdbarch = expr->gdbarch;
14385796c8dcSSimon Schubert       do_cleanups (old_chain);
14395796c8dcSSimon Schubert     }
14405796c8dcSSimon Schubert 
14415796c8dcSSimon Schubert   if (!next_gdbarch)
14425796c8dcSSimon Schubert     error_no_arg (_("starting display address"));
14435796c8dcSSimon Schubert 
14445796c8dcSSimon Schubert   do_examine (fmt, next_gdbarch, next_address);
14455796c8dcSSimon Schubert 
14465796c8dcSSimon Schubert   /* If the examine succeeds, we remember its size and format for next
1447cf7f2e2dSJohn Marino      time.  Set last_size to 'b' for strings.  */
1448cf7f2e2dSJohn Marino   if (fmt.format == 's')
1449cf7f2e2dSJohn Marino     last_size = 'b';
1450cf7f2e2dSJohn Marino   else
14515796c8dcSSimon Schubert     last_size = fmt.size;
14525796c8dcSSimon Schubert   last_format = fmt.format;
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert   /* Set a couple of internal variables if appropriate.  */
14555796c8dcSSimon Schubert   if (last_examine_value)
14565796c8dcSSimon Schubert     {
14575796c8dcSSimon Schubert       /* Make last address examined available to the user as $_.  Use
14585796c8dcSSimon Schubert          the correct pointer type.  */
14595796c8dcSSimon Schubert       struct type *pointer_type
14605796c8dcSSimon Schubert 	= lookup_pointer_type (value_type (last_examine_value));
14615796c8dcSSimon Schubert       set_internalvar (lookup_internalvar ("_"),
14625796c8dcSSimon Schubert 		       value_from_pointer (pointer_type,
14635796c8dcSSimon Schubert 					   last_examine_address));
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert       /* Make contents of last address examined available to the user
14665796c8dcSSimon Schubert 	 as $__.  If the last value has not been fetched from memory
14675796c8dcSSimon Schubert 	 then don't fetch it now; instead mark it by voiding the $__
14685796c8dcSSimon Schubert 	 variable.  */
14695796c8dcSSimon Schubert       if (value_lazy (last_examine_value))
14705796c8dcSSimon Schubert 	clear_internalvar (lookup_internalvar ("__"));
14715796c8dcSSimon Schubert       else
14725796c8dcSSimon Schubert 	set_internalvar (lookup_internalvar ("__"), last_examine_value);
14735796c8dcSSimon Schubert     }
14745796c8dcSSimon Schubert }
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert 
14775796c8dcSSimon Schubert /* Add an expression to the auto-display chain.
14785796c8dcSSimon Schubert    Specify the expression.  */
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert static void
14815796c8dcSSimon Schubert display_command (char *exp, int from_tty)
14825796c8dcSSimon Schubert {
14835796c8dcSSimon Schubert   struct format_data fmt;
14845796c8dcSSimon Schubert   struct expression *expr;
14855796c8dcSSimon Schubert   struct display *new;
14865796c8dcSSimon Schubert   int display_it = 1;
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert #if defined(TUI)
14895796c8dcSSimon Schubert   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
14905796c8dcSSimon Schubert      `tui_version'.  */
14915796c8dcSSimon Schubert   if (tui_active && exp != NULL && *exp == '$')
14925796c8dcSSimon Schubert     display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
14935796c8dcSSimon Schubert #endif
14945796c8dcSSimon Schubert 
14955796c8dcSSimon Schubert   if (display_it)
14965796c8dcSSimon Schubert     {
14975796c8dcSSimon Schubert       if (exp == 0)
14985796c8dcSSimon Schubert 	{
14995796c8dcSSimon Schubert 	  do_displays ();
15005796c8dcSSimon Schubert 	  return;
15015796c8dcSSimon Schubert 	}
15025796c8dcSSimon Schubert 
15035796c8dcSSimon Schubert       if (*exp == '/')
15045796c8dcSSimon Schubert 	{
15055796c8dcSSimon Schubert 	  exp++;
15065796c8dcSSimon Schubert 	  fmt = decode_format (&exp, 0, 0);
15075796c8dcSSimon Schubert 	  if (fmt.size && fmt.format == 0)
15085796c8dcSSimon Schubert 	    fmt.format = 'x';
15095796c8dcSSimon Schubert 	  if (fmt.format == 'i' || fmt.format == 's')
15105796c8dcSSimon Schubert 	    fmt.size = 'b';
15115796c8dcSSimon Schubert 	}
15125796c8dcSSimon Schubert       else
15135796c8dcSSimon Schubert 	{
15145796c8dcSSimon Schubert 	  fmt.format = 0;
15155796c8dcSSimon Schubert 	  fmt.size = 0;
15165796c8dcSSimon Schubert 	  fmt.count = 0;
15175796c8dcSSimon Schubert 	  fmt.raw = 0;
15185796c8dcSSimon Schubert 	}
15195796c8dcSSimon Schubert 
15205796c8dcSSimon Schubert       innermost_block = NULL;
15215796c8dcSSimon Schubert       expr = parse_expression (exp);
15225796c8dcSSimon Schubert 
15235796c8dcSSimon Schubert       new = (struct display *) xmalloc (sizeof (struct display));
15245796c8dcSSimon Schubert 
15255796c8dcSSimon Schubert       new->exp_string = xstrdup (exp);
15265796c8dcSSimon Schubert       new->exp = expr;
15275796c8dcSSimon Schubert       new->block = innermost_block;
1528cf7f2e2dSJohn Marino       new->pspace = current_program_space;
15295796c8dcSSimon Schubert       new->next = display_chain;
15305796c8dcSSimon Schubert       new->number = ++display_number;
15315796c8dcSSimon Schubert       new->format = fmt;
15325796c8dcSSimon Schubert       new->enabled_p = 1;
15335796c8dcSSimon Schubert       display_chain = new;
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert       if (from_tty && target_has_execution)
15365796c8dcSSimon Schubert 	do_one_display (new);
15375796c8dcSSimon Schubert 
15385796c8dcSSimon Schubert       dont_repeat ();
15395796c8dcSSimon Schubert     }
15405796c8dcSSimon Schubert }
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert static void
15435796c8dcSSimon Schubert free_display (struct display *d)
15445796c8dcSSimon Schubert {
15455796c8dcSSimon Schubert   xfree (d->exp_string);
15465796c8dcSSimon Schubert   xfree (d->exp);
15475796c8dcSSimon Schubert   xfree (d);
15485796c8dcSSimon Schubert }
15495796c8dcSSimon Schubert 
15505796c8dcSSimon Schubert /* Clear out the display_chain.  Done when new symtabs are loaded,
15515796c8dcSSimon Schubert    since this invalidates the types stored in many expressions.  */
15525796c8dcSSimon Schubert 
15535796c8dcSSimon Schubert void
15545796c8dcSSimon Schubert clear_displays (void)
15555796c8dcSSimon Schubert {
15565796c8dcSSimon Schubert   struct display *d;
15575796c8dcSSimon Schubert 
15585796c8dcSSimon Schubert   while ((d = display_chain) != NULL)
15595796c8dcSSimon Schubert     {
15605796c8dcSSimon Schubert       display_chain = d->next;
15615796c8dcSSimon Schubert       free_display (d);
15625796c8dcSSimon Schubert     }
15635796c8dcSSimon Schubert }
15645796c8dcSSimon Schubert 
1565c50c785cSJohn Marino /* Delete the auto-display DISPLAY.  */
15665796c8dcSSimon Schubert 
15675796c8dcSSimon Schubert static void
1568c50c785cSJohn Marino delete_display (struct display *display)
15695796c8dcSSimon Schubert {
1570c50c785cSJohn Marino   struct display *d;
15715796c8dcSSimon Schubert 
1572c50c785cSJohn Marino   gdb_assert (display != NULL);
15735796c8dcSSimon Schubert 
1574c50c785cSJohn Marino   if (display_chain == display)
1575c50c785cSJohn Marino     display_chain = display->next;
1576c50c785cSJohn Marino 
1577c50c785cSJohn Marino   ALL_DISPLAYS (d)
1578c50c785cSJohn Marino     if (d->next == display)
15795796c8dcSSimon Schubert       {
1580c50c785cSJohn Marino 	d->next = display->next;
15815796c8dcSSimon Schubert 	break;
15825796c8dcSSimon Schubert       }
1583c50c785cSJohn Marino 
1584c50c785cSJohn Marino   free_display (display);
1585c50c785cSJohn Marino }
1586c50c785cSJohn Marino 
1587c50c785cSJohn Marino /* Call FUNCTION on each of the displays whose numbers are given in
1588c50c785cSJohn Marino    ARGS.  DATA is passed unmodified to FUNCTION.  */
1589c50c785cSJohn Marino 
1590c50c785cSJohn Marino static void
1591c50c785cSJohn Marino map_display_numbers (char *args,
1592c50c785cSJohn Marino 		     void (*function) (struct display *,
1593c50c785cSJohn Marino 				       void *),
1594c50c785cSJohn Marino 		     void *data)
1595c50c785cSJohn Marino {
1596c50c785cSJohn Marino   struct get_number_or_range_state state;
1597c50c785cSJohn Marino   struct display *b, *tmp;
1598c50c785cSJohn Marino   int num;
1599c50c785cSJohn Marino 
1600c50c785cSJohn Marino   if (args == NULL)
1601c50c785cSJohn Marino     error_no_arg (_("one or more display numbers"));
1602c50c785cSJohn Marino 
1603c50c785cSJohn Marino   init_number_or_range (&state, args);
1604c50c785cSJohn Marino 
1605c50c785cSJohn Marino   while (!state.finished)
1606c50c785cSJohn Marino     {
1607c50c785cSJohn Marino       char *p = state.string;
1608c50c785cSJohn Marino 
1609c50c785cSJohn Marino       num = get_number_or_range (&state);
1610c50c785cSJohn Marino       if (num == 0)
1611c50c785cSJohn Marino 	warning (_("bad display number at or near '%s'"), p);
1612c50c785cSJohn Marino       else
1613c50c785cSJohn Marino 	{
1614c50c785cSJohn Marino 	  struct display *d, *tmp;
1615c50c785cSJohn Marino 
1616c50c785cSJohn Marino 	  ALL_DISPLAYS_SAFE (d, tmp)
1617c50c785cSJohn Marino 	    if (d->number == num)
1618c50c785cSJohn Marino 	      break;
1619c50c785cSJohn Marino 	  if (d == NULL)
1620c50c785cSJohn Marino 	    printf_unfiltered (_("No display number %d.\n"), num);
1621c50c785cSJohn Marino 	  else
1622c50c785cSJohn Marino 	    function (d, data);
1623c50c785cSJohn Marino 	}
16245796c8dcSSimon Schubert     }
16255796c8dcSSimon Schubert }
16265796c8dcSSimon Schubert 
1627c50c785cSJohn Marino /* Callback for map_display_numbers, that deletes a display.  */
1628c50c785cSJohn Marino 
1629c50c785cSJohn Marino static void
1630c50c785cSJohn Marino do_delete_display (struct display *d, void *data)
1631c50c785cSJohn Marino {
1632c50c785cSJohn Marino   delete_display (d);
1633c50c785cSJohn Marino }
1634c50c785cSJohn Marino 
1635c50c785cSJohn Marino /* "undisplay" command.  */
16365796c8dcSSimon Schubert 
16375796c8dcSSimon Schubert static void
16385796c8dcSSimon Schubert undisplay_command (char *args, int from_tty)
16395796c8dcSSimon Schubert {
16405796c8dcSSimon Schubert   int num;
1641c50c785cSJohn Marino   struct get_number_or_range_state state;
16425796c8dcSSimon Schubert 
1643c50c785cSJohn Marino   if (args == NULL)
16445796c8dcSSimon Schubert     {
16455796c8dcSSimon Schubert       if (query (_("Delete all auto-display expressions? ")))
16465796c8dcSSimon Schubert 	clear_displays ();
16475796c8dcSSimon Schubert       dont_repeat ();
16485796c8dcSSimon Schubert       return;
16495796c8dcSSimon Schubert     }
16505796c8dcSSimon Schubert 
1651c50c785cSJohn Marino   map_display_numbers (args, do_delete_display, NULL);
16525796c8dcSSimon Schubert   dont_repeat ();
16535796c8dcSSimon Schubert }
16545796c8dcSSimon Schubert 
16555796c8dcSSimon Schubert /* Display a single auto-display.
16565796c8dcSSimon Schubert    Do nothing if the display cannot be printed in the current context,
16575796c8dcSSimon Schubert    or if the display is disabled.  */
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert static void
16605796c8dcSSimon Schubert do_one_display (struct display *d)
16615796c8dcSSimon Schubert {
1662*a45ae5f8SJohn Marino   struct cleanup *old_chain;
16635796c8dcSSimon Schubert   int within_current_scope;
16645796c8dcSSimon Schubert 
16655796c8dcSSimon Schubert   if (d->enabled_p == 0)
16665796c8dcSSimon Schubert     return;
16675796c8dcSSimon Schubert 
1668cf7f2e2dSJohn Marino   /* The expression carries the architecture that was used at parse time.
1669cf7f2e2dSJohn Marino      This is a problem if the expression depends on architecture features
1670cf7f2e2dSJohn Marino      (e.g. register numbers), and the current architecture is now different.
1671cf7f2e2dSJohn Marino      For example, a display statement like "display/i $pc" is expected to
1672cf7f2e2dSJohn Marino      display the PC register of the current architecture, not the arch at
1673cf7f2e2dSJohn Marino      the time the display command was given.  Therefore, we re-parse the
1674cf7f2e2dSJohn Marino      expression if the current architecture has changed.  */
1675cf7f2e2dSJohn Marino   if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
1676cf7f2e2dSJohn Marino     {
1677cf7f2e2dSJohn Marino       xfree (d->exp);
1678cf7f2e2dSJohn Marino       d->exp = NULL;
1679cf7f2e2dSJohn Marino       d->block = NULL;
1680cf7f2e2dSJohn Marino     }
1681cf7f2e2dSJohn Marino 
16825796c8dcSSimon Schubert   if (d->exp == NULL)
16835796c8dcSSimon Schubert     {
16845796c8dcSSimon Schubert       volatile struct gdb_exception ex;
1685cf7f2e2dSJohn Marino 
16865796c8dcSSimon Schubert       TRY_CATCH (ex, RETURN_MASK_ALL)
16875796c8dcSSimon Schubert 	{
16885796c8dcSSimon Schubert 	  innermost_block = NULL;
16895796c8dcSSimon Schubert 	  d->exp = parse_expression (d->exp_string);
16905796c8dcSSimon Schubert 	  d->block = innermost_block;
16915796c8dcSSimon Schubert 	}
16925796c8dcSSimon Schubert       if (ex.reason < 0)
16935796c8dcSSimon Schubert 	{
16945796c8dcSSimon Schubert 	  /* Can't re-parse the expression.  Disable this display item.  */
16955796c8dcSSimon Schubert 	  d->enabled_p = 0;
16965796c8dcSSimon Schubert 	  warning (_("Unable to display \"%s\": %s"),
16975796c8dcSSimon Schubert 		   d->exp_string, ex.message);
16985796c8dcSSimon Schubert 	  return;
16995796c8dcSSimon Schubert 	}
17005796c8dcSSimon Schubert     }
17015796c8dcSSimon Schubert 
17025796c8dcSSimon Schubert   if (d->block)
1703cf7f2e2dSJohn Marino     {
1704cf7f2e2dSJohn Marino       if (d->pspace == current_program_space)
17055796c8dcSSimon Schubert 	within_current_scope = contained_in (get_selected_block (0), d->block);
17065796c8dcSSimon Schubert       else
1707cf7f2e2dSJohn Marino 	within_current_scope = 0;
1708cf7f2e2dSJohn Marino     }
1709cf7f2e2dSJohn Marino   else
17105796c8dcSSimon Schubert     within_current_scope = 1;
17115796c8dcSSimon Schubert   if (!within_current_scope)
17125796c8dcSSimon Schubert     return;
17135796c8dcSSimon Schubert 
1714*a45ae5f8SJohn Marino   old_chain = make_cleanup_restore_integer (&current_display_number);
17155796c8dcSSimon Schubert   current_display_number = d->number;
17165796c8dcSSimon Schubert 
17175796c8dcSSimon Schubert   annotate_display_begin ();
17185796c8dcSSimon Schubert   printf_filtered ("%d", d->number);
17195796c8dcSSimon Schubert   annotate_display_number_end ();
17205796c8dcSSimon Schubert   printf_filtered (": ");
17215796c8dcSSimon Schubert   if (d->format.size)
17225796c8dcSSimon Schubert     {
1723*a45ae5f8SJohn Marino       volatile struct gdb_exception ex;
17245796c8dcSSimon Schubert 
17255796c8dcSSimon Schubert       annotate_display_format ();
17265796c8dcSSimon Schubert 
17275796c8dcSSimon Schubert       printf_filtered ("x/");
17285796c8dcSSimon Schubert       if (d->format.count != 1)
17295796c8dcSSimon Schubert 	printf_filtered ("%d", d->format.count);
17305796c8dcSSimon Schubert       printf_filtered ("%c", d->format.format);
17315796c8dcSSimon Schubert       if (d->format.format != 'i' && d->format.format != 's')
17325796c8dcSSimon Schubert 	printf_filtered ("%c", d->format.size);
17335796c8dcSSimon Schubert       printf_filtered (" ");
17345796c8dcSSimon Schubert 
17355796c8dcSSimon Schubert       annotate_display_expression ();
17365796c8dcSSimon Schubert 
17375796c8dcSSimon Schubert       puts_filtered (d->exp_string);
17385796c8dcSSimon Schubert       annotate_display_expression_end ();
17395796c8dcSSimon Schubert 
17405796c8dcSSimon Schubert       if (d->format.count != 1 || d->format.format == 'i')
17415796c8dcSSimon Schubert 	printf_filtered ("\n");
17425796c8dcSSimon Schubert       else
17435796c8dcSSimon Schubert 	printf_filtered ("  ");
17445796c8dcSSimon Schubert 
1745*a45ae5f8SJohn Marino       annotate_display_value ();
1746*a45ae5f8SJohn Marino 
1747*a45ae5f8SJohn Marino       TRY_CATCH (ex, RETURN_MASK_ERROR)
1748*a45ae5f8SJohn Marino         {
1749*a45ae5f8SJohn Marino 	  struct value *val;
1750*a45ae5f8SJohn Marino 	  CORE_ADDR addr;
1751*a45ae5f8SJohn Marino 
17525796c8dcSSimon Schubert 	  val = evaluate_expression (d->exp);
17535796c8dcSSimon Schubert 	  addr = value_as_address (val);
17545796c8dcSSimon Schubert 	  if (d->format.format == 'i')
17555796c8dcSSimon Schubert 	    addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
17565796c8dcSSimon Schubert 	  do_examine (d->format, d->exp->gdbarch, addr);
17575796c8dcSSimon Schubert 	}
1758*a45ae5f8SJohn Marino       if (ex.reason < 0)
1759*a45ae5f8SJohn Marino 	fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
1760*a45ae5f8SJohn Marino     }
17615796c8dcSSimon Schubert   else
17625796c8dcSSimon Schubert     {
17635796c8dcSSimon Schubert       struct value_print_options opts;
1764*a45ae5f8SJohn Marino       volatile struct gdb_exception ex;
17655796c8dcSSimon Schubert 
17665796c8dcSSimon Schubert       annotate_display_format ();
17675796c8dcSSimon Schubert 
17685796c8dcSSimon Schubert       if (d->format.format)
17695796c8dcSSimon Schubert 	printf_filtered ("/%c ", d->format.format);
17705796c8dcSSimon Schubert 
17715796c8dcSSimon Schubert       annotate_display_expression ();
17725796c8dcSSimon Schubert 
17735796c8dcSSimon Schubert       puts_filtered (d->exp_string);
17745796c8dcSSimon Schubert       annotate_display_expression_end ();
17755796c8dcSSimon Schubert 
17765796c8dcSSimon Schubert       printf_filtered (" = ");
17775796c8dcSSimon Schubert 
17785796c8dcSSimon Schubert       annotate_display_expression ();
17795796c8dcSSimon Schubert 
17805796c8dcSSimon Schubert       get_formatted_print_options (&opts, d->format.format);
17815796c8dcSSimon Schubert       opts.raw = d->format.raw;
1782*a45ae5f8SJohn Marino 
1783*a45ae5f8SJohn Marino       TRY_CATCH (ex, RETURN_MASK_ERROR)
1784*a45ae5f8SJohn Marino         {
1785*a45ae5f8SJohn Marino 	  struct value *val;
1786*a45ae5f8SJohn Marino 
1787*a45ae5f8SJohn Marino 	  val = evaluate_expression (d->exp);
1788*a45ae5f8SJohn Marino 	  print_formatted (val, d->format.size, &opts, gdb_stdout);
1789*a45ae5f8SJohn Marino 	}
1790*a45ae5f8SJohn Marino       if (ex.reason < 0)
1791*a45ae5f8SJohn Marino 	fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
17925796c8dcSSimon Schubert       printf_filtered ("\n");
17935796c8dcSSimon Schubert     }
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert   annotate_display_end ();
17965796c8dcSSimon Schubert 
17975796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
1798*a45ae5f8SJohn Marino   do_cleanups (old_chain);
17995796c8dcSSimon Schubert }
18005796c8dcSSimon Schubert 
18015796c8dcSSimon Schubert /* Display all of the values on the auto-display chain which can be
18025796c8dcSSimon Schubert    evaluated in the current scope.  */
18035796c8dcSSimon Schubert 
18045796c8dcSSimon Schubert void
18055796c8dcSSimon Schubert do_displays (void)
18065796c8dcSSimon Schubert {
18075796c8dcSSimon Schubert   struct display *d;
18085796c8dcSSimon Schubert 
18095796c8dcSSimon Schubert   for (d = display_chain; d; d = d->next)
18105796c8dcSSimon Schubert     do_one_display (d);
18115796c8dcSSimon Schubert }
18125796c8dcSSimon Schubert 
18135796c8dcSSimon Schubert /* Delete the auto-display which we were in the process of displaying.
18145796c8dcSSimon Schubert    This is done when there is an error or a signal.  */
18155796c8dcSSimon Schubert 
18165796c8dcSSimon Schubert void
18175796c8dcSSimon Schubert disable_display (int num)
18185796c8dcSSimon Schubert {
18195796c8dcSSimon Schubert   struct display *d;
18205796c8dcSSimon Schubert 
18215796c8dcSSimon Schubert   for (d = display_chain; d; d = d->next)
18225796c8dcSSimon Schubert     if (d->number == num)
18235796c8dcSSimon Schubert       {
18245796c8dcSSimon Schubert 	d->enabled_p = 0;
18255796c8dcSSimon Schubert 	return;
18265796c8dcSSimon Schubert       }
18275796c8dcSSimon Schubert   printf_unfiltered (_("No display number %d.\n"), num);
18285796c8dcSSimon Schubert }
18295796c8dcSSimon Schubert 
18305796c8dcSSimon Schubert void
18315796c8dcSSimon Schubert disable_current_display (void)
18325796c8dcSSimon Schubert {
18335796c8dcSSimon Schubert   if (current_display_number >= 0)
18345796c8dcSSimon Schubert     {
18355796c8dcSSimon Schubert       disable_display (current_display_number);
1836c50c785cSJohn Marino       fprintf_unfiltered (gdb_stderr,
1837c50c785cSJohn Marino 			  _("Disabling display %d to "
1838c50c785cSJohn Marino 			    "avoid infinite recursion.\n"),
18395796c8dcSSimon Schubert 			  current_display_number);
18405796c8dcSSimon Schubert     }
18415796c8dcSSimon Schubert   current_display_number = -1;
18425796c8dcSSimon Schubert }
18435796c8dcSSimon Schubert 
18445796c8dcSSimon Schubert static void
18455796c8dcSSimon Schubert display_info (char *ignore, int from_tty)
18465796c8dcSSimon Schubert {
18475796c8dcSSimon Schubert   struct display *d;
18485796c8dcSSimon Schubert 
18495796c8dcSSimon Schubert   if (!display_chain)
18505796c8dcSSimon Schubert     printf_unfiltered (_("There are no auto-display expressions now.\n"));
18515796c8dcSSimon Schubert   else
18525796c8dcSSimon Schubert     printf_filtered (_("Auto-display expressions now in effect:\n\
18535796c8dcSSimon Schubert Num Enb Expression\n"));
18545796c8dcSSimon Schubert 
18555796c8dcSSimon Schubert   for (d = display_chain; d; d = d->next)
18565796c8dcSSimon Schubert     {
18575796c8dcSSimon Schubert       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
18585796c8dcSSimon Schubert       if (d->format.size)
18595796c8dcSSimon Schubert 	printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
18605796c8dcSSimon Schubert 			 d->format.format);
18615796c8dcSSimon Schubert       else if (d->format.format)
18625796c8dcSSimon Schubert 	printf_filtered ("/%c ", d->format.format);
18635796c8dcSSimon Schubert       puts_filtered (d->exp_string);
18645796c8dcSSimon Schubert       if (d->block && !contained_in (get_selected_block (0), d->block))
18655796c8dcSSimon Schubert 	printf_filtered (_(" (cannot be evaluated in the current context)"));
18665796c8dcSSimon Schubert       printf_filtered ("\n");
18675796c8dcSSimon Schubert       gdb_flush (gdb_stdout);
18685796c8dcSSimon Schubert     }
18695796c8dcSSimon Schubert }
18705796c8dcSSimon Schubert 
1871c50c785cSJohn Marino /* Callback fo map_display_numbers, that enables or disables the
1872c50c785cSJohn Marino    passed in display D.  */
1873c50c785cSJohn Marino 
18745796c8dcSSimon Schubert static void
1875c50c785cSJohn Marino do_enable_disable_display (struct display *d, void *data)
18765796c8dcSSimon Schubert {
1877c50c785cSJohn Marino   d->enabled_p = *(int *) data;
1878c50c785cSJohn Marino }
1879c50c785cSJohn Marino 
1880c50c785cSJohn Marino /* Implamentation of both the "disable display" and "enable display"
1881c50c785cSJohn Marino    commands.  ENABLE decides what to do.  */
1882c50c785cSJohn Marino 
1883c50c785cSJohn Marino static void
1884c50c785cSJohn Marino enable_disable_display_command (char *args, int from_tty, int enable)
1885c50c785cSJohn Marino {
1886c50c785cSJohn Marino   if (args == NULL)
1887c50c785cSJohn Marino     {
18885796c8dcSSimon Schubert       struct display *d;
18895796c8dcSSimon Schubert 
1890c50c785cSJohn Marino       ALL_DISPLAYS (d)
1891c50c785cSJohn Marino 	d->enabled_p = enable;
1892c50c785cSJohn Marino       return;
18935796c8dcSSimon Schubert     }
18945796c8dcSSimon Schubert 
1895c50c785cSJohn Marino   map_display_numbers (args, do_enable_disable_display, &enable);
1896c50c785cSJohn Marino }
18975796c8dcSSimon Schubert 
1898c50c785cSJohn Marino /* The "enable display" command.  */
1899c50c785cSJohn Marino 
1900c50c785cSJohn Marino static void
1901c50c785cSJohn Marino enable_display_command (char *args, int from_tty)
19025796c8dcSSimon Schubert {
1903c50c785cSJohn Marino   enable_disable_display_command (args, from_tty, 1);
19045796c8dcSSimon Schubert }
1905c50c785cSJohn Marino 
1906c50c785cSJohn Marino /* The "disable display" command.  */
19075796c8dcSSimon Schubert 
19085796c8dcSSimon Schubert static void
19095796c8dcSSimon Schubert disable_display_command (char *args, int from_tty)
19105796c8dcSSimon Schubert {
1911c50c785cSJohn Marino   enable_disable_display_command (args, from_tty, 0);
19125796c8dcSSimon Schubert }
19135796c8dcSSimon Schubert 
19145796c8dcSSimon Schubert /* display_chain items point to blocks and expressions.  Some expressions in
19155796c8dcSSimon Schubert    turn may point to symbols.
19165796c8dcSSimon Schubert    Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
19175796c8dcSSimon Schubert    obstack_free'd when a shared library is unloaded.
19185796c8dcSSimon Schubert    Clear pointers that are about to become dangling.
19195796c8dcSSimon Schubert    Both .exp and .block fields will be restored next time we need to display
19205796c8dcSSimon Schubert    an item by re-parsing .exp_string field in the new execution context.  */
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert static void
19235796c8dcSSimon Schubert clear_dangling_display_expressions (struct so_list *solib)
19245796c8dcSSimon Schubert {
1925cf7f2e2dSJohn Marino   struct objfile *objfile = solib->objfile;
19265796c8dcSSimon Schubert   struct display *d;
19275796c8dcSSimon Schubert 
1928cf7f2e2dSJohn Marino   /* With no symbol file we cannot have a block or expression from it.  */
1929cf7f2e2dSJohn Marino   if (objfile == NULL)
1930cf7f2e2dSJohn Marino     return;
1931cf7f2e2dSJohn Marino   if (objfile->separate_debug_objfile_backlink)
1932cf7f2e2dSJohn Marino     objfile = objfile->separate_debug_objfile_backlink;
1933cf7f2e2dSJohn Marino   gdb_assert (objfile->pspace == solib->pspace);
1934cf7f2e2dSJohn Marino 
1935cf7f2e2dSJohn Marino   for (d = display_chain; d != NULL; d = d->next)
19365796c8dcSSimon Schubert     {
1937cf7f2e2dSJohn Marino       if (d->pspace != solib->pspace)
1938cf7f2e2dSJohn Marino 	continue;
1939cf7f2e2dSJohn Marino 
1940cf7f2e2dSJohn Marino       if (lookup_objfile_from_block (d->block) == objfile
1941cf7f2e2dSJohn Marino 	  || (d->exp && exp_uses_objfile (d->exp, objfile)))
19425796c8dcSSimon Schubert       {
19435796c8dcSSimon Schubert 	xfree (d->exp);
19445796c8dcSSimon Schubert 	d->exp = NULL;
19455796c8dcSSimon Schubert 	d->block = NULL;
19465796c8dcSSimon Schubert       }
19475796c8dcSSimon Schubert     }
19485796c8dcSSimon Schubert }
19495796c8dcSSimon Schubert 
19505796c8dcSSimon Schubert 
19515796c8dcSSimon Schubert /* Print the value in stack frame FRAME of a variable specified by a
19525796c8dcSSimon Schubert    struct symbol.  NAME is the name to print; if NULL then VAR's print
19535796c8dcSSimon Schubert    name will be used.  STREAM is the ui_file on which to print the
19545796c8dcSSimon Schubert    value.  INDENT specifies the number of indent levels to print
19555796c8dcSSimon Schubert    before printing the variable name.  */
19565796c8dcSSimon Schubert 
19575796c8dcSSimon Schubert void
19585796c8dcSSimon Schubert print_variable_and_value (const char *name, struct symbol *var,
19595796c8dcSSimon Schubert 			  struct frame_info *frame,
19605796c8dcSSimon Schubert 			  struct ui_file *stream, int indent)
19615796c8dcSSimon Schubert {
1962cf7f2e2dSJohn Marino   volatile struct gdb_exception except;
19635796c8dcSSimon Schubert 
19645796c8dcSSimon Schubert   if (!name)
19655796c8dcSSimon Schubert     name = SYMBOL_PRINT_NAME (var);
19665796c8dcSSimon Schubert 
19675796c8dcSSimon Schubert   fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
1968cf7f2e2dSJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
1969cf7f2e2dSJohn Marino     {
1970cf7f2e2dSJohn Marino       struct value *val;
1971cf7f2e2dSJohn Marino       struct value_print_options opts;
19725796c8dcSSimon Schubert 
19735796c8dcSSimon Schubert       val = read_var_value (var, frame);
19745796c8dcSSimon Schubert       get_user_print_options (&opts);
1975*a45ae5f8SJohn Marino       opts.deref_ref = 1;
19765796c8dcSSimon Schubert       common_val_print (val, stream, indent, &opts, current_language);
1977cf7f2e2dSJohn Marino     }
1978cf7f2e2dSJohn Marino   if (except.reason < 0)
1979cf7f2e2dSJohn Marino     fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
1980cf7f2e2dSJohn Marino 		     except.message);
19815796c8dcSSimon Schubert   fprintf_filtered (stream, "\n");
19825796c8dcSSimon Schubert }
19835796c8dcSSimon Schubert 
1984cf7f2e2dSJohn Marino /* printf "printf format string" ARG to STREAM.  */
1985cf7f2e2dSJohn Marino 
19865796c8dcSSimon Schubert static void
1987cf7f2e2dSJohn Marino ui_printf (char *arg, struct ui_file *stream)
19885796c8dcSSimon Schubert {
19895796c8dcSSimon Schubert   char *f = NULL;
19905796c8dcSSimon Schubert   char *s = arg;
19915796c8dcSSimon Schubert   char *string = NULL;
19925796c8dcSSimon Schubert   struct value **val_args;
19935796c8dcSSimon Schubert   char *substrings;
19945796c8dcSSimon Schubert   char *current_substring;
19955796c8dcSSimon Schubert   int nargs = 0;
19965796c8dcSSimon Schubert   int allocated_args = 20;
19975796c8dcSSimon Schubert   struct cleanup *old_cleanups;
19985796c8dcSSimon Schubert 
19995796c8dcSSimon Schubert   val_args = xmalloc (allocated_args * sizeof (struct value *));
20005796c8dcSSimon Schubert   old_cleanups = make_cleanup (free_current_contents, &val_args);
20015796c8dcSSimon Schubert 
20025796c8dcSSimon Schubert   if (s == 0)
20035796c8dcSSimon Schubert     error_no_arg (_("format-control string and values to print"));
20045796c8dcSSimon Schubert 
2005c50c785cSJohn Marino   s = skip_spaces (s);
20065796c8dcSSimon Schubert 
20075796c8dcSSimon Schubert   /* A format string should follow, enveloped in double quotes.  */
20085796c8dcSSimon Schubert   if (*s++ != '"')
20095796c8dcSSimon Schubert     error (_("Bad format string, missing '\"'."));
20105796c8dcSSimon Schubert 
20115796c8dcSSimon Schubert   /* Parse the format-control string and copy it into the string STRING,
20125796c8dcSSimon Schubert      processing some kinds of escape sequence.  */
20135796c8dcSSimon Schubert 
20145796c8dcSSimon Schubert   f = string = (char *) alloca (strlen (s) + 1);
20155796c8dcSSimon Schubert 
20165796c8dcSSimon Schubert   while (*s != '"')
20175796c8dcSSimon Schubert     {
20185796c8dcSSimon Schubert       int c = *s++;
20195796c8dcSSimon Schubert       switch (c)
20205796c8dcSSimon Schubert 	{
20215796c8dcSSimon Schubert 	case '\0':
20225796c8dcSSimon Schubert 	  error (_("Bad format string, non-terminated '\"'."));
20235796c8dcSSimon Schubert 
20245796c8dcSSimon Schubert 	case '\\':
20255796c8dcSSimon Schubert 	  switch (c = *s++)
20265796c8dcSSimon Schubert 	    {
20275796c8dcSSimon Schubert 	    case '\\':
20285796c8dcSSimon Schubert 	      *f++ = '\\';
20295796c8dcSSimon Schubert 	      break;
20305796c8dcSSimon Schubert 	    case 'a':
20315796c8dcSSimon Schubert 	      *f++ = '\a';
20325796c8dcSSimon Schubert 	      break;
20335796c8dcSSimon Schubert 	    case 'b':
20345796c8dcSSimon Schubert 	      *f++ = '\b';
20355796c8dcSSimon Schubert 	      break;
20365796c8dcSSimon Schubert 	    case 'f':
20375796c8dcSSimon Schubert 	      *f++ = '\f';
20385796c8dcSSimon Schubert 	      break;
20395796c8dcSSimon Schubert 	    case 'n':
20405796c8dcSSimon Schubert 	      *f++ = '\n';
20415796c8dcSSimon Schubert 	      break;
20425796c8dcSSimon Schubert 	    case 'r':
20435796c8dcSSimon Schubert 	      *f++ = '\r';
20445796c8dcSSimon Schubert 	      break;
20455796c8dcSSimon Schubert 	    case 't':
20465796c8dcSSimon Schubert 	      *f++ = '\t';
20475796c8dcSSimon Schubert 	      break;
20485796c8dcSSimon Schubert 	    case 'v':
20495796c8dcSSimon Schubert 	      *f++ = '\v';
20505796c8dcSSimon Schubert 	      break;
20515796c8dcSSimon Schubert 	    case '"':
20525796c8dcSSimon Schubert 	      *f++ = '"';
20535796c8dcSSimon Schubert 	      break;
20545796c8dcSSimon Schubert 	    default:
2055c50c785cSJohn Marino 	      /* ??? TODO: handle other escape sequences.  */
20565796c8dcSSimon Schubert 	      error (_("Unrecognized escape character \\%c in format string."),
20575796c8dcSSimon Schubert 		     c);
20585796c8dcSSimon Schubert 	    }
20595796c8dcSSimon Schubert 	  break;
20605796c8dcSSimon Schubert 
20615796c8dcSSimon Schubert 	default:
20625796c8dcSSimon Schubert 	  *f++ = c;
20635796c8dcSSimon Schubert 	}
20645796c8dcSSimon Schubert     }
20655796c8dcSSimon Schubert 
20665796c8dcSSimon Schubert   /* Skip over " and following space and comma.  */
20675796c8dcSSimon Schubert   s++;
20685796c8dcSSimon Schubert   *f++ = '\0';
2069c50c785cSJohn Marino   s = skip_spaces (s);
20705796c8dcSSimon Schubert 
20715796c8dcSSimon Schubert   if (*s != ',' && *s != 0)
20725796c8dcSSimon Schubert     error (_("Invalid argument syntax"));
20735796c8dcSSimon Schubert 
20745796c8dcSSimon Schubert   if (*s == ',')
20755796c8dcSSimon Schubert     s++;
2076c50c785cSJohn Marino   s = skip_spaces (s);
20775796c8dcSSimon Schubert 
20785796c8dcSSimon Schubert   /* Need extra space for the '\0's.  Doubling the size is sufficient.  */
20795796c8dcSSimon Schubert   substrings = alloca (strlen (string) * 2);
20805796c8dcSSimon Schubert   current_substring = substrings;
20815796c8dcSSimon Schubert 
20825796c8dcSSimon Schubert   {
20835796c8dcSSimon Schubert     /* Now scan the string for %-specs and see what kinds of args they want.
20845796c8dcSSimon Schubert        argclass[I] classifies the %-specs so we can give printf_filtered
20855796c8dcSSimon Schubert        something of the right size.  */
20865796c8dcSSimon Schubert 
20875796c8dcSSimon Schubert     enum argclass
20885796c8dcSSimon Schubert       {
20895796c8dcSSimon Schubert 	int_arg, long_arg, long_long_arg, ptr_arg,
20905796c8dcSSimon Schubert 	string_arg, wide_string_arg, wide_char_arg,
20915796c8dcSSimon Schubert 	double_arg, long_double_arg, decfloat_arg
20925796c8dcSSimon Schubert       };
20935796c8dcSSimon Schubert     enum argclass *argclass;
20945796c8dcSSimon Schubert     enum argclass this_argclass;
20955796c8dcSSimon Schubert     char *last_arg;
20965796c8dcSSimon Schubert     int nargs_wanted;
20975796c8dcSSimon Schubert     int i;
20985796c8dcSSimon Schubert 
20995796c8dcSSimon Schubert     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
21005796c8dcSSimon Schubert     nargs_wanted = 0;
21015796c8dcSSimon Schubert     f = string;
21025796c8dcSSimon Schubert     last_arg = string;
21035796c8dcSSimon Schubert     while (*f)
21045796c8dcSSimon Schubert       if (*f++ == '%')
21055796c8dcSSimon Schubert 	{
21065796c8dcSSimon Schubert 	  int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
21075796c8dcSSimon Schubert 	  int seen_space = 0, seen_plus = 0;
21085796c8dcSSimon Schubert 	  int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
21095796c8dcSSimon Schubert 	  int seen_big_d = 0, seen_double_big_d = 0;
21105796c8dcSSimon Schubert 	  int bad = 0;
21115796c8dcSSimon Schubert 
21125796c8dcSSimon Schubert 	  /* Check the validity of the format specifier, and work
21135796c8dcSSimon Schubert 	     out what argument it expects.  We only accept C89
21145796c8dcSSimon Schubert 	     format strings, with the exception of long long (which
21155796c8dcSSimon Schubert 	     we autoconf for).  */
21165796c8dcSSimon Schubert 
21175796c8dcSSimon Schubert 	  /* Skip over "%%".  */
21185796c8dcSSimon Schubert 	  if (*f == '%')
21195796c8dcSSimon Schubert 	    {
21205796c8dcSSimon Schubert 	      f++;
21215796c8dcSSimon Schubert 	      continue;
21225796c8dcSSimon Schubert 	    }
21235796c8dcSSimon Schubert 
21245796c8dcSSimon Schubert 	  /* The first part of a format specifier is a set of flag
21255796c8dcSSimon Schubert 	     characters.  */
21265796c8dcSSimon Schubert 	  while (strchr ("0-+ #", *f))
21275796c8dcSSimon Schubert 	    {
21285796c8dcSSimon Schubert 	      if (*f == '#')
21295796c8dcSSimon Schubert 		seen_hash = 1;
21305796c8dcSSimon Schubert 	      else if (*f == '0')
21315796c8dcSSimon Schubert 		seen_zero = 1;
21325796c8dcSSimon Schubert 	      else if (*f == ' ')
21335796c8dcSSimon Schubert 		seen_space = 1;
21345796c8dcSSimon Schubert 	      else if (*f == '+')
21355796c8dcSSimon Schubert 		seen_plus = 1;
21365796c8dcSSimon Schubert 	      f++;
21375796c8dcSSimon Schubert 	    }
21385796c8dcSSimon Schubert 
21395796c8dcSSimon Schubert 	  /* The next part of a format specifier is a width.  */
21405796c8dcSSimon Schubert 	  while (strchr ("0123456789", *f))
21415796c8dcSSimon Schubert 	    f++;
21425796c8dcSSimon Schubert 
21435796c8dcSSimon Schubert 	  /* The next part of a format specifier is a precision.  */
21445796c8dcSSimon Schubert 	  if (*f == '.')
21455796c8dcSSimon Schubert 	    {
21465796c8dcSSimon Schubert 	      seen_prec = 1;
21475796c8dcSSimon Schubert 	      f++;
21485796c8dcSSimon Schubert 	      while (strchr ("0123456789", *f))
21495796c8dcSSimon Schubert 		f++;
21505796c8dcSSimon Schubert 	    }
21515796c8dcSSimon Schubert 
21525796c8dcSSimon Schubert 	  /* The next part of a format specifier is a length modifier.  */
21535796c8dcSSimon Schubert 	  if (*f == 'h')
21545796c8dcSSimon Schubert 	    {
21555796c8dcSSimon Schubert 	      seen_h = 1;
21565796c8dcSSimon Schubert 	      f++;
21575796c8dcSSimon Schubert 	    }
21585796c8dcSSimon Schubert 	  else if (*f == 'l')
21595796c8dcSSimon Schubert 	    {
21605796c8dcSSimon Schubert 	      f++;
21615796c8dcSSimon Schubert 	      lcount++;
21625796c8dcSSimon Schubert 	      if (*f == 'l')
21635796c8dcSSimon Schubert 		{
21645796c8dcSSimon Schubert 		  f++;
21655796c8dcSSimon Schubert 		  lcount++;
21665796c8dcSSimon Schubert 		}
21675796c8dcSSimon Schubert 	    }
21685796c8dcSSimon Schubert 	  else if (*f == 'L')
21695796c8dcSSimon Schubert 	    {
21705796c8dcSSimon Schubert 	      seen_big_l = 1;
21715796c8dcSSimon Schubert 	      f++;
21725796c8dcSSimon Schubert 	    }
21735796c8dcSSimon Schubert 	  /* Decimal32 modifier.  */
21745796c8dcSSimon Schubert 	  else if (*f == 'H')
21755796c8dcSSimon Schubert 	    {
21765796c8dcSSimon Schubert 	      seen_big_h = 1;
21775796c8dcSSimon Schubert 	      f++;
21785796c8dcSSimon Schubert 	    }
21795796c8dcSSimon Schubert 	  /* Decimal64 and Decimal128 modifiers.  */
21805796c8dcSSimon Schubert 	  else if (*f == 'D')
21815796c8dcSSimon Schubert 	    {
21825796c8dcSSimon Schubert 	      f++;
21835796c8dcSSimon Schubert 
21845796c8dcSSimon Schubert 	      /* Check for a Decimal128.  */
21855796c8dcSSimon Schubert 	      if (*f == 'D')
21865796c8dcSSimon Schubert 		{
21875796c8dcSSimon Schubert 		  f++;
21885796c8dcSSimon Schubert 		  seen_double_big_d = 1;
21895796c8dcSSimon Schubert 		}
21905796c8dcSSimon Schubert 	      else
21915796c8dcSSimon Schubert 		seen_big_d = 1;
21925796c8dcSSimon Schubert 	    }
21935796c8dcSSimon Schubert 
21945796c8dcSSimon Schubert 	  switch (*f)
21955796c8dcSSimon Schubert 	    {
21965796c8dcSSimon Schubert 	    case 'u':
21975796c8dcSSimon Schubert 	      if (seen_hash)
21985796c8dcSSimon Schubert 		bad = 1;
21995796c8dcSSimon Schubert 	      /* FALLTHROUGH */
22005796c8dcSSimon Schubert 
22015796c8dcSSimon Schubert 	    case 'o':
22025796c8dcSSimon Schubert 	    case 'x':
22035796c8dcSSimon Schubert 	    case 'X':
22045796c8dcSSimon Schubert 	      if (seen_space || seen_plus)
22055796c8dcSSimon Schubert 		bad = 1;
22065796c8dcSSimon Schubert 	      /* FALLTHROUGH */
22075796c8dcSSimon Schubert 
22085796c8dcSSimon Schubert 	    case 'd':
22095796c8dcSSimon Schubert 	    case 'i':
22105796c8dcSSimon Schubert 	      if (lcount == 0)
22115796c8dcSSimon Schubert 		this_argclass = int_arg;
22125796c8dcSSimon Schubert 	      else if (lcount == 1)
22135796c8dcSSimon Schubert 		this_argclass = long_arg;
22145796c8dcSSimon Schubert 	      else
22155796c8dcSSimon Schubert 		this_argclass = long_long_arg;
22165796c8dcSSimon Schubert 
22175796c8dcSSimon Schubert 	      if (seen_big_l)
22185796c8dcSSimon Schubert 		bad = 1;
22195796c8dcSSimon Schubert 	      break;
22205796c8dcSSimon Schubert 
22215796c8dcSSimon Schubert 	    case 'c':
22225796c8dcSSimon Schubert 	      this_argclass = lcount == 0 ? int_arg : wide_char_arg;
22235796c8dcSSimon Schubert 	      if (lcount > 1 || seen_h || seen_big_l)
22245796c8dcSSimon Schubert 		bad = 1;
22255796c8dcSSimon Schubert 	      if (seen_prec || seen_zero || seen_space || seen_plus)
22265796c8dcSSimon Schubert 		bad = 1;
22275796c8dcSSimon Schubert 	      break;
22285796c8dcSSimon Schubert 
22295796c8dcSSimon Schubert 	    case 'p':
22305796c8dcSSimon Schubert 	      this_argclass = ptr_arg;
22315796c8dcSSimon Schubert 	      if (lcount || seen_h || seen_big_l)
22325796c8dcSSimon Schubert 		bad = 1;
22335796c8dcSSimon Schubert 	      if (seen_prec || seen_zero || seen_space || seen_plus)
22345796c8dcSSimon Schubert 		bad = 1;
22355796c8dcSSimon Schubert 	      break;
22365796c8dcSSimon Schubert 
22375796c8dcSSimon Schubert 	    case 's':
22385796c8dcSSimon Schubert 	      this_argclass = lcount == 0 ? string_arg : wide_string_arg;
22395796c8dcSSimon Schubert 	      if (lcount > 1 || seen_h || seen_big_l)
22405796c8dcSSimon Schubert 		bad = 1;
22415796c8dcSSimon Schubert 	      if (seen_zero || seen_space || seen_plus)
22425796c8dcSSimon Schubert 		bad = 1;
22435796c8dcSSimon Schubert 	      break;
22445796c8dcSSimon Schubert 
22455796c8dcSSimon Schubert 	    case 'e':
22465796c8dcSSimon Schubert 	    case 'f':
22475796c8dcSSimon Schubert 	    case 'g':
22485796c8dcSSimon Schubert 	    case 'E':
22495796c8dcSSimon Schubert 	    case 'G':
22505796c8dcSSimon Schubert 	      if (seen_big_h || seen_big_d || seen_double_big_d)
22515796c8dcSSimon Schubert 		this_argclass = decfloat_arg;
22525796c8dcSSimon Schubert 	      else if (seen_big_l)
22535796c8dcSSimon Schubert 		this_argclass = long_double_arg;
22545796c8dcSSimon Schubert 	      else
22555796c8dcSSimon Schubert 		this_argclass = double_arg;
22565796c8dcSSimon Schubert 
22575796c8dcSSimon Schubert 	      if (lcount || seen_h)
22585796c8dcSSimon Schubert 		bad = 1;
22595796c8dcSSimon Schubert 	      break;
22605796c8dcSSimon Schubert 
22615796c8dcSSimon Schubert 	    case '*':
22625796c8dcSSimon Schubert 	      error (_("`*' not supported for precision or width in printf"));
22635796c8dcSSimon Schubert 
22645796c8dcSSimon Schubert 	    case 'n':
22655796c8dcSSimon Schubert 	      error (_("Format specifier `n' not supported in printf"));
22665796c8dcSSimon Schubert 
22675796c8dcSSimon Schubert 	    case '\0':
22685796c8dcSSimon Schubert 	      error (_("Incomplete format specifier at end of format string"));
22695796c8dcSSimon Schubert 
22705796c8dcSSimon Schubert 	    default:
22715796c8dcSSimon Schubert 	      error (_("Unrecognized format specifier '%c' in printf"), *f);
22725796c8dcSSimon Schubert 	    }
22735796c8dcSSimon Schubert 
22745796c8dcSSimon Schubert 	  if (bad)
2275c50c785cSJohn Marino 	    error (_("Inappropriate modifiers to "
2276c50c785cSJohn Marino 		     "format specifier '%c' in printf"),
22775796c8dcSSimon Schubert 		   *f);
22785796c8dcSSimon Schubert 
22795796c8dcSSimon Schubert 	  f++;
22805796c8dcSSimon Schubert 
22815796c8dcSSimon Schubert 	  if (lcount > 1 && USE_PRINTF_I64)
22825796c8dcSSimon Schubert 	    {
22835796c8dcSSimon Schubert 	      /* Windows' printf does support long long, but not the usual way.
22845796c8dcSSimon Schubert 		 Convert %lld to %I64d.  */
22855796c8dcSSimon Schubert 	      int length_before_ll = f - last_arg - 1 - lcount;
2286cf7f2e2dSJohn Marino 
22875796c8dcSSimon Schubert 	      strncpy (current_substring, last_arg, length_before_ll);
22885796c8dcSSimon Schubert 	      strcpy (current_substring + length_before_ll, "I64");
22895796c8dcSSimon Schubert 	      current_substring[length_before_ll + 3] =
22905796c8dcSSimon Schubert 		last_arg[length_before_ll + lcount];
22915796c8dcSSimon Schubert 	      current_substring += length_before_ll + 4;
22925796c8dcSSimon Schubert 	    }
22935796c8dcSSimon Schubert 	  else if (this_argclass == wide_string_arg
22945796c8dcSSimon Schubert 		   || this_argclass == wide_char_arg)
22955796c8dcSSimon Schubert 	    {
22965796c8dcSSimon Schubert 	      /* Convert %ls or %lc to %s.  */
22975796c8dcSSimon Schubert 	      int length_before_ls = f - last_arg - 2;
2298cf7f2e2dSJohn Marino 
22995796c8dcSSimon Schubert 	      strncpy (current_substring, last_arg, length_before_ls);
23005796c8dcSSimon Schubert 	      strcpy (current_substring + length_before_ls, "s");
23015796c8dcSSimon Schubert 	      current_substring += length_before_ls + 2;
23025796c8dcSSimon Schubert 	    }
23035796c8dcSSimon Schubert 	  else
23045796c8dcSSimon Schubert 	    {
23055796c8dcSSimon Schubert 	      strncpy (current_substring, last_arg, f - last_arg);
23065796c8dcSSimon Schubert 	      current_substring += f - last_arg;
23075796c8dcSSimon Schubert 	    }
23085796c8dcSSimon Schubert 	  *current_substring++ = '\0';
23095796c8dcSSimon Schubert 	  last_arg = f;
23105796c8dcSSimon Schubert 	  argclass[nargs_wanted++] = this_argclass;
23115796c8dcSSimon Schubert 	}
23125796c8dcSSimon Schubert 
23135796c8dcSSimon Schubert     /* Now, parse all arguments and evaluate them.
23145796c8dcSSimon Schubert        Store the VALUEs in VAL_ARGS.  */
23155796c8dcSSimon Schubert 
23165796c8dcSSimon Schubert     while (*s != '\0')
23175796c8dcSSimon Schubert       {
23185796c8dcSSimon Schubert 	char *s1;
2319cf7f2e2dSJohn Marino 
23205796c8dcSSimon Schubert 	if (nargs == allocated_args)
23215796c8dcSSimon Schubert 	  val_args = (struct value **) xrealloc ((char *) val_args,
23225796c8dcSSimon Schubert 						 (allocated_args *= 2)
23235796c8dcSSimon Schubert 						 * sizeof (struct value *));
23245796c8dcSSimon Schubert 	s1 = s;
23255796c8dcSSimon Schubert 	val_args[nargs] = parse_to_comma_and_eval (&s1);
23265796c8dcSSimon Schubert 
23275796c8dcSSimon Schubert 	nargs++;
23285796c8dcSSimon Schubert 	s = s1;
23295796c8dcSSimon Schubert 	if (*s == ',')
23305796c8dcSSimon Schubert 	  s++;
23315796c8dcSSimon Schubert       }
23325796c8dcSSimon Schubert 
23335796c8dcSSimon Schubert     if (nargs != nargs_wanted)
23345796c8dcSSimon Schubert       error (_("Wrong number of arguments for specified format-string"));
23355796c8dcSSimon Schubert 
23365796c8dcSSimon Schubert     /* Now actually print them.  */
23375796c8dcSSimon Schubert     current_substring = substrings;
23385796c8dcSSimon Schubert     for (i = 0; i < nargs; i++)
23395796c8dcSSimon Schubert       {
23405796c8dcSSimon Schubert 	switch (argclass[i])
23415796c8dcSSimon Schubert 	  {
23425796c8dcSSimon Schubert 	  case string_arg:
23435796c8dcSSimon Schubert 	    {
23445796c8dcSSimon Schubert 	      gdb_byte *str;
23455796c8dcSSimon Schubert 	      CORE_ADDR tem;
23465796c8dcSSimon Schubert 	      int j;
2347cf7f2e2dSJohn Marino 
23485796c8dcSSimon Schubert 	      tem = value_as_address (val_args[i]);
23495796c8dcSSimon Schubert 
23505796c8dcSSimon Schubert 	      /* This is a %s argument.  Find the length of the string.  */
23515796c8dcSSimon Schubert 	      for (j = 0;; j++)
23525796c8dcSSimon Schubert 		{
23535796c8dcSSimon Schubert 		  gdb_byte c;
2354cf7f2e2dSJohn Marino 
23555796c8dcSSimon Schubert 		  QUIT;
23565796c8dcSSimon Schubert 		  read_memory (tem + j, &c, 1);
23575796c8dcSSimon Schubert 		  if (c == 0)
23585796c8dcSSimon Schubert 		    break;
23595796c8dcSSimon Schubert 		}
23605796c8dcSSimon Schubert 
23615796c8dcSSimon Schubert 	      /* Copy the string contents into a string inside GDB.  */
23625796c8dcSSimon Schubert 	      str = (gdb_byte *) alloca (j + 1);
23635796c8dcSSimon Schubert 	      if (j != 0)
23645796c8dcSSimon Schubert 		read_memory (tem, str, j);
23655796c8dcSSimon Schubert 	      str[j] = 0;
23665796c8dcSSimon Schubert 
2367cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, (char *) str);
23685796c8dcSSimon Schubert 	    }
23695796c8dcSSimon Schubert 	    break;
23705796c8dcSSimon Schubert 	  case wide_string_arg:
23715796c8dcSSimon Schubert 	    {
23725796c8dcSSimon Schubert 	      gdb_byte *str;
23735796c8dcSSimon Schubert 	      CORE_ADDR tem;
23745796c8dcSSimon Schubert 	      int j;
23755796c8dcSSimon Schubert 	      struct gdbarch *gdbarch
23765796c8dcSSimon Schubert 		= get_type_arch (value_type (val_args[i]));
23775796c8dcSSimon Schubert 	      enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
23785796c8dcSSimon Schubert 	      struct type *wctype = lookup_typename (current_language, gdbarch,
23795796c8dcSSimon Schubert 						     "wchar_t", NULL, 0);
23805796c8dcSSimon Schubert 	      int wcwidth = TYPE_LENGTH (wctype);
23815796c8dcSSimon Schubert 	      gdb_byte *buf = alloca (wcwidth);
23825796c8dcSSimon Schubert 	      struct obstack output;
23835796c8dcSSimon Schubert 	      struct cleanup *inner_cleanup;
23845796c8dcSSimon Schubert 
23855796c8dcSSimon Schubert 	      tem = value_as_address (val_args[i]);
23865796c8dcSSimon Schubert 
23875796c8dcSSimon Schubert 	      /* This is a %s argument.  Find the length of the string.  */
23885796c8dcSSimon Schubert 	      for (j = 0;; j += wcwidth)
23895796c8dcSSimon Schubert 		{
23905796c8dcSSimon Schubert 		  QUIT;
23915796c8dcSSimon Schubert 		  read_memory (tem + j, buf, wcwidth);
23925796c8dcSSimon Schubert 		  if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
23935796c8dcSSimon Schubert 		    break;
23945796c8dcSSimon Schubert 		}
23955796c8dcSSimon Schubert 
23965796c8dcSSimon Schubert 	      /* Copy the string contents into a string inside GDB.  */
23975796c8dcSSimon Schubert 	      str = (gdb_byte *) alloca (j + wcwidth);
23985796c8dcSSimon Schubert 	      if (j != 0)
23995796c8dcSSimon Schubert 		read_memory (tem, str, j);
24005796c8dcSSimon Schubert 	      memset (&str[j], 0, wcwidth);
24015796c8dcSSimon Schubert 
24025796c8dcSSimon Schubert 	      obstack_init (&output);
24035796c8dcSSimon Schubert 	      inner_cleanup = make_cleanup_obstack_free (&output);
24045796c8dcSSimon Schubert 
2405cf7f2e2dSJohn Marino 	      convert_between_encodings (target_wide_charset (gdbarch),
24065796c8dcSSimon Schubert 					 host_charset (),
24075796c8dcSSimon Schubert 					 str, j, wcwidth,
24085796c8dcSSimon Schubert 					 &output, translit_char);
24095796c8dcSSimon Schubert 	      obstack_grow_str0 (&output, "");
24105796c8dcSSimon Schubert 
2411cf7f2e2dSJohn Marino 	      fprintf_filtered (stream, current_substring,
2412cf7f2e2dSJohn Marino                                 obstack_base (&output));
24135796c8dcSSimon Schubert 	      do_cleanups (inner_cleanup);
24145796c8dcSSimon Schubert 	    }
24155796c8dcSSimon Schubert 	    break;
24165796c8dcSSimon Schubert 	  case wide_char_arg:
24175796c8dcSSimon Schubert 	    {
24185796c8dcSSimon Schubert 	      struct gdbarch *gdbarch
24195796c8dcSSimon Schubert 		= get_type_arch (value_type (val_args[i]));
24205796c8dcSSimon Schubert 	      struct type *wctype = lookup_typename (current_language, gdbarch,
24215796c8dcSSimon Schubert 						     "wchar_t", NULL, 0);
24225796c8dcSSimon Schubert 	      struct type *valtype;
24235796c8dcSSimon Schubert 	      struct obstack output;
24245796c8dcSSimon Schubert 	      struct cleanup *inner_cleanup;
24255796c8dcSSimon Schubert 	      const gdb_byte *bytes;
24265796c8dcSSimon Schubert 
24275796c8dcSSimon Schubert 	      valtype = value_type (val_args[i]);
24285796c8dcSSimon Schubert 	      if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
24295796c8dcSSimon Schubert 		  || TYPE_CODE (valtype) != TYPE_CODE_INT)
24305796c8dcSSimon Schubert 		error (_("expected wchar_t argument for %%lc"));
24315796c8dcSSimon Schubert 
24325796c8dcSSimon Schubert 	      bytes = value_contents (val_args[i]);
24335796c8dcSSimon Schubert 
24345796c8dcSSimon Schubert 	      obstack_init (&output);
24355796c8dcSSimon Schubert 	      inner_cleanup = make_cleanup_obstack_free (&output);
24365796c8dcSSimon Schubert 
2437cf7f2e2dSJohn Marino 	      convert_between_encodings (target_wide_charset (gdbarch),
24385796c8dcSSimon Schubert 					 host_charset (),
24395796c8dcSSimon Schubert 					 bytes, TYPE_LENGTH (valtype),
24405796c8dcSSimon Schubert 					 TYPE_LENGTH (valtype),
24415796c8dcSSimon Schubert 					 &output, translit_char);
24425796c8dcSSimon Schubert 	      obstack_grow_str0 (&output, "");
24435796c8dcSSimon Schubert 
2444cf7f2e2dSJohn Marino 	      fprintf_filtered (stream, current_substring,
2445cf7f2e2dSJohn Marino                                 obstack_base (&output));
24465796c8dcSSimon Schubert 	      do_cleanups (inner_cleanup);
24475796c8dcSSimon Schubert 	    }
24485796c8dcSSimon Schubert 	    break;
24495796c8dcSSimon Schubert 	  case double_arg:
24505796c8dcSSimon Schubert 	    {
24515796c8dcSSimon Schubert 	      struct type *type = value_type (val_args[i]);
24525796c8dcSSimon Schubert 	      DOUBLEST val;
24535796c8dcSSimon Schubert 	      int inv;
24545796c8dcSSimon Schubert 
24555796c8dcSSimon Schubert 	      /* If format string wants a float, unchecked-convert the value
24565796c8dcSSimon Schubert 		 to floating point of the same size.  */
24575796c8dcSSimon Schubert 	      type = float_type_from_length (type);
24585796c8dcSSimon Schubert 	      val = unpack_double (type, value_contents (val_args[i]), &inv);
24595796c8dcSSimon Schubert 	      if (inv)
24605796c8dcSSimon Schubert 		error (_("Invalid floating value found in program."));
24615796c8dcSSimon Schubert 
2462cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, (double) val);
24635796c8dcSSimon Schubert 	      break;
24645796c8dcSSimon Schubert 	    }
24655796c8dcSSimon Schubert 	  case long_double_arg:
24665796c8dcSSimon Schubert #ifdef HAVE_LONG_DOUBLE
24675796c8dcSSimon Schubert 	    {
24685796c8dcSSimon Schubert 	      struct type *type = value_type (val_args[i]);
24695796c8dcSSimon Schubert 	      DOUBLEST val;
24705796c8dcSSimon Schubert 	      int inv;
24715796c8dcSSimon Schubert 
24725796c8dcSSimon Schubert 	      /* If format string wants a float, unchecked-convert the value
24735796c8dcSSimon Schubert 		 to floating point of the same size.  */
24745796c8dcSSimon Schubert 	      type = float_type_from_length (type);
24755796c8dcSSimon Schubert 	      val = unpack_double (type, value_contents (val_args[i]), &inv);
24765796c8dcSSimon Schubert 	      if (inv)
24775796c8dcSSimon Schubert 		error (_("Invalid floating value found in program."));
24785796c8dcSSimon Schubert 
2479cf7f2e2dSJohn Marino 	      fprintf_filtered (stream, current_substring,
2480cf7f2e2dSJohn Marino                                 (long double) val);
24815796c8dcSSimon Schubert 	      break;
24825796c8dcSSimon Schubert 	    }
24835796c8dcSSimon Schubert #else
24845796c8dcSSimon Schubert 	    error (_("long double not supported in printf"));
24855796c8dcSSimon Schubert #endif
24865796c8dcSSimon Schubert 	  case long_long_arg:
24875796c8dcSSimon Schubert #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
24885796c8dcSSimon Schubert 	    {
24895796c8dcSSimon Schubert 	      long long val = value_as_long (val_args[i]);
2490cf7f2e2dSJohn Marino 
2491cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, val);
24925796c8dcSSimon Schubert 	      break;
24935796c8dcSSimon Schubert 	    }
24945796c8dcSSimon Schubert #else
24955796c8dcSSimon Schubert 	    error (_("long long not supported in printf"));
24965796c8dcSSimon Schubert #endif
24975796c8dcSSimon Schubert 	  case int_arg:
24985796c8dcSSimon Schubert 	    {
24995796c8dcSSimon Schubert 	      int val = value_as_long (val_args[i]);
2500cf7f2e2dSJohn Marino 
2501cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, val);
25025796c8dcSSimon Schubert 	      break;
25035796c8dcSSimon Schubert 	    }
25045796c8dcSSimon Schubert 	  case long_arg:
25055796c8dcSSimon Schubert 	    {
25065796c8dcSSimon Schubert 	      long val = value_as_long (val_args[i]);
2507cf7f2e2dSJohn Marino 
2508cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, val);
25095796c8dcSSimon Schubert 	      break;
25105796c8dcSSimon Schubert 	    }
25115796c8dcSSimon Schubert 
25125796c8dcSSimon Schubert 	  /* Handles decimal floating values.  */
25135796c8dcSSimon Schubert 	case decfloat_arg:
25145796c8dcSSimon Schubert 	    {
25155796c8dcSSimon Schubert 	      const gdb_byte *param_ptr = value_contents (val_args[i]);
2516cf7f2e2dSJohn Marino 
25175796c8dcSSimon Schubert #if defined (PRINTF_HAS_DECFLOAT)
25185796c8dcSSimon Schubert 	      /* If we have native support for Decimal floating
25195796c8dcSSimon Schubert 		 printing, handle it here.  */
2520cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, param_ptr);
25215796c8dcSSimon Schubert #else
25225796c8dcSSimon Schubert 
25235796c8dcSSimon Schubert 	      /* As a workaround until vasprintf has native support for DFP
25245796c8dcSSimon Schubert 	       we convert the DFP values to string and print them using
25255796c8dcSSimon Schubert 	       the %s format specifier.  */
25265796c8dcSSimon Schubert 
25275796c8dcSSimon Schubert 	      char *eos, *sos;
25285796c8dcSSimon Schubert 	      int nnull_chars = 0;
25295796c8dcSSimon Schubert 
25305796c8dcSSimon Schubert 	      /* Parameter data.  */
25315796c8dcSSimon Schubert 	      struct type *param_type = value_type (val_args[i]);
25325796c8dcSSimon Schubert 	      unsigned int param_len = TYPE_LENGTH (param_type);
25335796c8dcSSimon Schubert 	      struct gdbarch *gdbarch = get_type_arch (param_type);
25345796c8dcSSimon Schubert 	      enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
25355796c8dcSSimon Schubert 
25365796c8dcSSimon Schubert 	      /* DFP output data.  */
25375796c8dcSSimon Schubert 	      struct value *dfp_value = NULL;
25385796c8dcSSimon Schubert 	      gdb_byte *dfp_ptr;
25395796c8dcSSimon Schubert 	      int dfp_len = 16;
25405796c8dcSSimon Schubert 	      gdb_byte dec[16];
25415796c8dcSSimon Schubert 	      struct type *dfp_type = NULL;
25425796c8dcSSimon Schubert 	      char decstr[MAX_DECIMAL_STRING];
25435796c8dcSSimon Schubert 
25445796c8dcSSimon Schubert 	      /* Points to the end of the string so that we can go back
25455796c8dcSSimon Schubert 		 and check for DFP length modifiers.  */
25465796c8dcSSimon Schubert 	      eos = current_substring + strlen (current_substring);
25475796c8dcSSimon Schubert 
25485796c8dcSSimon Schubert 	      /* Look for the float/double format specifier.  */
25495796c8dcSSimon Schubert 	      while (*eos != 'f' && *eos != 'e' && *eos != 'E'
25505796c8dcSSimon Schubert 		     && *eos != 'g' && *eos != 'G')
25515796c8dcSSimon Schubert 		  eos--;
25525796c8dcSSimon Schubert 
25535796c8dcSSimon Schubert 	      sos = eos;
25545796c8dcSSimon Schubert 
25555796c8dcSSimon Schubert 	      /* Search for the '%' char and extract the size and type of
25565796c8dcSSimon Schubert 		 the output decimal value based on its modifiers
25575796c8dcSSimon Schubert 		 (%Hf, %Df, %DDf).  */
25585796c8dcSSimon Schubert 	      while (*--sos != '%')
25595796c8dcSSimon Schubert 		{
25605796c8dcSSimon Schubert 		  if (*sos == 'H')
25615796c8dcSSimon Schubert 		    {
25625796c8dcSSimon Schubert 		      dfp_len = 4;
25635796c8dcSSimon Schubert 		      dfp_type = builtin_type (gdbarch)->builtin_decfloat;
25645796c8dcSSimon Schubert 		    }
25655796c8dcSSimon Schubert 		  else if (*sos == 'D' && *(sos - 1) == 'D')
25665796c8dcSSimon Schubert 		    {
25675796c8dcSSimon Schubert 		      dfp_len = 16;
25685796c8dcSSimon Schubert 		      dfp_type = builtin_type (gdbarch)->builtin_declong;
25695796c8dcSSimon Schubert 		      sos--;
25705796c8dcSSimon Schubert 		    }
25715796c8dcSSimon Schubert 		  else
25725796c8dcSSimon Schubert 		    {
25735796c8dcSSimon Schubert 		      dfp_len = 8;
25745796c8dcSSimon Schubert 		      dfp_type = builtin_type (gdbarch)->builtin_decdouble;
25755796c8dcSSimon Schubert 		    }
25765796c8dcSSimon Schubert 		}
25775796c8dcSSimon Schubert 
25785796c8dcSSimon Schubert 	      /* Replace %Hf, %Df and %DDf with %s's.  */
25795796c8dcSSimon Schubert 	      *++sos = 's';
25805796c8dcSSimon Schubert 
25815796c8dcSSimon Schubert 	      /* Go through the whole format string and pull the correct
25825796c8dcSSimon Schubert 		 number of chars back to compensate for the change in the
25835796c8dcSSimon Schubert 		 format specifier.  */
25845796c8dcSSimon Schubert 	      while (nnull_chars < nargs - i)
25855796c8dcSSimon Schubert 		{
25865796c8dcSSimon Schubert 		  if (*eos == '\0')
25875796c8dcSSimon Schubert 		    nnull_chars++;
25885796c8dcSSimon Schubert 
25895796c8dcSSimon Schubert 		  *++sos = *++eos;
25905796c8dcSSimon Schubert 		}
25915796c8dcSSimon Schubert 
25925796c8dcSSimon Schubert 	      /* Conversion between different DFP types.  */
25935796c8dcSSimon Schubert 	      if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
25945796c8dcSSimon Schubert 		decimal_convert (param_ptr, param_len, byte_order,
25955796c8dcSSimon Schubert 				 dec, dfp_len, byte_order);
25965796c8dcSSimon Schubert 	      else
25975796c8dcSSimon Schubert 		/* If this is a non-trivial conversion, just output 0.
25985796c8dcSSimon Schubert 		   A correct converted value can be displayed by explicitly
25995796c8dcSSimon Schubert 		   casting to a DFP type.  */
26005796c8dcSSimon Schubert 		decimal_from_string (dec, dfp_len, byte_order, "0");
26015796c8dcSSimon Schubert 
26025796c8dcSSimon Schubert 	      dfp_value = value_from_decfloat (dfp_type, dec);
26035796c8dcSSimon Schubert 
26045796c8dcSSimon Schubert 	      dfp_ptr = (gdb_byte *) value_contents (dfp_value);
26055796c8dcSSimon Schubert 
26065796c8dcSSimon Schubert 	      decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
26075796c8dcSSimon Schubert 
26085796c8dcSSimon Schubert 	      /* Print the DFP value.  */
2609cf7f2e2dSJohn Marino               fprintf_filtered (stream, current_substring, decstr);
26105796c8dcSSimon Schubert 
26115796c8dcSSimon Schubert 	      break;
26125796c8dcSSimon Schubert #endif
26135796c8dcSSimon Schubert 	    }
26145796c8dcSSimon Schubert 
26155796c8dcSSimon Schubert 	  case ptr_arg:
26165796c8dcSSimon Schubert 	    {
26175796c8dcSSimon Schubert 	      /* We avoid the host's %p because pointers are too
26185796c8dcSSimon Schubert 		 likely to be the wrong size.  The only interesting
26195796c8dcSSimon Schubert 		 modifier for %p is a width; extract that, and then
26205796c8dcSSimon Schubert 		 handle %p as glibc would: %#x or a literal "(nil)".  */
26215796c8dcSSimon Schubert 
26225796c8dcSSimon Schubert 	      char *p, *fmt, *fmt_p;
26235796c8dcSSimon Schubert #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
26245796c8dcSSimon Schubert 	      long long val = value_as_long (val_args[i]);
26255796c8dcSSimon Schubert #else
26265796c8dcSSimon Schubert 	      long val = value_as_long (val_args[i]);
26275796c8dcSSimon Schubert #endif
26285796c8dcSSimon Schubert 
26295796c8dcSSimon Schubert 	      fmt = alloca (strlen (current_substring) + 5);
26305796c8dcSSimon Schubert 
26315796c8dcSSimon Schubert 	      /* Copy up to the leading %.  */
26325796c8dcSSimon Schubert 	      p = current_substring;
26335796c8dcSSimon Schubert 	      fmt_p = fmt;
26345796c8dcSSimon Schubert 	      while (*p)
26355796c8dcSSimon Schubert 		{
26365796c8dcSSimon Schubert 		  int is_percent = (*p == '%');
2637cf7f2e2dSJohn Marino 
26385796c8dcSSimon Schubert 		  *fmt_p++ = *p++;
26395796c8dcSSimon Schubert 		  if (is_percent)
26405796c8dcSSimon Schubert 		    {
26415796c8dcSSimon Schubert 		      if (*p == '%')
26425796c8dcSSimon Schubert 			*fmt_p++ = *p++;
26435796c8dcSSimon Schubert 		      else
26445796c8dcSSimon Schubert 			break;
26455796c8dcSSimon Schubert 		    }
26465796c8dcSSimon Schubert 		}
26475796c8dcSSimon Schubert 
26485796c8dcSSimon Schubert 	      if (val != 0)
26495796c8dcSSimon Schubert 		*fmt_p++ = '#';
26505796c8dcSSimon Schubert 
26515796c8dcSSimon Schubert 	      /* Copy any width.  */
26525796c8dcSSimon Schubert 	      while (*p >= '0' && *p < '9')
26535796c8dcSSimon Schubert 		*fmt_p++ = *p++;
26545796c8dcSSimon Schubert 
26555796c8dcSSimon Schubert 	      gdb_assert (*p == 'p' && *(p + 1) == '\0');
26565796c8dcSSimon Schubert 	      if (val != 0)
26575796c8dcSSimon Schubert 		{
26585796c8dcSSimon Schubert #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
26595796c8dcSSimon Schubert 		  *fmt_p++ = 'l';
26605796c8dcSSimon Schubert #endif
26615796c8dcSSimon Schubert 		  *fmt_p++ = 'l';
26625796c8dcSSimon Schubert 		  *fmt_p++ = 'x';
26635796c8dcSSimon Schubert 		  *fmt_p++ = '\0';
2664cf7f2e2dSJohn Marino                   fprintf_filtered (stream, fmt, val);
26655796c8dcSSimon Schubert 		}
26665796c8dcSSimon Schubert 	      else
26675796c8dcSSimon Schubert 		{
26685796c8dcSSimon Schubert 		  *fmt_p++ = 's';
26695796c8dcSSimon Schubert 		  *fmt_p++ = '\0';
2670cf7f2e2dSJohn Marino                   fprintf_filtered (stream, fmt, "(nil)");
26715796c8dcSSimon Schubert 		}
26725796c8dcSSimon Schubert 
26735796c8dcSSimon Schubert 	      break;
26745796c8dcSSimon Schubert 	    }
26755796c8dcSSimon Schubert 	  default:
26765796c8dcSSimon Schubert 	    internal_error (__FILE__, __LINE__,
26775796c8dcSSimon Schubert 			    _("failed internal consistency check"));
26785796c8dcSSimon Schubert 	  }
26795796c8dcSSimon Schubert 	/* Skip to the next substring.  */
26805796c8dcSSimon Schubert 	current_substring += strlen (current_substring) + 1;
26815796c8dcSSimon Schubert       }
2682cf7f2e2dSJohn Marino     /* Print the portion of the format string after the last argument.
2683cf7f2e2dSJohn Marino        Note that this will not include any ordinary %-specs, but it
2684cf7f2e2dSJohn Marino        might include "%%".  That is why we use printf_filtered and not
2685cf7f2e2dSJohn Marino        puts_filtered here.  Also, we pass a dummy argument because
2686cf7f2e2dSJohn Marino        some platforms have modified GCC to include -Wformat-security
2687cf7f2e2dSJohn Marino        by default, which will warn here if there is no argument.  */
2688cf7f2e2dSJohn Marino     fprintf_filtered (stream, last_arg, 0);
26895796c8dcSSimon Schubert   }
26905796c8dcSSimon Schubert   do_cleanups (old_cleanups);
26915796c8dcSSimon Schubert }
26925796c8dcSSimon Schubert 
2693cf7f2e2dSJohn Marino /* Implement the "printf" command.  */
2694cf7f2e2dSJohn Marino 
2695cf7f2e2dSJohn Marino static void
2696cf7f2e2dSJohn Marino printf_command (char *arg, int from_tty)
2697cf7f2e2dSJohn Marino {
2698cf7f2e2dSJohn Marino   ui_printf (arg, gdb_stdout);
2699cf7f2e2dSJohn Marino }
2700cf7f2e2dSJohn Marino 
2701cf7f2e2dSJohn Marino /* Implement the "eval" command.  */
2702cf7f2e2dSJohn Marino 
2703cf7f2e2dSJohn Marino static void
2704cf7f2e2dSJohn Marino eval_command (char *arg, int from_tty)
2705cf7f2e2dSJohn Marino {
2706cf7f2e2dSJohn Marino   struct ui_file *ui_out = mem_fileopen ();
2707cf7f2e2dSJohn Marino   struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
2708cf7f2e2dSJohn Marino   char *expanded;
2709cf7f2e2dSJohn Marino 
2710cf7f2e2dSJohn Marino   ui_printf (arg, ui_out);
2711cf7f2e2dSJohn Marino 
2712cf7f2e2dSJohn Marino   expanded = ui_file_xstrdup (ui_out, NULL);
2713cf7f2e2dSJohn Marino   make_cleanup (xfree, expanded);
2714cf7f2e2dSJohn Marino 
2715cf7f2e2dSJohn Marino   execute_command (expanded, from_tty);
2716cf7f2e2dSJohn Marino 
2717cf7f2e2dSJohn Marino   do_cleanups (cleanups);
2718cf7f2e2dSJohn Marino }
2719cf7f2e2dSJohn Marino 
27205796c8dcSSimon Schubert void
27215796c8dcSSimon Schubert _initialize_printcmd (void)
27225796c8dcSSimon Schubert {
27235796c8dcSSimon Schubert   struct cmd_list_element *c;
27245796c8dcSSimon Schubert 
27255796c8dcSSimon Schubert   current_display_number = -1;
27265796c8dcSSimon Schubert 
27275796c8dcSSimon Schubert   observer_attach_solib_unloaded (clear_dangling_display_expressions);
27285796c8dcSSimon Schubert 
27295796c8dcSSimon Schubert   add_info ("address", address_info,
27305796c8dcSSimon Schubert 	    _("Describe where symbol SYM is stored."));
27315796c8dcSSimon Schubert 
27325796c8dcSSimon Schubert   add_info ("symbol", sym_info, _("\
27335796c8dcSSimon Schubert Describe what symbol is at location ADDR.\n\
27345796c8dcSSimon Schubert Only for symbols with fixed locations (global or static scope)."));
27355796c8dcSSimon Schubert 
27365796c8dcSSimon Schubert   add_com ("x", class_vars, x_command, _("\
27375796c8dcSSimon Schubert Examine memory: x/FMT ADDRESS.\n\
27385796c8dcSSimon Schubert ADDRESS is an expression for the memory address to examine.\n\
27395796c8dcSSimon Schubert FMT is a repeat count followed by a format letter and a size letter.\n\
27405796c8dcSSimon Schubert Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
27415796c8dcSSimon Schubert   t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
27425796c8dcSSimon Schubert Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
27435796c8dcSSimon Schubert The specified number of objects of the specified size are printed\n\
27445796c8dcSSimon Schubert according to the format.\n\n\
27455796c8dcSSimon Schubert Defaults for format and size letters are those previously used.\n\
27465796c8dcSSimon Schubert Default count is 1.  Default address is following last thing printed\n\
27475796c8dcSSimon Schubert with this command or \"print\"."));
27485796c8dcSSimon Schubert 
27495796c8dcSSimon Schubert #if 0
27505796c8dcSSimon Schubert   add_com ("whereis", class_vars, whereis_command,
27515796c8dcSSimon Schubert 	   _("Print line number and file of definition of variable."));
27525796c8dcSSimon Schubert #endif
27535796c8dcSSimon Schubert 
27545796c8dcSSimon Schubert   add_info ("display", display_info, _("\
27555796c8dcSSimon Schubert Expressions to display when program stops, with code numbers."));
27565796c8dcSSimon Schubert 
27575796c8dcSSimon Schubert   add_cmd ("undisplay", class_vars, undisplay_command, _("\
27585796c8dcSSimon Schubert Cancel some expressions to be displayed when program stops.\n\
27595796c8dcSSimon Schubert Arguments are the code numbers of the expressions to stop displaying.\n\
27605796c8dcSSimon Schubert No argument means cancel all automatic-display expressions.\n\
27615796c8dcSSimon Schubert \"delete display\" has the same effect as this command.\n\
27625796c8dcSSimon Schubert Do \"info display\" to see current list of code numbers."),
27635796c8dcSSimon Schubert 	   &cmdlist);
27645796c8dcSSimon Schubert 
27655796c8dcSSimon Schubert   add_com ("display", class_vars, display_command, _("\
27665796c8dcSSimon Schubert Print value of expression EXP each time the program stops.\n\
27675796c8dcSSimon Schubert /FMT may be used before EXP as in the \"print\" command.\n\
27685796c8dcSSimon Schubert /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
27695796c8dcSSimon Schubert as in the \"x\" command, and then EXP is used to get the address to examine\n\
27705796c8dcSSimon Schubert and examining is done as in the \"x\" command.\n\n\
27715796c8dcSSimon Schubert With no argument, display all currently requested auto-display expressions.\n\
27725796c8dcSSimon Schubert Use \"undisplay\" to cancel display requests previously made."));
27735796c8dcSSimon Schubert 
2774c50c785cSJohn Marino   add_cmd ("display", class_vars, enable_display_command, _("\
27755796c8dcSSimon Schubert Enable some expressions to be displayed when program stops.\n\
27765796c8dcSSimon Schubert Arguments are the code numbers of the expressions to resume displaying.\n\
27775796c8dcSSimon Schubert No argument means enable all automatic-display expressions.\n\
27785796c8dcSSimon Schubert Do \"info display\" to see current list of code numbers."), &enablelist);
27795796c8dcSSimon Schubert 
27805796c8dcSSimon Schubert   add_cmd ("display", class_vars, disable_display_command, _("\
27815796c8dcSSimon Schubert Disable some expressions to be displayed when program stops.\n\
27825796c8dcSSimon Schubert Arguments are the code numbers of the expressions to stop displaying.\n\
27835796c8dcSSimon Schubert No argument means disable all automatic-display expressions.\n\
27845796c8dcSSimon Schubert Do \"info display\" to see current list of code numbers."), &disablelist);
27855796c8dcSSimon Schubert 
27865796c8dcSSimon Schubert   add_cmd ("display", class_vars, undisplay_command, _("\
27875796c8dcSSimon Schubert Cancel some expressions to be displayed when program stops.\n\
27885796c8dcSSimon Schubert Arguments are the code numbers of the expressions to stop displaying.\n\
27895796c8dcSSimon Schubert No argument means cancel all automatic-display expressions.\n\
27905796c8dcSSimon Schubert Do \"info display\" to see current list of code numbers."), &deletelist);
27915796c8dcSSimon Schubert 
27925796c8dcSSimon Schubert   add_com ("printf", class_vars, printf_command, _("\
27935796c8dcSSimon Schubert printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
27945796c8dcSSimon Schubert This is useful for formatted output in user-defined commands."));
27955796c8dcSSimon Schubert 
27965796c8dcSSimon Schubert   add_com ("output", class_vars, output_command, _("\
27975796c8dcSSimon Schubert Like \"print\" but don't put in value history and don't print newline.\n\
27985796c8dcSSimon Schubert This is useful in user-defined commands."));
27995796c8dcSSimon Schubert 
28005796c8dcSSimon Schubert   add_prefix_cmd ("set", class_vars, set_command, _("\
28015796c8dcSSimon Schubert Evaluate expression EXP and assign result to variable VAR, using assignment\n\
28025796c8dcSSimon Schubert syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
28035796c8dcSSimon Schubert example).  VAR may be a debugger \"convenience\" variable (names starting\n\
28045796c8dcSSimon Schubert with $), a register (a few standard names starting with $), or an actual\n\
28055796c8dcSSimon Schubert variable in the program being debugged.  EXP is any valid expression.\n\
28065796c8dcSSimon Schubert Use \"set variable\" for variables with names identical to set subcommands.\n\
28075796c8dcSSimon Schubert \n\
28085796c8dcSSimon Schubert With a subcommand, this command modifies parts of the gdb environment.\n\
28095796c8dcSSimon Schubert You can see these environment settings with the \"show\" command."),
28105796c8dcSSimon Schubert 		  &setlist, "set ", 1, &cmdlist);
28115796c8dcSSimon Schubert   if (dbx_commands)
28125796c8dcSSimon Schubert     add_com ("assign", class_vars, set_command, _("\
28135796c8dcSSimon Schubert Evaluate expression EXP and assign result to variable VAR, using assignment\n\
28145796c8dcSSimon Schubert syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
28155796c8dcSSimon Schubert example).  VAR may be a debugger \"convenience\" variable (names starting\n\
28165796c8dcSSimon Schubert with $), a register (a few standard names starting with $), or an actual\n\
28175796c8dcSSimon Schubert variable in the program being debugged.  EXP is any valid expression.\n\
28185796c8dcSSimon Schubert Use \"set variable\" for variables with names identical to set subcommands.\n\
28195796c8dcSSimon Schubert \nWith a subcommand, this command modifies parts of the gdb environment.\n\
28205796c8dcSSimon Schubert You can see these environment settings with the \"show\" command."));
28215796c8dcSSimon Schubert 
28225796c8dcSSimon Schubert   /* "call" is the same as "set", but handy for dbx users to call fns.  */
28235796c8dcSSimon Schubert   c = add_com ("call", class_vars, call_command, _("\
28245796c8dcSSimon Schubert Call a function in the program.\n\
28255796c8dcSSimon Schubert The argument is the function name and arguments, in the notation of the\n\
28265796c8dcSSimon Schubert current working language.  The result is printed and saved in the value\n\
28275796c8dcSSimon Schubert history, if it is not void."));
28285796c8dcSSimon Schubert   set_cmd_completer (c, expression_completer);
28295796c8dcSSimon Schubert 
28305796c8dcSSimon Schubert   add_cmd ("variable", class_vars, set_command, _("\
28315796c8dcSSimon Schubert Evaluate expression EXP and assign result to variable VAR, using assignment\n\
28325796c8dcSSimon Schubert syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
28335796c8dcSSimon Schubert example).  VAR may be a debugger \"convenience\" variable (names starting\n\
28345796c8dcSSimon Schubert with $), a register (a few standard names starting with $), or an actual\n\
28355796c8dcSSimon Schubert variable in the program being debugged.  EXP is any valid expression.\n\
28365796c8dcSSimon Schubert This may usually be abbreviated to simply \"set\"."),
28375796c8dcSSimon Schubert 	   &setlist);
28385796c8dcSSimon Schubert 
28395796c8dcSSimon Schubert   c = add_com ("print", class_vars, print_command, _("\
28405796c8dcSSimon Schubert Print value of expression EXP.\n\
28415796c8dcSSimon Schubert Variables accessible are those of the lexical environment of the selected\n\
28425796c8dcSSimon Schubert stack frame, plus all those whose scope is global or an entire file.\n\
28435796c8dcSSimon Schubert \n\
28445796c8dcSSimon Schubert $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
28455796c8dcSSimon Schubert $$NUM refers to NUM'th value back from the last one.\n\
28465796c8dcSSimon Schubert Names starting with $ refer to registers (with the values they would have\n\
28475796c8dcSSimon Schubert if the program were to return to the stack frame now selected, restoring\n\
28485796c8dcSSimon Schubert all registers saved by frames farther in) or else to debugger\n\
28495796c8dcSSimon Schubert \"convenience\" variables (any such name not a known register).\n\
28505796c8dcSSimon Schubert Use assignment expressions to give values to convenience variables.\n\
28515796c8dcSSimon Schubert \n\
28525796c8dcSSimon Schubert {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
28535796c8dcSSimon Schubert @ is a binary operator for treating consecutive data objects\n\
28545796c8dcSSimon Schubert anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
28555796c8dcSSimon Schubert element is FOO, whose second element is stored in the space following\n\
28565796c8dcSSimon Schubert where FOO is stored, etc.  FOO must be an expression whose value\n\
28575796c8dcSSimon Schubert resides in memory.\n\
28585796c8dcSSimon Schubert \n\
28595796c8dcSSimon Schubert EXP may be preceded with /FMT, where FMT is a format letter\n\
28605796c8dcSSimon Schubert but no count or size letter (see \"x\" command)."));
28615796c8dcSSimon Schubert   set_cmd_completer (c, expression_completer);
28625796c8dcSSimon Schubert   add_com_alias ("p", "print", class_vars, 1);
28635796c8dcSSimon Schubert 
28645796c8dcSSimon Schubert   c = add_com ("inspect", class_vars, inspect_command, _("\
28655796c8dcSSimon Schubert Same as \"print\" command, except that if you are running in the epoch\n\
28665796c8dcSSimon Schubert environment, the value is printed in its own window."));
28675796c8dcSSimon Schubert   set_cmd_completer (c, expression_completer);
28685796c8dcSSimon Schubert 
28695796c8dcSSimon Schubert   add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
28705796c8dcSSimon Schubert 			    &max_symbolic_offset, _("\
28715796c8dcSSimon Schubert Set the largest offset that will be printed in <symbol+1234> form."), _("\
28725796c8dcSSimon Schubert Show the largest offset that will be printed in <symbol+1234> form."), NULL,
28735796c8dcSSimon Schubert 			    NULL,
28745796c8dcSSimon Schubert 			    show_max_symbolic_offset,
28755796c8dcSSimon Schubert 			    &setprintlist, &showprintlist);
28765796c8dcSSimon Schubert   add_setshow_boolean_cmd ("symbol-filename", no_class,
28775796c8dcSSimon Schubert 			   &print_symbol_filename, _("\
28785796c8dcSSimon Schubert Set printing of source filename and line number with <symbol>."), _("\
28795796c8dcSSimon Schubert Show printing of source filename and line number with <symbol>."), NULL,
28805796c8dcSSimon Schubert 			   NULL,
28815796c8dcSSimon Schubert 			   show_print_symbol_filename,
28825796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
2883cf7f2e2dSJohn Marino 
2884cf7f2e2dSJohn Marino   add_com ("eval", no_class, eval_command, _("\
2885cf7f2e2dSJohn Marino Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2886cf7f2e2dSJohn Marino a command line, and call it."));
28875796c8dcSSimon Schubert }
2888