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