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