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