stack.c revision 1.2
1/* Print and select stack frames for GDB, the GNU debugger.
2   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996
3   Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "value.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "language.h"
29#include "frame.h"
30#include "gdbcmd.h"
31#include "gdbcore.h"
32#include "target.h"
33#include "breakpoint.h"
34#include "demangle.h"
35#include "inferior.h"
36#include "annotate.h"
37#include "symfile.h"
38#include "objfiles.h"
39
40static void return_command PARAMS ((char *, int));
41
42static void down_command PARAMS ((char *, int));
43
44static void down_silently_command PARAMS ((char *, int));
45
46static void up_command PARAMS ((char *, int));
47
48static void up_silently_command PARAMS ((char *, int));
49
50static void frame_command PARAMS ((char *, int));
51
52static void select_frame_command PARAMS ((char *, int));
53
54static void args_info PARAMS ((char *, int));
55
56static void print_frame_arg_vars PARAMS ((struct frame_info *, GDB_FILE *));
57
58static void catch_info PARAMS ((char *, int));
59
60static void locals_info PARAMS ((char *, int));
61
62static void print_frame_label_vars PARAMS ((struct frame_info *, int,
63					    GDB_FILE *));
64
65static void print_frame_local_vars PARAMS ((struct frame_info *, GDB_FILE *));
66
67static int print_block_frame_labels PARAMS ((struct block *, int *,
68					     GDB_FILE *));
69
70static int print_block_frame_locals PARAMS ((struct block *,
71					     struct frame_info *,
72					     GDB_FILE *));
73
74static void backtrace_command PARAMS ((char *, int));
75
76static struct frame_info *parse_frame_specification PARAMS ((char *));
77
78static void frame_info PARAMS ((char *, int));
79
80extern int addressprint;	/* Print addresses, or stay symbolic only? */
81extern int info_verbose;	/* Verbosity of symbol reading msgs */
82extern int lines_to_list;	/* # of lines "list" command shows by default */
83
84/* The "selected" stack frame is used by default for local and arg access.
85   May be zero, for no selected frame.  */
86
87struct frame_info *selected_frame;
88
89/* Level of the selected frame:
90   0 for innermost, 1 for its caller, ...
91   or -1 for frame specified by address with no defined level.  */
92
93int selected_frame_level;
94
95/* Zero means do things normally; we are interacting directly with the
96   user.  One means print the full filename and linenumber when a
97   frame is printed, and do so in a format emacs18/emacs19.22 can
98   parse.  Two means print similar annotations, but in many more
99   cases and in a slightly different syntax.  */
100
101int annotation_level = 0;
102
103
104struct print_stack_frame_args {
105  struct frame_info *fi;
106  int level;
107  int source;
108  int args;
109};
110
111static int print_stack_frame_stub PARAMS ((char *));
112
113/* Pass the args the way catch_errors wants them.  */
114static int
115print_stack_frame_stub (args)
116     char *args;
117{
118  struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
119
120  print_frame_info (p->fi, p->level, p->source, p->args);
121  return 0;
122}
123
124/* Print a stack frame briefly.  FRAME_INFI should be the frame info
125   and LEVEL should be its level in the stack (or -1 for level not defined).
126   This prints the level, the function executing, the arguments,
127   and the file name and line number.
128   If the pc is not at the beginning of the source line,
129   the actual pc is printed at the beginning.
130
131   If SOURCE is 1, print the source line as well.
132   If SOURCE is -1, print ONLY the source line.  */
133
134void
135print_stack_frame (fi, level, source)
136     struct frame_info *fi;
137     int level;
138     int source;
139{
140  struct print_stack_frame_args args;
141
142  args.fi = fi;
143  args.level = level;
144  args.source = source;
145  args.args = 1;
146
147  catch_errors (print_stack_frame_stub, (char *)&args, "", RETURN_MASK_ALL);
148}
149
150struct print_args_args {
151  struct symbol *func;
152  struct frame_info *fi;
153};
154
155static int print_args_stub PARAMS ((char *));
156
157/* Pass the args the way catch_errors wants them.  */
158
159static int
160print_args_stub (args)
161     char *args;
162{
163  int numargs;
164  struct print_args_args *p = (struct print_args_args *)args;
165
166  FRAME_NUM_ARGS (numargs, (p->fi));
167  print_frame_args (p->func, p->fi, numargs, gdb_stdout);
168  return 0;
169}
170
171/* LEVEL is the level of the frame, or -1 if it is the innermost frame
172   but we don't want to print the level.  */
173
174void
175print_frame_info (fi, level, source, args)
176     struct frame_info *fi;
177     register int level;
178     int source;
179     int args;
180{
181  struct symtab_and_line sal;
182  struct symbol *func;
183  register char *funname = 0;
184  enum language funlang = language_unknown;
185
186#if 0
187  char buf[MAX_REGISTER_RAW_SIZE];
188  CORE_ADDR sp;
189
190  /* On the 68k, this spends too much time in m68k_find_saved_regs.  */
191
192  /* Get the value of SP_REGNUM relative to the frame.  */
193  get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
194		      FRAME_INFO_ID (fi), SP_REGNUM, (enum lval_type *)NULL);
195  sp = extract_address (buf, REGISTER_RAW_SIZE (SP_REGNUM));
196
197  /* This is not a perfect test, because if a function alloca's some
198     memory, puts some code there, and then jumps into it, then the test
199     will succeed even though there is no call dummy.  Probably best is
200     to check for a bp_call_dummy breakpoint.  */
201  if (PC_IN_CALL_DUMMY (fi->pc, sp, fi->frame))
202#else
203  if (frame_in_dummy (fi))
204#endif
205    {
206      annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
207
208      /* Do this regardless of SOURCE because we don't have any source
209	 to list for this frame.  */
210      if (level >= 0)
211	printf_filtered ("#%-2d ", level);
212      annotate_function_call ();
213      printf_filtered ("<function called from gdb>\n");
214      annotate_frame_end ();
215      return;
216    }
217  if (fi->signal_handler_caller)
218    {
219      annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
220
221      /* Do this regardless of SOURCE because we don't have any source
222	 to list for this frame.  */
223      if (level >= 0)
224	printf_filtered ("#%-2d ", level);
225      annotate_signal_handler_caller ();
226      printf_filtered ("<signal handler called>\n");
227      annotate_frame_end ();
228      return;
229    }
230
231  /* If fi is not the innermost frame, that normally means that fi->pc
232     points to *after* the call instruction, and we want to get the line
233     containing the call, never the next line.  But if the next frame is
234     a signal_handler_caller or a dummy frame, then the next frame was
235     not entered as the result of a call, and we want to get the line
236     containing fi->pc.  */
237  sal =
238    find_pc_line (fi->pc,
239		  fi->next != NULL
240		  && !fi->next->signal_handler_caller
241		  && !frame_in_dummy (fi->next));
242
243  func = find_pc_function (fi->pc);
244  if (func)
245    {
246      /* In certain pathological cases, the symtabs give the wrong
247	 function (when we are in the first function in a file which
248	 is compiled without debugging symbols, the previous function
249	 is compiled with debugging symbols, and the "foo.o" symbol
250	 that is supposed to tell us where the file with debugging symbols
251	 ends has been truncated by ar because it is longer than 15
252	 characters).  This also occurs if the user uses asm() to create
253	 a function but not stabs for it (in a file compiled -g).
254
255	 So look in the minimal symbol tables as well, and if it comes
256	 up with a larger address for the function use that instead.
257	 I don't think this can ever cause any problems; there shouldn't
258	 be any minimal symbols in the middle of a function; if this is
259	 ever changed many parts of GDB will need to be changed (and we'll
260	 create a find_pc_minimal_function or some such).  */
261
262      struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
263      if (msymbol != NULL
264	  && (SYMBOL_VALUE_ADDRESS (msymbol)
265	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
266	{
267#if 0
268	  /* There is no particular reason to think the line number
269	     information is wrong.  Someone might have just put in
270	     a label with asm() but left the line numbers alone.  */
271	  /* In this case we have no way of knowing the source file
272	     and line number, so don't print them.  */
273	  sal.symtab = 0;
274#endif
275	  /* We also don't know anything about the function besides
276	     its address and name.  */
277	  func = 0;
278	  funname = SYMBOL_NAME (msymbol);
279	  funlang = SYMBOL_LANGUAGE (msymbol);
280	}
281      else
282	{
283	  funname = SYMBOL_NAME (func);
284	  funlang = SYMBOL_LANGUAGE (func);
285	}
286    }
287  else
288    {
289      struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
290      if (msymbol != NULL)
291	{
292	  funname = SYMBOL_NAME (msymbol);
293	  funlang = SYMBOL_LANGUAGE (msymbol);
294	}
295    }
296
297  if (source >= 0 || !sal.symtab)
298    {
299      annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
300
301      if (level >= 0)
302	printf_filtered ("#%-2d ", level);
303      if (addressprint)
304	if (fi->pc != sal.pc || !sal.symtab)
305	  {
306	    annotate_frame_address ();
307	    print_address_numeric (fi->pc, 1, gdb_stdout);
308	    annotate_frame_address_end ();
309	    printf_filtered (" in ");
310	  }
311      annotate_frame_function_name ();
312      fprintf_symbol_filtered (gdb_stdout, funname ? funname : "??", funlang,
313			       DMGL_ANSI);
314      wrap_here ("   ");
315      annotate_frame_args ();
316      fputs_filtered (" (", gdb_stdout);
317      if (args)
318	{
319	  struct print_args_args args;
320	  args.fi = fi;
321	  args.func = func;
322	  catch_errors (print_args_stub, (char *)&args, "", RETURN_MASK_ALL);
323	  QUIT;
324	}
325      printf_filtered (")");
326      if (sal.symtab && sal.symtab->filename)
327	{
328	  annotate_frame_source_begin ();
329          wrap_here ("   ");
330	  printf_filtered (" at ");
331	  annotate_frame_source_file ();
332	  printf_filtered ("%s", sal.symtab->filename);
333	  annotate_frame_source_file_end ();
334	  printf_filtered (":");
335	  annotate_frame_source_line ();
336	  printf_filtered ("%d", sal.line);
337	  annotate_frame_source_end ();
338	}
339
340#ifdef PC_LOAD_SEGMENT
341     /* If we couldn't print out function name but if can figure out what
342        load segment this pc value is from, at least print out some info
343	about its load segment. */
344      if (!funname)
345	{
346	  annotate_frame_where ();
347	  wrap_here ("  ");
348	  printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
349	}
350#endif
351#ifdef PC_SOLIB
352      if (!funname)
353	{
354	  char *lib = PC_SOLIB (fi->pc);
355	  if (lib)
356	    {
357	      annotate_frame_where ();
358	      wrap_here ("  ");
359	      printf_filtered (" from %s", lib);
360	    }
361	}
362#endif
363      printf_filtered ("\n");
364    }
365
366  if ((source != 0) && sal.symtab)
367    {
368      int done = 0;
369      int mid_statement = source < 0 && fi->pc != sal.pc;
370      if (annotation_level)
371	done = identify_source_line (sal.symtab, sal.line, mid_statement,
372				     fi->pc);
373      if (!done)
374	{
375	  if (addressprint && mid_statement)
376	    {
377	      print_address_numeric (fi->pc, 1, gdb_stdout);
378	      printf_filtered ("\t");
379	    }
380	  if (print_frame_info_listing_hook)
381	    print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
382	  else
383	    print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
384	}
385      current_source_line = max (sal.line - lines_to_list/2, 1);
386    }
387  if (source != 0)
388    set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
389
390  annotate_frame_end ();
391
392  gdb_flush (gdb_stdout);
393}
394
395/* Read a frame specification in whatever the appropriate format is.
396   Call error() if the specification is in any way invalid (i.e.
397   this function never returns NULL).  */
398
399static struct frame_info *
400parse_frame_specification (frame_exp)
401     char *frame_exp;
402{
403  int numargs = 0;
404#define	MAXARGS	4
405  CORE_ADDR args[MAXARGS];
406
407  if (frame_exp)
408    {
409      char *addr_string, *p;
410      struct cleanup *tmp_cleanup;
411
412      while (*frame_exp == ' ') frame_exp++;
413
414      while (*frame_exp)
415	{
416	  if (numargs > MAXARGS)
417	    error ("Too many args in frame specification");
418	  /* Parse an argument.  */
419          for (p = frame_exp; *p && *p != ' '; p++)
420	    ;
421	  addr_string = savestring(frame_exp, p - frame_exp);
422
423	  {
424	    tmp_cleanup = make_cleanup (free, addr_string);
425	    args[numargs++] = parse_and_eval_address (addr_string);
426	    do_cleanups (tmp_cleanup);
427	  }
428
429	  /* Skip spaces, move to possible next arg.  */
430	  while (*p == ' ') p++;
431	  frame_exp = p;
432	}
433    }
434
435  switch (numargs)
436    {
437    case 0:
438      if (selected_frame == NULL)
439	error ("No selected frame.");
440      return selected_frame;
441      /* NOTREACHED */
442    case 1:
443      {
444	int level = args[0];
445	struct frame_info *fid =
446	  find_relative_frame (get_current_frame (), &level);
447	struct frame_info *tfid;
448
449	if (level == 0)
450	  /* find_relative_frame was successful */
451	  return fid;
452
453	/* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
454	   take at least 2 addresses.  It is important to detect this case
455	   here so that "frame 100" does not give a confusing error message
456	   like "frame specification requires two addresses".  This of course
457	   does not solve the "frame 100" problem for machines on which
458	   a frame specification can be made with one address.  To solve
459	   that, we need a new syntax for a specifying a frame by address.
460	   I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
461	   two args, etc.), but people might think that is too much typing,
462	   so I guess *0x23,0x45 would be a possible alternative (commas
463	   really should be used instead of spaces to delimit; using spaces
464	   normally works in an expression).  */
465#ifdef SETUP_ARBITRARY_FRAME
466	error ("No frame %d", args[0]);
467#endif
468
469	/* If (s)he specifies the frame with an address, he deserves what
470	   (s)he gets.  Still, give the highest one that matches.  */
471
472	for (fid = get_current_frame ();
473	     fid && fid->frame != args[0];
474	     fid = get_prev_frame (fid))
475	  ;
476
477	if (fid)
478	  while ((tfid = get_prev_frame (fid)) &&
479		 (tfid->frame == args[0]))
480	    fid = tfid;
481
482	/* We couldn't identify the frame as an existing frame, but
483	   perhaps we can create one with a single argument.  */
484      }
485
486     default:
487#ifdef SETUP_ARBITRARY_FRAME
488      return SETUP_ARBITRARY_FRAME (numargs, args);
489#else
490      /* Usual case.  Do it here rather than have everyone supply
491	 a SETUP_ARBITRARY_FRAME that does this.  */
492      if (numargs == 1)
493	return create_new_frame (args[0], 0);
494      error ("Too many args in frame specification");
495#endif
496      /* NOTREACHED */
497    }
498  /* NOTREACHED */
499}
500
501/* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
502   that if it is unsure about the answer, it returns 0
503   instead of guessing (this happens on the VAX and i960, for example).
504
505   On most machines, we never have to guess about the args address,
506   so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
507#if !defined (FRAME_ARGS_ADDRESS_CORRECT)
508#define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
509#endif
510
511/* Print verbosely the selected frame or the frame at address ADDR.
512   This means absolutely all information in the frame is printed.  */
513
514static void
515frame_info (addr_exp, from_tty)
516     char *addr_exp;
517     int from_tty;
518{
519  struct frame_info *fi;
520  struct frame_saved_regs fsr;
521  struct symtab_and_line sal;
522  struct symbol *func;
523  struct symtab *s;
524  struct frame_info *calling_frame_info;
525  int i, count, numregs;
526  char *funname = 0;
527  enum language funlang = language_unknown;
528
529  if (!target_has_stack)
530    error ("No stack.");
531
532  fi = parse_frame_specification (addr_exp);
533  if (fi == NULL)
534    error ("Invalid frame specified.");
535
536  sal = find_pc_line (fi->pc,
537		      fi->next != NULL
538		      && !fi->next->signal_handler_caller
539		      && !frame_in_dummy (fi->next));
540  func = get_frame_function (fi);
541  s = find_pc_symtab(fi->pc);
542  if (func)
543    {
544      funname = SYMBOL_NAME (func);
545      funlang = SYMBOL_LANGUAGE (func);
546    }
547  else
548    {
549      register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
550      if (msymbol != NULL)
551	{
552	  funname = SYMBOL_NAME (msymbol);
553	  funlang = SYMBOL_LANGUAGE (msymbol);
554	}
555    }
556  calling_frame_info = get_prev_frame (fi);
557
558  if (!addr_exp && selected_frame_level >= 0)
559    {
560      printf_filtered ("Stack level %d, frame at ", selected_frame_level);
561      print_address_numeric (fi->frame, 1, gdb_stdout);
562      printf_filtered (":\n");
563    }
564  else
565    {
566      printf_filtered ("Stack frame at ");
567      print_address_numeric (fi->frame, 1, gdb_stdout);
568      printf_filtered (":\n");
569    }
570  printf_filtered (" %s = ", reg_names[PC_REGNUM]);
571  print_address_numeric (fi->pc, 1, gdb_stdout);
572
573  wrap_here ("   ");
574  if (funname)
575    {
576      printf_filtered (" in ");
577      fprintf_symbol_filtered (gdb_stdout, funname, funlang,
578			       DMGL_ANSI | DMGL_PARAMS);
579    }
580  wrap_here ("   ");
581  if (sal.symtab)
582    printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
583  puts_filtered ("; ");
584  wrap_here ("    ");
585  printf_filtered ("saved %s ", reg_names[PC_REGNUM]);
586  print_address_numeric (FRAME_SAVED_PC (fi), 1, gdb_stdout);
587  printf_filtered ("\n");
588
589  {
590    int frameless = 0;
591#ifdef FRAMELESS_FUNCTION_INVOCATION
592    FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
593#endif
594    if (frameless)
595      printf_filtered (" (FRAMELESS),");
596  }
597
598  if (calling_frame_info)
599    {
600      printf_filtered (" called by frame at ");
601      print_address_numeric (calling_frame_info->frame, 1, gdb_stdout);
602    }
603  if (fi->next && calling_frame_info)
604    puts_filtered (",");
605  wrap_here ("   ");
606  if (fi->next)
607    {
608      printf_filtered (" caller of frame at ");
609      print_address_numeric (fi->next->frame, 1, gdb_stdout);
610    }
611  if (fi->next || calling_frame_info)
612    puts_filtered ("\n");
613  if (s)
614    printf_filtered (" source language %s.\n", language_str (s->language));
615
616#ifdef PRINT_EXTRA_FRAME_INFO
617  PRINT_EXTRA_FRAME_INFO (fi);
618#endif
619
620  {
621    /* Address of the argument list for this frame, or 0.  */
622    CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
623    /* Number of args for this frame, or -1 if unknown.  */
624    int numargs;
625
626    if (arg_list == 0)
627      printf_filtered (" Arglist at unknown address.\n");
628    else
629      {
630	printf_filtered (" Arglist at ");
631	print_address_numeric (arg_list, 1, gdb_stdout);
632	printf_filtered (",");
633
634	FRAME_NUM_ARGS (numargs, fi);
635	if (numargs < 0)
636	  puts_filtered (" args: ");
637	else if (numargs == 0)
638	  puts_filtered (" no args.");
639	else if (numargs == 1)
640	  puts_filtered (" 1 arg: ");
641	else
642	  printf_filtered (" %d args: ", numargs);
643	print_frame_args (func, fi, numargs, gdb_stdout);
644	puts_filtered ("\n");
645      }
646  }
647  {
648    /* Address of the local variables for this frame, or 0.  */
649    CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
650
651    if (arg_list == 0)
652      printf_filtered (" Locals at unknown address,");
653    else
654      {
655	printf_filtered (" Locals at ");
656	print_address_numeric (arg_list, 1, gdb_stdout);
657	printf_filtered (",");
658      }
659  }
660
661#if defined (FRAME_FIND_SAVED_REGS)
662  get_frame_saved_regs (fi, &fsr);
663  /* The sp is special; what's returned isn't the save address, but
664     actually the value of the previous frame's sp.  */
665  printf_filtered (" Previous frame's sp is ");
666  print_address_numeric (fsr.regs[SP_REGNUM], 1, gdb_stdout);
667  printf_filtered ("\n");
668  count = 0;
669  numregs = ARCH_NUM_REGS;
670  for (i = 0; i < numregs; i++)
671    if (fsr.regs[i] && i != SP_REGNUM)
672      {
673	if (count == 0)
674	  puts_filtered (" Saved registers:\n ");
675	else
676	  puts_filtered (",");
677	wrap_here (" ");
678	printf_filtered (" %s at ", reg_names[i]);
679	print_address_numeric (fsr.regs[i], 1, gdb_stdout);
680	count++;
681      }
682  if (count)
683    puts_filtered ("\n");
684#else  /* Have FRAME_FIND_SAVED_REGS.  */
685  /* We could get some information about saved registers by calling
686     get_saved_register on each register.  Which info goes with which frame
687     is necessarily lost, however, and I suspect that the users don't care
688     whether they get the info.  */
689  puts_filtered ("\n");
690#endif /* Have FRAME_FIND_SAVED_REGS.  */
691}
692
693#if 0
694/* Set a limit on the number of frames printed by default in a
695   backtrace.  */
696
697static int backtrace_limit;
698
699static void
700set_backtrace_limit_command (count_exp, from_tty)
701     char *count_exp;
702     int from_tty;
703{
704  int count = parse_and_eval_address (count_exp);
705
706  if (count < 0)
707    error ("Negative argument not meaningful as backtrace limit.");
708
709  backtrace_limit = count;
710}
711
712static void
713backtrace_limit_info (arg, from_tty)
714     char *arg;
715     int from_tty;
716{
717  if (arg)
718    error ("\"Info backtrace-limit\" takes no arguments.");
719
720  printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
721}
722#endif
723
724/* Print briefly all stack frames or just the innermost COUNT frames.  */
725
726static void
727backtrace_command (count_exp, from_tty)
728     char *count_exp;
729     int from_tty;
730{
731  struct frame_info *fi;
732  register int count;
733  register int i;
734  register struct frame_info *trailing;
735  register int trailing_level;
736
737  if (!target_has_stack)
738    error ("No stack.");
739
740  /* The following code must do two things.  First, it must
741     set the variable TRAILING to the frame from which we should start
742     printing.  Second, it must set the variable count to the number
743     of frames which we should print, or -1 if all of them.  */
744  trailing = get_current_frame ();
745  trailing_level = 0;
746  if (count_exp)
747    {
748      count = parse_and_eval_address (count_exp);
749      if (count < 0)
750	{
751	  struct frame_info *current;
752
753	  count = -count;
754
755	  current = trailing;
756	  while (current && count--)
757	    {
758	      QUIT;
759	      current = get_prev_frame (current);
760	    }
761
762	  /* Will stop when CURRENT reaches the top of the stack.  TRAILING
763	     will be COUNT below it.  */
764	  while (current)
765	    {
766	      QUIT;
767	      trailing = get_prev_frame (trailing);
768	      current = get_prev_frame (current);
769	      trailing_level++;
770	    }
771
772	  count = -1;
773	}
774    }
775  else
776    count = -1;
777
778  if (info_verbose)
779    {
780      struct partial_symtab *ps;
781
782      /* Read in symbols for all of the frames.  Need to do this in
783	 a separate pass so that "Reading in symbols for xxx" messages
784	 don't screw up the appearance of the backtrace.  Also
785	 if people have strong opinions against reading symbols for
786	 backtrace this may have to be an option.  */
787      i = count;
788      for (fi = trailing;
789	   fi != NULL && i--;
790	   fi = get_prev_frame (fi))
791	{
792	  QUIT;
793	  ps = find_pc_psymtab (fi->pc);
794	  if (ps)
795	    PSYMTAB_TO_SYMTAB (ps);	/* Force syms to come in */
796	}
797    }
798
799  for (i = 0, fi = trailing;
800       fi && count--;
801       i++, fi = get_prev_frame (fi))
802    {
803      QUIT;
804
805      /* Don't use print_stack_frame; if an error() occurs it probably
806	 means further attempts to backtrace would fail (on the other
807	 hand, perhaps the code does or could be fixed to make sure
808	 the frame->prev field gets set to NULL in that case).  */
809      print_frame_info (fi, trailing_level + i, 0, 1);
810    }
811
812  /* If we've stopped before the end, mention that.  */
813  if (fi && from_tty)
814    printf_filtered ("(More stack frames follow...)\n");
815}
816
817/* Print the local variables of a block B active in FRAME.
818   Return 1 if any variables were printed; 0 otherwise.  */
819
820static int
821print_block_frame_locals (b, fi, stream)
822     struct block *b;
823     register struct frame_info *fi;
824     register GDB_FILE *stream;
825{
826  int nsyms;
827  register int i;
828  register struct symbol *sym;
829  register int values_printed = 0;
830
831  nsyms = BLOCK_NSYMS (b);
832
833  for (i = 0; i < nsyms; i++)
834    {
835      sym = BLOCK_SYM (b, i);
836      switch (SYMBOL_CLASS (sym))
837	{
838	case LOC_LOCAL:
839	case LOC_REGISTER:
840	case LOC_STATIC:
841	case LOC_BASEREG:
842	  values_printed = 1;
843	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
844	  fputs_filtered (" = ", stream);
845	  print_variable_value (sym, fi, stream);
846	  fprintf_filtered (stream, "\n");
847	  break;
848
849	default:
850	  /* Ignore symbols which are not locals.  */
851	  break;
852	}
853    }
854  return values_printed;
855}
856
857/* Same, but print labels.  */
858
859static int
860print_block_frame_labels (b, have_default, stream)
861     struct block *b;
862     int *have_default;
863     register GDB_FILE *stream;
864{
865  int nsyms;
866  register int i;
867  register struct symbol *sym;
868  register int values_printed = 0;
869
870  nsyms = BLOCK_NSYMS (b);
871
872  for (i = 0; i < nsyms; i++)
873    {
874      sym = BLOCK_SYM (b, i);
875      if (STREQ (SYMBOL_NAME (sym), "default"))
876	{
877	  if (*have_default)
878	    continue;
879	  *have_default = 1;
880	}
881      if (SYMBOL_CLASS (sym) == LOC_LABEL)
882	{
883	  struct symtab_and_line sal;
884	  sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
885	  values_printed = 1;
886	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
887	  if (addressprint)
888	    {
889	      fprintf_filtered (stream, " ");
890	      print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
891	    }
892	  fprintf_filtered (stream, " in file %s, line %d\n",
893			    sal.symtab->filename, sal.line);
894	}
895    }
896  return values_printed;
897}
898
899/* Print on STREAM all the local variables in frame FRAME,
900   including all the blocks active in that frame
901   at its current pc.
902
903   Returns 1 if the job was done,
904   or 0 if nothing was printed because we have no info
905   on the function running in FRAME.  */
906
907static void
908print_frame_local_vars (fi, stream)
909     register struct frame_info *fi;
910     register GDB_FILE *stream;
911{
912  register struct block *block = get_frame_block (fi);
913  register int values_printed = 0;
914
915  if (block == 0)
916    {
917      fprintf_filtered (stream, "No symbol table info available.\n");
918      return;
919    }
920
921  while (block != 0)
922    {
923      if (print_block_frame_locals (block, fi, stream))
924	values_printed = 1;
925      /* After handling the function's top-level block, stop.
926	 Don't continue to its superblock, the block of
927	 per-file symbols.  */
928      if (BLOCK_FUNCTION (block))
929	break;
930      block = BLOCK_SUPERBLOCK (block);
931    }
932
933  if (!values_printed)
934    {
935      fprintf_filtered (stream, "No locals.\n");
936    }
937}
938
939/* Same, but print labels.  */
940
941static void
942print_frame_label_vars (fi, this_level_only, stream)
943     register struct frame_info *fi;
944     int this_level_only;
945     register GDB_FILE *stream;
946{
947  register struct blockvector *bl;
948  register struct block *block = get_frame_block (fi);
949  register int values_printed = 0;
950  int index, have_default = 0;
951  char *blocks_printed;
952  CORE_ADDR pc = fi->pc;
953
954  if (block == 0)
955    {
956      fprintf_filtered (stream, "No symbol table info available.\n");
957      return;
958    }
959
960  bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
961  blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
962  memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
963
964  while (block != 0)
965    {
966      CORE_ADDR end = BLOCK_END (block) - 4;
967      int last_index;
968
969      if (bl != blockvector_for_pc (end, &index))
970	error ("blockvector blotch");
971      if (BLOCKVECTOR_BLOCK (bl, index) != block)
972	error ("blockvector botch");
973      last_index = BLOCKVECTOR_NBLOCKS (bl);
974      index += 1;
975
976      /* Don't print out blocks that have gone by.  */
977      while (index < last_index
978	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
979	index++;
980
981      while (index < last_index
982	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
983	{
984	  if (blocks_printed[index] == 0)
985	    {
986	      if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
987		values_printed = 1;
988	      blocks_printed[index] = 1;
989	    }
990	  index++;
991	}
992      if (have_default)
993	return;
994      if (values_printed && this_level_only)
995	return;
996
997      /* After handling the function's top-level block, stop.
998	 Don't continue to its superblock, the block of
999	 per-file symbols.  */
1000      if (BLOCK_FUNCTION (block))
1001	break;
1002      block = BLOCK_SUPERBLOCK (block);
1003    }
1004
1005  if (!values_printed && !this_level_only)
1006    {
1007      fprintf_filtered (stream, "No catches.\n");
1008    }
1009}
1010
1011/* ARGSUSED */
1012static void
1013locals_info (args, from_tty)
1014     char *args;
1015     int from_tty;
1016{
1017  if (!selected_frame)
1018    error ("No frame selected.");
1019  print_frame_local_vars (selected_frame, gdb_stdout);
1020}
1021
1022static void
1023catch_info (ignore, from_tty)
1024     char *ignore;
1025     int from_tty;
1026{
1027  if (!selected_frame)
1028    error ("No frame selected.");
1029  print_frame_label_vars (selected_frame, 0, gdb_stdout);
1030}
1031
1032static void
1033print_frame_arg_vars (fi, stream)
1034     register struct frame_info *fi;
1035     register GDB_FILE *stream;
1036{
1037  struct symbol *func = get_frame_function (fi);
1038  register struct block *b;
1039  int nsyms;
1040  register int i;
1041  register struct symbol *sym, *sym2;
1042  register int values_printed = 0;
1043
1044  if (func == 0)
1045    {
1046      fprintf_filtered (stream, "No symbol table info available.\n");
1047      return;
1048    }
1049
1050  b = SYMBOL_BLOCK_VALUE (func);
1051  nsyms = BLOCK_NSYMS (b);
1052
1053  for (i = 0; i < nsyms; i++)
1054    {
1055      sym = BLOCK_SYM (b, i);
1056      switch (SYMBOL_CLASS (sym))
1057	{
1058	case LOC_ARG:
1059	case LOC_LOCAL_ARG:
1060	case LOC_REF_ARG:
1061	case LOC_REGPARM:
1062	case LOC_REGPARM_ADDR:
1063	case LOC_BASEREG_ARG:
1064	  values_printed = 1;
1065	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1066	  fputs_filtered (" = ", stream);
1067
1068	  /* We have to look up the symbol because arguments can have
1069	     two entries (one a parameter, one a local) and the one we
1070	     want is the local, which lookup_symbol will find for us.
1071	     This includes gcc1 (not gcc2) on the sparc when passing a
1072	     small structure and gcc2 when the argument type is float
1073	     and it is passed as a double and converted to float by
1074	     the prologue (in the latter case the type of the LOC_ARG
1075	     symbol is double and the type of the LOC_LOCAL symbol is
1076	     float).  There are also LOC_ARG/LOC_REGISTER pairs which
1077	     are not combined in symbol-reading.  */
1078
1079	  sym2 = lookup_symbol (SYMBOL_NAME (sym),
1080			b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
1081	  print_variable_value (sym2, fi, stream);
1082	  fprintf_filtered (stream, "\n");
1083	  break;
1084
1085	default:
1086	  /* Don't worry about things which aren't arguments.  */
1087	  break;
1088	}
1089    }
1090
1091  if (!values_printed)
1092    {
1093      fprintf_filtered (stream, "No arguments.\n");
1094    }
1095}
1096
1097static void
1098args_info (ignore, from_tty)
1099     char *ignore;
1100     int from_tty;
1101{
1102  if (!selected_frame)
1103    error ("No frame selected.");
1104  print_frame_arg_vars (selected_frame, gdb_stdout);
1105}
1106
1107/* Select frame FI, and note that its stack level is LEVEL.
1108   LEVEL may be -1 if an actual level number is not known.  */
1109
1110void
1111select_frame (fi, level)
1112     struct frame_info *fi;
1113     int level;
1114{
1115  register struct symtab *s;
1116
1117  selected_frame = fi;
1118  selected_frame_level = level;
1119
1120  /* Ensure that symbols for this frame are read in.  Also, determine the
1121     source language of this frame, and switch to it if desired.  */
1122  if (fi)
1123  {
1124    s = find_pc_symtab (fi->pc);
1125    if (s
1126	&& s->language != current_language->la_language
1127	&& s->language != language_unknown
1128	&& language_mode == language_mode_auto) {
1129      set_language(s->language);
1130    }
1131  }
1132}
1133
1134/* Store the selected frame and its level into *FRAMEP and *LEVELP.
1135   If there is no selected frame, *FRAMEP is set to NULL.  */
1136
1137void
1138record_selected_frame (frameaddrp, levelp)
1139     CORE_ADDR *frameaddrp;
1140     int *levelp;
1141{
1142  *frameaddrp = selected_frame ? selected_frame->frame : 0;
1143  *levelp = selected_frame_level;
1144}
1145
1146/* Return the symbol-block in which the selected frame is executing.
1147   Can return zero under various legitimate circumstances.  */
1148
1149struct block *
1150get_selected_block ()
1151{
1152  if (!target_has_stack)
1153    return 0;
1154
1155  if (!selected_frame)
1156    return get_current_block ();
1157  return get_frame_block (selected_frame);
1158}
1159
1160/* Find a frame a certain number of levels away from FRAME.
1161   LEVEL_OFFSET_PTR points to an int containing the number of levels.
1162   Positive means go to earlier frames (up); negative, the reverse.
1163   The int that contains the number of levels is counted toward
1164   zero as the frames for those levels are found.
1165   If the top or bottom frame is reached, that frame is returned,
1166   but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1167   how much farther the original request asked to go.  */
1168
1169struct frame_info *
1170find_relative_frame (frame, level_offset_ptr)
1171     register struct frame_info *frame;
1172     register int *level_offset_ptr;
1173{
1174  register struct frame_info *prev;
1175  register struct frame_info *frame1;
1176
1177  /* Going up is simple: just do get_prev_frame enough times
1178     or until initial frame is reached.  */
1179  while (*level_offset_ptr > 0)
1180    {
1181      prev = get_prev_frame (frame);
1182      if (prev == 0)
1183	break;
1184      (*level_offset_ptr)--;
1185      frame = prev;
1186    }
1187  /* Going down is just as simple.  */
1188  if (*level_offset_ptr < 0)
1189    {
1190      while (*level_offset_ptr < 0) {
1191	frame1 = get_next_frame (frame);
1192	if (!frame1)
1193	  break;
1194	frame = frame1;
1195	(*level_offset_ptr)++;
1196      }
1197    }
1198  return frame;
1199}
1200
1201/* The "select_frame" command.  With no arg, NOP.
1202   With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1203   valid level.  Otherwise, treat level_exp as an address expression
1204   and select it.  See parse_frame_specification for more info on proper
1205   frame expressions. */
1206
1207/* ARGSUSED */
1208static void
1209select_frame_command (level_exp, from_tty)
1210     char *level_exp;
1211     int from_tty;
1212{
1213  register struct frame_info *frame, *frame1;
1214  unsigned int level = 0;
1215
1216  if (!target_has_stack)
1217    error ("No stack.");
1218
1219  frame = parse_frame_specification (level_exp);
1220
1221  /* Try to figure out what level this frame is.  But if there is
1222     no current stack, don't error out -- let the user set one.  */
1223  frame1 = 0;
1224  if (get_current_frame()) {
1225    for (frame1 = get_prev_frame (0);
1226	 frame1 && frame1 != frame;
1227	 frame1 = get_prev_frame (frame1))
1228      level++;
1229  }
1230
1231  if (!frame1)
1232    level = 0;
1233
1234  select_frame (frame, level);
1235}
1236
1237/* The "frame" command.  With no arg, print selected frame briefly.
1238   With arg, behaves like select_frame and then prints the selected
1239   frame.  */
1240
1241static void
1242frame_command (level_exp, from_tty)
1243     char *level_exp;
1244     int from_tty;
1245{
1246  select_frame_command (level_exp, from_tty);
1247  print_stack_frame (selected_frame, selected_frame_level, 1);
1248}
1249
1250/* Select the frame up one or COUNT stack levels
1251   from the previously selected frame, and print it briefly.  */
1252
1253/* ARGSUSED */
1254static void
1255up_silently_command (count_exp, from_tty)
1256     char *count_exp;
1257     int from_tty;
1258{
1259  register struct frame_info *fi;
1260  int count = 1, count1;
1261  if (count_exp)
1262    count = parse_and_eval_address (count_exp);
1263  count1 = count;
1264
1265  if (target_has_stack == 0 || selected_frame == 0)
1266    error ("No stack.");
1267
1268  fi = find_relative_frame (selected_frame, &count1);
1269  if (count1 != 0 && count_exp == 0)
1270    error ("Initial frame selected; you cannot go up.");
1271  select_frame (fi, selected_frame_level + count - count1);
1272}
1273
1274static void
1275up_command (count_exp, from_tty)
1276     char *count_exp;
1277     int from_tty;
1278{
1279  up_silently_command (count_exp, from_tty);
1280  print_stack_frame (selected_frame, selected_frame_level, 1);
1281}
1282
1283/* Select the frame down one or COUNT stack levels
1284   from the previously selected frame, and print it briefly.  */
1285
1286/* ARGSUSED */
1287static void
1288down_silently_command (count_exp, from_tty)
1289     char *count_exp;
1290     int from_tty;
1291{
1292  register struct frame_info *frame;
1293  int count = -1, count1;
1294  if (count_exp)
1295    count = - parse_and_eval_address (count_exp);
1296  count1 = count;
1297
1298  if (target_has_stack == 0 || selected_frame == 0)
1299    error ("No stack.");
1300
1301  frame = find_relative_frame (selected_frame, &count1);
1302  if (count1 != 0 && count_exp == 0)
1303    {
1304
1305      /* We only do this if count_exp is not specified.  That way "down"
1306	 means to really go down (and let me know if that is
1307	 impossible), but "down 9999" can be used to mean go all the way
1308	 down without getting an error.  */
1309
1310      error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1311    }
1312
1313  select_frame (frame, selected_frame_level + count - count1);
1314}
1315
1316
1317static void
1318down_command (count_exp, from_tty)
1319     char *count_exp;
1320     int from_tty;
1321{
1322  down_silently_command (count_exp, from_tty);
1323  print_stack_frame (selected_frame, selected_frame_level, 1);
1324}
1325
1326static void
1327return_command (retval_exp, from_tty)
1328     char *retval_exp;
1329     int from_tty;
1330{
1331  struct symbol *thisfun;
1332  CORE_ADDR selected_frame_addr;
1333  CORE_ADDR selected_frame_pc;
1334  struct frame_info *frame;
1335  value_ptr return_value = NULL;
1336
1337  if (selected_frame == NULL)
1338    error ("No selected frame.");
1339  thisfun = get_frame_function (selected_frame);
1340  selected_frame_addr = FRAME_FP (selected_frame);
1341  selected_frame_pc = selected_frame->pc;
1342
1343  /* Compute the return value (if any -- possibly getting errors here).  */
1344
1345  if (retval_exp)
1346    {
1347      struct type *return_type = NULL;
1348
1349      return_value = parse_and_eval (retval_exp);
1350
1351      /* Cast return value to the return type of the function.  */
1352      if (thisfun != NULL)
1353	return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1354      if (return_type == NULL)
1355	return_type = builtin_type_int;
1356      return_value = value_cast (return_type, return_value);
1357
1358      /* Make sure we have fully evaluated it, since
1359	 it might live in the stack frame we're about to pop.  */
1360      if (VALUE_LAZY (return_value))
1361	value_fetch_lazy (return_value);
1362    }
1363
1364  /* If interactive, require confirmation.  */
1365
1366  if (from_tty)
1367    {
1368      if (thisfun != 0)
1369	{
1370	  if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1371	    {
1372	      error ("Not confirmed.");
1373	      /* NOTREACHED */
1374	    }
1375	}
1376      else
1377	if (!query ("Make selected stack frame return now? "))
1378	  error ("Not confirmed.");
1379    }
1380
1381  /* Do the real work.  Pop until the specified frame is current.  We
1382     use this method because the selected_frame is not valid after
1383     a POP_FRAME.  The pc comparison makes this work even if the
1384     selected frame shares its fp with another frame.  */
1385
1386  while (selected_frame_addr != (frame = get_current_frame())->frame
1387	 || selected_frame_pc != frame->pc)
1388    POP_FRAME;
1389
1390  /* Then pop that frame.  */
1391
1392  POP_FRAME;
1393
1394  /* Compute the return value (if any) and store in the place
1395     for return values.  */
1396
1397  if (retval_exp)
1398    set_return_value (return_value);
1399
1400  /* If interactive, print the frame that is now current.  */
1401
1402  if (from_tty)
1403    frame_command ("0", 1);
1404  else
1405    select_frame_command ("0", 0);
1406}
1407
1408/* Gets the language of the current frame.  */
1409
1410enum language
1411get_frame_language()
1412{
1413  register struct symtab *s;
1414  enum language flang;		/* The language of the current frame */
1415
1416  if (selected_frame)
1417    {
1418      s = find_pc_symtab(selected_frame->pc);
1419      if (s)
1420	flang = s->language;
1421      else
1422	flang = language_unknown;
1423    }
1424  else
1425    flang = language_unknown;
1426
1427  return flang;
1428}
1429
1430void
1431_initialize_stack ()
1432{
1433#if 0
1434  backtrace_limit = 30;
1435#endif
1436
1437  add_com ("return", class_stack, return_command,
1438	   "Make selected stack frame return to its caller.\n\
1439Control remains in the debugger, but when you continue\n\
1440execution will resume in the frame above the one now selected.\n\
1441If an argument is given, it is an expression for the value to return.");
1442
1443  add_com ("up", class_stack, up_command,
1444	   "Select and print stack frame that called this one.\n\
1445An argument says how many frames up to go.");
1446  add_com ("up-silently", class_support, up_silently_command,
1447	   "Same as the `up' command, but does not print anything.\n\
1448This is useful in command scripts.");
1449
1450  add_com ("down", class_stack, down_command,
1451	   "Select and print stack frame called by this one.\n\
1452An argument says how many frames down to go.");
1453  add_com_alias ("do", "down", class_stack, 1);
1454  add_com_alias ("dow", "down", class_stack, 1);
1455  add_com ("down-silently", class_support, down_silently_command,
1456	   "Same as the `down' command, but does not print anything.\n\
1457This is useful in command scripts.");
1458
1459  add_com ("frame", class_stack, frame_command,
1460	   "Select and print a stack frame.\n\
1461With no argument, print the selected stack frame.  (See also \"info frame\").\n\
1462An argument specifies the frame to select.\n\
1463It can be a stack frame number or the address of the frame.\n\
1464With argument, nothing is printed if input is coming from\n\
1465a command file or a user-defined command.");
1466
1467  add_com_alias ("f", "frame", class_stack, 1);
1468
1469  add_com ("select-frame", class_stack, select_frame_command,
1470	   "Select a stack frame without printing anything.\n\
1471An argument specifies the frame to select.\n\
1472It can be a stack frame number or the address of the frame.\n");
1473
1474  add_com ("backtrace", class_stack, backtrace_command,
1475	   "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1476With a negative argument, print outermost -COUNT frames.");
1477  add_com_alias ("bt", "backtrace", class_stack, 0);
1478  add_com_alias ("where", "backtrace", class_alias, 0);
1479  add_info ("stack", backtrace_command,
1480	    "Backtrace of the stack, or innermost COUNT frames.");
1481  add_info_alias ("s", "stack", 1);
1482  add_info ("frame", frame_info,
1483	    "All about selected stack frame, or frame at ADDR.");
1484  add_info_alias ("f", "frame", 1);
1485  add_info ("locals", locals_info,
1486	    "Local variables of current stack frame.");
1487  add_info ("args", args_info,
1488	    "Argument variables of current stack frame.");
1489  add_info ("catch", catch_info,
1490	    "Exceptions that can be caught in the current stack frame.");
1491
1492#if 0
1493  add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
1494	   "Specify maximum number of frames for \"backtrace\" to print by default.",
1495	   &setlist);
1496  add_info ("backtrace-limit", backtrace_limit_info,
1497	    "The maximum number of frames for \"backtrace\" to print by default.");
1498#endif
1499}
1500