1 /* MI Command Set - stack commands.
2 Copyright (C) 2000, 2002-2005, 2007-2012 Free Software Foundation,
4 Contributed by Cygnus Solutions (a Red Hat company).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "dictionary.h"
31 #include "gdb_string.h"
34 #include "exceptions.h"
36 enum what_to_list { locals, arguments, all };
38 static void list_args_or_locals (enum what_to_list what,
39 enum print_values values,
40 struct frame_info *fi);
42 /* Print a list of the stack frames. Args can be none, in which case
43 we want to print the whole backtrace, or a pair of numbers
44 specifying the frame numbers at which to start and stop the
45 display. If the two numbers are equal, a single frame will be
48 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
53 struct cleanup *cleanup_stack;
54 struct frame_info *fi;
56 if (argc > 2 || argc == 1)
57 error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
61 frame_low = atoi (argv[0]);
62 frame_high = atoi (argv[1]);
66 /* Called with no arguments, it means we want the whole
72 /* Let's position fi on the frame at which to start the
73 display. Could be the innermost frame if the whole stack needs
74 displaying, or if frame_low is 0. */
75 for (i = 0, fi = get_current_frame ();
77 i++, fi = get_prev_frame (fi));
80 error (_("-stack-list-frames: Not enough frames in stack."));
82 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
84 /* Now let;s print the frames up to frame_high, or until there are
85 frames in the stack. */
87 fi && (i <= frame_high || frame_high == -1);
88 i++, fi = get_prev_frame (fi))
91 /* Print the location and the address always, even for level 0.
92 args == 0: don't print the arguments. */
93 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
96 do_cleanups (cleanup_stack);
100 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
104 struct frame_info *fi;
107 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
110 frame_high = atoi (argv[0]);
112 /* Called with no arguments, it means we want the real depth of
116 for (i = 0, fi = get_current_frame ();
117 fi && (i < frame_high || frame_high == -1);
118 i++, fi = get_prev_frame (fi))
121 ui_out_field_int (current_uiout, "depth", i);
124 static enum print_values
125 parse_print_values (char *name)
127 if (strcmp (name, "0") == 0
128 || strcmp (name, mi_no_values) == 0)
129 return PRINT_NO_VALUES;
130 else if (strcmp (name, "1") == 0
131 || strcmp (name, mi_all_values) == 0)
132 return PRINT_ALL_VALUES;
133 else if (strcmp (name, "2") == 0
134 || strcmp (name, mi_simple_values) == 0)
135 return PRINT_SIMPLE_VALUES;
137 error (_("Unknown value for PRINT_VALUES: must be: \
138 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
139 mi_no_values, mi_all_values, mi_simple_values);
142 /* Print a list of the locals for the current frame. With argument of
143 0, print only the names, with argument of 1 print also the
146 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
148 struct frame_info *frame;
151 error (_("-stack-list-locals: Usage: PRINT_VALUES"));
153 frame = get_selected_frame (NULL);
155 list_args_or_locals (locals, parse_print_values (argv[0]), frame);
158 /* Print a list of the arguments for the current frame. With argument
159 of 0, print only the names, with argument of 1 print also the
162 mi_cmd_stack_list_args (char *command, char **argv, int argc)
167 struct frame_info *fi;
168 struct cleanup *cleanup_stack_args;
169 enum print_values print_values;
170 struct ui_out *uiout = current_uiout;
172 if (argc < 1 || argc > 3 || argc == 2)
173 error (_("-stack-list-arguments: Usage: "
174 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
178 frame_low = atoi (argv[1]);
179 frame_high = atoi (argv[2]);
183 /* Called with no arguments, it means we want args for the whole
189 print_values = parse_print_values (argv[0]);
191 /* Let's position fi on the frame at which to start the
192 display. Could be the innermost frame if the whole stack needs
193 displaying, or if frame_low is 0. */
194 for (i = 0, fi = get_current_frame ();
196 i++, fi = get_prev_frame (fi));
199 error (_("-stack-list-arguments: Not enough frames in stack."));
202 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
204 /* Now let's print the frames up to frame_high, or until there are
205 frames in the stack. */
207 fi && (i <= frame_high || frame_high == -1);
208 i++, fi = get_prev_frame (fi))
210 struct cleanup *cleanup_frame;
213 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
214 ui_out_field_int (uiout, "level", i);
215 list_args_or_locals (arguments, print_values, fi);
216 do_cleanups (cleanup_frame);
219 do_cleanups (cleanup_stack_args);
222 /* Print a list of the local variables (including arguments) for the
223 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
224 or both names and values of the variables must be printed. See
225 parse_print_value for possible values. */
227 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
229 struct frame_info *frame;
232 error (_("Usage: PRINT_VALUES"));
234 frame = get_selected_frame (NULL);
236 list_args_or_locals (all, parse_print_values (argv[0]), frame);
239 /* Print single local or argument. ARG must be already read in. For WHAT and
240 VALUES see list_args_or_locals.
242 Errors are printed as if they would be the parameter value. Use zeroed ARG
243 iff it should not be printed accoring to VALUES. */
246 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
247 enum print_values values)
249 struct cleanup *cleanup_tuple = NULL;
250 struct ui_out *uiout = current_uiout;
251 struct ui_stream *stb = ui_out_stream_new (uiout);
253 gdb_assert (!arg->val || !arg->error);
254 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
255 && arg->error == NULL)
256 || values == PRINT_SIMPLE_VALUES
257 || (values == PRINT_ALL_VALUES
258 && (arg->val != NULL || arg->error != NULL)));
259 gdb_assert (arg->entry_kind == print_entry_values_no
260 || (arg->entry_kind == print_entry_values_only
261 && (arg->val || arg->error)));
263 if (values != PRINT_NO_VALUES || what == all)
264 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
266 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb->stream);
267 if (arg->entry_kind == print_entry_values_only)
268 fputs_filtered ("@entry", stb->stream);
269 ui_out_field_stream (uiout, "name", stb);
271 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
272 ui_out_field_int (uiout, "arg", 1);
274 if (values == PRINT_SIMPLE_VALUES)
276 check_typedef (arg->sym->type);
277 type_print (arg->sym->type, "", stb->stream, -1);
278 ui_out_field_stream (uiout, "type", stb);
281 if (arg->val || arg->error)
283 volatile struct gdb_exception except;
286 except.message = arg->error;
289 /* TRY_CATCH has two statements, wrap it in a block. */
291 TRY_CATCH (except, RETURN_MASK_ERROR)
293 struct value_print_options opts;
295 get_raw_print_options (&opts);
297 common_val_print (arg->val, stb->stream, 0, &opts,
298 language_def (SYMBOL_LANGUAGE (arg->sym)));
302 fprintf_filtered (stb->stream, _("<error reading variable: %s>"),
304 ui_out_field_stream (uiout, "value", stb);
307 ui_out_stream_delete (stb);
308 if (values != PRINT_NO_VALUES || what == all)
309 do_cleanups (cleanup_tuple);
312 /* Print a list of the locals or the arguments for the currently
313 selected frame. If the argument passed is 0, printonly the names
314 of the variables, if an argument of 1 is passed, print the values
317 list_args_or_locals (enum what_to_list what, enum print_values values,
318 struct frame_info *fi)
322 struct dict_iterator iter;
323 struct cleanup *cleanup_list;
324 struct ui_stream *stb;
326 char *name_of_result;
327 struct ui_out *uiout = current_uiout;
329 stb = ui_out_stream_new (uiout);
331 block = get_frame_block (fi, 0);
336 name_of_result = "locals";
339 name_of_result = "args";
342 name_of_result = "variables";
345 internal_error (__FILE__, __LINE__,
346 "unexpected what_to_list: %d", (int) what);
349 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
353 ALL_BLOCK_SYMBOLS (block, iter, sym)
357 switch (SYMBOL_CLASS (sym))
360 case LOC_UNDEF: /* catches errors */
361 case LOC_CONST: /* constant */
362 case LOC_TYPEDEF: /* local typedef */
363 case LOC_LABEL: /* local label */
364 case LOC_BLOCK: /* local function */
365 case LOC_CONST_BYTES: /* loc. byte seq. */
366 case LOC_UNRESOLVED: /* unresolved static */
367 case LOC_OPTIMIZED_OUT: /* optimized out */
371 case LOC_ARG: /* argument */
372 case LOC_REF_ARG: /* reference arg */
373 case LOC_REGPARM_ADDR: /* indirect register arg */
374 case LOC_LOCAL: /* stack local */
375 case LOC_STATIC: /* static */
376 case LOC_REGISTER: /* register */
377 case LOC_COMPUTED: /* computed location */
380 else if (what == locals)
381 print_me = !SYMBOL_IS_ARGUMENT (sym);
383 print_me = SYMBOL_IS_ARGUMENT (sym);
389 struct frame_arg arg, entryarg;
391 if (SYMBOL_IS_ARGUMENT (sym))
392 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
398 memset (&arg, 0, sizeof (arg));
400 arg.entry_kind = print_entry_values_no;
401 memset (&entryarg, 0, sizeof (entryarg));
403 entryarg.entry_kind = print_entry_values_no;
407 case PRINT_SIMPLE_VALUES:
408 type = check_typedef (sym2->type);
409 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
410 && TYPE_CODE (type) != TYPE_CODE_STRUCT
411 && TYPE_CODE (type) != TYPE_CODE_UNION)
413 case PRINT_ALL_VALUES:
414 read_frame_arg (sym2, fi, &arg, &entryarg);
419 if (arg.entry_kind != print_entry_values_only)
420 list_arg_or_local (&arg, what, values);
421 if (entryarg.entry_kind != print_entry_values_no)
422 list_arg_or_local (&entryarg, what, values);
424 xfree (entryarg.error);
427 if (BLOCK_FUNCTION (block))
430 block = BLOCK_SUPERBLOCK (block);
432 do_cleanups (cleanup_list);
433 ui_out_stream_delete (stb);
437 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
439 if (argc == 0 || argc > 1)
440 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
442 select_frame_command (argv[0], 1 /* not used */ );
446 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
449 error (_("-stack-info-frame: No arguments required"));
451 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);