Introduce gdbarch_num_cooked_regs
[external/binutils.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2018 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "language.h"
26 #include "frame.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "source.h"
31 #include "breakpoint.h"
32 #include "demangle.h"
33 #include "inferior.h"
34 #include "annotate.h"
35 #include "ui-out.h"
36 #include "block.h"
37 #include "stack.h"
38 #include "dictionary.h"
39 #include "reggroups.h"
40 #include "regcache.h"
41 #include "solib.h"
42 #include "valprint.h"
43 #include "gdbthread.h"
44 #include "cp-support.h"
45 #include "disasm.h"
46 #include "inline-frame.h"
47 #include "linespec.h"
48 #include "cli/cli-utils.h"
49 #include "objfiles.h"
50
51 #include "symfile.h"
52 #include "extension.h"
53 #include "observable.h"
54 #include "common/def-vector.h"
55
56 /* The possible choices of "set print frame-arguments", and the value
57    of this setting.  */
58
59 static const char *const print_frame_arguments_choices[] =
60   {"all", "scalars", "none", NULL};
61 static const char *print_frame_arguments = "scalars";
62
63 /* If non-zero, don't invoke pretty-printers for frame arguments.  */
64 static int print_raw_frame_arguments;
65
66 /* The possible choices of "set print entry-values", and the value
67    of this setting.  */
68
69 const char print_entry_values_no[] = "no";
70 const char print_entry_values_only[] = "only";
71 const char print_entry_values_preferred[] = "preferred";
72 const char print_entry_values_if_needed[] = "if-needed";
73 const char print_entry_values_both[] = "both";
74 const char print_entry_values_compact[] = "compact";
75 const char print_entry_values_default[] = "default";
76 static const char *const print_entry_values_choices[] =
77 {
78   print_entry_values_no,
79   print_entry_values_only,
80   print_entry_values_preferred,
81   print_entry_values_if_needed,
82   print_entry_values_both,
83   print_entry_values_compact,
84   print_entry_values_default,
85   NULL
86 };
87 const char *print_entry_values = print_entry_values_default;
88
89 /* Prototypes for local functions.  */
90
91 static void print_frame_local_vars (struct frame_info *, int,
92                                     struct ui_file *);
93
94 static void print_frame (struct frame_info *frame, int print_level,
95                          enum print_what print_what,  int print_args,
96                          struct symtab_and_line sal);
97
98 static void set_last_displayed_sal (int valid,
99                                     struct program_space *pspace,
100                                     CORE_ADDR addr,
101                                     struct symtab *symtab,
102                                     int line);
103
104 static struct frame_info *find_frame_for_function (const char *);
105 static struct frame_info *find_frame_for_address (CORE_ADDR);
106
107 /* Zero means do things normally; we are interacting directly with the
108    user.  One means print the full filename and linenumber when a
109    frame is printed, and do so in a format emacs18/emacs19.22 can
110    parse.  Two means print similar annotations, but in many more
111    cases and in a slightly different syntax.  */
112
113 int annotation_level = 0;
114
115 /* These variables hold the last symtab and line we displayed to the user.
116  * This is where we insert a breakpoint or a skiplist entry by default.  */
117 static int last_displayed_sal_valid = 0;
118 static struct program_space *last_displayed_pspace = 0;
119 static CORE_ADDR last_displayed_addr = 0;
120 static struct symtab *last_displayed_symtab = 0;
121 static int last_displayed_line = 0;
122 \f
123
124 /* Return 1 if we should display the address in addition to the location,
125    because we are in the middle of a statement.  */
126
127 static int
128 frame_show_address (struct frame_info *frame,
129                     struct symtab_and_line sal)
130 {
131   /* If there is a line number, but no PC, then there is no location
132      information associated with this sal.  The only way that should
133      happen is for the call sites of inlined functions (SAL comes from
134      find_frame_sal).  Otherwise, we would have some PC range if the
135      SAL came from a line table.  */
136   if (sal.line != 0 && sal.pc == 0 && sal.end == 0)
137     {
138       if (get_next_frame (frame) == NULL)
139         gdb_assert (inline_skipped_frames (inferior_thread ()) > 0);
140       else
141         gdb_assert (get_frame_type (get_next_frame (frame)) == INLINE_FRAME);
142       return 0;
143     }
144
145   return get_frame_pc (frame) != sal.pc;
146 }
147
148 /* See frame.h.  */
149
150 void
151 print_stack_frame_to_uiout (struct ui_out *uiout, struct frame_info *frame,
152                             int print_level, enum print_what print_what,
153                             int set_current_sal)
154 {
155   scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
156
157   print_stack_frame (frame, print_level, print_what, set_current_sal);
158 }
159
160 /* Show or print a stack frame FRAME briefly.  The output is formatted
161    according to PRINT_LEVEL and PRINT_WHAT printing the frame's
162    relative level, function name, argument list, and file name and
163    line number.  If the frame's PC is not at the beginning of the
164    source line, the actual PC is printed at the beginning.  */
165
166 void
167 print_stack_frame (struct frame_info *frame, int print_level,
168                    enum print_what print_what,
169                    int set_current_sal)
170 {
171
172   /* For mi, alway print location and address.  */
173   if (current_uiout->is_mi_like_p ())
174     print_what = LOC_AND_ADDRESS;
175
176   TRY
177     {
178       print_frame_info (frame, print_level, print_what, 1 /* print_args */,
179                         set_current_sal);
180       if (set_current_sal)
181         set_current_sal_from_frame (frame);
182     }
183   CATCH (e, RETURN_MASK_ERROR)
184     {
185     }
186   END_CATCH
187 }
188
189 /* Print nameless arguments of frame FRAME on STREAM, where START is
190    the offset of the first nameless argument, and NUM is the number of
191    nameless arguments to print.  FIRST is nonzero if this is the first
192    argument (not just the first nameless argument).  */
193
194 static void
195 print_frame_nameless_args (struct frame_info *frame, long start, int num,
196                            int first, struct ui_file *stream)
197 {
198   struct gdbarch *gdbarch = get_frame_arch (frame);
199   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
200   int i;
201   CORE_ADDR argsaddr;
202   long arg_value;
203
204   for (i = 0; i < num; i++)
205     {
206       QUIT;
207       argsaddr = get_frame_args_address (frame);
208       if (!argsaddr)
209         return;
210       arg_value = read_memory_integer (argsaddr + start,
211                                        sizeof (int), byte_order);
212       if (!first)
213         fprintf_filtered (stream, ", ");
214       fprintf_filtered (stream, "%ld", arg_value);
215       first = 0;
216       start += sizeof (int);
217     }
218 }
219
220 /* Print single argument of inferior function.  ARG must be already
221    read in.
222
223    Errors are printed as if they would be the parameter value.  Use zeroed ARG
224    iff it should not be printed accoring to user settings.  */
225
226 static void
227 print_frame_arg (const struct frame_arg *arg)
228 {
229   struct ui_out *uiout = current_uiout;
230   const char *error_message = NULL;
231
232   string_file stb;
233
234   gdb_assert (!arg->val || !arg->error);
235   gdb_assert (arg->entry_kind == print_entry_values_no
236               || arg->entry_kind == print_entry_values_only
237               || (!uiout->is_mi_like_p ()
238                   && arg->entry_kind == print_entry_values_compact));
239
240   annotate_arg_emitter arg_emitter;
241   ui_out_emit_tuple tuple_emitter (uiout, NULL);
242   fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (arg->sym),
243                            SYMBOL_LANGUAGE (arg->sym), DMGL_PARAMS | DMGL_ANSI);
244   if (arg->entry_kind == print_entry_values_compact)
245     {
246       /* It is OK to provide invalid MI-like stream as with
247          PRINT_ENTRY_VALUE_COMPACT we never use MI.  */
248       stb.puts ("=");
249
250       fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (arg->sym),
251                                SYMBOL_LANGUAGE (arg->sym),
252                                DMGL_PARAMS | DMGL_ANSI);
253     }
254   if (arg->entry_kind == print_entry_values_only
255       || arg->entry_kind == print_entry_values_compact)
256     stb.puts ("@entry");
257   uiout->field_stream ("name", stb);
258   annotate_arg_name_end ();
259   uiout->text ("=");
260
261   if (!arg->val && !arg->error)
262     uiout->text ("...");
263   else
264     {
265       if (arg->error)
266         error_message = arg->error;
267       else
268         {
269           TRY
270             {
271               const struct language_defn *language;
272               struct value_print_options opts;
273
274               /* Avoid value_print because it will deref ref parameters.  We
275                  just want to print their addresses.  Print ??? for args whose
276                  address we do not know.  We pass 2 as "recurse" to val_print
277                  because our standard indentation here is 4 spaces, and
278                  val_print indents 2 for each recurse.  */ 
279
280               annotate_arg_value (value_type (arg->val));
281
282               /* Use the appropriate language to display our symbol, unless the
283                  user forced the language to a specific language.  */
284               if (language_mode == language_mode_auto)
285                 language = language_def (SYMBOL_LANGUAGE (arg->sym));
286               else
287                 language = current_language;
288
289               get_no_prettyformat_print_options (&opts);
290               opts.deref_ref = 1;
291               opts.raw = print_raw_frame_arguments;
292
293               /* True in "summary" mode, false otherwise.  */
294               opts.summary = !strcmp (print_frame_arguments, "scalars");
295
296               common_val_print (arg->val, &stb, 2, &opts, language);
297             }
298           CATCH (except, RETURN_MASK_ERROR)
299             {
300               error_message = except.message;
301             }
302           END_CATCH
303         }
304       if (error_message != NULL)
305         stb.printf (_("<error reading variable: %s>"), error_message);
306     }
307
308   uiout->field_stream ("value", stb);
309 }
310
311 /* Read in inferior function local SYM at FRAME into ARGP.  Caller is
312    responsible for xfree of ARGP->ERROR.  This function never throws an
313    exception.  */
314
315 void
316 read_frame_local (struct symbol *sym, struct frame_info *frame,
317                   struct frame_arg *argp)
318 {
319   argp->sym = sym;
320   argp->val = NULL;
321   argp->error = NULL;
322
323   TRY
324     {
325       argp->val = read_var_value (sym, NULL, frame);
326     }
327   CATCH (except, RETURN_MASK_ERROR)
328     {
329       argp->error = xstrdup (except.message);
330     }
331   END_CATCH
332 }
333
334 /* Read in inferior function parameter SYM at FRAME into ARGP.  Caller is
335    responsible for xfree of ARGP->ERROR.  This function never throws an
336    exception.  */
337
338 void
339 read_frame_arg (struct symbol *sym, struct frame_info *frame,
340                 struct frame_arg *argp, struct frame_arg *entryargp)
341 {
342   struct value *val = NULL, *entryval = NULL;
343   char *val_error = NULL, *entryval_error = NULL;
344   int val_equal = 0;
345
346   if (print_entry_values != print_entry_values_only
347       && print_entry_values != print_entry_values_preferred)
348     {
349       TRY
350         {
351           val = read_var_value (sym, NULL, frame);
352         }
353       CATCH (except, RETURN_MASK_ERROR)
354         {
355           val_error = (char *) alloca (strlen (except.message) + 1);
356           strcpy (val_error, except.message);
357         }
358       END_CATCH
359     }
360
361   if (SYMBOL_COMPUTED_OPS (sym) != NULL
362       && SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry != NULL
363       && print_entry_values != print_entry_values_no
364       && (print_entry_values != print_entry_values_if_needed
365           || !val || value_optimized_out (val)))
366     {
367       TRY
368         {
369           const struct symbol_computed_ops *ops;
370
371           ops = SYMBOL_COMPUTED_OPS (sym);
372           entryval = ops->read_variable_at_entry (sym, frame);
373         }
374       CATCH (except, RETURN_MASK_ERROR)
375         {
376           if (except.error != NO_ENTRY_VALUE_ERROR)
377             {
378               entryval_error = (char *) alloca (strlen (except.message) + 1);
379               strcpy (entryval_error, except.message);
380             }
381         }
382       END_CATCH
383
384       if (entryval != NULL && value_optimized_out (entryval))
385         entryval = NULL;
386
387       if (print_entry_values == print_entry_values_compact
388           || print_entry_values == print_entry_values_default)
389         {
390           /* For MI do not try to use print_entry_values_compact for ARGP.  */
391
392           if (val && entryval && !current_uiout->is_mi_like_p ())
393             {
394               struct type *type = value_type (val);
395
396               if (value_lazy (val))
397                 value_fetch_lazy (val);
398               if (value_lazy (entryval))
399                 value_fetch_lazy (entryval);
400
401               if (value_contents_eq (val, 0, entryval, 0, TYPE_LENGTH (type)))
402                 {
403                   /* Initialize it just to avoid a GCC false warning.  */
404                   struct value *val_deref = NULL, *entryval_deref;
405
406                   /* DW_AT_call_value does match with the current
407                      value.  If it is a reference still try to verify if
408                      dereferenced DW_AT_call_data_value does not differ.  */
409
410                   TRY
411                     {
412                       struct type *type_deref;
413
414                       val_deref = coerce_ref (val);
415                       if (value_lazy (val_deref))
416                         value_fetch_lazy (val_deref);
417                       type_deref = value_type (val_deref);
418
419                       entryval_deref = coerce_ref (entryval);
420                       if (value_lazy (entryval_deref))
421                         value_fetch_lazy (entryval_deref);
422
423                       /* If the reference addresses match but dereferenced
424                          content does not match print them.  */
425                       if (val != val_deref
426                           && value_contents_eq (val_deref, 0,
427                                                 entryval_deref, 0,
428                                                 TYPE_LENGTH (type_deref)))
429                         val_equal = 1;
430                     }
431                   CATCH (except, RETURN_MASK_ERROR)
432                     {
433                       /* If the dereferenced content could not be
434                          fetched do not display anything.  */
435                       if (except.error == NO_ENTRY_VALUE_ERROR)
436                         val_equal = 1;
437                       else if (except.message != NULL)
438                         {
439                           entryval_error = (char *) alloca (strlen (except.message) + 1);
440                           strcpy (entryval_error, except.message);
441                         }
442                     }
443                   END_CATCH
444
445                   /* Value was not a reference; and its content matches.  */
446                   if (val == val_deref)
447                     val_equal = 1;
448
449                   if (val_equal)
450                     entryval = NULL;
451                 }
452             }
453
454           /* Try to remove possibly duplicate error message for ENTRYARGP even
455              in MI mode.  */
456
457           if (val_error && entryval_error
458               && strcmp (val_error, entryval_error) == 0)
459             {
460               entryval_error = NULL;
461
462               /* Do not se VAL_EQUAL as the same error message may be shown for
463                  the entry value even if no entry values are present in the
464                  inferior.  */
465             }
466         }
467     }
468
469   if (entryval == NULL)
470     {
471       if (print_entry_values == print_entry_values_preferred)
472         {
473           gdb_assert (val == NULL);
474
475           TRY
476             {
477               val = read_var_value (sym, NULL, frame);
478             }
479           CATCH (except, RETURN_MASK_ERROR)
480             {
481               val_error = (char *) alloca (strlen (except.message) + 1);
482               strcpy (val_error, except.message);
483             }
484           END_CATCH
485         }
486       if (print_entry_values == print_entry_values_only
487           || print_entry_values == print_entry_values_both
488           || (print_entry_values == print_entry_values_preferred
489               && (!val || value_optimized_out (val))))
490         {
491           entryval = allocate_optimized_out_value (SYMBOL_TYPE (sym));
492           entryval_error = NULL;
493         }
494     }
495   if ((print_entry_values == print_entry_values_compact
496        || print_entry_values == print_entry_values_if_needed
497        || print_entry_values == print_entry_values_preferred)
498       && (!val || value_optimized_out (val)) && entryval != NULL)
499     {
500       val = NULL;
501       val_error = NULL;
502     }
503
504   argp->sym = sym;
505   argp->val = val;
506   argp->error = val_error ? xstrdup (val_error) : NULL;
507   if (!val && !val_error)
508     argp->entry_kind = print_entry_values_only;
509   else if ((print_entry_values == print_entry_values_compact
510            || print_entry_values == print_entry_values_default) && val_equal)
511     {
512       argp->entry_kind = print_entry_values_compact;
513       gdb_assert (!current_uiout->is_mi_like_p ());
514     }
515   else
516     argp->entry_kind = print_entry_values_no;
517
518   entryargp->sym = sym;
519   entryargp->val = entryval;
520   entryargp->error = entryval_error ? xstrdup (entryval_error) : NULL;
521   if (!entryval && !entryval_error)
522     entryargp->entry_kind = print_entry_values_no;
523   else
524     entryargp->entry_kind = print_entry_values_only;
525 }
526
527 /* Print the arguments of frame FRAME on STREAM, given the function
528    FUNC running in that frame (as a symbol), where NUM is the number
529    of arguments according to the stack frame (or -1 if the number of
530    arguments is unknown).  */
531
532 /* Note that currently the "number of arguments according to the
533    stack frame" is only known on VAX where i refers to the "number of
534    ints of arguments according to the stack frame".  */
535
536 static void
537 print_frame_args (struct symbol *func, struct frame_info *frame,
538                   int num, struct ui_file *stream)
539 {
540   struct ui_out *uiout = current_uiout;
541   int first = 1;
542   /* Offset of next stack argument beyond the one we have seen that is
543      at the highest offset, or -1 if we haven't come to a stack
544      argument yet.  */
545   long highest_offset = -1;
546   /* Number of ints of arguments that we have printed so far.  */
547   int args_printed = 0;
548   /* True if we should print arguments, false otherwise.  */
549   int print_args = strcmp (print_frame_arguments, "none");
550
551   if (func)
552     {
553       const struct block *b = SYMBOL_BLOCK_VALUE (func);
554       struct block_iterator iter;
555       struct symbol *sym;
556
557       ALL_BLOCK_SYMBOLS (b, iter, sym)
558         {
559           struct frame_arg arg, entryarg;
560
561           QUIT;
562
563           /* Keep track of the highest stack argument offset seen, and
564              skip over any kinds of symbols we don't care about.  */
565
566           if (!SYMBOL_IS_ARGUMENT (sym))
567             continue;
568
569           switch (SYMBOL_CLASS (sym))
570             {
571             case LOC_ARG:
572             case LOC_REF_ARG:
573               {
574                 long current_offset = SYMBOL_VALUE (sym);
575                 int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
576
577                 /* Compute address of next argument by adding the size of
578                    this argument and rounding to an int boundary.  */
579                 current_offset =
580                   ((current_offset + arg_size + sizeof (int) - 1)
581                    & ~(sizeof (int) - 1));
582
583                 /* If this is the highest offset seen yet, set
584                    highest_offset.  */
585                 if (highest_offset == -1
586                     || (current_offset > highest_offset))
587                   highest_offset = current_offset;
588
589                 /* Add the number of ints we're about to print to
590                    args_printed.  */
591                 args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
592               }
593
594               /* We care about types of symbols, but don't need to
595                  keep track of stack offsets in them.  */
596             case LOC_REGISTER:
597             case LOC_REGPARM_ADDR:
598             case LOC_COMPUTED:
599             case LOC_OPTIMIZED_OUT:
600             default:
601               break;
602             }
603
604           /* We have to look up the symbol because arguments can have
605              two entries (one a parameter, one a local) and the one we
606              want is the local, which lookup_symbol will find for us.
607              This includes gcc1 (not gcc2) on SPARC when passing a
608              small structure and gcc2 when the argument type is float
609              and it is passed as a double and converted to float by
610              the prologue (in the latter case the type of the LOC_ARG
611              symbol is double and the type of the LOC_LOCAL symbol is
612              float).  */
613           /* But if the parameter name is null, don't try it.  Null
614              parameter names occur on the RS/6000, for traceback
615              tables.  FIXME, should we even print them?  */
616
617           if (*SYMBOL_LINKAGE_NAME (sym))
618             {
619               struct symbol *nsym;
620
621               nsym = lookup_symbol_search_name (SYMBOL_SEARCH_NAME (sym),
622                                                 b, VAR_DOMAIN).symbol;
623               gdb_assert (nsym != NULL);
624               if (SYMBOL_CLASS (nsym) == LOC_REGISTER
625                   && !SYMBOL_IS_ARGUMENT (nsym))
626                 {
627                   /* There is a LOC_ARG/LOC_REGISTER pair.  This means
628                      that it was passed on the stack and loaded into a
629                      register, or passed in a register and stored in a
630                      stack slot.  GDB 3.x used the LOC_ARG; GDB
631                      4.0-4.11 used the LOC_REGISTER.
632
633                      Reasons for using the LOC_ARG:
634
635                      (1) Because find_saved_registers may be slow for
636                          remote debugging.
637
638                      (2) Because registers are often re-used and stack
639                          slots rarely (never?) are.  Therefore using
640                          the stack slot is much less likely to print
641                          garbage.
642
643                      Reasons why we might want to use the LOC_REGISTER:
644
645                      (1) So that the backtrace prints the same value
646                          as "print foo".  I see no compelling reason
647                          why this needs to be the case; having the
648                          backtrace print the value which was passed
649                          in, and "print foo" print the value as
650                          modified within the called function, makes
651                          perfect sense to me.
652
653                      Additional note: It might be nice if "info args"
654                      displayed both values.
655
656                      One more note: There is a case with SPARC
657                      structure passing where we need to use the
658                      LOC_REGISTER, but this is dealt with by creating
659                      a single LOC_REGPARM in symbol reading.  */
660
661                   /* Leave sym (the LOC_ARG) alone.  */
662                   ;
663                 }
664               else
665                 sym = nsym;
666             }
667
668           /* Print the current arg.  */
669           if (!first)
670             uiout->text (", ");
671           uiout->wrap_hint ("    ");
672
673           if (!print_args)
674             {
675               memset (&arg, 0, sizeof (arg));
676               arg.sym = sym;
677               arg.entry_kind = print_entry_values_no;
678               memset (&entryarg, 0, sizeof (entryarg));
679               entryarg.sym = sym;
680               entryarg.entry_kind = print_entry_values_no;
681             }
682           else
683             read_frame_arg (sym, frame, &arg, &entryarg);
684
685           if (arg.entry_kind != print_entry_values_only)
686             print_frame_arg (&arg);
687
688           if (entryarg.entry_kind != print_entry_values_no)
689             {
690               if (arg.entry_kind != print_entry_values_only)
691                 {
692                   uiout->text (", ");
693                   uiout->wrap_hint ("    ");
694                 }
695
696               print_frame_arg (&entryarg);
697             }
698
699           xfree (arg.error);
700           xfree (entryarg.error);
701
702           first = 0;
703         }
704     }
705
706   /* Don't print nameless args in situations where we don't know
707      enough about the stack to find them.  */
708   if (num != -1)
709     {
710       long start;
711
712       if (highest_offset == -1)
713         start = gdbarch_frame_args_skip (get_frame_arch (frame));
714       else
715         start = highest_offset;
716
717       print_frame_nameless_args (frame, start, num - args_printed,
718                                  first, stream);
719     }
720 }
721
722 /* Set the current source and line to the location given by frame
723    FRAME, if possible.  When CENTER is true, adjust so the relevant
724    line is in the center of the next 'list'.  */
725
726 void
727 set_current_sal_from_frame (struct frame_info *frame)
728 {
729   symtab_and_line sal = find_frame_sal (frame);
730   if (sal.symtab != NULL)
731     set_current_source_symtab_and_line (sal);
732 }
733
734 /* If ON, GDB will display disassembly of the next source line when
735    execution of the program being debugged stops.
736    If AUTO (which is the default), or there's no line info to determine
737    the source line of the next instruction, display disassembly of next
738    instruction instead.  */
739
740 static enum auto_boolean disassemble_next_line;
741
742 static void
743 show_disassemble_next_line (struct ui_file *file, int from_tty,
744                                  struct cmd_list_element *c,
745                                  const char *value)
746 {
747   fprintf_filtered (file,
748                     _("Debugger's willingness to use "
749                       "disassemble-next-line is %s.\n"),
750                     value);
751 }
752
753 /* Use TRY_CATCH to catch the exception from the gdb_disassembly
754    because it will be broken by filter sometime.  */
755
756 static void
757 do_gdb_disassembly (struct gdbarch *gdbarch,
758                     int how_many, CORE_ADDR low, CORE_ADDR high)
759 {
760
761   TRY
762     {
763       gdb_disassembly (gdbarch, current_uiout,
764                        DISASSEMBLY_RAW_INSN, how_many,
765                        low, high);
766     }
767   CATCH (exception, RETURN_MASK_ERROR)
768     {
769       /* If an exception was thrown while doing the disassembly, print
770          the error message, to give the user a clue of what happened.  */
771       exception_print (gdb_stderr, exception);
772     }
773   END_CATCH
774 }
775
776 /* Print information about frame FRAME.  The output is format according
777    to PRINT_LEVEL and PRINT_WHAT and PRINT_ARGS.  The meaning of
778    PRINT_WHAT is:
779    
780    SRC_LINE: Print only source line.
781    LOCATION: Print only location.
782    SRC_AND_LOC: Print location and source line.
783
784    Used in "where" output, and to emit breakpoint or step
785    messages.  */
786
787 void
788 print_frame_info (struct frame_info *frame, int print_level,
789                   enum print_what print_what, int print_args,
790                   int set_current_sal)
791 {
792   struct gdbarch *gdbarch = get_frame_arch (frame);
793   int source_print;
794   int location_print;
795   struct ui_out *uiout = current_uiout;
796
797   if (get_frame_type (frame) == DUMMY_FRAME
798       || get_frame_type (frame) == SIGTRAMP_FRAME
799       || get_frame_type (frame) == ARCH_FRAME)
800     {
801       ui_out_emit_tuple tuple_emitter (uiout, "frame");
802
803       annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
804                             gdbarch, get_frame_pc (frame));
805
806       /* Do this regardless of SOURCE because we don't have any source
807          to list for this frame.  */
808       if (print_level)
809         {
810           uiout->text ("#");
811           uiout->field_fmt_int (2, ui_left, "level",
812                                 frame_relative_level (frame));
813         }
814       if (uiout->is_mi_like_p ())
815         {
816           annotate_frame_address ();
817           uiout->field_core_addr ("addr",
818                                   gdbarch, get_frame_pc (frame));
819           annotate_frame_address_end ();
820         }
821
822       if (get_frame_type (frame) == DUMMY_FRAME)
823         {
824           annotate_function_call ();
825           uiout->field_string ("func", "<function called from gdb>");
826         }
827       else if (get_frame_type (frame) == SIGTRAMP_FRAME)
828         {
829           annotate_signal_handler_caller ();
830           uiout->field_string ("func", "<signal handler called>");
831         }
832       else if (get_frame_type (frame) == ARCH_FRAME)
833         {
834           uiout->field_string ("func", "<cross-architecture call>");
835         }
836       uiout->text ("\n");
837       annotate_frame_end ();
838
839       /* If disassemble-next-line is set to auto or on output the next
840          instruction.  */
841       if (disassemble_next_line == AUTO_BOOLEAN_AUTO
842           || disassemble_next_line == AUTO_BOOLEAN_TRUE)
843         do_gdb_disassembly (get_frame_arch (frame), 1,
844                             get_frame_pc (frame), get_frame_pc (frame) + 1);
845
846       return;
847     }
848
849   /* If FRAME is not the innermost frame, that normally means that
850      FRAME->pc points to *after* the call instruction, and we want to
851      get the line containing the call, never the next line.  But if
852      the next frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the
853      next frame was not entered as the result of a call, and we want
854      to get the line containing FRAME->pc.  */
855   symtab_and_line sal = find_frame_sal (frame);
856
857   location_print = (print_what == LOCATION 
858                     || print_what == LOC_AND_ADDRESS
859                     || print_what == SRC_AND_LOC);
860
861   if (location_print || !sal.symtab)
862     print_frame (frame, print_level, print_what, print_args, sal);
863
864   source_print = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
865
866   /* If disassemble-next-line is set to auto or on and doesn't have
867      the line debug messages for $pc, output the next instruction.  */
868   if ((disassemble_next_line == AUTO_BOOLEAN_AUTO
869        || disassemble_next_line == AUTO_BOOLEAN_TRUE)
870       && source_print && !sal.symtab)
871     do_gdb_disassembly (get_frame_arch (frame), 1,
872                         get_frame_pc (frame), get_frame_pc (frame) + 1);
873
874   if (source_print && sal.symtab)
875     {
876       int done = 0;
877       int mid_statement = ((print_what == SRC_LINE)
878                            && frame_show_address (frame, sal));
879
880       if (annotation_level)
881         done = identify_source_line (sal.symtab, sal.line, mid_statement,
882                                      get_frame_pc (frame));
883       if (!done)
884         {
885           if (deprecated_print_frame_info_listing_hook)
886             deprecated_print_frame_info_listing_hook (sal.symtab, 
887                                                       sal.line, 
888                                                       sal.line + 1, 0);
889           else
890             {
891               struct value_print_options opts;
892
893               get_user_print_options (&opts);
894               /* We used to do this earlier, but that is clearly
895                  wrong.  This function is used by many different
896                  parts of gdb, including normal_stop in infrun.c,
897                  which uses this to print out the current PC
898                  when we stepi/nexti into the middle of a source
899                  line.  Only the command line really wants this
900                  behavior.  Other UIs probably would like the
901                  ability to decide for themselves if it is desired.  */
902               if (opts.addressprint && mid_statement)
903                 {
904                   uiout->field_core_addr ("addr",
905                                           gdbarch, get_frame_pc (frame));
906                   uiout->text ("\t");
907                 }
908
909               print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
910             }
911         }
912
913       /* If disassemble-next-line is set to on and there is line debug
914          messages, output assembly codes for next line.  */
915       if (disassemble_next_line == AUTO_BOOLEAN_TRUE)
916         do_gdb_disassembly (get_frame_arch (frame), -1, sal.pc, sal.end);
917     }
918
919   if (set_current_sal)
920     {
921       CORE_ADDR pc;
922
923       if (get_frame_pc_if_available (frame, &pc))
924         set_last_displayed_sal (1, sal.pspace, pc, sal.symtab, sal.line);
925       else
926         set_last_displayed_sal (0, 0, 0, 0, 0);
927     }
928
929   annotate_frame_end ();
930
931   gdb_flush (gdb_stdout);
932 }
933
934 /* Remember the last symtab and line we displayed, which we use e.g.
935  * as the place to put a breakpoint when the `break' command is
936  * invoked with no arguments.  */
937
938 static void
939 set_last_displayed_sal (int valid, struct program_space *pspace,
940                         CORE_ADDR addr, struct symtab *symtab,
941                         int line)
942 {
943   last_displayed_sal_valid = valid;
944   last_displayed_pspace = pspace;
945   last_displayed_addr = addr;
946   last_displayed_symtab = symtab;
947   last_displayed_line = line;
948   if (valid && pspace == NULL)
949     {
950       clear_last_displayed_sal ();
951       internal_error (__FILE__, __LINE__,
952                       _("Trying to set NULL pspace."));
953     }
954 }
955
956 /* Forget the last sal we displayed.  */
957
958 void
959 clear_last_displayed_sal (void)
960 {
961   last_displayed_sal_valid = 0;
962   last_displayed_pspace = 0;
963   last_displayed_addr = 0;
964   last_displayed_symtab = 0;
965   last_displayed_line = 0;
966 }
967
968 /* Is our record of the last sal we displayed valid?  If not,
969  * the get_last_displayed_* functions will return NULL or 0, as
970  * appropriate.  */
971
972 int
973 last_displayed_sal_is_valid (void)
974 {
975   return last_displayed_sal_valid;
976 }
977
978 /* Get the pspace of the last sal we displayed, if it's valid.  */
979
980 struct program_space *
981 get_last_displayed_pspace (void)
982 {
983   if (last_displayed_sal_valid)
984     return last_displayed_pspace;
985   return 0;
986 }
987
988 /* Get the address of the last sal we displayed, if it's valid.  */
989
990 CORE_ADDR
991 get_last_displayed_addr (void)
992 {
993   if (last_displayed_sal_valid)
994     return last_displayed_addr;
995   return 0;
996 }
997
998 /* Get the symtab of the last sal we displayed, if it's valid.  */
999
1000 struct symtab*
1001 get_last_displayed_symtab (void)
1002 {
1003   if (last_displayed_sal_valid)
1004     return last_displayed_symtab;
1005   return 0;
1006 }
1007
1008 /* Get the line of the last sal we displayed, if it's valid.  */
1009
1010 int
1011 get_last_displayed_line (void)
1012 {
1013   if (last_displayed_sal_valid)
1014     return last_displayed_line;
1015   return 0;
1016 }
1017
1018 /* Get the last sal we displayed, if it's valid.  */
1019
1020 symtab_and_line
1021 get_last_displayed_sal ()
1022 {
1023   symtab_and_line sal;
1024
1025   if (last_displayed_sal_valid)
1026     {
1027       sal.pspace = last_displayed_pspace;
1028       sal.pc = last_displayed_addr;
1029       sal.symtab = last_displayed_symtab;
1030       sal.line = last_displayed_line;
1031     }
1032
1033   return sal;
1034 }
1035
1036
1037 /* Attempt to obtain the name, FUNLANG and optionally FUNCP of the function
1038    corresponding to FRAME.  */
1039
1040 gdb::unique_xmalloc_ptr<char>
1041 find_frame_funname (struct frame_info *frame, enum language *funlang,
1042                     struct symbol **funcp)
1043 {
1044   struct symbol *func;
1045   gdb::unique_xmalloc_ptr<char> funname;
1046
1047   *funlang = language_unknown;
1048   if (funcp)
1049     *funcp = NULL;
1050
1051   func = get_frame_function (frame);
1052   if (func)
1053     {
1054       /* In certain pathological cases, the symtabs give the wrong
1055          function (when we are in the first function in a file which
1056          is compiled without debugging symbols, the previous function
1057          is compiled with debugging symbols, and the "foo.o" symbol
1058          that is supposed to tell us where the file with debugging
1059          symbols ends has been truncated by ar because it is longer
1060          than 15 characters).  This also occurs if the user uses asm()
1061          to create a function but not stabs for it (in a file compiled
1062          with -g).
1063
1064          So look in the minimal symbol tables as well, and if it comes
1065          up with a larger address for the function use that instead.
1066          I don't think this can ever cause any problems; there
1067          shouldn't be any minimal symbols in the middle of a function;
1068          if this is ever changed many parts of GDB will need to be
1069          changed (and we'll create a find_pc_minimal_function or some
1070          such).  */
1071
1072       struct bound_minimal_symbol msymbol;
1073
1074       /* Don't attempt to do this for inlined functions, which do not
1075          have a corresponding minimal symbol.  */
1076       if (!block_inlined_p (SYMBOL_BLOCK_VALUE (func)))
1077         msymbol
1078           = lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
1079       else
1080         memset (&msymbol, 0, sizeof (msymbol));
1081
1082       if (msymbol.minsym != NULL
1083           && (BMSYMBOL_VALUE_ADDRESS (msymbol)
1084               > BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func))))
1085         {
1086           /* We also don't know anything about the function besides
1087              its address and name.  */
1088           func = 0;
1089           funname.reset (xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym)));
1090           *funlang = MSYMBOL_LANGUAGE (msymbol.minsym);
1091         }
1092       else
1093         {
1094           const char *print_name = SYMBOL_PRINT_NAME (func);
1095
1096           *funlang = SYMBOL_LANGUAGE (func);
1097           if (funcp)
1098             *funcp = func;
1099           if (*funlang == language_cplus)
1100             {
1101               /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
1102                  to display the demangled name that we already have
1103                  stored in the symbol table, but we stored a version
1104                  with DMGL_PARAMS turned on, and here we don't want to
1105                  display parameters.  So remove the parameters.  */
1106               funname = cp_remove_params (print_name);
1107             }
1108
1109           /* If we didn't hit the C++ case above, set *funname
1110              here.  */
1111           if (funname == NULL)
1112             funname.reset (xstrdup (print_name));
1113         }
1114     }
1115   else
1116     {
1117       struct bound_minimal_symbol msymbol;
1118       CORE_ADDR pc;
1119
1120       if (!get_frame_address_in_block_if_available (frame, &pc))
1121         return funname;
1122
1123       msymbol = lookup_minimal_symbol_by_pc (pc);
1124       if (msymbol.minsym != NULL)
1125         {
1126           funname.reset (xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym)));
1127           *funlang = MSYMBOL_LANGUAGE (msymbol.minsym);
1128         }
1129     }
1130
1131   return funname;
1132 }
1133
1134 static void
1135 print_frame (struct frame_info *frame, int print_level,
1136              enum print_what print_what, int print_args,
1137              struct symtab_and_line sal)
1138 {
1139   struct gdbarch *gdbarch = get_frame_arch (frame);
1140   struct ui_out *uiout = current_uiout;
1141   enum language funlang = language_unknown;
1142   struct value_print_options opts;
1143   struct symbol *func;
1144   CORE_ADDR pc = 0;
1145   int pc_p;
1146
1147   pc_p = get_frame_pc_if_available (frame, &pc);
1148
1149   gdb::unique_xmalloc_ptr<char> funname
1150     = find_frame_funname (frame, &funlang, &func);
1151
1152   annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
1153                         gdbarch, pc);
1154
1155   {
1156     ui_out_emit_tuple tuple_emitter (uiout, "frame");
1157
1158     if (print_level)
1159       {
1160         uiout->text ("#");
1161         uiout->field_fmt_int (2, ui_left, "level",
1162                               frame_relative_level (frame));
1163       }
1164     get_user_print_options (&opts);
1165     if (opts.addressprint)
1166       if (!sal.symtab
1167           || frame_show_address (frame, sal)
1168           || print_what == LOC_AND_ADDRESS)
1169         {
1170           annotate_frame_address ();
1171           if (pc_p)
1172             uiout->field_core_addr ("addr", gdbarch, pc);
1173           else
1174             uiout->field_string ("addr", "<unavailable>");
1175           annotate_frame_address_end ();
1176           uiout->text (" in ");
1177         }
1178     annotate_frame_function_name ();
1179
1180     string_file stb;
1181     fprintf_symbol_filtered (&stb, funname ? funname.get () : "??",
1182                              funlang, DMGL_ANSI);
1183     uiout->field_stream ("func", stb);
1184     uiout->wrap_hint ("   ");
1185     annotate_frame_args ();
1186       
1187     uiout->text (" (");
1188     if (print_args)
1189       {
1190         int numargs;
1191
1192         if (gdbarch_frame_num_args_p (gdbarch))
1193           {
1194             numargs = gdbarch_frame_num_args (gdbarch, frame);
1195             gdb_assert (numargs >= 0);
1196           }
1197         else
1198           numargs = -1;
1199     
1200         {
1201           ui_out_emit_list list_emitter (uiout, "args");
1202           TRY
1203             {
1204               print_frame_args (func, frame, numargs, gdb_stdout);
1205             }
1206           CATCH (e, RETURN_MASK_ERROR)
1207             {
1208             }
1209           END_CATCH
1210
1211             /* FIXME: ARGS must be a list.  If one argument is a string it
1212                will have " that will not be properly escaped.  */
1213             }
1214         QUIT;
1215       }
1216     uiout->text (")");
1217     if (sal.symtab)
1218       {
1219         const char *filename_display;
1220       
1221         filename_display = symtab_to_filename_for_display (sal.symtab);
1222         annotate_frame_source_begin ();
1223         uiout->wrap_hint ("   ");
1224         uiout->text (" at ");
1225         annotate_frame_source_file ();
1226         uiout->field_string ("file", filename_display);
1227         if (uiout->is_mi_like_p ())
1228           {
1229             const char *fullname = symtab_to_fullname (sal.symtab);
1230
1231             uiout->field_string ("fullname", fullname);
1232           }
1233         annotate_frame_source_file_end ();
1234         uiout->text (":");
1235         annotate_frame_source_line ();
1236         uiout->field_int ("line", sal.line);
1237         annotate_frame_source_end ();
1238       }
1239
1240     if (pc_p && (funname == NULL || sal.symtab == NULL))
1241       {
1242         char *lib = solib_name_from_address (get_frame_program_space (frame),
1243                                              get_frame_pc (frame));
1244
1245         if (lib)
1246           {
1247             annotate_frame_where ();
1248             uiout->wrap_hint ("  ");
1249             uiout->text (" from ");
1250             uiout->field_string ("from", lib);
1251           }
1252       }
1253     if (uiout->is_mi_like_p ())
1254       uiout->field_string ("arch",
1255                            (gdbarch_bfd_arch_info (gdbarch))->printable_name);
1256   }
1257
1258   uiout->text ("\n");
1259 }
1260 \f
1261
1262 /* Completion function for "frame function", "info frame function", and
1263    "select-frame function" commands.  */
1264
1265 void
1266 frame_selection_by_function_completer (struct cmd_list_element *ignore,
1267                                        completion_tracker &tracker,
1268                                        const char *text, const char *word)
1269 {
1270   /* This is used to complete function names within a stack.  It would be
1271      nice if we only offered functions that were actually in the stack.
1272      However, this would mean unwinding the stack to completion, which
1273      could take too long, or on a corrupted stack, possibly not end.
1274      Instead, we offer all symbol names as a safer choice.  */
1275   collect_symbol_completion_matches (tracker,
1276                                      complete_symbol_mode::EXPRESSION,
1277                                      symbol_name_match_type::EXPRESSION,
1278                                      text, word);
1279 }
1280
1281 /* Core of all the "info frame" sub-commands.  Print information about a
1282    frame FI.  If SELECTED_FRAME_P is true then the user didn't provide a
1283    frame specification, they just entered 'info frame'.  If the user did
1284    provide a frame specification (for example 'info frame 0', 'info frame
1285    level 1') then SELECTED_FRAME_P will be false.  */
1286
1287 static void
1288 info_frame_command_core (struct frame_info *fi, bool selected_frame_p)
1289 {
1290   struct symbol *func;
1291   struct symtab *s;
1292   struct frame_info *calling_frame_info;
1293   int numregs;
1294   const char *funname = 0;
1295   enum language funlang = language_unknown;
1296   const char *pc_regname;
1297   struct gdbarch *gdbarch;
1298   CORE_ADDR frame_pc;
1299   int frame_pc_p;
1300   /* Initialize it to avoid "may be used uninitialized" warning.  */
1301   CORE_ADDR caller_pc = 0;
1302   int caller_pc_p = 0;
1303
1304   gdbarch = get_frame_arch (fi);
1305
1306   /* Name of the value returned by get_frame_pc().  Per comments, "pc"
1307      is not a good name.  */
1308   if (gdbarch_pc_regnum (gdbarch) >= 0)
1309     /* OK, this is weird.  The gdbarch_pc_regnum hardware register's value can
1310        easily not match that of the internal value returned by
1311        get_frame_pc().  */
1312     pc_regname = gdbarch_register_name (gdbarch, gdbarch_pc_regnum (gdbarch));
1313   else
1314     /* But then, this is weird to.  Even without gdbarch_pc_regnum, an
1315        architectures will often have a hardware register called "pc",
1316        and that register's value, again, can easily not match
1317        get_frame_pc().  */
1318     pc_regname = "pc";
1319
1320   frame_pc_p = get_frame_pc_if_available (fi, &frame_pc);
1321   func = get_frame_function (fi);
1322   symtab_and_line sal = find_frame_sal (fi);
1323   s = sal.symtab;
1324   gdb::unique_xmalloc_ptr<char> func_only;
1325   if (func)
1326     {
1327       funname = SYMBOL_PRINT_NAME (func);
1328       funlang = SYMBOL_LANGUAGE (func);
1329       if (funlang == language_cplus)
1330         {
1331           /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
1332              to display the demangled name that we already have
1333              stored in the symbol table, but we stored a version
1334              with DMGL_PARAMS turned on, and here we don't want to
1335              display parameters.  So remove the parameters.  */
1336           func_only = cp_remove_params (funname);
1337
1338           if (func_only)
1339             funname = func_only.get ();
1340         }
1341     }
1342   else if (frame_pc_p)
1343     {
1344       struct bound_minimal_symbol msymbol;
1345
1346       msymbol = lookup_minimal_symbol_by_pc (frame_pc);
1347       if (msymbol.minsym != NULL)
1348         {
1349           funname = MSYMBOL_PRINT_NAME (msymbol.minsym);
1350           funlang = MSYMBOL_LANGUAGE (msymbol.minsym);
1351         }
1352     }
1353   calling_frame_info = get_prev_frame (fi);
1354
1355   if (selected_frame_p && frame_relative_level (fi) >= 0)
1356     {
1357       printf_filtered (_("Stack level %d, frame at "),
1358                        frame_relative_level (fi));
1359     }
1360   else
1361     {
1362       printf_filtered (_("Stack frame at "));
1363     }
1364   fputs_filtered (paddress (gdbarch, get_frame_base (fi)), gdb_stdout);
1365   printf_filtered (":\n");
1366   printf_filtered (" %s = ", pc_regname);
1367   if (frame_pc_p)
1368     fputs_filtered (paddress (gdbarch, get_frame_pc (fi)), gdb_stdout);
1369   else
1370     fputs_filtered ("<unavailable>", gdb_stdout);
1371
1372   wrap_here ("   ");
1373   if (funname)
1374     {
1375       printf_filtered (" in ");
1376       fprintf_symbol_filtered (gdb_stdout, funname, funlang,
1377                                DMGL_ANSI | DMGL_PARAMS);
1378     }
1379   wrap_here ("   ");
1380   if (sal.symtab)
1381     printf_filtered (" (%s:%d)", symtab_to_filename_for_display (sal.symtab),
1382                      sal.line);
1383   puts_filtered ("; ");
1384   wrap_here ("    ");
1385   printf_filtered ("saved %s = ", pc_regname);
1386
1387   if (!frame_id_p (frame_unwind_caller_id (fi)))
1388     val_print_not_saved (gdb_stdout);
1389   else
1390     {
1391       TRY
1392         {
1393           caller_pc = frame_unwind_caller_pc (fi);
1394           caller_pc_p = 1;
1395         }
1396       CATCH (ex, RETURN_MASK_ERROR)
1397         {
1398           switch (ex.error)
1399             {
1400             case NOT_AVAILABLE_ERROR:
1401               val_print_unavailable (gdb_stdout);
1402               break;
1403             case OPTIMIZED_OUT_ERROR:
1404               val_print_not_saved (gdb_stdout);
1405               break;
1406             default:
1407               fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
1408               break;
1409             }
1410         }
1411       END_CATCH
1412     }
1413
1414   if (caller_pc_p)
1415     fputs_filtered (paddress (gdbarch, caller_pc), gdb_stdout);
1416   printf_filtered ("\n");
1417
1418   if (calling_frame_info == NULL)
1419     {
1420       enum unwind_stop_reason reason;
1421
1422       reason = get_frame_unwind_stop_reason (fi);
1423       if (reason != UNWIND_NO_REASON)
1424         printf_filtered (_(" Outermost frame: %s\n"),
1425                          frame_stop_reason_string (fi));
1426     }
1427   else if (get_frame_type (fi) == TAILCALL_FRAME)
1428     puts_filtered (" tail call frame");
1429   else if (get_frame_type (fi) == INLINE_FRAME)
1430     printf_filtered (" inlined into frame %d",
1431                      frame_relative_level (get_prev_frame (fi)));
1432   else
1433     {
1434       printf_filtered (" called by frame at ");
1435       fputs_filtered (paddress (gdbarch, get_frame_base (calling_frame_info)),
1436                       gdb_stdout);
1437     }
1438   if (get_next_frame (fi) && calling_frame_info)
1439     puts_filtered (",");
1440   wrap_here ("   ");
1441   if (get_next_frame (fi))
1442     {
1443       printf_filtered (" caller of frame at ");
1444       fputs_filtered (paddress (gdbarch, get_frame_base (get_next_frame (fi))),
1445                       gdb_stdout);
1446     }
1447   if (get_next_frame (fi) || calling_frame_info)
1448     puts_filtered ("\n");
1449
1450   if (s)
1451     printf_filtered (" source language %s.\n",
1452                      language_str (s->language));
1453
1454   {
1455     /* Address of the argument list for this frame, or 0.  */
1456     CORE_ADDR arg_list = get_frame_args_address (fi);
1457     /* Number of args for this frame, or -1 if unknown.  */
1458     int numargs;
1459
1460     if (arg_list == 0)
1461       printf_filtered (" Arglist at unknown address.\n");
1462     else
1463       {
1464         printf_filtered (" Arglist at ");
1465         fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1466         printf_filtered (",");
1467
1468         if (!gdbarch_frame_num_args_p (gdbarch))
1469           {
1470             numargs = -1;
1471             puts_filtered (" args: ");
1472           }
1473         else
1474           {
1475             numargs = gdbarch_frame_num_args (gdbarch, fi);
1476             gdb_assert (numargs >= 0);
1477             if (numargs == 0)
1478               puts_filtered (" no args.");
1479             else if (numargs == 1)
1480               puts_filtered (" 1 arg: ");
1481             else
1482               printf_filtered (" %d args: ", numargs);
1483           }
1484         print_frame_args (func, fi, numargs, gdb_stdout);
1485         puts_filtered ("\n");
1486       }
1487   }
1488   {
1489     /* Address of the local variables for this frame, or 0.  */
1490     CORE_ADDR arg_list = get_frame_locals_address (fi);
1491
1492     if (arg_list == 0)
1493       printf_filtered (" Locals at unknown address,");
1494     else
1495       {
1496         printf_filtered (" Locals at ");
1497         fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1498         printf_filtered (",");
1499       }
1500   }
1501
1502   /* Print as much information as possible on the location of all the
1503      registers.  */
1504   {
1505     int count;
1506     int i;
1507     int need_nl = 1;
1508     int sp_regnum = gdbarch_sp_regnum (gdbarch);
1509
1510     /* The sp is special; what's displayed isn't the save address, but
1511        the value of the previous frame's sp.  This is a legacy thing,
1512        at one stage the frame cached the previous frame's SP instead
1513        of its address, hence it was easiest to just display the cached
1514        value.  */
1515     if (sp_regnum >= 0)
1516       {
1517         struct value *value = frame_unwind_register_value (fi, sp_regnum);
1518         gdb_assert (value != NULL);
1519
1520         if (!value_optimized_out (value) && value_entirely_available (value))
1521           {
1522             if (VALUE_LVAL (value) == not_lval)
1523               {
1524                 CORE_ADDR sp;
1525                 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1526                 int sp_size = register_size (gdbarch, sp_regnum);
1527
1528                 sp = extract_unsigned_integer (value_contents_all (value),
1529                                                sp_size, byte_order);
1530
1531                 printf_filtered (" Previous frame's sp is ");
1532                 fputs_filtered (paddress (gdbarch, sp), gdb_stdout);
1533                 printf_filtered ("\n");
1534               }
1535             else if (VALUE_LVAL (value) == lval_memory)
1536               {
1537                 printf_filtered (" Previous frame's sp at ");
1538                 fputs_filtered (paddress (gdbarch, value_address (value)),
1539                                 gdb_stdout);
1540                 printf_filtered ("\n");
1541               }
1542             else if (VALUE_LVAL (value) == lval_register)
1543               {
1544                 printf_filtered (" Previous frame's sp in %s\n",
1545                                  gdbarch_register_name (gdbarch,
1546                                                         VALUE_REGNUM (value)));
1547               }
1548
1549             release_value (value);
1550             need_nl = 0;
1551           }
1552         /* else keep quiet.  */
1553       }
1554
1555     count = 0;
1556     numregs = gdbarch_num_cooked_regs (gdbarch);
1557     for (i = 0; i < numregs; i++)
1558       if (i != sp_regnum
1559           && gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1560         {
1561           enum lval_type lval;
1562           int optimized;
1563           int unavailable;
1564           CORE_ADDR addr;
1565           int realnum;
1566
1567           /* Find out the location of the saved register without
1568              fetching the corresponding value.  */
1569           frame_register_unwind (fi, i, &optimized, &unavailable,
1570                                  &lval, &addr, &realnum, NULL);
1571           /* For moment, only display registers that were saved on the
1572              stack.  */
1573           if (!optimized && !unavailable && lval == lval_memory)
1574             {
1575               if (count == 0)
1576                 puts_filtered (" Saved registers:\n ");
1577               else
1578                 puts_filtered (",");
1579               wrap_here (" ");
1580               printf_filtered (" %s at ",
1581                                gdbarch_register_name (gdbarch, i));
1582               fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1583               count++;
1584             }
1585         }
1586     if (count || need_nl)
1587       puts_filtered ("\n");
1588   }
1589 }
1590
1591 /* Return the innermost frame at level LEVEL.  */
1592
1593 static struct frame_info *
1594 leading_innermost_frame (int level)
1595 {
1596   struct frame_info *leading;
1597
1598   leading = get_current_frame ();
1599
1600   gdb_assert (level >= 0);
1601
1602   while (leading != nullptr && level)
1603     {
1604       QUIT;
1605       leading = get_prev_frame (leading);
1606       level--;
1607     }
1608
1609   return leading;
1610 }
1611
1612 /* Return the starting frame needed to handle COUNT outermost frames.  */
1613
1614 static struct frame_info *
1615 trailing_outermost_frame (int count)
1616 {
1617   struct frame_info *current;
1618   struct frame_info *trailing;
1619
1620   trailing = get_current_frame ();
1621
1622   gdb_assert (count > 0);
1623
1624   current = trailing;
1625   while (current != nullptr && count--)
1626     {
1627       QUIT;
1628       current = get_prev_frame (current);
1629     }
1630
1631   /* Will stop when CURRENT reaches the top of the stack.
1632      TRAILING will be COUNT below it.  */
1633   while (current != nullptr)
1634     {
1635       QUIT;
1636       trailing = get_prev_frame (trailing);
1637       current = get_prev_frame (current);
1638     }
1639
1640   return trailing;
1641 }
1642
1643 /* The core of all the "select-frame" sub-commands.  Just wraps a call to
1644    SELECT_FRAME.  */
1645
1646 static void
1647 select_frame_command_core (struct frame_info *fi, bool ignored)
1648 {
1649   struct frame_info *prev_frame = get_selected_frame_if_set ();
1650   select_frame (fi);
1651   if (get_selected_frame_if_set () != prev_frame)
1652     gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
1653 }
1654
1655 /* See stack.h.  */
1656
1657 void
1658 select_frame_for_mi (struct frame_info *fi)
1659 {
1660   select_frame_command_core (fi, FALSE /* Ignored.  */);
1661 }
1662
1663 /* The core of all the "frame" sub-commands.  Select frame FI, and if this
1664    means we change frame send out a change notification (otherwise, just
1665    reprint the current frame summary).   */
1666
1667 static void
1668 frame_command_core (struct frame_info *fi, bool ignored)
1669 {
1670   struct frame_info *prev_frame = get_selected_frame_if_set ();
1671
1672   select_frame (fi);
1673   if (get_selected_frame_if_set () != prev_frame)
1674     gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
1675   else
1676     print_selected_thread_frame (current_uiout, USER_SELECTED_FRAME);
1677 }
1678
1679 /* The three commands 'frame', 'select-frame', and 'info frame' all have a
1680    common set of sub-commands that allow a specific frame to be selected.
1681    All of the sub-command functions are static methods within this class
1682    template which is then instantiated below.  The template parameter is a
1683    callback used to implement the functionality of the base command
1684    ('frame', 'select-frame', or 'info frame').
1685
1686    In the template parameter FI is the frame being selected.  The
1687    SELECTED_FRAME_P flag is true if the frame being selected was done by
1688    default, which happens when the user uses the base command with no
1689    arguments.  For example the commands 'info frame', 'select-frame',
1690    'frame' will all cause SELECTED_FRAME_P to be true.  In all other cases
1691    SELECTED_FRAME_P is false.  */
1692
1693 template <void (*FPTR) (struct frame_info *fi, bool selected_frame_p)>
1694 class frame_command_helper
1695 {
1696 public:
1697
1698   /* The "frame level" family of commands.  The ARG is an integer that is
1699      the frame's level in the stack.  */
1700   static void
1701   level (const char *arg, int from_tty)
1702   {
1703     int level = value_as_long (parse_and_eval (arg));
1704     struct frame_info *fid
1705       = find_relative_frame (get_current_frame (), &level);
1706     if (level != 0)
1707       error (_("No frame at level %s."), arg);
1708     FPTR (fid, false);
1709   }
1710
1711   /* The "frame address" family of commands.  ARG is a stack-pointer
1712      address for an existing frame.  This command does not allow new
1713      frames to be created.  */
1714
1715   static void
1716   address (const char *arg, int from_tty)
1717   {
1718     CORE_ADDR addr = value_as_address (parse_and_eval (arg));
1719     struct frame_info *fid = find_frame_for_address (addr);
1720     if (fid == NULL)
1721       error (_("No frame at address %s."), arg);
1722     FPTR (fid, false);
1723   }
1724
1725   /* The "frame view" family of commands.  ARG is one or two addresses and
1726      is used to view a frame that might be outside the current backtrace.
1727      The addresses are stack-pointer address, and (optional) pc-address.  */
1728
1729   static void
1730   view (const char *args, int from_tty)
1731   {
1732     struct frame_info *fid;
1733
1734     if (args == NULL)
1735     error (_("Missing address argument to view a frame"));
1736
1737     gdb_argv argv (args);
1738
1739     if (argv.count () == 2)
1740       {
1741         CORE_ADDR addr[2];
1742
1743         addr [0] = value_as_address (parse_and_eval (argv[0]));
1744         addr [1] = value_as_address (parse_and_eval (argv[1]));
1745         fid = create_new_frame (addr[0], addr[1]);
1746       }
1747     else
1748       {
1749         CORE_ADDR addr = value_as_address (parse_and_eval (argv[0]));
1750         fid = create_new_frame (addr, false);
1751       }
1752     FPTR (fid, false);
1753   }
1754
1755   /* The "frame function" family of commands.  ARG is the name of a
1756      function within the stack, the first function (searching from frame
1757      0) with that name will be selected.  */
1758
1759   static void
1760   function (const char *arg, int from_tty)
1761   {
1762     if (arg == NULL)
1763       error (_("Missing function name argument"));
1764     struct frame_info *fid = find_frame_for_function (arg);
1765     if (fid == NULL)
1766       error (_("No frame for function \"%s\"."), arg);
1767     FPTR (fid, false);
1768   }
1769
1770   /* The "frame" base command, that is, when no sub-command is specified.
1771      If one argument is provided then we assume that this is a frame's
1772      level as historically, this was the supported command syntax that was
1773      used most often.
1774
1775      If no argument is provided, then the current frame is selected.  */
1776
1777   static void
1778   base_command (const char *arg, int from_tty)
1779   {
1780     if (arg == NULL)
1781       FPTR (get_selected_frame (_("No stack.")), true);
1782     else
1783       level (arg, from_tty);
1784   }
1785 };
1786
1787 /* Instantiate three FRAME_COMMAND_HELPER instances to implement the
1788    sub-commands for 'info frame', 'frame', and 'select-frame' commands.  */
1789
1790 static frame_command_helper <info_frame_command_core> info_frame_cmd;
1791 static frame_command_helper <frame_command_core> frame_cmd;
1792 static frame_command_helper <select_frame_command_core> select_frame_cmd;
1793
1794 /* Print briefly all stack frames or just the innermost COUNT_EXP
1795    frames.  */
1796
1797 static void
1798 backtrace_command_1 (const char *count_exp, frame_filter_flags flags,
1799                      int no_filters, int from_tty)
1800 {
1801   struct frame_info *fi;
1802   int count;
1803   int py_start = 0, py_end = 0;
1804   enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
1805
1806   if (!target_has_stack)
1807     error (_("No stack."));
1808
1809   if (count_exp)
1810     {
1811       count = parse_and_eval_long (count_exp);
1812       if (count < 0)
1813         py_start = count;
1814       else
1815         {
1816           py_start = 0;
1817           /* The argument to apply_ext_lang_frame_filter is the number
1818              of the final frame to print, and frames start at 0.  */
1819           py_end = count - 1;
1820         }
1821     }
1822   else
1823     {
1824       py_end = -1;
1825       count = -1;
1826     }
1827
1828   if (! no_filters)
1829     {
1830       enum ext_lang_frame_args arg_type;
1831
1832       flags |= PRINT_LEVEL | PRINT_FRAME_INFO | PRINT_ARGS;
1833       if (from_tty)
1834         flags |= PRINT_MORE_FRAMES;
1835
1836       if (!strcmp (print_frame_arguments, "scalars"))
1837         arg_type = CLI_SCALAR_VALUES;
1838       else if (!strcmp (print_frame_arguments, "all"))
1839         arg_type = CLI_ALL_VALUES;
1840       else
1841         arg_type = NO_VALUES;
1842
1843       result = apply_ext_lang_frame_filter (get_current_frame (), flags,
1844                                             arg_type, current_uiout,
1845                                             py_start, py_end);
1846     }
1847
1848   /* Run the inbuilt backtrace if there are no filters registered, or
1849      "no-filters" has been specified from the command.  */
1850   if (no_filters ||  result == EXT_LANG_BT_NO_FILTERS)
1851     {
1852       struct frame_info *trailing;
1853
1854       /* The following code must do two things.  First, it must set the
1855          variable TRAILING to the frame from which we should start
1856          printing.  Second, it must set the variable count to the number
1857          of frames which we should print, or -1 if all of them.  */
1858
1859       if (count_exp != NULL && count < 0)
1860         {
1861           trailing = trailing_outermost_frame (-count);
1862           count = -1;
1863         }
1864       else
1865         trailing = get_current_frame ();
1866
1867       for (fi = trailing; fi && count--; fi = get_prev_frame (fi))
1868         {
1869           QUIT;
1870
1871           /* Don't use print_stack_frame; if an error() occurs it probably
1872              means further attempts to backtrace would fail (on the other
1873              hand, perhaps the code does or could be fixed to make sure
1874              the frame->prev field gets set to NULL in that case).  */
1875
1876           print_frame_info (fi, 1, LOCATION, 1, 0);
1877           if ((flags & PRINT_LOCALS) != 0)
1878             {
1879               struct frame_id frame_id = get_frame_id (fi);
1880
1881               print_frame_local_vars (fi, 1, gdb_stdout);
1882
1883               /* print_frame_local_vars invalidates FI.  */
1884               fi = frame_find_by_id (frame_id);
1885               if (fi == NULL)
1886                 {
1887                   trailing = NULL;
1888                   warning (_("Unable to restore previously selected frame."));
1889                   break;
1890                 }
1891             }
1892
1893           /* Save the last frame to check for error conditions.  */
1894           trailing = fi;
1895         }
1896
1897       /* If we've stopped before the end, mention that.  */
1898       if (fi && from_tty)
1899         printf_filtered (_("(More stack frames follow...)\n"));
1900
1901       /* If we've run out of frames, and the reason appears to be an error
1902          condition, print it.  */
1903       if (fi == NULL && trailing != NULL)
1904         {
1905           enum unwind_stop_reason reason;
1906
1907           reason = get_frame_unwind_stop_reason (trailing);
1908           if (reason >= UNWIND_FIRST_ERROR)
1909             printf_filtered (_("Backtrace stopped: %s\n"),
1910                              frame_stop_reason_string (trailing));
1911         }
1912     }
1913 }
1914
1915 static void
1916 backtrace_command (const char *arg, int from_tty)
1917 {
1918   bool filters = true;
1919   frame_filter_flags flags = 0;
1920
1921   if (arg)
1922     {
1923       bool done = false;
1924
1925       while (!done)
1926         {
1927           const char *save_arg = arg;
1928           std::string this_arg = extract_arg (&arg);
1929
1930           if (this_arg.empty ())
1931             break;
1932
1933           if (subset_compare (this_arg.c_str (), "no-filters"))
1934             filters = false;
1935           else if (subset_compare (this_arg.c_str (), "full"))
1936             flags |= PRINT_LOCALS;
1937           else if (subset_compare (this_arg.c_str (), "hide"))
1938             flags |= PRINT_HIDE;
1939           else
1940             {
1941               /* Not a recognized argument, so stop.  */
1942               arg = save_arg;
1943               done = true;
1944             }
1945         }
1946
1947       if (*arg == '\0')
1948         arg = NULL;
1949     }
1950
1951   backtrace_command_1 (arg, flags, !filters /* no frame-filters */, from_tty);
1952 }
1953
1954 /* Iterate over the local variables of a block B, calling CB with
1955    CB_DATA.  */
1956
1957 static void
1958 iterate_over_block_locals (const struct block *b,
1959                            iterate_over_block_arg_local_vars_cb cb,
1960                            void *cb_data)
1961 {
1962   struct block_iterator iter;
1963   struct symbol *sym;
1964
1965   ALL_BLOCK_SYMBOLS (b, iter, sym)
1966     {
1967       switch (SYMBOL_CLASS (sym))
1968         {
1969         case LOC_LOCAL:
1970         case LOC_REGISTER:
1971         case LOC_STATIC:
1972         case LOC_COMPUTED:
1973         case LOC_OPTIMIZED_OUT:
1974           if (SYMBOL_IS_ARGUMENT (sym))
1975             break;
1976           if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
1977             break;
1978           (*cb) (SYMBOL_PRINT_NAME (sym), sym, cb_data);
1979           break;
1980
1981         default:
1982           /* Ignore symbols which are not locals.  */
1983           break;
1984         }
1985     }
1986 }
1987
1988
1989 /* Same, but print labels.  */
1990
1991 #if 0
1992 /* Commented out, as the code using this function has also been
1993    commented out.  FIXME:brobecker/2009-01-13: Find out why the code
1994    was commented out in the first place.  The discussion introducing
1995    this change (2007-12-04: Support lexical blocks and function bodies
1996    that occupy non-contiguous address ranges) did not explain why
1997    this change was made.  */
1998 static int
1999 print_block_frame_labels (struct gdbarch *gdbarch, struct block *b,
2000                           int *have_default, struct ui_file *stream)
2001 {
2002   struct block_iterator iter;
2003   struct symbol *sym;
2004   int values_printed = 0;
2005
2006   ALL_BLOCK_SYMBOLS (b, iter, sym)
2007     {
2008       if (strcmp (SYMBOL_LINKAGE_NAME (sym), "default") == 0)
2009         {
2010           if (*have_default)
2011             continue;
2012           *have_default = 1;
2013         }
2014       if (SYMBOL_CLASS (sym) == LOC_LABEL)
2015         {
2016           struct symtab_and_line sal;
2017           struct value_print_options opts;
2018
2019           sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
2020           values_printed = 1;
2021           fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
2022           get_user_print_options (&opts);
2023           if (opts.addressprint)
2024             {
2025               fprintf_filtered (stream, " ");
2026               fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (sym)),
2027                               stream);
2028             }
2029           fprintf_filtered (stream, " in file %s, line %d\n",
2030                             sal.symtab->filename, sal.line);
2031         }
2032     }
2033
2034   return values_printed;
2035 }
2036 #endif
2037
2038 /* Iterate over all the local variables in block B, including all its
2039    superblocks, stopping when the top-level block is reached.  */
2040
2041 void
2042 iterate_over_block_local_vars (const struct block *block,
2043                                iterate_over_block_arg_local_vars_cb cb,
2044                                void *cb_data)
2045 {
2046   while (block)
2047     {
2048       iterate_over_block_locals (block, cb, cb_data);
2049       /* After handling the function's top-level block, stop.  Don't
2050          continue to its superblock, the block of per-file
2051          symbols.  */
2052       if (BLOCK_FUNCTION (block))
2053         break;
2054       block = BLOCK_SUPERBLOCK (block);
2055     }
2056 }
2057
2058 /* Data to be passed around in the calls to the locals and args
2059    iterators.  */
2060
2061 struct print_variable_and_value_data
2062 {
2063   struct frame_id frame_id;
2064   int num_tabs;
2065   struct ui_file *stream;
2066   int values_printed;
2067 };
2068
2069 /* The callback for the locals and args iterators.  */
2070
2071 static void
2072 do_print_variable_and_value (const char *print_name,
2073                              struct symbol *sym,
2074                              void *cb_data)
2075 {
2076   struct print_variable_and_value_data *p
2077     = (struct print_variable_and_value_data *) cb_data;
2078   struct frame_info *frame;
2079
2080   frame = frame_find_by_id (p->frame_id);
2081   if (frame == NULL)
2082     {
2083       warning (_("Unable to restore previously selected frame."));
2084       return;
2085     }
2086
2087   print_variable_and_value (print_name, sym, frame, p->stream, p->num_tabs);
2088
2089   /* print_variable_and_value invalidates FRAME.  */
2090   frame = NULL;
2091
2092   p->values_printed = 1;
2093 }
2094
2095 /* Print all variables from the innermost up to the function block of FRAME.
2096    Print them with values to STREAM indented by NUM_TABS.
2097
2098    This function will invalidate FRAME.  */
2099
2100 static void
2101 print_frame_local_vars (struct frame_info *frame, int num_tabs,
2102                         struct ui_file *stream)
2103 {
2104   struct print_variable_and_value_data cb_data;
2105   const struct block *block;
2106   CORE_ADDR pc;
2107
2108   if (!get_frame_pc_if_available (frame, &pc))
2109     {
2110       fprintf_filtered (stream,
2111                         _("PC unavailable, cannot determine locals.\n"));
2112       return;
2113     }
2114
2115   block = get_frame_block (frame, 0);
2116   if (block == 0)
2117     {
2118       fprintf_filtered (stream, "No symbol table info available.\n");
2119       return;
2120     }
2121
2122   cb_data.frame_id = get_frame_id (frame);
2123   cb_data.num_tabs = 4 * num_tabs;
2124   cb_data.stream = stream;
2125   cb_data.values_printed = 0;
2126
2127   /* Temporarily change the selected frame to the given FRAME.
2128      This allows routines that rely on the selected frame instead
2129      of being given a frame as parameter to use the correct frame.  */
2130   scoped_restore_selected_frame restore_selected_frame;
2131   select_frame (frame);
2132
2133   iterate_over_block_local_vars (block,
2134                                  do_print_variable_and_value,
2135                                  &cb_data);
2136
2137   if (!cb_data.values_printed)
2138     fprintf_filtered (stream, _("No locals.\n"));
2139 }
2140
2141 void
2142 info_locals_command (const char *args, int from_tty)
2143 {
2144   print_frame_local_vars (get_selected_frame (_("No frame selected.")),
2145                           0, gdb_stdout);
2146 }
2147
2148 /* Iterate over all the argument variables in block B.  */
2149
2150 void
2151 iterate_over_block_arg_vars (const struct block *b,
2152                              iterate_over_block_arg_local_vars_cb cb,
2153                              void *cb_data)
2154 {
2155   struct block_iterator iter;
2156   struct symbol *sym, *sym2;
2157
2158   ALL_BLOCK_SYMBOLS (b, iter, sym)
2159     {
2160       /* Don't worry about things which aren't arguments.  */
2161       if (SYMBOL_IS_ARGUMENT (sym))
2162         {
2163           /* We have to look up the symbol because arguments can have
2164              two entries (one a parameter, one a local) and the one we
2165              want is the local, which lookup_symbol will find for us.
2166              This includes gcc1 (not gcc2) on the sparc when passing a
2167              small structure and gcc2 when the argument type is float
2168              and it is passed as a double and converted to float by
2169              the prologue (in the latter case the type of the LOC_ARG
2170              symbol is double and the type of the LOC_LOCAL symbol is
2171              float).  There are also LOC_ARG/LOC_REGISTER pairs which
2172              are not combined in symbol-reading.  */
2173
2174           sym2 = lookup_symbol_search_name (SYMBOL_SEARCH_NAME (sym),
2175                                             b, VAR_DOMAIN).symbol;
2176           (*cb) (SYMBOL_PRINT_NAME (sym), sym2, cb_data);
2177         }
2178     }
2179 }
2180
2181 /* Print all argument variables of the function of FRAME.
2182    Print them with values to STREAM.
2183
2184    This function will invalidate FRAME.  */
2185
2186 static void
2187 print_frame_arg_vars (struct frame_info *frame, struct ui_file *stream)
2188 {
2189   struct print_variable_and_value_data cb_data;
2190   struct symbol *func;
2191   CORE_ADDR pc;
2192
2193   if (!get_frame_pc_if_available (frame, &pc))
2194     {
2195       fprintf_filtered (stream, _("PC unavailable, cannot determine args.\n"));
2196       return;
2197     }
2198
2199   func = get_frame_function (frame);
2200   if (func == NULL)
2201     {
2202       fprintf_filtered (stream, _("No symbol table info available.\n"));
2203       return;
2204     }
2205
2206   cb_data.frame_id = get_frame_id (frame);
2207   cb_data.num_tabs = 0;
2208   cb_data.stream = stream;
2209   cb_data.values_printed = 0;
2210
2211   iterate_over_block_arg_vars (SYMBOL_BLOCK_VALUE (func),
2212                                do_print_variable_and_value, &cb_data);
2213
2214   /* do_print_variable_and_value invalidates FRAME.  */
2215   frame = NULL;
2216
2217   if (!cb_data.values_printed)
2218     fprintf_filtered (stream, _("No arguments.\n"));
2219 }
2220
2221 void
2222 info_args_command (const char *ignore, int from_tty)
2223 {
2224   print_frame_arg_vars (get_selected_frame (_("No frame selected.")),
2225                         gdb_stdout);
2226 }
2227 \f
2228 /* Return the symbol-block in which the selected frame is executing.
2229    Can return zero under various legitimate circumstances.
2230
2231    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
2232    code address within the block returned.  We use this to decide
2233    which macros are in scope.  */
2234
2235 const struct block *
2236 get_selected_block (CORE_ADDR *addr_in_block)
2237 {
2238   if (!has_stack_frames ())
2239     return 0;
2240
2241   return get_frame_block (get_selected_frame (NULL), addr_in_block);
2242 }
2243
2244 /* Find a frame a certain number of levels away from FRAME.
2245    LEVEL_OFFSET_PTR points to an int containing the number of levels.
2246    Positive means go to earlier frames (up); negative, the reverse.
2247    The int that contains the number of levels is counted toward
2248    zero as the frames for those levels are found.
2249    If the top or bottom frame is reached, that frame is returned,
2250    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
2251    how much farther the original request asked to go.  */
2252
2253 struct frame_info *
2254 find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
2255 {
2256   /* Going up is simple: just call get_prev_frame enough times or
2257      until the initial frame is reached.  */
2258   while (*level_offset_ptr > 0)
2259     {
2260       struct frame_info *prev = get_prev_frame (frame);
2261
2262       if (!prev)
2263         break;
2264       (*level_offset_ptr)--;
2265       frame = prev;
2266     }
2267
2268   /* Going down is just as simple.  */
2269   while (*level_offset_ptr < 0)
2270     {
2271       struct frame_info *next = get_next_frame (frame);
2272
2273       if (!next)
2274         break;
2275       (*level_offset_ptr)++;
2276       frame = next;
2277     }
2278
2279   return frame;
2280 }
2281
2282 /* Select the frame up one or COUNT_EXP stack levels from the
2283    previously selected frame, and print it briefly.  */
2284
2285 static void
2286 up_silently_base (const char *count_exp)
2287 {
2288   struct frame_info *frame;
2289   int count = 1;
2290
2291   if (count_exp)
2292     count = parse_and_eval_long (count_exp);
2293
2294   frame = find_relative_frame (get_selected_frame ("No stack."), &count);
2295   if (count != 0 && count_exp == NULL)
2296     error (_("Initial frame selected; you cannot go up."));
2297   select_frame (frame);
2298 }
2299
2300 static void
2301 up_silently_command (const char *count_exp, int from_tty)
2302 {
2303   up_silently_base (count_exp);
2304 }
2305
2306 static void
2307 up_command (const char *count_exp, int from_tty)
2308 {
2309   up_silently_base (count_exp);
2310   gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
2311 }
2312
2313 /* Select the frame down one or COUNT_EXP stack levels from the previously
2314    selected frame, and print it briefly.  */
2315
2316 static void
2317 down_silently_base (const char *count_exp)
2318 {
2319   struct frame_info *frame;
2320   int count = -1;
2321
2322   if (count_exp)
2323     count = -parse_and_eval_long (count_exp);
2324
2325   frame = find_relative_frame (get_selected_frame ("No stack."), &count);
2326   if (count != 0 && count_exp == NULL)
2327     {
2328       /* We only do this if COUNT_EXP is not specified.  That way
2329          "down" means to really go down (and let me know if that is
2330          impossible), but "down 9999" can be used to mean go all the
2331          way down without getting an error.  */
2332
2333       error (_("Bottom (innermost) frame selected; you cannot go down."));
2334     }
2335
2336   select_frame (frame);
2337 }
2338
2339 static void
2340 down_silently_command (const char *count_exp, int from_tty)
2341 {
2342   down_silently_base (count_exp);
2343 }
2344
2345 static void
2346 down_command (const char *count_exp, int from_tty)
2347 {
2348   down_silently_base (count_exp);
2349   gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
2350 }
2351
2352 void
2353 return_command (const char *retval_exp, int from_tty)
2354 {
2355   /* Initialize it just to avoid a GCC false warning.  */
2356   enum return_value_convention rv_conv = RETURN_VALUE_STRUCT_CONVENTION;
2357   struct frame_info *thisframe;
2358   struct gdbarch *gdbarch;
2359   struct symbol *thisfun;
2360   struct value *return_value = NULL;
2361   struct value *function = NULL;
2362   const char *query_prefix = "";
2363
2364   thisframe = get_selected_frame ("No selected frame.");
2365   thisfun = get_frame_function (thisframe);
2366   gdbarch = get_frame_arch (thisframe);
2367
2368   if (get_frame_type (get_current_frame ()) == INLINE_FRAME)
2369     error (_("Can not force return from an inlined function."));
2370
2371   /* Compute the return value.  If the computation triggers an error,
2372      let it bail.  If the return type can't be handled, set
2373      RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
2374      message.  */
2375   if (retval_exp)
2376     {
2377       expression_up retval_expr = parse_expression (retval_exp);
2378       struct type *return_type = NULL;
2379
2380       /* Compute the return value.  Should the computation fail, this
2381          call throws an error.  */
2382       return_value = evaluate_expression (retval_expr.get ());
2383
2384       /* Cast return value to the return type of the function.  Should
2385          the cast fail, this call throws an error.  */
2386       if (thisfun != NULL)
2387         return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
2388       if (return_type == NULL)
2389         {
2390           if (retval_expr->elts[0].opcode != UNOP_CAST
2391               && retval_expr->elts[0].opcode != UNOP_CAST_TYPE)
2392             error (_("Return value type not available for selected "
2393                      "stack frame.\n"
2394                      "Please use an explicit cast of the value to return."));
2395           return_type = value_type (return_value);
2396         }
2397       return_type = check_typedef (return_type);
2398       return_value = value_cast (return_type, return_value);
2399
2400       /* Make sure the value is fully evaluated.  It may live in the
2401          stack frame we're about to pop.  */
2402       if (value_lazy (return_value))
2403         value_fetch_lazy (return_value);
2404
2405       if (thisfun != NULL)
2406         function = read_var_value (thisfun, NULL, thisframe);
2407
2408       rv_conv = RETURN_VALUE_REGISTER_CONVENTION;
2409       if (TYPE_CODE (return_type) == TYPE_CODE_VOID)
2410         /* If the return-type is "void", don't try to find the
2411            return-value's location.  However, do still evaluate the
2412            return expression so that, even when the expression result
2413            is discarded, side effects such as "return i++" still
2414            occur.  */
2415         return_value = NULL;
2416       else if (thisfun != NULL)
2417         {
2418           rv_conv = struct_return_convention (gdbarch, function, return_type);
2419           if (rv_conv == RETURN_VALUE_STRUCT_CONVENTION
2420               || rv_conv == RETURN_VALUE_ABI_RETURNS_ADDRESS)
2421             {
2422               query_prefix = "The location at which to store the "
2423                 "function's return value is unknown.\n"
2424                 "If you continue, the return value "
2425                 "that you specified will be ignored.\n";
2426               return_value = NULL;
2427             }
2428         }
2429     }
2430
2431   /* Does an interactive user really want to do this?  Include
2432      information, such as how well GDB can handle the return value, in
2433      the query message.  */
2434   if (from_tty)
2435     {
2436       int confirmed;
2437
2438       if (thisfun == NULL)
2439         confirmed = query (_("%sMake selected stack frame return now? "),
2440                            query_prefix);
2441       else
2442         {
2443           if (TYPE_NO_RETURN (thisfun->type))
2444             warning (_("Function does not return normally to caller."));
2445           confirmed = query (_("%sMake %s return now? "), query_prefix,
2446                              SYMBOL_PRINT_NAME (thisfun));
2447         }
2448       if (!confirmed)
2449         error (_("Not confirmed"));
2450     }
2451
2452   /* Discard the selected frame and all frames inner-to it.  */
2453   frame_pop (get_selected_frame (NULL));
2454
2455   /* Store RETURN_VALUE in the just-returned register set.  */
2456   if (return_value != NULL)
2457     {
2458       struct type *return_type = value_type (return_value);
2459       struct gdbarch *cache_arch = get_current_regcache ()->arch ();
2460
2461       gdb_assert (rv_conv != RETURN_VALUE_STRUCT_CONVENTION
2462                   && rv_conv != RETURN_VALUE_ABI_RETURNS_ADDRESS);
2463       gdbarch_return_value (cache_arch, function, return_type,
2464                             get_current_regcache (), NULL /*read*/,
2465                             value_contents (return_value) /*write*/);
2466     }
2467
2468   /* If we are at the end of a call dummy now, pop the dummy frame
2469      too.  */
2470   if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
2471     frame_pop (get_current_frame ());
2472
2473   select_frame (get_current_frame ());
2474   /* If interactive, print the frame that is now current.  */
2475   if (from_tty)
2476     print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2477 }
2478
2479 /* Find the most inner frame in the current stack for a function called
2480    FUNCTION_NAME.  If no matching frame is found return NULL.  */
2481
2482 static struct frame_info *
2483 find_frame_for_function (const char *function_name)
2484 {
2485   /* Used to hold the lower and upper addresses for each of the
2486      SYMTAB_AND_LINEs found for functions matching FUNCTION_NAME.  */
2487   struct function_bounds
2488   {
2489     CORE_ADDR low, high;
2490   };
2491   struct frame_info *frame;
2492   bool found = false;
2493   int level = 1;
2494
2495   gdb_assert (function_name != NULL);
2496
2497   frame = get_current_frame ();
2498   std::vector<symtab_and_line> sals
2499     = decode_line_with_current_source (function_name,
2500                                        DECODE_LINE_FUNFIRSTLINE);
2501   gdb::def_vector<function_bounds> func_bounds (sals.size ());
2502   for (size_t i = 0; i < sals.size (); i++)
2503     {
2504       if (sals[i].pspace != current_program_space)
2505         func_bounds[i].low = func_bounds[i].high = 0;
2506       else if (sals[i].pc == 0
2507                || find_pc_partial_function (sals[i].pc, NULL,
2508                                             &func_bounds[i].low,
2509                                             &func_bounds[i].high) == 0)
2510         func_bounds[i].low = func_bounds[i].high = 0;
2511     }
2512
2513   do
2514     {
2515       for (size_t i = 0; (i < sals.size () && !found); i++)
2516         found = (get_frame_pc (frame) >= func_bounds[i].low
2517                  && get_frame_pc (frame) < func_bounds[i].high);
2518       if (!found)
2519         {
2520           level = 1;
2521           frame = find_relative_frame (frame, &level);
2522         }
2523     }
2524   while (!found && level == 0);
2525
2526   if (!found)
2527     frame = NULL;
2528
2529   return frame;
2530 }
2531
2532 /* Implements the dbx 'func' command.  */
2533
2534 static void
2535 func_command (const char *arg, int from_tty)
2536 {
2537   if (arg == NULL)
2538     return;
2539
2540   struct frame_info *frame = find_frame_for_function (arg);
2541   if (frame == NULL)
2542     error (_("'%s' not within current stack frame."), arg);
2543   if (frame != get_selected_frame (NULL))
2544     {
2545       select_frame (frame);
2546       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2547     }
2548 }
2549
2550 /* Apply a GDB command to all stack frames, or a set of identified frames,
2551    or innermost COUNT frames.
2552    With a negative COUNT, apply command on outermost -COUNT frames.
2553
2554    frame apply 3 info frame     Apply 'info frame' to frames 0, 1, 2
2555    frame apply -3 info frame    Apply 'info frame' to outermost 3 frames.
2556    frame apply all x/i $pc      Apply 'x/i $pc' cmd to all frames.
2557    frame apply all -s p local_var_no_idea_in_which_frame
2558                 If a frame has a local variable called
2559                 local_var_no_idea_in_which_frame, print frame
2560                 and value of local_var_no_idea_in_which_frame.
2561    frame apply all -s -q p local_var_no_idea_in_which_frame
2562                 Same as before, but only print the variable value.
2563    frame apply level 2-5 0 4-7 -s p i = i + 1
2564                 Adds 1 to the variable i in the specified frames.
2565                 Note that i will be incremented twice in
2566                 frames 4 and 5.  */
2567
2568 /* Apply a GDB command to COUNT stack frames, starting at TRAILING.
2569    CMD starts with 0 or more qcs flags followed by the GDB command to apply.
2570    COUNT -1 means all frames starting at TRAILING.  WHICH_COMMAND is used
2571    for error messages.  */
2572
2573 static void
2574 frame_apply_command_count (const char *which_command,
2575                            const char *cmd, int from_tty,
2576                            struct frame_info *trailing, int count)
2577 {
2578   qcs_flags flags;
2579   struct frame_info *fi;
2580
2581   while (cmd != NULL && parse_flags_qcs (which_command, &cmd, &flags))
2582     ;
2583
2584   if (cmd == NULL || *cmd == '\0')
2585     error (_("Please specify a command to apply on the selected frames"));
2586
2587   /* The below will restore the current inferior/thread/frame.
2588      Usually, only the frame is effectively to be restored.
2589      But in case CMD switches of inferior/thread, better restore
2590      these also.  */
2591   scoped_restore_current_thread restore_thread;
2592
2593   for (fi = trailing; fi && count--; fi = get_prev_frame (fi))
2594     {
2595       QUIT;
2596
2597       select_frame (fi);
2598       TRY
2599         {
2600           std::string cmd_result;
2601           {
2602             /* In case CMD switches of inferior/thread/frame, the below
2603                restores the inferior/thread/frame.  FI can then be
2604                set to the selected frame.  */
2605             scoped_restore_current_thread restore_fi_current_frame;
2606
2607             cmd_result = execute_command_to_string (cmd, from_tty);
2608           }
2609           fi = get_selected_frame (_("frame apply "
2610                                      "unable to get selected frame."));
2611           if (!flags.silent || cmd_result.length () > 0)
2612             {
2613               if (!flags.quiet)
2614                 print_stack_frame (fi, 1, LOCATION, 0);
2615               printf_filtered ("%s", cmd_result.c_str ());
2616             }
2617         }
2618       CATCH (ex, RETURN_MASK_ERROR)
2619         {
2620           fi = get_selected_frame (_("frame apply "
2621                                      "unable to get selected frame."));
2622           if (!flags.silent)
2623             {
2624               if (!flags.quiet)
2625                 print_stack_frame (fi, 1, LOCATION, 0);
2626               if (flags.cont)
2627                 printf_filtered ("%s\n", ex.message);
2628               else
2629                 throw_exception (ex);
2630             }
2631         }
2632       END_CATCH;
2633     }
2634 }
2635
2636 /* Implementation of the "frame apply level" command.  */
2637
2638 static void
2639 frame_apply_level_command (const char *cmd, int from_tty)
2640 {
2641   if (!target_has_stack)
2642     error (_("No stack."));
2643
2644   bool level_found = false;
2645   const char *levels_str = cmd;
2646   number_or_range_parser levels (levels_str);
2647
2648   /* Skip the LEVEL list to find the flags and command args.  */
2649   while (!levels.finished ())
2650     {
2651       /* Call for effect.  */
2652       levels.get_number ();
2653
2654       level_found = true;
2655       if (levels.in_range ())
2656         levels.skip_range ();
2657     }
2658
2659   if (!level_found)
2660     error (_("Missing or invalid LEVEL... argument"));
2661
2662   cmd = levels.cur_tok ();
2663
2664   /* Redo the LEVELS parsing, but applying COMMAND.  */
2665   levels.init (levels_str);
2666   while (!levels.finished ())
2667     {
2668       const int level_beg = levels.get_number ();
2669       int n_frames;
2670
2671       if (levels.in_range ())
2672         {
2673           n_frames = levels.end_value () - level_beg + 1;
2674           levels.skip_range ();
2675         }
2676       else
2677         n_frames = 1;
2678
2679       frame_apply_command_count ("frame apply level", cmd, from_tty,
2680                                  leading_innermost_frame (level_beg), n_frames);
2681     }
2682 }
2683
2684 /* Implementation of the "frame apply all" command.  */
2685
2686 static void
2687 frame_apply_all_command (const char *cmd, int from_tty)
2688 {
2689   if (!target_has_stack)
2690     error (_("No stack."));
2691
2692   frame_apply_command_count ("frame apply all", cmd, from_tty,
2693                              get_current_frame (), INT_MAX);
2694 }
2695
2696 /* Implementation of the "frame apply" command.  */
2697
2698 static void
2699 frame_apply_command (const char* cmd, int from_tty)
2700 {
2701   int count;
2702   struct frame_info *trailing;
2703
2704   if (!target_has_stack)
2705     error (_("No stack."));
2706
2707   if (cmd == NULL)
2708     error (_("Missing COUNT argument."));
2709   count = get_number_trailer (&cmd, 0);
2710   if (count == 0)
2711     error (_("Invalid COUNT argument."));
2712
2713   if (count < 0)
2714     {
2715       trailing = trailing_outermost_frame (-count);
2716       count = -1;
2717     }
2718   else
2719     trailing = get_current_frame ();
2720
2721   frame_apply_command_count ("frame apply", cmd, from_tty,
2722                              trailing, count);
2723 }
2724
2725 /* Implementation of the "faas" command.  */
2726
2727 static void
2728 faas_command (const char *cmd, int from_tty)
2729 {
2730   std::string expanded = std::string ("frame apply all -s ") + cmd;
2731   execute_command (expanded.c_str (), from_tty);
2732 }
2733
2734
2735 /* Find inner-mode frame with frame address ADDRESS.  Return NULL if no
2736    matching frame can be found.  */
2737
2738 static struct frame_info *
2739 find_frame_for_address (CORE_ADDR address)
2740 {
2741   struct frame_id id;
2742   struct frame_info *fid;
2743
2744   id = frame_id_build_wild (address);
2745
2746   /* If (s)he specifies the frame with an address, he deserves
2747      what (s)he gets.  Still, give the highest one that matches.
2748      (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
2749      know).  */
2750   for (fid = get_current_frame ();
2751        fid != NULL;
2752        fid = get_prev_frame (fid))
2753     {
2754       if (frame_id_eq (id, get_frame_id (fid)))
2755         {
2756           struct frame_info *prev_frame;
2757
2758           while (1)
2759             {
2760               prev_frame = get_prev_frame (fid);
2761               if (!prev_frame
2762                   || !frame_id_eq (id, get_frame_id (prev_frame)))
2763                 break;
2764               fid = prev_frame;
2765             }
2766           return fid;
2767         }
2768     }
2769   return NULL;
2770 }
2771
2772 \f
2773
2774 /* Commands with a prefix of `frame apply'.  */
2775 static struct cmd_list_element *frame_apply_cmd_list = NULL;
2776
2777 /* Commands with a prefix of `frame'.  */
2778 static struct cmd_list_element *frame_cmd_list = NULL;
2779
2780 /* Commands with a prefix of `select frame'.  */
2781 static struct cmd_list_element *select_frame_cmd_list = NULL;
2782
2783 /* Commands with a prefix of `info frame'.  */
2784 static struct cmd_list_element *info_frame_cmd_list = NULL;
2785
2786 void
2787 _initialize_stack (void)
2788 {
2789   struct cmd_list_element *cmd;
2790
2791   add_com ("return", class_stack, return_command, _("\
2792 Make selected stack frame return to its caller.\n\
2793 Control remains in the debugger, but when you continue\n\
2794 execution will resume in the frame above the one now selected.\n\
2795 If an argument is given, it is an expression for the value to return."));
2796
2797   add_com ("up", class_stack, up_command, _("\
2798 Select and print stack frame that called this one.\n\
2799 An argument says how many frames up to go."));
2800   add_com ("up-silently", class_support, up_silently_command, _("\
2801 Same as the `up' command, but does not print anything.\n\
2802 This is useful in command scripts."));
2803
2804   add_com ("down", class_stack, down_command, _("\
2805 Select and print stack frame called by this one.\n\
2806 An argument says how many frames down to go."));
2807   add_com_alias ("do", "down", class_stack, 1);
2808   add_com_alias ("dow", "down", class_stack, 1);
2809   add_com ("down-silently", class_support, down_silently_command, _("\
2810 Same as the `down' command, but does not print anything.\n\
2811 This is useful in command scripts."));
2812
2813   add_prefix_cmd ("frame", class_stack,
2814                   &frame_cmd.base_command, _("\
2815 Select and print a stack frame.\n\
2816 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
2817 A single numerical argument specifies the frame to select."),
2818                   &frame_cmd_list, "frame ", 1, &cmdlist);
2819
2820   add_com_alias ("f", "frame", class_stack, 1);
2821
2822 #define FRAME_APPLY_FLAGS_HELP "\
2823 Prints the frame location information followed by COMMAND output.\n\
2824 FLAG arguments are -q (quiet), -c (continue), -s (silent).\n\
2825 Flag -q disables printing the frame location information.\n\
2826 By default, if a COMMAND raises an error, frame apply is aborted.\n\
2827 Flag -c indicates to print the error and continue.\n\
2828 Flag -s indicates to silently ignore a COMMAND that raises an error\n\
2829 or produces no output."
2830
2831   add_prefix_cmd ("apply", class_stack, frame_apply_command,
2832                   _("Apply a command to a number of frames.\n\
2833 Usage: frame apply COUNT [FLAG]... COMMAND\n\
2834 With a negative COUNT argument, applies the command on outermost -COUNT frames.\n"
2835 FRAME_APPLY_FLAGS_HELP),
2836                   &frame_apply_cmd_list, "frame apply ", 1, &frame_cmd_list);
2837
2838   add_cmd ("all", class_stack, frame_apply_all_command,
2839            _("\
2840 Apply a command to all frames.\n\
2841 \n\
2842 Usage: frame apply all [FLAG]... COMMAND\n"
2843 FRAME_APPLY_FLAGS_HELP),
2844            &frame_apply_cmd_list);
2845
2846   add_cmd ("level", class_stack, frame_apply_level_command,
2847            _("\
2848 Apply a command to a list of frames.\n\
2849 \n\
2850 Usage: frame apply level LEVEL... [FLAG]... COMMAND\n\
2851 ID is a space-separated list of LEVELs of frames to apply COMMAND on.\n"
2852 FRAME_APPLY_FLAGS_HELP),
2853            &frame_apply_cmd_list);
2854
2855   add_com ("faas", class_stack, faas_command, _("\
2856 Apply a command to all frames (ignoring errors and empty output).\n\
2857 Usage: faas COMMAND\n\
2858 shortcut for 'frame apply all -s COMMAND'"));
2859
2860
2861   add_prefix_cmd ("frame", class_stack,
2862                   &frame_cmd.base_command, _("\
2863 Select and print a stack frame.\n\
2864 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
2865 A single numerical argument specifies the frame to select."),
2866                   &frame_cmd_list, "frame ", 1, &cmdlist);
2867   add_com_alias ("f", "frame", class_stack, 1);
2868
2869   add_cmd ("address", class_stack, &frame_cmd.address,
2870            _("\
2871 Select and print a stack frame by stack address\n\
2872 \n\
2873 Usage: frame address STACK-ADDRESS"),
2874            &frame_cmd_list);
2875
2876   add_cmd ("view", class_stack, &frame_cmd.view,
2877            _("\
2878 View a stack frame that might be outside the current backtrace.\n\
2879 \n\
2880 Usage: frame view STACK-ADDRESS\n\
2881        frame view STACK-ADDRESS PC-ADDRESS"),
2882            &frame_cmd_list);
2883
2884   cmd = add_cmd ("function", class_stack, &frame_cmd.function,
2885            _("\
2886 Select and print a stack frame by function name.\n\
2887 \n\
2888 Usage: frame function NAME\n\
2889 \n\
2890 The innermost frame that visited function NAME is selected."),
2891            &frame_cmd_list);
2892   set_cmd_completer (cmd, frame_selection_by_function_completer);
2893
2894
2895   add_cmd ("level", class_stack, &frame_cmd.level,
2896            _("\
2897 Select and print a stack frame by level.\n\
2898 \n\
2899 Usage: frame level LEVEL"),
2900            &frame_cmd_list);
2901
2902   cmd = add_prefix_cmd_suppress_notification ("select-frame", class_stack,
2903                       &select_frame_cmd.base_command, _("\
2904 Select a stack frame without printing anything.\n\
2905 A single numerical argument specifies the frame to select."),
2906                       &select_frame_cmd_list, "select-frame ", 1, &cmdlist,
2907                       &cli_suppress_notification.user_selected_context);
2908
2909   add_cmd_suppress_notification ("address", class_stack,
2910                          &select_frame_cmd.address, _("\
2911 Select a stack frame by stack address.\n\
2912 \n\
2913 Usage: select-frame address STACK-ADDRESS"),
2914                          &select_frame_cmd_list,
2915                          &cli_suppress_notification.user_selected_context);
2916
2917
2918   add_cmd_suppress_notification ("view", class_stack,
2919                  &select_frame_cmd.view, _("\
2920 Select a stack frame that might be outside the current backtrace.\n\
2921 \n\
2922 Usage: select-frame view STACK-ADDRESS\n\
2923        select-frame view STACK-ADDRESS PC-ADDRESS"),
2924                  &select_frame_cmd_list,
2925                  &cli_suppress_notification.user_selected_context);
2926
2927   cmd = add_cmd_suppress_notification ("function", class_stack,
2928                &select_frame_cmd.function, _("\
2929 Select a stack frame by function name.\n\
2930 \n\
2931 Usage: select-frame function NAME"),
2932                &select_frame_cmd_list,
2933                &cli_suppress_notification.user_selected_context);
2934   set_cmd_completer (cmd, frame_selection_by_function_completer);
2935
2936   add_cmd_suppress_notification ("level", class_stack,
2937                          &select_frame_cmd.level, _("\
2938 Select a stack frame by level.\n\
2939 \n\
2940 Usage: select-frame level LEVEL"),
2941                          &select_frame_cmd_list,
2942                          &cli_suppress_notification.user_selected_context);
2943
2944   add_com ("backtrace", class_stack, backtrace_command, _("\
2945 Print backtrace of all stack frames, or innermost COUNT frames.\n\
2946 Usage: backtrace [QUALIFIERS]... [COUNT]\n\
2947 With a negative argument, print outermost -COUNT frames.\n\
2948 Use of the 'full' qualifier also prints the values of the local variables.\n\
2949 Use of the 'no-filters' qualifier prohibits frame filters from executing\n\
2950 on this backtrace."));
2951   add_com_alias ("bt", "backtrace", class_stack, 0);
2952
2953   add_com_alias ("where", "backtrace", class_alias, 0);
2954   add_info ("stack", backtrace_command,
2955             _("Backtrace of the stack, or innermost COUNT frames."));
2956   add_info_alias ("s", "stack", 1);
2957
2958   add_prefix_cmd ("frame", class_info, &info_frame_cmd.base_command,
2959                   _("All about the selected stack frame.\n\
2960 With no arguments, displays information about the currently selected stack\n\
2961 frame.  Alternatively a frame specification may be provided (See \"frame\")\n\
2962 the information is then printed about the specified frame."),
2963                   &info_frame_cmd_list, "info frame ", 1, &infolist);
2964   add_info_alias ("f", "frame", 1);
2965
2966   add_cmd ("address", class_stack, &info_frame_cmd.address,
2967            _("\
2968 Print information about a stack frame selected by stack address.\n\
2969 \n\
2970 Usage: info frame address STACK-ADDRESS"),
2971            &info_frame_cmd_list);
2972
2973   add_cmd ("view", class_stack, &info_frame_cmd.view,
2974            _("\
2975 Print information about a stack frame outside the current backtrace.\n\
2976 \n\
2977 Usage: info frame view STACK-ADDRESS\n\
2978        info frame view STACK-ADDRESS PC-ADDRESS"),
2979            &info_frame_cmd_list);
2980
2981   cmd = add_cmd ("function", class_stack, &info_frame_cmd.function,
2982            _("\
2983 Print information about a stack frame selected by function name.\n\
2984 \n\
2985 Usage: info frame function NAME"),
2986            &info_frame_cmd_list);
2987   set_cmd_completer (cmd, frame_selection_by_function_completer);
2988
2989   add_cmd ("level", class_stack, &info_frame_cmd.level,
2990            _("\
2991 Print information about a stack frame selected by level.\n\
2992 \n\
2993 Usage: info frame level LEVEL"),
2994            &info_frame_cmd_list);
2995
2996   add_info ("locals", info_locals_command,
2997             _("Local variables of current stack frame."));
2998   add_info ("args", info_args_command,
2999             _("Argument variables of current stack frame."));
3000
3001   if (dbx_commands)
3002     add_com ("func", class_stack, func_command, _("\
3003 Select the stack frame that contains NAME.\n\
3004 Usage: func NAME"));
3005
3006   add_setshow_enum_cmd ("frame-arguments", class_stack,
3007                         print_frame_arguments_choices, &print_frame_arguments,
3008                         _("Set printing of non-scalar frame arguments"),
3009                         _("Show printing of non-scalar frame arguments"),
3010                         NULL, NULL, NULL, &setprintlist, &showprintlist);
3011
3012   add_setshow_boolean_cmd ("frame-arguments", no_class,
3013                            &print_raw_frame_arguments, _("\
3014 Set whether to print frame arguments in raw form."), _("\
3015 Show whether to print frame arguments in raw form."), _("\
3016 If set, frame arguments are printed in raw form, bypassing any\n\
3017 pretty-printers for that value."),
3018                            NULL, NULL,
3019                            &setprintrawlist, &showprintrawlist);
3020
3021   add_setshow_auto_boolean_cmd ("disassemble-next-line", class_stack,
3022                                 &disassemble_next_line, _("\
3023 Set whether to disassemble next source line or insn when execution stops."),
3024                                 _("\
3025 Show whether to disassemble next source line or insn when execution stops."),
3026                                 _("\
3027 If ON, GDB will display disassembly of the next source line, in addition\n\
3028 to displaying the source line itself.  If the next source line cannot\n\
3029 be displayed (e.g., source is unavailable or there's no line info), GDB\n\
3030 will display disassembly of next instruction instead of showing the\n\
3031 source line.\n\
3032 If AUTO, display disassembly of next instruction only if the source line\n\
3033 cannot be displayed.\n\
3034 If OFF (which is the default), never display the disassembly of the next\n\
3035 source line."),
3036                                 NULL,
3037                                 show_disassemble_next_line,
3038                                 &setlist, &showlist);
3039   disassemble_next_line = AUTO_BOOLEAN_FALSE;
3040
3041   add_setshow_enum_cmd ("entry-values", class_stack,
3042                         print_entry_values_choices, &print_entry_values,
3043                         _("Set printing of function arguments at function "
3044                           "entry"),
3045                         _("Show printing of function arguments at function "
3046                           "entry"),
3047                         _("\
3048 GDB can sometimes determine the values of function arguments at entry,\n\
3049 in addition to their current values.  This option tells GDB whether\n\
3050 to print the current value, the value at entry (marked as val@entry),\n\
3051 or both.  Note that one or both of these values may be <optimized out>."),
3052                         NULL, NULL, &setprintlist, &showprintlist);
3053 }