1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2015 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 /* Like apply_ext_lang_frame_filter, but take a print_values */
58 static enum ext_lang_bt_status
59 mi_apply_ext_lang_frame_filter (struct frame_info *frame, int flags,
60 enum print_values print_values,
62 int frame_low, int frame_high)
64 /* ext_lang_frame_args's MI options are compatible with MI print
66 return apply_ext_lang_frame_filter (frame, flags,
67 (enum ext_lang_frame_args) print_values,
69 frame_low, frame_high);
72 /* Print a list of the stack frames. Args can be none, in which case
73 we want to print the whole backtrace, or a pair of numbers
74 specifying the frame numbers at which to start and stop the
75 display. If the two numbers are equal, a single frame will be
79 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
84 struct cleanup *cleanup_stack;
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 cleanup_stack = make_cleanup_ui_out_list_begin_end (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);
180 do_cleanups (cleanup_stack);
184 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
188 struct frame_info *fi;
191 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
194 frame_high = atoi (argv[0]);
196 /* Called with no arguments, it means we want the real depth of
200 for (i = 0, fi = get_current_frame ();
201 fi && (i < frame_high || frame_high == -1);
202 i++, fi = get_prev_frame (fi))
205 ui_out_field_int (current_uiout, "depth", i);
208 /* Print a list of the locals for the current frame. With argument of
209 0, print only the names, with argument of 1 print also the
213 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
215 struct frame_info *frame;
217 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
220 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:
251 case SKIP_UNAVAILABLE:
252 skip_unavailable = 1;
258 /* After the last option is parsed, there should be only
260 if (argc - oind != 1)
261 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
262 "[--skip-unavailable] PRINT_VALUES"));
264 frame = get_selected_frame (NULL);
265 print_value = mi_parse_print_values (argv[oind]);
267 if (! raw_arg && frame_filters)
269 int flags = PRINT_LEVEL | PRINT_LOCALS;
271 result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
272 current_uiout, 0, 0);
275 /* Run the inbuilt backtrace if there are no filters registered, or
276 if "--no-frame-filters" has been specified from the command. */
277 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
279 list_args_or_locals (locals, print_value, frame,
284 /* Print a list of the arguments for the current frame. With argument
285 of 0, print only the names, with argument of 1 print also the
289 mi_cmd_stack_list_args (char *command, char **argv, int argc)
294 struct frame_info *fi;
295 struct cleanup *cleanup_stack_args;
296 enum print_values print_values;
297 struct ui_out *uiout = current_uiout;
300 int skip_unavailable = 0;
301 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
307 static const struct mi_opt opts[] =
309 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
310 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
317 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
322 switch ((enum opt) opt)
324 case NO_FRAME_FILTERS:
327 case SKIP_UNAVAILABLE:
328 skip_unavailable = 1;
333 if (argc - oind != 1 && argc - oind != 3)
334 error (_("-stack-list-arguments: Usage: " \
335 "[--no-frame-filters] [--skip-unavailable] "
336 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
338 if (argc - oind == 3)
340 frame_low = atoi (argv[1 + oind]);
341 frame_high = atoi (argv[2 + oind]);
345 /* Called with no arguments, it means we want args for the whole
351 print_values = mi_parse_print_values (argv[oind]);
353 /* Let's position fi on the frame at which to start the
354 display. Could be the innermost frame if the whole stack needs
355 displaying, or if frame_low is 0. */
356 for (i = 0, fi = get_current_frame ();
358 i++, fi = get_prev_frame (fi));
361 error (_("-stack-list-arguments: Not enough frames in stack."));
364 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
366 if (! raw_arg && frame_filters)
368 int 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))
392 struct cleanup *cleanup_frame;
395 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
396 ui_out_field_int (uiout, "level", i);
397 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
398 do_cleanups (cleanup_frame);
401 do_cleanups (cleanup_stack_args);
404 /* Print a list of the local variables (including arguments) for the
405 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
406 or both names and values of the variables must be printed. See
407 parse_print_value for possible values. */
410 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
412 struct frame_info *frame;
414 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
417 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 int 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 (all, print_value, frame,
481 /* Print single local or argument. ARG must be already read in. For
482 WHAT and VALUES see list_args_or_locals.
484 Errors are printed as if they would be the parameter value. Use
485 zeroed ARG iff it should not be printed according to VALUES. If
486 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
489 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
490 enum print_values values, int skip_unavailable)
492 struct cleanup *old_chain;
493 struct ui_out *uiout = current_uiout;
496 gdb_assert (!arg->val || !arg->error);
497 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
498 && arg->error == NULL)
499 || values == PRINT_SIMPLE_VALUES
500 || (values == PRINT_ALL_VALUES
501 && (arg->val != NULL || arg->error != NULL)));
502 gdb_assert (arg->entry_kind == print_entry_values_no
503 || (arg->entry_kind == print_entry_values_only
504 && (arg->val || arg->error)));
506 if (skip_unavailable && arg->val != NULL
507 && (value_entirely_unavailable (arg->val)
508 /* A scalar object that does not have all bits available is
509 also considered unavailable, because all bits contribute
510 to its representation. */
511 || (val_print_scalar_type_p (value_type (arg->val))
512 && !value_bytes_available (arg->val,
513 value_embedded_offset (arg->val),
514 TYPE_LENGTH (value_type (arg->val))))))
517 stb = mem_fileopen ();
518 old_chain = make_cleanup_ui_file_delete (stb);
520 if (values != PRINT_NO_VALUES || what == all)
521 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
523 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
524 if (arg->entry_kind == print_entry_values_only)
525 fputs_filtered ("@entry", stb);
526 ui_out_field_stream (uiout, "name", stb);
528 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
529 ui_out_field_int (uiout, "arg", 1);
531 if (values == PRINT_SIMPLE_VALUES)
533 check_typedef (arg->sym->type);
534 type_print (arg->sym->type, "", stb, -1);
535 ui_out_field_stream (uiout, "type", stb);
538 if (arg->val || arg->error)
540 const char *error_message = NULL;
543 error_message = arg->error;
548 struct value_print_options opts;
550 get_no_prettyformat_print_options (&opts);
552 common_val_print (arg->val, stb, 0, &opts,
553 language_def (SYMBOL_LANGUAGE (arg->sym)));
555 CATCH (except, RETURN_MASK_ERROR)
557 error_message = except.message;
561 if (error_message != NULL)
562 fprintf_filtered (stb, _("<error reading variable: %s>"),
564 ui_out_field_stream (uiout, "value", stb);
567 do_cleanups (old_chain);
570 /* Print a list of the objects for the frame FI in a certain form,
571 which is determined by VALUES. The objects can be locals,
572 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
573 is true, only print the arguments or local variables whose values
577 list_args_or_locals (enum what_to_list what, enum print_values values,
578 struct frame_info *fi, int skip_unavailable)
580 const struct block *block;
582 struct block_iterator iter;
583 struct cleanup *cleanup_list;
585 char *name_of_result;
586 struct ui_out *uiout = current_uiout;
588 block = get_frame_block (fi, 0);
593 name_of_result = "locals";
596 name_of_result = "args";
599 name_of_result = "variables";
602 internal_error (__FILE__, __LINE__,
603 "unexpected what_to_list: %d", (int) what);
606 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
610 ALL_BLOCK_SYMBOLS (block, iter, sym)
614 switch (SYMBOL_CLASS (sym))
617 case LOC_UNDEF: /* catches errors */
618 case LOC_CONST: /* constant */
619 case LOC_TYPEDEF: /* local typedef */
620 case LOC_LABEL: /* local label */
621 case LOC_BLOCK: /* local function */
622 case LOC_CONST_BYTES: /* loc. byte seq. */
623 case LOC_UNRESOLVED: /* unresolved static */
624 case LOC_OPTIMIZED_OUT: /* optimized out */
628 case LOC_ARG: /* argument */
629 case LOC_REF_ARG: /* reference arg */
630 case LOC_REGPARM_ADDR: /* indirect register arg */
631 case LOC_LOCAL: /* stack local */
632 case LOC_STATIC: /* static */
633 case LOC_REGISTER: /* register */
634 case LOC_COMPUTED: /* computed location */
637 else if (what == locals)
638 print_me = !SYMBOL_IS_ARGUMENT (sym);
640 print_me = SYMBOL_IS_ARGUMENT (sym);
646 struct frame_arg arg, entryarg;
648 if (SYMBOL_IS_ARGUMENT (sym))
649 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
654 gdb_assert (sym2 != NULL);
656 memset (&arg, 0, sizeof (arg));
658 arg.entry_kind = print_entry_values_no;
659 memset (&entryarg, 0, sizeof (entryarg));
661 entryarg.entry_kind = print_entry_values_no;
665 case PRINT_SIMPLE_VALUES:
666 type = check_typedef (sym2->type);
667 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
668 && TYPE_CODE (type) != TYPE_CODE_STRUCT
669 && TYPE_CODE (type) != TYPE_CODE_UNION)
671 case PRINT_ALL_VALUES:
672 if (SYMBOL_IS_ARGUMENT (sym))
673 read_frame_arg (sym2, fi, &arg, &entryarg);
675 read_frame_local (sym2, fi, &arg);
680 if (arg.entry_kind != print_entry_values_only)
681 list_arg_or_local (&arg, what, values, skip_unavailable);
682 if (entryarg.entry_kind != print_entry_values_no)
683 list_arg_or_local (&entryarg, what, values, skip_unavailable);
685 xfree (entryarg.error);
689 if (BLOCK_FUNCTION (block))
692 block = BLOCK_SUPERBLOCK (block);
694 do_cleanups (cleanup_list);
698 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
700 if (argc == 0 || argc > 1)
701 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
703 select_frame_command (argv[0], 1 /* not used */ );
707 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
710 error (_("-stack-info-frame: No arguments allowed"));
712 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);