1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2019 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"
38 #include "safe-ctype.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 (const char *command
, char **argv
, int argc
)
54 error (_("-enable-frame-filters: no arguments allowed"));
58 /* Like apply_ext_lang_frame_filter, but take a print_values */
60 static enum ext_lang_bt_status
61 mi_apply_ext_lang_frame_filter (struct frame_info
*frame
,
62 frame_filter_flags flags
,
63 enum print_values print_values
,
65 int frame_low
, int frame_high
)
67 /* ext_lang_frame_args's MI options are compatible with MI print
69 return apply_ext_lang_frame_filter (frame
, flags
,
70 (enum ext_lang_frame_args
) print_values
,
72 frame_low
, frame_high
);
75 /* Print a list of the stack frames. Args can be none, in which case
76 we want to print the whole backtrace, or a pair of numbers
77 specifying the frame numbers at which to start and stop the
78 display. If the two numbers are equal, a single frame will be
82 mi_cmd_stack_list_frames (const char *command
, char **argv
, int argc
)
87 struct frame_info
*fi
;
88 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
95 static const struct mi_opt opts
[] =
97 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
101 /* Parse arguments. In this instance we are just looking for
102 --no-frame-filters. */
106 int opt
= mi_getopt ("-stack-list-frames", argc
, argv
,
110 switch ((enum opt
) opt
)
112 case NO_FRAME_FILTERS
:
118 /* After the last option is parsed, there should either be low -
119 high range, or no further arguments. */
120 if ((argc
- oind
!= 0) && (argc
- oind
!= 2))
121 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
123 /* If there is a range, set it. */
124 if (argc
- oind
== 2)
126 frame_low
= atoi (argv
[0 + oind
]);
127 frame_high
= atoi (argv
[1 + oind
]);
131 /* Called with no arguments, it means we want the whole
137 /* Let's position fi on the frame at which to start the
138 display. Could be the innermost frame if the whole stack needs
139 displaying, or if frame_low is 0. */
140 for (i
= 0, fi
= get_current_frame ();
142 i
++, fi
= get_prev_frame (fi
));
145 error (_("-stack-list-frames: Not enough frames in stack."));
147 ui_out_emit_list
list_emitter (current_uiout
, "stack");
149 if (! raw_arg
&& frame_filters
)
151 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_FRAME_INFO
;
152 int py_frame_low
= frame_low
;
154 /* We cannot pass -1 to frame_low, as that would signify a
155 relative backtrace from the tail of the stack. So, in the case
156 of frame_low == -1, assign and increment it. */
157 if (py_frame_low
== -1)
160 result
= apply_ext_lang_frame_filter (get_current_frame (), flags
,
161 NO_VALUES
, current_uiout
,
162 py_frame_low
, frame_high
);
165 /* Run the inbuilt backtrace if there are no filters registered, or
166 if "--no-frame-filters" has been specified from the command. */
167 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
169 /* Now let's print the frames up to frame_high, or until there are
170 frames in the stack. */
172 fi
&& (i
<= frame_high
|| frame_high
== -1);
173 i
++, fi
= get_prev_frame (fi
))
176 /* Print the location and the address always, even for level 0.
177 If args is 0, don't print the arguments. */
178 print_frame_info (fi
, 1, LOC_AND_ADDRESS
, 0 /* args */, 0);
184 mi_cmd_stack_info_depth (const 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 current_uiout
->field_int ("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 (const char *command
, char **argv
, int argc
)
215 struct frame_info
*frame
;
217 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
218 enum print_values print_value
;
220 int skip_unavailable
= 0;
229 static const struct mi_opt opts
[] =
231 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
232 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
239 /* Don't parse 'print-values' as an option. */
240 int opt
= mi_getopt ("-stack-list-locals", argc
- 1, argv
,
245 switch ((enum opt
) opt
)
247 case NO_FRAME_FILTERS
:
250 case SKIP_UNAVAILABLE
:
251 skip_unavailable
= 1;
257 /* After the last option is parsed, there should be only
259 if (argc
- oind
!= 1)
260 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
261 "[--skip-unavailable] PRINT_VALUES"));
263 frame
= get_selected_frame (NULL
);
264 print_value
= mi_parse_print_values (argv
[oind
]);
266 if (! raw_arg
&& frame_filters
)
268 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_LOCALS
;
270 result
= mi_apply_ext_lang_frame_filter (frame
, flags
, print_value
,
271 current_uiout
, 0, 0);
274 /* Run the inbuilt backtrace if there are no filters registered, or
275 if "--no-frame-filters" has been specified from the command. */
276 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
278 list_args_or_locals (locals
, print_value
, frame
,
283 /* Print a list of the arguments for the current frame. With argument
284 of 0, print only the names, with argument of 1 print also the
288 mi_cmd_stack_list_args (const char *command
, char **argv
, int argc
)
293 struct frame_info
*fi
;
294 enum print_values print_values
;
295 struct ui_out
*uiout
= current_uiout
;
298 int skip_unavailable
= 0;
299 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
305 static const struct mi_opt opts
[] =
307 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
308 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
315 int opt
= mi_getopt_allow_unknown ("-stack-list-args", argc
, argv
,
320 switch ((enum opt
) opt
)
322 case NO_FRAME_FILTERS
:
325 case SKIP_UNAVAILABLE
:
326 skip_unavailable
= 1;
331 if (argc
- oind
!= 1 && argc
- oind
!= 3)
332 error (_("-stack-list-arguments: Usage: " \
333 "[--no-frame-filters] [--skip-unavailable] "
334 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
336 if (argc
- oind
== 3)
338 frame_low
= atoi (argv
[1 + oind
]);
339 frame_high
= atoi (argv
[2 + oind
]);
343 /* Called with no arguments, it means we want args for the whole
349 print_values
= mi_parse_print_values (argv
[oind
]);
351 /* Let's position fi on the frame at which to start the
352 display. Could be the innermost frame if the whole stack needs
353 displaying, or if frame_low is 0. */
354 for (i
= 0, fi
= get_current_frame ();
356 i
++, fi
= get_prev_frame (fi
));
359 error (_("-stack-list-arguments: Not enough frames in stack."));
361 ui_out_emit_list
list_emitter (uiout
, "stack-args");
363 if (! raw_arg
&& frame_filters
)
365 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
;
366 int py_frame_low
= frame_low
;
368 /* We cannot pass -1 to frame_low, as that would signify a
369 relative backtrace from the tail of the stack. So, in the case
370 of frame_low == -1, assign and increment it. */
371 if (py_frame_low
== -1)
374 result
= mi_apply_ext_lang_frame_filter (get_current_frame (), flags
,
375 print_values
, current_uiout
,
376 py_frame_low
, frame_high
);
379 /* Run the inbuilt backtrace if there are no filters registered, or
380 if "--no-frame-filters" has been specified from the command. */
381 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
383 /* Now let's print the frames up to frame_high, or until there are
384 frames in the stack. */
386 fi
&& (i
<= frame_high
|| frame_high
== -1);
387 i
++, fi
= get_prev_frame (fi
))
390 ui_out_emit_tuple
tuple_emitter (uiout
, "frame");
391 uiout
->field_int ("level", i
);
392 list_args_or_locals (arguments
, print_values
, fi
, skip_unavailable
);
397 /* Print a list of the local variables (including arguments) for the
398 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
399 or both names and values of the variables must be printed. See
400 parse_print_value for possible values. */
403 mi_cmd_stack_list_variables (const char *command
, char **argv
, int argc
)
405 struct frame_info
*frame
;
407 enum ext_lang_bt_status result
= EXT_LANG_BT_ERROR
;
408 enum print_values print_value
;
410 int skip_unavailable
= 0;
419 static const struct mi_opt opts
[] =
421 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
422 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
429 /* Don't parse 'print-values' as an option. */
430 int opt
= mi_getopt ("-stack-list-variables", argc
- 1,
431 argv
, opts
, &oind
, &oarg
);
434 switch ((enum opt
) opt
)
436 case NO_FRAME_FILTERS
:
439 case SKIP_UNAVAILABLE
:
440 skip_unavailable
= 1;
446 /* After the last option is parsed, there should be only
448 if (argc
- oind
!= 1)
449 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
450 "[--skip-unavailable] PRINT_VALUES"));
452 frame
= get_selected_frame (NULL
);
453 print_value
= mi_parse_print_values (argv
[oind
]);
455 if (! raw_arg
&& frame_filters
)
457 frame_filter_flags flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
459 result
= mi_apply_ext_lang_frame_filter (frame
, flags
,
461 current_uiout
, 0, 0);
464 /* Run the inbuilt backtrace if there are no filters registered, or
465 if "--no-frame-filters" has been specified from the command. */
466 if (! frame_filters
|| raw_arg
|| result
== EXT_LANG_BT_NO_FILTERS
)
468 list_args_or_locals (all
, print_value
, frame
,
473 /* Print single local or argument. ARG must be already read in. For
474 WHAT and VALUES see list_args_or_locals.
476 Errors are printed as if they would be the parameter value. Use
477 zeroed ARG iff it should not be printed according to VALUES. If
478 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
481 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
482 enum print_values values
, int skip_unavailable
)
484 struct ui_out
*uiout
= current_uiout
;
486 gdb_assert (!arg
->val
|| !arg
->error
);
487 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
488 && arg
->error
== NULL
)
489 || values
== PRINT_SIMPLE_VALUES
490 || (values
== PRINT_ALL_VALUES
491 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
492 gdb_assert (arg
->entry_kind
== print_entry_values_no
493 || (arg
->entry_kind
== print_entry_values_only
494 && (arg
->val
|| arg
->error
)));
496 if (skip_unavailable
&& arg
->val
!= NULL
497 && (value_entirely_unavailable (arg
->val
)
498 /* A scalar object that does not have all bits available is
499 also considered unavailable, because all bits contribute
500 to its representation. */
501 || (val_print_scalar_type_p (value_type (arg
->val
))
502 && !value_bytes_available (arg
->val
,
503 value_embedded_offset (arg
->val
),
504 TYPE_LENGTH (value_type (arg
->val
))))))
507 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
508 if (values
!= PRINT_NO_VALUES
|| what
== all
)
509 tuple_emitter
.emplace (uiout
, nullptr);
513 stb
.puts (SYMBOL_PRINT_NAME (arg
->sym
));
514 if (arg
->entry_kind
== print_entry_values_only
)
516 uiout
->field_stream ("name", stb
);
518 if (what
== all
&& SYMBOL_IS_ARGUMENT (arg
->sym
))
519 uiout
->field_int ("arg", 1);
521 if (values
== PRINT_SIMPLE_VALUES
)
523 check_typedef (arg
->sym
->type
);
524 type_print (arg
->sym
->type
, "", &stb
, -1);
525 uiout
->field_stream ("type", stb
);
528 if (arg
->val
|| arg
->error
)
531 stb
.printf (_("<error reading variable: %s>"), arg
->error
);
536 struct value_print_options opts
;
538 get_no_prettyformat_print_options (&opts
);
540 common_val_print (arg
->val
, &stb
, 0, &opts
,
541 language_def (SYMBOL_LANGUAGE (arg
->sym
)));
543 catch (const gdb_exception_error
&except
)
545 stb
.printf (_("<error reading variable: %s>"),
549 uiout
->field_stream ("value", stb
);
553 /* Print a list of the objects for the frame FI in a certain form,
554 which is determined by VALUES. The objects can be locals,
555 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
556 is true, only print the arguments or local variables whose values
560 list_args_or_locals (enum what_to_list what
, enum print_values values
,
561 struct frame_info
*fi
, int skip_unavailable
)
563 const struct block
*block
;
565 struct block_iterator iter
;
567 const 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 ui_out_emit_list
list_emitter (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
);
678 /* Read a frame specification from FRAME_EXP and return the selected frame.
679 Call error() if the specification is in any way invalid (so this
680 function never returns NULL).
682 The frame specification is usually an integer level number, however if
683 the number does not match a valid frame level then it will be treated as
684 a frame address. The frame address will then be used to find a matching
685 frame in the stack. If no matching frame is found then a new frame will
688 The use of FRAME_EXP as an address is undocumented in the GDB user
689 manual, this feature is supported here purely for backward
692 static struct frame_info
*
693 parse_frame_specification (const char *frame_exp
)
695 gdb_assert (frame_exp
!= NULL
);
697 /* NOTE: Parse and evaluate expression, but do not use
698 functions such as parse_and_eval_long or
699 parse_and_eval_address to also extract the value.
700 Instead value_as_long and value_as_address are used.
701 This avoids problems with expressions that contain
703 struct value
*arg
= parse_and_eval (frame_exp
);
705 /* Assume ARG is an integer, and try using that to select a frame. */
706 struct frame_info
*fid
;
707 int level
= value_as_long (arg
);
709 fid
= find_relative_frame (get_current_frame (), &level
);
711 /* find_relative_frame was successful. */
714 /* Convert the value into a corresponding address. */
715 CORE_ADDR addr
= value_as_address (arg
);
717 /* Assume that ADDR is an address, use that to identify a frame with a
719 struct frame_id id
= frame_id_build_wild (addr
);
721 /* If (s)he specifies the frame with an address, he deserves
722 what (s)he gets. Still, give the highest one that matches.
723 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
725 for (fid
= get_current_frame ();
727 fid
= get_prev_frame (fid
))
729 if (frame_id_eq (id
, get_frame_id (fid
)))
731 struct frame_info
*prev_frame
;
735 prev_frame
= get_prev_frame (fid
);
737 || !frame_id_eq (id
, get_frame_id (prev_frame
)))
745 /* We couldn't identify the frame as an existing frame, but
746 perhaps we can create one with a single argument. */
747 return create_new_frame (addr
, 0);
750 /* Implement the -stack-select-frame MI command. */
753 mi_cmd_stack_select_frame (const char *command
, char **argv
, int argc
)
755 if (argc
== 0 || argc
> 1)
756 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
758 select_frame_for_mi (parse_frame_specification (argv
[0]));
762 mi_cmd_stack_info_frame (const char *command
, char **argv
, int argc
)
765 error (_("-stack-info-frame: No arguments allowed"));
767 print_frame_info (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0, 1);