1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2014 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"
38 enum what_to_list { locals, arguments, all };
40 static void list_args_or_locals (enum what_to_list what,
41 enum print_values values,
42 struct frame_info *fi,
43 int skip_unavailable);
45 /* True if we want to allow Python-based frame filters. */
46 static int frame_filters = 0;
49 mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
52 error (_("-enable-frame-filters: no arguments allowed"));
56 /* Print a list of the stack frames. Args can be none, in which case
57 we want to print the whole backtrace, or a pair of numbers
58 specifying the frame numbers at which to start and stop the
59 display. If the two numbers are equal, a single frame will be
63 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
68 struct cleanup *cleanup_stack;
69 struct frame_info *fi;
70 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
77 static const struct mi_opt opts[] =
79 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
83 /* Parse arguments. In this instance we are just looking for
84 --no-frame-filters. */
88 int opt = mi_getopt ("-stack-list-frames", argc, argv,
92 switch ((enum opt) opt)
94 case NO_FRAME_FILTERS:
100 /* After the last option is parsed, there should either be low -
101 high range, or no further arguments. */
102 if ((argc - oind != 0) && (argc - oind != 2))
103 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
105 /* If there is a range, set it. */
106 if (argc - oind == 2)
108 frame_low = atoi (argv[0 + oind]);
109 frame_high = atoi (argv[1 + oind]);
113 /* Called with no arguments, it means we want the whole
119 /* Let's position fi on the frame at which to start the
120 display. Could be the innermost frame if the whole stack needs
121 displaying, or if frame_low is 0. */
122 for (i = 0, fi = get_current_frame ();
124 i++, fi = get_prev_frame (fi));
127 error (_("-stack-list-frames: Not enough frames in stack."));
129 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
131 if (! raw_arg && frame_filters)
133 int flags = PRINT_LEVEL | PRINT_FRAME_INFO;
134 int py_frame_low = frame_low;
136 /* We cannot pass -1 to frame_low, as that would signify a
137 relative backtrace from the tail of the stack. So, in the case
138 of frame_low == -1, assign and increment it. */
139 if (py_frame_low == -1)
142 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
143 NO_VALUES, current_uiout,
144 py_frame_low, frame_high);
147 /* Run the inbuilt backtrace if there are no filters registered, or
148 if "--no-frame-filters" has been specified from the command. */
149 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
151 /* Now let's print the frames up to frame_high, or until there are
152 frames in the stack. */
154 fi && (i <= frame_high || frame_high == -1);
155 i++, fi = get_prev_frame (fi))
158 /* Print the location and the address always, even for level 0.
159 If args is 0, don't print the arguments. */
160 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
164 do_cleanups (cleanup_stack);
168 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
172 struct frame_info *fi;
175 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
178 frame_high = atoi (argv[0]);
180 /* Called with no arguments, it means we want the real depth of
184 for (i = 0, fi = get_current_frame ();
185 fi && (i < frame_high || frame_high == -1);
186 i++, fi = get_prev_frame (fi))
189 ui_out_field_int (current_uiout, "depth", i);
192 /* Print a list of the locals for the current frame. With argument of
193 0, print only the names, with argument of 1 print also the
197 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
199 struct frame_info *frame;
201 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
204 int skip_unavailable = 0;
215 static const struct mi_opt opts[] =
217 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
218 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
225 /* Don't parse 'print-values' as an option. */
226 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
231 switch ((enum opt) opt)
233 case NO_FRAME_FILTERS:
235 case SKIP_UNAVAILABLE:
236 skip_unavailable = 1;
242 /* After the last option is parsed, there should be only
244 if (argc - oind != 1)
245 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
246 "[--skip-unavailable] PRINT_VALUES"));
248 frame = get_selected_frame (NULL);
249 print_value = mi_parse_print_values (argv[oind]);
251 if (! raw_arg && frame_filters)
253 int flags = PRINT_LEVEL | PRINT_LOCALS;
255 result = apply_ext_lang_frame_filter (frame, flags, print_value,
256 current_uiout, 0, 0);
259 /* Run the inbuilt backtrace if there are no filters registered, or
260 if "--no-frame-filters" has been specified from the command. */
261 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
263 list_args_or_locals (locals, print_value, frame,
268 /* Print a list of the arguments for the current frame. With argument
269 of 0, print only the names, with argument of 1 print also the
273 mi_cmd_stack_list_args (char *command, char **argv, int argc)
278 struct frame_info *fi;
279 struct cleanup *cleanup_stack_args;
280 enum print_values print_values;
281 struct ui_out *uiout = current_uiout;
284 int skip_unavailable = 0;
285 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
291 static const struct mi_opt opts[] =
293 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
294 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
301 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
306 switch ((enum opt) opt)
308 case NO_FRAME_FILTERS:
311 case SKIP_UNAVAILABLE:
312 skip_unavailable = 1;
317 if (argc - oind != 1 && argc - oind != 3)
318 error (_("-stack-list-arguments: Usage: " \
319 "[--no-frame-filters] [--skip-unavailable] "
320 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
322 if (argc - oind == 3)
324 frame_low = atoi (argv[1 + oind]);
325 frame_high = atoi (argv[2 + oind]);
329 /* Called with no arguments, it means we want args for the whole
335 print_values = mi_parse_print_values (argv[oind]);
337 /* Let's position fi on the frame at which to start the
338 display. Could be the innermost frame if the whole stack needs
339 displaying, or if frame_low is 0. */
340 for (i = 0, fi = get_current_frame ();
342 i++, fi = get_prev_frame (fi));
345 error (_("-stack-list-arguments: Not enough frames in stack."));
348 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
350 if (! raw_arg && frame_filters)
352 int flags = PRINT_LEVEL | PRINT_ARGS;
353 int py_frame_low = frame_low;
355 /* We cannot pass -1 to frame_low, as that would signify a
356 relative backtrace from the tail of the stack. So, in the case
357 of frame_low == -1, assign and increment it. */
358 if (py_frame_low == -1)
361 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
362 print_values, current_uiout,
363 py_frame_low, frame_high);
366 /* Run the inbuilt backtrace if there are no filters registered, or
367 if "--no-frame-filters" has been specified from the command. */
368 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
370 /* Now let's print the frames up to frame_high, or until there are
371 frames in the stack. */
373 fi && (i <= frame_high || frame_high == -1);
374 i++, fi = get_prev_frame (fi))
376 struct cleanup *cleanup_frame;
379 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
380 ui_out_field_int (uiout, "level", i);
381 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
382 do_cleanups (cleanup_frame);
385 do_cleanups (cleanup_stack_args);
388 /* Print a list of the local variables (including arguments) for the
389 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
390 or both names and values of the variables must be printed. See
391 parse_print_value for possible values. */
394 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
396 struct frame_info *frame;
398 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
401 int skip_unavailable = 0;
411 static const struct mi_opt opts[] =
413 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
414 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
421 /* Don't parse 'print-values' as an option. */
422 int opt = mi_getopt ("-stack-list-variables", argc - 1,
423 argv, opts, &oind, &oarg);
426 switch ((enum opt) opt)
428 case NO_FRAME_FILTERS:
431 case SKIP_UNAVAILABLE:
432 skip_unavailable = 1;
438 /* After the last option is parsed, there should be only
440 if (argc - oind != 1)
441 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
442 "[--skip-unavailable] PRINT_VALUES"));
444 frame = get_selected_frame (NULL);
445 print_value = mi_parse_print_values (argv[oind]);
447 if (! raw_arg && frame_filters)
449 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
451 result = apply_ext_lang_frame_filter (frame, flags, print_value,
452 current_uiout, 0, 0);
455 /* Run the inbuilt backtrace if there are no filters registered, or
456 if "--no-frame-filters" has been specified from the command. */
457 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
459 list_args_or_locals (all, print_value, frame,
464 /* Print single local or argument. ARG must be already read in. For
465 WHAT and VALUES see list_args_or_locals.
467 Errors are printed as if they would be the parameter value. Use
468 zeroed ARG iff it should not be printed according to VALUES. If
469 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
472 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
473 enum print_values values, int skip_unavailable)
475 struct cleanup *old_chain;
476 struct ui_out *uiout = current_uiout;
479 gdb_assert (!arg->val || !arg->error);
480 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
481 && arg->error == NULL)
482 || values == PRINT_SIMPLE_VALUES
483 || (values == PRINT_ALL_VALUES
484 && (arg->val != NULL || arg->error != NULL)));
485 gdb_assert (arg->entry_kind == print_entry_values_no
486 || (arg->entry_kind == print_entry_values_only
487 && (arg->val || arg->error)));
489 if (skip_unavailable && arg->val != NULL
490 && (value_entirely_unavailable (arg->val)
491 /* A scalar object that does not have all bits available is
492 also considered unavailable, because all bits contribute
493 to its representation. */
494 || (val_print_scalar_type_p (value_type (arg->val))
495 && !value_bytes_available (arg->val,
496 value_embedded_offset (arg->val),
497 TYPE_LENGTH (value_type (arg->val))))))
500 stb = mem_fileopen ();
501 old_chain = make_cleanup_ui_file_delete (stb);
503 if (values != PRINT_NO_VALUES || what == all)
504 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
506 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
507 if (arg->entry_kind == print_entry_values_only)
508 fputs_filtered ("@entry", stb);
509 ui_out_field_stream (uiout, "name", stb);
511 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
512 ui_out_field_int (uiout, "arg", 1);
514 if (values == PRINT_SIMPLE_VALUES)
516 check_typedef (arg->sym->type);
517 type_print (arg->sym->type, "", stb, -1);
518 ui_out_field_stream (uiout, "type", stb);
521 if (arg->val || arg->error)
523 volatile struct gdb_exception except;
526 except.message = arg->error;
529 /* TRY_CATCH has two statements, wrap it in a block. */
531 TRY_CATCH (except, RETURN_MASK_ERROR)
533 struct value_print_options opts;
535 get_no_prettyformat_print_options (&opts);
537 common_val_print (arg->val, stb, 0, &opts,
538 language_def (SYMBOL_LANGUAGE (arg->sym)));
542 fprintf_filtered (stb, _("<error reading variable: %s>"),
544 ui_out_field_stream (uiout, "value", stb);
547 do_cleanups (old_chain);
550 /* Print a list of the objects for the frame FI in a certain form,
551 which is determined by VALUES. The objects can be locals,
552 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
553 is true, only print the arguments or local variables whose values
557 list_args_or_locals (enum what_to_list what, enum print_values values,
558 struct frame_info *fi, int skip_unavailable)
560 const struct block *block;
562 struct block_iterator iter;
563 struct cleanup *cleanup_list;
565 char *name_of_result;
566 struct ui_out *uiout = current_uiout;
568 block = get_frame_block (fi, 0);
573 name_of_result = "locals";
576 name_of_result = "args";
579 name_of_result = "variables";
582 internal_error (__FILE__, __LINE__,
583 "unexpected what_to_list: %d", (int) what);
586 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
590 ALL_BLOCK_SYMBOLS (block, iter, sym)
594 switch (SYMBOL_CLASS (sym))
597 case LOC_UNDEF: /* catches errors */
598 case LOC_CONST: /* constant */
599 case LOC_TYPEDEF: /* local typedef */
600 case LOC_LABEL: /* local label */
601 case LOC_BLOCK: /* local function */
602 case LOC_CONST_BYTES: /* loc. byte seq. */
603 case LOC_UNRESOLVED: /* unresolved static */
604 case LOC_OPTIMIZED_OUT: /* optimized out */
608 case LOC_ARG: /* argument */
609 case LOC_REF_ARG: /* reference arg */
610 case LOC_REGPARM_ADDR: /* indirect register arg */
611 case LOC_LOCAL: /* stack local */
612 case LOC_STATIC: /* static */
613 case LOC_REGISTER: /* register */
614 case LOC_COMPUTED: /* computed location */
617 else if (what == locals)
618 print_me = !SYMBOL_IS_ARGUMENT (sym);
620 print_me = SYMBOL_IS_ARGUMENT (sym);
626 struct frame_arg arg, entryarg;
628 if (SYMBOL_IS_ARGUMENT (sym))
629 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
634 gdb_assert (sym2 != NULL);
636 memset (&arg, 0, sizeof (arg));
638 arg.entry_kind = print_entry_values_no;
639 memset (&entryarg, 0, sizeof (entryarg));
641 entryarg.entry_kind = print_entry_values_no;
645 case PRINT_SIMPLE_VALUES:
646 type = check_typedef (sym2->type);
647 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
648 && TYPE_CODE (type) != TYPE_CODE_STRUCT
649 && TYPE_CODE (type) != TYPE_CODE_UNION)
651 case PRINT_ALL_VALUES:
652 if (SYMBOL_IS_ARGUMENT (sym))
653 read_frame_arg (sym2, fi, &arg, &entryarg);
655 read_frame_local (sym2, fi, &arg);
660 if (arg.entry_kind != print_entry_values_only)
661 list_arg_or_local (&arg, what, values, skip_unavailable);
662 if (entryarg.entry_kind != print_entry_values_no)
663 list_arg_or_local (&entryarg, what, values, skip_unavailable);
665 xfree (entryarg.error);
669 if (BLOCK_FUNCTION (block))
672 block = BLOCK_SUPERBLOCK (block);
674 do_cleanups (cleanup_list);
678 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
680 if (argc == 0 || argc > 1)
681 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
683 select_frame_command (argv[0], 1 /* not used */ );
687 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
690 error (_("-stack-info-frame: No arguments allowed"));
692 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);