OSDN Git Service

2003-02-01 Andrew Cagney <ac131313@redhat.com>
[pf3gnuchains/pf3gnuchains3x.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
5    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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include <ctype.h>
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include "value.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "language.h"
32 #include "frame.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "target.h"
36 #include "source.h"
37 #include "breakpoint.h"
38 #include "demangle.h"
39 #include "inferior.h"
40 #include "annotate.h"
41 #include "ui-out.h"
42
43 /* Prototypes for exported functions. */
44
45 void args_info (char *, int);
46
47 void locals_info (char *, int);
48
49 void (*selected_frame_level_changed_hook) (int);
50
51 void _initialize_stack (void);
52
53 void return_command (char *, int);
54
55 /* Prototypes for local functions. */
56
57 static void down_command (char *, int);
58
59 static void down_silently_base (char *);
60
61 static void down_silently_command (char *, int);
62
63 static void up_command (char *, int);
64
65 static void up_silently_base (char *);
66
67 static void up_silently_command (char *, int);
68
69 void frame_command (char *, int);
70
71 static void current_frame_command (char *, int);
72
73 static void select_frame_command (char *, int);
74
75 static void print_frame_arg_vars (struct frame_info *, struct ui_file *);
76
77 static void catch_info (char *, int);
78
79 static void args_plus_locals_info (char *, int);
80
81 static void print_frame_label_vars (struct frame_info *, int,
82                                     struct ui_file *);
83
84 static void print_frame_local_vars (struct frame_info *, int,
85                                     struct ui_file *);
86
87 static int print_block_frame_labels (struct block *, int *,
88                                      struct ui_file *);
89
90 static int print_block_frame_locals (struct block *,
91                                      struct frame_info *,
92                                      int,
93                                      struct ui_file *);
94
95 static void print_frame (struct frame_info *fi, 
96                          int level, 
97                          int source, 
98                          int args, 
99                          struct symtab_and_line sal);
100
101 static void backtrace_command (char *, int);
102
103 struct frame_info *parse_frame_specification (char *);
104
105 static void frame_info (char *, int);
106
107 extern int addressprint;        /* Print addresses, or stay symbolic only? */
108
109 /* Zero means do things normally; we are interacting directly with the
110    user.  One means print the full filename and linenumber when a
111    frame is printed, and do so in a format emacs18/emacs19.22 can
112    parse.  Two means print similar annotations, but in many more
113    cases and in a slightly different syntax.  */
114
115 int annotation_level = 0;
116 \f
117
118 struct print_stack_frame_args
119   {
120     struct frame_info *fi;
121     int level;
122     int source;
123     int args;
124   };
125
126 /* Show or print the frame arguments.
127    Pass the args the way catch_errors wants them.  */
128 static int print_stack_frame_stub (void *args);
129 static int
130 print_stack_frame_stub (void *args)
131 {
132   struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
133
134   print_frame_info (p->fi, p->level, p->source, p->args);
135   return 0;
136 }
137
138 /* Show or print a stack frame briefly.  FRAME_INFI should be the frame info
139    and LEVEL should be its level in the stack (or -1 for level not defined).
140    This prints the level, the function executing, the arguments,
141    and the file name and line number.
142    If the pc is not at the beginning of the source line,
143    the actual pc is printed at the beginning.
144
145    If SOURCE is 1, print the source line as well.
146    If SOURCE is -1, print ONLY the source line.  */
147
148 void
149 print_stack_frame (struct frame_info *fi, int level, int source)
150 {
151   struct print_stack_frame_args args;
152
153   args.fi = fi;
154   args.level = level;
155   args.source = source;
156   args.args = 1;
157
158   catch_errors (print_stack_frame_stub, (char *) &args, "", RETURN_MASK_ALL);
159 }  
160
161 struct print_args_args
162 {
163   struct symbol *func;
164   struct frame_info *fi;
165   struct ui_file *stream;
166 };
167
168 static int print_args_stub (void *);
169
170 /* Pass the args the way catch_errors wants them.  */
171
172 static int
173 print_args_stub (void *args)
174 {
175   int numargs;
176   struct print_args_args *p = (struct print_args_args *) args;
177
178   numargs = FRAME_NUM_ARGS (p->fi);
179   print_frame_args (p->func, p->fi, numargs, p->stream);
180   return 0;
181 }
182
183 /* Print information about a frame for frame "fi" at level "level".
184    Used in "where" output, also used to emit breakpoint or step
185    messages.  
186    LEVEL is the level of the frame, or -1 if it is the
187    innermost frame but we don't want to print the level.  
188    The meaning of the SOURCE argument is: 
189    SRC_LINE: Print only source line
190    LOCATION: Print only location 
191    LOC_AND_SRC: Print location and source line.  */
192
193 void
194 print_frame_info (struct frame_info *fi, int level, int source, int args)
195 {
196   struct symtab_and_line sal;
197   int source_print;
198   int location_print;
199
200   if (get_frame_type (fi) == DUMMY_FRAME
201       || get_frame_type (fi) == SIGTRAMP_FRAME)
202     {
203       struct cleanup *uiout_cleanup
204         = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
205
206       /* Do this regardless of SOURCE because we don't have any source
207          to list for this frame.  */
208       if (level >= 0)
209         {
210           ui_out_text (uiout, "#");
211           ui_out_field_fmt_int (uiout, 2, ui_left, "level", level);
212         }
213       if (ui_out_is_mi_like_p (uiout))
214         {
215           annotate_frame_address ();
216           ui_out_field_core_addr (uiout, "addr", fi->pc);
217           annotate_frame_address_end ();
218         }
219       
220       if (get_frame_type (fi) == DUMMY_FRAME)
221         {
222           annotate_function_call ();
223           ui_out_field_string (uiout, "func", "<function called from gdb>");
224         }
225       else if (get_frame_type (fi) == SIGTRAMP_FRAME)
226         {
227           annotate_signal_handler_caller ();
228           ui_out_field_string (uiout, "func", "<signal handler called>");
229         }
230       ui_out_text (uiout, "\n");
231       annotate_frame_end ();
232
233       do_cleanups (uiout_cleanup);
234       return;
235     }
236
237   /* If fi is not the innermost frame, that normally means that fi->pc
238      points to *after* the call instruction, and we want to get the
239      line containing the call, never the next line.  But if the next
240      frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the next frame
241      was not entered as the result of a call, and we want to get the
242      line containing fi->pc.  */
243   find_frame_sal (fi, &sal);
244
245   location_print = (source == LOCATION 
246                     || source == LOC_AND_ADDRESS
247                     || source == SRC_AND_LOC);
248
249   if (location_print || !sal.symtab)
250     print_frame (fi, level, source, args, sal);
251
252   source_print = (source == SRC_LINE || source == SRC_AND_LOC);
253
254   if (sal.symtab)
255     set_current_source_symtab_and_line (&sal);
256
257   if (source_print && sal.symtab)
258     {
259       struct symtab_and_line cursal;
260       int done = 0;
261       int mid_statement = (source == SRC_LINE) && (get_frame_pc (fi) != sal.pc);
262
263       if (annotation_level)
264         done = identify_source_line (sal.symtab, sal.line, mid_statement,
265                                      get_frame_pc (fi));
266       if (!done)
267         {
268           if (print_frame_info_listing_hook)
269             print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
270           else
271             {
272               /* We used to do this earlier, but that is clearly
273                  wrong. This function is used by many different
274                  parts of gdb, including normal_stop in infrun.c,
275                  which uses this to print out the current PC
276                  when we stepi/nexti into the middle of a source
277                  line. Only the command line really wants this
278                  behavior. Other UIs probably would like the
279                  ability to decide for themselves if it is desired. */
280               if (addressprint && mid_statement)
281                 {
282                   ui_out_field_core_addr (uiout, "addr", get_frame_pc (fi));
283                   ui_out_text (uiout, "\t");
284                 }
285
286               print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
287             }
288         }
289       /* Make sure we have at least a default source file */
290       set_default_source_symtab_and_line ();
291       cursal = get_current_source_symtab_and_line ();
292       cursal.line = max (sal.line - get_lines_to_list () / 2, 1);
293       set_current_source_symtab_and_line (&cursal);
294     }
295
296   if (source != 0)
297     set_default_breakpoint (1, get_frame_pc (fi), sal.symtab, sal.line);
298
299   annotate_frame_end ();
300
301   gdb_flush (gdb_stdout);
302 }
303
304 static void
305 print_frame (struct frame_info *fi, 
306              int level, 
307              int source, 
308              int args, 
309              struct symtab_and_line sal)
310 {
311   struct symbol *func;
312   register char *funname = 0;
313   enum language funlang = language_unknown;
314   struct ui_stream *stb;
315   struct cleanup *old_chain;
316   struct cleanup *list_chain;
317
318   stb = ui_out_stream_new (uiout);
319   old_chain = make_cleanup_ui_out_stream_delete (stb);
320
321   func = find_pc_function (frame_address_in_block (fi));
322   if (func)
323     {
324       /* In certain pathological cases, the symtabs give the wrong
325          function (when we are in the first function in a file which
326          is compiled without debugging symbols, the previous function
327          is compiled with debugging symbols, and the "foo.o" symbol
328          that is supposed to tell us where the file with debugging symbols
329          ends has been truncated by ar because it is longer than 15
330          characters).  This also occurs if the user uses asm() to create
331          a function but not stabs for it (in a file compiled -g).
332
333          So look in the minimal symbol tables as well, and if it comes
334          up with a larger address for the function use that instead.
335          I don't think this can ever cause any problems; there shouldn't
336          be any minimal symbols in the middle of a function; if this is
337          ever changed many parts of GDB will need to be changed (and we'll
338          create a find_pc_minimal_function or some such).  */
339
340       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (frame_address_in_block (fi));
341       if (msymbol != NULL
342           && (SYMBOL_VALUE_ADDRESS (msymbol)
343               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
344         {
345 #if 0
346           /* There is no particular reason to think the line number
347              information is wrong.  Someone might have just put in
348              a label with asm() but left the line numbers alone.  */
349           /* In this case we have no way of knowing the source file
350              and line number, so don't print them.  */
351           sal.symtab = 0;
352 #endif
353           /* We also don't know anything about the function besides
354              its address and name.  */
355           func = 0;
356           funname = SYMBOL_NAME (msymbol);
357           funlang = SYMBOL_LANGUAGE (msymbol);
358         }
359       else
360         {
361           /* I'd like to use SYMBOL_SOURCE_NAME() here, to display the
362              demangled name that we already have stored in the symbol
363              table, but we stored a version with DMGL_PARAMS turned
364              on, and here we don't want to display parameters. So call
365              the demangler again, with DMGL_ANSI only. (Yes, I know
366              that printf_symbol_filtered() will again try to demangle
367              the name on the fly, but the issue is that if
368              cplus_demangle() fails here, it'll fail there too. So we
369              want to catch the failure ("demangled==NULL" case below)
370              here, while we still have our hands on the function
371              symbol.) */
372           char *demangled;
373           funname = SYMBOL_NAME (func);
374           funlang = SYMBOL_LANGUAGE (func);
375           if (funlang == language_cplus)
376             {
377               demangled = cplus_demangle (funname, DMGL_ANSI);
378               if (demangled == NULL)
379                 /* If the demangler fails, try the demangled name from
380                    the symbol table. This'll have parameters, but
381                    that's preferable to diplaying a mangled name. */
382                 funname = SYMBOL_SOURCE_NAME (func);
383             }
384         }
385     }
386   else
387     {
388       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (frame_address_in_block (fi));
389       if (msymbol != NULL)
390         {
391           funname = SYMBOL_NAME (msymbol);
392           funlang = SYMBOL_LANGUAGE (msymbol);
393         }
394     }
395
396   annotate_frame_begin (level == -1 ? 0 : level, get_frame_pc (fi));
397
398   list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
399
400   if (level >= 0)
401     {
402       ui_out_text (uiout, "#");
403       ui_out_field_fmt_int (uiout, 2, ui_left, "level", level);
404     }
405   if (addressprint)
406     if (get_frame_pc (fi) != sal.pc
407         || !sal.symtab
408         || source == LOC_AND_ADDRESS)
409       {
410         annotate_frame_address ();
411         ui_out_field_core_addr (uiout, "addr", get_frame_pc (fi));
412         annotate_frame_address_end ();
413         ui_out_text (uiout, " in ");
414       }
415   annotate_frame_function_name ();
416   fprintf_symbol_filtered (stb->stream, funname ? funname : "??", funlang,
417                            DMGL_ANSI);
418   ui_out_field_stream (uiout, "func", stb);
419   ui_out_wrap_hint (uiout, "   ");
420   annotate_frame_args ();
421       
422   ui_out_text (uiout, " (");
423   if (args)
424     {
425       struct print_args_args args;
426       struct cleanup *args_list_chain;
427       args.fi = fi;
428       args.func = func;
429       args.stream = gdb_stdout;
430       args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
431       catch_errors (print_args_stub, &args, "", RETURN_MASK_ALL);
432       /* FIXME: args must be a list. If one argument is a string it will
433                  have " that will not be properly escaped.  */
434       /* Invoke ui_out_tuple_end.  */
435       do_cleanups (args_list_chain);
436       QUIT;
437     }
438   ui_out_text (uiout, ")");
439   if (sal.symtab && sal.symtab->filename)
440     {
441       annotate_frame_source_begin ();
442       ui_out_wrap_hint (uiout, "   ");
443       ui_out_text (uiout, " at ");
444       annotate_frame_source_file ();
445       ui_out_field_string (uiout, "file", sal.symtab->filename);
446       annotate_frame_source_file_end ();
447       ui_out_text (uiout, ":");
448       annotate_frame_source_line ();
449       ui_out_field_int (uiout, "line", sal.line);
450       annotate_frame_source_end ();
451     }
452
453 #ifdef PC_SOLIB
454   if (!funname || (!sal.symtab || !sal.symtab->filename))
455     {
456       char *lib = PC_SOLIB (get_frame_pc (fi));
457       if (lib)
458         {
459           annotate_frame_where ();
460           ui_out_wrap_hint (uiout, "  ");
461           ui_out_text (uiout, " from ");
462           ui_out_field_string (uiout, "from", lib);
463         }
464     }
465 #endif /* PC_SOLIB */
466
467   /* do_cleanups will call ui_out_tuple_end() for us.  */
468   do_cleanups (list_chain);
469   ui_out_text (uiout, "\n");
470   do_cleanups (old_chain);
471 }
472 \f
473 /* Show the frame info.  If this is the tui, it will be shown in 
474    the source display otherwise, nothing is done */
475 void
476 show_stack_frame (struct frame_info *fi)
477 {
478 }
479 \f
480
481 /* Read a frame specification in whatever the appropriate format is.
482    Call error() if the specification is in any way invalid (i.e.
483    this function never returns NULL).  */
484
485 struct frame_info *
486 parse_frame_specification (char *frame_exp)
487 {
488   int numargs = 0;
489 #define MAXARGS 4
490   CORE_ADDR args[MAXARGS];
491   int level;
492
493   if (frame_exp)
494     {
495       char *addr_string, *p;
496       struct cleanup *tmp_cleanup;
497
498       while (*frame_exp == ' ')
499         frame_exp++;
500
501       while (*frame_exp)
502         {
503           if (numargs > MAXARGS)
504             error ("Too many args in frame specification");
505           /* Parse an argument.  */
506           for (p = frame_exp; *p && *p != ' '; p++)
507             ;
508           addr_string = savestring (frame_exp, p - frame_exp);
509
510           {
511             struct value *vp;
512
513             tmp_cleanup = make_cleanup (xfree, addr_string);
514
515             /* NOTE: we call parse_and_eval and then both
516                value_as_long and value_as_address rather than calling
517                parse_and_eval_long and parse_and_eval_address because
518                of the issue of potential side effects from evaluating
519                the expression.  */
520             vp = parse_and_eval (addr_string);
521             if (numargs == 0)
522               level = value_as_long (vp);
523
524             args[numargs++] = value_as_address (vp);
525             do_cleanups (tmp_cleanup);
526           }
527
528           /* Skip spaces, move to possible next arg.  */
529           while (*p == ' ')
530             p++;
531           frame_exp = p;
532         }
533     }
534
535   switch (numargs)
536     {
537     case 0:
538       if (deprecated_selected_frame == NULL)
539         error ("No selected frame.");
540       return deprecated_selected_frame;
541       /* NOTREACHED */
542     case 1:
543       {
544         struct frame_info *fid =
545         find_relative_frame (get_current_frame (), &level);
546         struct frame_info *tfid;
547
548         if (level == 0)
549           /* find_relative_frame was successful */
550           return fid;
551
552         /* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
553            take at least 2 addresses.  It is important to detect this case
554            here so that "frame 100" does not give a confusing error message
555            like "frame specification requires two addresses".  This of course
556            does not solve the "frame 100" problem for machines on which
557            a frame specification can be made with one address.  To solve
558            that, we need a new syntax for a specifying a frame by address.
559            I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
560            two args, etc.), but people might think that is too much typing,
561            so I guess *0x23,0x45 would be a possible alternative (commas
562            really should be used instead of spaces to delimit; using spaces
563            normally works in an expression).  */
564 #ifdef SETUP_ARBITRARY_FRAME
565         error ("No frame %s", paddr_d (args[0]));
566 #endif
567
568         /* If (s)he specifies the frame with an address, he deserves what
569            (s)he gets.  Still, give the highest one that matches.  */
570
571         for (fid = get_current_frame ();
572              fid && get_frame_base (fid) != args[0];
573              fid = get_prev_frame (fid))
574           ;
575
576         if (fid)
577           while ((tfid = get_prev_frame (fid)) &&
578                  (get_frame_base (tfid) == args[0]))
579             fid = tfid;
580
581         /* We couldn't identify the frame as an existing frame, but
582            perhaps we can create one with a single argument.  */
583       }
584
585     default:
586 #ifdef SETUP_ARBITRARY_FRAME
587       return SETUP_ARBITRARY_FRAME (numargs, args);
588 #else
589       /* Usual case.  Do it here rather than have everyone supply
590          a SETUP_ARBITRARY_FRAME that does this.  */
591       if (numargs == 1)
592         return create_new_frame (args[0], 0);
593       error ("Too many args in frame specification");
594 #endif
595       /* NOTREACHED */
596     }
597   /* NOTREACHED */
598 }
599
600 /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
601    that if it is unsure about the answer, it returns 0
602    instead of guessing (this happens on the VAX and i960, for example).
603
604    On most machines, we never have to guess about the args address,
605    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
606 #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
607 #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
608 #endif
609
610 /* Print verbosely the selected frame or the frame at address ADDR.
611    This means absolutely all information in the frame is printed.  */
612
613 static void
614 frame_info (char *addr_exp, int from_tty)
615 {
616   struct frame_info *fi;
617   struct symtab_and_line sal;
618   struct symbol *func;
619   struct symtab *s;
620   struct frame_info *calling_frame_info;
621   int i, count, numregs;
622   char *funname = 0;
623   enum language funlang = language_unknown;
624
625   if (!target_has_stack)
626     error ("No stack.");
627
628   fi = parse_frame_specification (addr_exp);
629   if (fi == NULL)
630     error ("Invalid frame specified.");
631
632   find_frame_sal (fi, &sal);
633   func = get_frame_function (fi);
634   /* FIXME: cagney/2002-11-28: Why bother?  Won't sal.symtab contain
635      the same value.  */
636   s = find_pc_symtab (get_frame_pc (fi));
637   if (func)
638     {
639       /* I'd like to use SYMBOL_SOURCE_NAME() here, to display
640        * the demangled name that we already have stored in
641        * the symbol table, but we stored a version with
642        * DMGL_PARAMS turned on, and here we don't want
643        * to display parameters. So call the demangler again,
644        * with DMGL_ANSI only. RT
645        * (Yes, I know that printf_symbol_filtered() will
646        * again try to demangle the name on the fly, but
647        * the issue is that if cplus_demangle() fails here,
648        * it'll fail there too. So we want to catch the failure
649        * ("demangled==NULL" case below) here, while we still
650        * have our hands on the function symbol.)
651        */
652       char *demangled;
653       funname = SYMBOL_NAME (func);
654       funlang = SYMBOL_LANGUAGE (func);
655       if (funlang == language_cplus)
656         {
657           demangled = cplus_demangle (funname, DMGL_ANSI);
658           /* If the demangler fails, try the demangled name
659            * from the symbol table. This'll have parameters,
660            * but that's preferable to diplaying a mangled name.
661            */
662           if (demangled == NULL)
663             funname = SYMBOL_SOURCE_NAME (func);
664         }
665     }
666   else
667     {
668       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
669       if (msymbol != NULL)
670         {
671           funname = SYMBOL_NAME (msymbol);
672           funlang = SYMBOL_LANGUAGE (msymbol);
673         }
674     }
675   calling_frame_info = get_prev_frame (fi);
676
677   if (!addr_exp && frame_relative_level (deprecated_selected_frame) >= 0)
678     {
679       printf_filtered ("Stack level %d, frame at ",
680                        frame_relative_level (deprecated_selected_frame));
681       print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
682       printf_filtered (":\n");
683     }
684   else
685     {
686       printf_filtered ("Stack frame at ");
687       print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
688       printf_filtered (":\n");
689     }
690   printf_filtered (" %s = ", REGISTER_NAME (PC_REGNUM));
691   print_address_numeric (get_frame_pc (fi), 1, gdb_stdout);
692
693   wrap_here ("   ");
694   if (funname)
695     {
696       printf_filtered (" in ");
697       fprintf_symbol_filtered (gdb_stdout, funname, funlang,
698                                DMGL_ANSI | DMGL_PARAMS);
699     }
700   wrap_here ("   ");
701   if (sal.symtab)
702     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
703   puts_filtered ("; ");
704   wrap_here ("    ");
705   printf_filtered ("saved %s ", REGISTER_NAME (PC_REGNUM));
706   print_address_numeric (frame_pc_unwind (fi), 1, gdb_stdout);
707   printf_filtered ("\n");
708
709   {
710     int frameless;
711     frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
712     if (frameless)
713       printf_filtered (" (FRAMELESS),");
714   }
715
716   if (calling_frame_info)
717     {
718       printf_filtered (" called by frame at ");
719       print_address_numeric (get_frame_base (calling_frame_info),
720                              1, gdb_stdout);
721     }
722   if (get_next_frame (fi) && calling_frame_info)
723     puts_filtered (",");
724   wrap_here ("   ");
725   if (get_next_frame (fi))
726     {
727       printf_filtered (" caller of frame at ");
728       print_address_numeric (get_frame_base (get_next_frame (fi)), 1,
729                              gdb_stdout);
730     }
731   if (get_next_frame (fi) || calling_frame_info)
732     puts_filtered ("\n");
733   if (s)
734     printf_filtered (" source language %s.\n",
735                      language_str (s->language));
736
737 #ifdef PRINT_EXTRA_FRAME_INFO
738   PRINT_EXTRA_FRAME_INFO (fi);
739 #endif
740
741   {
742     /* Address of the argument list for this frame, or 0.  */
743     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
744     /* Number of args for this frame, or -1 if unknown.  */
745     int numargs;
746
747     if (arg_list == 0)
748       printf_filtered (" Arglist at unknown address.\n");
749     else
750       {
751         printf_filtered (" Arglist at ");
752         print_address_numeric (arg_list, 1, gdb_stdout);
753         printf_filtered (",");
754
755         numargs = FRAME_NUM_ARGS (fi);
756         if (numargs < 0)
757           puts_filtered (" args: ");
758         else if (numargs == 0)
759           puts_filtered (" no args.");
760         else if (numargs == 1)
761           puts_filtered (" 1 arg: ");
762         else
763           printf_filtered (" %d args: ", numargs);
764         print_frame_args (func, fi, numargs, gdb_stdout);
765         puts_filtered ("\n");
766       }
767   }
768   {
769     /* Address of the local variables for this frame, or 0.  */
770     CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
771
772     if (arg_list == 0)
773       printf_filtered (" Locals at unknown address,");
774     else
775       {
776         printf_filtered (" Locals at ");
777         print_address_numeric (arg_list, 1, gdb_stdout);
778         printf_filtered (",");
779       }
780   }
781
782   if (FRAME_INIT_SAVED_REGS_P ()
783       && get_frame_saved_regs (fi) == NULL)
784     FRAME_INIT_SAVED_REGS (fi);
785   /* Print as much information as possible on the location of all the
786      registers.  */
787   {
788     enum lval_type lval;
789     int optimized;
790     CORE_ADDR addr;
791     int realnum;
792     int count;
793     int i;
794     int need_nl = 1;
795
796     /* The sp is special; what's displayed isn't the save address, but
797        the value of the previous frame's sp.  This is a legacy thing,
798        at one stage the frame cached the previous frame's SP instead
799        of its address, hence it was easiest to just display the cached
800        value.  */
801     if (SP_REGNUM >= 0)
802       {
803         /* Find out the location of the saved stack pointer with out
804            actually evaluating it.  */
805         frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
806                                &realnum, NULL);
807         if (!optimized && lval == not_lval)
808           {
809             void *value = alloca (MAX_REGISTER_RAW_SIZE);
810             CORE_ADDR sp;
811             frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
812                                    &realnum, value);
813             sp = extract_address (value, REGISTER_RAW_SIZE (SP_REGNUM));
814             printf_filtered (" Previous frame's sp is ");
815             print_address_numeric (sp, 1, gdb_stdout);
816             printf_filtered ("\n");
817             need_nl = 0;
818           }
819         else if (!optimized && lval == lval_memory)
820           {
821             printf_filtered (" Previous frame's sp at ");
822             print_address_numeric (addr, 1, gdb_stdout);
823             printf_filtered ("\n");
824             need_nl = 0;
825           }
826         else if (!optimized && lval == lval_register)
827           {
828             printf_filtered (" Previous frame's sp in %s\n",
829                              REGISTER_NAME (realnum));
830             need_nl = 0;
831           }
832         /* else keep quiet.  */
833       }
834
835     count = 0;
836     numregs = NUM_REGS + NUM_PSEUDO_REGS;
837     for (i = 0; i < numregs; i++)
838       if (i != SP_REGNUM)
839         {
840           /* Find out the location of the saved register without
841              fetching the corresponding value.  */
842           frame_register_unwind (fi, i, &optimized, &lval, &addr, &realnum,
843                                  NULL);
844           /* For moment, only display registers that were saved on the
845              stack.  */
846           if (!optimized && lval == lval_memory)
847             {
848               if (count == 0)
849                 puts_filtered (" Saved registers:\n ");
850               else
851                 puts_filtered (",");
852               wrap_here (" ");
853               printf_filtered (" %s at ", REGISTER_NAME (i));
854               print_address_numeric (addr, 1, gdb_stdout);
855               count++;
856             }
857         }
858     if (count || need_nl)
859       puts_filtered ("\n");
860   }
861 }
862
863 #if 0
864 /* Set a limit on the number of frames printed by default in a
865    backtrace.  */
866
867 static int backtrace_limit;
868
869 static void
870 set_backtrace_limit_command (char *count_exp, int from_tty)
871 {
872   int count = parse_and_eval_long (count_exp);
873
874   if (count < 0)
875     error ("Negative argument not meaningful as backtrace limit.");
876
877   backtrace_limit = count;
878 }
879
880 static void
881 backtrace_limit_info (char *arg, int from_tty)
882 {
883   if (arg)
884     error ("\"Info backtrace-limit\" takes no arguments.");
885
886   printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
887 }
888 #endif
889
890 /* Print briefly all stack frames or just the innermost COUNT frames.  */
891
892 static void backtrace_command_1 (char *count_exp, int show_locals,
893                                  int from_tty);
894 static void
895 backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
896 {
897   struct frame_info *fi;
898   register int count;
899   register int i;
900   register struct frame_info *trailing;
901   register int trailing_level;
902
903   if (!target_has_stack)
904     error ("No stack.");
905
906   /* The following code must do two things.  First, it must
907      set the variable TRAILING to the frame from which we should start
908      printing.  Second, it must set the variable count to the number
909      of frames which we should print, or -1 if all of them.  */
910   trailing = get_current_frame ();
911
912   /* The target can be in a state where there is no valid frames
913      (e.g., just connected). */
914   if (trailing == NULL)
915     error ("No stack.");
916
917   trailing_level = 0;
918   if (count_exp)
919     {
920       count = parse_and_eval_long (count_exp);
921       if (count < 0)
922         {
923           struct frame_info *current;
924
925           count = -count;
926
927           current = trailing;
928           while (current && count--)
929             {
930               QUIT;
931               current = get_prev_frame (current);
932             }
933
934           /* Will stop when CURRENT reaches the top of the stack.  TRAILING
935              will be COUNT below it.  */
936           while (current)
937             {
938               QUIT;
939               trailing = get_prev_frame (trailing);
940               current = get_prev_frame (current);
941               trailing_level++;
942             }
943
944           count = -1;
945         }
946     }
947   else
948     count = -1;
949
950   if (info_verbose)
951     {
952       struct partial_symtab *ps;
953
954       /* Read in symbols for all of the frames.  Need to do this in
955          a separate pass so that "Reading in symbols for xxx" messages
956          don't screw up the appearance of the backtrace.  Also
957          if people have strong opinions against reading symbols for
958          backtrace this may have to be an option.  */
959       i = count;
960       for (fi = trailing;
961            fi != NULL && i--;
962            fi = get_prev_frame (fi))
963         {
964           QUIT;
965           ps = find_pc_psymtab (frame_address_in_block (fi));
966           if (ps)
967             PSYMTAB_TO_SYMTAB (ps);     /* Force syms to come in */
968         }
969     }
970
971   for (i = 0, fi = trailing;
972        fi && count--;
973        i++, fi = get_prev_frame (fi))
974     {
975       QUIT;
976
977       /* Don't use print_stack_frame; if an error() occurs it probably
978          means further attempts to backtrace would fail (on the other
979          hand, perhaps the code does or could be fixed to make sure
980          the frame->prev field gets set to NULL in that case).  */
981       print_frame_info (fi, trailing_level + i, 0, 1);
982       if (show_locals)
983         print_frame_local_vars (fi, 1, gdb_stdout);
984     }
985
986   /* If we've stopped before the end, mention that.  */
987   if (fi && from_tty)
988     printf_filtered ("(More stack frames follow...)\n");
989 }
990
991 static void
992 backtrace_command (char *arg, int from_tty)
993 {
994   struct cleanup *old_chain = (struct cleanup *) NULL;
995   char **argv = (char **) NULL;
996   int argIndicatingFullTrace = (-1), totArgLen = 0, argc = 0;
997   char *argPtr = arg;
998
999   if (arg != (char *) NULL)
1000     {
1001       int i;
1002
1003       argv = buildargv (arg);
1004       old_chain = make_cleanup_freeargv (argv);
1005       argc = 0;
1006       for (i = 0; (argv[i] != (char *) NULL); i++)
1007         {
1008           unsigned int j;
1009
1010           for (j = 0; (j < strlen (argv[i])); j++)
1011             argv[i][j] = tolower (argv[i][j]);
1012
1013           if (argIndicatingFullTrace < 0 && subset_compare (argv[i], "full"))
1014             argIndicatingFullTrace = argc;
1015           else
1016             {
1017               argc++;
1018               totArgLen += strlen (argv[i]);
1019             }
1020         }
1021       totArgLen += argc;
1022       if (argIndicatingFullTrace >= 0)
1023         {
1024           if (totArgLen > 0)
1025             {
1026               argPtr = (char *) xmalloc (totArgLen + 1);
1027               if (!argPtr)
1028                 nomem (0);
1029               else
1030                 {
1031                   memset (argPtr, 0, totArgLen + 1);
1032                   for (i = 0; (i < (argc + 1)); i++)
1033                     {
1034                       if (i != argIndicatingFullTrace)
1035                         {
1036                           strcat (argPtr, argv[i]);
1037                           strcat (argPtr, " ");
1038                         }
1039                     }
1040                 }
1041             }
1042           else
1043             argPtr = (char *) NULL;
1044         }
1045     }
1046
1047   backtrace_command_1 (argPtr, (argIndicatingFullTrace >= 0), from_tty);
1048
1049   if (argIndicatingFullTrace >= 0 && totArgLen > 0)
1050     xfree (argPtr);
1051
1052   if (old_chain)
1053     do_cleanups (old_chain);
1054 }
1055
1056 static void backtrace_full_command (char *arg, int from_tty);
1057 static void
1058 backtrace_full_command (char *arg, int from_tty)
1059 {
1060   backtrace_command_1 (arg, 1, from_tty);
1061 }
1062 \f
1063
1064 /* Print the local variables of a block B active in FRAME.
1065    Return 1 if any variables were printed; 0 otherwise.  */
1066
1067 static int
1068 print_block_frame_locals (struct block *b, register struct frame_info *fi,
1069                           int num_tabs, register struct ui_file *stream)
1070 {
1071   register int i, j;
1072   register struct symbol *sym;
1073   register int values_printed = 0;
1074
1075   ALL_BLOCK_SYMBOLS (b, i, sym)
1076     {
1077       switch (SYMBOL_CLASS (sym))
1078         {
1079         case LOC_LOCAL:
1080         case LOC_REGISTER:
1081         case LOC_STATIC:
1082         case LOC_BASEREG:
1083           values_printed = 1;
1084           for (j = 0; j < num_tabs; j++)
1085             fputs_filtered ("\t", stream);
1086           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1087           fputs_filtered (" = ", stream);
1088           print_variable_value (sym, fi, stream);
1089           fprintf_filtered (stream, "\n");
1090           break;
1091
1092         default:
1093           /* Ignore symbols which are not locals.  */
1094           break;
1095         }
1096     }
1097   return values_printed;
1098 }
1099
1100 /* Same, but print labels.  */
1101
1102 static int
1103 print_block_frame_labels (struct block *b, int *have_default,
1104                           register struct ui_file *stream)
1105 {
1106   register int i;
1107   register struct symbol *sym;
1108   register int values_printed = 0;
1109
1110   ALL_BLOCK_SYMBOLS (b, i, sym)
1111     {
1112       if (STREQ (SYMBOL_NAME (sym), "default"))
1113         {
1114           if (*have_default)
1115             continue;
1116           *have_default = 1;
1117         }
1118       if (SYMBOL_CLASS (sym) == LOC_LABEL)
1119         {
1120           struct symtab_and_line sal;
1121           sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1122           values_printed = 1;
1123           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1124           if (addressprint)
1125             {
1126               fprintf_filtered (stream, " ");
1127               print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
1128             }
1129           fprintf_filtered (stream, " in file %s, line %d\n",
1130                             sal.symtab->filename, sal.line);
1131         }
1132     }
1133   return values_printed;
1134 }
1135
1136 /* Print on STREAM all the local variables in frame FRAME,
1137    including all the blocks active in that frame
1138    at its current pc.
1139
1140    Returns 1 if the job was done,
1141    or 0 if nothing was printed because we have no info
1142    on the function running in FRAME.  */
1143
1144 static void
1145 print_frame_local_vars (register struct frame_info *fi, register int num_tabs,
1146                         register struct ui_file *stream)
1147 {
1148   register struct block *block = get_frame_block (fi, 0);
1149   register int values_printed = 0;
1150
1151   if (block == 0)
1152     {
1153       fprintf_filtered (stream, "No symbol table info available.\n");
1154       return;
1155     }
1156
1157   while (block != 0)
1158     {
1159       if (print_block_frame_locals (block, fi, num_tabs, stream))
1160         values_printed = 1;
1161       /* After handling the function's top-level block, stop.
1162          Don't continue to its superblock, the block of
1163          per-file symbols.  */
1164       if (BLOCK_FUNCTION (block))
1165         break;
1166       block = BLOCK_SUPERBLOCK (block);
1167     }
1168
1169   if (!values_printed)
1170     {
1171       fprintf_filtered (stream, "No locals.\n");
1172     }
1173 }
1174
1175 /* Same, but print labels.  */
1176
1177 static void
1178 print_frame_label_vars (register struct frame_info *fi, int this_level_only,
1179                         register struct ui_file *stream)
1180 {
1181   register struct blockvector *bl;
1182   register struct block *block = get_frame_block (fi, 0);
1183   register int values_printed = 0;
1184   int index, have_default = 0;
1185   char *blocks_printed;
1186   CORE_ADDR pc = get_frame_pc (fi);
1187
1188   if (block == 0)
1189     {
1190       fprintf_filtered (stream, "No symbol table info available.\n");
1191       return;
1192     }
1193
1194   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1195   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1196   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1197
1198   while (block != 0)
1199     {
1200       CORE_ADDR end = BLOCK_END (block) - 4;
1201       int last_index;
1202
1203       if (bl != blockvector_for_pc (end, &index))
1204         error ("blockvector blotch");
1205       if (BLOCKVECTOR_BLOCK (bl, index) != block)
1206         error ("blockvector botch");
1207       last_index = BLOCKVECTOR_NBLOCKS (bl);
1208       index += 1;
1209
1210       /* Don't print out blocks that have gone by.  */
1211       while (index < last_index
1212              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1213         index++;
1214
1215       while (index < last_index
1216              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1217         {
1218           if (blocks_printed[index] == 0)
1219             {
1220               if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
1221                 values_printed = 1;
1222               blocks_printed[index] = 1;
1223             }
1224           index++;
1225         }
1226       if (have_default)
1227         return;
1228       if (values_printed && this_level_only)
1229         return;
1230
1231       /* After handling the function's top-level block, stop.
1232          Don't continue to its superblock, the block of
1233          per-file symbols.  */
1234       if (BLOCK_FUNCTION (block))
1235         break;
1236       block = BLOCK_SUPERBLOCK (block);
1237     }
1238
1239   if (!values_printed && !this_level_only)
1240     {
1241       fprintf_filtered (stream, "No catches.\n");
1242     }
1243 }
1244
1245 /* ARGSUSED */
1246 void
1247 locals_info (char *args, int from_tty)
1248 {
1249   if (!deprecated_selected_frame)
1250     error ("No frame selected.");
1251   print_frame_local_vars (deprecated_selected_frame, 0, gdb_stdout);
1252 }
1253
1254 static void
1255 catch_info (char *ignore, int from_tty)
1256 {
1257   struct symtab_and_line *sal;
1258
1259   /* Check for target support for exception handling */
1260   sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
1261   if (sal)
1262     {
1263       /* Currently not handling this */
1264       /* Ideally, here we should interact with the C++ runtime
1265          system to find the list of active handlers, etc. */
1266       fprintf_filtered (gdb_stdout, "Info catch not supported with this target/compiler combination.\n");
1267 #if 0
1268       if (!deprecated_selected_frame)
1269         error ("No frame selected.");
1270 #endif
1271     }
1272   else
1273     {
1274       /* Assume g++ compiled code -- old v 4.16 behaviour */
1275       if (!deprecated_selected_frame)
1276         error ("No frame selected.");
1277
1278       print_frame_label_vars (deprecated_selected_frame, 0, gdb_stdout);
1279     }
1280 }
1281
1282 static void
1283 print_frame_arg_vars (register struct frame_info *fi,
1284                       register struct ui_file *stream)
1285 {
1286   struct symbol *func = get_frame_function (fi);
1287   register struct block *b;
1288   register int i;
1289   register struct symbol *sym, *sym2;
1290   register int values_printed = 0;
1291
1292   if (func == 0)
1293     {
1294       fprintf_filtered (stream, "No symbol table info available.\n");
1295       return;
1296     }
1297
1298   b = SYMBOL_BLOCK_VALUE (func);
1299   ALL_BLOCK_SYMBOLS (b, i, sym)
1300     {
1301       switch (SYMBOL_CLASS (sym))
1302         {
1303         case LOC_ARG:
1304         case LOC_LOCAL_ARG:
1305         case LOC_REF_ARG:
1306         case LOC_REGPARM:
1307         case LOC_REGPARM_ADDR:
1308         case LOC_BASEREG_ARG:
1309           values_printed = 1;
1310           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1311           fputs_filtered (" = ", stream);
1312
1313           /* We have to look up the symbol because arguments can have
1314              two entries (one a parameter, one a local) and the one we
1315              want is the local, which lookup_symbol will find for us.
1316              This includes gcc1 (not gcc2) on the sparc when passing a
1317              small structure and gcc2 when the argument type is float
1318              and it is passed as a double and converted to float by
1319              the prologue (in the latter case the type of the LOC_ARG
1320              symbol is double and the type of the LOC_LOCAL symbol is
1321              float).  There are also LOC_ARG/LOC_REGISTER pairs which
1322              are not combined in symbol-reading.  */
1323
1324           sym2 = lookup_symbol (SYMBOL_NAME (sym),
1325                    b, VAR_NAMESPACE, (int *) NULL, (struct symtab **) NULL);
1326           print_variable_value (sym2, fi, stream);
1327           fprintf_filtered (stream, "\n");
1328           break;
1329
1330         default:
1331           /* Don't worry about things which aren't arguments.  */
1332           break;
1333         }
1334     }
1335   if (!values_printed)
1336     {
1337       fprintf_filtered (stream, "No arguments.\n");
1338     }
1339 }
1340
1341 void
1342 args_info (char *ignore, int from_tty)
1343 {
1344   if (!deprecated_selected_frame)
1345     error ("No frame selected.");
1346   print_frame_arg_vars (deprecated_selected_frame, gdb_stdout);
1347 }
1348
1349
1350 static void
1351 args_plus_locals_info (char *ignore, int from_tty)
1352 {
1353   args_info (ignore, from_tty);
1354   locals_info (ignore, from_tty);
1355 }
1356 \f
1357
1358 /* Select frame FI.  Also print the stack frame and show the source if
1359    this is the tui version.  */
1360 static void
1361 select_and_print_frame (struct frame_info *fi)
1362 {
1363   select_frame (fi);
1364   if (fi)
1365     {
1366       print_stack_frame (fi, frame_relative_level (fi), 1);
1367     }
1368 }
1369 \f
1370 /* Return the symbol-block in which the selected frame is executing.
1371    Can return zero under various legitimate circumstances.
1372
1373    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
1374    code address within the block returned.  We use this to decide
1375    which macros are in scope.  */
1376
1377 struct block *
1378 get_selected_block (CORE_ADDR *addr_in_block)
1379 {
1380   if (!target_has_stack)
1381     return 0;
1382
1383   /* NOTE: cagney/2002-11-28: Why go to all this effort to not create
1384      a selected/current frame?  Perhaphs this function is called,
1385      indirectly, by WFI in "infrun.c" where avoiding the creation of
1386      an inner most frame is very important (it slows down single
1387      step).  I suspect, though that this was true in the deep dark
1388      past but is no longer the case.  A mindless look at all the
1389      callers tends to support this theory.  I think we should be able
1390      to assume that there is always a selcted frame.  */
1391   /* gdb_assert (deprecated_selected_frame != NULL); So, do you feel
1392      lucky? */
1393   if (!deprecated_selected_frame)
1394     {
1395       CORE_ADDR pc = read_pc ();
1396       if (addr_in_block != NULL)
1397         *addr_in_block = pc;
1398       return block_for_pc (pc);
1399     }
1400   return get_frame_block (deprecated_selected_frame, addr_in_block);
1401 }
1402
1403 /* Find a frame a certain number of levels away from FRAME.
1404    LEVEL_OFFSET_PTR points to an int containing the number of levels.
1405    Positive means go to earlier frames (up); negative, the reverse.
1406    The int that contains the number of levels is counted toward
1407    zero as the frames for those levels are found.
1408    If the top or bottom frame is reached, that frame is returned,
1409    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1410    how much farther the original request asked to go.  */
1411
1412 struct frame_info *
1413 find_relative_frame (register struct frame_info *frame,
1414                      register int *level_offset_ptr)
1415 {
1416   register struct frame_info *prev;
1417   register struct frame_info *frame1;
1418
1419   /* Going up is simple: just do get_prev_frame enough times
1420      or until initial frame is reached.  */
1421   while (*level_offset_ptr > 0)
1422     {
1423       prev = get_prev_frame (frame);
1424       if (prev == 0)
1425         break;
1426       (*level_offset_ptr)--;
1427       frame = prev;
1428     }
1429   /* Going down is just as simple.  */
1430   if (*level_offset_ptr < 0)
1431     {
1432       while (*level_offset_ptr < 0)
1433         {
1434           frame1 = get_next_frame (frame);
1435           if (!frame1)
1436             break;
1437           frame = frame1;
1438           (*level_offset_ptr)++;
1439         }
1440     }
1441   return frame;
1442 }
1443
1444 /* The "select_frame" command.  With no arg, NOP.
1445    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1446    valid level.  Otherwise, treat level_exp as an address expression
1447    and select it.  See parse_frame_specification for more info on proper
1448    frame expressions. */
1449
1450 /* ARGSUSED */
1451 void
1452 select_frame_command_wrapper (char *level_exp, int from_tty)
1453 {
1454   select_frame_command (level_exp, from_tty);
1455 }
1456
1457 static void
1458 select_frame_command (char *level_exp, int from_tty)
1459 {
1460   struct frame_info *frame;
1461   int level = frame_relative_level (deprecated_selected_frame);
1462
1463   if (!target_has_stack)
1464     error ("No stack.");
1465
1466   frame = parse_frame_specification (level_exp);
1467
1468   select_frame (frame);
1469   if (level != frame_relative_level (deprecated_selected_frame))
1470     selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1471 }
1472
1473 /* The "frame" command.  With no arg, print selected frame briefly.
1474    With arg, behaves like select_frame and then prints the selected
1475    frame.  */
1476
1477 void
1478 frame_command (char *level_exp, int from_tty)
1479 {
1480   select_frame_command (level_exp, from_tty);
1481   print_stack_frame (deprecated_selected_frame,
1482                      frame_relative_level (deprecated_selected_frame), 1);
1483 }
1484
1485 /* The XDB Compatibility command to print the current frame. */
1486
1487 static void
1488 current_frame_command (char *level_exp, int from_tty)
1489 {
1490   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1491     error ("No stack.");
1492   print_stack_frame (deprecated_selected_frame,
1493                           frame_relative_level (deprecated_selected_frame), 1);
1494 }
1495
1496 /* Select the frame up one or COUNT stack levels
1497    from the previously selected frame, and print it briefly.  */
1498
1499 /* ARGSUSED */
1500 static void
1501 up_silently_base (char *count_exp)
1502 {
1503   register struct frame_info *fi;
1504   int count = 1, count1;
1505   if (count_exp)
1506     count = parse_and_eval_long (count_exp);
1507   count1 = count;
1508
1509   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1510     error ("No stack.");
1511
1512   fi = find_relative_frame (deprecated_selected_frame, &count1);
1513   if (count1 != 0 && count_exp == 0)
1514     error ("Initial frame selected; you cannot go up.");
1515   select_frame (fi);
1516   selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1517 }
1518
1519 static void
1520 up_silently_command (char *count_exp, int from_tty)
1521 {
1522   up_silently_base (count_exp);
1523 }
1524
1525 static void
1526 up_command (char *count_exp, int from_tty)
1527 {
1528   up_silently_base (count_exp);
1529   print_stack_frame (deprecated_selected_frame,
1530                      frame_relative_level (deprecated_selected_frame), 1);
1531 }
1532
1533 /* Select the frame down one or COUNT stack levels
1534    from the previously selected frame, and print it briefly.  */
1535
1536 /* ARGSUSED */
1537 static void
1538 down_silently_base (char *count_exp)
1539 {
1540   register struct frame_info *frame;
1541   int count = -1, count1;
1542   if (count_exp)
1543     count = -parse_and_eval_long (count_exp);
1544   count1 = count;
1545
1546   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1547     error ("No stack.");
1548
1549   frame = find_relative_frame (deprecated_selected_frame, &count1);
1550   if (count1 != 0 && count_exp == 0)
1551     {
1552
1553       /* We only do this if count_exp is not specified.  That way "down"
1554          means to really go down (and let me know if that is
1555          impossible), but "down 9999" can be used to mean go all the way
1556          down without getting an error.  */
1557
1558       error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1559     }
1560
1561   select_frame (frame);
1562   selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1563 }
1564
1565 /* ARGSUSED */
1566 static void
1567 down_silently_command (char *count_exp, int from_tty)
1568 {
1569   down_silently_base (count_exp);
1570 }
1571
1572 static void
1573 down_command (char *count_exp, int from_tty)
1574 {
1575   down_silently_base (count_exp);
1576   print_stack_frame (deprecated_selected_frame,
1577                      frame_relative_level (deprecated_selected_frame), 1);
1578 }
1579 \f
1580 void
1581 return_command (char *retval_exp, int from_tty)
1582 {
1583   struct symbol *thisfun;
1584   CORE_ADDR selected_frame_addr;
1585   CORE_ADDR selected_frame_pc;
1586   struct frame_info *frame;
1587   struct value *return_value = NULL;
1588
1589   if (deprecated_selected_frame == NULL)
1590     error ("No selected frame.");
1591   thisfun = get_frame_function (deprecated_selected_frame);
1592   selected_frame_addr = get_frame_base (deprecated_selected_frame);
1593   selected_frame_pc = get_frame_pc (deprecated_selected_frame);
1594
1595   /* Compute the return value (if any -- possibly getting errors here).  */
1596
1597   if (retval_exp)
1598     {
1599       struct type *return_type = NULL;
1600
1601       return_value = parse_and_eval (retval_exp);
1602
1603       /* Cast return value to the return type of the function.  */
1604       if (thisfun != NULL)
1605         return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1606       if (return_type == NULL)
1607         return_type = builtin_type_int;
1608       return_value = value_cast (return_type, return_value);
1609
1610       /* Make sure we have fully evaluated it, since
1611          it might live in the stack frame we're about to pop.  */
1612       if (VALUE_LAZY (return_value))
1613         value_fetch_lazy (return_value);
1614     }
1615
1616   /* If interactive, require confirmation.  */
1617
1618   if (from_tty)
1619     {
1620       if (thisfun != 0)
1621         {
1622           if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1623             {
1624               error ("Not confirmed.");
1625               /* NOTREACHED */
1626             }
1627         }
1628       else if (!query ("Make selected stack frame return now? "))
1629         error ("Not confirmed.");
1630     }
1631
1632   /* FIXME: cagney/2003-01-18: Rather than pop each frame in turn,
1633      this code should just go straight to the relevant frame and pop
1634      that.  */
1635
1636   /* Do the real work.  Pop until the specified frame is current.  We
1637      use this method because the deprecated_selected_frame is not valid after
1638      a POP_FRAME.  The pc comparison makes this work even if the
1639      selected frame shares its fp with another frame.  */
1640
1641   while (selected_frame_addr != get_frame_base (frame = get_current_frame ())
1642          || selected_frame_pc != get_frame_pc (frame))
1643     frame_pop (get_current_frame ());
1644
1645   /* Then pop that frame.  */
1646
1647   frame_pop (get_current_frame ());
1648
1649   /* Compute the return value (if any) and store in the place
1650      for return values.  */
1651
1652   if (retval_exp)
1653     set_return_value (return_value);
1654
1655   /* If we are at the end of a call dummy now, pop the dummy frame too.  */
1656
1657   /* FIXME: cagney/2003-01-18: This is silly.  Instead of popping all
1658      the frames except the dummy, and then, as an afterthought,
1659      popping the dummy frame, this code should just pop through to the
1660      dummy frame.  */
1661   
1662   if (CALL_DUMMY_HAS_COMPLETED (read_pc(), read_sp (),
1663                                 get_frame_base (get_current_frame ())))
1664     frame_pop (get_current_frame ());
1665
1666   /* If interactive, print the frame that is now current.  */
1667
1668   if (from_tty)
1669     frame_command ("0", 1);
1670   else
1671     select_frame_command ("0", 0);
1672 }
1673
1674 /* Sets the scope to input function name, provided that the
1675    function is within the current stack frame */
1676
1677 struct function_bounds
1678 {
1679   CORE_ADDR low, high;
1680 };
1681
1682 static void func_command (char *arg, int from_tty);
1683 static void
1684 func_command (char *arg, int from_tty)
1685 {
1686   struct frame_info *fp;
1687   int found = 0;
1688   struct symtabs_and_lines sals;
1689   int i;
1690   int level = 1;
1691   struct function_bounds *func_bounds = (struct function_bounds *) NULL;
1692
1693   if (arg != (char *) NULL)
1694     return;
1695
1696   fp = parse_frame_specification ("0");
1697   sals = decode_line_spec (arg, 1);
1698   func_bounds = (struct function_bounds *) xmalloc (
1699                               sizeof (struct function_bounds) * sals.nelts);
1700   for (i = 0; (i < sals.nelts && !found); i++)
1701     {
1702       if (sals.sals[i].pc == (CORE_ADDR) 0 ||
1703           find_pc_partial_function (sals.sals[i].pc,
1704                                     (char **) NULL,
1705                                     &func_bounds[i].low,
1706                                     &func_bounds[i].high) == 0)
1707         {
1708           func_bounds[i].low =
1709             func_bounds[i].high = (CORE_ADDR) NULL;
1710         }
1711     }
1712
1713   do
1714     {
1715       for (i = 0; (i < sals.nelts && !found); i++)
1716         found = (get_frame_pc (fp) >= func_bounds[i].low &&
1717                  get_frame_pc (fp) < func_bounds[i].high);
1718       if (!found)
1719         {
1720           level = 1;
1721           fp = find_relative_frame (fp, &level);
1722         }
1723     }
1724   while (!found && level == 0);
1725
1726   if (func_bounds)
1727     xfree (func_bounds);
1728
1729   if (!found)
1730     printf_filtered ("'%s' not within current stack frame.\n", arg);
1731   else if (fp != deprecated_selected_frame)
1732     select_and_print_frame (fp);
1733 }
1734
1735 /* Gets the language of the current frame.  */
1736
1737 enum language
1738 get_frame_language (void)
1739 {
1740   register struct symtab *s;
1741   enum language flang;          /* The language of the current frame */
1742
1743   if (deprecated_selected_frame)
1744     {
1745       s = find_pc_symtab (get_frame_pc (deprecated_selected_frame));
1746       if (s)
1747         flang = s->language;
1748       else
1749         flang = language_unknown;
1750     }
1751   else
1752     flang = language_unknown;
1753
1754   return flang;
1755 }
1756 \f
1757 void
1758 _initialize_stack (void)
1759 {
1760 #if 0
1761   backtrace_limit = 30;
1762 #endif
1763
1764   add_com ("return", class_stack, return_command,
1765            "Make selected stack frame return to its caller.\n\
1766 Control remains in the debugger, but when you continue\n\
1767 execution will resume in the frame above the one now selected.\n\
1768 If an argument is given, it is an expression for the value to return.");
1769
1770   add_com ("up", class_stack, up_command,
1771            "Select and print stack frame that called this one.\n\
1772 An argument says how many frames up to go.");
1773   add_com ("up-silently", class_support, up_silently_command,
1774            "Same as the `up' command, but does not print anything.\n\
1775 This is useful in command scripts.");
1776
1777   add_com ("down", class_stack, down_command,
1778            "Select and print stack frame called by this one.\n\
1779 An argument says how many frames down to go.");
1780   add_com_alias ("do", "down", class_stack, 1);
1781   add_com_alias ("dow", "down", class_stack, 1);
1782   add_com ("down-silently", class_support, down_silently_command,
1783            "Same as the `down' command, but does not print anything.\n\
1784 This is useful in command scripts.");
1785
1786   add_com ("frame", class_stack, frame_command,
1787            "Select and print a stack frame.\n\
1788 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
1789 An argument specifies the frame to select.\n\
1790 It can be a stack frame number or the address of the frame.\n\
1791 With argument, nothing is printed if input is coming from\n\
1792 a command file or a user-defined command.");
1793
1794   add_com_alias ("f", "frame", class_stack, 1);
1795
1796   if (xdb_commands)
1797     {
1798       add_com ("L", class_stack, current_frame_command,
1799                "Print the current stack frame.\n");
1800       add_com_alias ("V", "frame", class_stack, 1);
1801     }
1802   add_com ("select-frame", class_stack, select_frame_command,
1803            "Select a stack frame without printing anything.\n\
1804 An argument specifies the frame to select.\n\
1805 It can be a stack frame number or the address of the frame.\n");
1806
1807   add_com ("backtrace", class_stack, backtrace_command,
1808            "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1809 With a negative argument, print outermost -COUNT frames.\n\
1810 Use of the 'full' qualifier also prints the values of the local variables.\n");
1811   add_com_alias ("bt", "backtrace", class_stack, 0);
1812   if (xdb_commands)
1813     {
1814       add_com_alias ("t", "backtrace", class_stack, 0);
1815       add_com ("T", class_stack, backtrace_full_command,
1816                "Print backtrace of all stack frames, or innermost COUNT frames \n\
1817 and the values of the local variables.\n\
1818 With a negative argument, print outermost -COUNT frames.\n\
1819 Usage: T <count>\n");
1820     }
1821
1822   add_com_alias ("where", "backtrace", class_alias, 0);
1823   add_info ("stack", backtrace_command,
1824             "Backtrace of the stack, or innermost COUNT frames.");
1825   add_info_alias ("s", "stack", 1);
1826   add_info ("frame", frame_info,
1827             "All about selected stack frame, or frame at ADDR.");
1828   add_info_alias ("f", "frame", 1);
1829   add_info ("locals", locals_info,
1830             "Local variables of current stack frame.");
1831   add_info ("args", args_info,
1832             "Argument variables of current stack frame.");
1833   if (xdb_commands)
1834     add_com ("l", class_info, args_plus_locals_info,
1835              "Argument and local variables of current stack frame.");
1836
1837   if (dbx_commands)
1838     add_com ("func", class_stack, func_command,
1839       "Select the stack frame that contains <func>.\nUsage: func <name>\n");
1840
1841   add_info ("catch", catch_info,
1842             "Exceptions that can be caught in the current stack frame.");
1843
1844 #if 0
1845   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
1846   "Specify maximum number of frames for \"backtrace\" to print by default.",
1847            &setlist);
1848   add_info ("backtrace-limit", backtrace_limit_info,
1849      "The maximum number of frames for \"backtrace\" to print by default.");
1850 #endif
1851 }