1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2017 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, int flags,
61 enum print_values print_values,
63 int frame_low, int frame_high)
65 /* ext_lang_frame_args's MI options are compatible with MI print
67 return apply_ext_lang_frame_filter (frame, flags,
68 (enum ext_lang_frame_args) print_values,
70 frame_low, frame_high);
73 /* Print a list of the stack frames. Args can be none, in which case
74 we want to print the whole backtrace, or a pair of numbers
75 specifying the frame numbers at which to start and stop the
76 display. If the two numbers are equal, a single frame will be
80 mi_cmd_stack_list_frames (const char *command, char **argv, int argc)
85 struct frame_info *fi;
86 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
93 static const struct mi_opt opts[] =
95 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
99 /* Parse arguments. In this instance we are just looking for
100 --no-frame-filters. */
104 int opt = mi_getopt ("-stack-list-frames", argc, argv,
108 switch ((enum opt) opt)
110 case NO_FRAME_FILTERS:
116 /* After the last option is parsed, there should either be low -
117 high range, or no further arguments. */
118 if ((argc - oind != 0) && (argc - oind != 2))
119 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
121 /* If there is a range, set it. */
122 if (argc - oind == 2)
124 frame_low = atoi (argv[0 + oind]);
125 frame_high = atoi (argv[1 + oind]);
129 /* Called with no arguments, it means we want the whole
135 /* Let's position fi on the frame at which to start the
136 display. Could be the innermost frame if the whole stack needs
137 displaying, or if frame_low is 0. */
138 for (i = 0, fi = get_current_frame ();
140 i++, fi = get_prev_frame (fi));
143 error (_("-stack-list-frames: Not enough frames in stack."));
145 ui_out_emit_list list_emitter (current_uiout, "stack");
147 if (! raw_arg && frame_filters)
149 int flags = PRINT_LEVEL | PRINT_FRAME_INFO;
150 int py_frame_low = frame_low;
152 /* We cannot pass -1 to frame_low, as that would signify a
153 relative backtrace from the tail of the stack. So, in the case
154 of frame_low == -1, assign and increment it. */
155 if (py_frame_low == -1)
158 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
159 NO_VALUES, current_uiout,
160 py_frame_low, frame_high);
163 /* Run the inbuilt backtrace if there are no filters registered, or
164 if "--no-frame-filters" has been specified from the command. */
165 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
167 /* Now let's print the frames up to frame_high, or until there are
168 frames in the stack. */
170 fi && (i <= frame_high || frame_high == -1);
171 i++, fi = get_prev_frame (fi))
174 /* Print the location and the address always, even for level 0.
175 If args is 0, don't print the arguments. */
176 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
182 mi_cmd_stack_info_depth (const char *command, char **argv, int argc)
186 struct frame_info *fi;
189 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
192 frame_high = atoi (argv[0]);
194 /* Called with no arguments, it means we want the real depth of
198 for (i = 0, fi = get_current_frame ();
199 fi && (i < frame_high || frame_high == -1);
200 i++, fi = get_prev_frame (fi))
203 current_uiout->field_int ("depth", i);
206 /* Print a list of the locals for the current frame. With argument of
207 0, print only the names, with argument of 1 print also the
211 mi_cmd_stack_list_locals (const char *command, char **argv, int argc)
213 struct frame_info *frame;
215 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
216 enum print_values print_value;
218 int skip_unavailable = 0;
227 static const struct mi_opt opts[] =
229 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
230 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
237 /* Don't parse 'print-values' as an option. */
238 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
243 switch ((enum opt) opt)
245 case NO_FRAME_FILTERS:
247 case SKIP_UNAVAILABLE:
248 skip_unavailable = 1;
254 /* After the last option is parsed, there should be only
256 if (argc - oind != 1)
257 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
258 "[--skip-unavailable] PRINT_VALUES"));
260 frame = get_selected_frame (NULL);
261 print_value = mi_parse_print_values (argv[oind]);
263 if (! raw_arg && frame_filters)
265 int flags = PRINT_LEVEL | PRINT_LOCALS;
267 result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
268 current_uiout, 0, 0);
271 /* Run the inbuilt backtrace if there are no filters registered, or
272 if "--no-frame-filters" has been specified from the command. */
273 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
275 list_args_or_locals (locals, print_value, frame,
280 /* Print a list of the arguments for the current frame. With argument
281 of 0, print only the names, with argument of 1 print also the
285 mi_cmd_stack_list_args (const char *command, char **argv, int argc)
290 struct frame_info *fi;
291 enum print_values print_values;
292 struct ui_out *uiout = current_uiout;
295 int skip_unavailable = 0;
296 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
302 static const struct mi_opt opts[] =
304 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
305 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
312 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
317 switch ((enum opt) opt)
319 case NO_FRAME_FILTERS:
322 case SKIP_UNAVAILABLE:
323 skip_unavailable = 1;
328 if (argc - oind != 1 && argc - oind != 3)
329 error (_("-stack-list-arguments: Usage: " \
330 "[--no-frame-filters] [--skip-unavailable] "
331 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
333 if (argc - oind == 3)
335 frame_low = atoi (argv[1 + oind]);
336 frame_high = atoi (argv[2 + oind]);
340 /* Called with no arguments, it means we want args for the whole
346 print_values = mi_parse_print_values (argv[oind]);
348 /* Let's position fi on the frame at which to start the
349 display. Could be the innermost frame if the whole stack needs
350 displaying, or if frame_low is 0. */
351 for (i = 0, fi = get_current_frame ();
353 i++, fi = get_prev_frame (fi));
356 error (_("-stack-list-arguments: Not enough frames in stack."));
358 ui_out_emit_list list_emitter (uiout, "stack-args");
360 if (! raw_arg && frame_filters)
362 int flags = PRINT_LEVEL | PRINT_ARGS;
363 int py_frame_low = frame_low;
365 /* We cannot pass -1 to frame_low, as that would signify a
366 relative backtrace from the tail of the stack. So, in the case
367 of frame_low == -1, assign and increment it. */
368 if (py_frame_low == -1)
371 result = mi_apply_ext_lang_frame_filter (get_current_frame (), flags,
372 print_values, current_uiout,
373 py_frame_low, frame_high);
376 /* Run the inbuilt backtrace if there are no filters registered, or
377 if "--no-frame-filters" has been specified from the command. */
378 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
380 /* Now let's print the frames up to frame_high, or until there are
381 frames in the stack. */
383 fi && (i <= frame_high || frame_high == -1);
384 i++, fi = get_prev_frame (fi))
387 ui_out_emit_tuple tuple_emitter (uiout, "frame");
388 uiout->field_int ("level", i);
389 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
394 /* Print a list of the local variables (including arguments) for the
395 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
396 or both names and values of the variables must be printed. See
397 parse_print_value for possible values. */
400 mi_cmd_stack_list_variables (const char *command, char **argv, int argc)
402 struct frame_info *frame;
404 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
405 enum print_values print_value;
407 int skip_unavailable = 0;
416 static const struct mi_opt opts[] =
418 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
419 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
426 /* Don't parse 'print-values' as an option. */
427 int opt = mi_getopt ("-stack-list-variables", argc - 1,
428 argv, opts, &oind, &oarg);
431 switch ((enum opt) opt)
433 case NO_FRAME_FILTERS:
436 case SKIP_UNAVAILABLE:
437 skip_unavailable = 1;
443 /* After the last option is parsed, there should be only
445 if (argc - oind != 1)
446 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
447 "[--skip-unavailable] PRINT_VALUES"));
449 frame = get_selected_frame (NULL);
450 print_value = mi_parse_print_values (argv[oind]);
452 if (! raw_arg && frame_filters)
454 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
456 result = mi_apply_ext_lang_frame_filter (frame, flags,
458 current_uiout, 0, 0);
461 /* Run the inbuilt backtrace if there are no filters registered, or
462 if "--no-frame-filters" has been specified from the command. */
463 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
465 list_args_or_locals (all, print_value, frame,
470 /* Print single local or argument. ARG must be already read in. For
471 WHAT and VALUES see list_args_or_locals.
473 Errors are printed as if they would be the parameter value. Use
474 zeroed ARG iff it should not be printed according to VALUES. If
475 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
478 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
479 enum print_values values, int skip_unavailable)
481 struct ui_out *uiout = current_uiout;
483 gdb_assert (!arg->val || !arg->error);
484 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
485 && arg->error == NULL)
486 || values == PRINT_SIMPLE_VALUES
487 || (values == PRINT_ALL_VALUES
488 && (arg->val != NULL || arg->error != NULL)));
489 gdb_assert (arg->entry_kind == print_entry_values_no
490 || (arg->entry_kind == print_entry_values_only
491 && (arg->val || arg->error)));
493 if (skip_unavailable && arg->val != NULL
494 && (value_entirely_unavailable (arg->val)
495 /* A scalar object that does not have all bits available is
496 also considered unavailable, because all bits contribute
497 to its representation. */
498 || (val_print_scalar_type_p (value_type (arg->val))
499 && !value_bytes_available (arg->val,
500 value_embedded_offset (arg->val),
501 TYPE_LENGTH (value_type (arg->val))))))
504 gdb::optional<ui_out_emit_tuple> tuple_emitter;
505 if (values != PRINT_NO_VALUES || what == all)
506 tuple_emitter.emplace (uiout, nullptr);
510 stb.puts (SYMBOL_PRINT_NAME (arg->sym));
511 if (arg->entry_kind == print_entry_values_only)
513 uiout->field_stream ("name", stb);
515 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
516 uiout->field_int ("arg", 1);
518 if (values == PRINT_SIMPLE_VALUES)
520 check_typedef (arg->sym->type);
521 type_print (arg->sym->type, "", &stb, -1);
522 uiout->field_stream ("type", stb);
525 if (arg->val || arg->error)
527 const char *error_message = NULL;
530 error_message = arg->error;
535 struct value_print_options opts;
537 get_no_prettyformat_print_options (&opts);
539 common_val_print (arg->val, &stb, 0, &opts,
540 language_def (SYMBOL_LANGUAGE (arg->sym)));
542 CATCH (except, RETURN_MASK_ERROR)
544 error_message = except.message;
548 if (error_message != NULL)
549 stb.printf (_("<error reading variable: %s>"), error_message);
550 uiout->field_stream ("value", stb);
554 /* Print a list of the objects for the frame FI in a certain form,
555 which is determined by VALUES. The objects can be locals,
556 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
557 is true, only print the arguments or local variables whose values
561 list_args_or_locals (enum what_to_list what, enum print_values values,
562 struct frame_info *fi, int skip_unavailable)
564 const struct block *block;
566 struct block_iterator iter;
568 const char *name_of_result;
569 struct ui_out *uiout = current_uiout;
571 block = get_frame_block (fi, 0);
576 name_of_result = "locals";
579 name_of_result = "args";
582 name_of_result = "variables";
585 internal_error (__FILE__, __LINE__,
586 "unexpected what_to_list: %d", (int) what);
589 ui_out_emit_list list_emitter (uiout, name_of_result);
593 ALL_BLOCK_SYMBOLS (block, iter, sym)
597 switch (SYMBOL_CLASS (sym))
600 case LOC_UNDEF: /* catches errors */
601 case LOC_CONST: /* constant */
602 case LOC_TYPEDEF: /* local typedef */
603 case LOC_LABEL: /* local label */
604 case LOC_BLOCK: /* local function */
605 case LOC_CONST_BYTES: /* loc. byte seq. */
606 case LOC_UNRESOLVED: /* unresolved static */
607 case LOC_OPTIMIZED_OUT: /* optimized out */
611 case LOC_ARG: /* argument */
612 case LOC_REF_ARG: /* reference arg */
613 case LOC_REGPARM_ADDR: /* indirect register arg */
614 case LOC_LOCAL: /* stack local */
615 case LOC_STATIC: /* static */
616 case LOC_REGISTER: /* register */
617 case LOC_COMPUTED: /* computed location */
620 else if (what == locals)
621 print_me = !SYMBOL_IS_ARGUMENT (sym);
623 print_me = SYMBOL_IS_ARGUMENT (sym);
629 struct frame_arg arg, entryarg;
631 if (SYMBOL_IS_ARGUMENT (sym))
632 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
637 gdb_assert (sym2 != NULL);
639 memset (&arg, 0, sizeof (arg));
641 arg.entry_kind = print_entry_values_no;
642 memset (&entryarg, 0, sizeof (entryarg));
644 entryarg.entry_kind = print_entry_values_no;
648 case PRINT_SIMPLE_VALUES:
649 type = check_typedef (sym2->type);
650 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
651 && TYPE_CODE (type) != TYPE_CODE_STRUCT
652 && TYPE_CODE (type) != TYPE_CODE_UNION)
654 case PRINT_ALL_VALUES:
655 if (SYMBOL_IS_ARGUMENT (sym))
656 read_frame_arg (sym2, fi, &arg, &entryarg);
658 read_frame_local (sym2, fi, &arg);
663 if (arg.entry_kind != print_entry_values_only)
664 list_arg_or_local (&arg, what, values, skip_unavailable);
665 if (entryarg.entry_kind != print_entry_values_no)
666 list_arg_or_local (&entryarg, what, values, skip_unavailable);
668 xfree (entryarg.error);
672 if (BLOCK_FUNCTION (block))
675 block = BLOCK_SUPERBLOCK (block);
680 mi_cmd_stack_select_frame (const char *command, char **argv, int argc)
682 if (argc == 0 || argc > 1)
683 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
685 select_frame_command (argv[0], 1 /* not used */ );
689 mi_cmd_stack_info_frame (const char *command, char **argv, int argc)
692 error (_("-stack-info-frame: No arguments allowed"));
694 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);