stack.c revision 98944
119370Spst/* Print and select stack frames for GDB, the GNU debugger.
219370Spst
398944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
498944Sobrien   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 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
2446283Sdfr#include <ctype.h>
2519370Spst#include "defs.h"
2619370Spst#include "gdb_string.h"
2719370Spst#include "value.h"
2819370Spst#include "symtab.h"
2919370Spst#include "gdbtypes.h"
3019370Spst#include "expression.h"
3119370Spst#include "language.h"
3219370Spst#include "frame.h"
3319370Spst#include "gdbcmd.h"
3419370Spst#include "gdbcore.h"
3519370Spst#include "target.h"
3619370Spst#include "breakpoint.h"
3719370Spst#include "demangle.h"
3819370Spst#include "inferior.h"
3919370Spst#include "annotate.h"
4098944Sobrien#include "ui-out.h"
4119370Spst
4246283Sdfr/* Prototypes for exported functions. */
4346283Sdfr
4498944Sobrienvoid args_info (char *, int);
4546283Sdfr
4698944Sobrienvoid locals_info (char *, int);
4746283Sdfr
4898944Sobrienvoid (*selected_frame_level_changed_hook) (int);
4946283Sdfr
5098944Sobrienvoid _initialize_stack (void);
5146283Sdfr
5246283Sdfr/* Prototypes for local functions. */
5346283Sdfr
5498944Sobrienstatic void return_command (char *, int);
5519370Spst
5698944Sobrienstatic void down_command (char *, int);
5719370Spst
5898944Sobrienstatic void down_silently_base (char *);
5946283Sdfr
6098944Sobrienstatic void down_silently_command (char *, int);
6119370Spst
6298944Sobrienstatic void up_command (char *, int);
6319370Spst
6498944Sobrienstatic void up_silently_base (char *);
6546283Sdfr
6698944Sobrienstatic void up_silently_command (char *, int);
6719370Spst
6898944Sobrienvoid frame_command (char *, int);
6919370Spst
7098944Sobrienstatic void current_frame_command (char *, int);
7119370Spst
7298944Sobrienstatic void select_frame_command (char *, int);
7319370Spst
7498944Sobrienstatic void print_frame_arg_vars (struct frame_info *, struct ui_file *);
7519370Spst
7698944Sobrienstatic void catch_info (char *, int);
7719370Spst
7898944Sobrienstatic void args_plus_locals_info (char *, int);
7919370Spst
8098944Sobrienstatic void print_frame_label_vars (struct frame_info *, int,
8198944Sobrien				    struct ui_file *);
8219370Spst
8398944Sobrienstatic void print_frame_local_vars (struct frame_info *, int,
8498944Sobrien				    struct ui_file *);
8519370Spst
8698944Sobrienstatic int print_block_frame_labels (struct block *, int *,
8798944Sobrien				     struct ui_file *);
8819370Spst
8998944Sobrienstatic int print_block_frame_locals (struct block *,
9098944Sobrien				     struct frame_info *,
9198944Sobrien				     int,
9298944Sobrien				     struct ui_file *);
9346283Sdfr
9498944Sobrienstatic void print_frame (struct frame_info *fi,
9598944Sobrien			 int level,
9698944Sobrien			 int source,
9798944Sobrien			 int args,
9898944Sobrien			 struct symtab_and_line sal);
9919370Spst
10098944Sobrienstatic void print_frame_info_base (struct frame_info *, int, int, int);
10119370Spst
10298944Sobrienstatic void print_stack_frame_base (struct frame_info *, int, int);
10319370Spst
10498944Sobrienstatic void backtrace_command (char *, int);
10598944Sobrien
10698944Sobrienstruct frame_info *parse_frame_specification (char *);
10798944Sobrien
10898944Sobrienstatic void frame_info (char *, int);
10998944Sobrien
11019370Spstextern int addressprint;	/* Print addresses, or stay symbolic only? */
11119370Spstextern int lines_to_list;	/* # of lines "list" command shows by default */
11219370Spst
11319370Spst/* The "selected" stack frame is used by default for local and arg access.
11419370Spst   May be zero, for no selected frame.  */
11519370Spst
11619370Spststruct frame_info *selected_frame;
11719370Spst
11819370Spst/* Level of the selected frame:
11919370Spst   0 for innermost, 1 for its caller, ...
12019370Spst   or -1 for frame specified by address with no defined level.  */
12119370Spst
12219370Spstint selected_frame_level;
12319370Spst
12419370Spst/* Zero means do things normally; we are interacting directly with the
12519370Spst   user.  One means print the full filename and linenumber when a
12619370Spst   frame is printed, and do so in a format emacs18/emacs19.22 can
12719370Spst   parse.  Two means print similar annotations, but in many more
12819370Spst   cases and in a slightly different syntax.  */
12919370Spst
13019370Spstint annotation_level = 0;
13119370Spst
13219370Spst
13398944Sobrienstruct print_stack_frame_args
13498944Sobrien  {
13598944Sobrien    struct frame_info *fi;
13698944Sobrien    int level;
13798944Sobrien    int source;
13898944Sobrien    int args;
13998944Sobrien  };
14019370Spst
14198944Sobrienstatic int print_stack_frame_base_stub (char *);
14298944Sobrien
14346283Sdfr/* Show and print the frame arguments.
14446283Sdfr   Pass the args the way catch_errors wants them.  */
14598944Sobrienstatic int show_and_print_stack_frame_stub (void *args);
14619370Spststatic int
14798944Sobrienshow_and_print_stack_frame_stub (void *args)
14819370Spst{
14998944Sobrien  struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
15019370Spst
15119370Spst  print_frame_info (p->fi, p->level, p->source, p->args);
15246283Sdfr
15319370Spst  return 0;
15419370Spst}
15519370Spst
15646283Sdfr/* Show or print the frame arguments.
15746283Sdfr   Pass the args the way catch_errors wants them.  */
15898944Sobrienstatic int print_stack_frame_stub (void *args);
15946283Sdfrstatic int
16098944Sobrienprint_stack_frame_stub (void *args)
16146283Sdfr{
16298944Sobrien  struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
16346283Sdfr
16498944Sobrien  print_frame_info_base (p->fi, p->level, p->source, p->args);
16546283Sdfr  return 0;
16646283Sdfr}
16746283Sdfr
16819370Spst/* Print a stack frame briefly.  FRAME_INFI should be the frame info
16946283Sdfr   and LEVEL should be its level in the stack (or -1 for level not
17046283Sdfr   defined). */
17146283Sdfr
17246283Sdfr/* Pass the args the way catch_errors wants them.  */
17346283Sdfrstatic int
17498944Sobrienprint_stack_frame_base_stub (char *args)
17546283Sdfr{
17698944Sobrien  struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
17746283Sdfr
17846283Sdfr  print_frame_info_base (p->fi, p->level, p->source, p->args);
17946283Sdfr  return 0;
18046283Sdfr}
18146283Sdfr
18246283Sdfr/* print the frame arguments to the terminal.
18346283Sdfr   Pass the args the way catch_errors wants them.  */
18498944Sobrienstatic int print_only_stack_frame_stub (void *);
18546283Sdfrstatic int
18698944Sobrienprint_only_stack_frame_stub (void *args)
18746283Sdfr{
18898944Sobrien  struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
18946283Sdfr
19046283Sdfr  print_frame_info_base (p->fi, p->level, p->source, p->args);
19146283Sdfr  return 0;
19246283Sdfr}
19346283Sdfr
19446283Sdfr/* Print a stack frame briefly.  FRAME_INFI should be the frame info
19519370Spst   and LEVEL should be its level in the stack (or -1 for level not defined).
19619370Spst   This prints the level, the function executing, the arguments,
19719370Spst   and the file name and line number.
19819370Spst   If the pc is not at the beginning of the source line,
19919370Spst   the actual pc is printed at the beginning.
20019370Spst
20119370Spst   If SOURCE is 1, print the source line as well.
20219370Spst   If SOURCE is -1, print ONLY the source line.  */
20319370Spst
20446283Sdfrstatic void
20598944Sobrienprint_stack_frame_base (struct frame_info *fi, int level, int source)
20646283Sdfr{
20746283Sdfr  struct print_stack_frame_args args;
20846283Sdfr
20946283Sdfr  args.fi = fi;
21046283Sdfr  args.level = level;
21146283Sdfr  args.source = source;
21246283Sdfr  args.args = 1;
21346283Sdfr
21498944Sobrien  catch_errors (print_stack_frame_stub, &args, "", RETURN_MASK_ALL);
21546283Sdfr}
21646283Sdfr
21746283Sdfr/* Show and print a stack frame briefly.  FRAME_INFI should be the frame info
21846283Sdfr   and LEVEL should be its level in the stack (or -1 for level not defined).
21946283Sdfr   This prints the level, the function executing, the arguments,
22046283Sdfr   and the file name and line number.
22146283Sdfr   If the pc is not at the beginning of the source line,
22246283Sdfr   the actual pc is printed at the beginning.
22346283Sdfr
22446283Sdfr   If SOURCE is 1, print the source line as well.
22546283Sdfr   If SOURCE is -1, print ONLY the source line.  */
22646283Sdfr
22719370Spstvoid
22898944Sobrienshow_and_print_stack_frame (struct frame_info *fi, int level, int source)
22946283Sdfr{
23046283Sdfr  struct print_stack_frame_args args;
23146283Sdfr
23246283Sdfr  args.fi = fi;
23346283Sdfr  args.level = level;
23446283Sdfr  args.source = source;
23546283Sdfr  args.args = 1;
23646283Sdfr
23798944Sobrien  catch_errors (show_and_print_stack_frame_stub, &args, "", RETURN_MASK_ALL);
23846283Sdfr}
23946283Sdfr
24046283Sdfr
24146283Sdfr/* Show or print a stack frame briefly.  FRAME_INFI should be the frame info
24246283Sdfr   and LEVEL should be its level in the stack (or -1 for level not defined).
24346283Sdfr   This prints the level, the function executing, the arguments,
24446283Sdfr   and the file name and line number.
24546283Sdfr   If the pc is not at the beginning of the source line,
24646283Sdfr   the actual pc is printed at the beginning.
24746283Sdfr
24846283Sdfr   If SOURCE is 1, print the source line as well.
24946283Sdfr   If SOURCE is -1, print ONLY the source line.  */
25046283Sdfr
25146283Sdfrvoid
25298944Sobrienprint_stack_frame (struct frame_info *fi, int level, int source)
25319370Spst{
25419370Spst  struct print_stack_frame_args args;
25519370Spst
25619370Spst  args.fi = fi;
25719370Spst  args.level = level;
25819370Spst  args.source = source;
25919370Spst  args.args = 1;
26019370Spst
26198944Sobrien  catch_errors (print_stack_frame_stub, (char *) &args, "", RETURN_MASK_ALL);
26219370Spst}
26319370Spst
26446283Sdfr/* Print a stack frame briefly.  FRAME_INFI should be the frame info
26546283Sdfr   and LEVEL should be its level in the stack (or -1 for level not defined).
26646283Sdfr   This prints the level, the function executing, the arguments,
26746283Sdfr   and the file name and line number.
26846283Sdfr   If the pc is not at the beginning of the source line,
26946283Sdfr   the actual pc is printed at the beginning.
27046283Sdfr
27146283Sdfr   If SOURCE is 1, print the source line as well.
27246283Sdfr   If SOURCE is -1, print ONLY the source line.  */
27346283Sdfr
27446283Sdfrvoid
27598944Sobrienprint_only_stack_frame (struct frame_info *fi, int level, int source)
27646283Sdfr{
27746283Sdfr  struct print_stack_frame_args args;
27846283Sdfr
27946283Sdfr  args.fi = fi;
28046283Sdfr  args.level = level;
28146283Sdfr  args.source = source;
28246283Sdfr  args.args = 1;
28346283Sdfr
28498944Sobrien  catch_errors (print_only_stack_frame_stub, &args, "", RETURN_MASK_ALL);
28546283Sdfr}
28646283Sdfr
28798944Sobrienstruct print_args_args
28898944Sobrien{
28919370Spst  struct symbol *func;
29019370Spst  struct frame_info *fi;
29198944Sobrien  struct ui_file *stream;
29219370Spst};
29319370Spst
29498944Sobrienstatic int print_args_stub (PTR);
29519370Spst
29619370Spst/* Pass the args the way catch_errors wants them.  */
29719370Spst
29819370Spststatic int
29998944Sobrienprint_args_stub (PTR args)
30019370Spst{
30119370Spst  int numargs;
30298944Sobrien  struct print_args_args *p = (struct print_args_args *) args;
30319370Spst
30498944Sobrien  numargs = FRAME_NUM_ARGS (p->fi);
30598944Sobrien  print_frame_args (p->func, p->fi, numargs, p->stream);
30619370Spst  return 0;
30719370Spst}
30819370Spst
30946283Sdfr/* Print information about a frame for frame "fi" at level "level".
31098944Sobrien   Used in "where" output, also used to emit breakpoint or step
31198944Sobrien   messages.
31298944Sobrien   LEVEL is the level of the frame, or -1 if it is the
31398944Sobrien   innermost frame but we don't want to print the level.
31498944Sobrien   The meaning of the SOURCE argument is:
31598944Sobrien   SRC_LINE: Print only source line
31698944Sobrien   LOCATION: Print only location
31798944Sobrien   LOC_AND_SRC: Print location and source line.  */
31819370Spst
31946283Sdfrstatic void
32098944Sobrienprint_frame_info_base (struct frame_info *fi, int level, int source, int args)
32119370Spst{
32219370Spst  struct symtab_and_line sal;
32398944Sobrien  int source_print;
32498944Sobrien  int location_print;
32519370Spst
32619370Spst#if 0
32719370Spst  char buf[MAX_REGISTER_RAW_SIZE];
32819370Spst  CORE_ADDR sp;
32919370Spst
33019370Spst  /* On the 68k, this spends too much time in m68k_find_saved_regs.  */
33119370Spst
33219370Spst  /* Get the value of SP_REGNUM relative to the frame.  */
33398944Sobrien  get_saved_register (buf, (int *) NULL, (CORE_ADDR *) NULL,
33498944Sobrien		    FRAME_INFO_ID (fi), SP_REGNUM, (enum lval_type *) NULL);
33519370Spst  sp = extract_address (buf, REGISTER_RAW_SIZE (SP_REGNUM));
33619370Spst
33719370Spst  /* This is not a perfect test, because if a function alloca's some
33819370Spst     memory, puts some code there, and then jumps into it, then the test
33919370Spst     will succeed even though there is no call dummy.  Probably best is
34019370Spst     to check for a bp_call_dummy breakpoint.  */
34119370Spst  if (PC_IN_CALL_DUMMY (fi->pc, sp, fi->frame))
34219370Spst#else
34319370Spst  if (frame_in_dummy (fi))
34419370Spst#endif
34519370Spst    {
34619370Spst      annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
34719370Spst
34819370Spst      /* Do this regardless of SOURCE because we don't have any source
34998944Sobrien         to list for this frame.  */
35019370Spst      if (level >= 0)
35119370Spst	printf_filtered ("#%-2d ", level);
35219370Spst      annotate_function_call ();
35319370Spst      printf_filtered ("<function called from gdb>\n");
35419370Spst      annotate_frame_end ();
35519370Spst      return;
35619370Spst    }
35719370Spst  if (fi->signal_handler_caller)
35819370Spst    {
35919370Spst      annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
36019370Spst
36119370Spst      /* Do this regardless of SOURCE because we don't have any source
36298944Sobrien         to list for this frame.  */
36319370Spst      if (level >= 0)
36419370Spst	printf_filtered ("#%-2d ", level);
36519370Spst      annotate_signal_handler_caller ();
36619370Spst      printf_filtered ("<signal handler called>\n");
36719370Spst      annotate_frame_end ();
36819370Spst      return;
36919370Spst    }
37019370Spst
37119370Spst  /* If fi is not the innermost frame, that normally means that fi->pc
37219370Spst     points to *after* the call instruction, and we want to get the line
37319370Spst     containing the call, never the next line.  But if the next frame is
37419370Spst     a signal_handler_caller or a dummy frame, then the next frame was
37519370Spst     not entered as the result of a call, and we want to get the line
37619370Spst     containing fi->pc.  */
37719370Spst  sal =
37819370Spst    find_pc_line (fi->pc,
37919370Spst		  fi->next != NULL
38019370Spst		  && !fi->next->signal_handler_caller
38119370Spst		  && !frame_in_dummy (fi->next));
38219370Spst
38398944Sobrien  location_print = (source == LOCATION
38498944Sobrien		    || source == LOC_AND_ADDRESS
38598944Sobrien		    || source == SRC_AND_LOC);
38698944Sobrien
38798944Sobrien  if (location_print || !sal.symtab)
38898944Sobrien    print_frame (fi, level, source, args, sal);
38998944Sobrien
39098944Sobrien  source_print = (source == SRC_LINE || source == SRC_AND_LOC);
39198944Sobrien
39298944Sobrien  if (source_print && sal.symtab)
39398944Sobrien    {
39498944Sobrien      int done = 0;
39598944Sobrien      int mid_statement = (source == SRC_LINE) && (fi->pc != sal.pc);
39698944Sobrien
39798944Sobrien      if (annotation_level)
39898944Sobrien	done = identify_source_line (sal.symtab, sal.line, mid_statement,
39998944Sobrien				     fi->pc);
40098944Sobrien      if (!done)
40198944Sobrien	{
40298944Sobrien	  if (print_frame_info_listing_hook)
40398944Sobrien	    {
40498944Sobrien	      print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
40598944Sobrien	      current_source_symtab = sal.symtab;
40698944Sobrien	    }
40798944Sobrien	  else
40898944Sobrien	    {
40998944Sobrien	      /* We used to do this earlier, but that is clearly
41098944Sobrien		 wrong. This function is used by many different
41198944Sobrien		 parts of gdb, including normal_stop in infrun.c,
41298944Sobrien		 which uses this to print out the current PC
41398944Sobrien		 when we stepi/nexti into the middle of a source
41498944Sobrien		 line. Only the command line really wants this
41598944Sobrien		 behavior. Other UIs probably would like the
41698944Sobrien		 ability to decide for themselves if it is desired. */
41798944Sobrien	      if (addressprint && mid_statement)
41898944Sobrien		{
41998944Sobrien		  ui_out_field_core_addr (uiout, "addr", fi->pc);
42098944Sobrien		  ui_out_text (uiout, "\t");
42198944Sobrien		}
42298944Sobrien
42398944Sobrien	      print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
42498944Sobrien	    }
42598944Sobrien	}
42698944Sobrien      current_source_line = max (sal.line - lines_to_list / 2, 1);
42798944Sobrien    }
42898944Sobrien
42998944Sobrien  if (source != 0)
43098944Sobrien    set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
43198944Sobrien
43298944Sobrien  annotate_frame_end ();
43398944Sobrien
43498944Sobrien  gdb_flush (gdb_stdout);
43598944Sobrien}
43698944Sobrien
43798944Sobrienstatic void
43898944Sobrienprint_frame (struct frame_info *fi,
43998944Sobrien	     int level,
44098944Sobrien	     int source,
44198944Sobrien	     int args,
44298944Sobrien	     struct symtab_and_line sal)
44398944Sobrien{
44498944Sobrien  struct symbol *func;
44598944Sobrien  register char *funname = 0;
44698944Sobrien  enum language funlang = language_unknown;
44798944Sobrien  struct ui_stream *stb;
44898944Sobrien  struct cleanup *old_chain;
44998944Sobrien  struct cleanup *list_chain;
45098944Sobrien
45198944Sobrien  stb = ui_out_stream_new (uiout);
45298944Sobrien  old_chain = make_cleanup_ui_out_stream_delete (stb);
45398944Sobrien
45419370Spst  func = find_pc_function (fi->pc);
45519370Spst  if (func)
45619370Spst    {
45719370Spst      /* In certain pathological cases, the symtabs give the wrong
45898944Sobrien         function (when we are in the first function in a file which
45998944Sobrien         is compiled without debugging symbols, the previous function
46098944Sobrien         is compiled with debugging symbols, and the "foo.o" symbol
46198944Sobrien         that is supposed to tell us where the file with debugging symbols
46298944Sobrien         ends has been truncated by ar because it is longer than 15
46398944Sobrien         characters).  This also occurs if the user uses asm() to create
46498944Sobrien         a function but not stabs for it (in a file compiled -g).
46519370Spst
46698944Sobrien         So look in the minimal symbol tables as well, and if it comes
46798944Sobrien         up with a larger address for the function use that instead.
46898944Sobrien         I don't think this can ever cause any problems; there shouldn't
46998944Sobrien         be any minimal symbols in the middle of a function; if this is
47098944Sobrien         ever changed many parts of GDB will need to be changed (and we'll
47198944Sobrien         create a find_pc_minimal_function or some such).  */
47219370Spst
47319370Spst      struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
47419370Spst      if (msymbol != NULL
47598944Sobrien	  && (SYMBOL_VALUE_ADDRESS (msymbol)
47619370Spst	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
47719370Spst	{
47819370Spst#if 0
47919370Spst	  /* There is no particular reason to think the line number
48019370Spst	     information is wrong.  Someone might have just put in
48119370Spst	     a label with asm() but left the line numbers alone.  */
48219370Spst	  /* In this case we have no way of knowing the source file
48319370Spst	     and line number, so don't print them.  */
48419370Spst	  sal.symtab = 0;
48519370Spst#endif
48619370Spst	  /* We also don't know anything about the function besides
48719370Spst	     its address and name.  */
48819370Spst	  func = 0;
48919370Spst	  funname = SYMBOL_NAME (msymbol);
49019370Spst	  funlang = SYMBOL_LANGUAGE (msymbol);
49119370Spst	}
49219370Spst      else
49319370Spst	{
49498944Sobrien	  /* I'd like to use SYMBOL_SOURCE_NAME() here, to display the
49598944Sobrien	     demangled name that we already have stored in the symbol
49698944Sobrien	     table, but we stored a version with DMGL_PARAMS turned
49798944Sobrien	     on, and here we don't want to display parameters. So call
49898944Sobrien	     the demangler again, with DMGL_ANSI only. (Yes, I know
49998944Sobrien	     that printf_symbol_filtered() will again try to demangle
50098944Sobrien	     the name on the fly, but the issue is that if
50198944Sobrien	     cplus_demangle() fails here, it'll fail there too. So we
50298944Sobrien	     want to catch the failure ("demangled==NULL" case below)
50398944Sobrien	     here, while we still have our hands on the function
50498944Sobrien	     symbol.) */
50598944Sobrien	  char *demangled;
50619370Spst	  funname = SYMBOL_NAME (func);
50719370Spst	  funlang = SYMBOL_LANGUAGE (func);
50898944Sobrien	  if (funlang == language_cplus)
50998944Sobrien	    {
51098944Sobrien	      demangled = cplus_demangle (funname, DMGL_ANSI);
51198944Sobrien	      if (demangled == NULL)
51298944Sobrien		/* If the demangler fails, try the demangled name from
51398944Sobrien		   the symbol table. This'll have parameters, but
51498944Sobrien		   that's preferable to diplaying a mangled name. */
51598944Sobrien		funname = SYMBOL_SOURCE_NAME (func);
51698944Sobrien	    }
51719370Spst	}
51819370Spst    }
51919370Spst  else
52019370Spst    {
52146283Sdfr      struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
52246283Sdfr      if (msymbol != NULL)
52319370Spst	{
52446283Sdfr	  funname = SYMBOL_NAME (msymbol);
52546283Sdfr	  funlang = SYMBOL_LANGUAGE (msymbol);
52619370Spst	}
52719370Spst    }
52819370Spst
52998944Sobrien  annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
53019370Spst
53198944Sobrien  list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
53219370Spst
53398944Sobrien  if (level >= 0)
53498944Sobrien    {
53598944Sobrien      ui_out_text (uiout, "#");
53698944Sobrien      ui_out_field_fmt (uiout, "level", "%-2d", level);
53798944Sobrien      ui_out_spaces (uiout, 1);
53819370Spst    }
53998944Sobrien  if (addressprint)
54098944Sobrien    if (fi->pc != sal.pc || !sal.symtab || source == LOC_AND_ADDRESS)
54198944Sobrien      {
54298944Sobrien	annotate_frame_address ();
54398944Sobrien	ui_out_field_core_addr (uiout, "addr", fi->pc);
54498944Sobrien	annotate_frame_address_end ();
54598944Sobrien	ui_out_text (uiout, " in ");
54698944Sobrien      }
54798944Sobrien  annotate_frame_function_name ();
54898944Sobrien  fprintf_symbol_filtered (stb->stream, funname ? funname : "??", funlang,
54998944Sobrien			   DMGL_ANSI);
55098944Sobrien  ui_out_field_stream (uiout, "func", stb);
55198944Sobrien  ui_out_wrap_hint (uiout, "   ");
55298944Sobrien  annotate_frame_args ();
55398944Sobrien
55498944Sobrien  ui_out_text (uiout, " (");
55598944Sobrien  if (args)
55698944Sobrien    {
55798944Sobrien      struct print_args_args args;
55898944Sobrien      struct cleanup *args_list_chain;
55998944Sobrien      args.fi = fi;
56098944Sobrien      args.func = func;
56198944Sobrien      args.stream = gdb_stdout;
56298944Sobrien      args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
56398944Sobrien      catch_errors (print_args_stub, &args, "", RETURN_MASK_ALL);
56498944Sobrien      /* FIXME: args must be a list. If one argument is a string it will
56598944Sobrien		 have " that will not be properly escaped.  */
56698944Sobrien      /* Invoke ui_out_tuple_end.  */
56798944Sobrien      do_cleanups (args_list_chain);
56898944Sobrien      QUIT;
56998944Sobrien    }
57098944Sobrien  ui_out_text (uiout, ")");
57198944Sobrien  if (sal.symtab && sal.symtab->filename)
57298944Sobrien    {
57398944Sobrien      annotate_frame_source_begin ();
57498944Sobrien      ui_out_wrap_hint (uiout, "   ");
57598944Sobrien      ui_out_text (uiout, " at ");
57698944Sobrien      annotate_frame_source_file ();
57798944Sobrien      ui_out_field_string (uiout, "file", sal.symtab->filename);
57898944Sobrien      annotate_frame_source_file_end ();
57998944Sobrien      ui_out_text (uiout, ":");
58098944Sobrien      annotate_frame_source_line ();
58198944Sobrien      ui_out_field_int (uiout, "line", sal.line);
58298944Sobrien      annotate_frame_source_end ();
58398944Sobrien    }
58419370Spst
58598944Sobrien#ifdef PC_SOLIB
58698944Sobrien  if (!funname || (!sal.symtab || !sal.symtab->filename))
58719370Spst    {
58898944Sobrien      char *lib = PC_SOLIB (fi->pc);
58998944Sobrien      if (lib)
59019370Spst	{
59198944Sobrien	  annotate_frame_where ();
59298944Sobrien	  ui_out_wrap_hint (uiout, "  ");
59398944Sobrien	  ui_out_text (uiout, " from ");
59498944Sobrien	  ui_out_field_string (uiout, "from", lib);
59519370Spst	}
59619370Spst    }
59798944Sobrien#endif /* PC_SOLIB */
59819370Spst
59998944Sobrien  /* do_cleanups will call ui_out_tuple_end() for us.  */
60098944Sobrien  do_cleanups (list_chain);
60198944Sobrien  ui_out_text (uiout, "\n");
60298944Sobrien  do_cleanups (old_chain);
60319370Spst}
60446283Sdfr
60519370Spst
60646283Sdfr/* Show or print the frame info.  If this is the tui, it will be shown in
60746283Sdfr   the source display */
60846283Sdfrvoid
60998944Sobrienprint_frame_info (struct frame_info *fi, register int level, int source,
61098944Sobrien		  int args)
61146283Sdfr{
61298944Sobrien  print_frame_info_base (fi, level, source, args);
61346283Sdfr}
61446283Sdfr
61546283Sdfr/* Show the frame info.  If this is the tui, it will be shown in
61646283Sdfr   the source display otherwise, nothing is done */
61746283Sdfrvoid
61898944Sobrienshow_stack_frame (struct frame_info *fi)
61946283Sdfr{
62046283Sdfr}
62198944Sobrien
62246283Sdfr
62319370Spst/* Read a frame specification in whatever the appropriate format is.
62419370Spst   Call error() if the specification is in any way invalid (i.e.
62519370Spst   this function never returns NULL).  */
62619370Spst
62746283Sdfrstruct frame_info *
62898944Sobrienparse_frame_specification (char *frame_exp)
62919370Spst{
63019370Spst  int numargs = 0;
63119370Spst#define	MAXARGS	4
63219370Spst  CORE_ADDR args[MAXARGS];
63398944Sobrien  int level;
63498944Sobrien
63519370Spst  if (frame_exp)
63619370Spst    {
63719370Spst      char *addr_string, *p;
63819370Spst      struct cleanup *tmp_cleanup;
63919370Spst
64098944Sobrien      while (*frame_exp == ' ')
64198944Sobrien	frame_exp++;
64219370Spst
64319370Spst      while (*frame_exp)
64419370Spst	{
64519370Spst	  if (numargs > MAXARGS)
64619370Spst	    error ("Too many args in frame specification");
64719370Spst	  /* Parse an argument.  */
64898944Sobrien	  for (p = frame_exp; *p && *p != ' '; p++)
64919370Spst	    ;
65098944Sobrien	  addr_string = savestring (frame_exp, p - frame_exp);
65119370Spst
65219370Spst	  {
65398944Sobrien	    struct value *vp;
65498944Sobrien
65598944Sobrien	    tmp_cleanup = make_cleanup (xfree, addr_string);
65698944Sobrien
65798944Sobrien	    /* NOTE: we call parse_and_eval and then both
65898944Sobrien	       value_as_long and value_as_address rather than calling
65998944Sobrien	       parse_and_eval_long and parse_and_eval_address because
66098944Sobrien	       of the issue of potential side effects from evaluating
66198944Sobrien	       the expression.  */
66298944Sobrien	    vp = parse_and_eval (addr_string);
66398944Sobrien	    if (numargs == 0)
66498944Sobrien	      level = value_as_long (vp);
66598944Sobrien
66698944Sobrien	    args[numargs++] = value_as_address (vp);
66719370Spst	    do_cleanups (tmp_cleanup);
66819370Spst	  }
66919370Spst
67019370Spst	  /* Skip spaces, move to possible next arg.  */
67198944Sobrien	  while (*p == ' ')
67298944Sobrien	    p++;
67319370Spst	  frame_exp = p;
67419370Spst	}
67519370Spst    }
67619370Spst
67719370Spst  switch (numargs)
67819370Spst    {
67919370Spst    case 0:
68019370Spst      if (selected_frame == NULL)
68119370Spst	error ("No selected frame.");
68219370Spst      return selected_frame;
68319370Spst      /* NOTREACHED */
68419370Spst    case 1:
68519370Spst      {
68619370Spst	struct frame_info *fid =
68798944Sobrien	find_relative_frame (get_current_frame (), &level);
68819370Spst	struct frame_info *tfid;
68919370Spst
69019370Spst	if (level == 0)
69119370Spst	  /* find_relative_frame was successful */
69219370Spst	  return fid;
69319370Spst
69419370Spst	/* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
69519370Spst	   take at least 2 addresses.  It is important to detect this case
69619370Spst	   here so that "frame 100" does not give a confusing error message
69719370Spst	   like "frame specification requires two addresses".  This of course
69819370Spst	   does not solve the "frame 100" problem for machines on which
69919370Spst	   a frame specification can be made with one address.  To solve
70019370Spst	   that, we need a new syntax for a specifying a frame by address.
70119370Spst	   I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
70219370Spst	   two args, etc.), but people might think that is too much typing,
70319370Spst	   so I guess *0x23,0x45 would be a possible alternative (commas
70419370Spst	   really should be used instead of spaces to delimit; using spaces
70519370Spst	   normally works in an expression).  */
70619370Spst#ifdef SETUP_ARBITRARY_FRAME
70719370Spst	error ("No frame %d", args[0]);
70819370Spst#endif
70919370Spst
71019370Spst	/* If (s)he specifies the frame with an address, he deserves what
71119370Spst	   (s)he gets.  Still, give the highest one that matches.  */
71219370Spst
71319370Spst	for (fid = get_current_frame ();
71419370Spst	     fid && fid->frame != args[0];
71519370Spst	     fid = get_prev_frame (fid))
71619370Spst	  ;
71719370Spst
71819370Spst	if (fid)
71919370Spst	  while ((tfid = get_prev_frame (fid)) &&
72019370Spst		 (tfid->frame == args[0]))
72119370Spst	    fid = tfid;
72298944Sobrien
72319370Spst	/* We couldn't identify the frame as an existing frame, but
72419370Spst	   perhaps we can create one with a single argument.  */
72519370Spst      }
72619370Spst
72798944Sobrien    default:
72819370Spst#ifdef SETUP_ARBITRARY_FRAME
72919370Spst      return SETUP_ARBITRARY_FRAME (numargs, args);
73019370Spst#else
73119370Spst      /* Usual case.  Do it here rather than have everyone supply
73298944Sobrien         a SETUP_ARBITRARY_FRAME that does this.  */
73319370Spst      if (numargs == 1)
73419370Spst	return create_new_frame (args[0], 0);
73519370Spst      error ("Too many args in frame specification");
73619370Spst#endif
73719370Spst      /* NOTREACHED */
73819370Spst    }
73919370Spst  /* NOTREACHED */
74019370Spst}
74119370Spst
74219370Spst/* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
74319370Spst   that if it is unsure about the answer, it returns 0
74419370Spst   instead of guessing (this happens on the VAX and i960, for example).
74519370Spst
74619370Spst   On most machines, we never have to guess about the args address,
74719370Spst   so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
74819370Spst#if !defined (FRAME_ARGS_ADDRESS_CORRECT)
74919370Spst#define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
75019370Spst#endif
75119370Spst
75219370Spst/* Print verbosely the selected frame or the frame at address ADDR.
75319370Spst   This means absolutely all information in the frame is printed.  */
75419370Spst
75519370Spststatic void
75698944Sobrienframe_info (char *addr_exp, int from_tty)
75719370Spst{
75819370Spst  struct frame_info *fi;
75919370Spst  struct symtab_and_line sal;
76019370Spst  struct symbol *func;
76119370Spst  struct symtab *s;
76219370Spst  struct frame_info *calling_frame_info;
76319370Spst  int i, count, numregs;
76419370Spst  char *funname = 0;
76519370Spst  enum language funlang = language_unknown;
76619370Spst
76719370Spst  if (!target_has_stack)
76819370Spst    error ("No stack.");
76919370Spst
77019370Spst  fi = parse_frame_specification (addr_exp);
77119370Spst  if (fi == NULL)
77219370Spst    error ("Invalid frame specified.");
77319370Spst
77419370Spst  sal = find_pc_line (fi->pc,
77519370Spst		      fi->next != NULL
77619370Spst		      && !fi->next->signal_handler_caller
77719370Spst		      && !frame_in_dummy (fi->next));
77819370Spst  func = get_frame_function (fi);
77998944Sobrien  s = find_pc_symtab (fi->pc);
78019370Spst  if (func)
78119370Spst    {
78246283Sdfr      /* I'd like to use SYMBOL_SOURCE_NAME() here, to display
78346283Sdfr       * the demangled name that we already have stored in
78446283Sdfr       * the symbol table, but we stored a version with
78546283Sdfr       * DMGL_PARAMS turned on, and here we don't want
78646283Sdfr       * to display parameters. So call the demangler again,
78746283Sdfr       * with DMGL_ANSI only. RT
78846283Sdfr       * (Yes, I know that printf_symbol_filtered() will
78946283Sdfr       * again try to demangle the name on the fly, but
79046283Sdfr       * the issue is that if cplus_demangle() fails here,
79146283Sdfr       * it'll fail there too. So we want to catch the failure
79246283Sdfr       * ("demangled==NULL" case below) here, while we still
79346283Sdfr       * have our hands on the function symbol.)
79446283Sdfr       */
79598944Sobrien      char *demangled;
79698944Sobrien      funname = SYMBOL_NAME (func);
79798944Sobrien      funlang = SYMBOL_LANGUAGE (func);
79898944Sobrien      if (funlang == language_cplus)
79998944Sobrien	{
80098944Sobrien	  demangled = cplus_demangle (funname, DMGL_ANSI);
80198944Sobrien	  /* If the demangler fails, try the demangled name
80298944Sobrien	   * from the symbol table. This'll have parameters,
80398944Sobrien	   * but that's preferable to diplaying a mangled name.
80498944Sobrien	   */
80598944Sobrien	  if (demangled == NULL)
80698944Sobrien	    funname = SYMBOL_SOURCE_NAME (func);
80798944Sobrien	}
80819370Spst    }
80919370Spst  else
81019370Spst    {
81119370Spst      register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
81219370Spst      if (msymbol != NULL)
81319370Spst	{
81419370Spst	  funname = SYMBOL_NAME (msymbol);
81519370Spst	  funlang = SYMBOL_LANGUAGE (msymbol);
81619370Spst	}
81719370Spst    }
81819370Spst  calling_frame_info = get_prev_frame (fi);
81919370Spst
82019370Spst  if (!addr_exp && selected_frame_level >= 0)
82119370Spst    {
82219370Spst      printf_filtered ("Stack level %d, frame at ", selected_frame_level);
82319370Spst      print_address_numeric (fi->frame, 1, gdb_stdout);
82419370Spst      printf_filtered (":\n");
82519370Spst    }
82619370Spst  else
82719370Spst    {
82819370Spst      printf_filtered ("Stack frame at ");
82919370Spst      print_address_numeric (fi->frame, 1, gdb_stdout);
83019370Spst      printf_filtered (":\n");
83119370Spst    }
83246283Sdfr  printf_filtered (" %s = ", REGISTER_NAME (PC_REGNUM));
83319370Spst  print_address_numeric (fi->pc, 1, gdb_stdout);
83419370Spst
83519370Spst  wrap_here ("   ");
83619370Spst  if (funname)
83719370Spst    {
83819370Spst      printf_filtered (" in ");
83919370Spst      fprintf_symbol_filtered (gdb_stdout, funname, funlang,
84019370Spst			       DMGL_ANSI | DMGL_PARAMS);
84119370Spst    }
84219370Spst  wrap_here ("   ");
84319370Spst  if (sal.symtab)
84419370Spst    printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
84519370Spst  puts_filtered ("; ");
84619370Spst  wrap_here ("    ");
84746283Sdfr  printf_filtered ("saved %s ", REGISTER_NAME (PC_REGNUM));
84819370Spst  print_address_numeric (FRAME_SAVED_PC (fi), 1, gdb_stdout);
84919370Spst  printf_filtered ("\n");
85019370Spst
85119370Spst  {
85298944Sobrien    int frameless;
85398944Sobrien    frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
85419370Spst    if (frameless)
85519370Spst      printf_filtered (" (FRAMELESS),");
85619370Spst  }
85719370Spst
85819370Spst  if (calling_frame_info)
85919370Spst    {
86019370Spst      printf_filtered (" called by frame at ");
86119370Spst      print_address_numeric (calling_frame_info->frame, 1, gdb_stdout);
86219370Spst    }
86319370Spst  if (fi->next && calling_frame_info)
86419370Spst    puts_filtered (",");
86519370Spst  wrap_here ("   ");
86619370Spst  if (fi->next)
86719370Spst    {
86819370Spst      printf_filtered (" caller of frame at ");
86919370Spst      print_address_numeric (fi->next->frame, 1, gdb_stdout);
87019370Spst    }
87119370Spst  if (fi->next || calling_frame_info)
87219370Spst    puts_filtered ("\n");
87319370Spst  if (s)
87419370Spst    printf_filtered (" source language %s.\n", language_str (s->language));
87519370Spst
87619370Spst#ifdef PRINT_EXTRA_FRAME_INFO
87719370Spst  PRINT_EXTRA_FRAME_INFO (fi);
87819370Spst#endif
87919370Spst
88019370Spst  {
88119370Spst    /* Address of the argument list for this frame, or 0.  */
88219370Spst    CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
88319370Spst    /* Number of args for this frame, or -1 if unknown.  */
88419370Spst    int numargs;
88519370Spst
88619370Spst    if (arg_list == 0)
88719370Spst      printf_filtered (" Arglist at unknown address.\n");
88819370Spst    else
88919370Spst      {
89019370Spst	printf_filtered (" Arglist at ");
89119370Spst	print_address_numeric (arg_list, 1, gdb_stdout);
89219370Spst	printf_filtered (",");
89319370Spst
89498944Sobrien	numargs = FRAME_NUM_ARGS (fi);
89519370Spst	if (numargs < 0)
89619370Spst	  puts_filtered (" args: ");
89719370Spst	else if (numargs == 0)
89819370Spst	  puts_filtered (" no args.");
89919370Spst	else if (numargs == 1)
90019370Spst	  puts_filtered (" 1 arg: ");
90119370Spst	else
90219370Spst	  printf_filtered (" %d args: ", numargs);
90319370Spst	print_frame_args (func, fi, numargs, gdb_stdout);
90419370Spst	puts_filtered ("\n");
90519370Spst      }
90619370Spst  }
90719370Spst  {
90819370Spst    /* Address of the local variables for this frame, or 0.  */
90919370Spst    CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
91019370Spst
91119370Spst    if (arg_list == 0)
91219370Spst      printf_filtered (" Locals at unknown address,");
91319370Spst    else
91419370Spst      {
91519370Spst	printf_filtered (" Locals at ");
91619370Spst	print_address_numeric (arg_list, 1, gdb_stdout);
91719370Spst	printf_filtered (",");
91819370Spst      }
91919370Spst  }
92019370Spst
92146283Sdfr  FRAME_INIT_SAVED_REGS (fi);
92246283Sdfr  if (fi->saved_regs != NULL)
92346283Sdfr    {
92446283Sdfr      /* The sp is special; what's returned isn't the save address, but
92598944Sobrien         actually the value of the previous frame's sp.  */
92646283Sdfr      printf_filtered (" Previous frame's sp is ");
92746283Sdfr      print_address_numeric (fi->saved_regs[SP_REGNUM], 1, gdb_stdout);
92846283Sdfr      printf_filtered ("\n");
92946283Sdfr      count = 0;
93098944Sobrien      numregs = NUM_REGS + NUM_PSEUDO_REGS;
93146283Sdfr      for (i = 0; i < numregs; i++)
93246283Sdfr	if (fi->saved_regs[i] && i != SP_REGNUM)
93346283Sdfr	  {
93446283Sdfr	    if (count == 0)
93546283Sdfr	      puts_filtered (" Saved registers:\n ");
93646283Sdfr	    else
93746283Sdfr	      puts_filtered (",");
93846283Sdfr	    wrap_here (" ");
93946283Sdfr	    printf_filtered (" %s at ", REGISTER_NAME (i));
94046283Sdfr	    print_address_numeric (fi->saved_regs[i], 1, gdb_stdout);
94146283Sdfr	    count++;
94246283Sdfr	  }
94346283Sdfr      if (count)
94446283Sdfr	puts_filtered ("\n");
94546283Sdfr    }
94646283Sdfr  else
94746283Sdfr    {
94846283Sdfr      /* We could get some information about saved registers by
94998944Sobrien         calling get_saved_register on each register.  Which info goes
95098944Sobrien         with which frame is necessarily lost, however, and I suspect
95198944Sobrien         that the users don't care whether they get the info.  */
95246283Sdfr      puts_filtered ("\n");
95346283Sdfr    }
95419370Spst}
95519370Spst
95619370Spst#if 0
95719370Spst/* Set a limit on the number of frames printed by default in a
95819370Spst   backtrace.  */
95919370Spst
96019370Spststatic int backtrace_limit;
96119370Spst
96219370Spststatic void
96398944Sobrienset_backtrace_limit_command (char *count_exp, int from_tty)
96419370Spst{
96598944Sobrien  int count = parse_and_eval_long (count_exp);
96619370Spst
96719370Spst  if (count < 0)
96819370Spst    error ("Negative argument not meaningful as backtrace limit.");
96919370Spst
97019370Spst  backtrace_limit = count;
97119370Spst}
97219370Spst
97319370Spststatic void
97498944Sobrienbacktrace_limit_info (char *arg, int from_tty)
97519370Spst{
97619370Spst  if (arg)
97719370Spst    error ("\"Info backtrace-limit\" takes no arguments.");
97819370Spst
97919370Spst  printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
98019370Spst}
98119370Spst#endif
98219370Spst
98319370Spst/* Print briefly all stack frames or just the innermost COUNT frames.  */
98419370Spst
98598944Sobrienstatic void backtrace_command_1 (char *count_exp, int show_locals,
98698944Sobrien				 int from_tty);
98719370Spststatic void
98898944Sobrienbacktrace_command_1 (char *count_exp, int show_locals, int from_tty)
98919370Spst{
99019370Spst  struct frame_info *fi;
99119370Spst  register int count;
99219370Spst  register int i;
99319370Spst  register struct frame_info *trailing;
99419370Spst  register int trailing_level;
99519370Spst
99619370Spst  if (!target_has_stack)
99719370Spst    error ("No stack.");
99819370Spst
99919370Spst  /* The following code must do two things.  First, it must
100019370Spst     set the variable TRAILING to the frame from which we should start
100119370Spst     printing.  Second, it must set the variable count to the number
100219370Spst     of frames which we should print, or -1 if all of them.  */
100319370Spst  trailing = get_current_frame ();
100498944Sobrien
100598944Sobrien  /* The target can be in a state where there is no valid frames
100698944Sobrien     (e.g., just connected). */
100798944Sobrien  if (trailing == NULL)
100898944Sobrien    error ("No stack.");
100998944Sobrien
101019370Spst  trailing_level = 0;
101119370Spst  if (count_exp)
101219370Spst    {
101398944Sobrien      count = parse_and_eval_long (count_exp);
101419370Spst      if (count < 0)
101519370Spst	{
101619370Spst	  struct frame_info *current;
101719370Spst
101819370Spst	  count = -count;
101919370Spst
102019370Spst	  current = trailing;
102119370Spst	  while (current && count--)
102219370Spst	    {
102319370Spst	      QUIT;
102419370Spst	      current = get_prev_frame (current);
102519370Spst	    }
102698944Sobrien
102719370Spst	  /* Will stop when CURRENT reaches the top of the stack.  TRAILING
102819370Spst	     will be COUNT below it.  */
102919370Spst	  while (current)
103019370Spst	    {
103119370Spst	      QUIT;
103219370Spst	      trailing = get_prev_frame (trailing);
103319370Spst	      current = get_prev_frame (current);
103419370Spst	      trailing_level++;
103519370Spst	    }
103698944Sobrien
103719370Spst	  count = -1;
103819370Spst	}
103919370Spst    }
104019370Spst  else
104119370Spst    count = -1;
104219370Spst
104319370Spst  if (info_verbose)
104419370Spst    {
104519370Spst      struct partial_symtab *ps;
104698944Sobrien
104719370Spst      /* Read in symbols for all of the frames.  Need to do this in
104898944Sobrien         a separate pass so that "Reading in symbols for xxx" messages
104998944Sobrien         don't screw up the appearance of the backtrace.  Also
105098944Sobrien         if people have strong opinions against reading symbols for
105198944Sobrien         backtrace this may have to be an option.  */
105219370Spst      i = count;
105319370Spst      for (fi = trailing;
105419370Spst	   fi != NULL && i--;
105519370Spst	   fi = get_prev_frame (fi))
105619370Spst	{
105719370Spst	  QUIT;
105819370Spst	  ps = find_pc_psymtab (fi->pc);
105919370Spst	  if (ps)
106019370Spst	    PSYMTAB_TO_SYMTAB (ps);	/* Force syms to come in */
106119370Spst	}
106219370Spst    }
106319370Spst
106419370Spst  for (i = 0, fi = trailing;
106519370Spst       fi && count--;
106619370Spst       i++, fi = get_prev_frame (fi))
106719370Spst    {
106819370Spst      QUIT;
106919370Spst
107019370Spst      /* Don't use print_stack_frame; if an error() occurs it probably
107198944Sobrien         means further attempts to backtrace would fail (on the other
107298944Sobrien         hand, perhaps the code does or could be fixed to make sure
107398944Sobrien         the frame->prev field gets set to NULL in that case).  */
107446283Sdfr      print_frame_info_base (fi, trailing_level + i, 0, 1);
107546283Sdfr      if (show_locals)
107698944Sobrien	print_frame_local_vars (fi, 1, gdb_stdout);
107719370Spst    }
107819370Spst
107919370Spst  /* If we've stopped before the end, mention that.  */
108019370Spst  if (fi && from_tty)
108119370Spst    printf_filtered ("(More stack frames follow...)\n");
108219370Spst}
108346283Sdfr
108446283Sdfrstatic void
108598944Sobrienbacktrace_command (char *arg, int from_tty)
108646283Sdfr{
108798944Sobrien  struct cleanup *old_chain = (struct cleanup *) NULL;
108898944Sobrien  char **argv = (char **) NULL;
108998944Sobrien  int argIndicatingFullTrace = (-1), totArgLen = 0, argc = 0;
109098944Sobrien  char *argPtr = arg;
109146283Sdfr
109298944Sobrien  if (arg != (char *) NULL)
109346283Sdfr    {
109446283Sdfr      int i;
109546283Sdfr
109698944Sobrien      argv = buildargv (arg);
109798944Sobrien      old_chain = make_cleanup_freeargv (argv);
109846283Sdfr      argc = 0;
109998944Sobrien      for (i = 0; (argv[i] != (char *) NULL); i++)
110098944Sobrien	{
110198944Sobrien	  unsigned int j;
110246283Sdfr
110398944Sobrien	  for (j = 0; (j < strlen (argv[i])); j++)
110498944Sobrien	    argv[i][j] = tolower (argv[i][j]);
110546283Sdfr
110698944Sobrien	  if (argIndicatingFullTrace < 0 && subset_compare (argv[i], "full"))
110798944Sobrien	    argIndicatingFullTrace = argc;
110898944Sobrien	  else
110998944Sobrien	    {
111098944Sobrien	      argc++;
111198944Sobrien	      totArgLen += strlen (argv[i]);
111298944Sobrien	    }
111398944Sobrien	}
111446283Sdfr      totArgLen += argc;
111546283Sdfr      if (argIndicatingFullTrace >= 0)
111698944Sobrien	{
111798944Sobrien	  if (totArgLen > 0)
111898944Sobrien	    {
111998944Sobrien	      argPtr = (char *) xmalloc (totArgLen + 1);
112098944Sobrien	      if (!argPtr)
112198944Sobrien		nomem (0);
112298944Sobrien	      else
112398944Sobrien		{
112498944Sobrien		  memset (argPtr, 0, totArgLen + 1);
112598944Sobrien		  for (i = 0; (i < (argc + 1)); i++)
112698944Sobrien		    {
112798944Sobrien		      if (i != argIndicatingFullTrace)
112898944Sobrien			{
112998944Sobrien			  strcat (argPtr, argv[i]);
113098944Sobrien			  strcat (argPtr, " ");
113198944Sobrien			}
113298944Sobrien		    }
113398944Sobrien		}
113498944Sobrien	    }
113598944Sobrien	  else
113698944Sobrien	    argPtr = (char *) NULL;
113798944Sobrien	}
113846283Sdfr    }
113946283Sdfr
114046283Sdfr  backtrace_command_1 (argPtr, (argIndicatingFullTrace >= 0), from_tty);
114146283Sdfr
114246283Sdfr  if (argIndicatingFullTrace >= 0 && totArgLen > 0)
114398944Sobrien    xfree (argPtr);
114446283Sdfr
114546283Sdfr  if (old_chain)
114698944Sobrien    do_cleanups (old_chain);
114746283Sdfr}
114846283Sdfr
114998944Sobrienstatic void backtrace_full_command (char *arg, int from_tty);
115046283Sdfrstatic void
115198944Sobrienbacktrace_full_command (char *arg, int from_tty)
115246283Sdfr{
115346283Sdfr  backtrace_command_1 (arg, 1, from_tty);
115446283Sdfr}
115598944Sobrien
115646283Sdfr
115719370Spst/* Print the local variables of a block B active in FRAME.
115819370Spst   Return 1 if any variables were printed; 0 otherwise.  */
115919370Spst
116019370Spststatic int
116198944Sobrienprint_block_frame_locals (struct block *b, register struct frame_info *fi,
116298944Sobrien			  int num_tabs, register struct ui_file *stream)
116319370Spst{
116446283Sdfr  register int i, j;
116519370Spst  register struct symbol *sym;
116619370Spst  register int values_printed = 0;
116719370Spst
116898944Sobrien  ALL_BLOCK_SYMBOLS (b, i, sym)
116919370Spst    {
117019370Spst      switch (SYMBOL_CLASS (sym))
117119370Spst	{
117219370Spst	case LOC_LOCAL:
117319370Spst	case LOC_REGISTER:
117419370Spst	case LOC_STATIC:
117519370Spst	case LOC_BASEREG:
117619370Spst	  values_printed = 1;
117746283Sdfr	  for (j = 0; j < num_tabs; j++)
117898944Sobrien	    fputs_filtered ("\t", stream);
117919370Spst	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
118019370Spst	  fputs_filtered (" = ", stream);
118119370Spst	  print_variable_value (sym, fi, stream);
118219370Spst	  fprintf_filtered (stream, "\n");
118319370Spst	  break;
118419370Spst
118519370Spst	default:
118619370Spst	  /* Ignore symbols which are not locals.  */
118719370Spst	  break;
118819370Spst	}
118919370Spst    }
119019370Spst  return values_printed;
119119370Spst}
119219370Spst
119319370Spst/* Same, but print labels.  */
119419370Spst
119519370Spststatic int
119698944Sobrienprint_block_frame_labels (struct block *b, int *have_default,
119798944Sobrien			  register struct ui_file *stream)
119819370Spst{
119919370Spst  register int i;
120019370Spst  register struct symbol *sym;
120119370Spst  register int values_printed = 0;
120219370Spst
120398944Sobrien  ALL_BLOCK_SYMBOLS (b, i, sym)
120419370Spst    {
120519370Spst      if (STREQ (SYMBOL_NAME (sym), "default"))
120619370Spst	{
120719370Spst	  if (*have_default)
120819370Spst	    continue;
120919370Spst	  *have_default = 1;
121019370Spst	}
121119370Spst      if (SYMBOL_CLASS (sym) == LOC_LABEL)
121219370Spst	{
121319370Spst	  struct symtab_and_line sal;
121419370Spst	  sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
121519370Spst	  values_printed = 1;
121619370Spst	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
121719370Spst	  if (addressprint)
121819370Spst	    {
121919370Spst	      fprintf_filtered (stream, " ");
122019370Spst	      print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
122119370Spst	    }
122219370Spst	  fprintf_filtered (stream, " in file %s, line %d\n",
122319370Spst			    sal.symtab->filename, sal.line);
122419370Spst	}
122519370Spst    }
122619370Spst  return values_printed;
122719370Spst}
122819370Spst
122919370Spst/* Print on STREAM all the local variables in frame FRAME,
123019370Spst   including all the blocks active in that frame
123119370Spst   at its current pc.
123219370Spst
123319370Spst   Returns 1 if the job was done,
123419370Spst   or 0 if nothing was printed because we have no info
123519370Spst   on the function running in FRAME.  */
123619370Spst
123719370Spststatic void
123898944Sobrienprint_frame_local_vars (register struct frame_info *fi, register int num_tabs,
123998944Sobrien			register struct ui_file *stream)
124019370Spst{
124119370Spst  register struct block *block = get_frame_block (fi);
124219370Spst  register int values_printed = 0;
124319370Spst
124419370Spst  if (block == 0)
124519370Spst    {
124619370Spst      fprintf_filtered (stream, "No symbol table info available.\n");
124719370Spst      return;
124819370Spst    }
124998944Sobrien
125019370Spst  while (block != 0)
125119370Spst    {
125246283Sdfr      if (print_block_frame_locals (block, fi, num_tabs, stream))
125319370Spst	values_printed = 1;
125419370Spst      /* After handling the function's top-level block, stop.
125598944Sobrien         Don't continue to its superblock, the block of
125698944Sobrien         per-file symbols.  */
125719370Spst      if (BLOCK_FUNCTION (block))
125819370Spst	break;
125919370Spst      block = BLOCK_SUPERBLOCK (block);
126019370Spst    }
126119370Spst
126219370Spst  if (!values_printed)
126319370Spst    {
126419370Spst      fprintf_filtered (stream, "No locals.\n");
126519370Spst    }
126619370Spst}
126719370Spst
126819370Spst/* Same, but print labels.  */
126919370Spst
127019370Spststatic void
127198944Sobrienprint_frame_label_vars (register struct frame_info *fi, int this_level_only,
127298944Sobrien			register struct ui_file *stream)
127319370Spst{
127419370Spst  register struct blockvector *bl;
127519370Spst  register struct block *block = get_frame_block (fi);
127619370Spst  register int values_printed = 0;
127719370Spst  int index, have_default = 0;
127819370Spst  char *blocks_printed;
127919370Spst  CORE_ADDR pc = fi->pc;
128019370Spst
128119370Spst  if (block == 0)
128219370Spst    {
128319370Spst      fprintf_filtered (stream, "No symbol table info available.\n");
128419370Spst      return;
128519370Spst    }
128619370Spst
128719370Spst  bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
128819370Spst  blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
128919370Spst  memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
129019370Spst
129119370Spst  while (block != 0)
129219370Spst    {
129319370Spst      CORE_ADDR end = BLOCK_END (block) - 4;
129419370Spst      int last_index;
129519370Spst
129619370Spst      if (bl != blockvector_for_pc (end, &index))
129719370Spst	error ("blockvector blotch");
129819370Spst      if (BLOCKVECTOR_BLOCK (bl, index) != block)
129919370Spst	error ("blockvector botch");
130019370Spst      last_index = BLOCKVECTOR_NBLOCKS (bl);
130119370Spst      index += 1;
130219370Spst
130319370Spst      /* Don't print out blocks that have gone by.  */
130419370Spst      while (index < last_index
130519370Spst	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
130619370Spst	index++;
130719370Spst
130819370Spst      while (index < last_index
130919370Spst	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
131019370Spst	{
131119370Spst	  if (blocks_printed[index] == 0)
131219370Spst	    {
131319370Spst	      if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
131419370Spst		values_printed = 1;
131519370Spst	      blocks_printed[index] = 1;
131619370Spst	    }
131719370Spst	  index++;
131819370Spst	}
131919370Spst      if (have_default)
132019370Spst	return;
132119370Spst      if (values_printed && this_level_only)
132219370Spst	return;
132319370Spst
132419370Spst      /* After handling the function's top-level block, stop.
132598944Sobrien         Don't continue to its superblock, the block of
132698944Sobrien         per-file symbols.  */
132719370Spst      if (BLOCK_FUNCTION (block))
132819370Spst	break;
132919370Spst      block = BLOCK_SUPERBLOCK (block);
133019370Spst    }
133119370Spst
133219370Spst  if (!values_printed && !this_level_only)
133319370Spst    {
133419370Spst      fprintf_filtered (stream, "No catches.\n");
133519370Spst    }
133619370Spst}
133719370Spst
133819370Spst/* ARGSUSED */
133946283Sdfrvoid
134098944Sobrienlocals_info (char *args, int from_tty)
134119370Spst{
134219370Spst  if (!selected_frame)
134319370Spst    error ("No frame selected.");
134446283Sdfr  print_frame_local_vars (selected_frame, 0, gdb_stdout);
134519370Spst}
134619370Spst
134719370Spststatic void
134898944Sobriencatch_info (char *ignore, int from_tty)
134919370Spst{
135098944Sobrien  struct symtab_and_line *sal;
135146283Sdfr
135298944Sobrien  /* Check for target support for exception handling */
135346283Sdfr  sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
135446283Sdfr  if (sal)
135546283Sdfr    {
135646283Sdfr      /* Currently not handling this */
135746283Sdfr      /* Ideally, here we should interact with the C++ runtime
135846283Sdfr         system to find the list of active handlers, etc. */
135946283Sdfr      fprintf_filtered (gdb_stdout, "Info catch not supported with this target/compiler combination.\n");
136046283Sdfr#if 0
136198944Sobrien      if (!selected_frame)
136298944Sobrien	error ("No frame selected.");
136346283Sdfr#endif
136446283Sdfr    }
136546283Sdfr  else
136646283Sdfr    {
136798944Sobrien      /* Assume g++ compiled code -- old v 4.16 behaviour */
136846283Sdfr      if (!selected_frame)
136998944Sobrien	error ("No frame selected.");
137098944Sobrien
137146283Sdfr      print_frame_label_vars (selected_frame, 0, gdb_stdout);
137246283Sdfr    }
137319370Spst}
137419370Spst
137519370Spststatic void
137698944Sobrienprint_frame_arg_vars (register struct frame_info *fi,
137798944Sobrien		      register struct ui_file *stream)
137819370Spst{
137919370Spst  struct symbol *func = get_frame_function (fi);
138019370Spst  register struct block *b;
138119370Spst  register int i;
138219370Spst  register struct symbol *sym, *sym2;
138319370Spst  register int values_printed = 0;
138419370Spst
138519370Spst  if (func == 0)
138619370Spst    {
138719370Spst      fprintf_filtered (stream, "No symbol table info available.\n");
138819370Spst      return;
138919370Spst    }
139019370Spst
139119370Spst  b = SYMBOL_BLOCK_VALUE (func);
139298944Sobrien  ALL_BLOCK_SYMBOLS (b, i, sym)
139319370Spst    {
139419370Spst      switch (SYMBOL_CLASS (sym))
139519370Spst	{
139619370Spst	case LOC_ARG:
139719370Spst	case LOC_LOCAL_ARG:
139819370Spst	case LOC_REF_ARG:
139919370Spst	case LOC_REGPARM:
140019370Spst	case LOC_REGPARM_ADDR:
140119370Spst	case LOC_BASEREG_ARG:
140219370Spst	  values_printed = 1;
140319370Spst	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
140419370Spst	  fputs_filtered (" = ", stream);
140519370Spst
140619370Spst	  /* We have to look up the symbol because arguments can have
140719370Spst	     two entries (one a parameter, one a local) and the one we
140819370Spst	     want is the local, which lookup_symbol will find for us.
140919370Spst	     This includes gcc1 (not gcc2) on the sparc when passing a
141019370Spst	     small structure and gcc2 when the argument type is float
141119370Spst	     and it is passed as a double and converted to float by
141219370Spst	     the prologue (in the latter case the type of the LOC_ARG
141319370Spst	     symbol is double and the type of the LOC_LOCAL symbol is
141419370Spst	     float).  There are also LOC_ARG/LOC_REGISTER pairs which
141519370Spst	     are not combined in symbol-reading.  */
141619370Spst
141719370Spst	  sym2 = lookup_symbol (SYMBOL_NAME (sym),
141898944Sobrien		   b, VAR_NAMESPACE, (int *) NULL, (struct symtab **) NULL);
141919370Spst	  print_variable_value (sym2, fi, stream);
142019370Spst	  fprintf_filtered (stream, "\n");
142119370Spst	  break;
142219370Spst
142319370Spst	default:
142419370Spst	  /* Don't worry about things which aren't arguments.  */
142519370Spst	  break;
142619370Spst	}
142719370Spst    }
142819370Spst  if (!values_printed)
142919370Spst    {
143019370Spst      fprintf_filtered (stream, "No arguments.\n");
143119370Spst    }
143219370Spst}
143319370Spst
143446283Sdfrvoid
143598944Sobrienargs_info (char *ignore, int from_tty)
143619370Spst{
143719370Spst  if (!selected_frame)
143819370Spst    error ("No frame selected.");
143919370Spst  print_frame_arg_vars (selected_frame, gdb_stdout);
144019370Spst}
144146283Sdfr
144246283Sdfr
144346283Sdfrstatic void
144498944Sobrienargs_plus_locals_info (char *ignore, int from_tty)
144546283Sdfr{
144698944Sobrien  args_info (ignore, from_tty);
144798944Sobrien  locals_info (ignore, from_tty);
144846283Sdfr}
144998944Sobrien
145046283Sdfr
145119370Spst/* Select frame FI, and note that its stack level is LEVEL.
145219370Spst   LEVEL may be -1 if an actual level number is not known.  */
145319370Spst
145419370Spstvoid
145598944Sobrienselect_frame (struct frame_info *fi, int level)
145619370Spst{
145719370Spst  register struct symtab *s;
145819370Spst
145919370Spst  selected_frame = fi;
146019370Spst  selected_frame_level = level;
146146283Sdfr  if (selected_frame_level_changed_hook)
146246283Sdfr    selected_frame_level_changed_hook (level);
146319370Spst
146419370Spst  /* Ensure that symbols for this frame are read in.  Also, determine the
146519370Spst     source language of this frame, and switch to it if desired.  */
146619370Spst  if (fi)
146798944Sobrien    {
146898944Sobrien      s = find_pc_symtab (fi->pc);
146998944Sobrien      if (s
147098944Sobrien	  && s->language != current_language->la_language
147198944Sobrien	  && s->language != language_unknown
147298944Sobrien	  && language_mode == language_mode_auto)
147398944Sobrien	{
147498944Sobrien	  set_language (s->language);
147598944Sobrien	}
147619370Spst    }
147719370Spst}
147898944Sobrien
147919370Spst
148046283Sdfr/* Select frame FI, noting that its stack level is LEVEL.  Also print
148146283Sdfr   the stack frame and show the source if this is the tui version.  */
148246283Sdfrvoid
148398944Sobrienselect_and_print_frame (struct frame_info *fi, int level)
148446283Sdfr{
148598944Sobrien  select_frame (fi, level);
148646283Sdfr  if (fi)
148746283Sdfr    {
148898944Sobrien      print_stack_frame (fi, level, 1);
148946283Sdfr    }
149046283Sdfr}
149146283Sdfr
149246283Sdfr
149319370Spst/* Store the selected frame and its level into *FRAMEP and *LEVELP.
149419370Spst   If there is no selected frame, *FRAMEP is set to NULL.  */
149519370Spst
149619370Spstvoid
149798944Sobrienrecord_selected_frame (CORE_ADDR *frameaddrp, int *levelp)
149819370Spst{
149919370Spst  *frameaddrp = selected_frame ? selected_frame->frame : 0;
150019370Spst  *levelp = selected_frame_level;
150119370Spst}
150219370Spst
150319370Spst/* Return the symbol-block in which the selected frame is executing.
150419370Spst   Can return zero under various legitimate circumstances.  */
150519370Spst
150619370Spststruct block *
150798944Sobrienget_selected_block (void)
150819370Spst{
150919370Spst  if (!target_has_stack)
151019370Spst    return 0;
151119370Spst
151219370Spst  if (!selected_frame)
151319370Spst    return get_current_block ();
151419370Spst  return get_frame_block (selected_frame);
151519370Spst}
151619370Spst
151719370Spst/* Find a frame a certain number of levels away from FRAME.
151819370Spst   LEVEL_OFFSET_PTR points to an int containing the number of levels.
151919370Spst   Positive means go to earlier frames (up); negative, the reverse.
152019370Spst   The int that contains the number of levels is counted toward
152119370Spst   zero as the frames for those levels are found.
152219370Spst   If the top or bottom frame is reached, that frame is returned,
152319370Spst   but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
152419370Spst   how much farther the original request asked to go.  */
152519370Spst
152619370Spststruct frame_info *
152798944Sobrienfind_relative_frame (register struct frame_info *frame,
152898944Sobrien		     register int *level_offset_ptr)
152919370Spst{
153019370Spst  register struct frame_info *prev;
153119370Spst  register struct frame_info *frame1;
153219370Spst
153319370Spst  /* Going up is simple: just do get_prev_frame enough times
153419370Spst     or until initial frame is reached.  */
153519370Spst  while (*level_offset_ptr > 0)
153619370Spst    {
153719370Spst      prev = get_prev_frame (frame);
153819370Spst      if (prev == 0)
153919370Spst	break;
154019370Spst      (*level_offset_ptr)--;
154119370Spst      frame = prev;
154219370Spst    }
154319370Spst  /* Going down is just as simple.  */
154419370Spst  if (*level_offset_ptr < 0)
154519370Spst    {
154698944Sobrien      while (*level_offset_ptr < 0)
154798944Sobrien	{
154898944Sobrien	  frame1 = get_next_frame (frame);
154998944Sobrien	  if (!frame1)
155098944Sobrien	    break;
155198944Sobrien	  frame = frame1;
155298944Sobrien	  (*level_offset_ptr)++;
155398944Sobrien	}
155419370Spst    }
155519370Spst  return frame;
155619370Spst}
155719370Spst
155819370Spst/* The "select_frame" command.  With no arg, NOP.
155919370Spst   With arg LEVEL_EXP, select the frame at level LEVEL if it is a
156019370Spst   valid level.  Otherwise, treat level_exp as an address expression
156119370Spst   and select it.  See parse_frame_specification for more info on proper
156219370Spst   frame expressions. */
156319370Spst
156419370Spst/* ARGSUSED */
156598944Sobrienvoid
156698944Sobrienselect_frame_command_wrapper (char *level_exp, int from_tty)
156798944Sobrien{
156898944Sobrien  select_frame_command (level_exp, from_tty);
156998944Sobrien}
157098944Sobrien
157119370Spststatic void
157298944Sobrienselect_frame_command (char *level_exp, int from_tty)
157319370Spst{
157419370Spst  register struct frame_info *frame, *frame1;
157519370Spst  unsigned int level = 0;
157619370Spst
157719370Spst  if (!target_has_stack)
157819370Spst    error ("No stack.");
157919370Spst
158019370Spst  frame = parse_frame_specification (level_exp);
158119370Spst
158219370Spst  /* Try to figure out what level this frame is.  But if there is
158319370Spst     no current stack, don't error out -- let the user set one.  */
158419370Spst  frame1 = 0;
158598944Sobrien  if (get_current_frame ())
158698944Sobrien    {
158798944Sobrien      for (frame1 = get_prev_frame (0);
158898944Sobrien	   frame1 && frame1 != frame;
158998944Sobrien	   frame1 = get_prev_frame (frame1))
159098944Sobrien	level++;
159198944Sobrien    }
159219370Spst
159319370Spst  if (!frame1)
159419370Spst    level = 0;
159519370Spst
159619370Spst  select_frame (frame, level);
159719370Spst}
159819370Spst
159919370Spst/* The "frame" command.  With no arg, print selected frame briefly.
160019370Spst   With arg, behaves like select_frame and then prints the selected
160119370Spst   frame.  */
160219370Spst
160346283Sdfrvoid
160498944Sobrienframe_command (char *level_exp, int from_tty)
160519370Spst{
160619370Spst  select_frame_command (level_exp, from_tty);
160746283Sdfr  show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
160819370Spst}
160919370Spst
161046283Sdfr/* The XDB Compatibility command to print the current frame. */
161146283Sdfr
161298944Sobrienstatic void
161398944Sobriencurrent_frame_command (char *level_exp, int from_tty)
161446283Sdfr{
161546283Sdfr  if (target_has_stack == 0 || selected_frame == 0)
161698944Sobrien    error ("No stack.");
161798944Sobrien  print_only_stack_frame (selected_frame, selected_frame_level, 1);
161898944Sobrien}
161946283Sdfr
162019370Spst/* Select the frame up one or COUNT stack levels
162119370Spst   from the previously selected frame, and print it briefly.  */
162219370Spst
162319370Spst/* ARGSUSED */
162419370Spststatic void
162598944Sobrienup_silently_base (char *count_exp)
162619370Spst{
162719370Spst  register struct frame_info *fi;
162819370Spst  int count = 1, count1;
162919370Spst  if (count_exp)
163098944Sobrien    count = parse_and_eval_long (count_exp);
163119370Spst  count1 = count;
163298944Sobrien
163319370Spst  if (target_has_stack == 0 || selected_frame == 0)
163419370Spst    error ("No stack.");
163519370Spst
163619370Spst  fi = find_relative_frame (selected_frame, &count1);
163719370Spst  if (count1 != 0 && count_exp == 0)
163819370Spst    error ("Initial frame selected; you cannot go up.");
163919370Spst  select_frame (fi, selected_frame_level + count - count1);
164019370Spst}
164119370Spst
164219370Spststatic void
164398944Sobrienup_silently_command (char *count_exp, int from_tty)
164446283Sdfr{
164598944Sobrien  up_silently_base (count_exp);
164646283Sdfr}
164746283Sdfr
164846283Sdfrstatic void
164998944Sobrienup_command (char *count_exp, int from_tty)
165019370Spst{
165146283Sdfr  up_silently_base (count_exp);
165246283Sdfr  show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
165319370Spst}
165419370Spst
165519370Spst/* Select the frame down one or COUNT stack levels
165619370Spst   from the previously selected frame, and print it briefly.  */
165719370Spst
165819370Spst/* ARGSUSED */
165919370Spststatic void
166098944Sobriendown_silently_base (char *count_exp)
166119370Spst{
166219370Spst  register struct frame_info *frame;
166319370Spst  int count = -1, count1;
166419370Spst  if (count_exp)
166598944Sobrien    count = -parse_and_eval_long (count_exp);
166619370Spst  count1 = count;
166798944Sobrien
166819370Spst  if (target_has_stack == 0 || selected_frame == 0)
166919370Spst    error ("No stack.");
167019370Spst
167119370Spst  frame = find_relative_frame (selected_frame, &count1);
167219370Spst  if (count1 != 0 && count_exp == 0)
167319370Spst    {
167419370Spst
167519370Spst      /* We only do this if count_exp is not specified.  That way "down"
167698944Sobrien         means to really go down (and let me know if that is
167798944Sobrien         impossible), but "down 9999" can be used to mean go all the way
167898944Sobrien         down without getting an error.  */
167919370Spst
168019370Spst      error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
168119370Spst    }
168219370Spst
168319370Spst  select_frame (frame, selected_frame_level + count - count1);
168419370Spst}
168519370Spst
168646283Sdfr/* ARGSUSED */
168746283Sdfrstatic void
168898944Sobriendown_silently_command (char *count_exp, int from_tty)
168946283Sdfr{
169046283Sdfr  down_silently_base (count_exp);
169146283Sdfr}
169219370Spst
169319370Spststatic void
169498944Sobriendown_command (char *count_exp, int from_tty)
169519370Spst{
169646283Sdfr  down_silently_base (count_exp);
169746283Sdfr  show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
169819370Spst}
169919370Spst
170098944Sobrienvoid
170198944Sobrienreturn_command_wrapper (char *retval_exp, int from_tty)
170298944Sobrien{
170398944Sobrien  return_command (retval_exp, from_tty);
170498944Sobrien}
170598944Sobrien
170619370Spststatic void
170798944Sobrienreturn_command (char *retval_exp, int from_tty)
170819370Spst{
170919370Spst  struct symbol *thisfun;
171019370Spst  CORE_ADDR selected_frame_addr;
171119370Spst  CORE_ADDR selected_frame_pc;
171219370Spst  struct frame_info *frame;
171398944Sobrien  struct value *return_value = NULL;
171419370Spst
171519370Spst  if (selected_frame == NULL)
171619370Spst    error ("No selected frame.");
171719370Spst  thisfun = get_frame_function (selected_frame);
171819370Spst  selected_frame_addr = FRAME_FP (selected_frame);
171919370Spst  selected_frame_pc = selected_frame->pc;
172019370Spst
172119370Spst  /* Compute the return value (if any -- possibly getting errors here).  */
172219370Spst
172319370Spst  if (retval_exp)
172419370Spst    {
172519370Spst      struct type *return_type = NULL;
172619370Spst
172719370Spst      return_value = parse_and_eval (retval_exp);
172819370Spst
172919370Spst      /* Cast return value to the return type of the function.  */
173019370Spst      if (thisfun != NULL)
173119370Spst	return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
173219370Spst      if (return_type == NULL)
173319370Spst	return_type = builtin_type_int;
173419370Spst      return_value = value_cast (return_type, return_value);
173519370Spst
173619370Spst      /* Make sure we have fully evaluated it, since
173798944Sobrien         it might live in the stack frame we're about to pop.  */
173819370Spst      if (VALUE_LAZY (return_value))
173919370Spst	value_fetch_lazy (return_value);
174019370Spst    }
174119370Spst
174219370Spst  /* If interactive, require confirmation.  */
174319370Spst
174419370Spst  if (from_tty)
174519370Spst    {
174619370Spst      if (thisfun != 0)
174719370Spst	{
174819370Spst	  if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
174919370Spst	    {
175019370Spst	      error ("Not confirmed.");
175119370Spst	      /* NOTREACHED */
175219370Spst	    }
175319370Spst	}
175498944Sobrien      else if (!query ("Make selected stack frame return now? "))
175598944Sobrien	error ("Not confirmed.");
175619370Spst    }
175719370Spst
175819370Spst  /* Do the real work.  Pop until the specified frame is current.  We
175919370Spst     use this method because the selected_frame is not valid after
176019370Spst     a POP_FRAME.  The pc comparison makes this work even if the
176119370Spst     selected frame shares its fp with another frame.  */
176219370Spst
176398944Sobrien  while (selected_frame_addr != (frame = get_current_frame ())->frame
176419370Spst	 || selected_frame_pc != frame->pc)
176519370Spst    POP_FRAME;
176619370Spst
176719370Spst  /* Then pop that frame.  */
176819370Spst
176919370Spst  POP_FRAME;
177019370Spst
177119370Spst  /* Compute the return value (if any) and store in the place
177219370Spst     for return values.  */
177319370Spst
177419370Spst  if (retval_exp)
177519370Spst    set_return_value (return_value);
177619370Spst
177798944Sobrien  /* If we are at the end of a call dummy now, pop the dummy frame too.  */
177898944Sobrien
177998944Sobrien  if (CALL_DUMMY_HAS_COMPLETED (read_pc(), read_sp (),
178098944Sobrien				FRAME_FP (get_current_frame ())))
178198944Sobrien    POP_FRAME;
178298944Sobrien
178319370Spst  /* If interactive, print the frame that is now current.  */
178419370Spst
178519370Spst  if (from_tty)
178619370Spst    frame_command ("0", 1);
178719370Spst  else
178819370Spst    select_frame_command ("0", 0);
178919370Spst}
179019370Spst
179146283Sdfr/* Sets the scope to input function name, provided that the
179246283Sdfr   function is within the current stack frame */
179346283Sdfr
179446283Sdfrstruct function_bounds
179546283Sdfr{
179646283Sdfr  CORE_ADDR low, high;
179746283Sdfr};
179846283Sdfr
179998944Sobrienstatic void func_command (char *arg, int from_tty);
180046283Sdfrstatic void
180198944Sobrienfunc_command (char *arg, int from_tty)
180246283Sdfr{
180346283Sdfr  struct frame_info *fp;
180446283Sdfr  int found = 0;
180546283Sdfr  struct symtabs_and_lines sals;
180646283Sdfr  int i;
180746283Sdfr  int level = 1;
180846283Sdfr  struct function_bounds *func_bounds = (struct function_bounds *) NULL;
180946283Sdfr
181046283Sdfr  if (arg != (char *) NULL)
181146283Sdfr    return;
181246283Sdfr
181346283Sdfr  fp = parse_frame_specification ("0");
181446283Sdfr  sals = decode_line_spec (arg, 1);
181546283Sdfr  func_bounds = (struct function_bounds *) xmalloc (
181646283Sdfr			      sizeof (struct function_bounds) * sals.nelts);
181746283Sdfr  for (i = 0; (i < sals.nelts && !found); i++)
181846283Sdfr    {
181946283Sdfr      if (sals.sals[i].pc == (CORE_ADDR) 0 ||
182046283Sdfr	  find_pc_partial_function (sals.sals[i].pc,
182146283Sdfr				    (char **) NULL,
182246283Sdfr				    &func_bounds[i].low,
182346283Sdfr				    &func_bounds[i].high) == 0)
182446283Sdfr	{
182546283Sdfr	  func_bounds[i].low =
182646283Sdfr	    func_bounds[i].high = (CORE_ADDR) NULL;
182746283Sdfr	}
182846283Sdfr    }
182946283Sdfr
183046283Sdfr  do
183146283Sdfr    {
183246283Sdfr      for (i = 0; (i < sals.nelts && !found); i++)
183346283Sdfr	found = (fp->pc >= func_bounds[i].low &&
183446283Sdfr		 fp->pc < func_bounds[i].high);
183546283Sdfr      if (!found)
183646283Sdfr	{
183746283Sdfr	  level = 1;
183846283Sdfr	  fp = find_relative_frame (fp, &level);
183946283Sdfr	}
184046283Sdfr    }
184146283Sdfr  while (!found && level == 0);
184246283Sdfr
184346283Sdfr  if (func_bounds)
184498944Sobrien    xfree (func_bounds);
184546283Sdfr
184646283Sdfr  if (!found)
184746283Sdfr    printf_filtered ("'%s' not within current stack frame.\n", arg);
184846283Sdfr  else if (fp != selected_frame)
184946283Sdfr    select_and_print_frame (fp, level);
185046283Sdfr}
185146283Sdfr
185219370Spst/* Gets the language of the current frame.  */
185319370Spst
185419370Spstenum language
185598944Sobrienget_frame_language (void)
185619370Spst{
185719370Spst  register struct symtab *s;
185819370Spst  enum language flang;		/* The language of the current frame */
185998944Sobrien
186019370Spst  if (selected_frame)
186119370Spst    {
186298944Sobrien      s = find_pc_symtab (selected_frame->pc);
186319370Spst      if (s)
186419370Spst	flang = s->language;
186519370Spst      else
186619370Spst	flang = language_unknown;
186719370Spst    }
186819370Spst  else
186919370Spst    flang = language_unknown;
187019370Spst
187119370Spst  return flang;
187219370Spst}
187319370Spst
187419370Spstvoid
187598944Sobrien_initialize_stack (void)
187619370Spst{
187798944Sobrien#if 0
187819370Spst  backtrace_limit = 30;
187919370Spst#endif
188019370Spst
188119370Spst  add_com ("return", class_stack, return_command,
188219370Spst	   "Make selected stack frame return to its caller.\n\
188319370SpstControl remains in the debugger, but when you continue\n\
188419370Spstexecution will resume in the frame above the one now selected.\n\
188519370SpstIf an argument is given, it is an expression for the value to return.");
188619370Spst
188719370Spst  add_com ("up", class_stack, up_command,
188819370Spst	   "Select and print stack frame that called this one.\n\
188919370SpstAn argument says how many frames up to go.");
189019370Spst  add_com ("up-silently", class_support, up_silently_command,
189119370Spst	   "Same as the `up' command, but does not print anything.\n\
189219370SpstThis is useful in command scripts.");
189319370Spst
189419370Spst  add_com ("down", class_stack, down_command,
189519370Spst	   "Select and print stack frame called by this one.\n\
189619370SpstAn argument says how many frames down to go.");
189719370Spst  add_com_alias ("do", "down", class_stack, 1);
189819370Spst  add_com_alias ("dow", "down", class_stack, 1);
189919370Spst  add_com ("down-silently", class_support, down_silently_command,
190019370Spst	   "Same as the `down' command, but does not print anything.\n\
190119370SpstThis is useful in command scripts.");
190219370Spst
190319370Spst  add_com ("frame", class_stack, frame_command,
190419370Spst	   "Select and print a stack frame.\n\
190519370SpstWith no argument, print the selected stack frame.  (See also \"info frame\").\n\
190619370SpstAn argument specifies the frame to select.\n\
190719370SpstIt can be a stack frame number or the address of the frame.\n\
190819370SpstWith argument, nothing is printed if input is coming from\n\
190919370Spsta command file or a user-defined command.");
191019370Spst
191119370Spst  add_com_alias ("f", "frame", class_stack, 1);
191219370Spst
191346283Sdfr  if (xdb_commands)
191446283Sdfr    {
191598944Sobrien      add_com ("L", class_stack, current_frame_command,
191698944Sobrien	       "Print the current stack frame.\n");
191746283Sdfr      add_com_alias ("V", "frame", class_stack, 1);
191846283Sdfr    }
191919370Spst  add_com ("select-frame", class_stack, select_frame_command,
192019370Spst	   "Select a stack frame without printing anything.\n\
192119370SpstAn argument specifies the frame to select.\n\
192219370SpstIt can be a stack frame number or the address of the frame.\n");
192319370Spst
192419370Spst  add_com ("backtrace", class_stack, backtrace_command,
192519370Spst	   "Print backtrace of all stack frames, or innermost COUNT frames.\n\
192646283SdfrWith a negative argument, print outermost -COUNT frames.\n\
192746283SdfrUse of the 'full' qualifier also prints the values of the local variables.\n");
192819370Spst  add_com_alias ("bt", "backtrace", class_stack, 0);
192946283Sdfr  if (xdb_commands)
193046283Sdfr    {
193146283Sdfr      add_com_alias ("t", "backtrace", class_stack, 0);
193246283Sdfr      add_com ("T", class_stack, backtrace_full_command,
193398944Sobrien	       "Print backtrace of all stack frames, or innermost COUNT frames \n\
193446283Sdfrand the values of the local variables.\n\
193546283SdfrWith a negative argument, print outermost -COUNT frames.\n\
193646283SdfrUsage: T <count>\n");
193746283Sdfr    }
193846283Sdfr
193919370Spst  add_com_alias ("where", "backtrace", class_alias, 0);
194019370Spst  add_info ("stack", backtrace_command,
194119370Spst	    "Backtrace of the stack, or innermost COUNT frames.");
194219370Spst  add_info_alias ("s", "stack", 1);
194319370Spst  add_info ("frame", frame_info,
194419370Spst	    "All about selected stack frame, or frame at ADDR.");
194519370Spst  add_info_alias ("f", "frame", 1);
194619370Spst  add_info ("locals", locals_info,
194719370Spst	    "Local variables of current stack frame.");
194819370Spst  add_info ("args", args_info,
194919370Spst	    "Argument variables of current stack frame.");
195046283Sdfr  if (xdb_commands)
195198944Sobrien    add_com ("l", class_info, args_plus_locals_info,
195298944Sobrien	     "Argument and local variables of current stack frame.");
195346283Sdfr
195446283Sdfr  if (dbx_commands)
195598944Sobrien    add_com ("func", class_stack, func_command,
195698944Sobrien      "Select the stack frame that contains <func>.\nUsage: func <name>\n");
195746283Sdfr
195819370Spst  add_info ("catch", catch_info,
195919370Spst	    "Exceptions that can be caught in the current stack frame.");
196019370Spst
196119370Spst#if 0
196298944Sobrien  add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
196398944Sobrien  "Specify maximum number of frames for \"backtrace\" to print by default.",
196419370Spst	   &setlist);
196519370Spst  add_info ("backtrace-limit", backtrace_limit_info,
196698944Sobrien     "The maximum number of frames for \"backtrace\" to print by default.");
196719370Spst#endif
196819370Spst}
1969