1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2018 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 "common/gdb_optional.h"
39 enum what_to_list { locals, arguments, all };
41 static void list_args_or_locals (enum what_to_list what,
42 enum print_values values,
43 struct frame_info *fi,
44 int skip_unavailable);
46 /* True if we want to allow Python-based frame filters. */
47 static int frame_filters = 0;
50 mi_cmd_enable_frame_filters (const char *command, char **argv, int argc)
53 error (_("-enable-frame-filters: no arguments allowed"));
57 /* Like apply_ext_lang_frame_filter, but take a print_values */
59 static enum ext_lang_bt_status
60 mi_apply_ext_lang_frame_filter (struct frame_info *frame,
61 frame_filter_flags flags,
62 enum print_values print_values,
64 int frame_low, int frame_high)
66 /* ext_lang_frame_args's MI options are compatible with MI print
68 return apply_ext_lang_frame_filter (frame, flags,
69 (enum ext_lang_frame_args) print_values,
71 frame_low, frame_high);
74 /* Print a list of the stack frames. Args can be none, in which case
75 we want to print the whole backtrace, or a pair of numbers
76 specifying the frame numbers at which to start and stop the
77 display. If the two numbers are equal, a single frame will be
81 mi_cmd_stack_list_frames (const char *command, char **argv, int argc)
86 struct frame_info *fi;
87 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
94 static const struct mi_opt opts[] =
96 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
100 /* Parse arguments. In this instance we are just looking for
101 --no-frame-filters. */
105 int opt = mi_getopt ("-stack-list-frames", argc, argv,
109 switch ((enum opt) opt)
111 case NO_FRAME_FILTERS:
117 /* After the last option is parsed, there should either be low -
118 high range, or no further arguments. */
119 if ((argc - oind != 0) && (argc - oind != 2))
120 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
122 /* If there is a range, set it. */
123 if (argc - oind == 2)
125 frame_low = atoi (argv[0 + oind]);
126 frame_high = atoi (argv[1 + oind]);
130 /* Called with no arguments, it means we want the whole
136 /* Let's position fi on the frame at which to start the
137 display. Could be the innermost frame if the whole stack needs
138 displaying, or if frame_low is 0. */
139 for (i = 0, fi = get_current_frame ();
141 i++, fi = get_prev_frame (fi));
144 error (_("-stack-list-frames: Not enough frames in stack."));
146 ui_out_emit_list list_emitter (current_uiout, "stack");
148 if (! raw_arg && frame_filters)
150 frame_filter_flags flags = PRINT_LEVEL | PRINT_FRAME_INFO;
151 int py_frame_low = frame_low;
153 /* We cannot pass -1 to frame_low, as that would signify a
154 relative backtrace from the tail of the stack. So, in the case
155 of frame_low == -1, assign and increment it. */
156 if (py_frame_low == -1)
159 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
160 NO_VALUES, current_uiout,
161 py_frame_low, frame_high);
164 /* Run the inbuilt backtrace if there are no filters registered, or
165 if "--no-frame-filters" has been specified from the command. */
166 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
168 /* Now let's print the frames up to frame_high, or until there are
169 frames in the stack. */
171 fi && (i <= frame_high || frame_high == -1);
172 i++, fi = get_prev_frame (fi))
175 /* Print the location and the address always, even for level 0.
176 If args is 0, don't print the arguments. */
177 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
183 mi_cmd_stack_info_depth (const char *command, char **argv, int argc)
187 struct frame_info *fi;
190 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
193 frame_high = atoi (argv[0]);
195 /* Called with no arguments, it means we want the real depth of
199 for (i = 0, fi = get_current_frame ();
200 fi && (i < frame_high || frame_high == -1);
201 i++, fi = get_prev_frame (fi))
204 current_uiout->field_int ("depth", i);
207 /* Print a list of the locals for the current frame. With argument of
208 0, print only the names, with argument of 1 print also the
212 mi_cmd_stack_list_locals (const char *command, char **argv, int argc)
214 struct frame_info *frame;
216 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
217 enum print_values print_value;
219 int skip_unavailable = 0;
228 static const struct mi_opt opts[] =
230 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
231 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
238 /* Don't parse 'print-values' as an option. */
239 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
244 switch ((enum opt) opt)
246 case NO_FRAME_FILTERS:
248 case SKIP_UNAVAILABLE:
249 skip_unavailable = 1;
255 /* After the last option is parsed, there should be only
257 if (argc - oind != 1)
258 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
259 "[--skip-unavailable] PRINT_VALUES"));
261 frame = get_selected_frame (NULL);
262 print_value = mi_parse_print_values (argv[oind]);
264 if (! raw_arg && frame_filters)
266 frame_filter_flags flags = PRINT_LEVEL | PRINT_LOCALS;
268 result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
269 current_uiout, 0, 0);
272 /* Run the inbuilt backtrace if there are no filters registered, or
273 if "--no-frame-filters" has been specified from the command. */
274 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
276 list_args_or_locals (locals, print_value, frame,
281 /* Print a list of the arguments for the current frame. With argument
282 of 0, print only the names, with argument of 1 print also the
286 mi_cmd_stack_list_args (const char *command, char **argv, int argc)
291 struct frame_info *fi;
292 enum print_values print_values;
293 struct ui_out *uiout = current_uiout;
296 int skip_unavailable = 0;
297 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
303 static const struct mi_opt opts[] =
305 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
306 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
313 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
318 switch ((enum opt) opt)
320 case NO_FRAME_FILTERS:
323 case SKIP_UNAVAILABLE:
324 skip_unavailable = 1;
329 if (argc - oind != 1 && argc - oind != 3)
330 error (_("-stack-list-arguments: Usage: " \
331 "[--no-frame-filters] [--skip-unavailable] "
332 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
334 if (argc - oind == 3)
336 frame_low = atoi (argv[1 + oind]);
337 frame_high = atoi (argv[2 + oind]);
341 /* Called with no arguments, it means we want args for the whole
347 print_values = mi_parse_print_values (argv[oind]);
349 /* Let's position fi on the frame at which to start the
350 display. Could be the innermost frame if the whole stack needs
351 displaying, or if frame_low is 0. */
352 for (i = 0, fi = get_current_frame ();
354 i++, fi = get_prev_frame (fi));
357 error (_("-stack-list-arguments: Not enough frames in stack."));
359 ui_out_emit_list list_emitter (uiout, "stack-args");
361 if (! raw_arg && frame_filters)
363 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS;
364 int py_frame_low = frame_low;
366 /* We cannot pass -1 to frame_low, as that would signify a
367 relative backtrace from the tail of the stack. So, in the case
368 of frame_low == -1, assign and increment it. */
369 if (py_frame_low == -1)
372 result = mi_apply_ext_lang_frame_filter (get_current_frame (), flags,
373 print_values, current_uiout,
374 py_frame_low, frame_high);
377 /* Run the inbuilt backtrace if there are no filters registered, or
378 if "--no-frame-filters" has been specified from the command. */
379 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
381 /* Now let's print the frames up to frame_high, or until there are
382 frames in the stack. */
384 fi && (i <= frame_high || frame_high == -1);
385 i++, fi = get_prev_frame (fi))
388 ui_out_emit_tuple tuple_emitter (uiout, "frame");
389 uiout->field_int ("level", i);
390 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
395 /* Print a list of the local variables (including arguments) for the
396 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
397 or both names and values of the variables must be printed. See
398 parse_print_value for possible values. */
401 mi_cmd_stack_list_variables (const char *command, char **argv, int argc)
403 struct frame_info *frame;
405 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
406 enum print_values print_value;
408 int skip_unavailable = 0;
417 static const struct mi_opt opts[] =
419 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
420 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
427 /* Don't parse 'print-values' as an option. */
428 int opt = mi_getopt ("-stack-list-variables", argc - 1,
429 argv, opts, &oind, &oarg);
432 switch ((enum opt) opt)
434 case NO_FRAME_FILTERS:
437 case SKIP_UNAVAILABLE:
438 skip_unavailable = 1;
444 /* After the last option is parsed, there should be only
446 if (argc - oind != 1)
447 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
448 "[--skip-unavailable] PRINT_VALUES"));
450 frame = get_selected_frame (NULL);
451 print_value = mi_parse_print_values (argv[oind]);
453 if (! raw_arg && frame_filters)
455 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
457 result = mi_apply_ext_lang_frame_filter (frame, flags,
459 current_uiout, 0, 0);
462 /* Run the inbuilt backtrace if there are no filters registered, or
463 if "--no-frame-filters" has been specified from the command. */
464 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
466 list_args_or_locals (all, print_value, frame,
471 /* Print single local or argument. ARG must be already read in. For
472 WHAT and VALUES see list_args_or_locals.
474 Errors are printed as if they would be the parameter value. Use
475 zeroed ARG iff it should not be printed according to VALUES. If
476 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
479 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
480 enum print_values values, int skip_unavailable)
482 struct ui_out *uiout = current_uiout;
484 gdb_assert (!arg->val || !arg->error);
485 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
486 && arg->error == NULL)
487 || values == PRINT_SIMPLE_VALUES
488 || (values == PRINT_ALL_VALUES
489 && (arg->val != NULL || arg->error != NULL)));
490 gdb_assert (arg->entry_kind == print_entry_values_no
491 || (arg->entry_kind == print_entry_values_only
492 && (arg->val || arg->error)));
494 if (skip_unavailable && arg->val != NULL
495 && (value_entirely_unavailable (arg->val)
496 /* A scalar object that does not have all bits available is
497 also considered unavailable, because all bits contribute
498 to its representation. */
499 || (val_print_scalar_type_p (value_type (arg->val))
500 && !value_bytes_available (arg->val,
501 value_embedded_offset (arg->val),
502 TYPE_LENGTH (value_type (arg->val))))))
505 gdb::optional<ui_out_emit_tuple> tuple_emitter;
506 if (values != PRINT_NO_VALUES || what == all)
507 tuple_emitter.emplace (uiout, nullptr);
511 stb.puts (SYMBOL_PRINT_NAME (arg->sym));
512 if (arg->entry_kind == print_entry_values_only)
514 uiout->field_stream ("name", stb);
516 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
517 uiout->field_int ("arg", 1);
519 if (values == PRINT_SIMPLE_VALUES)
521 check_typedef (arg->sym->type);
522 type_print (arg->sym->type, "", &stb, -1);
523 uiout->field_stream ("type", stb);
526 if (arg->val || arg->error)
528 const char *error_message = NULL;
531 error_message = arg->error;
536 struct value_print_options opts;
538 get_no_prettyformat_print_options (&opts);
540 common_val_print (arg->val, &stb, 0, &opts,
541 language_def (SYMBOL_LANGUAGE (arg->sym)));
543 CATCH (except, RETURN_MASK_ERROR)
545 error_message = except.message;
549 if (error_message != NULL)
550 stb.printf (_("<error reading variable: %s>"), error_message);
551 uiout->field_stream ("value", stb);
555 /* Print a list of the objects for the frame FI in a certain form,
556 which is determined by VALUES. The objects can be locals,
557 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
558 is true, only print the arguments or local variables whose values
562 list_args_or_locals (enum what_to_list what, enum print_values values,
563 struct frame_info *fi, int skip_unavailable)
565 const struct block *block;
567 struct block_iterator iter;
569 const char *name_of_result;
570 struct ui_out *uiout = current_uiout;
572 block = get_frame_block (fi, 0);
577 name_of_result = "locals";
580 name_of_result = "args";
583 name_of_result = "variables";
586 internal_error (__FILE__, __LINE__,
587 "unexpected what_to_list: %d", (int) what);
590 ui_out_emit_list list_emitter (uiout, name_of_result);
594 ALL_BLOCK_SYMBOLS (block, iter, sym)
598 switch (SYMBOL_CLASS (sym))
601 case LOC_UNDEF: /* catches errors */
602 case LOC_CONST: /* constant */
603 case LOC_TYPEDEF: /* local typedef */
604 case LOC_LABEL: /* local label */
605 case LOC_BLOCK: /* local function */
606 case LOC_CONST_BYTES: /* loc. byte seq. */
607 case LOC_UNRESOLVED: /* unresolved static */
608 case LOC_OPTIMIZED_OUT: /* optimized out */
612 case LOC_ARG: /* argument */
613 case LOC_REF_ARG: /* reference arg */
614 case LOC_REGPARM_ADDR: /* indirect register arg */
615 case LOC_LOCAL: /* stack local */
616 case LOC_STATIC: /* static */
617 case LOC_REGISTER: /* register */
618 case LOC_COMPUTED: /* computed location */
621 else if (what == locals)
622 print_me = !SYMBOL_IS_ARGUMENT (sym);
624 print_me = SYMBOL_IS_ARGUMENT (sym);
630 struct frame_arg arg, entryarg;
632 if (SYMBOL_IS_ARGUMENT (sym))
633 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
638 gdb_assert (sym2 != NULL);
640 memset (&arg, 0, sizeof (arg));
642 arg.entry_kind = print_entry_values_no;
643 memset (&entryarg, 0, sizeof (entryarg));
645 entryarg.entry_kind = print_entry_values_no;
649 case PRINT_SIMPLE_VALUES:
650 type = check_typedef (sym2->type);
651 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
652 && TYPE_CODE (type) != TYPE_CODE_STRUCT
653 && TYPE_CODE (type) != TYPE_CODE_UNION)
655 case PRINT_ALL_VALUES:
656 if (SYMBOL_IS_ARGUMENT (sym))
657 read_frame_arg (sym2, fi, &arg, &entryarg);
659 read_frame_local (sym2, fi, &arg);
664 if (arg.entry_kind != print_entry_values_only)
665 list_arg_or_local (&arg, what, values, skip_unavailable);
666 if (entryarg.entry_kind != print_entry_values_no)
667 list_arg_or_local (&entryarg, what, values, skip_unavailable);
669 xfree (entryarg.error);
673 if (BLOCK_FUNCTION (block))
676 block = BLOCK_SUPERBLOCK (block);
681 mi_cmd_stack_select_frame (const char *command, char **argv, int argc)
683 if (argc == 0 || argc > 1)
684 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
686 select_frame_command (argv[0], 1 /* not used */ );
690 mi_cmd_stack_info_frame (const char *command, char **argv, int argc)
693 error (_("-stack-info-frame: No arguments allowed"));
695 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);