1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "dictionary.h"
33 #include "mi-getopt.h"
34 #include "extension.h"
37 #include "gdbsupport/gdb_optional.h"
38 #include "safe-ctype.h"
40 #include "observable.h"
42 enum what_to_list
{ locals
, arguments
, all
};
44 static void list_args_or_locals (const frame_print_options
&fp_opts
,
45 enum what_to_list what
,
46 enum print_values values
,
48 int skip_unavailable
);
50 /* True if we want to allow Python-based frame filters. */
51 static int frame_filters
= 0;
54 mi_cmd_enable_frame_filters (const char *command
, char **argv
, int argc
)
57 error (_("-enable-frame-filters: no arguments allowed"));
61 /* Like apply_ext_lang_frame_filter, but take a print_values */
63 static enum ext_lang_bt_status
64 mi_apply_ext_lang_frame_filter (frame_info_ptr frame
,
65 frame_filter_flags flags
,
66 enum print_values print_values
,
68 int frame_low
, int frame_high
)
70 /* ext_lang_frame_args's MI options are compatible with MI print
72 return apply_ext_lang_frame_filter (frame
, flags
,
73 (enum ext_lang_frame_args
) print_values
,
75 frame_low
, frame_high
);
78 /* Print a list of the stack frames. Args can be none, in which case
79 we want to print the whole backtrace, or a pair of numbers
80 specifying the frame numbers at which to start and stop the
81 display. If the two numbers are equal, a single frame will be
85 mi_cmd_stack_list_frames (const char *command
, char **argv
, int argc
)
91 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
98 static const struct mi_opt opts
[] =
100 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
104 /* Parse arguments. In this instance we are just looking for
105 --no-frame-filters. */
109 int opt
= mi_getopt ("-stack-list-frames", argc
, argv
,
113 switch ((enum opt
) opt
)
115 case NO_FRAME_FILTERS
:
121 /* After the last option is parsed, there should either be low -
122 high range, or no further arguments. */
123 if ((argc
- oind
!= 0) && (argc
- oind
!= 2))
124 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
126 /* If there is a range, set it. */
127 if (argc
- oind
== 2)
129 frame_low
= atoi (argv
[0 + oind
]);
130 frame_high
= atoi (argv
[1 + oind
]);
134 /* Called with no arguments, it means we want the whole
140 /* Let's position fi on the frame at which to start the
141 display. Could be the innermost frame if the whole stack needs
142 displaying, or if frame_low is 0. */
143 for (i
= 0, fi
= get_current_frame ();
145 i
++, fi
= get_prev_frame (fi
));
148 error (_("-stack-list-frames: Not enough frames in stack."));
150 ui_out_emit_list
list_emitter (current_uiout
, "stack");
152 if (! raw_arg
&& frame_filters
)
154 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_FRAME_INFO
;
155 int py_frame_low
= frame_low
;
157 /* We cannot pass -1 to frame_low, as that would signify a
158 relative backtrace from the tail of the stack. So, in the case
159 of frame_low == -1, assign and increment it. */
160 if (py_frame_low
== -1)
163 result
= apply_ext_lang_frame_filter (get_current_frame (), flags
,
164 NO_VALUES
, current_uiout
,
165 py_frame_low
, frame_high
);
168 /* Run the inbuilt backtrace if there are no filters registered, or
169 if "--no-frame-filters" has been specified from the command. */
170 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
172 /* Now let's print the frames up to frame_high, or until there are
173 frames in the stack. */
175 fi
&& (i
<= frame_high
|| frame_high
== -1);
176 i
++, fi
= get_prev_frame (fi
))
179 fi
.prepare_reinflate ();
180 /* Print the location and the address always, even for level 0.
181 If args is 0, don't print the arguments. */
182 print_frame_info (user_frame_print_options
,
183 fi
, 1, LOC_AND_ADDRESS
, 0 /* args */, 0);
190 mi_cmd_stack_info_depth (const char *command
, char **argv
, int argc
)
197 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
200 frame_high
= atoi (argv
[0]);
202 /* Called with no arguments, it means we want the real depth of
206 for (i
= 0, fi
= get_current_frame ();
207 fi
&& (i
< frame_high
|| frame_high
== -1);
208 i
++, fi
= get_prev_frame (fi
))
211 current_uiout
->field_signed ("depth", i
);
214 /* Print a list of the locals for the current frame. With argument of
215 0, print only the names, with argument of 1 print also the
219 mi_cmd_stack_list_locals (const char *command
, char **argv
, int argc
)
221 frame_info_ptr frame
;
223 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
224 enum print_values print_value
;
226 int skip_unavailable
= 0;
235 static const struct mi_opt opts
[] =
237 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
238 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
245 /* Don't parse 'print-values' as an option. */
246 int opt
= mi_getopt ("-stack-list-locals", argc
- 1, argv
,
251 switch ((enum opt
) opt
)
253 case NO_FRAME_FILTERS
:
256 case SKIP_UNAVAILABLE
:
257 skip_unavailable
= 1;
263 /* After the last option is parsed, there should be only
265 if (argc
- oind
!= 1)
266 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
267 "[--skip-unavailable] PRINT_VALUES"));
269 frame
= get_selected_frame (NULL
);
270 print_value
= mi_parse_print_values (argv
[oind
]);
272 if (! raw_arg
&& frame_filters
)
274 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_LOCALS
;
276 result
= mi_apply_ext_lang_frame_filter (frame
, flags
, print_value
,
277 current_uiout
, 0, 0);
280 /* Run the inbuilt backtrace if there are no filters registered, or
281 if "--no-frame-filters" has been specified from the command. */
282 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
284 list_args_or_locals (user_frame_print_options
,
285 locals
, print_value
, frame
,
290 /* Print a list of the arguments for the current frame. With argument
291 of 0, print only the names, with argument of 1 print also the
295 mi_cmd_stack_list_args (const char *command
, char **argv
, int argc
)
301 enum print_values print_values
;
302 struct ui_out
*uiout
= current_uiout
;
305 int skip_unavailable
= 0;
306 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
312 static const struct mi_opt opts
[] =
314 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
315 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
322 int opt
= mi_getopt_allow_unknown ("-stack-list-args", argc
, argv
,
327 switch ((enum opt
) opt
)
329 case NO_FRAME_FILTERS
:
332 case SKIP_UNAVAILABLE
:
333 skip_unavailable
= 1;
338 if (argc
- oind
!= 1 && argc
- oind
!= 3)
339 error (_("-stack-list-arguments: Usage: " \
340 "[--no-frame-filters] [--skip-unavailable] "
341 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
343 if (argc
- oind
== 3)
345 frame_low
= atoi (argv
[1 + oind
]);
346 frame_high
= atoi (argv
[2 + oind
]);
350 /* Called with no arguments, it means we want args for the whole
356 print_values
= mi_parse_print_values (argv
[oind
]);
358 /* Let's position fi on the frame at which to start the
359 display. Could be the innermost frame if the whole stack needs
360 displaying, or if frame_low is 0. */
361 for (i
= 0, fi
= get_current_frame ();
363 i
++, fi
= get_prev_frame (fi
));
366 error (_("-stack-list-arguments: Not enough frames in stack."));
368 ui_out_emit_list
list_emitter (uiout
, "stack-args");
370 if (! raw_arg
&& frame_filters
)
372 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
;
373 int py_frame_low
= frame_low
;
375 /* We cannot pass -1 to frame_low, as that would signify a
376 relative backtrace from the tail of the stack. So, in the case
377 of frame_low == -1, assign and increment it. */
378 if (py_frame_low
== -1)
381 result
= mi_apply_ext_lang_frame_filter (get_current_frame (), flags
,
382 print_values
, current_uiout
,
383 py_frame_low
, frame_high
);
386 /* Run the inbuilt backtrace if there are no filters registered, or
387 if "--no-frame-filters" has been specified from the command. */
388 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
390 /* Now let's print the frames up to frame_high, or until there are
391 frames in the stack. */
393 fi
&& (i
<= frame_high
|| frame_high
== -1);
394 i
++, fi
= get_prev_frame (fi
))
397 ui_out_emit_tuple
tuple_emitter (uiout
, "frame");
398 uiout
->field_signed ("level", i
);
399 list_args_or_locals (user_frame_print_options
,
400 arguments
, print_values
, fi
, skip_unavailable
);
405 /* Print a list of the local variables (including arguments) for the
406 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
407 or both names and values of the variables must be printed. See
408 parse_print_value for possible values. */
411 mi_cmd_stack_list_variables (const char *command
, char **argv
, int argc
)
413 frame_info_ptr frame
;
415 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
416 enum print_values print_value
;
418 int skip_unavailable
= 0;
427 static const struct mi_opt opts
[] =
429 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
430 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
437 /* Don't parse 'print-values' as an option. */
438 int opt
= mi_getopt ("-stack-list-variables", argc
- 1,
439 argv
, opts
, &oind
, &oarg
);
442 switch ((enum opt
) opt
)
444 case NO_FRAME_FILTERS
:
447 case SKIP_UNAVAILABLE
:
448 skip_unavailable
= 1;
454 /* After the last option is parsed, there should be only
456 if (argc
- oind
!= 1)
457 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
458 "[--skip-unavailable] PRINT_VALUES"));
460 frame
= get_selected_frame (NULL
);
461 print_value
= mi_parse_print_values (argv
[oind
]);
463 if (! raw_arg
&& frame_filters
)
465 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
467 result
= mi_apply_ext_lang_frame_filter (frame
, flags
,
469 current_uiout
, 0, 0);
472 /* Run the inbuilt backtrace if there are no filters registered, or
473 if "--no-frame-filters" has been specified from the command. */
474 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
476 list_args_or_locals (user_frame_print_options
,
477 all
, print_value
, frame
,
482 /* Print single local or argument. ARG must be already read in. For
483 WHAT and VALUES see list_args_or_locals.
485 Errors are printed as if they would be the parameter value. Use
486 zeroed ARG iff it should not be printed according to VALUES. If
487 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
490 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
491 enum print_values values
, int skip_unavailable
)
493 struct ui_out
*uiout
= current_uiout
;
495 gdb_assert (!arg
->val
|| !arg
->error
);
496 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
497 && arg
->error
== NULL
)
498 || values
== PRINT_SIMPLE_VALUES
499 || (values
== PRINT_ALL_VALUES
500 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
501 gdb_assert (arg
->entry_kind
== print_entry_values_no
502 || (arg
->entry_kind
== print_entry_values_only
503 && (arg
->val
|| arg
->error
)));
505 if (skip_unavailable
&& arg
->val
!= NULL
506 && (value_entirely_unavailable (arg
->val
)
507 /* A scalar object that does not have all bits available is
508 also considered unavailable, because all bits contribute
509 to its representation. */
510 || (val_print_scalar_type_p (value_type (arg
->val
))
511 && !value_bytes_available (arg
->val
,
512 value_embedded_offset (arg
->val
),
513 value_type (arg
->val
)->length ()))))
516 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
517 if (values
!= PRINT_NO_VALUES
|| what
== all
)
518 tuple_emitter
.emplace (uiout
, nullptr);
522 stb
.puts (arg
->sym
->print_name ());
523 if (arg
->entry_kind
== print_entry_values_only
)
525 uiout
->field_stream ("name", stb
);
527 if (what
== all
&& arg
->sym
->is_argument ())
528 uiout
->field_signed ("arg", 1);
530 if (values
== PRINT_SIMPLE_VALUES
)
532 check_typedef (arg
->sym
->type ());
533 type_print (arg
->sym
->type (), "", &stb
, -1);
534 uiout
->field_stream ("type", stb
);
537 if (arg
->val
|| arg
->error
)
540 stb
.printf (_("<error reading variable: %s>"), arg
->error
.get ());
545 struct value_print_options opts
;
547 get_no_prettyformat_print_options (&opts
);
549 common_val_print (arg
->val
, &stb
, 0, &opts
,
550 language_def (arg
->sym
->language ()));
552 catch (const gdb_exception_error
&except
)
554 stb
.printf (_("<error reading variable: %s>"),
558 uiout
->field_stream ("value", stb
);
562 /* Print a list of the objects for the frame FI in a certain form,
563 which is determined by VALUES. The objects can be locals,
564 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
565 is true, only print the arguments or local variables whose values
569 list_args_or_locals (const frame_print_options
&fp_opts
,
570 enum what_to_list what
, enum print_values values
,
571 frame_info_ptr fi
, int skip_unavailable
)
573 const struct block
*block
;
575 struct block_iterator iter
;
577 const char *name_of_result
;
578 struct ui_out
*uiout
= current_uiout
;
580 block
= get_frame_block (fi
, 0);
585 name_of_result
= "locals";
588 name_of_result
= "args";
591 name_of_result
= "variables";
594 internal_error ("unexpected what_to_list: %d", (int) what
);
597 ui_out_emit_list
list_emitter (uiout
, name_of_result
);
601 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
605 switch (sym
->aclass ())
608 case LOC_UNDEF
: /* catches errors */
609 case LOC_CONST
: /* constant */
610 case LOC_TYPEDEF
: /* local typedef */
611 case LOC_LABEL
: /* local label */
612 case LOC_BLOCK
: /* local function */
613 case LOC_CONST_BYTES
: /* loc. byte seq. */
614 case LOC_UNRESOLVED
: /* unresolved static */
615 case LOC_OPTIMIZED_OUT
: /* optimized out */
619 case LOC_ARG
: /* argument */
620 case LOC_REF_ARG
: /* reference arg */
621 case LOC_REGPARM_ADDR
: /* indirect register arg */
622 case LOC_LOCAL
: /* stack local */
623 case LOC_STATIC
: /* static */
624 case LOC_REGISTER
: /* register */
625 case LOC_COMPUTED
: /* computed location */
628 else if (what
== locals
)
629 print_me
= !sym
->is_argument ();
631 print_me
= sym
->is_argument ();
637 struct frame_arg arg
, entryarg
;
639 if (sym
->is_argument ())
640 sym2
= lookup_symbol_search_name (sym
->search_name (),
641 block
, VAR_DOMAIN
).symbol
;
644 gdb_assert (sym2
!= NULL
);
647 arg
.entry_kind
= print_entry_values_no
;
649 entryarg
.entry_kind
= print_entry_values_no
;
653 case PRINT_SIMPLE_VALUES
:
654 type
= check_typedef (sym2
->type ());
655 if (type
->code () != TYPE_CODE_ARRAY
656 && type
->code () != TYPE_CODE_STRUCT
657 && type
->code () != TYPE_CODE_UNION
)
659 case PRINT_ALL_VALUES
:
660 if (sym
->is_argument ())
661 read_frame_arg (fp_opts
, sym2
, fi
, &arg
, &entryarg
);
663 read_frame_local (sym2
, fi
, &arg
);
668 if (arg
.entry_kind
!= print_entry_values_only
)
669 list_arg_or_local (&arg
, what
, values
, skip_unavailable
);
670 if (entryarg
.entry_kind
!= print_entry_values_no
)
671 list_arg_or_local (&entryarg
, what
, values
, skip_unavailable
);
675 if (block
->function ())
678 block
= block
->superblock ();
682 /* Read a frame specification from FRAME_EXP and return the selected frame.
683 Call error() if the specification is in any way invalid (so this
684 function never returns NULL).
686 The frame specification is usually an integer level number, however if
687 the number does not match a valid frame level then it will be treated as
688 a frame address. The frame address will then be used to find a matching
689 frame in the stack. If no matching frame is found then a new frame will
692 The use of FRAME_EXP as an address is undocumented in the GDB user
693 manual, this feature is supported here purely for backward
696 static frame_info_ptr
697 parse_frame_specification (const char *frame_exp
)
699 gdb_assert (frame_exp
!= NULL
);
701 /* NOTE: Parse and evaluate expression, but do not use
702 functions such as parse_and_eval_long or
703 parse_and_eval_address to also extract the value.
704 Instead value_as_long and value_as_address are used.
705 This avoids problems with expressions that contain
707 struct value
*arg
= parse_and_eval (frame_exp
);
709 /* Assume ARG is an integer, and try using that to select a frame. */
711 int level
= value_as_long (arg
);
713 fid
= find_relative_frame (get_current_frame (), &level
);
715 /* find_relative_frame was successful. */
718 /* Convert the value into a corresponding address. */
719 CORE_ADDR addr
= value_as_address (arg
);
721 /* Assume that ADDR is an address, use that to identify a frame with a
723 struct frame_id id
= frame_id_build_wild (addr
);
725 /* If (s)he specifies the frame with an address, he deserves
726 what (s)he gets. Still, give the highest one that matches.
727 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
729 for (fid
= get_current_frame ();
731 fid
= get_prev_frame (fid
))
733 if (id
== get_frame_id (fid
))
735 frame_info_ptr prev_frame
;
739 prev_frame
= get_prev_frame (fid
);
741 || id
!= get_frame_id (prev_frame
))
749 /* We couldn't identify the frame as an existing frame, but
750 perhaps we can create one with a single argument. */
751 return create_new_frame (addr
, 0);
754 /* Implement the -stack-select-frame MI command. */
757 mi_cmd_stack_select_frame (const char *command
, char **argv
, int argc
)
759 if (argc
== 0 || argc
> 1)
760 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
761 select_frame (parse_frame_specification (argv
[0]));
765 mi_cmd_stack_info_frame (const char *command
, char **argv
, int argc
)
768 error (_("-stack-info-frame: No arguments allowed"));
770 print_frame_info (user_frame_print_options
,
771 get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0, 1);