1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2013 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"
30 #include "gdb_string.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
);
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 py_bt_status result
= PY_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_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
== PY_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 */ );
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 py_bt_status result
= PY_BT_ERROR
;
213 static const struct mi_opt opts
[] =
215 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
222 /* Don't parse 'print-values' as an option. */
223 int opt
= mi_getopt ("-stack-list-locals", argc
- 1, argv
,
228 switch ((enum opt
) opt
)
230 case NO_FRAME_FILTERS
:
237 /* After the last option is parsed, there should be only
239 if (argc
- oind
!= 1)
240 error (_("-stack-list-locals: Usage: [--no-frame-filters] PRINT_VALUES"));
242 frame
= get_selected_frame (NULL
);
243 print_value
= mi_parse_print_values (argv
[oind
]);
245 if (! raw_arg
&& frame_filters
)
247 int flags
= PRINT_LEVEL
| PRINT_LOCALS
;
249 result
= apply_frame_filter (frame
, flags
, print_value
,
250 current_uiout
, 0, 0);
253 /* Run the inbuilt backtrace if there are no filters registered, or
254 if "--no-frame-filters" has been specified from the command. */
255 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
257 list_args_or_locals (locals
, print_value
, frame
);
261 /* Print a list of the arguments for the current frame. With argument
262 of 0, print only the names, with argument of 1 print also the
266 mi_cmd_stack_list_args (char *command
, char **argv
, int argc
)
271 struct frame_info
*fi
;
272 struct cleanup
*cleanup_stack_args
;
273 enum print_values print_values
;
274 struct ui_out
*uiout
= current_uiout
;
277 enum py_bt_status result
= PY_BT_ERROR
;
282 static const struct mi_opt opts
[] =
284 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
291 int opt
= mi_getopt_allow_unknown ("-stack-list-args", argc
, argv
,
296 switch ((enum opt
) opt
)
298 case NO_FRAME_FILTERS
:
304 if (argc
- oind
!= 1 && argc
- oind
!= 3)
305 error (_("-stack-list-arguments: Usage: " \
306 "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
308 if (argc
- oind
== 3)
310 frame_low
= atoi (argv
[1 + oind
]);
311 frame_high
= atoi (argv
[2 + oind
]);
315 /* Called with no arguments, it means we want args for the whole
321 print_values
= mi_parse_print_values (argv
[oind
]);
323 /* Let's position fi on the frame at which to start the
324 display. Could be the innermost frame if the whole stack needs
325 displaying, or if frame_low is 0. */
326 for (i
= 0, fi
= get_current_frame ();
328 i
++, fi
= get_prev_frame (fi
));
331 error (_("-stack-list-arguments: Not enough frames in stack."));
334 = make_cleanup_ui_out_list_begin_end (uiout
, "stack-args");
336 if (! raw_arg
&& frame_filters
)
338 int flags
= PRINT_LEVEL
| PRINT_ARGS
;
339 int py_frame_low
= frame_low
;
341 /* We cannot pass -1 to frame_low, as that would signify a
342 relative backtrace from the tail of the stack. So, in the case
343 of frame_low == -1, assign and increment it. */
344 if (py_frame_low
== -1)
347 result
= apply_frame_filter (get_current_frame (), flags
,
348 print_values
, current_uiout
,
349 py_frame_low
, frame_high
);
352 /* Run the inbuilt backtrace if there are no filters registered, or
353 if "--no-frame-filters" has been specified from the command. */
354 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
356 /* Now let's print the frames up to frame_high, or until there are
357 frames in the stack. */
359 fi
&& (i
<= frame_high
|| frame_high
== -1);
360 i
++, fi
= get_prev_frame (fi
))
362 struct cleanup
*cleanup_frame
;
365 cleanup_frame
= make_cleanup_ui_out_tuple_begin_end (uiout
, "frame");
366 ui_out_field_int (uiout
, "level", i
);
367 list_args_or_locals (arguments
, print_values
, fi
);
368 do_cleanups (cleanup_frame
);
371 do_cleanups (cleanup_stack_args
);
374 /* Print a list of the local variables (including arguments) for the
375 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
376 or both names and values of the variables must be printed. See
377 parse_print_value for possible values. */
380 mi_cmd_stack_list_variables (char *command
, char **argv
, int argc
)
382 struct frame_info
*frame
;
384 enum py_bt_status result
= PY_BT_ERROR
;
395 static const struct mi_opt opts
[] =
397 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
404 /* Don't parse 'print-values' as an option. */
405 int opt
= mi_getopt ("-stack-list-variables", argc
- 1,
406 argv
, opts
, &oind
, &oarg
);
409 switch ((enum opt
) opt
)
411 case NO_FRAME_FILTERS
:
418 /* After the last option is parsed, there should be only
420 if (argc
- oind
!= 1)
421 error (_("-stack-list-variables: Usage: " \
422 "[--no-frame-filters] PRINT_VALUES"));
424 frame
= get_selected_frame (NULL
);
425 print_value
= mi_parse_print_values (argv
[oind
]);
427 if (! raw_arg
&& frame_filters
)
429 int flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
431 result
= apply_frame_filter (frame
, flags
, print_value
,
432 current_uiout
, 0, 0);
435 /* Run the inbuilt backtrace if there are no filters registered, or
436 if "--no-frame-filters" has been specified from the command. */
437 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
439 list_args_or_locals (all
, print_value
, frame
);
443 /* Print single local or argument. ARG must be already read in. For
444 WHAT and VALUES see list_args_or_locals.
446 Errors are printed as if they would be the parameter value. Use
447 zeroed ARG iff it should not be printed according to VALUES. */
450 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
451 enum print_values values
)
453 struct cleanup
*old_chain
;
454 struct ui_out
*uiout
= current_uiout
;
457 stb
= mem_fileopen ();
458 old_chain
= make_cleanup_ui_file_delete (stb
);
460 gdb_assert (!arg
->val
|| !arg
->error
);
461 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
462 && arg
->error
== NULL
)
463 || values
== PRINT_SIMPLE_VALUES
464 || (values
== PRINT_ALL_VALUES
465 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
466 gdb_assert (arg
->entry_kind
== print_entry_values_no
467 || (arg
->entry_kind
== print_entry_values_only
468 && (arg
->val
|| arg
->error
)));
470 if (values
!= PRINT_NO_VALUES
|| what
== all
)
471 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
473 fputs_filtered (SYMBOL_PRINT_NAME (arg
->sym
), stb
);
474 if (arg
->entry_kind
== print_entry_values_only
)
475 fputs_filtered ("@entry", stb
);
476 ui_out_field_stream (uiout
, "name", stb
);
478 if (what
== all
&& SYMBOL_IS_ARGUMENT (arg
->sym
))
479 ui_out_field_int (uiout
, "arg", 1);
481 if (values
== PRINT_SIMPLE_VALUES
)
483 check_typedef (arg
->sym
->type
);
484 type_print (arg
->sym
->type
, "", stb
, -1);
485 ui_out_field_stream (uiout
, "type", stb
);
488 if (arg
->val
|| arg
->error
)
490 volatile struct gdb_exception except
;
493 except
.message
= arg
->error
;
496 /* TRY_CATCH has two statements, wrap it in a block. */
498 TRY_CATCH (except
, RETURN_MASK_ERROR
)
500 struct value_print_options opts
;
502 get_no_prettyformat_print_options (&opts
);
504 common_val_print (arg
->val
, stb
, 0, &opts
,
505 language_def (SYMBOL_LANGUAGE (arg
->sym
)));
509 fprintf_filtered (stb
, _("<error reading variable: %s>"),
511 ui_out_field_stream (uiout
, "value", stb
);
514 do_cleanups (old_chain
);
517 /* Print a list of the objects for the frame FI in a certain form,
518 which is determined by VALUES. The objects can be locals,
519 arguments or both, which is determined by WHAT. */
522 list_args_or_locals (enum what_to_list what
, enum print_values values
,
523 struct frame_info
*fi
)
527 struct block_iterator iter
;
528 struct cleanup
*cleanup_list
;
530 char *name_of_result
;
531 struct ui_out
*uiout
= current_uiout
;
533 block
= get_frame_block (fi
, 0);
538 name_of_result
= "locals";
541 name_of_result
= "args";
544 name_of_result
= "variables";
547 internal_error (__FILE__
, __LINE__
,
548 "unexpected what_to_list: %d", (int) what
);
551 cleanup_list
= make_cleanup_ui_out_list_begin_end (uiout
, name_of_result
);
555 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
559 switch (SYMBOL_CLASS (sym
))
562 case LOC_UNDEF
: /* catches errors */
563 case LOC_CONST
: /* constant */
564 case LOC_TYPEDEF
: /* local typedef */
565 case LOC_LABEL
: /* local label */
566 case LOC_BLOCK
: /* local function */
567 case LOC_CONST_BYTES
: /* loc. byte seq. */
568 case LOC_UNRESOLVED
: /* unresolved static */
569 case LOC_OPTIMIZED_OUT
: /* optimized out */
573 case LOC_ARG
: /* argument */
574 case LOC_REF_ARG
: /* reference arg */
575 case LOC_REGPARM_ADDR
: /* indirect register arg */
576 case LOC_LOCAL
: /* stack local */
577 case LOC_STATIC
: /* static */
578 case LOC_REGISTER
: /* register */
579 case LOC_COMPUTED
: /* computed location */
582 else if (what
== locals
)
583 print_me
= !SYMBOL_IS_ARGUMENT (sym
);
585 print_me
= SYMBOL_IS_ARGUMENT (sym
);
591 struct frame_arg arg
, entryarg
;
593 if (SYMBOL_IS_ARGUMENT (sym
))
594 sym2
= lookup_symbol (SYMBOL_LINKAGE_NAME (sym
),
599 gdb_assert (sym2
!= NULL
);
601 memset (&arg
, 0, sizeof (arg
));
603 arg
.entry_kind
= print_entry_values_no
;
604 memset (&entryarg
, 0, sizeof (entryarg
));
606 entryarg
.entry_kind
= print_entry_values_no
;
610 case PRINT_SIMPLE_VALUES
:
611 type
= check_typedef (sym2
->type
);
612 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
613 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
614 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
616 case PRINT_ALL_VALUES
:
617 if (SYMBOL_IS_ARGUMENT (sym
))
618 read_frame_arg (sym2
, fi
, &arg
, &entryarg
);
620 read_frame_local (sym2
, fi
, &arg
);
625 if (arg
.entry_kind
!= print_entry_values_only
)
626 list_arg_or_local (&arg
, what
, values
);
627 if (entryarg
.entry_kind
!= print_entry_values_no
)
628 list_arg_or_local (&entryarg
, what
, values
);
630 xfree (entryarg
.error
);
634 if (BLOCK_FUNCTION (block
))
637 block
= BLOCK_SUPERBLOCK (block
);
639 do_cleanups (cleanup_list
);
643 mi_cmd_stack_select_frame (char *command
, char **argv
, int argc
)
645 if (argc
== 0 || argc
> 1)
646 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
648 select_frame_command (argv
[0], 1 /* not used */ );
652 mi_cmd_stack_info_frame (char *command
, char **argv
, int argc
)
655 error (_("-stack-info-frame: No arguments allowed"));
657 print_frame_info (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0);