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