xref: /dflybsd-src/contrib/gdb-7/gdb/stack.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Print and select stack frames for GDB, the GNU debugger.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4*5796c8dcSSimon Schubert    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
5*5796c8dcSSimon Schubert    2009 Free Software Foundation, Inc.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This file is part of GDB.
8*5796c8dcSSimon Schubert 
9*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
10*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
11*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
12*5796c8dcSSimon Schubert    (at your option) any later version.
13*5796c8dcSSimon Schubert 
14*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
15*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*5796c8dcSSimon Schubert    GNU General Public License for more details.
18*5796c8dcSSimon Schubert 
19*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
20*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*5796c8dcSSimon Schubert 
22*5796c8dcSSimon Schubert #include "defs.h"
23*5796c8dcSSimon Schubert #include "value.h"
24*5796c8dcSSimon Schubert #include "symtab.h"
25*5796c8dcSSimon Schubert #include "gdbtypes.h"
26*5796c8dcSSimon Schubert #include "expression.h"
27*5796c8dcSSimon Schubert #include "language.h"
28*5796c8dcSSimon Schubert #include "frame.h"
29*5796c8dcSSimon Schubert #include "gdbcmd.h"
30*5796c8dcSSimon Schubert #include "gdbcore.h"
31*5796c8dcSSimon Schubert #include "target.h"
32*5796c8dcSSimon Schubert #include "source.h"
33*5796c8dcSSimon Schubert #include "breakpoint.h"
34*5796c8dcSSimon Schubert #include "demangle.h"
35*5796c8dcSSimon Schubert #include "inferior.h"
36*5796c8dcSSimon Schubert #include "annotate.h"
37*5796c8dcSSimon Schubert #include "ui-out.h"
38*5796c8dcSSimon Schubert #include "block.h"
39*5796c8dcSSimon Schubert #include "stack.h"
40*5796c8dcSSimon Schubert #include "dictionary.h"
41*5796c8dcSSimon Schubert #include "exceptions.h"
42*5796c8dcSSimon Schubert #include "reggroups.h"
43*5796c8dcSSimon Schubert #include "regcache.h"
44*5796c8dcSSimon Schubert #include "solib.h"
45*5796c8dcSSimon Schubert #include "valprint.h"
46*5796c8dcSSimon Schubert #include "gdbthread.h"
47*5796c8dcSSimon Schubert #include "cp-support.h"
48*5796c8dcSSimon Schubert #include "disasm.h"
49*5796c8dcSSimon Schubert #include "inline-frame.h"
50*5796c8dcSSimon Schubert 
51*5796c8dcSSimon Schubert #include "gdb_assert.h"
52*5796c8dcSSimon Schubert #include <ctype.h>
53*5796c8dcSSimon Schubert #include "gdb_string.h"
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert void (*deprecated_selected_frame_level_changed_hook) (int);
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert /* The possible choices of "set print frame-arguments, and the value
58*5796c8dcSSimon Schubert    of this setting.  */
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert static const char *print_frame_arguments_choices[] =
61*5796c8dcSSimon Schubert   {"all", "scalars", "none", NULL};
62*5796c8dcSSimon Schubert static const char *print_frame_arguments = "scalars";
63*5796c8dcSSimon Schubert 
64*5796c8dcSSimon Schubert /* Prototypes for local functions. */
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert static void print_frame_local_vars (struct frame_info *, int,
67*5796c8dcSSimon Schubert 				    struct ui_file *);
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert static void print_frame (struct frame_info *frame, int print_level,
70*5796c8dcSSimon Schubert 			 enum print_what print_what,  int print_args,
71*5796c8dcSSimon Schubert 			 struct symtab_and_line sal);
72*5796c8dcSSimon Schubert 
73*5796c8dcSSimon Schubert /* Zero means do things normally; we are interacting directly with the
74*5796c8dcSSimon Schubert    user.  One means print the full filename and linenumber when a
75*5796c8dcSSimon Schubert    frame is printed, and do so in a format emacs18/emacs19.22 can
76*5796c8dcSSimon Schubert    parse.  Two means print similar annotations, but in many more
77*5796c8dcSSimon Schubert    cases and in a slightly different syntax.  */
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert int annotation_level = 0;
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert 
82*5796c8dcSSimon Schubert struct print_stack_frame_args
83*5796c8dcSSimon Schubert {
84*5796c8dcSSimon Schubert   struct frame_info *frame;
85*5796c8dcSSimon Schubert   int print_level;
86*5796c8dcSSimon Schubert   enum print_what print_what;
87*5796c8dcSSimon Schubert   int print_args;
88*5796c8dcSSimon Schubert };
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert /* Show or print the frame arguments; stub for catch_errors.  */
91*5796c8dcSSimon Schubert 
92*5796c8dcSSimon Schubert static int
93*5796c8dcSSimon Schubert print_stack_frame_stub (void *args)
94*5796c8dcSSimon Schubert {
95*5796c8dcSSimon Schubert   struct print_stack_frame_args *p = args;
96*5796c8dcSSimon Schubert   int center = (p->print_what == SRC_LINE || p->print_what == SRC_AND_LOC);
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert   print_frame_info (p->frame, p->print_level, p->print_what, p->print_args);
99*5796c8dcSSimon Schubert   set_current_sal_from_frame (p->frame, center);
100*5796c8dcSSimon Schubert   return 0;
101*5796c8dcSSimon Schubert }
102*5796c8dcSSimon Schubert 
103*5796c8dcSSimon Schubert /* Return 1 if we should display the address in addition to the location,
104*5796c8dcSSimon Schubert    because we are in the middle of a statement.  */
105*5796c8dcSSimon Schubert 
106*5796c8dcSSimon Schubert static int
107*5796c8dcSSimon Schubert frame_show_address (struct frame_info *frame,
108*5796c8dcSSimon Schubert 		    struct symtab_and_line sal)
109*5796c8dcSSimon Schubert {
110*5796c8dcSSimon Schubert   /* If there is a line number, but no PC, then there is no location
111*5796c8dcSSimon Schubert      information associated with this sal.  The only way that should
112*5796c8dcSSimon Schubert      happen is for the call sites of inlined functions (SAL comes from
113*5796c8dcSSimon Schubert      find_frame_sal).  Otherwise, we would have some PC range if the
114*5796c8dcSSimon Schubert      SAL came from a line table.  */
115*5796c8dcSSimon Schubert   if (sal.line != 0 && sal.pc == 0 && sal.end == 0)
116*5796c8dcSSimon Schubert     {
117*5796c8dcSSimon Schubert       if (get_next_frame (frame) == NULL)
118*5796c8dcSSimon Schubert 	gdb_assert (inline_skipped_frames (inferior_ptid) > 0);
119*5796c8dcSSimon Schubert       else
120*5796c8dcSSimon Schubert 	gdb_assert (get_frame_type (get_next_frame (frame)) == INLINE_FRAME);
121*5796c8dcSSimon Schubert       return 0;
122*5796c8dcSSimon Schubert     }
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert   return get_frame_pc (frame) != sal.pc;
125*5796c8dcSSimon Schubert }
126*5796c8dcSSimon Schubert 
127*5796c8dcSSimon Schubert /* Show or print a stack frame FRAME briefly.  The output is format
128*5796c8dcSSimon Schubert    according to PRINT_LEVEL and PRINT_WHAT printing the frame's
129*5796c8dcSSimon Schubert    relative level, function name, argument list, and file name and
130*5796c8dcSSimon Schubert    line number.  If the frame's PC is not at the beginning of the
131*5796c8dcSSimon Schubert    source line, the actual PC is printed at the beginning.  */
132*5796c8dcSSimon Schubert 
133*5796c8dcSSimon Schubert void
134*5796c8dcSSimon Schubert print_stack_frame (struct frame_info *frame, int print_level,
135*5796c8dcSSimon Schubert 		   enum print_what print_what)
136*5796c8dcSSimon Schubert {
137*5796c8dcSSimon Schubert   struct print_stack_frame_args args;
138*5796c8dcSSimon Schubert 
139*5796c8dcSSimon Schubert   args.frame = frame;
140*5796c8dcSSimon Schubert   args.print_level = print_level;
141*5796c8dcSSimon Schubert   args.print_what = print_what;
142*5796c8dcSSimon Schubert   /* For mi, alway print location and address.  */
143*5796c8dcSSimon Schubert   args.print_what = ui_out_is_mi_like_p (uiout) ? LOC_AND_ADDRESS : print_what;
144*5796c8dcSSimon Schubert   args.print_args = 1;
145*5796c8dcSSimon Schubert 
146*5796c8dcSSimon Schubert   catch_errors (print_stack_frame_stub, &args, "", RETURN_MASK_ERROR);
147*5796c8dcSSimon Schubert }
148*5796c8dcSSimon Schubert 
149*5796c8dcSSimon Schubert struct print_args_args
150*5796c8dcSSimon Schubert {
151*5796c8dcSSimon Schubert   struct symbol *func;
152*5796c8dcSSimon Schubert   struct frame_info *frame;
153*5796c8dcSSimon Schubert   struct ui_file *stream;
154*5796c8dcSSimon Schubert };
155*5796c8dcSSimon Schubert 
156*5796c8dcSSimon Schubert static int print_args_stub (void *args);
157*5796c8dcSSimon Schubert 
158*5796c8dcSSimon Schubert /* Print nameless arguments of frame FRAME on STREAM, where START is
159*5796c8dcSSimon Schubert    the offset of the first nameless argument, and NUM is the number of
160*5796c8dcSSimon Schubert    nameless arguments to print.  FIRST is nonzero if this is the first
161*5796c8dcSSimon Schubert    argument (not just the first nameless argument).  */
162*5796c8dcSSimon Schubert 
163*5796c8dcSSimon Schubert static void
164*5796c8dcSSimon Schubert print_frame_nameless_args (struct frame_info *frame, long start, int num,
165*5796c8dcSSimon Schubert 			   int first, struct ui_file *stream)
166*5796c8dcSSimon Schubert {
167*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
168*5796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
169*5796c8dcSSimon Schubert   int i;
170*5796c8dcSSimon Schubert   CORE_ADDR argsaddr;
171*5796c8dcSSimon Schubert   long arg_value;
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert   for (i = 0; i < num; i++)
174*5796c8dcSSimon Schubert     {
175*5796c8dcSSimon Schubert       QUIT;
176*5796c8dcSSimon Schubert       argsaddr = get_frame_args_address (frame);
177*5796c8dcSSimon Schubert       if (!argsaddr)
178*5796c8dcSSimon Schubert 	return;
179*5796c8dcSSimon Schubert       arg_value = read_memory_integer (argsaddr + start,
180*5796c8dcSSimon Schubert 				       sizeof (int), byte_order);
181*5796c8dcSSimon Schubert       if (!first)
182*5796c8dcSSimon Schubert 	fprintf_filtered (stream, ", ");
183*5796c8dcSSimon Schubert       fprintf_filtered (stream, "%ld", arg_value);
184*5796c8dcSSimon Schubert       first = 0;
185*5796c8dcSSimon Schubert       start += sizeof (int);
186*5796c8dcSSimon Schubert     }
187*5796c8dcSSimon Schubert }
188*5796c8dcSSimon Schubert 
189*5796c8dcSSimon Schubert /* Print the arguments of frame FRAME on STREAM, given the function
190*5796c8dcSSimon Schubert    FUNC running in that frame (as a symbol), where NUM is the number
191*5796c8dcSSimon Schubert    of arguments according to the stack frame (or -1 if the number of
192*5796c8dcSSimon Schubert    arguments is unknown).  */
193*5796c8dcSSimon Schubert 
194*5796c8dcSSimon Schubert /* Note that currently the "number of arguments according to the
195*5796c8dcSSimon Schubert    stack frame" is only known on VAX where i refers to the "number of
196*5796c8dcSSimon Schubert    ints of arguments according to the stack frame".  */
197*5796c8dcSSimon Schubert 
198*5796c8dcSSimon Schubert static void
199*5796c8dcSSimon Schubert print_frame_args (struct symbol *func, struct frame_info *frame,
200*5796c8dcSSimon Schubert 		  int num, struct ui_file *stream)
201*5796c8dcSSimon Schubert {
202*5796c8dcSSimon Schubert   int first = 1;
203*5796c8dcSSimon Schubert   /* Offset of next stack argument beyond the one we have seen that is
204*5796c8dcSSimon Schubert      at the highest offset, or -1 if we haven't come to a stack
205*5796c8dcSSimon Schubert      argument yet.  */
206*5796c8dcSSimon Schubert   long highest_offset = -1;
207*5796c8dcSSimon Schubert   /* Number of ints of arguments that we have printed so far.  */
208*5796c8dcSSimon Schubert   int args_printed = 0;
209*5796c8dcSSimon Schubert   struct cleanup *old_chain, *list_chain;
210*5796c8dcSSimon Schubert   struct ui_stream *stb;
211*5796c8dcSSimon Schubert   /* True if we should print arguments, false otherwise.  */
212*5796c8dcSSimon Schubert   int print_args = strcmp (print_frame_arguments, "none");
213*5796c8dcSSimon Schubert   /* True in "summary" mode, false otherwise.  */
214*5796c8dcSSimon Schubert   int summary = !strcmp (print_frame_arguments, "scalars");
215*5796c8dcSSimon Schubert 
216*5796c8dcSSimon Schubert   stb = ui_out_stream_new (uiout);
217*5796c8dcSSimon Schubert   old_chain = make_cleanup_ui_out_stream_delete (stb);
218*5796c8dcSSimon Schubert 
219*5796c8dcSSimon Schubert   if (func)
220*5796c8dcSSimon Schubert     {
221*5796c8dcSSimon Schubert       struct block *b = SYMBOL_BLOCK_VALUE (func);
222*5796c8dcSSimon Schubert       struct dict_iterator iter;
223*5796c8dcSSimon Schubert       struct symbol *sym;
224*5796c8dcSSimon Schubert       struct value *val;
225*5796c8dcSSimon Schubert 
226*5796c8dcSSimon Schubert       ALL_BLOCK_SYMBOLS (b, iter, sym)
227*5796c8dcSSimon Schubert         {
228*5796c8dcSSimon Schubert 	  QUIT;
229*5796c8dcSSimon Schubert 
230*5796c8dcSSimon Schubert 	  /* Keep track of the highest stack argument offset seen, and
231*5796c8dcSSimon Schubert 	     skip over any kinds of symbols we don't care about.  */
232*5796c8dcSSimon Schubert 
233*5796c8dcSSimon Schubert 	  if (!SYMBOL_IS_ARGUMENT (sym))
234*5796c8dcSSimon Schubert 	    continue;
235*5796c8dcSSimon Schubert 
236*5796c8dcSSimon Schubert 	  switch (SYMBOL_CLASS (sym))
237*5796c8dcSSimon Schubert 	    {
238*5796c8dcSSimon Schubert 	    case LOC_ARG:
239*5796c8dcSSimon Schubert 	    case LOC_REF_ARG:
240*5796c8dcSSimon Schubert 	      {
241*5796c8dcSSimon Schubert 		long current_offset = SYMBOL_VALUE (sym);
242*5796c8dcSSimon Schubert 		int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
243*5796c8dcSSimon Schubert 
244*5796c8dcSSimon Schubert 		/* Compute address of next argument by adding the size of
245*5796c8dcSSimon Schubert 		   this argument and rounding to an int boundary.  */
246*5796c8dcSSimon Schubert 		current_offset =
247*5796c8dcSSimon Schubert 		  ((current_offset + arg_size + sizeof (int) - 1)
248*5796c8dcSSimon Schubert 		   & ~(sizeof (int) - 1));
249*5796c8dcSSimon Schubert 
250*5796c8dcSSimon Schubert 		/* If this is the highest offset seen yet, set
251*5796c8dcSSimon Schubert 		   highest_offset.  */
252*5796c8dcSSimon Schubert 		if (highest_offset == -1
253*5796c8dcSSimon Schubert 		    || (current_offset > highest_offset))
254*5796c8dcSSimon Schubert 		  highest_offset = current_offset;
255*5796c8dcSSimon Schubert 
256*5796c8dcSSimon Schubert 		/* Add the number of ints we're about to print to
257*5796c8dcSSimon Schubert 		   args_printed.  */
258*5796c8dcSSimon Schubert 		args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
259*5796c8dcSSimon Schubert 	      }
260*5796c8dcSSimon Schubert 
261*5796c8dcSSimon Schubert 	      /* We care about types of symbols, but don't need to
262*5796c8dcSSimon Schubert 		 keep track of stack offsets in them.  */
263*5796c8dcSSimon Schubert 	    case LOC_REGISTER:
264*5796c8dcSSimon Schubert 	    case LOC_REGPARM_ADDR:
265*5796c8dcSSimon Schubert 	    case LOC_COMPUTED:
266*5796c8dcSSimon Schubert 	    case LOC_OPTIMIZED_OUT:
267*5796c8dcSSimon Schubert 	    default:
268*5796c8dcSSimon Schubert 	      break;
269*5796c8dcSSimon Schubert 	    }
270*5796c8dcSSimon Schubert 
271*5796c8dcSSimon Schubert 	  /* We have to look up the symbol because arguments can have
272*5796c8dcSSimon Schubert 	     two entries (one a parameter, one a local) and the one we
273*5796c8dcSSimon Schubert 	     want is the local, which lookup_symbol will find for us.
274*5796c8dcSSimon Schubert 	     This includes gcc1 (not gcc2) on SPARC when passing a
275*5796c8dcSSimon Schubert 	     small structure and gcc2 when the argument type is float
276*5796c8dcSSimon Schubert 	     and it is passed as a double and converted to float by
277*5796c8dcSSimon Schubert 	     the prologue (in the latter case the type of the LOC_ARG
278*5796c8dcSSimon Schubert 	     symbol is double and the type of the LOC_LOCAL symbol is
279*5796c8dcSSimon Schubert 	     float).  */
280*5796c8dcSSimon Schubert 	  /* But if the parameter name is null, don't try it.  Null
281*5796c8dcSSimon Schubert 	     parameter names occur on the RS/6000, for traceback
282*5796c8dcSSimon Schubert 	     tables.  FIXME, should we even print them?  */
283*5796c8dcSSimon Schubert 
284*5796c8dcSSimon Schubert 	  if (*SYMBOL_LINKAGE_NAME (sym))
285*5796c8dcSSimon Schubert 	    {
286*5796c8dcSSimon Schubert 	      struct symbol *nsym;
287*5796c8dcSSimon Schubert 	      nsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
288*5796c8dcSSimon Schubert 				    b, VAR_DOMAIN, NULL);
289*5796c8dcSSimon Schubert 	      gdb_assert (nsym != NULL);
290*5796c8dcSSimon Schubert 	      if (SYMBOL_CLASS (nsym) == LOC_REGISTER
291*5796c8dcSSimon Schubert 		  && !SYMBOL_IS_ARGUMENT (nsym))
292*5796c8dcSSimon Schubert 		{
293*5796c8dcSSimon Schubert 		  /* There is a LOC_ARG/LOC_REGISTER pair.  This means
294*5796c8dcSSimon Schubert 		     that it was passed on the stack and loaded into a
295*5796c8dcSSimon Schubert 		     register, or passed in a register and stored in a
296*5796c8dcSSimon Schubert 		     stack slot.  GDB 3.x used the LOC_ARG; GDB
297*5796c8dcSSimon Schubert 		     4.0-4.11 used the LOC_REGISTER.
298*5796c8dcSSimon Schubert 
299*5796c8dcSSimon Schubert 		     Reasons for using the LOC_ARG:
300*5796c8dcSSimon Schubert 
301*5796c8dcSSimon Schubert 		     (1) Because find_saved_registers may be slow for
302*5796c8dcSSimon Schubert 		         remote debugging.
303*5796c8dcSSimon Schubert 
304*5796c8dcSSimon Schubert 		     (2) Because registers are often re-used and stack
305*5796c8dcSSimon Schubert 		         slots rarely (never?) are.  Therefore using
306*5796c8dcSSimon Schubert 		         the stack slot is much less likely to print
307*5796c8dcSSimon Schubert 		         garbage.
308*5796c8dcSSimon Schubert 
309*5796c8dcSSimon Schubert 		     Reasons why we might want to use the LOC_REGISTER:
310*5796c8dcSSimon Schubert 
311*5796c8dcSSimon Schubert 		     (1) So that the backtrace prints the same value
312*5796c8dcSSimon Schubert 		         as "print foo".  I see no compelling reason
313*5796c8dcSSimon Schubert 		         why this needs to be the case; having the
314*5796c8dcSSimon Schubert 		         backtrace print the value which was passed
315*5796c8dcSSimon Schubert 		         in, and "print foo" print the value as
316*5796c8dcSSimon Schubert 		         modified within the called function, makes
317*5796c8dcSSimon Schubert 		         perfect sense to me.
318*5796c8dcSSimon Schubert 
319*5796c8dcSSimon Schubert 		     Additional note: It might be nice if "info args"
320*5796c8dcSSimon Schubert 		     displayed both values.
321*5796c8dcSSimon Schubert 
322*5796c8dcSSimon Schubert 		     One more note: There is a case with SPARC
323*5796c8dcSSimon Schubert 		     structure passing where we need to use the
324*5796c8dcSSimon Schubert 		     LOC_REGISTER, but this is dealt with by creating
325*5796c8dcSSimon Schubert 		     a single LOC_REGPARM in symbol reading.  */
326*5796c8dcSSimon Schubert 
327*5796c8dcSSimon Schubert 		  /* Leave sym (the LOC_ARG) alone.  */
328*5796c8dcSSimon Schubert 		  ;
329*5796c8dcSSimon Schubert 		}
330*5796c8dcSSimon Schubert 	      else
331*5796c8dcSSimon Schubert 		sym = nsym;
332*5796c8dcSSimon Schubert 	    }
333*5796c8dcSSimon Schubert 
334*5796c8dcSSimon Schubert 	  /* Print the current arg.  */
335*5796c8dcSSimon Schubert 	  if (!first)
336*5796c8dcSSimon Schubert 	    ui_out_text (uiout, ", ");
337*5796c8dcSSimon Schubert 	  ui_out_wrap_hint (uiout, "    ");
338*5796c8dcSSimon Schubert 
339*5796c8dcSSimon Schubert 	  annotate_arg_begin ();
340*5796c8dcSSimon Schubert 
341*5796c8dcSSimon Schubert 	  list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
342*5796c8dcSSimon Schubert 	  fprintf_symbol_filtered (stb->stream, SYMBOL_PRINT_NAME (sym),
343*5796c8dcSSimon Schubert 				   SYMBOL_LANGUAGE (sym),
344*5796c8dcSSimon Schubert 				   DMGL_PARAMS | DMGL_ANSI);
345*5796c8dcSSimon Schubert 	  ui_out_field_stream (uiout, "name", stb);
346*5796c8dcSSimon Schubert 	  annotate_arg_name_end ();
347*5796c8dcSSimon Schubert 	  ui_out_text (uiout, "=");
348*5796c8dcSSimon Schubert 
349*5796c8dcSSimon Schubert           if (print_args)
350*5796c8dcSSimon Schubert             {
351*5796c8dcSSimon Schubert 	      /* Avoid value_print because it will deref ref parameters.
352*5796c8dcSSimon Schubert 		 We just want to print their addresses.  Print ??? for
353*5796c8dcSSimon Schubert 		 args whose address we do not know.  We pass 2 as
354*5796c8dcSSimon Schubert 		 "recurse" to val_print because our standard indentation
355*5796c8dcSSimon Schubert 		 here is 4 spaces, and val_print indents 2 for each
356*5796c8dcSSimon Schubert 		 recurse.  */
357*5796c8dcSSimon Schubert 	      val = read_var_value (sym, frame);
358*5796c8dcSSimon Schubert 
359*5796c8dcSSimon Schubert 	      annotate_arg_value (val == NULL ? NULL : value_type (val));
360*5796c8dcSSimon Schubert 
361*5796c8dcSSimon Schubert 	      if (val)
362*5796c8dcSSimon Schubert 	        {
363*5796c8dcSSimon Schubert                   const struct language_defn *language;
364*5796c8dcSSimon Schubert 		  struct value_print_options opts;
365*5796c8dcSSimon Schubert 
366*5796c8dcSSimon Schubert                   /* Use the appropriate language to display our symbol,
367*5796c8dcSSimon Schubert                      unless the user forced the language to a specific
368*5796c8dcSSimon Schubert                      language.  */
369*5796c8dcSSimon Schubert                   if (language_mode == language_mode_auto)
370*5796c8dcSSimon Schubert                     language = language_def (SYMBOL_LANGUAGE (sym));
371*5796c8dcSSimon Schubert                   else
372*5796c8dcSSimon Schubert                     language = current_language;
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert 		  get_raw_print_options (&opts);
375*5796c8dcSSimon Schubert 		  opts.deref_ref = 0;
376*5796c8dcSSimon Schubert 		  opts.summary = summary;
377*5796c8dcSSimon Schubert 		  common_val_print (val, stb->stream, 2, &opts, language);
378*5796c8dcSSimon Schubert 		  ui_out_field_stream (uiout, "value", stb);
379*5796c8dcSSimon Schubert 	        }
380*5796c8dcSSimon Schubert 	      else
381*5796c8dcSSimon Schubert 		ui_out_text (uiout, "???");
382*5796c8dcSSimon Schubert             }
383*5796c8dcSSimon Schubert           else
384*5796c8dcSSimon Schubert             ui_out_text (uiout, "...");
385*5796c8dcSSimon Schubert 
386*5796c8dcSSimon Schubert 
387*5796c8dcSSimon Schubert 	  /* Invoke ui_out_tuple_end.  */
388*5796c8dcSSimon Schubert 	  do_cleanups (list_chain);
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert 	  annotate_arg_end ();
391*5796c8dcSSimon Schubert 
392*5796c8dcSSimon Schubert 	  first = 0;
393*5796c8dcSSimon Schubert 	}
394*5796c8dcSSimon Schubert     }
395*5796c8dcSSimon Schubert 
396*5796c8dcSSimon Schubert   /* Don't print nameless args in situations where we don't know
397*5796c8dcSSimon Schubert      enough about the stack to find them.  */
398*5796c8dcSSimon Schubert   if (num != -1)
399*5796c8dcSSimon Schubert     {
400*5796c8dcSSimon Schubert       long start;
401*5796c8dcSSimon Schubert 
402*5796c8dcSSimon Schubert       if (highest_offset == -1)
403*5796c8dcSSimon Schubert 	start = gdbarch_frame_args_skip (get_frame_arch (frame));
404*5796c8dcSSimon Schubert       else
405*5796c8dcSSimon Schubert 	start = highest_offset;
406*5796c8dcSSimon Schubert 
407*5796c8dcSSimon Schubert       print_frame_nameless_args (frame, start, num - args_printed,
408*5796c8dcSSimon Schubert 				 first, stream);
409*5796c8dcSSimon Schubert     }
410*5796c8dcSSimon Schubert 
411*5796c8dcSSimon Schubert   do_cleanups (old_chain);
412*5796c8dcSSimon Schubert }
413*5796c8dcSSimon Schubert 
414*5796c8dcSSimon Schubert /* Stub for catch_errors.  */
415*5796c8dcSSimon Schubert 
416*5796c8dcSSimon Schubert static int
417*5796c8dcSSimon Schubert print_args_stub (void *args)
418*5796c8dcSSimon Schubert {
419*5796c8dcSSimon Schubert   struct print_args_args *p = args;
420*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (p->frame);
421*5796c8dcSSimon Schubert   int numargs;
422*5796c8dcSSimon Schubert 
423*5796c8dcSSimon Schubert   if (gdbarch_frame_num_args_p (gdbarch))
424*5796c8dcSSimon Schubert     {
425*5796c8dcSSimon Schubert       numargs = gdbarch_frame_num_args (gdbarch, p->frame);
426*5796c8dcSSimon Schubert       gdb_assert (numargs >= 0);
427*5796c8dcSSimon Schubert     }
428*5796c8dcSSimon Schubert   else
429*5796c8dcSSimon Schubert     numargs = -1;
430*5796c8dcSSimon Schubert   print_frame_args (p->func, p->frame, numargs, p->stream);
431*5796c8dcSSimon Schubert   return 0;
432*5796c8dcSSimon Schubert }
433*5796c8dcSSimon Schubert 
434*5796c8dcSSimon Schubert /* Set the current source and line to the location given by frame
435*5796c8dcSSimon Schubert    FRAME, if possible.  When CENTER is true, adjust so the relevant
436*5796c8dcSSimon Schubert    line is in the center of the next 'list'.  */
437*5796c8dcSSimon Schubert 
438*5796c8dcSSimon Schubert void
439*5796c8dcSSimon Schubert set_current_sal_from_frame (struct frame_info *frame, int center)
440*5796c8dcSSimon Schubert {
441*5796c8dcSSimon Schubert   struct symtab_and_line sal;
442*5796c8dcSSimon Schubert 
443*5796c8dcSSimon Schubert   find_frame_sal (frame, &sal);
444*5796c8dcSSimon Schubert   if (sal.symtab)
445*5796c8dcSSimon Schubert     {
446*5796c8dcSSimon Schubert       if (center)
447*5796c8dcSSimon Schubert         sal.line = max (sal.line - get_lines_to_list () / 2, 1);
448*5796c8dcSSimon Schubert       set_current_source_symtab_and_line (&sal);
449*5796c8dcSSimon Schubert     }
450*5796c8dcSSimon Schubert }
451*5796c8dcSSimon Schubert 
452*5796c8dcSSimon Schubert /* If ON, GDB will display disassembly of the next source line when
453*5796c8dcSSimon Schubert    execution of the program being debugged stops.
454*5796c8dcSSimon Schubert    If AUTO (which is the default), or there's no line info to determine
455*5796c8dcSSimon Schubert    the source line of the next instruction, display disassembly of next
456*5796c8dcSSimon Schubert    instruction instead.  */
457*5796c8dcSSimon Schubert 
458*5796c8dcSSimon Schubert static enum auto_boolean disassemble_next_line;
459*5796c8dcSSimon Schubert 
460*5796c8dcSSimon Schubert static void
461*5796c8dcSSimon Schubert show_disassemble_next_line (struct ui_file *file, int from_tty,
462*5796c8dcSSimon Schubert 				 struct cmd_list_element *c,
463*5796c8dcSSimon Schubert 				 const char *value)
464*5796c8dcSSimon Schubert {
465*5796c8dcSSimon Schubert   fprintf_filtered (file, _("\
466*5796c8dcSSimon Schubert Debugger's willingness to use disassemble-next-line is %s.\n"),
467*5796c8dcSSimon Schubert                     value);
468*5796c8dcSSimon Schubert }
469*5796c8dcSSimon Schubert 
470*5796c8dcSSimon Schubert /* Show assembly codes; stub for catch_errors.  */
471*5796c8dcSSimon Schubert 
472*5796c8dcSSimon Schubert struct gdb_disassembly_stub_args
473*5796c8dcSSimon Schubert {
474*5796c8dcSSimon Schubert   struct gdbarch *gdbarch;
475*5796c8dcSSimon Schubert   int how_many;
476*5796c8dcSSimon Schubert   CORE_ADDR low;
477*5796c8dcSSimon Schubert   CORE_ADDR high;
478*5796c8dcSSimon Schubert };
479*5796c8dcSSimon Schubert 
480*5796c8dcSSimon Schubert static void
481*5796c8dcSSimon Schubert gdb_disassembly_stub (void *args)
482*5796c8dcSSimon Schubert {
483*5796c8dcSSimon Schubert   struct gdb_disassembly_stub_args *p = args;
484*5796c8dcSSimon Schubert   gdb_disassembly (p->gdbarch, uiout, 0,
485*5796c8dcSSimon Schubert                    DISASSEMBLY_RAW_INSN, p->how_many,
486*5796c8dcSSimon Schubert                    p->low, p->high);
487*5796c8dcSSimon Schubert }
488*5796c8dcSSimon Schubert 
489*5796c8dcSSimon Schubert /* Use TRY_CATCH to catch the exception from the gdb_disassembly
490*5796c8dcSSimon Schubert    because it will be broken by filter sometime.  */
491*5796c8dcSSimon Schubert 
492*5796c8dcSSimon Schubert static void
493*5796c8dcSSimon Schubert do_gdb_disassembly (struct gdbarch *gdbarch,
494*5796c8dcSSimon Schubert 		    int how_many, CORE_ADDR low, CORE_ADDR high)
495*5796c8dcSSimon Schubert {
496*5796c8dcSSimon Schubert   volatile struct gdb_exception exception;
497*5796c8dcSSimon Schubert   struct gdb_disassembly_stub_args args;
498*5796c8dcSSimon Schubert 
499*5796c8dcSSimon Schubert   args.gdbarch = gdbarch;
500*5796c8dcSSimon Schubert   args.how_many = how_many;
501*5796c8dcSSimon Schubert   args.low = low;
502*5796c8dcSSimon Schubert   args.high = high;
503*5796c8dcSSimon Schubert   TRY_CATCH (exception, RETURN_MASK_ALL)
504*5796c8dcSSimon Schubert     {
505*5796c8dcSSimon Schubert       gdb_disassembly_stub (&args);
506*5796c8dcSSimon Schubert     }
507*5796c8dcSSimon Schubert   /* If an exception was thrown while doing the disassembly, print
508*5796c8dcSSimon Schubert      the error message, to give the user a clue of what happened.  */
509*5796c8dcSSimon Schubert   if (exception.reason == RETURN_ERROR)
510*5796c8dcSSimon Schubert     exception_print (gdb_stderr, exception);
511*5796c8dcSSimon Schubert }
512*5796c8dcSSimon Schubert 
513*5796c8dcSSimon Schubert /* Print information about frame FRAME.  The output is format according
514*5796c8dcSSimon Schubert    to PRINT_LEVEL and PRINT_WHAT and PRINT ARGS.  The meaning of
515*5796c8dcSSimon Schubert    PRINT_WHAT is:
516*5796c8dcSSimon Schubert 
517*5796c8dcSSimon Schubert    SRC_LINE: Print only source line.
518*5796c8dcSSimon Schubert    LOCATION: Print only location.
519*5796c8dcSSimon Schubert    LOC_AND_SRC: Print location and source line.
520*5796c8dcSSimon Schubert 
521*5796c8dcSSimon Schubert    Used in "where" output, and to emit breakpoint or step
522*5796c8dcSSimon Schubert    messages.  */
523*5796c8dcSSimon Schubert 
524*5796c8dcSSimon Schubert void
525*5796c8dcSSimon Schubert print_frame_info (struct frame_info *frame, int print_level,
526*5796c8dcSSimon Schubert 		  enum print_what print_what, int print_args)
527*5796c8dcSSimon Schubert {
528*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
529*5796c8dcSSimon Schubert   struct symtab_and_line sal;
530*5796c8dcSSimon Schubert   int source_print;
531*5796c8dcSSimon Schubert   int location_print;
532*5796c8dcSSimon Schubert 
533*5796c8dcSSimon Schubert   if (get_frame_type (frame) == DUMMY_FRAME
534*5796c8dcSSimon Schubert       || get_frame_type (frame) == SIGTRAMP_FRAME
535*5796c8dcSSimon Schubert       || get_frame_type (frame) == ARCH_FRAME)
536*5796c8dcSSimon Schubert     {
537*5796c8dcSSimon Schubert       struct cleanup *uiout_cleanup
538*5796c8dcSSimon Schubert 	= make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
539*5796c8dcSSimon Schubert 
540*5796c8dcSSimon Schubert       annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
541*5796c8dcSSimon Schubert 			    gdbarch, get_frame_pc (frame));
542*5796c8dcSSimon Schubert 
543*5796c8dcSSimon Schubert       /* Do this regardless of SOURCE because we don't have any source
544*5796c8dcSSimon Schubert          to list for this frame.  */
545*5796c8dcSSimon Schubert       if (print_level)
546*5796c8dcSSimon Schubert         {
547*5796c8dcSSimon Schubert           ui_out_text (uiout, "#");
548*5796c8dcSSimon Schubert           ui_out_field_fmt_int (uiout, 2, ui_left, "level",
549*5796c8dcSSimon Schubert 				frame_relative_level (frame));
550*5796c8dcSSimon Schubert         }
551*5796c8dcSSimon Schubert       if (ui_out_is_mi_like_p (uiout))
552*5796c8dcSSimon Schubert         {
553*5796c8dcSSimon Schubert           annotate_frame_address ();
554*5796c8dcSSimon Schubert           ui_out_field_core_addr (uiout, "addr",
555*5796c8dcSSimon Schubert 				  gdbarch, get_frame_pc (frame));
556*5796c8dcSSimon Schubert           annotate_frame_address_end ();
557*5796c8dcSSimon Schubert         }
558*5796c8dcSSimon Schubert 
559*5796c8dcSSimon Schubert       if (get_frame_type (frame) == DUMMY_FRAME)
560*5796c8dcSSimon Schubert         {
561*5796c8dcSSimon Schubert           annotate_function_call ();
562*5796c8dcSSimon Schubert           ui_out_field_string (uiout, "func", "<function called from gdb>");
563*5796c8dcSSimon Schubert 	}
564*5796c8dcSSimon Schubert       else if (get_frame_type (frame) == SIGTRAMP_FRAME)
565*5796c8dcSSimon Schubert         {
566*5796c8dcSSimon Schubert 	  annotate_signal_handler_caller ();
567*5796c8dcSSimon Schubert           ui_out_field_string (uiout, "func", "<signal handler called>");
568*5796c8dcSSimon Schubert         }
569*5796c8dcSSimon Schubert       else if (get_frame_type (frame) == ARCH_FRAME)
570*5796c8dcSSimon Schubert         {
571*5796c8dcSSimon Schubert           ui_out_field_string (uiout, "func", "<cross-architecture call>");
572*5796c8dcSSimon Schubert 	}
573*5796c8dcSSimon Schubert       ui_out_text (uiout, "\n");
574*5796c8dcSSimon Schubert       annotate_frame_end ();
575*5796c8dcSSimon Schubert 
576*5796c8dcSSimon Schubert       do_cleanups (uiout_cleanup);
577*5796c8dcSSimon Schubert       return;
578*5796c8dcSSimon Schubert     }
579*5796c8dcSSimon Schubert 
580*5796c8dcSSimon Schubert   /* If FRAME is not the innermost frame, that normally means that
581*5796c8dcSSimon Schubert      FRAME->pc points to *after* the call instruction, and we want to
582*5796c8dcSSimon Schubert      get the line containing the call, never the next line.  But if
583*5796c8dcSSimon Schubert      the next frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the
584*5796c8dcSSimon Schubert      next frame was not entered as the result of a call, and we want
585*5796c8dcSSimon Schubert      to get the line containing FRAME->pc.  */
586*5796c8dcSSimon Schubert   find_frame_sal (frame, &sal);
587*5796c8dcSSimon Schubert 
588*5796c8dcSSimon Schubert   location_print = (print_what == LOCATION
589*5796c8dcSSimon Schubert 		    || print_what == LOC_AND_ADDRESS
590*5796c8dcSSimon Schubert 		    || print_what == SRC_AND_LOC);
591*5796c8dcSSimon Schubert 
592*5796c8dcSSimon Schubert   if (location_print || !sal.symtab)
593*5796c8dcSSimon Schubert     print_frame (frame, print_level, print_what, print_args, sal);
594*5796c8dcSSimon Schubert 
595*5796c8dcSSimon Schubert   source_print = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
596*5796c8dcSSimon Schubert 
597*5796c8dcSSimon Schubert   /* If disassemble-next-line is set to auto or on and doesn't have
598*5796c8dcSSimon Schubert      the line debug messages for $pc, output the next instruction.  */
599*5796c8dcSSimon Schubert   if ((disassemble_next_line == AUTO_BOOLEAN_AUTO
600*5796c8dcSSimon Schubert        || disassemble_next_line == AUTO_BOOLEAN_TRUE)
601*5796c8dcSSimon Schubert       && source_print && !sal.symtab)
602*5796c8dcSSimon Schubert     do_gdb_disassembly (get_frame_arch (frame), 1,
603*5796c8dcSSimon Schubert 			get_frame_pc (frame), get_frame_pc (frame) + 1);
604*5796c8dcSSimon Schubert 
605*5796c8dcSSimon Schubert   if (source_print && sal.symtab)
606*5796c8dcSSimon Schubert     {
607*5796c8dcSSimon Schubert       int done = 0;
608*5796c8dcSSimon Schubert       int mid_statement = ((print_what == SRC_LINE)
609*5796c8dcSSimon Schubert 			   && frame_show_address (frame, sal));
610*5796c8dcSSimon Schubert 
611*5796c8dcSSimon Schubert       if (annotation_level)
612*5796c8dcSSimon Schubert 	done = identify_source_line (sal.symtab, sal.line, mid_statement,
613*5796c8dcSSimon Schubert 				     get_frame_pc (frame));
614*5796c8dcSSimon Schubert       if (!done)
615*5796c8dcSSimon Schubert 	{
616*5796c8dcSSimon Schubert 	  if (deprecated_print_frame_info_listing_hook)
617*5796c8dcSSimon Schubert 	    deprecated_print_frame_info_listing_hook (sal.symtab,
618*5796c8dcSSimon Schubert 						      sal.line,
619*5796c8dcSSimon Schubert 						      sal.line + 1, 0);
620*5796c8dcSSimon Schubert 	  else
621*5796c8dcSSimon Schubert 	    {
622*5796c8dcSSimon Schubert 	      struct value_print_options opts;
623*5796c8dcSSimon Schubert 	      get_user_print_options (&opts);
624*5796c8dcSSimon Schubert 	      /* We used to do this earlier, but that is clearly
625*5796c8dcSSimon Schubert 		 wrong. This function is used by many different
626*5796c8dcSSimon Schubert 		 parts of gdb, including normal_stop in infrun.c,
627*5796c8dcSSimon Schubert 		 which uses this to print out the current PC
628*5796c8dcSSimon Schubert 		 when we stepi/nexti into the middle of a source
629*5796c8dcSSimon Schubert 		 line. Only the command line really wants this
630*5796c8dcSSimon Schubert 		 behavior. Other UIs probably would like the
631*5796c8dcSSimon Schubert 		 ability to decide for themselves if it is desired.  */
632*5796c8dcSSimon Schubert 	      if (opts.addressprint && mid_statement)
633*5796c8dcSSimon Schubert 		{
634*5796c8dcSSimon Schubert 		  ui_out_field_core_addr (uiout, "addr",
635*5796c8dcSSimon Schubert 					  gdbarch, get_frame_pc (frame));
636*5796c8dcSSimon Schubert 		  ui_out_text (uiout, "\t");
637*5796c8dcSSimon Schubert 		}
638*5796c8dcSSimon Schubert 
639*5796c8dcSSimon Schubert 	      print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
640*5796c8dcSSimon Schubert 	    }
641*5796c8dcSSimon Schubert 	}
642*5796c8dcSSimon Schubert 
643*5796c8dcSSimon Schubert       /* If disassemble-next-line is set to on and there is line debug
644*5796c8dcSSimon Schubert          messages, output assembly codes for next line.  */
645*5796c8dcSSimon Schubert       if (disassemble_next_line == AUTO_BOOLEAN_TRUE)
646*5796c8dcSSimon Schubert 	do_gdb_disassembly (get_frame_arch (frame), -1,
647*5796c8dcSSimon Schubert 			    get_frame_pc (frame), sal.end);
648*5796c8dcSSimon Schubert     }
649*5796c8dcSSimon Schubert 
650*5796c8dcSSimon Schubert   if (print_what != LOCATION)
651*5796c8dcSSimon Schubert     set_default_breakpoint (1, get_frame_pc (frame), sal.symtab, sal.line);
652*5796c8dcSSimon Schubert 
653*5796c8dcSSimon Schubert   annotate_frame_end ();
654*5796c8dcSSimon Schubert 
655*5796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
656*5796c8dcSSimon Schubert }
657*5796c8dcSSimon Schubert 
658*5796c8dcSSimon Schubert /* Attempt to obtain the FUNNAME and FUNLANG of the function corresponding
659*5796c8dcSSimon Schubert    to FRAME.  */
660*5796c8dcSSimon Schubert void
661*5796c8dcSSimon Schubert find_frame_funname (struct frame_info *frame, char **funname,
662*5796c8dcSSimon Schubert 		    enum language *funlang)
663*5796c8dcSSimon Schubert {
664*5796c8dcSSimon Schubert   struct symbol *func;
665*5796c8dcSSimon Schubert 
666*5796c8dcSSimon Schubert   *funname = NULL;
667*5796c8dcSSimon Schubert   *funlang = language_unknown;
668*5796c8dcSSimon Schubert 
669*5796c8dcSSimon Schubert   func = get_frame_function (frame);
670*5796c8dcSSimon Schubert   if (func)
671*5796c8dcSSimon Schubert     {
672*5796c8dcSSimon Schubert       /* In certain pathological cases, the symtabs give the wrong
673*5796c8dcSSimon Schubert          function (when we are in the first function in a file which
674*5796c8dcSSimon Schubert          is compiled without debugging symbols, the previous function
675*5796c8dcSSimon Schubert          is compiled with debugging symbols, and the "foo.o" symbol
676*5796c8dcSSimon Schubert          that is supposed to tell us where the file with debugging
677*5796c8dcSSimon Schubert          symbols ends has been truncated by ar because it is longer
678*5796c8dcSSimon Schubert          than 15 characters).  This also occurs if the user uses asm()
679*5796c8dcSSimon Schubert          to create a function but not stabs for it (in a file compiled
680*5796c8dcSSimon Schubert          with -g).
681*5796c8dcSSimon Schubert 
682*5796c8dcSSimon Schubert          So look in the minimal symbol tables as well, and if it comes
683*5796c8dcSSimon Schubert          up with a larger address for the function use that instead.
684*5796c8dcSSimon Schubert          I don't think this can ever cause any problems; there
685*5796c8dcSSimon Schubert          shouldn't be any minimal symbols in the middle of a function;
686*5796c8dcSSimon Schubert          if this is ever changed many parts of GDB will need to be
687*5796c8dcSSimon Schubert          changed (and we'll create a find_pc_minimal_function or some
688*5796c8dcSSimon Schubert          such).  */
689*5796c8dcSSimon Schubert 
690*5796c8dcSSimon Schubert       struct minimal_symbol *msymbol = NULL;
691*5796c8dcSSimon Schubert 
692*5796c8dcSSimon Schubert       /* Don't attempt to do this for inlined functions, which do not
693*5796c8dcSSimon Schubert 	 have a corresponding minimal symbol.  */
694*5796c8dcSSimon Schubert       if (!block_inlined_p (SYMBOL_BLOCK_VALUE (func)))
695*5796c8dcSSimon Schubert 	msymbol
696*5796c8dcSSimon Schubert 	  = lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
697*5796c8dcSSimon Schubert 
698*5796c8dcSSimon Schubert       if (msymbol != NULL
699*5796c8dcSSimon Schubert 	  && (SYMBOL_VALUE_ADDRESS (msymbol)
700*5796c8dcSSimon Schubert 	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
701*5796c8dcSSimon Schubert 	{
702*5796c8dcSSimon Schubert 	  /* We also don't know anything about the function besides
703*5796c8dcSSimon Schubert 	     its address and name.  */
704*5796c8dcSSimon Schubert 	  func = 0;
705*5796c8dcSSimon Schubert 	  *funname = SYMBOL_PRINT_NAME (msymbol);
706*5796c8dcSSimon Schubert 	  *funlang = SYMBOL_LANGUAGE (msymbol);
707*5796c8dcSSimon Schubert 	}
708*5796c8dcSSimon Schubert       else
709*5796c8dcSSimon Schubert 	{
710*5796c8dcSSimon Schubert 	  *funname = SYMBOL_PRINT_NAME (func);
711*5796c8dcSSimon Schubert 	  *funlang = SYMBOL_LANGUAGE (func);
712*5796c8dcSSimon Schubert 	  if (*funlang == language_cplus)
713*5796c8dcSSimon Schubert 	    {
714*5796c8dcSSimon Schubert 	      /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
715*5796c8dcSSimon Schubert 		 to display the demangled name that we already have
716*5796c8dcSSimon Schubert 		 stored in the symbol table, but we stored a version
717*5796c8dcSSimon Schubert 		 with DMGL_PARAMS turned on, and here we don't want to
718*5796c8dcSSimon Schubert 		 display parameters.  So remove the parameters.  */
719*5796c8dcSSimon Schubert 	      char *func_only = cp_remove_params (*funname);
720*5796c8dcSSimon Schubert 	      if (func_only)
721*5796c8dcSSimon Schubert 		{
722*5796c8dcSSimon Schubert 		  *funname = func_only;
723*5796c8dcSSimon Schubert 		  make_cleanup (xfree, func_only);
724*5796c8dcSSimon Schubert 		}
725*5796c8dcSSimon Schubert 	    }
726*5796c8dcSSimon Schubert 	}
727*5796c8dcSSimon Schubert     }
728*5796c8dcSSimon Schubert   else
729*5796c8dcSSimon Schubert     {
730*5796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
731*5796c8dcSSimon Schubert 	lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
732*5796c8dcSSimon Schubert 
733*5796c8dcSSimon Schubert       if (msymbol != NULL)
734*5796c8dcSSimon Schubert 	{
735*5796c8dcSSimon Schubert 	  *funname = SYMBOL_PRINT_NAME (msymbol);
736*5796c8dcSSimon Schubert 	  *funlang = SYMBOL_LANGUAGE (msymbol);
737*5796c8dcSSimon Schubert 	}
738*5796c8dcSSimon Schubert     }
739*5796c8dcSSimon Schubert }
740*5796c8dcSSimon Schubert 
741*5796c8dcSSimon Schubert static void
742*5796c8dcSSimon Schubert print_frame (struct frame_info *frame, int print_level,
743*5796c8dcSSimon Schubert 	     enum print_what print_what, int print_args,
744*5796c8dcSSimon Schubert 	     struct symtab_and_line sal)
745*5796c8dcSSimon Schubert {
746*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
747*5796c8dcSSimon Schubert   char *funname = NULL;
748*5796c8dcSSimon Schubert   enum language funlang = language_unknown;
749*5796c8dcSSimon Schubert   struct ui_stream *stb;
750*5796c8dcSSimon Schubert   struct cleanup *old_chain, *list_chain;
751*5796c8dcSSimon Schubert   struct value_print_options opts;
752*5796c8dcSSimon Schubert 
753*5796c8dcSSimon Schubert   stb = ui_out_stream_new (uiout);
754*5796c8dcSSimon Schubert   old_chain = make_cleanup_ui_out_stream_delete (stb);
755*5796c8dcSSimon Schubert 
756*5796c8dcSSimon Schubert   find_frame_funname (frame, &funname, &funlang);
757*5796c8dcSSimon Schubert 
758*5796c8dcSSimon Schubert   annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
759*5796c8dcSSimon Schubert 			gdbarch, get_frame_pc (frame));
760*5796c8dcSSimon Schubert 
761*5796c8dcSSimon Schubert   list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
762*5796c8dcSSimon Schubert 
763*5796c8dcSSimon Schubert   if (print_level)
764*5796c8dcSSimon Schubert     {
765*5796c8dcSSimon Schubert       ui_out_text (uiout, "#");
766*5796c8dcSSimon Schubert       ui_out_field_fmt_int (uiout, 2, ui_left, "level",
767*5796c8dcSSimon Schubert 			    frame_relative_level (frame));
768*5796c8dcSSimon Schubert     }
769*5796c8dcSSimon Schubert   get_user_print_options (&opts);
770*5796c8dcSSimon Schubert   if (opts.addressprint)
771*5796c8dcSSimon Schubert     if (frame_show_address (frame, sal) || !sal.symtab
772*5796c8dcSSimon Schubert 	|| print_what == LOC_AND_ADDRESS)
773*5796c8dcSSimon Schubert       {
774*5796c8dcSSimon Schubert 	annotate_frame_address ();
775*5796c8dcSSimon Schubert 	ui_out_field_core_addr (uiout, "addr", gdbarch, get_frame_pc (frame));
776*5796c8dcSSimon Schubert 	annotate_frame_address_end ();
777*5796c8dcSSimon Schubert 	ui_out_text (uiout, " in ");
778*5796c8dcSSimon Schubert       }
779*5796c8dcSSimon Schubert   annotate_frame_function_name ();
780*5796c8dcSSimon Schubert   fprintf_symbol_filtered (stb->stream, funname ? funname : "??",
781*5796c8dcSSimon Schubert 			   funlang, DMGL_ANSI);
782*5796c8dcSSimon Schubert   ui_out_field_stream (uiout, "func", stb);
783*5796c8dcSSimon Schubert   ui_out_wrap_hint (uiout, "   ");
784*5796c8dcSSimon Schubert   annotate_frame_args ();
785*5796c8dcSSimon Schubert 
786*5796c8dcSSimon Schubert   ui_out_text (uiout, " (");
787*5796c8dcSSimon Schubert   if (print_args)
788*5796c8dcSSimon Schubert     {
789*5796c8dcSSimon Schubert       struct print_args_args args;
790*5796c8dcSSimon Schubert       struct cleanup *args_list_chain;
791*5796c8dcSSimon Schubert       args.frame = frame;
792*5796c8dcSSimon Schubert       args.func = find_pc_function (get_frame_address_in_block (frame));
793*5796c8dcSSimon Schubert       args.stream = gdb_stdout;
794*5796c8dcSSimon Schubert       args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
795*5796c8dcSSimon Schubert       catch_errors (print_args_stub, &args, "", RETURN_MASK_ERROR);
796*5796c8dcSSimon Schubert       /* FIXME: ARGS must be a list. If one argument is a string it
797*5796c8dcSSimon Schubert 	  will have " that will not be properly escaped.  */
798*5796c8dcSSimon Schubert       /* Invoke ui_out_tuple_end.  */
799*5796c8dcSSimon Schubert       do_cleanups (args_list_chain);
800*5796c8dcSSimon Schubert       QUIT;
801*5796c8dcSSimon Schubert     }
802*5796c8dcSSimon Schubert   ui_out_text (uiout, ")");
803*5796c8dcSSimon Schubert   if (sal.symtab && sal.symtab->filename)
804*5796c8dcSSimon Schubert     {
805*5796c8dcSSimon Schubert       annotate_frame_source_begin ();
806*5796c8dcSSimon Schubert       ui_out_wrap_hint (uiout, "   ");
807*5796c8dcSSimon Schubert       ui_out_text (uiout, " at ");
808*5796c8dcSSimon Schubert       annotate_frame_source_file ();
809*5796c8dcSSimon Schubert       ui_out_field_string (uiout, "file", sal.symtab->filename);
810*5796c8dcSSimon Schubert       if (ui_out_is_mi_like_p (uiout))
811*5796c8dcSSimon Schubert 	{
812*5796c8dcSSimon Schubert 	  const char *fullname = symtab_to_fullname (sal.symtab);
813*5796c8dcSSimon Schubert 	  if (fullname != NULL)
814*5796c8dcSSimon Schubert 	    ui_out_field_string (uiout, "fullname", fullname);
815*5796c8dcSSimon Schubert 	}
816*5796c8dcSSimon Schubert       annotate_frame_source_file_end ();
817*5796c8dcSSimon Schubert       ui_out_text (uiout, ":");
818*5796c8dcSSimon Schubert       annotate_frame_source_line ();
819*5796c8dcSSimon Schubert       ui_out_field_int (uiout, "line", sal.line);
820*5796c8dcSSimon Schubert       annotate_frame_source_end ();
821*5796c8dcSSimon Schubert     }
822*5796c8dcSSimon Schubert 
823*5796c8dcSSimon Schubert   if (!funname || (!sal.symtab || !sal.symtab->filename))
824*5796c8dcSSimon Schubert     {
825*5796c8dcSSimon Schubert #ifdef PC_SOLIB
826*5796c8dcSSimon Schubert       char *lib = PC_SOLIB (get_frame_pc (frame));
827*5796c8dcSSimon Schubert #else
828*5796c8dcSSimon Schubert       char *lib = solib_name_from_address (get_frame_pc (frame));
829*5796c8dcSSimon Schubert #endif
830*5796c8dcSSimon Schubert       if (lib)
831*5796c8dcSSimon Schubert 	{
832*5796c8dcSSimon Schubert 	  annotate_frame_where ();
833*5796c8dcSSimon Schubert 	  ui_out_wrap_hint (uiout, "  ");
834*5796c8dcSSimon Schubert 	  ui_out_text (uiout, " from ");
835*5796c8dcSSimon Schubert 	  ui_out_field_string (uiout, "from", lib);
836*5796c8dcSSimon Schubert 	}
837*5796c8dcSSimon Schubert     }
838*5796c8dcSSimon Schubert 
839*5796c8dcSSimon Schubert   /* do_cleanups will call ui_out_tuple_end() for us.  */
840*5796c8dcSSimon Schubert   do_cleanups (list_chain);
841*5796c8dcSSimon Schubert   ui_out_text (uiout, "\n");
842*5796c8dcSSimon Schubert   do_cleanups (old_chain);
843*5796c8dcSSimon Schubert }
844*5796c8dcSSimon Schubert 
845*5796c8dcSSimon Schubert 
846*5796c8dcSSimon Schubert /* Read a frame specification in whatever the appropriate format is
847*5796c8dcSSimon Schubert    from FRAME_EXP.  Call error(), printing MESSAGE, if the
848*5796c8dcSSimon Schubert    specification is in any way invalid (so this function never returns
849*5796c8dcSSimon Schubert    NULL).  When SEPECTED_P is non-NULL set its target to indicate that
850*5796c8dcSSimon Schubert    the default selected frame was used.  */
851*5796c8dcSSimon Schubert 
852*5796c8dcSSimon Schubert static struct frame_info *
853*5796c8dcSSimon Schubert parse_frame_specification_1 (const char *frame_exp, const char *message,
854*5796c8dcSSimon Schubert 			     int *selected_frame_p)
855*5796c8dcSSimon Schubert {
856*5796c8dcSSimon Schubert   int numargs;
857*5796c8dcSSimon Schubert   struct value *args[4];
858*5796c8dcSSimon Schubert   CORE_ADDR addrs[ARRAY_SIZE (args)];
859*5796c8dcSSimon Schubert 
860*5796c8dcSSimon Schubert   if (frame_exp == NULL)
861*5796c8dcSSimon Schubert     numargs = 0;
862*5796c8dcSSimon Schubert   else
863*5796c8dcSSimon Schubert     {
864*5796c8dcSSimon Schubert       char *addr_string;
865*5796c8dcSSimon Schubert       struct cleanup *tmp_cleanup;
866*5796c8dcSSimon Schubert 
867*5796c8dcSSimon Schubert       numargs = 0;
868*5796c8dcSSimon Schubert       while (1)
869*5796c8dcSSimon Schubert 	{
870*5796c8dcSSimon Schubert 	  char *addr_string;
871*5796c8dcSSimon Schubert 	  struct cleanup *cleanup;
872*5796c8dcSSimon Schubert 	  const char *p;
873*5796c8dcSSimon Schubert 
874*5796c8dcSSimon Schubert 	  /* Skip leading white space, bail of EOL.  */
875*5796c8dcSSimon Schubert 	  while (isspace (*frame_exp))
876*5796c8dcSSimon Schubert 	    frame_exp++;
877*5796c8dcSSimon Schubert 	  if (!*frame_exp)
878*5796c8dcSSimon Schubert 	    break;
879*5796c8dcSSimon Schubert 
880*5796c8dcSSimon Schubert 	  /* Parse the argument, extract it, save it.  */
881*5796c8dcSSimon Schubert 	  for (p = frame_exp;
882*5796c8dcSSimon Schubert 	       *p && !isspace (*p);
883*5796c8dcSSimon Schubert 	       p++);
884*5796c8dcSSimon Schubert 	  addr_string = savestring (frame_exp, p - frame_exp);
885*5796c8dcSSimon Schubert 	  frame_exp = p;
886*5796c8dcSSimon Schubert 	  cleanup = make_cleanup (xfree, addr_string);
887*5796c8dcSSimon Schubert 
888*5796c8dcSSimon Schubert 	  /* NOTE: Parse and evaluate expression, but do not use
889*5796c8dcSSimon Schubert 	     functions such as parse_and_eval_long or
890*5796c8dcSSimon Schubert 	     parse_and_eval_address to also extract the value.
891*5796c8dcSSimon Schubert 	     Instead value_as_long and value_as_address are used.
892*5796c8dcSSimon Schubert 	     This avoids problems with expressions that contain
893*5796c8dcSSimon Schubert 	     side-effects.  */
894*5796c8dcSSimon Schubert 	  if (numargs >= ARRAY_SIZE (args))
895*5796c8dcSSimon Schubert 	    error (_("Too many args in frame specification"));
896*5796c8dcSSimon Schubert 	  args[numargs++] = parse_and_eval (addr_string);
897*5796c8dcSSimon Schubert 
898*5796c8dcSSimon Schubert 	  do_cleanups (cleanup);
899*5796c8dcSSimon Schubert 	}
900*5796c8dcSSimon Schubert     }
901*5796c8dcSSimon Schubert 
902*5796c8dcSSimon Schubert   /* If no args, default to the selected frame.  */
903*5796c8dcSSimon Schubert   if (numargs == 0)
904*5796c8dcSSimon Schubert     {
905*5796c8dcSSimon Schubert       if (selected_frame_p != NULL)
906*5796c8dcSSimon Schubert 	(*selected_frame_p) = 1;
907*5796c8dcSSimon Schubert       return get_selected_frame (message);
908*5796c8dcSSimon Schubert     }
909*5796c8dcSSimon Schubert 
910*5796c8dcSSimon Schubert   /* None of the remaining use the selected frame.  */
911*5796c8dcSSimon Schubert   if (selected_frame_p != NULL)
912*5796c8dcSSimon Schubert     (*selected_frame_p) = 0;
913*5796c8dcSSimon Schubert 
914*5796c8dcSSimon Schubert   /* Assume the single arg[0] is an integer, and try using that to
915*5796c8dcSSimon Schubert      select a frame relative to current.  */
916*5796c8dcSSimon Schubert   if (numargs == 1)
917*5796c8dcSSimon Schubert     {
918*5796c8dcSSimon Schubert       struct frame_info *fid;
919*5796c8dcSSimon Schubert       int level = value_as_long (args[0]);
920*5796c8dcSSimon Schubert       fid = find_relative_frame (get_current_frame (), &level);
921*5796c8dcSSimon Schubert       if (level == 0)
922*5796c8dcSSimon Schubert 	/* find_relative_frame was successful */
923*5796c8dcSSimon Schubert 	return fid;
924*5796c8dcSSimon Schubert     }
925*5796c8dcSSimon Schubert 
926*5796c8dcSSimon Schubert   /* Convert each value into a corresponding address.  */
927*5796c8dcSSimon Schubert   {
928*5796c8dcSSimon Schubert     int i;
929*5796c8dcSSimon Schubert     for (i = 0; i < numargs; i++)
930*5796c8dcSSimon Schubert       addrs[i] = value_as_address (args[i]);
931*5796c8dcSSimon Schubert   }
932*5796c8dcSSimon Schubert 
933*5796c8dcSSimon Schubert   /* Assume that the single arg[0] is an address, use that to identify
934*5796c8dcSSimon Schubert      a frame with a matching ID.  Should this also accept stack/pc or
935*5796c8dcSSimon Schubert      stack/pc/special.  */
936*5796c8dcSSimon Schubert   if (numargs == 1)
937*5796c8dcSSimon Schubert     {
938*5796c8dcSSimon Schubert       struct frame_id id = frame_id_build_wild (addrs[0]);
939*5796c8dcSSimon Schubert       struct frame_info *fid;
940*5796c8dcSSimon Schubert 
941*5796c8dcSSimon Schubert       /* If (s)he specifies the frame with an address, he deserves
942*5796c8dcSSimon Schubert 	 what (s)he gets.  Still, give the highest one that matches.
943*5796c8dcSSimon Schubert 	 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
944*5796c8dcSSimon Schubert 	 know).  */
945*5796c8dcSSimon Schubert       for (fid = get_current_frame ();
946*5796c8dcSSimon Schubert 	   fid != NULL;
947*5796c8dcSSimon Schubert 	   fid = get_prev_frame (fid))
948*5796c8dcSSimon Schubert 	{
949*5796c8dcSSimon Schubert 	  if (frame_id_eq (id, get_frame_id (fid)))
950*5796c8dcSSimon Schubert 	    {
951*5796c8dcSSimon Schubert 	      struct frame_info *prev_frame;
952*5796c8dcSSimon Schubert 
953*5796c8dcSSimon Schubert 	      while (1)
954*5796c8dcSSimon Schubert 		{
955*5796c8dcSSimon Schubert 		  prev_frame = get_prev_frame (fid);
956*5796c8dcSSimon Schubert 		  if (!prev_frame
957*5796c8dcSSimon Schubert 		      || !frame_id_eq (id, get_frame_id (prev_frame)))
958*5796c8dcSSimon Schubert 		    break;
959*5796c8dcSSimon Schubert 		  fid = prev_frame;
960*5796c8dcSSimon Schubert 		}
961*5796c8dcSSimon Schubert 	      return fid;
962*5796c8dcSSimon Schubert 	    }
963*5796c8dcSSimon Schubert 	}
964*5796c8dcSSimon Schubert       }
965*5796c8dcSSimon Schubert 
966*5796c8dcSSimon Schubert   /* We couldn't identify the frame as an existing frame, but
967*5796c8dcSSimon Schubert      perhaps we can create one with a single argument.  */
968*5796c8dcSSimon Schubert   if (numargs == 1)
969*5796c8dcSSimon Schubert     return create_new_frame (addrs[0], 0);
970*5796c8dcSSimon Schubert   else if (numargs == 2)
971*5796c8dcSSimon Schubert     return create_new_frame (addrs[0], addrs[1]);
972*5796c8dcSSimon Schubert   else
973*5796c8dcSSimon Schubert     error (_("Too many args in frame specification"));
974*5796c8dcSSimon Schubert }
975*5796c8dcSSimon Schubert 
976*5796c8dcSSimon Schubert static struct frame_info *
977*5796c8dcSSimon Schubert parse_frame_specification (char *frame_exp)
978*5796c8dcSSimon Schubert {
979*5796c8dcSSimon Schubert   return parse_frame_specification_1 (frame_exp, NULL, NULL);
980*5796c8dcSSimon Schubert }
981*5796c8dcSSimon Schubert 
982*5796c8dcSSimon Schubert /* Print verbosely the selected frame or the frame at address
983*5796c8dcSSimon Schubert    ADDR_EXP.  Absolutely all information in the frame is printed.  */
984*5796c8dcSSimon Schubert 
985*5796c8dcSSimon Schubert static void
986*5796c8dcSSimon Schubert frame_info (char *addr_exp, int from_tty)
987*5796c8dcSSimon Schubert {
988*5796c8dcSSimon Schubert   struct frame_info *fi;
989*5796c8dcSSimon Schubert   struct symtab_and_line sal;
990*5796c8dcSSimon Schubert   struct symbol *func;
991*5796c8dcSSimon Schubert   struct symtab *s;
992*5796c8dcSSimon Schubert   struct frame_info *calling_frame_info;
993*5796c8dcSSimon Schubert   int i, count, numregs;
994*5796c8dcSSimon Schubert   char *funname = 0;
995*5796c8dcSSimon Schubert   enum language funlang = language_unknown;
996*5796c8dcSSimon Schubert   const char *pc_regname;
997*5796c8dcSSimon Schubert   int selected_frame_p;
998*5796c8dcSSimon Schubert   struct gdbarch *gdbarch;
999*5796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
1000*5796c8dcSSimon Schubert 
1001*5796c8dcSSimon Schubert   fi = parse_frame_specification_1 (addr_exp, "No stack.", &selected_frame_p);
1002*5796c8dcSSimon Schubert   gdbarch = get_frame_arch (fi);
1003*5796c8dcSSimon Schubert 
1004*5796c8dcSSimon Schubert   /* Name of the value returned by get_frame_pc().  Per comments, "pc"
1005*5796c8dcSSimon Schubert      is not a good name.  */
1006*5796c8dcSSimon Schubert   if (gdbarch_pc_regnum (gdbarch) >= 0)
1007*5796c8dcSSimon Schubert     /* OK, this is weird.  The gdbarch_pc_regnum hardware register's value can
1008*5796c8dcSSimon Schubert        easily not match that of the internal value returned by
1009*5796c8dcSSimon Schubert        get_frame_pc().  */
1010*5796c8dcSSimon Schubert     pc_regname = gdbarch_register_name (gdbarch, gdbarch_pc_regnum (gdbarch));
1011*5796c8dcSSimon Schubert   else
1012*5796c8dcSSimon Schubert     /* But then, this is weird to.  Even without gdbarch_pc_regnum, an
1013*5796c8dcSSimon Schubert        architectures will often have a hardware register called "pc",
1014*5796c8dcSSimon Schubert        and that register's value, again, can easily not match
1015*5796c8dcSSimon Schubert        get_frame_pc().  */
1016*5796c8dcSSimon Schubert     pc_regname = "pc";
1017*5796c8dcSSimon Schubert 
1018*5796c8dcSSimon Schubert   find_frame_sal (fi, &sal);
1019*5796c8dcSSimon Schubert   func = get_frame_function (fi);
1020*5796c8dcSSimon Schubert   /* FIXME: cagney/2002-11-28: Why bother?  Won't sal.symtab contain
1021*5796c8dcSSimon Schubert      the same value?  */
1022*5796c8dcSSimon Schubert   s = find_pc_symtab (get_frame_pc (fi));
1023*5796c8dcSSimon Schubert   if (func)
1024*5796c8dcSSimon Schubert     {
1025*5796c8dcSSimon Schubert       funname = SYMBOL_PRINT_NAME (func);
1026*5796c8dcSSimon Schubert       funlang = SYMBOL_LANGUAGE (func);
1027*5796c8dcSSimon Schubert       if (funlang == language_cplus)
1028*5796c8dcSSimon Schubert 	{
1029*5796c8dcSSimon Schubert 	  /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
1030*5796c8dcSSimon Schubert 	     to display the demangled name that we already have
1031*5796c8dcSSimon Schubert 	     stored in the symbol table, but we stored a version
1032*5796c8dcSSimon Schubert 	     with DMGL_PARAMS turned on, and here we don't want to
1033*5796c8dcSSimon Schubert 	     display parameters.  So remove the parameters.  */
1034*5796c8dcSSimon Schubert 	  char *func_only = cp_remove_params (funname);
1035*5796c8dcSSimon Schubert 	  if (func_only)
1036*5796c8dcSSimon Schubert 	    {
1037*5796c8dcSSimon Schubert 	      funname = func_only;
1038*5796c8dcSSimon Schubert 	      make_cleanup (xfree, func_only);
1039*5796c8dcSSimon Schubert 	    }
1040*5796c8dcSSimon Schubert 	}
1041*5796c8dcSSimon Schubert     }
1042*5796c8dcSSimon Schubert   else
1043*5796c8dcSSimon Schubert     {
1044*5796c8dcSSimon Schubert       struct minimal_symbol *msymbol;
1045*5796c8dcSSimon Schubert 
1046*5796c8dcSSimon Schubert       msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
1047*5796c8dcSSimon Schubert       if (msymbol != NULL)
1048*5796c8dcSSimon Schubert 	{
1049*5796c8dcSSimon Schubert 	  funname = SYMBOL_PRINT_NAME (msymbol);
1050*5796c8dcSSimon Schubert 	  funlang = SYMBOL_LANGUAGE (msymbol);
1051*5796c8dcSSimon Schubert 	}
1052*5796c8dcSSimon Schubert     }
1053*5796c8dcSSimon Schubert   calling_frame_info = get_prev_frame (fi);
1054*5796c8dcSSimon Schubert 
1055*5796c8dcSSimon Schubert   if (selected_frame_p && frame_relative_level (fi) >= 0)
1056*5796c8dcSSimon Schubert     {
1057*5796c8dcSSimon Schubert       printf_filtered (_("Stack level %d, frame at "),
1058*5796c8dcSSimon Schubert 		       frame_relative_level (fi));
1059*5796c8dcSSimon Schubert     }
1060*5796c8dcSSimon Schubert   else
1061*5796c8dcSSimon Schubert     {
1062*5796c8dcSSimon Schubert       printf_filtered (_("Stack frame at "));
1063*5796c8dcSSimon Schubert     }
1064*5796c8dcSSimon Schubert   fputs_filtered (paddress (gdbarch, get_frame_base (fi)), gdb_stdout);
1065*5796c8dcSSimon Schubert   printf_filtered (":\n");
1066*5796c8dcSSimon Schubert   printf_filtered (" %s = ", pc_regname);
1067*5796c8dcSSimon Schubert   fputs_filtered (paddress (gdbarch, get_frame_pc (fi)), gdb_stdout);
1068*5796c8dcSSimon Schubert 
1069*5796c8dcSSimon Schubert   wrap_here ("   ");
1070*5796c8dcSSimon Schubert   if (funname)
1071*5796c8dcSSimon Schubert     {
1072*5796c8dcSSimon Schubert       printf_filtered (" in ");
1073*5796c8dcSSimon Schubert       fprintf_symbol_filtered (gdb_stdout, funname, funlang,
1074*5796c8dcSSimon Schubert 			       DMGL_ANSI | DMGL_PARAMS);
1075*5796c8dcSSimon Schubert     }
1076*5796c8dcSSimon Schubert   wrap_here ("   ");
1077*5796c8dcSSimon Schubert   if (sal.symtab)
1078*5796c8dcSSimon Schubert     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
1079*5796c8dcSSimon Schubert   puts_filtered ("; ");
1080*5796c8dcSSimon Schubert   wrap_here ("    ");
1081*5796c8dcSSimon Schubert   printf_filtered ("saved %s ", pc_regname);
1082*5796c8dcSSimon Schubert   fputs_filtered (paddress (gdbarch, frame_unwind_caller_pc (fi)), gdb_stdout);
1083*5796c8dcSSimon Schubert   printf_filtered ("\n");
1084*5796c8dcSSimon Schubert 
1085*5796c8dcSSimon Schubert   if (calling_frame_info == NULL)
1086*5796c8dcSSimon Schubert     {
1087*5796c8dcSSimon Schubert       enum unwind_stop_reason reason;
1088*5796c8dcSSimon Schubert 
1089*5796c8dcSSimon Schubert       reason = get_frame_unwind_stop_reason (fi);
1090*5796c8dcSSimon Schubert       if (reason != UNWIND_NO_REASON)
1091*5796c8dcSSimon Schubert 	printf_filtered (_(" Outermost frame: %s\n"),
1092*5796c8dcSSimon Schubert 			 frame_stop_reason_string (reason));
1093*5796c8dcSSimon Schubert     }
1094*5796c8dcSSimon Schubert   else if (get_frame_type (fi) == INLINE_FRAME)
1095*5796c8dcSSimon Schubert     printf_filtered (" inlined into frame %d",
1096*5796c8dcSSimon Schubert 		     frame_relative_level (get_prev_frame (fi)));
1097*5796c8dcSSimon Schubert   else
1098*5796c8dcSSimon Schubert     {
1099*5796c8dcSSimon Schubert       printf_filtered (" called by frame at ");
1100*5796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, get_frame_base (calling_frame_info)),
1101*5796c8dcSSimon Schubert 		      gdb_stdout);
1102*5796c8dcSSimon Schubert     }
1103*5796c8dcSSimon Schubert   if (get_next_frame (fi) && calling_frame_info)
1104*5796c8dcSSimon Schubert     puts_filtered (",");
1105*5796c8dcSSimon Schubert   wrap_here ("   ");
1106*5796c8dcSSimon Schubert   if (get_next_frame (fi))
1107*5796c8dcSSimon Schubert     {
1108*5796c8dcSSimon Schubert       printf_filtered (" caller of frame at ");
1109*5796c8dcSSimon Schubert       fputs_filtered (paddress (gdbarch, get_frame_base (get_next_frame (fi))),
1110*5796c8dcSSimon Schubert 		      gdb_stdout);
1111*5796c8dcSSimon Schubert     }
1112*5796c8dcSSimon Schubert   if (get_next_frame (fi) || calling_frame_info)
1113*5796c8dcSSimon Schubert     puts_filtered ("\n");
1114*5796c8dcSSimon Schubert 
1115*5796c8dcSSimon Schubert   if (s)
1116*5796c8dcSSimon Schubert     printf_filtered (" source language %s.\n",
1117*5796c8dcSSimon Schubert 		     language_str (s->language));
1118*5796c8dcSSimon Schubert 
1119*5796c8dcSSimon Schubert   {
1120*5796c8dcSSimon Schubert     /* Address of the argument list for this frame, or 0.  */
1121*5796c8dcSSimon Schubert     CORE_ADDR arg_list = get_frame_args_address (fi);
1122*5796c8dcSSimon Schubert     /* Number of args for this frame, or -1 if unknown.  */
1123*5796c8dcSSimon Schubert     int numargs;
1124*5796c8dcSSimon Schubert 
1125*5796c8dcSSimon Schubert     if (arg_list == 0)
1126*5796c8dcSSimon Schubert       printf_filtered (" Arglist at unknown address.\n");
1127*5796c8dcSSimon Schubert     else
1128*5796c8dcSSimon Schubert       {
1129*5796c8dcSSimon Schubert 	printf_filtered (" Arglist at ");
1130*5796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1131*5796c8dcSSimon Schubert 	printf_filtered (",");
1132*5796c8dcSSimon Schubert 
1133*5796c8dcSSimon Schubert 	if (!gdbarch_frame_num_args_p (gdbarch))
1134*5796c8dcSSimon Schubert 	  {
1135*5796c8dcSSimon Schubert 	    numargs = -1;
1136*5796c8dcSSimon Schubert 	    puts_filtered (" args: ");
1137*5796c8dcSSimon Schubert 	  }
1138*5796c8dcSSimon Schubert 	else
1139*5796c8dcSSimon Schubert 	  {
1140*5796c8dcSSimon Schubert 	    numargs = gdbarch_frame_num_args (gdbarch, fi);
1141*5796c8dcSSimon Schubert 	    gdb_assert (numargs >= 0);
1142*5796c8dcSSimon Schubert 	    if (numargs == 0)
1143*5796c8dcSSimon Schubert 	      puts_filtered (" no args.");
1144*5796c8dcSSimon Schubert 	    else if (numargs == 1)
1145*5796c8dcSSimon Schubert 	      puts_filtered (" 1 arg: ");
1146*5796c8dcSSimon Schubert 	    else
1147*5796c8dcSSimon Schubert 	      printf_filtered (" %d args: ", numargs);
1148*5796c8dcSSimon Schubert 	  }
1149*5796c8dcSSimon Schubert 	print_frame_args (func, fi, numargs, gdb_stdout);
1150*5796c8dcSSimon Schubert 	puts_filtered ("\n");
1151*5796c8dcSSimon Schubert       }
1152*5796c8dcSSimon Schubert   }
1153*5796c8dcSSimon Schubert   {
1154*5796c8dcSSimon Schubert     /* Address of the local variables for this frame, or 0.  */
1155*5796c8dcSSimon Schubert     CORE_ADDR arg_list = get_frame_locals_address (fi);
1156*5796c8dcSSimon Schubert 
1157*5796c8dcSSimon Schubert     if (arg_list == 0)
1158*5796c8dcSSimon Schubert       printf_filtered (" Locals at unknown address,");
1159*5796c8dcSSimon Schubert     else
1160*5796c8dcSSimon Schubert       {
1161*5796c8dcSSimon Schubert 	printf_filtered (" Locals at ");
1162*5796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1163*5796c8dcSSimon Schubert 	printf_filtered (",");
1164*5796c8dcSSimon Schubert       }
1165*5796c8dcSSimon Schubert   }
1166*5796c8dcSSimon Schubert 
1167*5796c8dcSSimon Schubert   /* Print as much information as possible on the location of all the
1168*5796c8dcSSimon Schubert      registers.  */
1169*5796c8dcSSimon Schubert   {
1170*5796c8dcSSimon Schubert     enum lval_type lval;
1171*5796c8dcSSimon Schubert     int optimized;
1172*5796c8dcSSimon Schubert     CORE_ADDR addr;
1173*5796c8dcSSimon Schubert     int realnum;
1174*5796c8dcSSimon Schubert     int count;
1175*5796c8dcSSimon Schubert     int i;
1176*5796c8dcSSimon Schubert     int need_nl = 1;
1177*5796c8dcSSimon Schubert 
1178*5796c8dcSSimon Schubert     /* The sp is special; what's displayed isn't the save address, but
1179*5796c8dcSSimon Schubert        the value of the previous frame's sp.  This is a legacy thing,
1180*5796c8dcSSimon Schubert        at one stage the frame cached the previous frame's SP instead
1181*5796c8dcSSimon Schubert        of its address, hence it was easiest to just display the cached
1182*5796c8dcSSimon Schubert        value.  */
1183*5796c8dcSSimon Schubert     if (gdbarch_sp_regnum (gdbarch) >= 0)
1184*5796c8dcSSimon Schubert       {
1185*5796c8dcSSimon Schubert 	/* Find out the location of the saved stack pointer with out
1186*5796c8dcSSimon Schubert            actually evaluating it.  */
1187*5796c8dcSSimon Schubert 	frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1188*5796c8dcSSimon Schubert 			       &optimized, &lval, &addr,
1189*5796c8dcSSimon Schubert 			       &realnum, NULL);
1190*5796c8dcSSimon Schubert 	if (!optimized && lval == not_lval)
1191*5796c8dcSSimon Schubert 	  {
1192*5796c8dcSSimon Schubert 	    enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1193*5796c8dcSSimon Schubert 	    int sp_size = register_size (gdbarch, gdbarch_sp_regnum (gdbarch));
1194*5796c8dcSSimon Schubert 	    gdb_byte value[MAX_REGISTER_SIZE];
1195*5796c8dcSSimon Schubert 	    CORE_ADDR sp;
1196*5796c8dcSSimon Schubert 	    frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1197*5796c8dcSSimon Schubert 				   &optimized, &lval, &addr,
1198*5796c8dcSSimon Schubert 				   &realnum, value);
1199*5796c8dcSSimon Schubert 	    /* NOTE: cagney/2003-05-22: This is assuming that the
1200*5796c8dcSSimon Schubert                stack pointer was packed as an unsigned integer.  That
1201*5796c8dcSSimon Schubert                may or may not be valid.  */
1202*5796c8dcSSimon Schubert 	    sp = extract_unsigned_integer (value, sp_size, byte_order);
1203*5796c8dcSSimon Schubert 	    printf_filtered (" Previous frame's sp is ");
1204*5796c8dcSSimon Schubert 	    fputs_filtered (paddress (gdbarch, sp), gdb_stdout);
1205*5796c8dcSSimon Schubert 	    printf_filtered ("\n");
1206*5796c8dcSSimon Schubert 	    need_nl = 0;
1207*5796c8dcSSimon Schubert 	  }
1208*5796c8dcSSimon Schubert 	else if (!optimized && lval == lval_memory)
1209*5796c8dcSSimon Schubert 	  {
1210*5796c8dcSSimon Schubert 	    printf_filtered (" Previous frame's sp at ");
1211*5796c8dcSSimon Schubert 	    fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1212*5796c8dcSSimon Schubert 	    printf_filtered ("\n");
1213*5796c8dcSSimon Schubert 	    need_nl = 0;
1214*5796c8dcSSimon Schubert 	  }
1215*5796c8dcSSimon Schubert 	else if (!optimized && lval == lval_register)
1216*5796c8dcSSimon Schubert 	  {
1217*5796c8dcSSimon Schubert 	    printf_filtered (" Previous frame's sp in %s\n",
1218*5796c8dcSSimon Schubert 			     gdbarch_register_name (gdbarch, realnum));
1219*5796c8dcSSimon Schubert 	    need_nl = 0;
1220*5796c8dcSSimon Schubert 	  }
1221*5796c8dcSSimon Schubert 	/* else keep quiet.  */
1222*5796c8dcSSimon Schubert       }
1223*5796c8dcSSimon Schubert 
1224*5796c8dcSSimon Schubert     count = 0;
1225*5796c8dcSSimon Schubert     numregs = gdbarch_num_regs (gdbarch)
1226*5796c8dcSSimon Schubert 	      + gdbarch_num_pseudo_regs (gdbarch);
1227*5796c8dcSSimon Schubert     for (i = 0; i < numregs; i++)
1228*5796c8dcSSimon Schubert       if (i != gdbarch_sp_regnum (gdbarch)
1229*5796c8dcSSimon Schubert 	  && gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1230*5796c8dcSSimon Schubert 	{
1231*5796c8dcSSimon Schubert 	  /* Find out the location of the saved register without
1232*5796c8dcSSimon Schubert              fetching the corresponding value.  */
1233*5796c8dcSSimon Schubert 	  frame_register_unwind (fi, i, &optimized, &lval, &addr, &realnum,
1234*5796c8dcSSimon Schubert 				 NULL);
1235*5796c8dcSSimon Schubert 	  /* For moment, only display registers that were saved on the
1236*5796c8dcSSimon Schubert 	     stack.  */
1237*5796c8dcSSimon Schubert 	  if (!optimized && lval == lval_memory)
1238*5796c8dcSSimon Schubert 	    {
1239*5796c8dcSSimon Schubert 	      if (count == 0)
1240*5796c8dcSSimon Schubert 		puts_filtered (" Saved registers:\n ");
1241*5796c8dcSSimon Schubert 	      else
1242*5796c8dcSSimon Schubert 		puts_filtered (",");
1243*5796c8dcSSimon Schubert 	      wrap_here (" ");
1244*5796c8dcSSimon Schubert 	      printf_filtered (" %s at ",
1245*5796c8dcSSimon Schubert 			       gdbarch_register_name (gdbarch, i));
1246*5796c8dcSSimon Schubert 	      fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1247*5796c8dcSSimon Schubert 	      count++;
1248*5796c8dcSSimon Schubert 	    }
1249*5796c8dcSSimon Schubert 	}
1250*5796c8dcSSimon Schubert     if (count || need_nl)
1251*5796c8dcSSimon Schubert       puts_filtered ("\n");
1252*5796c8dcSSimon Schubert   }
1253*5796c8dcSSimon Schubert 
1254*5796c8dcSSimon Schubert   do_cleanups (back_to);
1255*5796c8dcSSimon Schubert }
1256*5796c8dcSSimon Schubert 
1257*5796c8dcSSimon Schubert /* Print briefly all stack frames or just the innermost COUNT_EXP
1258*5796c8dcSSimon Schubert    frames.  */
1259*5796c8dcSSimon Schubert 
1260*5796c8dcSSimon Schubert static void
1261*5796c8dcSSimon Schubert backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
1262*5796c8dcSSimon Schubert {
1263*5796c8dcSSimon Schubert   struct frame_info *fi;
1264*5796c8dcSSimon Schubert   int count;
1265*5796c8dcSSimon Schubert   int i;
1266*5796c8dcSSimon Schubert   struct frame_info *trailing;
1267*5796c8dcSSimon Schubert   int trailing_level;
1268*5796c8dcSSimon Schubert 
1269*5796c8dcSSimon Schubert   if (!target_has_stack)
1270*5796c8dcSSimon Schubert     error (_("No stack."));
1271*5796c8dcSSimon Schubert 
1272*5796c8dcSSimon Schubert   /* The following code must do two things.  First, it must set the
1273*5796c8dcSSimon Schubert      variable TRAILING to the frame from which we should start
1274*5796c8dcSSimon Schubert      printing.  Second, it must set the variable count to the number
1275*5796c8dcSSimon Schubert      of frames which we should print, or -1 if all of them.  */
1276*5796c8dcSSimon Schubert   trailing = get_current_frame ();
1277*5796c8dcSSimon Schubert 
1278*5796c8dcSSimon Schubert   trailing_level = 0;
1279*5796c8dcSSimon Schubert   if (count_exp)
1280*5796c8dcSSimon Schubert     {
1281*5796c8dcSSimon Schubert       count = parse_and_eval_long (count_exp);
1282*5796c8dcSSimon Schubert       if (count < 0)
1283*5796c8dcSSimon Schubert 	{
1284*5796c8dcSSimon Schubert 	  struct frame_info *current;
1285*5796c8dcSSimon Schubert 
1286*5796c8dcSSimon Schubert 	  count = -count;
1287*5796c8dcSSimon Schubert 
1288*5796c8dcSSimon Schubert 	  current = trailing;
1289*5796c8dcSSimon Schubert 	  while (current && count--)
1290*5796c8dcSSimon Schubert 	    {
1291*5796c8dcSSimon Schubert 	      QUIT;
1292*5796c8dcSSimon Schubert 	      current = get_prev_frame (current);
1293*5796c8dcSSimon Schubert 	    }
1294*5796c8dcSSimon Schubert 
1295*5796c8dcSSimon Schubert 	  /* Will stop when CURRENT reaches the top of the stack.
1296*5796c8dcSSimon Schubert 	     TRAILING will be COUNT below it.  */
1297*5796c8dcSSimon Schubert 	  while (current)
1298*5796c8dcSSimon Schubert 	    {
1299*5796c8dcSSimon Schubert 	      QUIT;
1300*5796c8dcSSimon Schubert 	      trailing = get_prev_frame (trailing);
1301*5796c8dcSSimon Schubert 	      current = get_prev_frame (current);
1302*5796c8dcSSimon Schubert 	      trailing_level++;
1303*5796c8dcSSimon Schubert 	    }
1304*5796c8dcSSimon Schubert 
1305*5796c8dcSSimon Schubert 	  count = -1;
1306*5796c8dcSSimon Schubert 	}
1307*5796c8dcSSimon Schubert     }
1308*5796c8dcSSimon Schubert   else
1309*5796c8dcSSimon Schubert     count = -1;
1310*5796c8dcSSimon Schubert 
1311*5796c8dcSSimon Schubert   if (info_verbose)
1312*5796c8dcSSimon Schubert     {
1313*5796c8dcSSimon Schubert       struct partial_symtab *ps;
1314*5796c8dcSSimon Schubert 
1315*5796c8dcSSimon Schubert       /* Read in symbols for all of the frames.  Need to do this in a
1316*5796c8dcSSimon Schubert          separate pass so that "Reading in symbols for xxx" messages
1317*5796c8dcSSimon Schubert          don't screw up the appearance of the backtrace.  Also if
1318*5796c8dcSSimon Schubert          people have strong opinions against reading symbols for
1319*5796c8dcSSimon Schubert          backtrace this may have to be an option.  */
1320*5796c8dcSSimon Schubert       i = count;
1321*5796c8dcSSimon Schubert       for (fi = trailing; fi != NULL && i--; fi = get_prev_frame (fi))
1322*5796c8dcSSimon Schubert 	{
1323*5796c8dcSSimon Schubert 	  QUIT;
1324*5796c8dcSSimon Schubert 	  ps = find_pc_psymtab (get_frame_address_in_block (fi));
1325*5796c8dcSSimon Schubert 	  if (ps)
1326*5796c8dcSSimon Schubert 	    PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in.  */
1327*5796c8dcSSimon Schubert 	}
1328*5796c8dcSSimon Schubert     }
1329*5796c8dcSSimon Schubert 
1330*5796c8dcSSimon Schubert   for (i = 0, fi = trailing; fi && count--; i++, fi = get_prev_frame (fi))
1331*5796c8dcSSimon Schubert     {
1332*5796c8dcSSimon Schubert       QUIT;
1333*5796c8dcSSimon Schubert 
1334*5796c8dcSSimon Schubert       /* Don't use print_stack_frame; if an error() occurs it probably
1335*5796c8dcSSimon Schubert          means further attempts to backtrace would fail (on the other
1336*5796c8dcSSimon Schubert          hand, perhaps the code does or could be fixed to make sure
1337*5796c8dcSSimon Schubert          the frame->prev field gets set to NULL in that case).  */
1338*5796c8dcSSimon Schubert       print_frame_info (fi, 1, LOCATION, 1);
1339*5796c8dcSSimon Schubert       if (show_locals)
1340*5796c8dcSSimon Schubert 	print_frame_local_vars (fi, 1, gdb_stdout);
1341*5796c8dcSSimon Schubert 
1342*5796c8dcSSimon Schubert       /* Save the last frame to check for error conditions.  */
1343*5796c8dcSSimon Schubert       trailing = fi;
1344*5796c8dcSSimon Schubert     }
1345*5796c8dcSSimon Schubert 
1346*5796c8dcSSimon Schubert   /* If we've stopped before the end, mention that.  */
1347*5796c8dcSSimon Schubert   if (fi && from_tty)
1348*5796c8dcSSimon Schubert     printf_filtered (_("(More stack frames follow...)\n"));
1349*5796c8dcSSimon Schubert 
1350*5796c8dcSSimon Schubert   /* If we've run out of frames, and the reason appears to be an error
1351*5796c8dcSSimon Schubert      condition, print it.  */
1352*5796c8dcSSimon Schubert   if (fi == NULL && trailing != NULL)
1353*5796c8dcSSimon Schubert     {
1354*5796c8dcSSimon Schubert       enum unwind_stop_reason reason;
1355*5796c8dcSSimon Schubert 
1356*5796c8dcSSimon Schubert       reason = get_frame_unwind_stop_reason (trailing);
1357*5796c8dcSSimon Schubert       if (reason > UNWIND_FIRST_ERROR)
1358*5796c8dcSSimon Schubert 	printf_filtered (_("Backtrace stopped: %s\n"),
1359*5796c8dcSSimon Schubert 			 frame_stop_reason_string (reason));
1360*5796c8dcSSimon Schubert     }
1361*5796c8dcSSimon Schubert }
1362*5796c8dcSSimon Schubert 
1363*5796c8dcSSimon Schubert struct backtrace_command_args
1364*5796c8dcSSimon Schubert {
1365*5796c8dcSSimon Schubert   char *count_exp;
1366*5796c8dcSSimon Schubert   int show_locals;
1367*5796c8dcSSimon Schubert   int from_tty;
1368*5796c8dcSSimon Schubert };
1369*5796c8dcSSimon Schubert 
1370*5796c8dcSSimon Schubert /* Stub for catch_errors.  */
1371*5796c8dcSSimon Schubert 
1372*5796c8dcSSimon Schubert static int
1373*5796c8dcSSimon Schubert backtrace_command_stub (void *data)
1374*5796c8dcSSimon Schubert {
1375*5796c8dcSSimon Schubert   struct backtrace_command_args *args = data;
1376*5796c8dcSSimon Schubert   backtrace_command_1 (args->count_exp, args->show_locals, args->from_tty);
1377*5796c8dcSSimon Schubert   return 0;
1378*5796c8dcSSimon Schubert }
1379*5796c8dcSSimon Schubert 
1380*5796c8dcSSimon Schubert static void
1381*5796c8dcSSimon Schubert backtrace_command (char *arg, int from_tty)
1382*5796c8dcSSimon Schubert {
1383*5796c8dcSSimon Schubert   struct cleanup *old_chain = NULL;
1384*5796c8dcSSimon Schubert   int fulltrace_arg = -1, arglen = 0, argc = 0;
1385*5796c8dcSSimon Schubert   struct backtrace_command_args btargs;
1386*5796c8dcSSimon Schubert 
1387*5796c8dcSSimon Schubert   if (arg)
1388*5796c8dcSSimon Schubert     {
1389*5796c8dcSSimon Schubert       char **argv;
1390*5796c8dcSSimon Schubert       int i;
1391*5796c8dcSSimon Schubert 
1392*5796c8dcSSimon Schubert       argv = gdb_buildargv (arg);
1393*5796c8dcSSimon Schubert       old_chain = make_cleanup_freeargv (argv);
1394*5796c8dcSSimon Schubert       argc = 0;
1395*5796c8dcSSimon Schubert       for (i = 0; argv[i]; i++)
1396*5796c8dcSSimon Schubert 	{
1397*5796c8dcSSimon Schubert 	  unsigned int j;
1398*5796c8dcSSimon Schubert 
1399*5796c8dcSSimon Schubert 	  for (j = 0; j < strlen (argv[i]); j++)
1400*5796c8dcSSimon Schubert 	    argv[i][j] = tolower (argv[i][j]);
1401*5796c8dcSSimon Schubert 
1402*5796c8dcSSimon Schubert 	  if (fulltrace_arg < 0 && subset_compare (argv[i], "full"))
1403*5796c8dcSSimon Schubert 	    fulltrace_arg = argc;
1404*5796c8dcSSimon Schubert 	  else
1405*5796c8dcSSimon Schubert 	    {
1406*5796c8dcSSimon Schubert 	      arglen += strlen (argv[i]);
1407*5796c8dcSSimon Schubert 	      argc++;
1408*5796c8dcSSimon Schubert 	    }
1409*5796c8dcSSimon Schubert 	}
1410*5796c8dcSSimon Schubert       arglen += argc;
1411*5796c8dcSSimon Schubert       if (fulltrace_arg >= 0)
1412*5796c8dcSSimon Schubert 	{
1413*5796c8dcSSimon Schubert 	  if (arglen > 0)
1414*5796c8dcSSimon Schubert 	    {
1415*5796c8dcSSimon Schubert 	      arg = xmalloc (arglen + 1);
1416*5796c8dcSSimon Schubert 	      memset (arg, 0, arglen + 1);
1417*5796c8dcSSimon Schubert 	      for (i = 0; i < (argc + 1); i++)
1418*5796c8dcSSimon Schubert 		{
1419*5796c8dcSSimon Schubert 		  if (i != fulltrace_arg)
1420*5796c8dcSSimon Schubert 		    {
1421*5796c8dcSSimon Schubert 		      strcat (arg, argv[i]);
1422*5796c8dcSSimon Schubert 		      strcat (arg, " ");
1423*5796c8dcSSimon Schubert 		    }
1424*5796c8dcSSimon Schubert 		}
1425*5796c8dcSSimon Schubert 	    }
1426*5796c8dcSSimon Schubert 	  else
1427*5796c8dcSSimon Schubert 	    arg = NULL;
1428*5796c8dcSSimon Schubert 	}
1429*5796c8dcSSimon Schubert     }
1430*5796c8dcSSimon Schubert 
1431*5796c8dcSSimon Schubert   btargs.count_exp = arg;
1432*5796c8dcSSimon Schubert   btargs.show_locals = (fulltrace_arg >= 0);
1433*5796c8dcSSimon Schubert   btargs.from_tty = from_tty;
1434*5796c8dcSSimon Schubert   catch_errors (backtrace_command_stub, &btargs, "", RETURN_MASK_ERROR);
1435*5796c8dcSSimon Schubert 
1436*5796c8dcSSimon Schubert   if (fulltrace_arg >= 0 && arglen > 0)
1437*5796c8dcSSimon Schubert     xfree (arg);
1438*5796c8dcSSimon Schubert 
1439*5796c8dcSSimon Schubert   if (old_chain)
1440*5796c8dcSSimon Schubert     do_cleanups (old_chain);
1441*5796c8dcSSimon Schubert }
1442*5796c8dcSSimon Schubert 
1443*5796c8dcSSimon Schubert static void
1444*5796c8dcSSimon Schubert backtrace_full_command (char *arg, int from_tty)
1445*5796c8dcSSimon Schubert {
1446*5796c8dcSSimon Schubert   struct backtrace_command_args btargs;
1447*5796c8dcSSimon Schubert   btargs.count_exp = arg;
1448*5796c8dcSSimon Schubert   btargs.show_locals = 1;
1449*5796c8dcSSimon Schubert   btargs.from_tty = from_tty;
1450*5796c8dcSSimon Schubert   catch_errors (backtrace_command_stub, &btargs, "", RETURN_MASK_ERROR);
1451*5796c8dcSSimon Schubert }
1452*5796c8dcSSimon Schubert 
1453*5796c8dcSSimon Schubert 
1454*5796c8dcSSimon Schubert /* Print the local variables of a block B active in FRAME on STREAM.
1455*5796c8dcSSimon Schubert    Return 1 if any variables were printed; 0 otherwise.  */
1456*5796c8dcSSimon Schubert 
1457*5796c8dcSSimon Schubert static int
1458*5796c8dcSSimon Schubert print_block_frame_locals (struct block *b, struct frame_info *frame,
1459*5796c8dcSSimon Schubert 			  int num_tabs, struct ui_file *stream)
1460*5796c8dcSSimon Schubert {
1461*5796c8dcSSimon Schubert   struct dict_iterator iter;
1462*5796c8dcSSimon Schubert   struct symbol *sym;
1463*5796c8dcSSimon Schubert   int values_printed = 0;
1464*5796c8dcSSimon Schubert   int j;
1465*5796c8dcSSimon Schubert 
1466*5796c8dcSSimon Schubert   ALL_BLOCK_SYMBOLS (b, iter, sym)
1467*5796c8dcSSimon Schubert     {
1468*5796c8dcSSimon Schubert       switch (SYMBOL_CLASS (sym))
1469*5796c8dcSSimon Schubert 	{
1470*5796c8dcSSimon Schubert 	case LOC_LOCAL:
1471*5796c8dcSSimon Schubert 	case LOC_REGISTER:
1472*5796c8dcSSimon Schubert 	case LOC_STATIC:
1473*5796c8dcSSimon Schubert 	case LOC_COMPUTED:
1474*5796c8dcSSimon Schubert 	  if (SYMBOL_IS_ARGUMENT (sym))
1475*5796c8dcSSimon Schubert 	    break;
1476*5796c8dcSSimon Schubert 	  values_printed = 1;
1477*5796c8dcSSimon Schubert 	  print_variable_and_value (NULL, sym, frame, stream, 4 * num_tabs);
1478*5796c8dcSSimon Schubert 	  break;
1479*5796c8dcSSimon Schubert 
1480*5796c8dcSSimon Schubert 	default:
1481*5796c8dcSSimon Schubert 	  /* Ignore symbols which are not locals.  */
1482*5796c8dcSSimon Schubert 	  break;
1483*5796c8dcSSimon Schubert 	}
1484*5796c8dcSSimon Schubert     }
1485*5796c8dcSSimon Schubert 
1486*5796c8dcSSimon Schubert   return values_printed;
1487*5796c8dcSSimon Schubert }
1488*5796c8dcSSimon Schubert 
1489*5796c8dcSSimon Schubert /* Same, but print labels.  */
1490*5796c8dcSSimon Schubert 
1491*5796c8dcSSimon Schubert static int
1492*5796c8dcSSimon Schubert print_block_frame_labels (struct gdbarch *gdbarch, struct block *b,
1493*5796c8dcSSimon Schubert 			  int *have_default, struct ui_file *stream)
1494*5796c8dcSSimon Schubert {
1495*5796c8dcSSimon Schubert   struct dict_iterator iter;
1496*5796c8dcSSimon Schubert   struct symbol *sym;
1497*5796c8dcSSimon Schubert   int values_printed = 0;
1498*5796c8dcSSimon Schubert 
1499*5796c8dcSSimon Schubert   ALL_BLOCK_SYMBOLS (b, iter, sym)
1500*5796c8dcSSimon Schubert     {
1501*5796c8dcSSimon Schubert       if (strcmp (SYMBOL_LINKAGE_NAME (sym), "default") == 0)
1502*5796c8dcSSimon Schubert 	{
1503*5796c8dcSSimon Schubert 	  if (*have_default)
1504*5796c8dcSSimon Schubert 	    continue;
1505*5796c8dcSSimon Schubert 	  *have_default = 1;
1506*5796c8dcSSimon Schubert 	}
1507*5796c8dcSSimon Schubert       if (SYMBOL_CLASS (sym) == LOC_LABEL)
1508*5796c8dcSSimon Schubert 	{
1509*5796c8dcSSimon Schubert 	  struct symtab_and_line sal;
1510*5796c8dcSSimon Schubert 	  struct value_print_options opts;
1511*5796c8dcSSimon Schubert 	  sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1512*5796c8dcSSimon Schubert 	  values_printed = 1;
1513*5796c8dcSSimon Schubert 	  fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1514*5796c8dcSSimon Schubert 	  get_user_print_options (&opts);
1515*5796c8dcSSimon Schubert 	  if (opts.addressprint)
1516*5796c8dcSSimon Schubert 	    {
1517*5796c8dcSSimon Schubert 	      fprintf_filtered (stream, " ");
1518*5796c8dcSSimon Schubert 	      fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (sym)),
1519*5796c8dcSSimon Schubert 			      stream);
1520*5796c8dcSSimon Schubert 	    }
1521*5796c8dcSSimon Schubert 	  fprintf_filtered (stream, " in file %s, line %d\n",
1522*5796c8dcSSimon Schubert 			    sal.symtab->filename, sal.line);
1523*5796c8dcSSimon Schubert 	}
1524*5796c8dcSSimon Schubert     }
1525*5796c8dcSSimon Schubert 
1526*5796c8dcSSimon Schubert   return values_printed;
1527*5796c8dcSSimon Schubert }
1528*5796c8dcSSimon Schubert 
1529*5796c8dcSSimon Schubert /* Print on STREAM all the local variables in frame FRAME, including
1530*5796c8dcSSimon Schubert    all the blocks active in that frame at its current PC.
1531*5796c8dcSSimon Schubert 
1532*5796c8dcSSimon Schubert    Returns 1 if the job was done, or 0 if nothing was printed because
1533*5796c8dcSSimon Schubert    we have no info on the function running in FRAME.  */
1534*5796c8dcSSimon Schubert 
1535*5796c8dcSSimon Schubert static void
1536*5796c8dcSSimon Schubert print_frame_local_vars (struct frame_info *frame, int num_tabs,
1537*5796c8dcSSimon Schubert 			struct ui_file *stream)
1538*5796c8dcSSimon Schubert {
1539*5796c8dcSSimon Schubert   struct block *block = get_frame_block (frame, 0);
1540*5796c8dcSSimon Schubert   int values_printed = 0;
1541*5796c8dcSSimon Schubert 
1542*5796c8dcSSimon Schubert   if (block == 0)
1543*5796c8dcSSimon Schubert     {
1544*5796c8dcSSimon Schubert       fprintf_filtered (stream, "No symbol table info available.\n");
1545*5796c8dcSSimon Schubert       return;
1546*5796c8dcSSimon Schubert     }
1547*5796c8dcSSimon Schubert 
1548*5796c8dcSSimon Schubert   while (block)
1549*5796c8dcSSimon Schubert     {
1550*5796c8dcSSimon Schubert       if (print_block_frame_locals (block, frame, num_tabs, stream))
1551*5796c8dcSSimon Schubert 	values_printed = 1;
1552*5796c8dcSSimon Schubert       /* After handling the function's top-level block, stop.  Don't
1553*5796c8dcSSimon Schubert          continue to its superblock, the block of per-file symbols.
1554*5796c8dcSSimon Schubert          Also do not continue to the containing function of an inlined
1555*5796c8dcSSimon Schubert          function.  */
1556*5796c8dcSSimon Schubert       if (BLOCK_FUNCTION (block))
1557*5796c8dcSSimon Schubert 	break;
1558*5796c8dcSSimon Schubert       block = BLOCK_SUPERBLOCK (block);
1559*5796c8dcSSimon Schubert     }
1560*5796c8dcSSimon Schubert 
1561*5796c8dcSSimon Schubert   if (!values_printed)
1562*5796c8dcSSimon Schubert     fprintf_filtered (stream, _("No locals.\n"));
1563*5796c8dcSSimon Schubert }
1564*5796c8dcSSimon Schubert 
1565*5796c8dcSSimon Schubert /* Same, but print labels.  */
1566*5796c8dcSSimon Schubert 
1567*5796c8dcSSimon Schubert static void
1568*5796c8dcSSimon Schubert print_frame_label_vars (struct frame_info *frame, int this_level_only,
1569*5796c8dcSSimon Schubert 			struct ui_file *stream)
1570*5796c8dcSSimon Schubert {
1571*5796c8dcSSimon Schubert #if 1
1572*5796c8dcSSimon Schubert   fprintf_filtered (stream, "print_frame_label_vars disabled.\n");
1573*5796c8dcSSimon Schubert #else
1574*5796c8dcSSimon Schubert   struct blockvector *bl;
1575*5796c8dcSSimon Schubert   struct block *block = get_frame_block (frame, 0);
1576*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
1577*5796c8dcSSimon Schubert   int values_printed = 0;
1578*5796c8dcSSimon Schubert   int index, have_default = 0;
1579*5796c8dcSSimon Schubert   char *blocks_printed;
1580*5796c8dcSSimon Schubert   CORE_ADDR pc = get_frame_pc (frame);
1581*5796c8dcSSimon Schubert 
1582*5796c8dcSSimon Schubert   if (block == 0)
1583*5796c8dcSSimon Schubert     {
1584*5796c8dcSSimon Schubert       fprintf_filtered (stream, "No symbol table info available.\n");
1585*5796c8dcSSimon Schubert       return;
1586*5796c8dcSSimon Schubert     }
1587*5796c8dcSSimon Schubert 
1588*5796c8dcSSimon Schubert   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1589*5796c8dcSSimon Schubert   blocks_printed = alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1590*5796c8dcSSimon Schubert   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1591*5796c8dcSSimon Schubert 
1592*5796c8dcSSimon Schubert   while (block != 0)
1593*5796c8dcSSimon Schubert     {
1594*5796c8dcSSimon Schubert       CORE_ADDR end = BLOCK_END (block) - 4;
1595*5796c8dcSSimon Schubert       int last_index;
1596*5796c8dcSSimon Schubert 
1597*5796c8dcSSimon Schubert       if (bl != blockvector_for_pc (end, &index))
1598*5796c8dcSSimon Schubert 	error (_("blockvector blotch"));
1599*5796c8dcSSimon Schubert       if (BLOCKVECTOR_BLOCK (bl, index) != block)
1600*5796c8dcSSimon Schubert 	error (_("blockvector botch"));
1601*5796c8dcSSimon Schubert       last_index = BLOCKVECTOR_NBLOCKS (bl);
1602*5796c8dcSSimon Schubert       index += 1;
1603*5796c8dcSSimon Schubert 
1604*5796c8dcSSimon Schubert       /* Don't print out blocks that have gone by.  */
1605*5796c8dcSSimon Schubert       while (index < last_index
1606*5796c8dcSSimon Schubert 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1607*5796c8dcSSimon Schubert 	index++;
1608*5796c8dcSSimon Schubert 
1609*5796c8dcSSimon Schubert       while (index < last_index
1610*5796c8dcSSimon Schubert 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1611*5796c8dcSSimon Schubert 	{
1612*5796c8dcSSimon Schubert 	  if (blocks_printed[index] == 0)
1613*5796c8dcSSimon Schubert 	    {
1614*5796c8dcSSimon Schubert 	      if (print_block_frame_labels (gdbarch,
1615*5796c8dcSSimon Schubert 					    BLOCKVECTOR_BLOCK (bl, index),
1616*5796c8dcSSimon Schubert 					    &have_default, stream))
1617*5796c8dcSSimon Schubert 		values_printed = 1;
1618*5796c8dcSSimon Schubert 	      blocks_printed[index] = 1;
1619*5796c8dcSSimon Schubert 	    }
1620*5796c8dcSSimon Schubert 	  index++;
1621*5796c8dcSSimon Schubert 	}
1622*5796c8dcSSimon Schubert       if (have_default)
1623*5796c8dcSSimon Schubert 	return;
1624*5796c8dcSSimon Schubert       if (values_printed && this_level_only)
1625*5796c8dcSSimon Schubert 	return;
1626*5796c8dcSSimon Schubert 
1627*5796c8dcSSimon Schubert       /* After handling the function's top-level block, stop.  Don't
1628*5796c8dcSSimon Schubert          continue to its superblock, the block of per-file symbols.
1629*5796c8dcSSimon Schubert          Also do not continue to the containing function of an inlined
1630*5796c8dcSSimon Schubert          function.  */
1631*5796c8dcSSimon Schubert       if (BLOCK_FUNCTION (block))
1632*5796c8dcSSimon Schubert 	break;
1633*5796c8dcSSimon Schubert       block = BLOCK_SUPERBLOCK (block);
1634*5796c8dcSSimon Schubert     }
1635*5796c8dcSSimon Schubert 
1636*5796c8dcSSimon Schubert   if (!values_printed && !this_level_only)
1637*5796c8dcSSimon Schubert     fprintf_filtered (stream, _("No catches.\n"));
1638*5796c8dcSSimon Schubert #endif
1639*5796c8dcSSimon Schubert }
1640*5796c8dcSSimon Schubert 
1641*5796c8dcSSimon Schubert void
1642*5796c8dcSSimon Schubert locals_info (char *args, int from_tty)
1643*5796c8dcSSimon Schubert {
1644*5796c8dcSSimon Schubert   print_frame_local_vars (get_selected_frame (_("No frame selected.")),
1645*5796c8dcSSimon Schubert 			  0, gdb_stdout);
1646*5796c8dcSSimon Schubert }
1647*5796c8dcSSimon Schubert 
1648*5796c8dcSSimon Schubert static void
1649*5796c8dcSSimon Schubert catch_info (char *ignore, int from_tty)
1650*5796c8dcSSimon Schubert {
1651*5796c8dcSSimon Schubert   struct symtab_and_line *sal;
1652*5796c8dcSSimon Schubert 
1653*5796c8dcSSimon Schubert   /* Assume g++ compiled code; old GDB 4.16 behaviour.  */
1654*5796c8dcSSimon Schubert   print_frame_label_vars (get_selected_frame (_("No frame selected.")),
1655*5796c8dcSSimon Schubert                           0, gdb_stdout);
1656*5796c8dcSSimon Schubert }
1657*5796c8dcSSimon Schubert 
1658*5796c8dcSSimon Schubert static void
1659*5796c8dcSSimon Schubert print_frame_arg_vars (struct frame_info *frame, struct ui_file *stream)
1660*5796c8dcSSimon Schubert {
1661*5796c8dcSSimon Schubert   struct symbol *func = get_frame_function (frame);
1662*5796c8dcSSimon Schubert   struct block *b;
1663*5796c8dcSSimon Schubert   struct dict_iterator iter;
1664*5796c8dcSSimon Schubert   struct symbol *sym, *sym2;
1665*5796c8dcSSimon Schubert   int values_printed = 0;
1666*5796c8dcSSimon Schubert 
1667*5796c8dcSSimon Schubert   if (func == 0)
1668*5796c8dcSSimon Schubert     {
1669*5796c8dcSSimon Schubert       fprintf_filtered (stream, _("No symbol table info available.\n"));
1670*5796c8dcSSimon Schubert       return;
1671*5796c8dcSSimon Schubert     }
1672*5796c8dcSSimon Schubert 
1673*5796c8dcSSimon Schubert   b = SYMBOL_BLOCK_VALUE (func);
1674*5796c8dcSSimon Schubert   ALL_BLOCK_SYMBOLS (b, iter, sym)
1675*5796c8dcSSimon Schubert     {
1676*5796c8dcSSimon Schubert       /* Don't worry about things which aren't arguments.  */
1677*5796c8dcSSimon Schubert       if (SYMBOL_IS_ARGUMENT (sym))
1678*5796c8dcSSimon Schubert 	{
1679*5796c8dcSSimon Schubert 	  values_printed = 1;
1680*5796c8dcSSimon Schubert 
1681*5796c8dcSSimon Schubert 	  /* We have to look up the symbol because arguments can have
1682*5796c8dcSSimon Schubert 	     two entries (one a parameter, one a local) and the one we
1683*5796c8dcSSimon Schubert 	     want is the local, which lookup_symbol will find for us.
1684*5796c8dcSSimon Schubert 	     This includes gcc1 (not gcc2) on the sparc when passing a
1685*5796c8dcSSimon Schubert 	     small structure and gcc2 when the argument type is float
1686*5796c8dcSSimon Schubert 	     and it is passed as a double and converted to float by
1687*5796c8dcSSimon Schubert 	     the prologue (in the latter case the type of the LOC_ARG
1688*5796c8dcSSimon Schubert 	     symbol is double and the type of the LOC_LOCAL symbol is
1689*5796c8dcSSimon Schubert 	     float).  There are also LOC_ARG/LOC_REGISTER pairs which
1690*5796c8dcSSimon Schubert 	     are not combined in symbol-reading.  */
1691*5796c8dcSSimon Schubert 
1692*5796c8dcSSimon Schubert 	  sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
1693*5796c8dcSSimon Schubert 				b, VAR_DOMAIN, NULL);
1694*5796c8dcSSimon Schubert 	  print_variable_and_value (SYMBOL_PRINT_NAME (sym), sym2,
1695*5796c8dcSSimon Schubert 				    frame, stream, 0);
1696*5796c8dcSSimon Schubert 	}
1697*5796c8dcSSimon Schubert     }
1698*5796c8dcSSimon Schubert 
1699*5796c8dcSSimon Schubert   if (!values_printed)
1700*5796c8dcSSimon Schubert     fprintf_filtered (stream, _("No arguments.\n"));
1701*5796c8dcSSimon Schubert }
1702*5796c8dcSSimon Schubert 
1703*5796c8dcSSimon Schubert void
1704*5796c8dcSSimon Schubert args_info (char *ignore, int from_tty)
1705*5796c8dcSSimon Schubert {
1706*5796c8dcSSimon Schubert   print_frame_arg_vars (get_selected_frame (_("No frame selected.")),
1707*5796c8dcSSimon Schubert 			gdb_stdout);
1708*5796c8dcSSimon Schubert }
1709*5796c8dcSSimon Schubert 
1710*5796c8dcSSimon Schubert 
1711*5796c8dcSSimon Schubert static void
1712*5796c8dcSSimon Schubert args_plus_locals_info (char *ignore, int from_tty)
1713*5796c8dcSSimon Schubert {
1714*5796c8dcSSimon Schubert   args_info (ignore, from_tty);
1715*5796c8dcSSimon Schubert   locals_info (ignore, from_tty);
1716*5796c8dcSSimon Schubert }
1717*5796c8dcSSimon Schubert 
1718*5796c8dcSSimon Schubert 
1719*5796c8dcSSimon Schubert /* Select frame FRAME.  Also print the stack frame and show the source
1720*5796c8dcSSimon Schubert    if this is the tui version.  */
1721*5796c8dcSSimon Schubert static void
1722*5796c8dcSSimon Schubert select_and_print_frame (struct frame_info *frame)
1723*5796c8dcSSimon Schubert {
1724*5796c8dcSSimon Schubert   select_frame (frame);
1725*5796c8dcSSimon Schubert   if (frame)
1726*5796c8dcSSimon Schubert     print_stack_frame (frame, 1, SRC_AND_LOC);
1727*5796c8dcSSimon Schubert }
1728*5796c8dcSSimon Schubert 
1729*5796c8dcSSimon Schubert /* Return the symbol-block in which the selected frame is executing.
1730*5796c8dcSSimon Schubert    Can return zero under various legitimate circumstances.
1731*5796c8dcSSimon Schubert 
1732*5796c8dcSSimon Schubert    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
1733*5796c8dcSSimon Schubert    code address within the block returned.  We use this to decide
1734*5796c8dcSSimon Schubert    which macros are in scope.  */
1735*5796c8dcSSimon Schubert 
1736*5796c8dcSSimon Schubert struct block *
1737*5796c8dcSSimon Schubert get_selected_block (CORE_ADDR *addr_in_block)
1738*5796c8dcSSimon Schubert {
1739*5796c8dcSSimon Schubert   if (!has_stack_frames ())
1740*5796c8dcSSimon Schubert     return 0;
1741*5796c8dcSSimon Schubert 
1742*5796c8dcSSimon Schubert   return get_frame_block (get_selected_frame (NULL), addr_in_block);
1743*5796c8dcSSimon Schubert }
1744*5796c8dcSSimon Schubert 
1745*5796c8dcSSimon Schubert /* Find a frame a certain number of levels away from FRAME.
1746*5796c8dcSSimon Schubert    LEVEL_OFFSET_PTR points to an int containing the number of levels.
1747*5796c8dcSSimon Schubert    Positive means go to earlier frames (up); negative, the reverse.
1748*5796c8dcSSimon Schubert    The int that contains the number of levels is counted toward
1749*5796c8dcSSimon Schubert    zero as the frames for those levels are found.
1750*5796c8dcSSimon Schubert    If the top or bottom frame is reached, that frame is returned,
1751*5796c8dcSSimon Schubert    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1752*5796c8dcSSimon Schubert    how much farther the original request asked to go.  */
1753*5796c8dcSSimon Schubert 
1754*5796c8dcSSimon Schubert struct frame_info *
1755*5796c8dcSSimon Schubert find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
1756*5796c8dcSSimon Schubert {
1757*5796c8dcSSimon Schubert   /* Going up is simple: just call get_prev_frame enough times or
1758*5796c8dcSSimon Schubert      until the initial frame is reached.  */
1759*5796c8dcSSimon Schubert   while (*level_offset_ptr > 0)
1760*5796c8dcSSimon Schubert     {
1761*5796c8dcSSimon Schubert       struct frame_info *prev = get_prev_frame (frame);
1762*5796c8dcSSimon Schubert       if (!prev)
1763*5796c8dcSSimon Schubert 	break;
1764*5796c8dcSSimon Schubert       (*level_offset_ptr)--;
1765*5796c8dcSSimon Schubert       frame = prev;
1766*5796c8dcSSimon Schubert     }
1767*5796c8dcSSimon Schubert 
1768*5796c8dcSSimon Schubert   /* Going down is just as simple.  */
1769*5796c8dcSSimon Schubert   while (*level_offset_ptr < 0)
1770*5796c8dcSSimon Schubert     {
1771*5796c8dcSSimon Schubert       struct frame_info *next = get_next_frame (frame);
1772*5796c8dcSSimon Schubert       if (!next)
1773*5796c8dcSSimon Schubert 	break;
1774*5796c8dcSSimon Schubert       (*level_offset_ptr)++;
1775*5796c8dcSSimon Schubert       frame = next;
1776*5796c8dcSSimon Schubert     }
1777*5796c8dcSSimon Schubert 
1778*5796c8dcSSimon Schubert   return frame;
1779*5796c8dcSSimon Schubert }
1780*5796c8dcSSimon Schubert 
1781*5796c8dcSSimon Schubert /* The "select_frame" command.  With no argument this is a NOP.
1782*5796c8dcSSimon Schubert    Select the frame at level LEVEL_EXP if it is a valid level.
1783*5796c8dcSSimon Schubert    Otherwise, treat LEVEL_EXP as an address expression and select it.
1784*5796c8dcSSimon Schubert 
1785*5796c8dcSSimon Schubert    See parse_frame_specification for more info on proper frame
1786*5796c8dcSSimon Schubert    expressions.  */
1787*5796c8dcSSimon Schubert 
1788*5796c8dcSSimon Schubert void
1789*5796c8dcSSimon Schubert select_frame_command (char *level_exp, int from_tty)
1790*5796c8dcSSimon Schubert {
1791*5796c8dcSSimon Schubert   select_frame (parse_frame_specification_1 (level_exp, "No stack.", NULL));
1792*5796c8dcSSimon Schubert }
1793*5796c8dcSSimon Schubert 
1794*5796c8dcSSimon Schubert /* The "frame" command.  With no argument, print the selected frame
1795*5796c8dcSSimon Schubert    briefly.  With an argument, behave like select_frame and then print
1796*5796c8dcSSimon Schubert    the selected frame.  */
1797*5796c8dcSSimon Schubert 
1798*5796c8dcSSimon Schubert static void
1799*5796c8dcSSimon Schubert frame_command (char *level_exp, int from_tty)
1800*5796c8dcSSimon Schubert {
1801*5796c8dcSSimon Schubert   select_frame_command (level_exp, from_tty);
1802*5796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1803*5796c8dcSSimon Schubert }
1804*5796c8dcSSimon Schubert 
1805*5796c8dcSSimon Schubert /* The XDB Compatibility command to print the current frame.  */
1806*5796c8dcSSimon Schubert 
1807*5796c8dcSSimon Schubert static void
1808*5796c8dcSSimon Schubert current_frame_command (char *level_exp, int from_tty)
1809*5796c8dcSSimon Schubert {
1810*5796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (_("No stack.")), 1, SRC_AND_LOC);
1811*5796c8dcSSimon Schubert }
1812*5796c8dcSSimon Schubert 
1813*5796c8dcSSimon Schubert /* Select the frame up one or COUNT_EXP stack levels from the
1814*5796c8dcSSimon Schubert    previously selected frame, and print it briefly.  */
1815*5796c8dcSSimon Schubert 
1816*5796c8dcSSimon Schubert static void
1817*5796c8dcSSimon Schubert up_silently_base (char *count_exp)
1818*5796c8dcSSimon Schubert {
1819*5796c8dcSSimon Schubert   struct frame_info *frame;
1820*5796c8dcSSimon Schubert   int count = 1;
1821*5796c8dcSSimon Schubert 
1822*5796c8dcSSimon Schubert   if (count_exp)
1823*5796c8dcSSimon Schubert     count = parse_and_eval_long (count_exp);
1824*5796c8dcSSimon Schubert 
1825*5796c8dcSSimon Schubert   frame = find_relative_frame (get_selected_frame ("No stack."), &count);
1826*5796c8dcSSimon Schubert   if (count != 0 && count_exp == NULL)
1827*5796c8dcSSimon Schubert     error (_("Initial frame selected; you cannot go up."));
1828*5796c8dcSSimon Schubert   select_frame (frame);
1829*5796c8dcSSimon Schubert }
1830*5796c8dcSSimon Schubert 
1831*5796c8dcSSimon Schubert static void
1832*5796c8dcSSimon Schubert up_silently_command (char *count_exp, int from_tty)
1833*5796c8dcSSimon Schubert {
1834*5796c8dcSSimon Schubert   up_silently_base (count_exp);
1835*5796c8dcSSimon Schubert }
1836*5796c8dcSSimon Schubert 
1837*5796c8dcSSimon Schubert static void
1838*5796c8dcSSimon Schubert up_command (char *count_exp, int from_tty)
1839*5796c8dcSSimon Schubert {
1840*5796c8dcSSimon Schubert   up_silently_base (count_exp);
1841*5796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1842*5796c8dcSSimon Schubert }
1843*5796c8dcSSimon Schubert 
1844*5796c8dcSSimon Schubert /* Select the frame down one or COUNT_EXP stack levels from the previously
1845*5796c8dcSSimon Schubert    selected frame, and print it briefly.  */
1846*5796c8dcSSimon Schubert 
1847*5796c8dcSSimon Schubert static void
1848*5796c8dcSSimon Schubert down_silently_base (char *count_exp)
1849*5796c8dcSSimon Schubert {
1850*5796c8dcSSimon Schubert   struct frame_info *frame;
1851*5796c8dcSSimon Schubert   int count = -1;
1852*5796c8dcSSimon Schubert   if (count_exp)
1853*5796c8dcSSimon Schubert     count = -parse_and_eval_long (count_exp);
1854*5796c8dcSSimon Schubert 
1855*5796c8dcSSimon Schubert   frame = find_relative_frame (get_selected_frame ("No stack."), &count);
1856*5796c8dcSSimon Schubert   if (count != 0 && count_exp == NULL)
1857*5796c8dcSSimon Schubert     {
1858*5796c8dcSSimon Schubert       /* We only do this if COUNT_EXP is not specified.  That way
1859*5796c8dcSSimon Schubert          "down" means to really go down (and let me know if that is
1860*5796c8dcSSimon Schubert          impossible), but "down 9999" can be used to mean go all the
1861*5796c8dcSSimon Schubert          way down without getting an error.  */
1862*5796c8dcSSimon Schubert 
1863*5796c8dcSSimon Schubert       error (_("Bottom (innermost) frame selected; you cannot go down."));
1864*5796c8dcSSimon Schubert     }
1865*5796c8dcSSimon Schubert 
1866*5796c8dcSSimon Schubert   select_frame (frame);
1867*5796c8dcSSimon Schubert }
1868*5796c8dcSSimon Schubert 
1869*5796c8dcSSimon Schubert static void
1870*5796c8dcSSimon Schubert down_silently_command (char *count_exp, int from_tty)
1871*5796c8dcSSimon Schubert {
1872*5796c8dcSSimon Schubert   down_silently_base (count_exp);
1873*5796c8dcSSimon Schubert }
1874*5796c8dcSSimon Schubert 
1875*5796c8dcSSimon Schubert static void
1876*5796c8dcSSimon Schubert down_command (char *count_exp, int from_tty)
1877*5796c8dcSSimon Schubert {
1878*5796c8dcSSimon Schubert   down_silently_base (count_exp);
1879*5796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1880*5796c8dcSSimon Schubert }
1881*5796c8dcSSimon Schubert 
1882*5796c8dcSSimon Schubert 
1883*5796c8dcSSimon Schubert void
1884*5796c8dcSSimon Schubert return_command (char *retval_exp, int from_tty)
1885*5796c8dcSSimon Schubert {
1886*5796c8dcSSimon Schubert   struct frame_info *thisframe;
1887*5796c8dcSSimon Schubert   struct gdbarch *gdbarch;
1888*5796c8dcSSimon Schubert   struct symbol *thisfun;
1889*5796c8dcSSimon Schubert   struct value *return_value = NULL;
1890*5796c8dcSSimon Schubert   const char *query_prefix = "";
1891*5796c8dcSSimon Schubert 
1892*5796c8dcSSimon Schubert   thisframe = get_selected_frame ("No selected frame.");
1893*5796c8dcSSimon Schubert   thisfun = get_frame_function (thisframe);
1894*5796c8dcSSimon Schubert   gdbarch = get_frame_arch (thisframe);
1895*5796c8dcSSimon Schubert 
1896*5796c8dcSSimon Schubert   if (get_frame_type (get_current_frame ()) == INLINE_FRAME)
1897*5796c8dcSSimon Schubert     error (_("Can not force return from an inlined function."));
1898*5796c8dcSSimon Schubert 
1899*5796c8dcSSimon Schubert   /* Compute the return value.  If the computation triggers an error,
1900*5796c8dcSSimon Schubert      let it bail.  If the return type can't be handled, set
1901*5796c8dcSSimon Schubert      RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
1902*5796c8dcSSimon Schubert      message.  */
1903*5796c8dcSSimon Schubert   if (retval_exp)
1904*5796c8dcSSimon Schubert     {
1905*5796c8dcSSimon Schubert       struct expression *retval_expr = parse_expression (retval_exp);
1906*5796c8dcSSimon Schubert       struct cleanup *old_chain = make_cleanup (xfree, retval_expr);
1907*5796c8dcSSimon Schubert       struct type *return_type = NULL;
1908*5796c8dcSSimon Schubert 
1909*5796c8dcSSimon Schubert       /* Compute the return value.  Should the computation fail, this
1910*5796c8dcSSimon Schubert          call throws an error.  */
1911*5796c8dcSSimon Schubert       return_value = evaluate_expression (retval_expr);
1912*5796c8dcSSimon Schubert 
1913*5796c8dcSSimon Schubert       /* Cast return value to the return type of the function.  Should
1914*5796c8dcSSimon Schubert          the cast fail, this call throws an error.  */
1915*5796c8dcSSimon Schubert       if (thisfun != NULL)
1916*5796c8dcSSimon Schubert 	return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1917*5796c8dcSSimon Schubert       if (return_type == NULL)
1918*5796c8dcSSimon Schubert       	{
1919*5796c8dcSSimon Schubert 	  if (retval_expr->elts[0].opcode != UNOP_CAST)
1920*5796c8dcSSimon Schubert 	    error (_("Return value type not available for selected "
1921*5796c8dcSSimon Schubert 		     "stack frame.\n"
1922*5796c8dcSSimon Schubert 		     "Please use an explicit cast of the value to return."));
1923*5796c8dcSSimon Schubert 	  return_type = value_type (return_value);
1924*5796c8dcSSimon Schubert 	}
1925*5796c8dcSSimon Schubert       do_cleanups (old_chain);
1926*5796c8dcSSimon Schubert       CHECK_TYPEDEF (return_type);
1927*5796c8dcSSimon Schubert       return_value = value_cast (return_type, return_value);
1928*5796c8dcSSimon Schubert 
1929*5796c8dcSSimon Schubert       /* Make sure the value is fully evaluated.  It may live in the
1930*5796c8dcSSimon Schubert          stack frame we're about to pop.  */
1931*5796c8dcSSimon Schubert       if (value_lazy (return_value))
1932*5796c8dcSSimon Schubert 	value_fetch_lazy (return_value);
1933*5796c8dcSSimon Schubert 
1934*5796c8dcSSimon Schubert       if (TYPE_CODE (return_type) == TYPE_CODE_VOID)
1935*5796c8dcSSimon Schubert 	/* If the return-type is "void", don't try to find the
1936*5796c8dcSSimon Schubert            return-value's location.  However, do still evaluate the
1937*5796c8dcSSimon Schubert            return expression so that, even when the expression result
1938*5796c8dcSSimon Schubert            is discarded, side effects such as "return i++" still
1939*5796c8dcSSimon Schubert            occur.  */
1940*5796c8dcSSimon Schubert 	return_value = NULL;
1941*5796c8dcSSimon Schubert       else if (thisfun != NULL
1942*5796c8dcSSimon Schubert 	       && using_struct_return (gdbarch,
1943*5796c8dcSSimon Schubert 				       SYMBOL_TYPE (thisfun), return_type))
1944*5796c8dcSSimon Schubert 	{
1945*5796c8dcSSimon Schubert 	  query_prefix = "\
1946*5796c8dcSSimon Schubert The location at which to store the function's return value is unknown.\n\
1947*5796c8dcSSimon Schubert If you continue, the return value that you specified will be ignored.\n";
1948*5796c8dcSSimon Schubert 	  return_value = NULL;
1949*5796c8dcSSimon Schubert 	}
1950*5796c8dcSSimon Schubert     }
1951*5796c8dcSSimon Schubert 
1952*5796c8dcSSimon Schubert   /* Does an interactive user really want to do this?  Include
1953*5796c8dcSSimon Schubert      information, such as how well GDB can handle the return value, in
1954*5796c8dcSSimon Schubert      the query message.  */
1955*5796c8dcSSimon Schubert   if (from_tty)
1956*5796c8dcSSimon Schubert     {
1957*5796c8dcSSimon Schubert       int confirmed;
1958*5796c8dcSSimon Schubert       if (thisfun == NULL)
1959*5796c8dcSSimon Schubert 	confirmed = query (_("%sMake selected stack frame return now? "),
1960*5796c8dcSSimon Schubert 			   query_prefix);
1961*5796c8dcSSimon Schubert       else
1962*5796c8dcSSimon Schubert 	confirmed = query (_("%sMake %s return now? "), query_prefix,
1963*5796c8dcSSimon Schubert 			   SYMBOL_PRINT_NAME (thisfun));
1964*5796c8dcSSimon Schubert       if (!confirmed)
1965*5796c8dcSSimon Schubert 	error (_("Not confirmed"));
1966*5796c8dcSSimon Schubert     }
1967*5796c8dcSSimon Schubert 
1968*5796c8dcSSimon Schubert   /* Discard the selected frame and all frames inner-to it.  */
1969*5796c8dcSSimon Schubert   frame_pop (get_selected_frame (NULL));
1970*5796c8dcSSimon Schubert 
1971*5796c8dcSSimon Schubert   /* Store RETURN_VALUE in the just-returned register set.  */
1972*5796c8dcSSimon Schubert   if (return_value != NULL)
1973*5796c8dcSSimon Schubert     {
1974*5796c8dcSSimon Schubert       struct type *return_type = value_type (return_value);
1975*5796c8dcSSimon Schubert       struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
1976*5796c8dcSSimon Schubert       struct type *func_type = thisfun == NULL ? NULL : SYMBOL_TYPE (thisfun);
1977*5796c8dcSSimon Schubert 
1978*5796c8dcSSimon Schubert       gdb_assert (gdbarch_return_value (gdbarch, func_type, return_type, NULL,
1979*5796c8dcSSimon Schubert 					NULL, NULL)
1980*5796c8dcSSimon Schubert 		  == RETURN_VALUE_REGISTER_CONVENTION);
1981*5796c8dcSSimon Schubert       gdbarch_return_value (gdbarch, func_type, return_type,
1982*5796c8dcSSimon Schubert 			    get_current_regcache (), NULL /*read*/,
1983*5796c8dcSSimon Schubert 			    value_contents (return_value) /*write*/);
1984*5796c8dcSSimon Schubert     }
1985*5796c8dcSSimon Schubert 
1986*5796c8dcSSimon Schubert   /* If we are at the end of a call dummy now, pop the dummy frame
1987*5796c8dcSSimon Schubert      too.  */
1988*5796c8dcSSimon Schubert   if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
1989*5796c8dcSSimon Schubert     frame_pop (get_current_frame ());
1990*5796c8dcSSimon Schubert 
1991*5796c8dcSSimon Schubert   /* If interactive, print the frame that is now current.  */
1992*5796c8dcSSimon Schubert   if (from_tty)
1993*5796c8dcSSimon Schubert     frame_command ("0", 1);
1994*5796c8dcSSimon Schubert   else
1995*5796c8dcSSimon Schubert     select_frame_command ("0", 0);
1996*5796c8dcSSimon Schubert }
1997*5796c8dcSSimon Schubert 
1998*5796c8dcSSimon Schubert /* Sets the scope to input function name, provided that the function
1999*5796c8dcSSimon Schubert    is within the current stack frame */
2000*5796c8dcSSimon Schubert 
2001*5796c8dcSSimon Schubert struct function_bounds
2002*5796c8dcSSimon Schubert {
2003*5796c8dcSSimon Schubert   CORE_ADDR low, high;
2004*5796c8dcSSimon Schubert };
2005*5796c8dcSSimon Schubert 
2006*5796c8dcSSimon Schubert static void
2007*5796c8dcSSimon Schubert func_command (char *arg, int from_tty)
2008*5796c8dcSSimon Schubert {
2009*5796c8dcSSimon Schubert   struct frame_info *frame;
2010*5796c8dcSSimon Schubert   int found = 0;
2011*5796c8dcSSimon Schubert   struct symtabs_and_lines sals;
2012*5796c8dcSSimon Schubert   int i;
2013*5796c8dcSSimon Schubert   int level = 1;
2014*5796c8dcSSimon Schubert   struct function_bounds *func_bounds = NULL;
2015*5796c8dcSSimon Schubert 
2016*5796c8dcSSimon Schubert   if (arg != NULL)
2017*5796c8dcSSimon Schubert     return;
2018*5796c8dcSSimon Schubert 
2019*5796c8dcSSimon Schubert   frame = parse_frame_specification ("0");
2020*5796c8dcSSimon Schubert   sals = decode_line_spec (arg, 1);
2021*5796c8dcSSimon Schubert   func_bounds = (struct function_bounds *) xmalloc (
2022*5796c8dcSSimon Schubert 			      sizeof (struct function_bounds) * sals.nelts);
2023*5796c8dcSSimon Schubert   for (i = 0; (i < sals.nelts && !found); i++)
2024*5796c8dcSSimon Schubert     {
2025*5796c8dcSSimon Schubert       if (sals.sals[i].pc == 0
2026*5796c8dcSSimon Schubert 	  || find_pc_partial_function (sals.sals[i].pc, NULL,
2027*5796c8dcSSimon Schubert 				       &func_bounds[i].low,
2028*5796c8dcSSimon Schubert 				       &func_bounds[i].high) == 0)
2029*5796c8dcSSimon Schubert 	{
2030*5796c8dcSSimon Schubert 	  func_bounds[i].low = func_bounds[i].high = 0;
2031*5796c8dcSSimon Schubert 	}
2032*5796c8dcSSimon Schubert     }
2033*5796c8dcSSimon Schubert 
2034*5796c8dcSSimon Schubert   do
2035*5796c8dcSSimon Schubert     {
2036*5796c8dcSSimon Schubert       for (i = 0; (i < sals.nelts && !found); i++)
2037*5796c8dcSSimon Schubert 	found = (get_frame_pc (frame) >= func_bounds[i].low
2038*5796c8dcSSimon Schubert 		 && get_frame_pc (frame) < func_bounds[i].high);
2039*5796c8dcSSimon Schubert       if (!found)
2040*5796c8dcSSimon Schubert 	{
2041*5796c8dcSSimon Schubert 	  level = 1;
2042*5796c8dcSSimon Schubert 	  frame = find_relative_frame (frame, &level);
2043*5796c8dcSSimon Schubert 	}
2044*5796c8dcSSimon Schubert     }
2045*5796c8dcSSimon Schubert   while (!found && level == 0);
2046*5796c8dcSSimon Schubert 
2047*5796c8dcSSimon Schubert   if (func_bounds)
2048*5796c8dcSSimon Schubert     xfree (func_bounds);
2049*5796c8dcSSimon Schubert 
2050*5796c8dcSSimon Schubert   if (!found)
2051*5796c8dcSSimon Schubert     printf_filtered (_("'%s' not within current stack frame.\n"), arg);
2052*5796c8dcSSimon Schubert   else if (frame != get_selected_frame (NULL))
2053*5796c8dcSSimon Schubert     select_and_print_frame (frame);
2054*5796c8dcSSimon Schubert }
2055*5796c8dcSSimon Schubert 
2056*5796c8dcSSimon Schubert /* Gets the language of the current frame.  */
2057*5796c8dcSSimon Schubert 
2058*5796c8dcSSimon Schubert enum language
2059*5796c8dcSSimon Schubert get_frame_language (void)
2060*5796c8dcSSimon Schubert {
2061*5796c8dcSSimon Schubert   struct frame_info *frame = deprecated_safe_get_selected_frame ();
2062*5796c8dcSSimon Schubert 
2063*5796c8dcSSimon Schubert   if (frame)
2064*5796c8dcSSimon Schubert     {
2065*5796c8dcSSimon Schubert       /* We determine the current frame language by looking up its
2066*5796c8dcSSimon Schubert          associated symtab.  To retrieve this symtab, we use the frame
2067*5796c8dcSSimon Schubert          PC.  However we cannot use the frame PC as is, because it
2068*5796c8dcSSimon Schubert          usually points to the instruction following the "call", which
2069*5796c8dcSSimon Schubert          is sometimes the first instruction of another function.  So
2070*5796c8dcSSimon Schubert          we rely on get_frame_address_in_block(), it provides us with
2071*5796c8dcSSimon Schubert          a PC that is guaranteed to be inside the frame's code
2072*5796c8dcSSimon Schubert          block.  */
2073*5796c8dcSSimon Schubert       CORE_ADDR pc = get_frame_address_in_block (frame);
2074*5796c8dcSSimon Schubert       struct symtab *s = find_pc_symtab (pc);
2075*5796c8dcSSimon Schubert 
2076*5796c8dcSSimon Schubert       if (s)
2077*5796c8dcSSimon Schubert 	return s->language;
2078*5796c8dcSSimon Schubert     }
2079*5796c8dcSSimon Schubert 
2080*5796c8dcSSimon Schubert   return language_unknown;
2081*5796c8dcSSimon Schubert }
2082*5796c8dcSSimon Schubert 
2083*5796c8dcSSimon Schubert 
2084*5796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
2085*5796c8dcSSimon Schubert void _initialize_stack (void);
2086*5796c8dcSSimon Schubert 
2087*5796c8dcSSimon Schubert void
2088*5796c8dcSSimon Schubert _initialize_stack (void)
2089*5796c8dcSSimon Schubert {
2090*5796c8dcSSimon Schubert #if 0
2091*5796c8dcSSimon Schubert   backtrace_limit = 30;
2092*5796c8dcSSimon Schubert #endif
2093*5796c8dcSSimon Schubert 
2094*5796c8dcSSimon Schubert   add_com ("return", class_stack, return_command, _("\
2095*5796c8dcSSimon Schubert Make selected stack frame return to its caller.\n\
2096*5796c8dcSSimon Schubert Control remains in the debugger, but when you continue\n\
2097*5796c8dcSSimon Schubert execution will resume in the frame above the one now selected.\n\
2098*5796c8dcSSimon Schubert If an argument is given, it is an expression for the value to return."));
2099*5796c8dcSSimon Schubert 
2100*5796c8dcSSimon Schubert   add_com ("up", class_stack, up_command, _("\
2101*5796c8dcSSimon Schubert Select and print stack frame that called this one.\n\
2102*5796c8dcSSimon Schubert An argument says how many frames up to go."));
2103*5796c8dcSSimon Schubert   add_com ("up-silently", class_support, up_silently_command, _("\
2104*5796c8dcSSimon Schubert Same as the `up' command, but does not print anything.\n\
2105*5796c8dcSSimon Schubert This is useful in command scripts."));
2106*5796c8dcSSimon Schubert 
2107*5796c8dcSSimon Schubert   add_com ("down", class_stack, down_command, _("\
2108*5796c8dcSSimon Schubert Select and print stack frame called by this one.\n\
2109*5796c8dcSSimon Schubert An argument says how many frames down to go."));
2110*5796c8dcSSimon Schubert   add_com_alias ("do", "down", class_stack, 1);
2111*5796c8dcSSimon Schubert   add_com_alias ("dow", "down", class_stack, 1);
2112*5796c8dcSSimon Schubert   add_com ("down-silently", class_support, down_silently_command, _("\
2113*5796c8dcSSimon Schubert Same as the `down' command, but does not print anything.\n\
2114*5796c8dcSSimon Schubert This is useful in command scripts."));
2115*5796c8dcSSimon Schubert 
2116*5796c8dcSSimon Schubert   add_com ("frame", class_stack, frame_command, _("\
2117*5796c8dcSSimon Schubert Select and print a stack frame.\n\
2118*5796c8dcSSimon Schubert With no argument, print the selected stack frame.  (See also \"info frame\").\n\
2119*5796c8dcSSimon Schubert An argument specifies the frame to select.\n\
2120*5796c8dcSSimon Schubert It can be a stack frame number or the address of the frame.\n\
2121*5796c8dcSSimon Schubert With argument, nothing is printed if input is coming from\n\
2122*5796c8dcSSimon Schubert a command file or a user-defined command."));
2123*5796c8dcSSimon Schubert 
2124*5796c8dcSSimon Schubert   add_com_alias ("f", "frame", class_stack, 1);
2125*5796c8dcSSimon Schubert 
2126*5796c8dcSSimon Schubert   if (xdb_commands)
2127*5796c8dcSSimon Schubert     {
2128*5796c8dcSSimon Schubert       add_com ("L", class_stack, current_frame_command,
2129*5796c8dcSSimon Schubert 	       _("Print the current stack frame.\n"));
2130*5796c8dcSSimon Schubert       add_com_alias ("V", "frame", class_stack, 1);
2131*5796c8dcSSimon Schubert     }
2132*5796c8dcSSimon Schubert   add_com ("select-frame", class_stack, select_frame_command, _("\
2133*5796c8dcSSimon Schubert Select a stack frame without printing anything.\n\
2134*5796c8dcSSimon Schubert An argument specifies the frame to select.\n\
2135*5796c8dcSSimon Schubert It can be a stack frame number or the address of the frame.\n"));
2136*5796c8dcSSimon Schubert 
2137*5796c8dcSSimon Schubert   add_com ("backtrace", class_stack, backtrace_command, _("\
2138*5796c8dcSSimon Schubert Print backtrace of all stack frames, or innermost COUNT frames.\n\
2139*5796c8dcSSimon Schubert With a negative argument, print outermost -COUNT frames.\n\
2140*5796c8dcSSimon Schubert Use of the 'full' qualifier also prints the values of the local variables.\n"));
2141*5796c8dcSSimon Schubert   add_com_alias ("bt", "backtrace", class_stack, 0);
2142*5796c8dcSSimon Schubert   if (xdb_commands)
2143*5796c8dcSSimon Schubert     {
2144*5796c8dcSSimon Schubert       add_com_alias ("t", "backtrace", class_stack, 0);
2145*5796c8dcSSimon Schubert       add_com ("T", class_stack, backtrace_full_command, _("\
2146*5796c8dcSSimon Schubert Print backtrace of all stack frames, or innermost COUNT frames \n\
2147*5796c8dcSSimon Schubert and the values of the local variables.\n\
2148*5796c8dcSSimon Schubert With a negative argument, print outermost -COUNT frames.\n\
2149*5796c8dcSSimon Schubert Usage: T <count>\n"));
2150*5796c8dcSSimon Schubert     }
2151*5796c8dcSSimon Schubert 
2152*5796c8dcSSimon Schubert   add_com_alias ("where", "backtrace", class_alias, 0);
2153*5796c8dcSSimon Schubert   add_info ("stack", backtrace_command,
2154*5796c8dcSSimon Schubert 	    _("Backtrace of the stack, or innermost COUNT frames."));
2155*5796c8dcSSimon Schubert   add_info_alias ("s", "stack", 1);
2156*5796c8dcSSimon Schubert   add_info ("frame", frame_info,
2157*5796c8dcSSimon Schubert 	    _("All about selected stack frame, or frame at ADDR."));
2158*5796c8dcSSimon Schubert   add_info_alias ("f", "frame", 1);
2159*5796c8dcSSimon Schubert   add_info ("locals", locals_info,
2160*5796c8dcSSimon Schubert 	    _("Local variables of current stack frame."));
2161*5796c8dcSSimon Schubert   add_info ("args", args_info,
2162*5796c8dcSSimon Schubert 	    _("Argument variables of current stack frame."));
2163*5796c8dcSSimon Schubert   if (xdb_commands)
2164*5796c8dcSSimon Schubert     add_com ("l", class_info, args_plus_locals_info,
2165*5796c8dcSSimon Schubert 	     _("Argument and local variables of current stack frame."));
2166*5796c8dcSSimon Schubert 
2167*5796c8dcSSimon Schubert   if (dbx_commands)
2168*5796c8dcSSimon Schubert     add_com ("func", class_stack, func_command, _("\
2169*5796c8dcSSimon Schubert Select the stack frame that contains <func>.\n\
2170*5796c8dcSSimon Schubert Usage: func <name>\n"));
2171*5796c8dcSSimon Schubert 
2172*5796c8dcSSimon Schubert   add_info ("catch", catch_info,
2173*5796c8dcSSimon Schubert 	    _("Exceptions that can be caught in the current stack frame."));
2174*5796c8dcSSimon Schubert 
2175*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("frame-arguments", class_stack,
2176*5796c8dcSSimon Schubert 			print_frame_arguments_choices, &print_frame_arguments,
2177*5796c8dcSSimon Schubert 			_("Set printing of non-scalar frame arguments"),
2178*5796c8dcSSimon Schubert 			_("Show printing of non-scalar frame arguments"),
2179*5796c8dcSSimon Schubert 			NULL, NULL, NULL, &setprintlist, &showprintlist);
2180*5796c8dcSSimon Schubert 
2181*5796c8dcSSimon Schubert   add_setshow_auto_boolean_cmd ("disassemble-next-line", class_stack,
2182*5796c8dcSSimon Schubert 			        &disassemble_next_line, _("\
2183*5796c8dcSSimon Schubert Set whether to disassemble next source line or insn when execution stops."), _("\
2184*5796c8dcSSimon Schubert Show whether to disassemble next source line or insn when execution stops."), _("\
2185*5796c8dcSSimon Schubert If ON, GDB will display disassembly of the next source line, in addition\n\
2186*5796c8dcSSimon Schubert to displaying the source line itself.  If the next source line cannot\n\
2187*5796c8dcSSimon Schubert be displayed (e.g., source is unavailable or there's no line info), GDB\n\
2188*5796c8dcSSimon Schubert will display disassembly of next instruction instead of showing the\n\
2189*5796c8dcSSimon Schubert source line.\n\
2190*5796c8dcSSimon Schubert If AUTO, display disassembly of next instruction only if the source line\n\
2191*5796c8dcSSimon Schubert cannot be displayed.\n\
2192*5796c8dcSSimon Schubert If OFF (which is the default), never display the disassembly of the next\n\
2193*5796c8dcSSimon Schubert source line."),
2194*5796c8dcSSimon Schubert 			        NULL,
2195*5796c8dcSSimon Schubert 			        show_disassemble_next_line,
2196*5796c8dcSSimon Schubert 			        &setlist, &showlist);
2197*5796c8dcSSimon Schubert   disassemble_next_line = AUTO_BOOLEAN_FALSE;
2198*5796c8dcSSimon Schubert 
2199*5796c8dcSSimon Schubert #if 0
2200*5796c8dcSSimon Schubert   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, _(\
2201*5796c8dcSSimon Schubert "Specify maximum number of frames for \"backtrace\" to print by default."),
2202*5796c8dcSSimon Schubert 	   &setlist);
2203*5796c8dcSSimon Schubert   add_info ("backtrace-limit", backtrace_limit_info, _("\
2204*5796c8dcSSimon Schubert The maximum number of frames for \"backtrace\" to print by default."));
2205*5796c8dcSSimon Schubert #endif
2206*5796c8dcSSimon Schubert }
2207