OSDN Git Service

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