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