1 /* MI Command Set - stack commands.
2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
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 static void list_args_or_locals (int locals, int values, struct frame_info *fi);
36 /* Print a list of the stack frames. Args can be none, in which case
37 we want to print the whole backtrace, or a pair of numbers
38 specifying the frame numbers at which to start and stop the
39 display. If the two numbers are equal, a single frame will be
42 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
47 struct cleanup *cleanup_stack;
48 struct frame_info *fi;
50 if (argc > 2 || argc == 1)
51 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
55 frame_low = atoi (argv[0]);
56 frame_high = atoi (argv[1]);
60 /* Called with no arguments, it means we want the whole
66 /* Let's position fi on the frame at which to start the
67 display. Could be the innermost frame if the whole stack needs
68 displaying, or if frame_low is 0. */
69 for (i = 0, fi = get_current_frame ();
71 i++, fi = get_prev_frame (fi));
74 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
76 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
78 /* Now let;s print the frames up to frame_high, or until there are
79 frames in the stack. */
81 fi && (i <= frame_high || frame_high == -1);
82 i++, fi = get_prev_frame (fi))
85 /* Print the location and the address always, even for level 0.
86 args == 0: don't print the arguments. */
87 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
90 do_cleanups (cleanup_stack);
96 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
100 struct frame_info *fi;
103 error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
106 frame_high = atoi (argv[0]);
108 /* Called with no arguments, it means we want the real depth of
112 for (i = 0, fi = get_current_frame ();
113 fi && (i < frame_high || frame_high == -1);
114 i++, fi = get_prev_frame (fi))
117 ui_out_field_int (uiout, "depth", i);
122 /* Print a list of the locals for the current frame. With argument of
123 0, print only the names, with argument of 1 print also the
126 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
128 struct frame_info *frame;
129 enum print_values print_values;
132 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
134 frame = get_selected_frame (NULL);
136 if (strcmp (argv[0], "0") == 0
137 || strcmp (argv[0], mi_no_values) == 0)
138 print_values = PRINT_NO_VALUES;
139 else if (strcmp (argv[0], "1") == 0
140 || strcmp (argv[0], mi_all_values) == 0)
141 print_values = PRINT_ALL_VALUES;
142 else if (strcmp (argv[0], "2") == 0
143 || strcmp (argv[0], mi_simple_values) == 0)
144 print_values = PRINT_SIMPLE_VALUES;
146 error (_("Unknown value for PRINT_VALUES: must be: \
147 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
148 mi_no_values, mi_all_values, mi_simple_values);
149 list_args_or_locals (1, print_values, frame);
153 /* Print a list of the arguments for the current frame. With argument
154 of 0, print only the names, with argument of 1 print also the
157 mi_cmd_stack_list_args (char *command, char **argv, int argc)
162 struct frame_info *fi;
163 struct cleanup *cleanup_stack_args;
165 if (argc < 1 || argc > 3 || argc == 2)
166 error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
170 frame_low = atoi (argv[1]);
171 frame_high = atoi (argv[2]);
175 /* Called with no arguments, it means we want args for the whole
181 /* Let's position fi on the frame at which to start the
182 display. Could be the innermost frame if the whole stack needs
183 displaying, or if frame_low is 0. */
184 for (i = 0, fi = get_current_frame ();
186 i++, fi = get_prev_frame (fi));
189 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
191 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
193 /* Now let's print the frames up to frame_high, or until there are
194 frames in the stack. */
196 fi && (i <= frame_high || frame_high == -1);
197 i++, fi = get_prev_frame (fi))
199 struct cleanup *cleanup_frame;
201 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
202 ui_out_field_int (uiout, "level", i);
203 list_args_or_locals (0, atoi (argv[0]), fi);
204 do_cleanups (cleanup_frame);
207 do_cleanups (cleanup_stack_args);
212 /* Print a list of the locals or the arguments for the currently
213 selected frame. If the argument passed is 0, printonly the names
214 of the variables, if an argument of 1 is passed, print the values
217 list_args_or_locals (int locals, int values, struct frame_info *fi)
221 struct dict_iterator iter;
223 struct cleanup *cleanup_list;
224 static struct ui_stream *stb = NULL;
227 stb = ui_out_stream_new (uiout);
229 block = get_frame_block (fi, 0);
231 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
235 ALL_BLOCK_SYMBOLS (block, iter, sym)
239 switch (SYMBOL_CLASS (sym))
242 case LOC_UNDEF: /* catches errors */
243 case LOC_CONST: /* constant */
244 case LOC_TYPEDEF: /* local typedef */
245 case LOC_LABEL: /* local label */
246 case LOC_BLOCK: /* local function */
247 case LOC_CONST_BYTES: /* loc. byte seq. */
248 case LOC_UNRESOLVED: /* unresolved static */
249 case LOC_OPTIMIZED_OUT: /* optimized out */
253 case LOC_ARG: /* argument */
254 case LOC_REF_ARG: /* reference arg */
255 case LOC_REGPARM: /* register arg */
256 case LOC_REGPARM_ADDR: /* indirect register arg */
257 case LOC_LOCAL_ARG: /* stack arg */
258 case LOC_BASEREG_ARG: /* basereg arg */
259 case LOC_COMPUTED_ARG: /* arg with computed location */
264 case LOC_LOCAL: /* stack local */
265 case LOC_BASEREG: /* basereg local */
266 case LOC_STATIC: /* static */
267 case LOC_REGISTER: /* register */
268 case LOC_COMPUTED: /* computed location */
275 struct cleanup *cleanup_tuple = NULL;
278 if (values != PRINT_NO_VALUES)
280 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
281 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
284 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
291 case PRINT_SIMPLE_VALUES:
292 type = check_typedef (sym2->type);
293 type_print (sym2->type, "", stb->stream, -1);
294 ui_out_field_stream (uiout, "type", stb);
295 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
296 && TYPE_CODE (type) != TYPE_CODE_STRUCT
297 && TYPE_CODE (type) != TYPE_CODE_UNION)
299 val = read_var_value (sym2, fi);
301 (val, stb->stream, 0, 1, 0, Val_no_prettyprint,
302 language_def (SYMBOL_LANGUAGE (sym2)));
303 ui_out_field_stream (uiout, "value", stb);
305 do_cleanups (cleanup_tuple);
307 case PRINT_ALL_VALUES:
308 val = read_var_value (sym2, fi);
310 (val, stb->stream, 0, 1, 0, Val_no_prettyprint,
311 language_def (SYMBOL_LANGUAGE (sym2)));
312 ui_out_field_stream (uiout, "value", stb);
313 do_cleanups (cleanup_tuple);
318 if (BLOCK_FUNCTION (block))
321 block = BLOCK_SUPERBLOCK (block);
323 do_cleanups (cleanup_list);
324 ui_out_stream_delete (stb);
328 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
330 if (argc == 0 || argc > 1)
331 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
333 select_frame_command (argv[0], 1 /* not used */ );
338 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
341 error (_("mi_cmd_stack_info_frame: No arguments required"));
343 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);