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