1 /* MI Command Set - stack commands.
2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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 #include "exceptions.h"
36 enum what_to_list
{ locals
, arguments
, all
};
38 static void list_args_or_locals (enum what_to_list what
,
39 int values
, struct frame_info
*fi
);
41 /* Print a list of the stack frames. Args can be none, in which case
42 we want to print the whole backtrace, or a pair of numbers
43 specifying the frame numbers at which to start and stop the
44 display. If the two numbers are equal, a single frame will be
47 mi_cmd_stack_list_frames (char *command
, char **argv
, int argc
)
52 struct cleanup
*cleanup_stack
;
53 struct frame_info
*fi
;
55 if (argc
> 2 || argc
== 1)
56 error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
60 frame_low
= atoi (argv
[0]);
61 frame_high
= atoi (argv
[1]);
65 /* Called with no arguments, it means we want the whole
71 /* Let's position fi on the frame at which to start the
72 display. Could be the innermost frame if the whole stack needs
73 displaying, or if frame_low is 0. */
74 for (i
= 0, fi
= get_current_frame ();
76 i
++, fi
= get_prev_frame (fi
));
79 error (_("-stack-list-frames: Not enough frames in stack."));
81 cleanup_stack
= make_cleanup_ui_out_list_begin_end (uiout
, "stack");
83 /* Now let;s print the frames up to frame_high, or until there are
84 frames in the stack. */
86 fi
&& (i
<= frame_high
|| frame_high
== -1);
87 i
++, fi
= get_prev_frame (fi
))
90 /* Print the location and the address always, even for level 0.
91 args == 0: don't print the arguments. */
92 print_frame_info (fi
, 1, LOC_AND_ADDRESS
, 0 /* args */ );
95 do_cleanups (cleanup_stack
);
99 mi_cmd_stack_info_depth (char *command
, char **argv
, int argc
)
103 struct frame_info
*fi
;
106 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
109 frame_high
= atoi (argv
[0]);
111 /* Called with no arguments, it means we want the real depth of
115 for (i
= 0, fi
= get_current_frame ();
116 fi
&& (i
< frame_high
|| frame_high
== -1);
117 i
++, fi
= get_prev_frame (fi
))
120 ui_out_field_int (uiout
, "depth", i
);
123 static enum print_values
124 parse_print_values (char *name
)
126 if (strcmp (name
, "0") == 0
127 || strcmp (name
, mi_no_values
) == 0)
128 return PRINT_NO_VALUES
;
129 else if (strcmp (name
, "1") == 0
130 || strcmp (name
, mi_all_values
) == 0)
131 return PRINT_ALL_VALUES
;
132 else if (strcmp (name
, "2") == 0
133 || strcmp (name
, mi_simple_values
) == 0)
134 return PRINT_SIMPLE_VALUES
;
136 error (_("Unknown value for PRINT_VALUES: must be: \
137 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
138 mi_no_values
, mi_all_values
, mi_simple_values
);
141 /* Print a list of the locals for the current frame. With argument of
142 0, print only the names, with argument of 1 print also the
145 mi_cmd_stack_list_locals (char *command
, char **argv
, int argc
)
147 struct frame_info
*frame
;
150 error (_("-stack-list-locals: Usage: PRINT_VALUES"));
152 frame
= get_selected_frame (NULL
);
154 list_args_or_locals (locals
, parse_print_values (argv
[0]), frame
);
157 /* Print a list of the arguments for the current frame. With argument
158 of 0, print only the names, with argument of 1 print also the
161 mi_cmd_stack_list_args (char *command
, char **argv
, int argc
)
166 struct frame_info
*fi
;
167 struct cleanup
*cleanup_stack_args
;
168 enum print_values print_values
;
170 if (argc
< 1 || argc
> 3 || argc
== 2)
171 error (_("-stack-list-arguments: Usage: "
172 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
176 frame_low
= atoi (argv
[1]);
177 frame_high
= atoi (argv
[2]);
181 /* Called with no arguments, it means we want args for the whole
187 print_values
= parse_print_values (argv
[0]);
189 /* Let's position fi on the frame at which to start the
190 display. Could be the innermost frame if the whole stack needs
191 displaying, or if frame_low is 0. */
192 for (i
= 0, fi
= get_current_frame ();
194 i
++, fi
= get_prev_frame (fi
));
197 error (_("-stack-list-arguments: Not enough frames in stack."));
200 = make_cleanup_ui_out_list_begin_end (uiout
, "stack-args");
202 /* Now let's print the frames up to frame_high, or until there are
203 frames in the stack. */
205 fi
&& (i
<= frame_high
|| frame_high
== -1);
206 i
++, fi
= get_prev_frame (fi
))
208 struct cleanup
*cleanup_frame
;
211 cleanup_frame
= make_cleanup_ui_out_tuple_begin_end (uiout
, "frame");
212 ui_out_field_int (uiout
, "level", i
);
213 list_args_or_locals (arguments
, print_values
, fi
);
214 do_cleanups (cleanup_frame
);
217 do_cleanups (cleanup_stack_args
);
220 /* Print a list of the local variables (including arguments) for the
221 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
222 or both names and values of the variables must be printed. See
223 parse_print_value for possible values. */
225 mi_cmd_stack_list_variables (char *command
, char **argv
, int argc
)
227 struct frame_info
*frame
;
230 error (_("Usage: PRINT_VALUES"));
232 frame
= get_selected_frame (NULL
);
234 list_args_or_locals (all
, parse_print_values (argv
[0]), frame
);
238 /* Print a list of the locals or the arguments for the currently
239 selected frame. If the argument passed is 0, printonly the names
240 of the variables, if an argument of 1 is passed, print the values
243 list_args_or_locals (enum what_to_list what
, int values
, struct frame_info
*fi
)
247 struct dict_iterator iter
;
248 struct cleanup
*cleanup_list
;
249 static struct ui_stream
*stb
= NULL
;
251 char *name_of_result
;
253 stb
= ui_out_stream_new (uiout
);
255 block
= get_frame_block (fi
, 0);
260 name_of_result
= "locals";
263 name_of_result
= "args";
266 name_of_result
= "variables";
269 internal_error (__FILE__
, __LINE__
,
270 "unexpected what_to_list: %d", (int) what
);
273 cleanup_list
= make_cleanup_ui_out_list_begin_end (uiout
, name_of_result
);
277 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
281 switch (SYMBOL_CLASS (sym
))
284 case LOC_UNDEF
: /* catches errors */
285 case LOC_CONST
: /* constant */
286 case LOC_TYPEDEF
: /* local typedef */
287 case LOC_LABEL
: /* local label */
288 case LOC_BLOCK
: /* local function */
289 case LOC_CONST_BYTES
: /* loc. byte seq. */
290 case LOC_UNRESOLVED
: /* unresolved static */
291 case LOC_OPTIMIZED_OUT
: /* optimized out */
295 case LOC_ARG
: /* argument */
296 case LOC_REF_ARG
: /* reference arg */
297 case LOC_REGPARM_ADDR
: /* indirect register arg */
298 case LOC_LOCAL
: /* stack local */
299 case LOC_STATIC
: /* static */
300 case LOC_REGISTER
: /* register */
301 case LOC_COMPUTED
: /* computed location */
304 else if (what
== locals
)
305 print_me
= !SYMBOL_IS_ARGUMENT (sym
);
307 print_me
= SYMBOL_IS_ARGUMENT (sym
);
312 struct cleanup
*cleanup_tuple
= NULL
;
316 if (values
!= PRINT_NO_VALUES
|| what
== all
)
318 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
319 ui_out_field_string (uiout
, "name", SYMBOL_PRINT_NAME (sym
));
320 if (what
== all
&& SYMBOL_IS_ARGUMENT (sym
))
321 ui_out_field_int (uiout
, "arg", 1);
323 if (SYMBOL_IS_ARGUMENT (sym
))
324 sym2
= lookup_symbol (SYMBOL_NATURAL_NAME (sym
),
331 case PRINT_SIMPLE_VALUES
:
332 type
= check_typedef (sym2
->type
);
333 type_print (sym2
->type
, "", stb
->stream
, -1);
334 ui_out_field_stream (uiout
, "type", stb
);
335 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
336 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
337 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
339 volatile struct gdb_exception except
;
341 TRY_CATCH (except
, RETURN_MASK_ERROR
)
343 struct value_print_options opts
;
345 val
= read_var_value (sym2
, fi
);
346 get_raw_print_options (&opts
);
349 (val
, stb
->stream
, 0, &opts
,
350 language_def (SYMBOL_LANGUAGE (sym2
)));
352 if (except
.reason
< 0)
353 fprintf_filtered (stb
->stream
,
354 _("<error reading variable: %s>"),
357 ui_out_field_stream (uiout
, "value", stb
);
360 case PRINT_ALL_VALUES
:
362 volatile struct gdb_exception except
;
364 TRY_CATCH (except
, RETURN_MASK_ERROR
)
366 struct value_print_options opts
;
368 val
= read_var_value (sym2
, fi
);
369 get_raw_print_options (&opts
);
372 (val
, stb
->stream
, 0, &opts
,
373 language_def (SYMBOL_LANGUAGE (sym2
)));
375 if (except
.reason
< 0)
376 fprintf_filtered (stb
->stream
,
377 _("<error reading variable: %s>"),
380 ui_out_field_stream (uiout
, "value", stb
);
385 if (values
!= PRINT_NO_VALUES
|| what
== all
)
386 do_cleanups (cleanup_tuple
);
389 if (BLOCK_FUNCTION (block
))
392 block
= BLOCK_SUPERBLOCK (block
);
394 do_cleanups (cleanup_list
);
395 ui_out_stream_delete (stb
);
399 mi_cmd_stack_select_frame (char *command
, char **argv
, int argc
)
401 if (argc
== 0 || argc
> 1)
402 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
404 select_frame_command (argv
[0], 1 /* not used */ );
408 mi_cmd_stack_info_frame (char *command
, char **argv
, int argc
)
411 error (_("-stack-info-frame: No arguments required"));
413 print_frame_info (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 0);