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