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 "exceptions.h"
35 #include "mi-getopt.h"
36 #include "python/python.h"
40 enum what_to_list { locals, arguments, all };
42 static void list_args_or_locals (enum what_to_list what,
43 enum print_values values,
44 struct frame_info *fi,
45 int skip_unavailable);
47 /* True if we want to allow Python-based frame filters. */
48 static int frame_filters = 0;
51 mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
54 error (_("-enable-frame-filters: no arguments allowed"));
58 /* Print a list of the stack frames. Args can be none, in which case
59 we want to print the whole backtrace, or a pair of numbers
60 specifying the frame numbers at which to start and stop the
61 display. If the two numbers are equal, a single frame will be
65 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
70 struct cleanup *cleanup_stack;
71 struct frame_info *fi;
72 enum py_bt_status result = PY_BT_ERROR;
79 static const struct mi_opt opts[] =
81 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
85 /* Parse arguments. In this instance we are just looking for
86 --no-frame-filters. */
90 int opt = mi_getopt ("-stack-list-frames", argc, argv,
94 switch ((enum opt) opt)
96 case NO_FRAME_FILTERS:
102 /* After the last option is parsed, there should either be low -
103 high range, or no further arguments. */
104 if ((argc - oind != 0) && (argc - oind != 2))
105 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
107 /* If there is a range, set it. */
108 if (argc - oind == 2)
110 frame_low = atoi (argv[0 + oind]);
111 frame_high = atoi (argv[1 + oind]);
115 /* Called with no arguments, it means we want the whole
121 /* Let's position fi on the frame at which to start the
122 display. Could be the innermost frame if the whole stack needs
123 displaying, or if frame_low is 0. */
124 for (i = 0, fi = get_current_frame ();
126 i++, fi = get_prev_frame (fi));
129 error (_("-stack-list-frames: Not enough frames in stack."));
131 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
133 if (! raw_arg && frame_filters)
135 int flags = PRINT_LEVEL | PRINT_FRAME_INFO;
136 int py_frame_low = frame_low;
138 /* We cannot pass -1 to frame_low, as that would signify a
139 relative backtrace from the tail of the stack. So, in the case
140 of frame_low == -1, assign and increment it. */
141 if (py_frame_low == -1)
144 result = apply_frame_filter (get_current_frame (), flags,
145 NO_VALUES, current_uiout,
146 py_frame_low, frame_high);
149 /* Run the inbuilt backtrace if there are no filters registered, or
150 if "--no-frame-filters" has been specified from the command. */
151 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
153 /* Now let's print the frames up to frame_high, or until there are
154 frames in the stack. */
156 fi && (i <= frame_high || frame_high == -1);
157 i++, fi = get_prev_frame (fi))
160 /* Print the location and the address always, even for level 0.
161 If args is 0, don't print the arguments. */
162 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
166 do_cleanups (cleanup_stack);
170 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
174 struct frame_info *fi;
177 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
180 frame_high = atoi (argv[0]);
182 /* Called with no arguments, it means we want the real depth of
186 for (i = 0, fi = get_current_frame ();
187 fi && (i < frame_high || frame_high == -1);
188 i++, fi = get_prev_frame (fi))
191 ui_out_field_int (current_uiout, "depth", i);
194 /* Print a list of the locals for the current frame. With argument of
195 0, print only the names, with argument of 1 print also the
199 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
201 struct frame_info *frame;
203 enum py_bt_status result = PY_BT_ERROR;
206 int skip_unavailable = 0;
217 static const struct mi_opt opts[] =
219 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
220 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
227 /* Don't parse 'print-values' as an option. */
228 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
233 switch ((enum opt) opt)
235 case NO_FRAME_FILTERS:
237 case SKIP_UNAVAILABLE:
238 skip_unavailable = 1;
244 /* After the last option is parsed, there should be only
246 if (argc - oind != 1)
247 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
248 "[--skip-unavailable] PRINT_VALUES"));
250 frame = get_selected_frame (NULL);
251 print_value = mi_parse_print_values (argv[oind]);
253 if (! raw_arg && frame_filters)
255 int flags = PRINT_LEVEL | PRINT_LOCALS;
257 result = apply_frame_filter (frame, flags, print_value,
258 current_uiout, 0, 0);
261 /* Run the inbuilt backtrace if there are no filters registered, or
262 if "--no-frame-filters" has been specified from the command. */
263 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
265 list_args_or_locals (locals, print_value, frame,
270 /* Print a list of the arguments for the current frame. With argument
271 of 0, print only the names, with argument of 1 print also the
275 mi_cmd_stack_list_args (char *command, char **argv, int argc)
280 struct frame_info *fi;
281 struct cleanup *cleanup_stack_args;
282 enum print_values print_values;
283 struct ui_out *uiout = current_uiout;
286 int skip_unavailable = 0;
287 enum py_bt_status result = PY_BT_ERROR;
293 static const struct mi_opt opts[] =
295 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
296 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
303 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
308 switch ((enum opt) opt)
310 case NO_FRAME_FILTERS:
313 case SKIP_UNAVAILABLE:
314 skip_unavailable = 1;
319 if (argc - oind != 1 && argc - oind != 3)
320 error (_("-stack-list-arguments: Usage: " \
321 "[--no-frame-filters] [--skip-unavailable] "
322 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
324 if (argc - oind == 3)
326 frame_low = atoi (argv[1 + oind]);
327 frame_high = atoi (argv[2 + oind]);
331 /* Called with no arguments, it means we want args for the whole
337 print_values = mi_parse_print_values (argv[oind]);
339 /* Let's position fi on the frame at which to start the
340 display. Could be the innermost frame if the whole stack needs
341 displaying, or if frame_low is 0. */
342 for (i = 0, fi = get_current_frame ();
344 i++, fi = get_prev_frame (fi));
347 error (_("-stack-list-arguments: Not enough frames in stack."));
350 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
352 if (! raw_arg && frame_filters)
354 int flags = PRINT_LEVEL | PRINT_ARGS;
355 int py_frame_low = frame_low;
357 /* We cannot pass -1 to frame_low, as that would signify a
358 relative backtrace from the tail of the stack. So, in the case
359 of frame_low == -1, assign and increment it. */
360 if (py_frame_low == -1)
363 result = apply_frame_filter (get_current_frame (), flags,
364 print_values, current_uiout,
365 py_frame_low, frame_high);
368 /* Run the inbuilt backtrace if there are no filters registered, or
369 if "--no-frame-filters" has been specified from the command. */
370 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
372 /* Now let's print the frames up to frame_high, or until there are
373 frames in the stack. */
375 fi && (i <= frame_high || frame_high == -1);
376 i++, fi = get_prev_frame (fi))
378 struct cleanup *cleanup_frame;
381 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
382 ui_out_field_int (uiout, "level", i);
383 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
384 do_cleanups (cleanup_frame);
387 do_cleanups (cleanup_stack_args);
390 /* Print a list of the local variables (including arguments) for the
391 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
392 or both names and values of the variables must be printed. See
393 parse_print_value for possible values. */
396 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
398 struct frame_info *frame;
400 enum py_bt_status result = PY_BT_ERROR;
403 int skip_unavailable = 0;
413 static const struct mi_opt opts[] =
415 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
416 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
423 /* Don't parse 'print-values' as an option. */
424 int opt = mi_getopt ("-stack-list-variables", argc - 1,
425 argv, opts, &oind, &oarg);
428 switch ((enum opt) opt)
430 case NO_FRAME_FILTERS:
433 case SKIP_UNAVAILABLE:
434 skip_unavailable = 1;
440 /* After the last option is parsed, there should be only
442 if (argc - oind != 1)
443 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
444 "[--skip-unavailable] PRINT_VALUES"));
446 frame = get_selected_frame (NULL);
447 print_value = mi_parse_print_values (argv[oind]);
449 if (! raw_arg && frame_filters)
451 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
453 result = apply_frame_filter (frame, flags, print_value,
454 current_uiout, 0, 0);
457 /* Run the inbuilt backtrace if there are no filters registered, or
458 if "--no-frame-filters" has been specified from the command. */
459 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
461 list_args_or_locals (all, print_value, frame,
466 /* Print single local or argument. ARG must be already read in. For
467 WHAT and VALUES see list_args_or_locals.
469 Errors are printed as if they would be the parameter value. Use
470 zeroed ARG iff it should not be printed according to VALUES. If
471 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
474 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
475 enum print_values values, int skip_unavailable)
477 struct cleanup *old_chain;
478 struct ui_out *uiout = current_uiout;
481 gdb_assert (!arg->val || !arg->error);
482 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
483 && arg->error == NULL)
484 || values == PRINT_SIMPLE_VALUES
485 || (values == PRINT_ALL_VALUES
486 && (arg->val != NULL || arg->error != NULL)));
487 gdb_assert (arg->entry_kind == print_entry_values_no
488 || (arg->entry_kind == print_entry_values_only
489 && (arg->val || arg->error)));
491 if (skip_unavailable && arg->val != NULL
492 && (value_entirely_unavailable (arg->val)
493 /* A scalar object that does not have all bits available is
494 also considered unavailable, because all bits contribute
495 to its representation. */
496 || (val_print_scalar_type_p (value_type (arg->val))
497 && !value_bytes_available (arg->val,
498 value_embedded_offset (arg->val),
499 TYPE_LENGTH (value_type (arg->val))))))
502 stb = mem_fileopen ();
503 old_chain = make_cleanup_ui_file_delete (stb);
505 if (values != PRINT_NO_VALUES || what == all)
506 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
508 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
509 if (arg->entry_kind == print_entry_values_only)
510 fputs_filtered ("@entry", stb);
511 ui_out_field_stream (uiout, "name", stb);
513 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
514 ui_out_field_int (uiout, "arg", 1);
516 if (values == PRINT_SIMPLE_VALUES)
518 check_typedef (arg->sym->type);
519 type_print (arg->sym->type, "", stb, -1);
520 ui_out_field_stream (uiout, "type", stb);
523 if (arg->val || arg->error)
525 volatile struct gdb_exception except;
528 except.message = arg->error;
531 /* TRY_CATCH has two statements, wrap it in a block. */
533 TRY_CATCH (except, RETURN_MASK_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)));
544 fprintf_filtered (stb, _("<error reading variable: %s>"),
546 ui_out_field_stream (uiout, "value", stb);
549 do_cleanups (old_chain);
552 /* Print a list of the objects for the frame FI in a certain form,
553 which is determined by VALUES. The objects can be locals,
554 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
555 is true, only print the arguments or local variables whose values
559 list_args_or_locals (enum what_to_list what, enum print_values values,
560 struct frame_info *fi, int skip_unavailable)
564 struct block_iterator iter;
565 struct cleanup *cleanup_list;
567 char *name_of_result;
568 struct ui_out *uiout = current_uiout;
570 block = get_frame_block (fi, 0);
575 name_of_result = "locals";
578 name_of_result = "args";
581 name_of_result = "variables";
584 internal_error (__FILE__, __LINE__,
585 "unexpected what_to_list: %d", (int) what);
588 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
592 ALL_BLOCK_SYMBOLS (block, iter, sym)
596 switch (SYMBOL_CLASS (sym))
599 case LOC_UNDEF: /* catches errors */
600 case LOC_CONST: /* constant */
601 case LOC_TYPEDEF: /* local typedef */
602 case LOC_LABEL: /* local label */
603 case LOC_BLOCK: /* local function */
604 case LOC_CONST_BYTES: /* loc. byte seq. */
605 case LOC_UNRESOLVED: /* unresolved static */
606 case LOC_OPTIMIZED_OUT: /* optimized out */
610 case LOC_ARG: /* argument */
611 case LOC_REF_ARG: /* reference arg */
612 case LOC_REGPARM_ADDR: /* indirect register arg */
613 case LOC_LOCAL: /* stack local */
614 case LOC_STATIC: /* static */
615 case LOC_REGISTER: /* register */
616 case LOC_COMPUTED: /* computed location */
619 else if (what == locals)
620 print_me = !SYMBOL_IS_ARGUMENT (sym);
622 print_me = SYMBOL_IS_ARGUMENT (sym);
628 struct frame_arg arg, entryarg;
630 if (SYMBOL_IS_ARGUMENT (sym))
631 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
636 gdb_assert (sym2 != NULL);
638 memset (&arg, 0, sizeof (arg));
640 arg.entry_kind = print_entry_values_no;
641 memset (&entryarg, 0, sizeof (entryarg));
643 entryarg.entry_kind = print_entry_values_no;
647 case PRINT_SIMPLE_VALUES:
648 type = check_typedef (sym2->type);
649 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
650 && TYPE_CODE (type) != TYPE_CODE_STRUCT
651 && TYPE_CODE (type) != TYPE_CODE_UNION)
653 case PRINT_ALL_VALUES:
654 if (SYMBOL_IS_ARGUMENT (sym))
655 read_frame_arg (sym2, fi, &arg, &entryarg);
657 read_frame_local (sym2, fi, &arg);
662 if (arg.entry_kind != print_entry_values_only)
663 list_arg_or_local (&arg, what, values, skip_unavailable);
664 if (entryarg.entry_kind != print_entry_values_no)
665 list_arg_or_local (&entryarg, what, values, skip_unavailable);
667 xfree (entryarg.error);
671 if (BLOCK_FUNCTION (block))
674 block = BLOCK_SUPERBLOCK (block);
676 do_cleanups (cleanup_list);
680 mi_cmd_stack_select_frame (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 (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);