1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2019 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 enum what_to_list
{ locals
, arguments
, all
};
42 static void list_args_or_locals (const frame_print_options
&fp_opts
,
43 enum what_to_list what
,
44 enum print_values values
,
45 struct frame_info
*fi
,
46 int skip_unavailable
);
48 /* True if we want to allow Python-based frame filters. */
49 static int frame_filters
= 0;
52 mi_cmd_enable_frame_filters (const char *command
, char **argv
, int argc
)
55 error (_("-enable-frame-filters: no arguments allowed"));
59 /* Like apply_ext_lang_frame_filter, but take a print_values */
61 static enum ext_lang_bt_status
62 mi_apply_ext_lang_frame_filter (struct frame_info
*frame
,
63 frame_filter_flags flags
,
64 enum print_values print_values
,
66 int frame_low
, int frame_high
)
68 /* ext_lang_frame_args's MI options are compatible with MI print
70 return apply_ext_lang_frame_filter (frame
, flags
,
71 (enum ext_lang_frame_args
) print_values
,
73 frame_low
, frame_high
);
76 /* Print a list of the stack frames. Args can be none, in which case
77 we want to print the whole backtrace, or a pair of numbers
78 specifying the frame numbers at which to start and stop the
79 display. If the two numbers are equal, a single frame will be
83 mi_cmd_stack_list_frames (const char *command
, char **argv
, int argc
)
88 struct frame_info
*fi
;
89 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
96 static const struct mi_opt opts
[] =
98 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
102 /* Parse arguments. In this instance we are just looking for
103 --no-frame-filters. */
107 int opt
= mi_getopt ("-stack-list-frames", argc
, argv
,
111 switch ((enum opt
) opt
)
113 case NO_FRAME_FILTERS
:
119 /* After the last option is parsed, there should either be low -
120 high range, or no further arguments. */
121 if ((argc
- oind
!= 0) && (argc
- oind
!= 2))
122 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
124 /* If there is a range, set it. */
125 if (argc
- oind
== 2)
127 frame_low
= atoi (argv
[0 + oind
]);
128 frame_high
= atoi (argv
[1 + oind
]);
132 /* Called with no arguments, it means we want the whole
138 /* Let's position fi on the frame at which to start the
139 display. Could be the innermost frame if the whole stack needs
140 displaying, or if frame_low is 0. */
141 for (i
= 0, fi
= get_current_frame ();
143 i
++, fi
= get_prev_frame (fi
));
146 error (_("-stack-list-frames: Not enough frames in stack."));
148 ui_out_emit_list
list_emitter (current_uiout
, "stack");
150 if (! raw_arg
&& frame_filters
)
152 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_FRAME_INFO
;
153 int py_frame_low
= frame_low
;
155 /* We cannot pass -1 to frame_low, as that would signify a
156 relative backtrace from the tail of the stack. So, in the case
157 of frame_low == -1, assign and increment it. */
158 if (py_frame_low
== -1)
161 result
= apply_ext_lang_frame_filter (get_current_frame (), flags
,
162 NO_VALUES
, current_uiout
,
163 py_frame_low
, frame_high
);
166 /* Run the inbuilt backtrace if there are no filters registered, or
167 if "--no-frame-filters" has been specified from the command. */
168 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
170 /* Now let's print the frames up to frame_high, or until there are
171 frames in the stack. */
173 fi
&& (i
<= frame_high
|| frame_high
== -1);
174 i
++, fi
= get_prev_frame (fi
))
177 /* Print the location and the address always, even for level 0.
178 If args is 0, don't print the arguments. */
179 print_frame_info (user_frame_print_options
,
180 fi
, 1, LOC_AND_ADDRESS
, 0 /* args */, 0);
186 mi_cmd_stack_info_depth (const char *command
, char **argv
, int argc
)
190 struct frame_info
*fi
;
193 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
196 frame_high
= atoi (argv
[0]);
198 /* Called with no arguments, it means we want the real depth of
202 for (i
= 0, fi
= get_current_frame ();
203 fi
&& (i
< frame_high
|| frame_high
== -1);
204 i
++, fi
= get_prev_frame (fi
))
207 current_uiout
->field_signed ("depth", i
);
210 /* Print a list of the locals for the current frame. With argument of
211 0, print only the names, with argument of 1 print also the
215 mi_cmd_stack_list_locals (const char *command
, char **argv
, int argc
)
217 struct frame_info
*frame
;
219 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
220 enum print_values print_value
;
222 int skip_unavailable
= 0;
231 static const struct mi_opt opts
[] =
233 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
234 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
241 /* Don't parse 'print-values' as an option. */
242 int opt
= mi_getopt ("-stack-list-locals", argc
- 1, argv
,
247 switch ((enum opt
) opt
)
249 case NO_FRAME_FILTERS
:
252 case SKIP_UNAVAILABLE
:
253 skip_unavailable
= 1;
259 /* After the last option is parsed, there should be only
261 if (argc
- oind
!= 1)
262 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
263 "[--skip-unavailable] PRINT_VALUES"));
265 frame
= get_selected_frame (NULL
);
266 print_value
= mi_parse_print_values (argv
[oind
]);
268 if (! raw_arg
&& frame_filters
)
270 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_LOCALS
;
272 result
= mi_apply_ext_lang_frame_filter (frame
, flags
, print_value
,
273 current_uiout
, 0, 0);
276 /* Run the inbuilt backtrace if there are no filters registered, or
277 if "--no-frame-filters" has been specified from the command. */
278 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
280 list_args_or_locals (user_frame_print_options
,
281 locals
, print_value
, frame
,
286 /* Print a list of the arguments for the current frame. With argument
287 of 0, print only the names, with argument of 1 print also the
291 mi_cmd_stack_list_args (const char *command
, char **argv
, int argc
)
296 struct frame_info
*fi
;
297 enum print_values print_values
;
298 struct ui_out
*uiout
= current_uiout
;
301 int skip_unavailable
= 0;
302 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
308 static const struct mi_opt opts
[] =
310 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
311 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
318 int opt
= mi_getopt_allow_unknown ("-stack-list-args", argc
, argv
,
323 switch ((enum opt
) opt
)
325 case NO_FRAME_FILTERS
:
328 case SKIP_UNAVAILABLE
:
329 skip_unavailable
= 1;
334 if (argc
- oind
!= 1 && argc
- oind
!= 3)
335 error (_("-stack-list-arguments: Usage: " \
336 "[--no-frame-filters] [--skip-unavailable] "
337 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
339 if (argc
- oind
== 3)
341 frame_low
= atoi (argv
[1 + oind
]);
342 frame_high
= atoi (argv
[2 + oind
]);
346 /* Called with no arguments, it means we want args for the whole
352 print_values
= mi_parse_print_values (argv
[oind
]);
354 /* Let's position fi on the frame at which to start the
355 display. Could be the innermost frame if the whole stack needs
356 displaying, or if frame_low is 0. */
357 for (i
= 0, fi
= get_current_frame ();
359 i
++, fi
= get_prev_frame (fi
));
362 error (_("-stack-list-arguments: Not enough frames in stack."));
364 ui_out_emit_list
list_emitter (uiout
, "stack-args");
366 if (! raw_arg
&& frame_filters
)
368 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
;
369 int py_frame_low
= frame_low
;
371 /* We cannot pass -1 to frame_low, as that would signify a
372 relative backtrace from the tail of the stack. So, in the case
373 of frame_low == -1, assign and increment it. */
374 if (py_frame_low
== -1)
377 result
= mi_apply_ext_lang_frame_filter (get_current_frame (), flags
,
378 print_values
, current_uiout
,
379 py_frame_low
, frame_high
);
382 /* Run the inbuilt backtrace if there are no filters registered, or
383 if "--no-frame-filters" has been specified from the command. */
384 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
386 /* Now let's print the frames up to frame_high, or until there are
387 frames in the stack. */
389 fi
&& (i
<= frame_high
|| frame_high
== -1);
390 i
++, fi
= get_prev_frame (fi
))
393 ui_out_emit_tuple
tuple_emitter (uiout
, "frame");
394 uiout
->field_signed ("level", i
);
395 list_args_or_locals (user_frame_print_options
,
396 arguments
, print_values
, fi
, skip_unavailable
);
401 /* Print a list of the local variables (including arguments) for the
402 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
403 or both names and values of the variables must be printed. See
404 parse_print_value for possible values. */
407 mi_cmd_stack_list_variables (const char *command
, char **argv
, int argc
)
409 struct frame_info
*frame
;
411 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
412 enum print_values print_value
;
414 int skip_unavailable
= 0;
423 static const struct mi_opt opts
[] =
425 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
426 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
433 /* Don't parse 'print-values' as an option. */
434 int opt
= mi_getopt ("-stack-list-variables", argc
- 1,
435 argv
, opts
, &oind
, &oarg
);
438 switch ((enum opt
) opt
)
440 case NO_FRAME_FILTERS
:
443 case SKIP_UNAVAILABLE
:
444 skip_unavailable
= 1;
450 /* After the last option is parsed, there should be only
452 if (argc
- oind
!= 1)
453 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
454 "[--skip-unavailable] PRINT_VALUES"));
456 frame
= get_selected_frame (NULL
);
457 print_value
= mi_parse_print_values (argv
[oind
]);
459 if (! raw_arg
&& frame_filters
)
461 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
463 result
= mi_apply_ext_lang_frame_filter (frame
, flags
,
465 current_uiout
, 0, 0);
468 /* Run the inbuilt backtrace if there are no filters registered, or
469 if "--no-frame-filters" has been specified from the command. */
470 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
472 list_args_or_locals (user_frame_print_options
,
473 all
, print_value
, frame
,
478 /* Print single local or argument. ARG must be already read in. For
479 WHAT and VALUES see list_args_or_locals.
481 Errors are printed as if they would be the parameter value. Use
482 zeroed ARG iff it should not be printed according to VALUES. If
483 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
486 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
487 enum print_values values
, int skip_unavailable
)
489 struct ui_out
*uiout
= current_uiout
;
491 gdb_assert (!arg
->val
|| !arg
->error
);
492 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
493 && arg
->error
== NULL
)
494 || values
== PRINT_SIMPLE_VALUES
495 || (values
== PRINT_ALL_VALUES
496 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
497 gdb_assert (arg
->entry_kind
== print_entry_values_no
498 || (arg
->entry_kind
== print_entry_values_only
499 && (arg
->val
|| arg
->error
)));
501 if (skip_unavailable
&& arg
->val
!= NULL
502 && (value_entirely_unavailable (arg
->val
)
503 /* A scalar object that does not have all bits available is
504 also considered unavailable, because all bits contribute
505 to its representation. */
506 || (val_print_scalar_type_p (value_type (arg
->val
))
507 && !value_bytes_available (arg
->val
,
508 value_embedded_offset (arg
->val
),
509 TYPE_LENGTH (value_type (arg
->val
))))))
512 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
513 if (values
!= PRINT_NO_VALUES
|| what
== all
)
514 tuple_emitter
.emplace (uiout
, nullptr);
518 stb
.puts (arg
->sym
->print_name ());
519 if (arg
->entry_kind
== print_entry_values_only
)
521 uiout
->field_stream ("name", stb
);
523 if (what
== all
&& SYMBOL_IS_ARGUMENT (arg
->sym
))
524 uiout
->field_signed ("arg", 1);
526 if (values
== PRINT_SIMPLE_VALUES
)
528 check_typedef (arg
->sym
->type
);
529 type_print (arg
->sym
->type
, "", &stb
, -1);
530 uiout
->field_stream ("type", stb
);
533 if (arg
->val
|| arg
->error
)
536 stb
.printf (_("<error reading variable: %s>"), arg
->error
.get ());
541 struct value_print_options opts
;
543 get_no_prettyformat_print_options (&opts
);
545 common_val_print (arg
->val
, &stb
, 0, &opts
,
546 language_def (SYMBOL_LANGUAGE (arg
->sym
)));
548 catch (const gdb_exception_error
&except
)
550 stb
.printf (_("<error reading variable: %s>"),
554 uiout
->field_stream ("value", stb
);
558 /* Print a list of the objects for the frame FI in a certain form,
559 which is determined by VALUES. The objects can be locals,
560 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
561 is true, only print the arguments or local variables whose values
565 list_args_or_locals (const frame_print_options
&fp_opts
,
566 enum what_to_list what
, enum print_values values
,
567 struct frame_info
*fi
, int skip_unavailable
)
569 const struct block
*block
;
571 struct block_iterator iter
;
573 const char *name_of_result
;
574 struct ui_out
*uiout
= current_uiout
;
576 block
= get_frame_block (fi
, 0);
581 name_of_result
= "locals";
584 name_of_result
= "args";
587 name_of_result
= "variables";
590 internal_error (__FILE__
, __LINE__
,
591 "unexpected what_to_list: %d", (int) what
);
594 ui_out_emit_list
list_emitter (uiout
, name_of_result
);
598 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
602 switch (SYMBOL_CLASS (sym
))
605 case LOC_UNDEF
: /* catches errors */
606 case LOC_CONST
: /* constant */
607 case LOC_TYPEDEF
: /* local typedef */
608 case LOC_LABEL
: /* local label */
609 case LOC_BLOCK
: /* local function */
610 case LOC_CONST_BYTES
: /* loc. byte seq. */
611 case LOC_UNRESOLVED
: /* unresolved static */
612 case LOC_OPTIMIZED_OUT
: /* optimized out */
616 case LOC_ARG
: /* argument */
617 case LOC_REF_ARG
: /* reference arg */
618 case LOC_REGPARM_ADDR
: /* indirect register arg */
619 case LOC_LOCAL
: /* stack local */
620 case LOC_STATIC
: /* static */
621 case LOC_REGISTER
: /* register */
622 case LOC_COMPUTED
: /* computed location */
625 else if (what
== locals
)
626 print_me
= !SYMBOL_IS_ARGUMENT (sym
);
628 print_me
= SYMBOL_IS_ARGUMENT (sym
);
634 struct frame_arg arg
, entryarg
;
636 if (SYMBOL_IS_ARGUMENT (sym
))
637 sym2
= lookup_symbol (sym
->linkage_name (),
642 gdb_assert (sym2
!= NULL
);
645 arg
.entry_kind
= print_entry_values_no
;
647 entryarg
.entry_kind
= print_entry_values_no
;
651 case PRINT_SIMPLE_VALUES
:
652 type
= check_typedef (sym2
->type
);
653 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
654 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
655 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
657 case PRINT_ALL_VALUES
:
658 if (SYMBOL_IS_ARGUMENT (sym
))
659 read_frame_arg (fp_opts
, sym2
, fi
, &arg
, &entryarg
);
661 read_frame_local (sym2
, fi
, &arg
);
666 if (arg
.entry_kind
!= print_entry_values_only
)
667 list_arg_or_local (&arg
, what
, values
, skip_unavailable
);
668 if (entryarg
.entry_kind
!= print_entry_values_no
)
669 list_arg_or_local (&entryarg
, what
, values
, skip_unavailable
);
673 if (BLOCK_FUNCTION (block
))
676 block
= BLOCK_SUPERBLOCK (block
);
680 /* Read a frame specification from FRAME_EXP and return the selected frame.
681 Call error() if the specification is in any way invalid (so this
682 function never returns NULL).
684 The frame specification is usually an integer level number, however if
685 the number does not match a valid frame level then it will be treated as
686 a frame address. The frame address will then be used to find a matching
687 frame in the stack. If no matching frame is found then a new frame will
690 The use of FRAME_EXP as an address is undocumented in the GDB user
691 manual, this feature is supported here purely for backward
694 static struct frame_info
*
695 parse_frame_specification (const char *frame_exp
)
697 gdb_assert (frame_exp
!= NULL
);
699 /* NOTE: Parse and evaluate expression, but do not use
700 functions such as parse_and_eval_long or
701 parse_and_eval_address to also extract the value.
702 Instead value_as_long and value_as_address are used.
703 This avoids problems with expressions that contain
705 struct value
*arg
= parse_and_eval (frame_exp
);
707 /* Assume ARG is an integer, and try using that to select a frame. */
708 struct frame_info
*fid
;
709 int level
= value_as_long (arg
);
711 fid
= find_relative_frame (get_current_frame (), &level
);
713 /* find_relative_frame was successful. */
716 /* Convert the value into a corresponding address. */
717 CORE_ADDR addr
= value_as_address (arg
);
719 /* Assume that ADDR is an address, use that to identify a frame with a
721 struct frame_id id
= frame_id_build_wild (addr
);
723 /* If (s)he specifies the frame with an address, he deserves
724 what (s)he gets. Still, give the highest one that matches.
725 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
727 for (fid
= get_current_frame ();
729 fid
= get_prev_frame (fid
))
731 if (frame_id_eq (id
, get_frame_id (fid
)))
733 struct frame_info
*prev_frame
;
737 prev_frame
= get_prev_frame (fid
);
739 || !frame_id_eq (id
, get_frame_id (prev_frame
)))
747 /* We couldn't identify the frame as an existing frame, but
748 perhaps we can create one with a single argument. */
749 return create_new_frame (addr
, 0);
752 /* Implement the -stack-select-frame MI command. */
755 mi_cmd_stack_select_frame (const char *command
, char **argv
, int argc
)
757 if (argc
== 0 || argc
> 1)
758 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
760 select_frame_for_mi (parse_frame_specification (argv
[0]));
764 mi_cmd_stack_info_frame (const char *command
, char **argv
, int argc
)
767 error (_("-stack-info-frame: No arguments allowed"));
769 print_frame_info (user_frame_print_options
,
770 get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0, 1);