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"
32 #include "exceptions.h"
34 #include "mi-getopt.h"
35 #include "extension.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 (char *command
, char **argv
, int argc
)
53 error (_("-enable-frame-filters: no arguments allowed"));
57 /* Print a list of the stack frames. Args can be none, in which case
58 we want to print the whole backtrace, or a pair of numbers
59 specifying the frame numbers at which to start and stop the
60 display. If the two numbers are equal, a single frame will be
64 mi_cmd_stack_list_frames (char *command
, char **argv
, int argc
)
69 struct cleanup
*cleanup_stack
;
70 struct frame_info
*fi
;
71 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
78 static const struct mi_opt opts
[] =
80 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
84 /* Parse arguments. In this instance we are just looking for
85 --no-frame-filters. */
89 int opt
= mi_getopt ("-stack-list-frames", argc
, argv
,
93 switch ((enum opt
) opt
)
95 case NO_FRAME_FILTERS
:
101 /* After the last option is parsed, there should either be low -
102 high range, or no further arguments. */
103 if ((argc
- oind
!= 0) && (argc
- oind
!= 2))
104 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
106 /* If there is a range, set it. */
107 if (argc
- oind
== 2)
109 frame_low
= atoi (argv
[0 + oind
]);
110 frame_high
= atoi (argv
[1 + oind
]);
114 /* Called with no arguments, it means we want the whole
120 /* Let's position fi on the frame at which to start the
121 display. Could be the innermost frame if the whole stack needs
122 displaying, or if frame_low is 0. */
123 for (i
= 0, fi
= get_current_frame ();
125 i
++, fi
= get_prev_frame (fi
));
128 error (_("-stack-list-frames: Not enough frames in stack."));
130 cleanup_stack
= make_cleanup_ui_out_list_begin_end (current_uiout
, "stack");
132 if (! raw_arg
&& frame_filters
)
134 int flags
= PRINT_LEVEL
| PRINT_FRAME_INFO
;
135 int py_frame_low
= frame_low
;
137 /* We cannot pass -1 to frame_low, as that would signify a
138 relative backtrace from the tail of the stack. So, in the case
139 of frame_low == -1, assign and increment it. */
140 if (py_frame_low
== -1)
143 result
= apply_ext_lang_frame_filter (get_current_frame (), flags
,
144 NO_VALUES
, current_uiout
,
145 py_frame_low
, frame_high
);
148 /* Run the inbuilt backtrace if there are no filters registered, or
149 if "--no-frame-filters" has been specified from the command. */
150 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
152 /* Now let's print the frames up to frame_high, or until there are
153 frames in the stack. */
155 fi
&& (i
<= frame_high
|| frame_high
== -1);
156 i
++, fi
= get_prev_frame (fi
))
159 /* Print the location and the address always, even for level 0.
160 If args is 0, don't print the arguments. */
161 print_frame_info (fi
, 1, LOC_AND_ADDRESS
, 0 /* args */, 0);
165 do_cleanups (cleanup_stack
);
169 mi_cmd_stack_info_depth (char *command
, char **argv
, int argc
)
173 struct frame_info
*fi
;
176 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
179 frame_high
= atoi (argv
[0]);
181 /* Called with no arguments, it means we want the real depth of
185 for (i
= 0, fi
= get_current_frame ();
186 fi
&& (i
< frame_high
|| frame_high
== -1);
187 i
++, fi
= get_prev_frame (fi
))
190 ui_out_field_int (current_uiout
, "depth", i
);
193 /* Print a list of the locals for the current frame. With argument of
194 0, print only the names, with argument of 1 print also the
198 mi_cmd_stack_list_locals (char *command
, char **argv
, int argc
)
200 struct frame_info
*frame
;
202 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
205 int skip_unavailable
= 0;
216 static const struct mi_opt opts
[] =
218 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
219 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
226 /* Don't parse 'print-values' as an option. */
227 int opt
= mi_getopt ("-stack-list-locals", argc
- 1, argv
,
232 switch ((enum opt
) opt
)
234 case NO_FRAME_FILTERS
:
236 case SKIP_UNAVAILABLE
:
237 skip_unavailable
= 1;
243 /* After the last option is parsed, there should be only
245 if (argc
- oind
!= 1)
246 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
247 "[--skip-unavailable] PRINT_VALUES"));
249 frame
= get_selected_frame (NULL
);
250 print_value
= mi_parse_print_values (argv
[oind
]);
252 if (! raw_arg
&& frame_filters
)
254 int flags
= PRINT_LEVEL
| PRINT_LOCALS
;
256 result
= apply_ext_lang_frame_filter (frame
, flags
, print_value
,
257 current_uiout
, 0, 0);
260 /* Run the inbuilt backtrace if there are no filters registered, or
261 if "--no-frame-filters" has been specified from the command. */
262 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
264 list_args_or_locals (locals
, print_value
, frame
,
269 /* Print a list of the arguments for the current frame. With argument
270 of 0, print only the names, with argument of 1 print also the
274 mi_cmd_stack_list_args (char *command
, char **argv
, int argc
)
279 struct frame_info
*fi
;
280 struct cleanup
*cleanup_stack_args
;
281 enum print_values print_values
;
282 struct ui_out
*uiout
= current_uiout
;
285 int skip_unavailable
= 0;
286 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
292 static const struct mi_opt opts
[] =
294 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
295 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
302 int opt
= mi_getopt_allow_unknown ("-stack-list-args", argc
, argv
,
307 switch ((enum opt
) opt
)
309 case NO_FRAME_FILTERS
:
312 case SKIP_UNAVAILABLE
:
313 skip_unavailable
= 1;
318 if (argc
- oind
!= 1 && argc
- oind
!= 3)
319 error (_("-stack-list-arguments: Usage: " \
320 "[--no-frame-filters] [--skip-unavailable] "
321 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
323 if (argc
- oind
== 3)
325 frame_low
= atoi (argv
[1 + oind
]);
326 frame_high
= atoi (argv
[2 + oind
]);
330 /* Called with no arguments, it means we want args for the whole
336 print_values
= mi_parse_print_values (argv
[oind
]);
338 /* Let's position fi on the frame at which to start the
339 display. Could be the innermost frame if the whole stack needs
340 displaying, or if frame_low is 0. */
341 for (i
= 0, fi
= get_current_frame ();
343 i
++, fi
= get_prev_frame (fi
));
346 error (_("-stack-list-arguments: Not enough frames in stack."));
349 = make_cleanup_ui_out_list_begin_end (uiout
, "stack-args");
351 if (! raw_arg
&& frame_filters
)
353 int flags
= PRINT_LEVEL
| PRINT_ARGS
;
354 int py_frame_low
= frame_low
;
356 /* We cannot pass -1 to frame_low, as that would signify a
357 relative backtrace from the tail of the stack. So, in the case
358 of frame_low == -1, assign and increment it. */
359 if (py_frame_low
== -1)
362 result
= apply_ext_lang_frame_filter (get_current_frame (), flags
,
363 print_values
, current_uiout
,
364 py_frame_low
, frame_high
);
367 /* Run the inbuilt backtrace if there are no filters registered, or
368 if "--no-frame-filters" has been specified from the command. */
369 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
371 /* Now let's print the frames up to frame_high, or until there are
372 frames in the stack. */
374 fi
&& (i
<= frame_high
|| frame_high
== -1);
375 i
++, fi
= get_prev_frame (fi
))
377 struct cleanup
*cleanup_frame
;
380 cleanup_frame
= make_cleanup_ui_out_tuple_begin_end (uiout
, "frame");
381 ui_out_field_int (uiout
, "level", i
);
382 list_args_or_locals (arguments
, print_values
, fi
, skip_unavailable
);
383 do_cleanups (cleanup_frame
);
386 do_cleanups (cleanup_stack_args
);
389 /* Print a list of the local variables (including arguments) for the
390 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
391 or both names and values of the variables must be printed. See
392 parse_print_value for possible values. */
395 mi_cmd_stack_list_variables (char *command
, char **argv
, int argc
)
397 struct frame_info
*frame
;
399 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
402 int skip_unavailable
= 0;
412 static const struct mi_opt opts
[] =
414 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
415 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
422 /* Don't parse 'print-values' as an option. */
423 int opt
= mi_getopt ("-stack-list-variables", argc
- 1,
424 argv
, opts
, &oind
, &oarg
);
427 switch ((enum opt
) opt
)
429 case NO_FRAME_FILTERS
:
432 case SKIP_UNAVAILABLE
:
433 skip_unavailable
= 1;
439 /* After the last option is parsed, there should be only
441 if (argc
- oind
!= 1)
442 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
443 "[--skip-unavailable] PRINT_VALUES"));
445 frame
= get_selected_frame (NULL
);
446 print_value
= mi_parse_print_values (argv
[oind
]);
448 if (! raw_arg
&& frame_filters
)
450 int flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
452 result
= apply_ext_lang_frame_filter (frame
, flags
, print_value
,
453 current_uiout
, 0, 0);
456 /* Run the inbuilt backtrace if there are no filters registered, or
457 if "--no-frame-filters" has been specified from the command. */
458 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
460 list_args_or_locals (all
, print_value
, frame
,
465 /* Print single local or argument. ARG must be already read in. For
466 WHAT and VALUES see list_args_or_locals.
468 Errors are printed as if they would be the parameter value. Use
469 zeroed ARG iff it should not be printed according to VALUES. If
470 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
473 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
474 enum print_values values
, int skip_unavailable
)
476 struct cleanup
*old_chain
;
477 struct ui_out
*uiout
= current_uiout
;
480 gdb_assert (!arg
->val
|| !arg
->error
);
481 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
482 && arg
->error
== NULL
)
483 || values
== PRINT_SIMPLE_VALUES
484 || (values
== PRINT_ALL_VALUES
485 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
486 gdb_assert (arg
->entry_kind
== print_entry_values_no
487 || (arg
->entry_kind
== print_entry_values_only
488 && (arg
->val
|| arg
->error
)));
490 if (skip_unavailable
&& arg
->val
!= NULL
491 && (value_entirely_unavailable (arg
->val
)
492 /* A scalar object that does not have all bits available is
493 also considered unavailable, because all bits contribute
494 to its representation. */
495 || (val_print_scalar_type_p (value_type (arg
->val
))
496 && !value_bytes_available (arg
->val
,
497 value_embedded_offset (arg
->val
),
498 TYPE_LENGTH (value_type (arg
->val
))))))
501 stb
= mem_fileopen ();
502 old_chain
= make_cleanup_ui_file_delete (stb
);
504 if (values
!= PRINT_NO_VALUES
|| what
== all
)
505 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
507 fputs_filtered (SYMBOL_PRINT_NAME (arg
->sym
), stb
);
508 if (arg
->entry_kind
== print_entry_values_only
)
509 fputs_filtered ("@entry", stb
);
510 ui_out_field_stream (uiout
, "name", stb
);
512 if (what
== all
&& SYMBOL_IS_ARGUMENT (arg
->sym
))
513 ui_out_field_int (uiout
, "arg", 1);
515 if (values
== PRINT_SIMPLE_VALUES
)
517 check_typedef (arg
->sym
->type
);
518 type_print (arg
->sym
->type
, "", stb
, -1);
519 ui_out_field_stream (uiout
, "type", stb
);
522 if (arg
->val
|| arg
->error
)
524 volatile struct gdb_exception except
;
527 except
.message
= arg
->error
;
530 /* TRY_CATCH has two statements, wrap it in a block. */
532 TRY_CATCH (except
, RETURN_MASK_ERROR
)
534 struct value_print_options opts
;
536 get_no_prettyformat_print_options (&opts
);
538 common_val_print (arg
->val
, stb
, 0, &opts
,
539 language_def (SYMBOL_LANGUAGE (arg
->sym
)));
543 fprintf_filtered (stb
, _("<error reading variable: %s>"),
545 ui_out_field_stream (uiout
, "value", stb
);
548 do_cleanups (old_chain
);
551 /* Print a list of the objects for the frame FI in a certain form,
552 which is determined by VALUES. The objects can be locals,
553 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
554 is true, only print the arguments or local variables whose values
558 list_args_or_locals (enum what_to_list what
, enum print_values values
,
559 struct frame_info
*fi
, int skip_unavailable
)
561 const struct block
*block
;
563 struct block_iterator iter
;
564 struct cleanup
*cleanup_list
;
566 char *name_of_result
;
567 struct ui_out
*uiout
= current_uiout
;
569 block
= get_frame_block (fi
, 0);
574 name_of_result
= "locals";
577 name_of_result
= "args";
580 name_of_result
= "variables";
583 internal_error (__FILE__
, __LINE__
,
584 "unexpected what_to_list: %d", (int) what
);
587 cleanup_list
= make_cleanup_ui_out_list_begin_end (uiout
, name_of_result
);
591 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
595 switch (SYMBOL_CLASS (sym
))
598 case LOC_UNDEF
: /* catches errors */
599 case LOC_CONST
: /* constant */
600 case LOC_TYPEDEF
: /* local typedef */
601 case LOC_LABEL
: /* local label */
602 case LOC_BLOCK
: /* local function */
603 case LOC_CONST_BYTES
: /* loc. byte seq. */
604 case LOC_UNRESOLVED
: /* unresolved static */
605 case LOC_OPTIMIZED_OUT
: /* optimized out */
609 case LOC_ARG
: /* argument */
610 case LOC_REF_ARG
: /* reference arg */
611 case LOC_REGPARM_ADDR
: /* indirect register arg */
612 case LOC_LOCAL
: /* stack local */
613 case LOC_STATIC
: /* static */
614 case LOC_REGISTER
: /* register */
615 case LOC_COMPUTED
: /* computed location */
618 else if (what
== locals
)
619 print_me
= !SYMBOL_IS_ARGUMENT (sym
);
621 print_me
= SYMBOL_IS_ARGUMENT (sym
);
627 struct frame_arg arg
, entryarg
;
629 if (SYMBOL_IS_ARGUMENT (sym
))
630 sym2
= lookup_symbol (SYMBOL_LINKAGE_NAME (sym
),
635 gdb_assert (sym2
!= NULL
);
637 memset (&arg
, 0, sizeof (arg
));
639 arg
.entry_kind
= print_entry_values_no
;
640 memset (&entryarg
, 0, sizeof (entryarg
));
642 entryarg
.entry_kind
= print_entry_values_no
;
646 case PRINT_SIMPLE_VALUES
:
647 type
= check_typedef (sym2
->type
);
648 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
649 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
650 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
652 case PRINT_ALL_VALUES
:
653 if (SYMBOL_IS_ARGUMENT (sym
))
654 read_frame_arg (sym2
, fi
, &arg
, &entryarg
);
656 read_frame_local (sym2
, fi
, &arg
);
661 if (arg
.entry_kind
!= print_entry_values_only
)
662 list_arg_or_local (&arg
, what
, values
, skip_unavailable
);
663 if (entryarg
.entry_kind
!= print_entry_values_no
)
664 list_arg_or_local (&entryarg
, what
, values
, skip_unavailable
);
666 xfree (entryarg
.error
);
670 if (BLOCK_FUNCTION (block
))
673 block
= BLOCK_SUPERBLOCK (block
);
675 do_cleanups (cleanup_list
);
679 mi_cmd_stack_select_frame (char *command
, char **argv
, int argc
)
681 if (argc
== 0 || argc
> 1)
682 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
684 select_frame_command (argv
[0], 1 /* not used */ );
688 mi_cmd_stack_info_frame (char *command
, char **argv
, int argc
)
691 error (_("-stack-info-frame: No arguments allowed"));
693 print_frame_info (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0, 1);