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