119370Spst/* Print values for GNU debugger GDB.
219370Spst
398944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130803Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
598944Sobrien   Foundation, Inc.
619370Spst
798944Sobrien   This file is part of GDB.
819370Spst
998944Sobrien   This program is free software; you can redistribute it and/or modify
1098944Sobrien   it under the terms of the GNU General Public License as published by
1198944Sobrien   the Free Software Foundation; either version 2 of the License, or
1298944Sobrien   (at your option) any later version.
1319370Spst
1498944Sobrien   This program is distributed in the hope that it will be useful,
1598944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798944Sobrien   GNU General Public License for more details.
1819370Spst
1998944Sobrien   You should have received a copy of the GNU General Public License
2098944Sobrien   along with this program; if not, write to the Free Software
2198944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298944Sobrien   Boston, MA 02111-1307, USA.  */
2398944Sobrien
2419370Spst#include "defs.h"
2519370Spst#include "gdb_string.h"
2619370Spst#include "frame.h"
2719370Spst#include "symtab.h"
2819370Spst#include "gdbtypes.h"
2919370Spst#include "value.h"
3019370Spst#include "language.h"
3119370Spst#include "expression.h"
3219370Spst#include "gdbcore.h"
3319370Spst#include "gdbcmd.h"
3419370Spst#include "target.h"
3519370Spst#include "breakpoint.h"
3619370Spst#include "demangle.h"
3719370Spst#include "valprint.h"
3819370Spst#include "annotate.h"
3998944Sobrien#include "symfile.h"		/* for overlay functions */
4098944Sobrien#include "objfiles.h"		/* ditto */
4198944Sobrien#include "completer.h"		/* for completion functions */
4298944Sobrien#include "ui-out.h"
43130803Smarcel#include "gdb_assert.h"
44130803Smarcel#include "block.h"
45130803Smarcel#include "disasm.h"
4619370Spst
47130803Smarcel#ifdef TUI
48130803Smarcel#include "tui/tui.h"		/* For tui_active et.al.   */
49130803Smarcel#endif
50130803Smarcel
5119370Spstextern int asm_demangle;	/* Whether to demangle syms in asm printouts */
5219370Spstextern int addressprint;	/* Whether to print hex addresses in HLL " */
5319370Spst
5419370Spststruct format_data
5598944Sobrien  {
5698944Sobrien    int count;
5798944Sobrien    char format;
5898944Sobrien    char size;
5998944Sobrien  };
6019370Spst
6119370Spst/* Last specified output format.  */
6219370Spst
6319370Spststatic char last_format = 'x';
6419370Spst
6519370Spst/* Last specified examination size.  'b', 'h', 'w' or `q'.  */
6619370Spst
6719370Spststatic char last_size = 'w';
6819370Spst
6919370Spst/* Default address to examine next.  */
7019370Spst
7119370Spststatic CORE_ADDR next_address;
7219370Spst
7346283Sdfr/* Default section to examine next. */
7446283Sdfr
7546283Sdfrstatic asection *next_section;
7646283Sdfr
7719370Spst/* Last address examined.  */
7819370Spst
7919370Spststatic CORE_ADDR last_examine_address;
8019370Spst
8119370Spst/* Contents of last address examined.
8219370Spst   This is not valid past the end of the `x' command!  */
8319370Spst
8498944Sobrienstatic struct value *last_examine_value;
8519370Spst
8619370Spst/* Largest offset between a symbolic value and an address, that will be
8719370Spst   printed as `0x1234 <symbol+offset>'.  */
8819370Spst
8919370Spststatic unsigned int max_symbolic_offset = UINT_MAX;
9019370Spst
9119370Spst/* Append the source filename and linenumber of the symbol when
9219370Spst   printing a symbolic value as `<symbol at filename:linenum>' if set.  */
9319370Spststatic int print_symbol_filename = 0;
9419370Spst
9519370Spst/* Number of auto-display expression currently being displayed.
9619370Spst   So that we can disable it if we get an error or a signal within it.
9719370Spst   -1 when not doing one.  */
9819370Spst
9919370Spstint current_display_number;
10019370Spst
10119370Spst/* Flag to low-level print routines that this value is being printed
10219370Spst   in an epoch window.  We'd like to pass this as a parameter, but
10319370Spst   every routine would need to take it.  Perhaps we can encapsulate
10419370Spst   this in the I/O stream once we have GNU stdio. */
10519370Spst
10619370Spstint inspect_it = 0;
10719370Spst
10819370Spststruct display
10998944Sobrien  {
11098944Sobrien    /* Chain link to next auto-display item.  */
11198944Sobrien    struct display *next;
11298944Sobrien    /* Expression to be evaluated and displayed.  */
11398944Sobrien    struct expression *exp;
11498944Sobrien    /* Item number of this auto-display item.  */
11598944Sobrien    int number;
11698944Sobrien    /* Display format specified.  */
11798944Sobrien    struct format_data format;
11898944Sobrien    /* Innermost block required by this expression when evaluated */
11998944Sobrien    struct block *block;
12098944Sobrien    /* Status of this display (enabled or disabled) */
12198944Sobrien    int enabled_p;
12298944Sobrien  };
12319370Spst
12419370Spst/* Chain of expressions whose values should be displayed
12519370Spst   automatically each time the program stops.  */
12619370Spst
12719370Spststatic struct display *display_chain;
12819370Spst
12919370Spststatic int display_number;
13019370Spst
13146283Sdfr/* Prototypes for exported functions. */
13219370Spst
13398944Sobrienvoid output_command (char *, int);
13419370Spst
13598944Sobrienvoid _initialize_printcmd (void);
13619370Spst
13746283Sdfr/* Prototypes for local functions. */
13846283Sdfr
13998944Sobrienstatic void delete_display (int);
14019370Spst
14198944Sobrienstatic void enable_display (char *, int);
14219370Spst
14398944Sobrienstatic void disable_display_command (char *, int);
14419370Spst
14598944Sobrienstatic void printf_command (char *, int);
14619370Spst
14798944Sobrienstatic void display_info (char *, int);
14819370Spst
14998944Sobrienstatic void do_one_display (struct display *);
15019370Spst
15198944Sobrienstatic void undisplay_command (char *, int);
15219370Spst
15398944Sobrienstatic void free_display (struct display *);
15419370Spst
15598944Sobrienstatic void display_command (char *, int);
15619370Spst
15798944Sobrienvoid x_command (char *, int);
15819370Spst
15998944Sobrienstatic void address_info (char *, int);
16019370Spst
16198944Sobrienstatic void set_command (char *, int);
16219370Spst
16398944Sobrienstatic void call_command (char *, int);
16419370Spst
16598944Sobrienstatic void inspect_command (char *, int);
16619370Spst
16798944Sobrienstatic void print_command (char *, int);
16819370Spst
16998944Sobrienstatic void print_command_1 (char *, int, int);
17019370Spst
17198944Sobrienstatic void validate_format (struct format_data, char *);
17219370Spst
17398944Sobrienstatic void do_examine (struct format_data, CORE_ADDR addr,
17498944Sobrien			asection * section);
17519370Spst
17698944Sobrienstatic void print_formatted (struct value *, int, int, struct ui_file *);
17719370Spst
17898944Sobrienstatic struct format_data decode_format (char **, int, int);
17919370Spst
18098944Sobrienstatic void sym_info (char *, int);
18198944Sobrien
18246283Sdfr
18319370Spst/* Decode a format specification.  *STRING_PTR should point to it.
18419370Spst   OFORMAT and OSIZE are used as defaults for the format and size
18519370Spst   if none are given in the format specification.
18619370Spst   If OSIZE is zero, then the size field of the returned value
18719370Spst   should be set only if a size is explicitly specified by the
18819370Spst   user.
18919370Spst   The structure returned describes all the data
19019370Spst   found in the specification.  In addition, *STRING_PTR is advanced
19119370Spst   past the specification and past all whitespace following it.  */
19219370Spst
19319370Spststatic struct format_data
19498944Sobriendecode_format (char **string_ptr, int oformat, int osize)
19519370Spst{
19619370Spst  struct format_data val;
197130803Smarcel  char *p = *string_ptr;
19819370Spst
19919370Spst  val.format = '?';
20019370Spst  val.size = '?';
20119370Spst  val.count = 1;
20219370Spst
20319370Spst  if (*p >= '0' && *p <= '9')
20419370Spst    val.count = atoi (p);
20598944Sobrien  while (*p >= '0' && *p <= '9')
20698944Sobrien    p++;
20719370Spst
20819370Spst  /* Now process size or format letters that follow.  */
20919370Spst
21019370Spst  while (1)
21119370Spst    {
21219370Spst      if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
21319370Spst	val.size = *p++;
21419370Spst      else if (*p >= 'a' && *p <= 'z')
21519370Spst	val.format = *p++;
21619370Spst      else
21719370Spst	break;
21819370Spst    }
21919370Spst
22098944Sobrien  while (*p == ' ' || *p == '\t')
22198944Sobrien    p++;
22219370Spst  *string_ptr = p;
22319370Spst
22419370Spst  /* Set defaults for format and size if not specified.  */
22519370Spst  if (val.format == '?')
22619370Spst    {
22719370Spst      if (val.size == '?')
22819370Spst	{
22919370Spst	  /* Neither has been specified.  */
23019370Spst	  val.format = oformat;
23119370Spst	  val.size = osize;
23219370Spst	}
23319370Spst      else
23419370Spst	/* If a size is specified, any format makes a reasonable
23519370Spst	   default except 'i'.  */
23619370Spst	val.format = oformat == 'i' ? 'x' : oformat;
23719370Spst    }
23819370Spst  else if (val.size == '?')
23919370Spst    switch (val.format)
24019370Spst      {
24119370Spst      case 'a':
24219370Spst      case 's':
24319370Spst	/* Pick the appropriate size for an address.  */
24419370Spst	if (TARGET_PTR_BIT == 64)
24519370Spst	  val.size = osize ? 'g' : osize;
24619370Spst	else if (TARGET_PTR_BIT == 32)
24719370Spst	  val.size = osize ? 'w' : osize;
24819370Spst	else if (TARGET_PTR_BIT == 16)
24919370Spst	  val.size = osize ? 'h' : osize;
25019370Spst	else
25119370Spst	  /* Bad value for TARGET_PTR_BIT */
25298944Sobrien	  internal_error (__FILE__, __LINE__, "failed internal consistency check");
25319370Spst	break;
25419370Spst      case 'f':
25519370Spst	/* Floating point has to be word or giantword.  */
25619370Spst	if (osize == 'w' || osize == 'g')
25719370Spst	  val.size = osize;
25819370Spst	else
25919370Spst	  /* Default it to giantword if the last used size is not
26019370Spst	     appropriate.  */
26119370Spst	  val.size = osize ? 'g' : osize;
26219370Spst	break;
26319370Spst      case 'c':
26419370Spst	/* Characters default to one byte.  */
26519370Spst	val.size = osize ? 'b' : osize;
26619370Spst	break;
26719370Spst      default:
26819370Spst	/* The default is the size most recently specified.  */
26919370Spst	val.size = osize;
27019370Spst      }
27119370Spst
27219370Spst  return val;
27319370Spst}
27419370Spst
27598944Sobrien/* Print value VAL on stream according to FORMAT, a letter or 0.
27619370Spst   Do not end with a newline.
27719370Spst   0 means print VAL according to its own type.
27819370Spst   SIZE is the letter for the size of datum being printed.
27919370Spst   This is used to pad hex numbers so they line up.  */
28019370Spst
28119370Spststatic void
282130803Smarcelprint_formatted (struct value *val, int format, int size,
28398944Sobrien		 struct ui_file *stream)
28419370Spst{
28519370Spst  struct type *type = check_typedef (VALUE_TYPE (val));
28619370Spst  int len = TYPE_LENGTH (type);
28719370Spst
28819370Spst  if (VALUE_LVAL (val) == lval_memory)
28946283Sdfr    {
29046283Sdfr      next_address = VALUE_ADDRESS (val) + len;
29146283Sdfr      next_section = VALUE_BFD_SECTION (val);
29246283Sdfr    }
29319370Spst
29419370Spst  switch (format)
29519370Spst    {
29619370Spst    case 's':
29746283Sdfr      /* FIXME: Need to handle wchar_t's here... */
29819370Spst      next_address = VALUE_ADDRESS (val)
29998944Sobrien	+ val_print_string (VALUE_ADDRESS (val), -1, 1, stream);
30046283Sdfr      next_section = VALUE_BFD_SECTION (val);
30119370Spst      break;
30219370Spst
30319370Spst    case 'i':
30419370Spst      /* The old comment says
30598944Sobrien         "Force output out, print_insn not using _filtered".
30698944Sobrien         I'm not completely sure what that means, I suspect most print_insn
30798944Sobrien         now do use _filtered, so I guess it's obsolete.
30898944Sobrien         --Yes, it does filter now, and so this is obsolete.  -JB  */
30946283Sdfr
31019370Spst      /* We often wrap here if there are long symbolic names.  */
31119370Spst      wrap_here ("    ");
31219370Spst      next_address = VALUE_ADDRESS (val)
313130803Smarcel	+ gdb_print_insn (VALUE_ADDRESS (val), stream);
31446283Sdfr      next_section = VALUE_BFD_SECTION (val);
31519370Spst      break;
31619370Spst
31719370Spst    default:
31819370Spst      if (format == 0
31919370Spst	  || TYPE_CODE (type) == TYPE_CODE_ARRAY
32019370Spst	  || TYPE_CODE (type) == TYPE_CODE_STRING
32119370Spst	  || TYPE_CODE (type) == TYPE_CODE_STRUCT
322130803Smarcel	  || TYPE_CODE (type) == TYPE_CODE_UNION
323130803Smarcel	  || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
32498944Sobrien	/* If format is 0, use the 'natural' format for
32598944Sobrien	 * that type of value.  If the type is non-scalar,
32698944Sobrien	 * we have to use language rules to print it as
32798944Sobrien	 * a series of scalars.
32898944Sobrien	 */
32998944Sobrien	value_print (val, stream, format, Val_pretty_default);
33019370Spst      else
33198944Sobrien	/* User specified format, so don't look to the
33298944Sobrien	 * the type to tell us what to do.
33398944Sobrien	 */
33419370Spst	print_scalar_formatted (VALUE_CONTENTS (val), type,
33598944Sobrien				format, size, stream);
33619370Spst    }
33719370Spst}
33819370Spst
33919370Spst/* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
34019370Spst   according to letters FORMAT and SIZE on STREAM.
34119370Spst   FORMAT may not be zero.  Formats s and i are not supported at this level.
34219370Spst
34319370Spst   This is how the elements of an array or structure are printed
34419370Spst   with a format.  */
34519370Spst
34619370Spstvoid
347130803Smarcelprint_scalar_formatted (void *valaddr, struct type *type, int format, int size,
34898944Sobrien			struct ui_file *stream)
34919370Spst{
350130803Smarcel  LONGEST val_long = 0;
35119370Spst  unsigned int len = TYPE_LENGTH (type);
35219370Spst
353130803Smarcel  if (len > sizeof(LONGEST) &&
354130803Smarcel      (TYPE_CODE (type) == TYPE_CODE_INT
355130803Smarcel       || TYPE_CODE (type) == TYPE_CODE_ENUM))
35619370Spst    {
357130803Smarcel      switch (format)
35819370Spst	{
359130803Smarcel	case 'o':
360130803Smarcel	  print_octal_chars (stream, valaddr, len);
36119370Spst	  return;
362130803Smarcel	case 'u':
363130803Smarcel	case 'd':
364130803Smarcel	  print_decimal_chars (stream, valaddr, len);
365130803Smarcel	  return;
366130803Smarcel	case 't':
367130803Smarcel	  print_binary_chars (stream, valaddr, len);
368130803Smarcel	  return;
369130803Smarcel	case 'x':
370130803Smarcel	  print_hex_chars (stream, valaddr, len);
371130803Smarcel	  return;
372130803Smarcel	case 'c':
373130803Smarcel	  print_char_chars (stream, valaddr, len);
374130803Smarcel	  return;
375130803Smarcel	default:
376130803Smarcel	  break;
377130803Smarcel	};
378130803Smarcel    }
37919370Spst
380130803Smarcel  if (format != 'f')
38119370Spst    val_long = unpack_long (type, valaddr);
38219370Spst
38398944Sobrien  /* If the value is a pointer, and pointers and addresses are not the
384130803Smarcel     same, then at this point, the value's length (in target bytes) is
385130803Smarcel     TARGET_ADDR_BIT/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
38698944Sobrien  if (TYPE_CODE (type) == TYPE_CODE_PTR)
387130803Smarcel    len = TARGET_ADDR_BIT / TARGET_CHAR_BIT;
38898944Sobrien
38919370Spst  /* If we are printing it as unsigned, truncate it in case it is actually
39019370Spst     a negative signed value (e.g. "print/u (short)-1" should print 65535
39119370Spst     (if shorts are 16 bits) instead of 4294967295).  */
39219370Spst  if (format != 'd')
39319370Spst    {
39419370Spst      if (len < sizeof (LONGEST))
39519370Spst	val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
39619370Spst    }
39719370Spst
39819370Spst  switch (format)
39919370Spst    {
40019370Spst    case 'x':
40119370Spst      if (!size)
40219370Spst	{
40319370Spst	  /* no size specified, like in print.  Print varying # of digits. */
40419370Spst	  print_longest (stream, 'x', 1, val_long);
40519370Spst	}
40619370Spst      else
40719370Spst	switch (size)
40819370Spst	  {
40919370Spst	  case 'b':
41019370Spst	  case 'h':
41119370Spst	  case 'w':
41219370Spst	  case 'g':
41319370Spst	    print_longest (stream, size, 1, val_long);
41419370Spst	    break;
41519370Spst	  default:
41619370Spst	    error ("Undefined output size \"%c\".", size);
41719370Spst	  }
41819370Spst      break;
41919370Spst
42019370Spst    case 'd':
42119370Spst      print_longest (stream, 'd', 1, val_long);
42219370Spst      break;
42319370Spst
42419370Spst    case 'u':
42519370Spst      print_longest (stream, 'u', 0, val_long);
42619370Spst      break;
42719370Spst
42819370Spst    case 'o':
42919370Spst      if (val_long)
43019370Spst	print_longest (stream, 'o', 1, val_long);
43119370Spst      else
43219370Spst	fprintf_filtered (stream, "0");
43319370Spst      break;
43419370Spst
43519370Spst    case 'a':
43698944Sobrien      {
43798944Sobrien	CORE_ADDR addr = unpack_pointer (type, valaddr);
43898944Sobrien	print_address (addr, stream);
43998944Sobrien      }
44019370Spst      break;
44119370Spst
44219370Spst    case 'c':
44398944Sobrien      value_print (value_from_longest (builtin_type_true_char, val_long),
44498944Sobrien		   stream, 0, Val_pretty_default);
44519370Spst      break;
44619370Spst
44719370Spst    case 'f':
44898944Sobrien      if (len == TYPE_LENGTH (builtin_type_float))
44998944Sobrien        type = builtin_type_float;
45098944Sobrien      else if (len == TYPE_LENGTH (builtin_type_double))
45198944Sobrien        type = builtin_type_double;
45298944Sobrien      else if (len == TYPE_LENGTH (builtin_type_long_double))
45398944Sobrien        type = builtin_type_long_double;
45419370Spst      print_floating (valaddr, type, stream);
45519370Spst      break;
45619370Spst
45719370Spst    case 0:
45898944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
45919370Spst
46019370Spst    case 't':
46119370Spst      /* Binary; 't' stands for "two".  */
46219370Spst      {
46398944Sobrien	char bits[8 * (sizeof val_long) + 1];
46498944Sobrien	char buf[8 * (sizeof val_long) + 32];
46519370Spst	char *cp = bits;
46619370Spst	int width;
46719370Spst
46898944Sobrien	if (!size)
46998944Sobrien	  width = 8 * (sizeof val_long);
47098944Sobrien	else
47198944Sobrien	  switch (size)
47219370Spst	    {
47319370Spst	    case 'b':
47419370Spst	      width = 8;
47519370Spst	      break;
47619370Spst	    case 'h':
47719370Spst	      width = 16;
47819370Spst	      break;
47919370Spst	    case 'w':
48019370Spst	      width = 32;
48119370Spst	      break;
48219370Spst	    case 'g':
48319370Spst	      width = 64;
48419370Spst	      break;
48519370Spst	    default:
48619370Spst	      error ("Undefined output size \"%c\".", size);
48719370Spst	    }
48819370Spst
48998944Sobrien	bits[width] = '\0';
49098944Sobrien	while (width-- > 0)
49198944Sobrien	  {
49298944Sobrien	    bits[width] = (val_long & 1) ? '1' : '0';
49398944Sobrien	    val_long >>= 1;
49498944Sobrien	  }
49519370Spst	if (!size)
49619370Spst	  {
49719370Spst	    while (*cp && *cp == '0')
49819370Spst	      cp++;
49919370Spst	    if (*cp == '\0')
50019370Spst	      cp--;
50119370Spst	  }
50298944Sobrien	strcpy (buf, local_binary_format_prefix ());
50346283Sdfr	strcat (buf, cp);
50498944Sobrien	strcat (buf, local_binary_format_suffix ());
505130803Smarcel	fputs_filtered (buf, stream);
50619370Spst      }
50719370Spst      break;
50819370Spst
50919370Spst    default:
51019370Spst      error ("Undefined output format \"%c\".", format);
51119370Spst    }
51219370Spst}
51319370Spst
51419370Spst/* Specify default address for `x' command.
51519370Spst   `info lines' uses this.  */
51619370Spst
51719370Spstvoid
51898944Sobrienset_next_address (CORE_ADDR addr)
51919370Spst{
52019370Spst  next_address = addr;
52119370Spst
52219370Spst  /* Make address available to the user as $_.  */
52319370Spst  set_internalvar (lookup_internalvar ("_"),
52498944Sobrien		   value_from_pointer (lookup_pointer_type (builtin_type_void),
52598944Sobrien				       addr));
52619370Spst}
52719370Spst
52819370Spst/* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
52919370Spst   after LEADIN.  Print nothing if no symbolic name is found nearby.
53019370Spst   Optionally also print source file and line number, if available.
53119370Spst   DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
53219370Spst   or to interpret it as a possible C++ name and convert it back to source
53319370Spst   form.  However note that DO_DEMANGLE can be overridden by the specific
53419370Spst   settings of the demangle and asm_demangle variables.  */
53519370Spst
53619370Spstvoid
53798944Sobrienprint_address_symbolic (CORE_ADDR addr, struct ui_file *stream, int do_demangle,
53898944Sobrien			char *leadin)
53919370Spst{
54098944Sobrien  char *name = NULL;
54198944Sobrien  char *filename = NULL;
54298944Sobrien  int unmapped = 0;
54398944Sobrien  int offset = 0;
54498944Sobrien  int line = 0;
54598944Sobrien
54698944Sobrien  /* throw away both name and filename */
54798944Sobrien  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
54898944Sobrien  make_cleanup (free_current_contents, &filename);
54998944Sobrien
55098944Sobrien  if (build_address_symbolic (addr, do_demangle, &name, &offset, &filename, &line, &unmapped))
55198944Sobrien    {
55298944Sobrien      do_cleanups (cleanup_chain);
55398944Sobrien      return;
55498944Sobrien    }
55598944Sobrien
55698944Sobrien  fputs_filtered (leadin, stream);
55798944Sobrien  if (unmapped)
55898944Sobrien    fputs_filtered ("<*", stream);
55998944Sobrien  else
56098944Sobrien    fputs_filtered ("<", stream);
56198944Sobrien  fputs_filtered (name, stream);
56298944Sobrien  if (offset != 0)
56398944Sobrien    fprintf_filtered (stream, "+%u", (unsigned int) offset);
56498944Sobrien
56598944Sobrien  /* Append source filename and line number if desired.  Give specific
56698944Sobrien     line # of this addr, if we have it; else line # of the nearest symbol.  */
56798944Sobrien  if (print_symbol_filename && filename != NULL)
56898944Sobrien    {
56998944Sobrien      if (line != -1)
57098944Sobrien	fprintf_filtered (stream, " at %s:%d", filename, line);
57198944Sobrien      else
57298944Sobrien	fprintf_filtered (stream, " in %s", filename);
57398944Sobrien    }
57498944Sobrien  if (unmapped)
57598944Sobrien    fputs_filtered ("*>", stream);
57698944Sobrien  else
57798944Sobrien    fputs_filtered (">", stream);
57898944Sobrien
57998944Sobrien  do_cleanups (cleanup_chain);
58098944Sobrien}
58198944Sobrien
58298944Sobrien/* Given an address ADDR return all the elements needed to print the
58398944Sobrien   address in a symbolic form. NAME can be mangled or not depending
58498944Sobrien   on DO_DEMANGLE (and also on the asm_demangle global variable,
58598944Sobrien   manipulated via ''set print asm-demangle''). Return 0 in case of
58698944Sobrien   success, when all the info in the OUT paramters is valid. Return 1
58798944Sobrien   otherwise. */
58898944Sobrienint
58998944Sobrienbuild_address_symbolic (CORE_ADDR addr,  /* IN */
59098944Sobrien			int do_demangle, /* IN */
59198944Sobrien			char **name,     /* OUT */
59298944Sobrien			int *offset,     /* OUT */
59398944Sobrien			char **filename, /* OUT */
59498944Sobrien			int *line,       /* OUT */
59598944Sobrien			int *unmapped)   /* OUT */
59698944Sobrien{
59719370Spst  struct minimal_symbol *msymbol;
59819370Spst  struct symbol *symbol;
59919370Spst  struct symtab *symtab = 0;
60019370Spst  CORE_ADDR name_location = 0;
60146283Sdfr  asection *section = 0;
60298944Sobrien  char *name_temp = "";
60398944Sobrien
60498944Sobrien  /* Let's say it is unmapped. */
60598944Sobrien  *unmapped = 0;
60619370Spst
60798944Sobrien  /* Determine if the address is in an overlay, and whether it is
60898944Sobrien     mapped. */
60946283Sdfr  if (overlay_debugging)
61046283Sdfr    {
61146283Sdfr      section = find_pc_overlay (addr);
61246283Sdfr      if (pc_in_unmapped_range (addr, section))
61346283Sdfr	{
61498944Sobrien	  *unmapped = 1;
61546283Sdfr	  addr = overlay_mapped_address (addr, section);
61646283Sdfr	}
61746283Sdfr    }
61846283Sdfr
61919370Spst  /* First try to find the address in the symbol table, then
62019370Spst     in the minsyms.  Take the closest one.  */
62119370Spst
62219370Spst  /* This is defective in the sense that it only finds text symbols.  So
62319370Spst     really this is kind of pointless--we should make sure that the
62419370Spst     minimal symbols have everything we need (by changing that we could
62519370Spst     save some memory, but for many debug format--ELF/DWARF or
62619370Spst     anything/stabs--it would be inconvenient to eliminate those minimal
62719370Spst     symbols anyway).  */
62846283Sdfr  msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
62946283Sdfr  symbol = find_pc_sect_function (addr, section);
63019370Spst
63119370Spst  if (symbol)
63219370Spst    {
63346283Sdfr      name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
634130803Smarcel      if (do_demangle || asm_demangle)
635130803Smarcel	name_temp = SYMBOL_PRINT_NAME (symbol);
63619370Spst      else
637130803Smarcel	name_temp = DEPRECATED_SYMBOL_NAME (symbol);
63819370Spst    }
63919370Spst
64019370Spst  if (msymbol != NULL)
64119370Spst    {
64219370Spst      if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
64319370Spst	{
64419370Spst	  /* The msymbol is closer to the address than the symbol;
64519370Spst	     use the msymbol instead.  */
64619370Spst	  symbol = 0;
64719370Spst	  symtab = 0;
64819370Spst	  name_location = SYMBOL_VALUE_ADDRESS (msymbol);
649130803Smarcel	  if (do_demangle || asm_demangle)
650130803Smarcel	    name_temp = SYMBOL_PRINT_NAME (msymbol);
65119370Spst	  else
652130803Smarcel	    name_temp = DEPRECATED_SYMBOL_NAME (msymbol);
65319370Spst	}
65419370Spst    }
65519370Spst  if (symbol == NULL && msymbol == NULL)
65698944Sobrien    return 1;
65719370Spst
65819370Spst  /* If the nearest symbol is too far away, don't print anything symbolic.  */
65919370Spst
66019370Spst  /* For when CORE_ADDR is larger than unsigned int, we do math in
66119370Spst     CORE_ADDR.  But when we detect unsigned wraparound in the
66219370Spst     CORE_ADDR math, we ignore this test and print the offset,
66319370Spst     because addr+max_symbolic_offset has wrapped through the end
66419370Spst     of the address space back to the beginning, giving bogus comparison.  */
66519370Spst  if (addr > name_location + max_symbolic_offset
66619370Spst      && name_location + max_symbolic_offset > name_location)
66798944Sobrien    return 1;
66819370Spst
66998944Sobrien  *offset = addr - name_location;
67019370Spst
67198944Sobrien  *name = xstrdup (name_temp);
67298944Sobrien
67319370Spst  if (print_symbol_filename)
67419370Spst    {
67519370Spst      struct symtab_and_line sal;
67619370Spst
67746283Sdfr      sal = find_pc_sect_line (addr, section, 0);
67846283Sdfr
67919370Spst      if (sal.symtab)
68098944Sobrien	{
68198944Sobrien	  *filename = xstrdup (sal.symtab->filename);
68298944Sobrien	  *line = sal.line;
68398944Sobrien	}
68419370Spst      else if (symtab && symbol && symbol->line)
68598944Sobrien	{
68698944Sobrien	  *filename = xstrdup (symtab->filename);
68798944Sobrien	  *line = symbol->line;
68898944Sobrien	}
68919370Spst      else if (symtab)
69098944Sobrien	{
69198944Sobrien	  *filename = xstrdup (symtab->filename);
69298944Sobrien	  *line = -1;
69398944Sobrien	}
69419370Spst    }
69598944Sobrien  return 0;
69619370Spst}
69719370Spst
69819370Spst/* Print address ADDR on STREAM.  USE_LOCAL means the same thing as for
69919370Spst   print_longest.  */
70019370Spstvoid
70198944Sobrienprint_address_numeric (CORE_ADDR addr, int use_local, struct ui_file *stream)
70219370Spst{
70398944Sobrien  /* Truncate address to the size of a target address, avoiding shifts
70498944Sobrien     larger or equal than the width of a CORE_ADDR.  The local
70598944Sobrien     variable ADDR_BIT stops the compiler reporting a shift overflow
70698944Sobrien     when it won't occur. */
70798944Sobrien  /* NOTE: This assumes that the significant address information is
70898944Sobrien     kept in the least significant bits of ADDR - the upper bits were
70998944Sobrien     either zero or sign extended.  Should ADDRESS_TO_POINTER() or
71098944Sobrien     some ADDRESS_TO_PRINTABLE() be used to do the conversion?  */
71198944Sobrien
71298944Sobrien  int addr_bit = TARGET_ADDR_BIT;
71398944Sobrien
71498944Sobrien  if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
71598944Sobrien    addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
71646283Sdfr  print_longest (stream, 'x', use_local, (ULONGEST) addr);
71719370Spst}
71819370Spst
71919370Spst/* Print address ADDR symbolically on STREAM.
72019370Spst   First print it as a number.  Then perhaps print
72119370Spst   <SYMBOL + OFFSET> after the number.  */
72219370Spst
72319370Spstvoid
72498944Sobrienprint_address (CORE_ADDR addr, struct ui_file *stream)
72519370Spst{
72619370Spst  print_address_numeric (addr, 1, stream);
72719370Spst  print_address_symbolic (addr, stream, asm_demangle, " ");
72819370Spst}
72919370Spst
73019370Spst/* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
73119370Spst   controls whether to print the symbolic name "raw" or demangled.
73219370Spst   Global setting "addressprint" controls whether to print hex address
73319370Spst   or not.  */
73419370Spst
73519370Spstvoid
73698944Sobrienprint_address_demangle (CORE_ADDR addr, struct ui_file *stream, int do_demangle)
73719370Spst{
73819370Spst  if (addr == 0)
73919370Spst    {
74019370Spst      fprintf_filtered (stream, "0");
74119370Spst    }
74219370Spst  else if (addressprint)
74319370Spst    {
74419370Spst      print_address_numeric (addr, 1, stream);
74519370Spst      print_address_symbolic (addr, stream, do_demangle, " ");
74619370Spst    }
74719370Spst  else
74819370Spst    {
74919370Spst      print_address_symbolic (addr, stream, do_demangle, "");
75019370Spst    }
75119370Spst}
75219370Spst
75319370Spst
75419370Spst/* These are the types that $__ will get after an examine command of one
75519370Spst   of these sizes.  */
75619370Spst
75746283Sdfrstatic struct type *examine_i_type;
75846283Sdfr
75919370Spststatic struct type *examine_b_type;
76019370Spststatic struct type *examine_h_type;
76119370Spststatic struct type *examine_w_type;
76219370Spststatic struct type *examine_g_type;
76319370Spst
76419370Spst/* Examine data at address ADDR in format FMT.
76519370Spst   Fetch it from memory and print on gdb_stdout.  */
76619370Spst
76719370Spststatic void
76898944Sobriendo_examine (struct format_data fmt, CORE_ADDR addr, asection *sect)
76919370Spst{
770130803Smarcel  char format = 0;
771130803Smarcel  char size;
772130803Smarcel  int count = 1;
77319370Spst  struct type *val_type = NULL;
774130803Smarcel  int i;
775130803Smarcel  int maxelts;
77619370Spst
77719370Spst  format = fmt.format;
77819370Spst  size = fmt.size;
77919370Spst  count = fmt.count;
78019370Spst  next_address = addr;
78146283Sdfr  next_section = sect;
78219370Spst
78319370Spst  /* String or instruction format implies fetch single bytes
78419370Spst     regardless of the specified size.  */
78519370Spst  if (format == 's' || format == 'i')
78619370Spst    size = 'b';
78719370Spst
78846283Sdfr  if (format == 'i')
78946283Sdfr    val_type = examine_i_type;
79046283Sdfr  else if (size == 'b')
79119370Spst    val_type = examine_b_type;
79219370Spst  else if (size == 'h')
79319370Spst    val_type = examine_h_type;
79419370Spst  else if (size == 'w')
79519370Spst    val_type = examine_w_type;
79619370Spst  else if (size == 'g')
79719370Spst    val_type = examine_g_type;
79819370Spst
79919370Spst  maxelts = 8;
80019370Spst  if (size == 'w')
80119370Spst    maxelts = 4;
80219370Spst  if (size == 'g')
80319370Spst    maxelts = 2;
80419370Spst  if (format == 's' || format == 'i')
80519370Spst    maxelts = 1;
80619370Spst
80719370Spst  /* Print as many objects as specified in COUNT, at most maxelts per line,
80819370Spst     with the address of the next one at the start of each line.  */
80919370Spst
81019370Spst  while (count > 0)
81119370Spst    {
81219370Spst      QUIT;
81319370Spst      print_address (next_address, gdb_stdout);
81419370Spst      printf_filtered (":");
81519370Spst      for (i = maxelts;
81619370Spst	   i > 0 && count > 0;
81719370Spst	   i--, count--)
81819370Spst	{
81919370Spst	  printf_filtered ("\t");
82019370Spst	  /* Note that print_formatted sets next_address for the next
82119370Spst	     object.  */
82219370Spst	  last_examine_address = next_address;
82346283Sdfr
82446283Sdfr	  if (last_examine_value)
82546283Sdfr	    value_free (last_examine_value);
82646283Sdfr
82746283Sdfr	  /* The value to be displayed is not fetched greedily.
82898944Sobrien	     Instead, to avoid the posibility of a fetched value not
82998944Sobrien	     being used, its retreval is delayed until the print code
83098944Sobrien	     uses it.  When examining an instruction stream, the
83198944Sobrien	     disassembler will perform its own memory fetch using just
83298944Sobrien	     the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
83398944Sobrien	     the disassembler be modified so that LAST_EXAMINE_VALUE
83498944Sobrien	     is left with the byte sequence from the last complete
83598944Sobrien	     instruction fetched from memory? */
83646283Sdfr	  last_examine_value = value_at_lazy (val_type, next_address, sect);
83746283Sdfr
83846283Sdfr	  if (last_examine_value)
83946283Sdfr	    release_value (last_examine_value);
84046283Sdfr
84198944Sobrien	  print_formatted (last_examine_value, format, size, gdb_stdout);
84219370Spst	}
84319370Spst      printf_filtered ("\n");
84419370Spst      gdb_flush (gdb_stdout);
84519370Spst    }
84619370Spst}
84719370Spst
84819370Spststatic void
84998944Sobrienvalidate_format (struct format_data fmt, char *cmdname)
85019370Spst{
85119370Spst  if (fmt.size != 0)
85219370Spst    error ("Size letters are meaningless in \"%s\" command.", cmdname);
85319370Spst  if (fmt.count != 1)
85419370Spst    error ("Item count other than 1 is meaningless in \"%s\" command.",
85519370Spst	   cmdname);
85619370Spst  if (fmt.format == 'i' || fmt.format == 's')
85719370Spst    error ("Format letter \"%c\" is meaningless in \"%s\" command.",
85819370Spst	   fmt.format, cmdname);
85919370Spst}
86019370Spst
86119370Spst/*  Evaluate string EXP as an expression in the current language and
86298944Sobrien   print the resulting value.  EXP may contain a format specifier as the
86398944Sobrien   first argument ("/x myvar" for example, to print myvar in hex).
86498944Sobrien */
86519370Spst
86619370Spststatic void
86798944Sobrienprint_command_1 (char *exp, int inspect, int voidprint)
86819370Spst{
86919370Spst  struct expression *expr;
870130803Smarcel  struct cleanup *old_chain = 0;
871130803Smarcel  char format = 0;
87298944Sobrien  struct value *val;
87319370Spst  struct format_data fmt;
87419370Spst  int cleanup = 0;
87519370Spst
87619370Spst  /* Pass inspect flag to the rest of the print routines in a global (sigh). */
87719370Spst  inspect_it = inspect;
87819370Spst
87919370Spst  if (exp && *exp == '/')
88019370Spst    {
88119370Spst      exp++;
88219370Spst      fmt = decode_format (&exp, last_format, 0);
88319370Spst      validate_format (fmt, "print");
88419370Spst      last_format = format = fmt.format;
88519370Spst    }
88619370Spst  else
88719370Spst    {
88819370Spst      fmt.count = 1;
88919370Spst      fmt.format = 0;
89019370Spst      fmt.size = 0;
89119370Spst    }
89219370Spst
89319370Spst  if (exp && *exp)
89419370Spst    {
89519370Spst      struct type *type;
89619370Spst      expr = parse_expression (exp);
89798944Sobrien      old_chain = make_cleanup (free_current_contents, &expr);
89819370Spst      cleanup = 1;
89919370Spst      val = evaluate_expression (expr);
90019370Spst    }
90119370Spst  else
90219370Spst    val = access_value_history (0);
90319370Spst
90419370Spst  if (voidprint || (val && VALUE_TYPE (val) &&
90598944Sobrien		    TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_VOID))
90619370Spst    {
90719370Spst      int histindex = record_latest_value (val);
90819370Spst
90919370Spst      if (histindex >= 0)
91019370Spst	annotate_value_history_begin (histindex, VALUE_TYPE (val));
91119370Spst      else
91219370Spst	annotate_value_begin (VALUE_TYPE (val));
91319370Spst
91419370Spst      if (inspect)
91519370Spst	printf_unfiltered ("\031(gdb-makebuffer \"%s\"  %d '(\"", exp, histindex);
91698944Sobrien      else if (histindex >= 0)
91798944Sobrien	printf_filtered ("$%d = ", histindex);
91819370Spst
91919370Spst      if (histindex >= 0)
92019370Spst	annotate_value_history_value ();
92119370Spst
92298944Sobrien      print_formatted (val, format, fmt.size, gdb_stdout);
92319370Spst      printf_filtered ("\n");
92419370Spst
92519370Spst      if (histindex >= 0)
92619370Spst	annotate_value_history_end ();
92719370Spst      else
92819370Spst	annotate_value_end ();
92919370Spst
93019370Spst      if (inspect)
93198944Sobrien	printf_unfiltered ("\") )\030");
93219370Spst    }
93319370Spst
93419370Spst  if (cleanup)
93519370Spst    do_cleanups (old_chain);
93698944Sobrien  inspect_it = 0;		/* Reset print routines to normal */
93719370Spst}
93819370Spst
93919370Spststatic void
94098944Sobrienprint_command (char *exp, int from_tty)
94119370Spst{
94219370Spst  print_command_1 (exp, 0, 1);
94319370Spst}
94419370Spst
94519370Spst/* Same as print, except in epoch, it gets its own window */
94619370Spststatic void
94798944Sobrieninspect_command (char *exp, int from_tty)
94819370Spst{
94919370Spst  extern int epoch_interface;
95019370Spst
95119370Spst  print_command_1 (exp, epoch_interface, 1);
95219370Spst}
95319370Spst
95419370Spst/* Same as print, except it doesn't print void results. */
95519370Spststatic void
95698944Sobriencall_command (char *exp, int from_tty)
95719370Spst{
95819370Spst  print_command_1 (exp, 0, 0);
95919370Spst}
96019370Spst
96146283Sdfrvoid
96298944Sobrienoutput_command (char *exp, int from_tty)
96319370Spst{
96419370Spst  struct expression *expr;
965130803Smarcel  struct cleanup *old_chain;
966130803Smarcel  char format = 0;
96798944Sobrien  struct value *val;
96819370Spst  struct format_data fmt;
96919370Spst
97019370Spst  if (exp && *exp == '/')
97119370Spst    {
97219370Spst      exp++;
97319370Spst      fmt = decode_format (&exp, 0, 0);
97419370Spst      validate_format (fmt, "output");
97519370Spst      format = fmt.format;
97619370Spst    }
97719370Spst
97819370Spst  expr = parse_expression (exp);
97998944Sobrien  old_chain = make_cleanup (free_current_contents, &expr);
98019370Spst
98119370Spst  val = evaluate_expression (expr);
98219370Spst
98319370Spst  annotate_value_begin (VALUE_TYPE (val));
98419370Spst
98598944Sobrien  print_formatted (val, format, fmt.size, gdb_stdout);
98619370Spst
98719370Spst  annotate_value_end ();
98819370Spst
98998944Sobrien  wrap_here ("");
99098944Sobrien  gdb_flush (gdb_stdout);
99198944Sobrien
99219370Spst  do_cleanups (old_chain);
99319370Spst}
99419370Spst
99519370Spststatic void
99698944Sobrienset_command (char *exp, int from_tty)
99719370Spst{
99819370Spst  struct expression *expr = parse_expression (exp);
999130803Smarcel  struct cleanup *old_chain =
100098944Sobrien    make_cleanup (free_current_contents, &expr);
100119370Spst  evaluate_expression (expr);
100219370Spst  do_cleanups (old_chain);
100319370Spst}
100419370Spst
100519370Spststatic void
100698944Sobriensym_info (char *arg, int from_tty)
100746283Sdfr{
100846283Sdfr  struct minimal_symbol *msymbol;
100998944Sobrien  struct objfile *objfile;
101098944Sobrien  struct obj_section *osect;
101198944Sobrien  asection *sect;
101298944Sobrien  CORE_ADDR addr, sect_addr;
101398944Sobrien  int matches = 0;
101498944Sobrien  unsigned int offset;
101546283Sdfr
101646283Sdfr  if (!arg)
101746283Sdfr    error_no_arg ("address");
101846283Sdfr
101946283Sdfr  addr = parse_and_eval_address (arg);
102046283Sdfr  ALL_OBJSECTIONS (objfile, osect)
102198944Sobrien  {
102298944Sobrien    sect = osect->the_bfd_section;
102398944Sobrien    sect_addr = overlay_mapped_address (addr, sect);
102446283Sdfr
102598944Sobrien    if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
102698944Sobrien	(msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
102798944Sobrien      {
102898944Sobrien	matches = 1;
102998944Sobrien	offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
103098944Sobrien	if (offset)
103198944Sobrien	  printf_filtered ("%s + %u in ",
1032130803Smarcel			   SYMBOL_PRINT_NAME (msymbol), offset);
103398944Sobrien	else
103498944Sobrien	  printf_filtered ("%s in ",
1035130803Smarcel			   SYMBOL_PRINT_NAME (msymbol));
103698944Sobrien	if (pc_in_unmapped_range (addr, sect))
103798944Sobrien	  printf_filtered ("load address range of ");
103898944Sobrien	if (section_is_overlay (sect))
103998944Sobrien	  printf_filtered ("%s overlay ",
104098944Sobrien			   section_is_mapped (sect) ? "mapped" : "unmapped");
104198944Sobrien	printf_filtered ("section %s", sect->name);
104298944Sobrien	printf_filtered ("\n");
104398944Sobrien      }
104498944Sobrien  }
104546283Sdfr  if (matches == 0)
104646283Sdfr    printf_filtered ("No symbol matches %s.\n", arg);
104746283Sdfr}
104846283Sdfr
104946283Sdfrstatic void
105098944Sobrienaddress_info (char *exp, int from_tty)
105119370Spst{
1052130803Smarcel  struct symbol *sym;
1053130803Smarcel  struct minimal_symbol *msymbol;
1054130803Smarcel  long val;
1055130803Smarcel  long basereg;
105646283Sdfr  asection *section;
105746283Sdfr  CORE_ADDR load_addr;
105819370Spst  int is_a_field_of_this;	/* C++: lookup_symbol sets this to nonzero
105919370Spst				   if exp is a field of `this'. */
106019370Spst
106119370Spst  if (exp == 0)
106219370Spst    error ("Argument required.");
106319370Spst
1064130803Smarcel  sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
106598944Sobrien		       &is_a_field_of_this, (struct symtab **) NULL);
106619370Spst  if (sym == NULL)
106719370Spst    {
106819370Spst      if (is_a_field_of_this)
106919370Spst	{
107019370Spst	  printf_filtered ("Symbol \"");
107119370Spst	  fprintf_symbol_filtered (gdb_stdout, exp,
107219370Spst				   current_language->la_language, DMGL_ANSI);
1073130803Smarcel	  printf_filtered ("\" is a field of the local class variable ");
1074130803Smarcel	  if (current_language->la_language == language_objc)
1075130803Smarcel	    printf_filtered ("`self'\n");	/* ObjC equivalent of "this" */
1076130803Smarcel	  else
1077130803Smarcel	    printf_filtered ("`this'\n");
107819370Spst	  return;
107919370Spst	}
108019370Spst
108119370Spst      msymbol = lookup_minimal_symbol (exp, NULL, NULL);
108219370Spst
108319370Spst      if (msymbol != NULL)
108419370Spst	{
108546283Sdfr	  load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
108646283Sdfr
108719370Spst	  printf_filtered ("Symbol \"");
108819370Spst	  fprintf_symbol_filtered (gdb_stdout, exp,
108919370Spst				   current_language->la_language, DMGL_ANSI);
109019370Spst	  printf_filtered ("\" is at ");
109146283Sdfr	  print_address_numeric (load_addr, 1, gdb_stdout);
109246283Sdfr	  printf_filtered (" in a file compiled without debugging");
109346283Sdfr	  section = SYMBOL_BFD_SECTION (msymbol);
109446283Sdfr	  if (section_is_overlay (section))
109546283Sdfr	    {
109646283Sdfr	      load_addr = overlay_unmapped_address (load_addr, section);
109746283Sdfr	      printf_filtered (",\n -- loaded at ");
109846283Sdfr	      print_address_numeric (load_addr, 1, gdb_stdout);
109946283Sdfr	      printf_filtered (" in overlay section %s", section->name);
110046283Sdfr	    }
110146283Sdfr	  printf_filtered (".\n");
110219370Spst	}
110319370Spst      else
110419370Spst	error ("No symbol \"%s\" in current context.", exp);
110519370Spst      return;
110619370Spst    }
110719370Spst
110819370Spst  printf_filtered ("Symbol \"");
1109130803Smarcel  fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym),
111019370Spst			   current_language->la_language, DMGL_ANSI);
111119370Spst  printf_filtered ("\" is ");
111298944Sobrien  val = SYMBOL_VALUE (sym);
111319370Spst  basereg = SYMBOL_BASEREG (sym);
111446283Sdfr  section = SYMBOL_BFD_SECTION (sym);
111519370Spst
111619370Spst  switch (SYMBOL_CLASS (sym))
111719370Spst    {
111819370Spst    case LOC_CONST:
111919370Spst    case LOC_CONST_BYTES:
112019370Spst      printf_filtered ("constant");
112119370Spst      break;
112219370Spst
112319370Spst    case LOC_LABEL:
112419370Spst      printf_filtered ("a label at address ");
112598944Sobrien      print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
112646283Sdfr			     1, gdb_stdout);
112746283Sdfr      if (section_is_overlay (section))
112846283Sdfr	{
112946283Sdfr	  load_addr = overlay_unmapped_address (load_addr, section);
113046283Sdfr	  printf_filtered (",\n -- loaded at ");
113146283Sdfr	  print_address_numeric (load_addr, 1, gdb_stdout);
113246283Sdfr	  printf_filtered (" in overlay section %s", section->name);
113346283Sdfr	}
113419370Spst      break;
113519370Spst
1136130803Smarcel    case LOC_COMPUTED:
1137130803Smarcel    case LOC_COMPUTED_ARG:
1138130803Smarcel      /* FIXME: cagney/2004-01-26: It should be possible to
1139130803Smarcel	 unconditionally call the SYMBOL_OPS method when available.
1140130803Smarcel	 Unfortunately DWARF 2 stores the frame-base (instead of the
1141130803Smarcel	 function) location in a function's symbol.  Oops!  For the
1142130803Smarcel	 moment enable this when/where applicable.  */
1143130803Smarcel      SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
1144130803Smarcel      break;
1145130803Smarcel
114619370Spst    case LOC_REGISTER:
114746283Sdfr      printf_filtered ("a variable in register %s", REGISTER_NAME (val));
114819370Spst      break;
114919370Spst
115019370Spst    case LOC_STATIC:
115119370Spst      printf_filtered ("static storage at address ");
115298944Sobrien      print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
115346283Sdfr			     1, gdb_stdout);
115446283Sdfr      if (section_is_overlay (section))
115546283Sdfr	{
115646283Sdfr	  load_addr = overlay_unmapped_address (load_addr, section);
115746283Sdfr	  printf_filtered (",\n -- loaded at ");
115846283Sdfr	  print_address_numeric (load_addr, 1, gdb_stdout);
115946283Sdfr	  printf_filtered (" in overlay section %s", section->name);
116046283Sdfr	}
116119370Spst      break;
116219370Spst
116346283Sdfr    case LOC_INDIRECT:
116446283Sdfr      printf_filtered ("external global (indirect addressing), at address *(");
116546283Sdfr      print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
116646283Sdfr			     1, gdb_stdout);
116746283Sdfr      printf_filtered (")");
116846283Sdfr      if (section_is_overlay (section))
116946283Sdfr	{
117046283Sdfr	  load_addr = overlay_unmapped_address (load_addr, section);
117146283Sdfr	  printf_filtered (",\n -- loaded at ");
117246283Sdfr	  print_address_numeric (load_addr, 1, gdb_stdout);
117346283Sdfr	  printf_filtered (" in overlay section %s", section->name);
117446283Sdfr	}
117546283Sdfr      break;
117646283Sdfr
117719370Spst    case LOC_REGPARM:
117846283Sdfr      printf_filtered ("an argument in register %s", REGISTER_NAME (val));
117919370Spst      break;
118019370Spst
118119370Spst    case LOC_REGPARM_ADDR:
118246283Sdfr      printf_filtered ("address of an argument in register %s", REGISTER_NAME (val));
118319370Spst      break;
118419370Spst
118519370Spst    case LOC_ARG:
118619370Spst      printf_filtered ("an argument at offset %ld", val);
118719370Spst      break;
118819370Spst
118919370Spst    case LOC_LOCAL_ARG:
119019370Spst      printf_filtered ("an argument at frame offset %ld", val);
119119370Spst      break;
119219370Spst
119319370Spst    case LOC_LOCAL:
119419370Spst      printf_filtered ("a local variable at frame offset %ld", val);
119519370Spst      break;
119619370Spst
119719370Spst    case LOC_REF_ARG:
119819370Spst      printf_filtered ("a reference argument at offset %ld", val);
119919370Spst      break;
120019370Spst
120119370Spst    case LOC_BASEREG:
120219370Spst      printf_filtered ("a variable at offset %ld from register %s",
120398944Sobrien		       val, REGISTER_NAME (basereg));
120419370Spst      break;
120519370Spst
120619370Spst    case LOC_BASEREG_ARG:
120719370Spst      printf_filtered ("an argument at offset %ld from register %s",
120898944Sobrien		       val, REGISTER_NAME (basereg));
120919370Spst      break;
121019370Spst
121119370Spst    case LOC_TYPEDEF:
121219370Spst      printf_filtered ("a typedef");
121319370Spst      break;
121419370Spst
121519370Spst    case LOC_BLOCK:
121619370Spst      printf_filtered ("a function at address ");
121798944Sobrien      print_address_numeric (load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
121846283Sdfr			     1, gdb_stdout);
121946283Sdfr      if (section_is_overlay (section))
122046283Sdfr	{
122146283Sdfr	  load_addr = overlay_unmapped_address (load_addr, section);
122246283Sdfr	  printf_filtered (",\n -- loaded at ");
122346283Sdfr	  print_address_numeric (load_addr, 1, gdb_stdout);
122446283Sdfr	  printf_filtered (" in overlay section %s", section->name);
122546283Sdfr	}
122619370Spst      break;
122719370Spst
122819370Spst    case LOC_UNRESOLVED:
122919370Spst      {
123019370Spst	struct minimal_symbol *msym;
123119370Spst
1232130803Smarcel	msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL);
123319370Spst	if (msym == NULL)
123419370Spst	  printf_filtered ("unresolved");
123519370Spst	else
123619370Spst	  {
123746283Sdfr	    section = SYMBOL_BFD_SECTION (msym);
123819370Spst	    printf_filtered ("static storage at address ");
123998944Sobrien	    print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (msym),
124046283Sdfr				   1, gdb_stdout);
124146283Sdfr	    if (section_is_overlay (section))
124246283Sdfr	      {
124346283Sdfr		load_addr = overlay_unmapped_address (load_addr, section);
124446283Sdfr		printf_filtered (",\n -- loaded at ");
124546283Sdfr		print_address_numeric (load_addr, 1, gdb_stdout);
124646283Sdfr		printf_filtered (" in overlay section %s", section->name);
124746283Sdfr	      }
124819370Spst	  }
124919370Spst      }
125019370Spst      break;
125119370Spst
1252130803Smarcel    case LOC_HP_THREAD_LOCAL_STATIC:
125346283Sdfr      printf_filtered (
125498944Sobrien			"a thread-local variable at offset %ld from the thread base register %s",
125598944Sobrien			val, REGISTER_NAME (basereg));
125646283Sdfr      break;
125746283Sdfr
125819370Spst    case LOC_OPTIMIZED_OUT:
125919370Spst      printf_filtered ("optimized out");
126019370Spst      break;
126198944Sobrien
126219370Spst    default:
126319370Spst      printf_filtered ("of unknown (botched) type");
126419370Spst      break;
126519370Spst    }
126619370Spst  printf_filtered (".\n");
126719370Spst}
126819370Spst
126946283Sdfrvoid
127098944Sobrienx_command (char *exp, int from_tty)
127119370Spst{
127219370Spst  struct expression *expr;
127319370Spst  struct format_data fmt;
127419370Spst  struct cleanup *old_chain;
127519370Spst  struct value *val;
127619370Spst
127719370Spst  fmt.format = last_format;
127819370Spst  fmt.size = last_size;
127919370Spst  fmt.count = 1;
128019370Spst
128119370Spst  if (exp && *exp == '/')
128219370Spst    {
128319370Spst      exp++;
128419370Spst      fmt = decode_format (&exp, last_format, last_size);
128519370Spst    }
128619370Spst
128719370Spst  /* If we have an expression, evaluate it and use it as the address.  */
128819370Spst
128919370Spst  if (exp != 0 && *exp != 0)
129019370Spst    {
129119370Spst      expr = parse_expression (exp);
129219370Spst      /* Cause expression not to be there any more
129398944Sobrien         if this command is repeated with Newline.
129498944Sobrien         But don't clobber a user-defined command's definition.  */
129519370Spst      if (from_tty)
129619370Spst	*exp = 0;
129798944Sobrien      old_chain = make_cleanup (free_current_contents, &expr);
129819370Spst      val = evaluate_expression (expr);
129919370Spst      if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF)
130019370Spst	val = value_ind (val);
130119370Spst      /* In rvalue contexts, such as this, functions are coerced into
130298944Sobrien         pointers to functions.  This makes "x/i main" work.  */
130398944Sobrien      if (/* last_format == 'i'  && */
130498944Sobrien	  TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
130598944Sobrien	   && VALUE_LVAL (val) == lval_memory)
130619370Spst	next_address = VALUE_ADDRESS (val);
130719370Spst      else
130898944Sobrien	next_address = value_as_address (val);
130946283Sdfr      if (VALUE_BFD_SECTION (val))
131046283Sdfr	next_section = VALUE_BFD_SECTION (val);
131119370Spst      do_cleanups (old_chain);
131219370Spst    }
131319370Spst
131446283Sdfr  do_examine (fmt, next_address, next_section);
131519370Spst
131619370Spst  /* If the examine succeeds, we remember its size and format for next time.  */
131719370Spst  last_size = fmt.size;
131819370Spst  last_format = fmt.format;
131919370Spst
132019370Spst  /* Set a couple of internal variables if appropriate. */
132119370Spst  if (last_examine_value)
132219370Spst    {
132319370Spst      /* Make last address examined available to the user as $_.  Use
132498944Sobrien         the correct pointer type.  */
132598944Sobrien      struct type *pointer_type
132698944Sobrien	= lookup_pointer_type (VALUE_TYPE (last_examine_value));
132719370Spst      set_internalvar (lookup_internalvar ("_"),
132898944Sobrien		       value_from_pointer (pointer_type,
132998944Sobrien					   last_examine_address));
133098944Sobrien
133198944Sobrien      /* Make contents of last address examined available to the user as $__. */
133246283Sdfr      /* If the last value has not been fetched from memory then don't
133346283Sdfr         fetch it now - instead mark it by voiding the $__ variable. */
133446283Sdfr      if (VALUE_LAZY (last_examine_value))
133546283Sdfr	set_internalvar (lookup_internalvar ("__"),
133646283Sdfr			 allocate_value (builtin_type_void));
133746283Sdfr      else
133846283Sdfr	set_internalvar (lookup_internalvar ("__"), last_examine_value);
133919370Spst    }
134019370Spst}
134198944Sobrien
134219370Spst
134319370Spst/* Add an expression to the auto-display chain.
134419370Spst   Specify the expression.  */
134519370Spst
134619370Spststatic void
134798944Sobriendisplay_command (char *exp, int from_tty)
134819370Spst{
134919370Spst  struct format_data fmt;
1350130803Smarcel  struct expression *expr;
1351130803Smarcel  struct display *new;
135246283Sdfr  int display_it = 1;
135319370Spst
135446283Sdfr#if defined(TUI)
1355130803Smarcel  /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1356130803Smarcel     `tui_version'.  */
1357130803Smarcel  if (tui_active && exp != NULL && *exp == '$')
1358130803Smarcel    display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
135946283Sdfr#endif
136019370Spst
136146283Sdfr  if (display_it)
136219370Spst    {
136346283Sdfr      if (exp == 0)
136446283Sdfr	{
136546283Sdfr	  do_displays ();
136646283Sdfr	  return;
136746283Sdfr	}
136819370Spst
136946283Sdfr      if (*exp == '/')
137046283Sdfr	{
137146283Sdfr	  exp++;
137246283Sdfr	  fmt = decode_format (&exp, 0, 0);
137346283Sdfr	  if (fmt.size && fmt.format == 0)
137446283Sdfr	    fmt.format = 'x';
137546283Sdfr	  if (fmt.format == 'i' || fmt.format == 's')
137646283Sdfr	    fmt.size = 'b';
137746283Sdfr	}
137846283Sdfr      else
137946283Sdfr	{
138046283Sdfr	  fmt.format = 0;
138146283Sdfr	  fmt.size = 0;
138246283Sdfr	  fmt.count = 0;
138346283Sdfr	}
138419370Spst
138546283Sdfr      innermost_block = 0;
138646283Sdfr      expr = parse_expression (exp);
138719370Spst
138846283Sdfr      new = (struct display *) xmalloc (sizeof (struct display));
138919370Spst
139046283Sdfr      new->exp = expr;
139146283Sdfr      new->block = innermost_block;
139246283Sdfr      new->next = display_chain;
139346283Sdfr      new->number = ++display_number;
139446283Sdfr      new->format = fmt;
139598944Sobrien      new->enabled_p = 1;
139646283Sdfr      display_chain = new;
139719370Spst
139846283Sdfr      if (from_tty && target_has_execution)
139946283Sdfr	do_one_display (new);
140046283Sdfr
140146283Sdfr      dont_repeat ();
140246283Sdfr    }
140319370Spst}
140419370Spst
140519370Spststatic void
140698944Sobrienfree_display (struct display *d)
140719370Spst{
140898944Sobrien  xfree (d->exp);
140998944Sobrien  xfree (d);
141019370Spst}
141119370Spst
141219370Spst/* Clear out the display_chain.
141319370Spst   Done when new symtabs are loaded, since this invalidates
141419370Spst   the types stored in many expressions.  */
141519370Spst
141619370Spstvoid
141798944Sobrienclear_displays (void)
141819370Spst{
1419130803Smarcel  struct display *d;
142019370Spst
142119370Spst  while ((d = display_chain) != NULL)
142219370Spst    {
142398944Sobrien      xfree (d->exp);
142419370Spst      display_chain = d->next;
142598944Sobrien      xfree (d);
142619370Spst    }
142719370Spst}
142819370Spst
142919370Spst/* Delete the auto-display number NUM.  */
143019370Spst
143119370Spststatic void
143298944Sobriendelete_display (int num)
143319370Spst{
1434130803Smarcel  struct display *d1, *d;
143519370Spst
143619370Spst  if (!display_chain)
143719370Spst    error ("No display number %d.", num);
143819370Spst
143919370Spst  if (display_chain->number == num)
144019370Spst    {
144119370Spst      d1 = display_chain;
144219370Spst      display_chain = d1->next;
144319370Spst      free_display (d1);
144419370Spst    }
144519370Spst  else
144698944Sobrien    for (d = display_chain;; d = d->next)
144719370Spst      {
144819370Spst	if (d->next == 0)
144919370Spst	  error ("No display number %d.", num);
145019370Spst	if (d->next->number == num)
145119370Spst	  {
145219370Spst	    d1 = d->next;
145319370Spst	    d->next = d1->next;
145419370Spst	    free_display (d1);
145519370Spst	    break;
145619370Spst	  }
145719370Spst      }
145819370Spst}
145919370Spst
146019370Spst/* Delete some values from the auto-display chain.
146119370Spst   Specify the element numbers.  */
146219370Spst
146319370Spststatic void
146498944Sobrienundisplay_command (char *args, int from_tty)
146519370Spst{
1466130803Smarcel  char *p = args;
1467130803Smarcel  char *p1;
1468130803Smarcel  int num;
146919370Spst
147019370Spst  if (args == 0)
147119370Spst    {
147219370Spst      if (query ("Delete all auto-display expressions? "))
147319370Spst	clear_displays ();
147419370Spst      dont_repeat ();
147519370Spst      return;
147619370Spst    }
147719370Spst
147819370Spst  while (*p)
147919370Spst    {
148019370Spst      p1 = p;
148198944Sobrien      while (*p1 >= '0' && *p1 <= '9')
148298944Sobrien	p1++;
148319370Spst      if (*p1 && *p1 != ' ' && *p1 != '\t')
148419370Spst	error ("Arguments must be display numbers.");
148519370Spst
148619370Spst      num = atoi (p);
148719370Spst
148819370Spst      delete_display (num);
148919370Spst
149019370Spst      p = p1;
149198944Sobrien      while (*p == ' ' || *p == '\t')
149298944Sobrien	p++;
149319370Spst    }
149419370Spst  dont_repeat ();
149519370Spst}
149619370Spst
149719370Spst/* Display a single auto-display.
149819370Spst   Do nothing if the display cannot be printed in the current context,
149919370Spst   or if the display is disabled. */
150019370Spst
150119370Spststatic void
150298944Sobriendo_one_display (struct display *d)
150319370Spst{
150419370Spst  int within_current_scope;
150519370Spst
150698944Sobrien  if (d->enabled_p == 0)
150719370Spst    return;
150819370Spst
150919370Spst  if (d->block)
1510130803Smarcel    within_current_scope = contained_in (get_selected_block (0), d->block);
151119370Spst  else
151219370Spst    within_current_scope = 1;
151319370Spst  if (!within_current_scope)
151419370Spst    return;
151519370Spst
151619370Spst  current_display_number = d->number;
151719370Spst
151819370Spst  annotate_display_begin ();
151919370Spst  printf_filtered ("%d", d->number);
152019370Spst  annotate_display_number_end ();
152119370Spst  printf_filtered (": ");
152219370Spst  if (d->format.size)
152319370Spst    {
152419370Spst      CORE_ADDR addr;
152598944Sobrien      struct value *val;
152619370Spst
152719370Spst      annotate_display_format ();
152819370Spst
152919370Spst      printf_filtered ("x/");
153019370Spst      if (d->format.count != 1)
153119370Spst	printf_filtered ("%d", d->format.count);
153219370Spst      printf_filtered ("%c", d->format.format);
153319370Spst      if (d->format.format != 'i' && d->format.format != 's')
153419370Spst	printf_filtered ("%c", d->format.size);
153519370Spst      printf_filtered (" ");
153619370Spst
153719370Spst      annotate_display_expression ();
153819370Spst
153919370Spst      print_expression (d->exp, gdb_stdout);
154019370Spst      annotate_display_expression_end ();
154119370Spst
154219370Spst      if (d->format.count != 1)
154319370Spst	printf_filtered ("\n");
154419370Spst      else
154519370Spst	printf_filtered ("  ");
154698944Sobrien
154746283Sdfr      val = evaluate_expression (d->exp);
154898944Sobrien      addr = value_as_address (val);
154919370Spst      if (d->format.format == 'i')
155019370Spst	addr = ADDR_BITS_REMOVE (addr);
155119370Spst
155219370Spst      annotate_display_value ();
155319370Spst
155446283Sdfr      do_examine (d->format, addr, VALUE_BFD_SECTION (val));
155519370Spst    }
155619370Spst  else
155719370Spst    {
155819370Spst      annotate_display_format ();
155919370Spst
156019370Spst      if (d->format.format)
156119370Spst	printf_filtered ("/%c ", d->format.format);
156219370Spst
156319370Spst      annotate_display_expression ();
156419370Spst
156519370Spst      print_expression (d->exp, gdb_stdout);
156619370Spst      annotate_display_expression_end ();
156719370Spst
156819370Spst      printf_filtered (" = ");
156919370Spst
157019370Spst      annotate_display_expression ();
157119370Spst
157219370Spst      print_formatted (evaluate_expression (d->exp),
157398944Sobrien		       d->format.format, d->format.size, gdb_stdout);
157419370Spst      printf_filtered ("\n");
157519370Spst    }
157619370Spst
157719370Spst  annotate_display_end ();
157819370Spst
157919370Spst  gdb_flush (gdb_stdout);
158019370Spst  current_display_number = -1;
158119370Spst}
158219370Spst
158319370Spst/* Display all of the values on the auto-display chain which can be
158419370Spst   evaluated in the current scope.  */
158519370Spst
158619370Spstvoid
158798944Sobriendo_displays (void)
158819370Spst{
1589130803Smarcel  struct display *d;
159019370Spst
159119370Spst  for (d = display_chain; d; d = d->next)
159219370Spst    do_one_display (d);
159319370Spst}
159419370Spst
159519370Spst/* Delete the auto-display which we were in the process of displaying.
159619370Spst   This is done when there is an error or a signal.  */
159719370Spst
159819370Spstvoid
159998944Sobriendisable_display (int num)
160019370Spst{
1601130803Smarcel  struct display *d;
160219370Spst
160319370Spst  for (d = display_chain; d; d = d->next)
160419370Spst    if (d->number == num)
160519370Spst      {
160698944Sobrien	d->enabled_p = 0;
160719370Spst	return;
160819370Spst      }
160919370Spst  printf_unfiltered ("No display number %d.\n", num);
161019370Spst}
161198944Sobrien
161219370Spstvoid
161398944Sobriendisable_current_display (void)
161419370Spst{
161519370Spst  if (current_display_number >= 0)
161619370Spst    {
161719370Spst      disable_display (current_display_number);
161819370Spst      fprintf_unfiltered (gdb_stderr, "Disabling display %d to avoid infinite recursion.\n",
161998944Sobrien			  current_display_number);
162019370Spst    }
162119370Spst  current_display_number = -1;
162219370Spst}
162319370Spst
162419370Spststatic void
162598944Sobriendisplay_info (char *ignore, int from_tty)
162619370Spst{
1627130803Smarcel  struct display *d;
162819370Spst
162919370Spst  if (!display_chain)
163019370Spst    printf_unfiltered ("There are no auto-display expressions now.\n");
163119370Spst  else
163298944Sobrien    printf_filtered ("Auto-display expressions now in effect:\n\
163319370SpstNum Enb Expression\n");
163419370Spst
163519370Spst  for (d = display_chain; d; d = d->next)
163619370Spst    {
163798944Sobrien      printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
163819370Spst      if (d->format.size)
163919370Spst	printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
164098944Sobrien			 d->format.format);
164119370Spst      else if (d->format.format)
164219370Spst	printf_filtered ("/%c ", d->format.format);
164319370Spst      print_expression (d->exp, gdb_stdout);
1644130803Smarcel      if (d->block && !contained_in (get_selected_block (0), d->block))
164519370Spst	printf_filtered (" (cannot be evaluated in the current context)");
164619370Spst      printf_filtered ("\n");
164719370Spst      gdb_flush (gdb_stdout);
164819370Spst    }
164919370Spst}
165019370Spst
165119370Spststatic void
165298944Sobrienenable_display (char *args, int from_tty)
165319370Spst{
1654130803Smarcel  char *p = args;
1655130803Smarcel  char *p1;
1656130803Smarcel  int num;
1657130803Smarcel  struct display *d;
165819370Spst
165919370Spst  if (p == 0)
166019370Spst    {
166119370Spst      for (d = display_chain; d; d = d->next)
166298944Sobrien	d->enabled_p = 1;
166319370Spst    }
166419370Spst  else
166519370Spst    while (*p)
166619370Spst      {
166719370Spst	p1 = p;
166819370Spst	while (*p1 >= '0' && *p1 <= '9')
166919370Spst	  p1++;
167019370Spst	if (*p1 && *p1 != ' ' && *p1 != '\t')
167119370Spst	  error ("Arguments must be display numbers.");
167298944Sobrien
167319370Spst	num = atoi (p);
167498944Sobrien
167519370Spst	for (d = display_chain; d; d = d->next)
167619370Spst	  if (d->number == num)
167719370Spst	    {
167898944Sobrien	      d->enabled_p = 1;
167919370Spst	      goto win;
168019370Spst	    }
168119370Spst	printf_unfiltered ("No display number %d.\n", num);
168219370Spst      win:
168319370Spst	p = p1;
168419370Spst	while (*p == ' ' || *p == '\t')
168519370Spst	  p++;
168619370Spst      }
168719370Spst}
168819370Spst
168919370Spststatic void
169098944Sobriendisable_display_command (char *args, int from_tty)
169119370Spst{
1692130803Smarcel  char *p = args;
1693130803Smarcel  char *p1;
1694130803Smarcel  struct display *d;
169519370Spst
169619370Spst  if (p == 0)
169719370Spst    {
169819370Spst      for (d = display_chain; d; d = d->next)
169998944Sobrien	d->enabled_p = 0;
170019370Spst    }
170119370Spst  else
170219370Spst    while (*p)
170319370Spst      {
170419370Spst	p1 = p;
170519370Spst	while (*p1 >= '0' && *p1 <= '9')
170619370Spst	  p1++;
170719370Spst	if (*p1 && *p1 != ' ' && *p1 != '\t')
170819370Spst	  error ("Arguments must be display numbers.");
170998944Sobrien
171019370Spst	disable_display (atoi (p));
171119370Spst
171219370Spst	p = p1;
171319370Spst	while (*p == ' ' || *p == '\t')
171419370Spst	  p++;
171519370Spst      }
171619370Spst}
171798944Sobrien
171819370Spst
171919370Spst/* Print the value in stack frame FRAME of a variable
172019370Spst   specified by a struct symbol.  */
172119370Spst
172219370Spstvoid
172398944Sobrienprint_variable_value (struct symbol *var, struct frame_info *frame,
172498944Sobrien		      struct ui_file *stream)
172519370Spst{
172698944Sobrien  struct value *val = read_var_value (var, frame);
172719370Spst
172819370Spst  value_print (val, stream, 0, Val_pretty_default);
172919370Spst}
173019370Spst
173119370Spststatic void
173298944Sobrienprintf_command (char *arg, int from_tty)
173319370Spst{
1734130803Smarcel  char *f = NULL;
1735130803Smarcel  char *s = arg;
173646283Sdfr  char *string = NULL;
173798944Sobrien  struct value **val_args;
173819370Spst  char *substrings;
173919370Spst  char *current_substring;
174019370Spst  int nargs = 0;
174119370Spst  int allocated_args = 20;
174219370Spst  struct cleanup *old_cleanups;
174319370Spst
174498944Sobrien  val_args = (struct value **) xmalloc (allocated_args
174598944Sobrien					* sizeof (struct value *));
174698944Sobrien  old_cleanups = make_cleanup (free_current_contents, &val_args);
174719370Spst
174819370Spst  if (s == 0)
174919370Spst    error_no_arg ("format-control string and values to print");
175019370Spst
175119370Spst  /* Skip white space before format string */
175298944Sobrien  while (*s == ' ' || *s == '\t')
175398944Sobrien    s++;
175419370Spst
175519370Spst  /* A format string should follow, enveloped in double quotes */
175619370Spst  if (*s++ != '"')
175719370Spst    error ("Bad format string, missing '\"'.");
175819370Spst
175919370Spst  /* Parse the format-control string and copy it into the string STRING,
176019370Spst     processing some kinds of escape sequence.  */
176119370Spst
176219370Spst  f = string = (char *) alloca (strlen (s) + 1);
176319370Spst
176419370Spst  while (*s != '"')
176519370Spst    {
176619370Spst      int c = *s++;
176719370Spst      switch (c)
176819370Spst	{
176919370Spst	case '\0':
177019370Spst	  error ("Bad format string, non-terminated '\"'.");
177119370Spst
177219370Spst	case '\\':
177319370Spst	  switch (c = *s++)
177419370Spst	    {
177519370Spst	    case '\\':
177619370Spst	      *f++ = '\\';
177719370Spst	      break;
177819370Spst	    case 'a':
177919370Spst	      *f++ = '\a';
178019370Spst	      break;
178119370Spst	    case 'b':
178219370Spst	      *f++ = '\b';
178319370Spst	      break;
178419370Spst	    case 'f':
178519370Spst	      *f++ = '\f';
178619370Spst	      break;
178719370Spst	    case 'n':
178819370Spst	      *f++ = '\n';
178919370Spst	      break;
179019370Spst	    case 'r':
179119370Spst	      *f++ = '\r';
179219370Spst	      break;
179319370Spst	    case 't':
179419370Spst	      *f++ = '\t';
179519370Spst	      break;
179619370Spst	    case 'v':
179719370Spst	      *f++ = '\v';
179819370Spst	      break;
179919370Spst	    case '"':
180019370Spst	      *f++ = '"';
180119370Spst	      break;
180219370Spst	    default:
180319370Spst	      /* ??? TODO: handle other escape sequences */
180419370Spst	      error ("Unrecognized escape character \\%c in format string.",
180519370Spst		     c);
180619370Spst	    }
180719370Spst	  break;
180819370Spst
180919370Spst	default:
181019370Spst	  *f++ = c;
181119370Spst	}
181219370Spst    }
181319370Spst
181419370Spst  /* Skip over " and following space and comma.  */
181519370Spst  s++;
181619370Spst  *f++ = '\0';
181798944Sobrien  while (*s == ' ' || *s == '\t')
181898944Sobrien    s++;
181919370Spst
182019370Spst  if (*s != ',' && *s != 0)
182119370Spst    error ("Invalid argument syntax");
182219370Spst
182398944Sobrien  if (*s == ',')
182498944Sobrien    s++;
182598944Sobrien  while (*s == ' ' || *s == '\t')
182698944Sobrien    s++;
182719370Spst
182819370Spst  /* Need extra space for the '\0's.  Doubling the size is sufficient.  */
182919370Spst  substrings = alloca (strlen (string) * 2);
183019370Spst  current_substring = substrings;
183119370Spst
183219370Spst  {
183319370Spst    /* Now scan the string for %-specs and see what kinds of args they want.
183419370Spst       argclass[I] classifies the %-specs so we can give printf_filtered
183519370Spst       something of the right size.  */
183619370Spst
183798944Sobrien    enum argclass
183898944Sobrien      {
183998944Sobrien	no_arg, int_arg, string_arg, double_arg, long_long_arg
184098944Sobrien      };
184119370Spst    enum argclass *argclass;
184219370Spst    enum argclass this_argclass;
184319370Spst    char *last_arg;
184419370Spst    int nargs_wanted;
184519370Spst    int lcount;
184619370Spst    int i;
184719370Spst
184819370Spst    argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
184919370Spst    nargs_wanted = 0;
185019370Spst    f = string;
185119370Spst    last_arg = string;
185219370Spst    while (*f)
185319370Spst      if (*f++ == '%')
185419370Spst	{
185519370Spst	  lcount = 0;
185698944Sobrien	  while (strchr ("0123456789.hlL-+ #", *f))
185719370Spst	    {
185819370Spst	      if (*f == 'l' || *f == 'L')
185919370Spst		lcount++;
186019370Spst	      f++;
186119370Spst	    }
186219370Spst	  switch (*f)
186319370Spst	    {
186419370Spst	    case 's':
186519370Spst	      this_argclass = string_arg;
186619370Spst	      break;
186719370Spst
186819370Spst	    case 'e':
186919370Spst	    case 'f':
187019370Spst	    case 'g':
187119370Spst	      this_argclass = double_arg;
187219370Spst	      break;
187319370Spst
187419370Spst	    case '*':
187519370Spst	      error ("`*' not supported for precision or width in printf");
187619370Spst
187719370Spst	    case 'n':
187819370Spst	      error ("Format specifier `n' not supported in printf");
187919370Spst
188019370Spst	    case '%':
188119370Spst	      this_argclass = no_arg;
188219370Spst	      break;
188319370Spst
188419370Spst	    default:
188519370Spst	      if (lcount > 1)
188619370Spst		this_argclass = long_long_arg;
188719370Spst	      else
188819370Spst		this_argclass = int_arg;
188919370Spst	      break;
189019370Spst	    }
189119370Spst	  f++;
189219370Spst	  if (this_argclass != no_arg)
189319370Spst	    {
189419370Spst	      strncpy (current_substring, last_arg, f - last_arg);
189519370Spst	      current_substring += f - last_arg;
189619370Spst	      *current_substring++ = '\0';
189719370Spst	      last_arg = f;
189819370Spst	      argclass[nargs_wanted++] = this_argclass;
189919370Spst	    }
190019370Spst	}
190119370Spst
190219370Spst    /* Now, parse all arguments and evaluate them.
190319370Spst       Store the VALUEs in VAL_ARGS.  */
190419370Spst
190519370Spst    while (*s != '\0')
190619370Spst      {
190719370Spst	char *s1;
190819370Spst	if (nargs == allocated_args)
190998944Sobrien	  val_args = (struct value **) xrealloc ((char *) val_args,
191098944Sobrien						 (allocated_args *= 2)
191198944Sobrien						 * sizeof (struct value *));
191219370Spst	s1 = s;
191319370Spst	val_args[nargs] = parse_to_comma_and_eval (&s1);
191498944Sobrien
191519370Spst	/* If format string wants a float, unchecked-convert the value to
191619370Spst	   floating point of the same size */
191798944Sobrien
191819370Spst	if (argclass[nargs] == double_arg)
191919370Spst	  {
192019370Spst	    struct type *type = VALUE_TYPE (val_args[nargs]);
192119370Spst	    if (TYPE_LENGTH (type) == sizeof (float))
192298944Sobrien	        VALUE_TYPE (val_args[nargs]) = builtin_type_float;
192319370Spst	    if (TYPE_LENGTH (type) == sizeof (double))
192498944Sobrien	        VALUE_TYPE (val_args[nargs]) = builtin_type_double;
192519370Spst	  }
192619370Spst	nargs++;
192719370Spst	s = s1;
192819370Spst	if (*s == ',')
192919370Spst	  s++;
193019370Spst      }
193198944Sobrien
193219370Spst    if (nargs != nargs_wanted)
193319370Spst      error ("Wrong number of arguments for specified format-string");
193419370Spst
193519370Spst    /* Now actually print them.  */
193619370Spst    current_substring = substrings;
193719370Spst    for (i = 0; i < nargs; i++)
193819370Spst      {
193919370Spst	switch (argclass[i])
194019370Spst	  {
194119370Spst	  case string_arg:
194219370Spst	    {
194319370Spst	      char *str;
194419370Spst	      CORE_ADDR tem;
194519370Spst	      int j;
194698944Sobrien	      tem = value_as_address (val_args[i]);
194719370Spst
194819370Spst	      /* This is a %s argument.  Find the length of the string.  */
194998944Sobrien	      for (j = 0;; j++)
195019370Spst		{
195119370Spst		  char c;
195219370Spst		  QUIT;
195398944Sobrien		  read_memory (tem + j, &c, 1);
195419370Spst		  if (c == 0)
195519370Spst		    break;
195619370Spst		}
195719370Spst
195819370Spst	      /* Copy the string contents into a string inside GDB.  */
195919370Spst	      str = (char *) alloca (j + 1);
196098944Sobrien	      if (j != 0)
196198944Sobrien		read_memory (tem, str, j);
196219370Spst	      str[j] = 0;
196319370Spst
196419370Spst	      printf_filtered (current_substring, str);
196519370Spst	    }
196619370Spst	    break;
196719370Spst	  case double_arg:
196819370Spst	    {
196919370Spst	      double val = value_as_double (val_args[i]);
197019370Spst	      printf_filtered (current_substring, val);
197119370Spst	      break;
197219370Spst	    }
197319370Spst	  case long_long_arg:
197419370Spst#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
197519370Spst	    {
197619370Spst	      long long val = value_as_long (val_args[i]);
197719370Spst	      printf_filtered (current_substring, val);
197819370Spst	      break;
197919370Spst	    }
198019370Spst#else
198119370Spst	    error ("long long not supported in printf");
198219370Spst#endif
198319370Spst	  case int_arg:
198419370Spst	    {
198519370Spst	      /* FIXME: there should be separate int_arg and long_arg.  */
198619370Spst	      long val = value_as_long (val_args[i]);
198719370Spst	      printf_filtered (current_substring, val);
198819370Spst	      break;
198919370Spst	    }
199098944Sobrien	  default:		/* purecov: deadcode */
199198944Sobrien	    error ("internal error in printf_command");		/* purecov: deadcode */
199219370Spst	  }
199319370Spst	/* Skip to the next substring.  */
199419370Spst	current_substring += strlen (current_substring) + 1;
199519370Spst      }
199619370Spst    /* Print the portion of the format string after the last argument.  */
1997130803Smarcel    puts_filtered (last_arg);
199819370Spst  }
199919370Spst  do_cleanups (old_cleanups);
200019370Spst}
200119370Spst
200219370Spstvoid
200398944Sobrien_initialize_printcmd (void)
200419370Spst{
200598944Sobrien  struct cmd_list_element *c;
200698944Sobrien
200719370Spst  current_display_number = -1;
200819370Spst
200919370Spst  add_info ("address", address_info,
201098944Sobrien	    "Describe where symbol SYM is stored.");
201119370Spst
201298944Sobrien  add_info ("symbol", sym_info,
201346283Sdfr	    "Describe what symbol is at location ADDR.\n\
201446283SdfrOnly for symbols with fixed locations (global or static scope).");
201546283Sdfr
201619370Spst  add_com ("x", class_vars, x_command,
201719370Spst	   concat ("Examine memory: x/FMT ADDRESS.\n\
201819370SpstADDRESS is an expression for the memory address to examine.\n\
201919370SpstFMT is a repeat count followed by a format letter and a size letter.\n\
202019370SpstFormat letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
202119370Spst  t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n",
202298944Sobrien		   "Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
202319370SpstThe specified number of objects of the specified size are printed\n\
202419370Spstaccording to the format.\n\n\
202519370SpstDefaults for format and size letters are those previously used.\n\
202619370SpstDefault count is 1.  Default address is following last thing printed\n\
202719370Spstwith this command or \"print\".", NULL));
202819370Spst
202919370Spst#if 0
203019370Spst  add_com ("whereis", class_vars, whereis_command,
203119370Spst	   "Print line number and file of definition of variable.");
203219370Spst#endif
203398944Sobrien
203419370Spst  add_info ("display", display_info,
203519370Spst	    "Expressions to display when program stops, with code numbers.");
203619370Spst
203719370Spst  add_cmd ("undisplay", class_vars, undisplay_command,
203819370Spst	   "Cancel some expressions to be displayed when program stops.\n\
203919370SpstArguments are the code numbers of the expressions to stop displaying.\n\
204019370SpstNo argument means cancel all automatic-display expressions.\n\
204119370Spst\"delete display\" has the same effect as this command.\n\
204219370SpstDo \"info display\" to see current list of code numbers.",
204398944Sobrien	   &cmdlist);
204419370Spst
204519370Spst  add_com ("display", class_vars, display_command,
204619370Spst	   "Print value of expression EXP each time the program stops.\n\
204719370Spst/FMT may be used before EXP as in the \"print\" command.\n\
204819370Spst/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
204919370Spstas in the \"x\" command, and then EXP is used to get the address to examine\n\
205019370Spstand examining is done as in the \"x\" command.\n\n\
205119370SpstWith no argument, display all currently requested auto-display expressions.\n\
205219370SpstUse \"undisplay\" to cancel display requests previously made."
205398944Sobrien    );
205419370Spst
205598944Sobrien  add_cmd ("display", class_vars, enable_display,
205619370Spst	   "Enable some expressions to be displayed when program stops.\n\
205719370SpstArguments are the code numbers of the expressions to resume displaying.\n\
205819370SpstNo argument means enable all automatic-display expressions.\n\
205919370SpstDo \"info display\" to see current list of code numbers.", &enablelist);
206019370Spst
206198944Sobrien  add_cmd ("display", class_vars, disable_display_command,
206219370Spst	   "Disable some expressions to be displayed when program stops.\n\
206319370SpstArguments are the code numbers of the expressions to stop displaying.\n\
206419370SpstNo argument means disable all automatic-display expressions.\n\
206519370SpstDo \"info display\" to see current list of code numbers.", &disablelist);
206619370Spst
206798944Sobrien  add_cmd ("display", class_vars, undisplay_command,
206819370Spst	   "Cancel some expressions to be displayed when program stops.\n\
206919370SpstArguments are the code numbers of the expressions to stop displaying.\n\
207019370SpstNo argument means cancel all automatic-display expressions.\n\
207119370SpstDo \"info display\" to see current list of code numbers.", &deletelist);
207219370Spst
207319370Spst  add_com ("printf", class_vars, printf_command,
207498944Sobrien	   "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
207519370SpstThis is useful for formatted output in user-defined commands.");
207619370Spst
207719370Spst  add_com ("output", class_vars, output_command,
207819370Spst	   "Like \"print\" but don't put in value history and don't print newline.\n\
207919370SpstThis is useful in user-defined commands.");
208019370Spst
208119370Spst  add_prefix_cmd ("set", class_vars, set_command,
208298944Sobrien		  concat ("Evaluate expression EXP and assign result to variable VAR, using assignment\n\
208319370Spstsyntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
208419370Spstexample).  VAR may be a debugger \"convenience\" variable (names starting\n\
208519370Spstwith $), a register (a few standard names starting with $), or an actual\n\
208619370Spstvariable in the program being debugged.  EXP is any valid expression.\n",
208798944Sobrien			  "Use \"set variable\" for variables with names identical to set subcommands.\n\
208819370Spst\nWith a subcommand, this command modifies parts of the gdb environment.\n\
208919370SpstYou can see these environment settings with the \"show\" command.", NULL),
209098944Sobrien		  &setlist, "set ", 1, &cmdlist);
209146283Sdfr  if (dbx_commands)
209298944Sobrien    add_com ("assign", class_vars, set_command, concat ("Evaluate expression \
209346283SdfrEXP and assign result to variable VAR, using assignment\n\
209446283Sdfrsyntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
209546283Sdfrexample).  VAR may be a debugger \"convenience\" variable (names starting\n\
209646283Sdfrwith $), a register (a few standard names starting with $), or an actual\n\
209746283Sdfrvariable in the program being debugged.  EXP is any valid expression.\n",
209898944Sobrien							"Use \"set variable\" for variables with names identical to set subcommands.\n\
209946283Sdfr\nWith a subcommand, this command modifies parts of the gdb environment.\n\
210046283SdfrYou can see these environment settings with the \"show\" command.", NULL));
210119370Spst
210219370Spst  /* "call" is the same as "set", but handy for dbx users to call fns. */
210398944Sobrien  c = add_com ("call", class_vars, call_command,
210498944Sobrien	       "Call a function in the program.\n\
210519370SpstThe argument is the function name and arguments, in the notation of the\n\
210619370Spstcurrent working language.  The result is printed and saved in the value\n\
210719370Spsthistory, if it is not void.");
2108130803Smarcel  set_cmd_completer (c, location_completer);
210919370Spst
211019370Spst  add_cmd ("variable", class_vars, set_command,
211198944Sobrien	   "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
211219370Spstsyntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
211319370Spstexample).  VAR may be a debugger \"convenience\" variable (names starting\n\
211419370Spstwith $), a register (a few standard names starting with $), or an actual\n\
211519370Spstvariable in the program being debugged.  EXP is any valid expression.\n\
211619370SpstThis may usually be abbreviated to simply \"set\".",
211798944Sobrien	   &setlist);
211819370Spst
211998944Sobrien  c = add_com ("print", class_vars, print_command,
212019370Spst	   concat ("Print value of expression EXP.\n\
212119370SpstVariables accessible are those of the lexical environment of the selected\n\
212219370Spststack frame, plus all those whose scope is global or an entire file.\n\
212319370Spst\n\
212419370Spst$NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
212519370Spst$$NUM refers to NUM'th value back from the last one.\n\
212619370SpstNames starting with $ refer to registers (with the values they would have\n",
212798944Sobrien		   "if the program were to return to the stack frame now selected, restoring\n\
212819370Spstall registers saved by frames farther in) or else to debugger\n\
212919370Spst\"convenience\" variables (any such name not a known register).\n\
213019370SpstUse assignment expressions to give values to convenience variables.\n",
213119370Spst		   "\n\
213219370Spst{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
213319370Spst@ is a binary operator for treating consecutive data objects\n\
213419370Spstanywhere in memory as an array.  FOO@NUM gives an array whose first\n\
213519370Spstelement is FOO, whose second element is stored in the space following\n\
213619370Spstwhere FOO is stored, etc.  FOO must be an expression whose value\n\
213719370Spstresides in memory.\n",
213819370Spst		   "\n\
213919370SpstEXP may be preceded with /FMT, where FMT is a format letter\n\
214019370Spstbut no count or size letter (see \"x\" command).", NULL));
2141130803Smarcel  set_cmd_completer (c, location_completer);
214219370Spst  add_com_alias ("p", "print", class_vars, 1);
214319370Spst
214498944Sobrien  c = add_com ("inspect", class_vars, inspect_command,
214598944Sobrien	   "Same as \"print\" command, except that if you are running in the epoch\n\
214619370Spstenvironment, the value is printed in its own window.");
2147130803Smarcel  set_cmd_completer (c, location_completer);
214819370Spst
214919370Spst  add_show_from_set (
215098944Sobrien		 add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,
215198944Sobrien			      (char *) &max_symbolic_offset,
215298944Sobrien       "Set the largest offset that will be printed in <symbol+1234> form.",
215398944Sobrien			      &setprintlist),
215498944Sobrien		      &showprintlist);
215519370Spst  add_show_from_set (
215698944Sobrien		      add_set_cmd ("symbol-filename", no_class, var_boolean,
215798944Sobrien				   (char *) &print_symbol_filename,
215898944Sobrien	   "Set printing of source filename and line number with <symbol>.",
215998944Sobrien				   &setprintlist),
216098944Sobrien		      &showprintlist);
216119370Spst
216246283Sdfr  /* For examine/instruction a single byte quantity is specified as
216346283Sdfr     the data.  This avoids problems with value_at_lazy() requiring a
216446283Sdfr     valid data type (and rejecting VOID). */
216546283Sdfr  examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
216646283Sdfr
216719370Spst  examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
216819370Spst  examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
216919370Spst  examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
217019370Spst  examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
217146283Sdfr
217219370Spst}
2173