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"
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
);
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 /* Parse the --no-frame-filters option in commands where we cannot use
59 parse_no_frames_option (const char *arg
)
61 if (arg
&& (strcmp (arg
, "--no-frame-filters") == 0))
67 /* Print a list of the stack frames. Args can be none, in which case
68 we want to print the whole backtrace, or a pair of numbers
69 specifying the frame numbers at which to start and stop the
70 display. If the two numbers are equal, a single frame will be
74 mi_cmd_stack_list_frames (char *command
, char **argv
, int argc
)
79 struct cleanup
*cleanup_stack
;
80 struct frame_info
*fi
;
81 enum py_bt_status result
= PY_BT_ERROR
;
88 static const struct mi_opt opts
[] =
90 {"-no-frame-filters", NO_FRAME_FILTERS
, 0},
94 /* Parse arguments. In this instance we are just looking for
95 --no-frame-filters. */
99 int opt
= mi_getopt ("-stack-list-frames", argc
, argv
,
103 switch ((enum opt
) opt
)
105 case NO_FRAME_FILTERS
:
111 /* After the last option is parsed, there should either be low -
112 high range, or no further arguments. */
113 if ((argc
- oind
!= 0) && (argc
- oind
!= 2))
114 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
116 /* If there is a range, set it. */
117 if (argc
- oind
== 2)
119 frame_low
= atoi (argv
[0 + oind
]);
120 frame_high
= atoi (argv
[1 + oind
]);
124 /* Called with no arguments, it means we want the whole
130 /* Let's position fi on the frame at which to start the
131 display. Could be the innermost frame if the whole stack needs
132 displaying, or if frame_low is 0. */
133 for (i
= 0, fi
= get_current_frame ();
135 i
++, fi
= get_prev_frame (fi
));
138 error (_("-stack-list-frames: Not enough frames in stack."));
140 cleanup_stack
= make_cleanup_ui_out_list_begin_end (current_uiout
, "stack");
142 if (! raw_arg
&& frame_filters
)
144 int flags
= PRINT_LEVEL
| PRINT_FRAME_INFO
;
145 int py_frame_low
= frame_low
;
147 /* We cannot pass -1 to frame_low, as that would signify a
148 relative backtrace from the tail of the stack. So, in the case
149 of frame_low == -1, assign and increment it. */
150 if (py_frame_low
== -1)
153 result
= apply_frame_filter (get_current_frame (), flags
,
154 NO_VALUES
, current_uiout
,
155 py_frame_low
, frame_high
);
158 /* Run the inbuilt backtrace if there are no filters registered, or
159 if "--no-frame-filters" has been specified from the command. */
160 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
162 /* Now let's print the frames up to frame_high, or until there are
163 frames in the stack. */
165 fi
&& (i
<= frame_high
|| frame_high
== -1);
166 i
++, fi
= get_prev_frame (fi
))
169 /* Print the location and the address always, even for level 0.
170 If args is 0, don't print the arguments. */
171 print_frame_info (fi
, 1, LOC_AND_ADDRESS
, 0 /* args */ );
175 do_cleanups (cleanup_stack
);
179 mi_cmd_stack_info_depth (char *command
, char **argv
, int argc
)
183 struct frame_info
*fi
;
186 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
189 frame_high
= atoi (argv
[0]);
191 /* Called with no arguments, it means we want the real depth of
195 for (i
= 0, fi
= get_current_frame ();
196 fi
&& (i
< frame_high
|| frame_high
== -1);
197 i
++, fi
= get_prev_frame (fi
))
200 ui_out_field_int (current_uiout
, "depth", i
);
203 static enum print_values
204 parse_print_values (char *name
)
206 if (strcmp (name
, "0") == 0
207 || strcmp (name
, mi_no_values
) == 0)
208 return PRINT_NO_VALUES
;
209 else if (strcmp (name
, "1") == 0
210 || strcmp (name
, mi_all_values
) == 0)
211 return PRINT_ALL_VALUES
;
212 else if (strcmp (name
, "2") == 0
213 || strcmp (name
, mi_simple_values
) == 0)
214 return PRINT_SIMPLE_VALUES
;
216 error (_("Unknown value for PRINT_VALUES: must be: \
217 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
218 mi_no_values
, mi_all_values
, mi_simple_values
);
221 /* Print a list of the locals for the current frame. With argument of
222 0, print only the names, with argument of 1 print also the
226 mi_cmd_stack_list_locals (char *command
, char **argv
, int argc
)
228 struct frame_info
*frame
;
230 enum py_bt_status result
= PY_BT_ERROR
;
234 raw_arg
= parse_no_frames_option (argv
[0]);
236 if (argc
< 1 || argc
> 2 || (argc
== 2 && ! raw_arg
)
237 || (argc
== 1 && raw_arg
))
238 error (_("-stack-list-locals: Usage: [--no-frame-filters] PRINT_VALUES"));
240 frame
= get_selected_frame (NULL
);
241 print_value
= parse_print_values (argv
[raw_arg
]);
243 if (! raw_arg
&& frame_filters
)
245 int flags
= PRINT_LEVEL
| PRINT_LOCALS
;
247 result
= apply_frame_filter (frame
, flags
, print_value
,
248 current_uiout
, 0, 0);
251 /* Run the inbuilt backtrace if there are no filters registered, or
252 if "--no-frame-filters" has been specified from the command. */
253 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
255 list_args_or_locals (locals
, print_value
, frame
);
259 /* Print a list of the arguments for the current frame. With argument
260 of 0, print only the names, with argument of 1 print also the
264 mi_cmd_stack_list_args (char *command
, char **argv
, int argc
)
269 struct frame_info
*fi
;
270 struct cleanup
*cleanup_stack_args
;
271 enum print_values print_values
;
272 struct ui_out
*uiout
= current_uiout
;
274 enum py_bt_status result
= PY_BT_ERROR
;
277 raw_arg
= parse_no_frames_option (argv
[0]);
279 if (argc
< 1 || (argc
> 3 && ! raw_arg
) || (argc
== 2 && ! raw_arg
))
280 error (_("-stack-list-arguments: Usage: " \
281 "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
285 frame_low
= atoi (argv
[1 + raw_arg
]);
286 frame_high
= atoi (argv
[2 + raw_arg
]);
290 /* Called with no arguments, it means we want args for the whole
296 print_values
= parse_print_values (argv
[raw_arg
]);
298 /* Let's position fi on the frame at which to start the
299 display. Could be the innermost frame if the whole stack needs
300 displaying, or if frame_low is 0. */
301 for (i
= 0, fi
= get_current_frame ();
303 i
++, fi
= get_prev_frame (fi
));
306 error (_("-stack-list-arguments: Not enough frames in stack."));
309 = make_cleanup_ui_out_list_begin_end (uiout
, "stack-args");
311 if (! raw_arg
&& frame_filters
)
313 int flags
= PRINT_LEVEL
| PRINT_ARGS
;
314 int py_frame_low
= frame_low
;
316 /* We cannot pass -1 to frame_low, as that would signify a
317 relative backtrace from the tail of the stack. So, in the case
318 of frame_low == -1, assign and increment it. */
319 if (py_frame_low
== -1)
322 result
= apply_frame_filter (get_current_frame (), flags
,
323 print_values
, current_uiout
,
324 py_frame_low
, frame_high
);
327 /* Run the inbuilt backtrace if there are no filters registered, or
328 if "--no-frame-filters" has been specified from the command. */
329 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
331 /* Now let's print the frames up to frame_high, or until there are
332 frames in the stack. */
334 fi
&& (i
<= frame_high
|| frame_high
== -1);
335 i
++, fi
= get_prev_frame (fi
))
337 struct cleanup
*cleanup_frame
;
340 cleanup_frame
= make_cleanup_ui_out_tuple_begin_end (uiout
, "frame");
341 ui_out_field_int (uiout
, "level", i
);
342 list_args_or_locals (arguments
, print_values
, fi
);
343 do_cleanups (cleanup_frame
);
346 do_cleanups (cleanup_stack_args
);
349 /* Print a list of the local variables (including arguments) for the
350 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
351 or both names and values of the variables must be printed. See
352 parse_print_value for possible values. */
355 mi_cmd_stack_list_variables (char *command
, char **argv
, int argc
)
357 struct frame_info
*frame
;
359 enum py_bt_status result
= PY_BT_ERROR
;
363 raw_arg
= parse_no_frames_option (argv
[0]);
365 if (argc
< 1 || argc
> 2 || (argc
== 2 && ! raw_arg
)
366 || (argc
== 1 && raw_arg
))
367 error (_("-stack-list-variables: Usage: " \
368 "[--no-frame-filters] PRINT_VALUES"));
370 frame
= get_selected_frame (NULL
);
371 print_value
= parse_print_values (argv
[raw_arg
]);
373 if (! raw_arg
&& frame_filters
)
375 int flags
= PRINT_LEVEL
| PRINT_ARGS
| PRINT_LOCALS
;
377 result
= apply_frame_filter (frame
, flags
, print_value
,
378 current_uiout
, 0, 0);
381 /* Run the inbuilt backtrace if there are no filters registered, or
382 if "--no-frame-filters" has been specified from the command. */
383 if (! frame_filters
|| raw_arg
|| result
== PY_BT_NO_FILTERS
)
385 list_args_or_locals (all
, print_value
, frame
);
389 /* Print single local or argument. ARG must be already read in. For
390 WHAT and VALUES see list_args_or_locals.
392 Errors are printed as if they would be the parameter value. Use
393 zeroed ARG iff it should not be printed according to VALUES. */
396 list_arg_or_local (const struct frame_arg
*arg
, enum what_to_list what
,
397 enum print_values values
)
399 struct cleanup
*old_chain
;
400 struct cleanup
*cleanup_tuple
= NULL
;
401 struct ui_out
*uiout
= current_uiout
;
404 stb
= mem_fileopen ();
405 old_chain
= make_cleanup_ui_file_delete (stb
);
407 gdb_assert (!arg
->val
|| !arg
->error
);
408 gdb_assert ((values
== PRINT_NO_VALUES
&& arg
->val
== NULL
409 && arg
->error
== NULL
)
410 || values
== PRINT_SIMPLE_VALUES
411 || (values
== PRINT_ALL_VALUES
412 && (arg
->val
!= NULL
|| arg
->error
!= NULL
)));
413 gdb_assert (arg
->entry_kind
== print_entry_values_no
414 || (arg
->entry_kind
== print_entry_values_only
415 && (arg
->val
|| arg
->error
)));
417 if (values
!= PRINT_NO_VALUES
|| what
== all
)
418 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
420 fputs_filtered (SYMBOL_PRINT_NAME (arg
->sym
), stb
);
421 if (arg
->entry_kind
== print_entry_values_only
)
422 fputs_filtered ("@entry", stb
);
423 ui_out_field_stream (uiout
, "name", stb
);
425 if (what
== all
&& SYMBOL_IS_ARGUMENT (arg
->sym
))
426 ui_out_field_int (uiout
, "arg", 1);
428 if (values
== PRINT_SIMPLE_VALUES
)
430 check_typedef (arg
->sym
->type
);
431 type_print (arg
->sym
->type
, "", stb
, -1);
432 ui_out_field_stream (uiout
, "type", stb
);
435 if (arg
->val
|| arg
->error
)
437 volatile struct gdb_exception except
;
440 except
.message
= arg
->error
;
443 /* TRY_CATCH has two statements, wrap it in a block. */
445 TRY_CATCH (except
, RETURN_MASK_ERROR
)
447 struct value_print_options opts
;
449 get_raw_print_options (&opts
);
451 common_val_print (arg
->val
, stb
, 0, &opts
,
452 language_def (SYMBOL_LANGUAGE (arg
->sym
)));
456 fprintf_filtered (stb
, _("<error reading variable: %s>"),
458 ui_out_field_stream (uiout
, "value", stb
);
461 if (values
!= PRINT_NO_VALUES
|| what
== all
)
462 do_cleanups (cleanup_tuple
);
463 do_cleanups (old_chain
);
466 /* Print a list of the locals or the arguments for the currently
467 selected frame. If the argument passed is 0, printonly the names
468 of the variables, if an argument of 1 is passed, print the values
472 list_args_or_locals (enum what_to_list what
, enum print_values values
,
473 struct frame_info
*fi
)
477 struct block_iterator iter
;
478 struct cleanup
*cleanup_list
;
480 char *name_of_result
;
481 struct ui_out
*uiout
= current_uiout
;
483 block
= get_frame_block (fi
, 0);
488 name_of_result
= "locals";
491 name_of_result
= "args";
494 name_of_result
= "variables";
497 internal_error (__FILE__
, __LINE__
,
498 "unexpected what_to_list: %d", (int) what
);
501 cleanup_list
= make_cleanup_ui_out_list_begin_end (uiout
, name_of_result
);
505 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
509 switch (SYMBOL_CLASS (sym
))
512 case LOC_UNDEF
: /* catches errors */
513 case LOC_CONST
: /* constant */
514 case LOC_TYPEDEF
: /* local typedef */
515 case LOC_LABEL
: /* local label */
516 case LOC_BLOCK
: /* local function */
517 case LOC_CONST_BYTES
: /* loc. byte seq. */
518 case LOC_UNRESOLVED
: /* unresolved static */
519 case LOC_OPTIMIZED_OUT
: /* optimized out */
523 case LOC_ARG
: /* argument */
524 case LOC_REF_ARG
: /* reference arg */
525 case LOC_REGPARM_ADDR
: /* indirect register arg */
526 case LOC_LOCAL
: /* stack local */
527 case LOC_STATIC
: /* static */
528 case LOC_REGISTER
: /* register */
529 case LOC_COMPUTED
: /* computed location */
532 else if (what
== locals
)
533 print_me
= !SYMBOL_IS_ARGUMENT (sym
);
535 print_me
= SYMBOL_IS_ARGUMENT (sym
);
541 struct frame_arg arg
, entryarg
;
543 if (SYMBOL_IS_ARGUMENT (sym
))
544 sym2
= lookup_symbol (SYMBOL_LINKAGE_NAME (sym
),
549 gdb_assert (sym2
!= NULL
);
551 memset (&arg
, 0, sizeof (arg
));
553 arg
.entry_kind
= print_entry_values_no
;
554 memset (&entryarg
, 0, sizeof (entryarg
));
556 entryarg
.entry_kind
= print_entry_values_no
;
560 case PRINT_SIMPLE_VALUES
:
561 type
= check_typedef (sym2
->type
);
562 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
563 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
564 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
566 case PRINT_ALL_VALUES
:
567 read_frame_arg (sym2
, fi
, &arg
, &entryarg
);
572 if (arg
.entry_kind
!= print_entry_values_only
)
573 list_arg_or_local (&arg
, what
, values
);
574 if (entryarg
.entry_kind
!= print_entry_values_no
)
575 list_arg_or_local (&entryarg
, what
, values
);
577 xfree (entryarg
.error
);
581 if (BLOCK_FUNCTION (block
))
584 block
= BLOCK_SUPERBLOCK (block
);
586 do_cleanups (cleanup_list
);
590 mi_cmd_stack_select_frame (char *command
, char **argv
, int argc
)
592 if (argc
== 0 || argc
> 1)
593 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
595 select_frame_command (argv
[0], 1 /* not used */ );
599 mi_cmd_stack_info_frame (char *command
, char **argv
, int argc
)
602 error (_("-stack-info-frame: No arguments allowed"));
604 print_frame_info (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0);