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