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