* options.h (DEFINE_var): Add set_user_set_##varname__.
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
9b254dd1 2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008
6aba47ca 3 Free Software Foundation, Inc.
ab91fdd5 4 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
20
21#include "defs.h"
22#include "target.h"
23#include "frame.h"
24#include "value.h"
25#include "mi-cmds.h"
26#include "ui-out.h"
e88c90f2 27#include "symtab.h"
fe898f56 28#include "block.h"
b9362cc7 29#include "stack.h"
de4f826b 30#include "dictionary.h"
f5ec2042 31#include "gdb_string.h"
fb40c209
AC
32
33static void list_args_or_locals (int locals, int values, struct frame_info *fi);
34
35/* Print a list of the stack frames. Args can be none, in which case
36 we want to print the whole backtrace, or a pair of numbers
37 specifying the frame numbers at which to start and stop the
38 display. If the two numbers are equal, a single frame will be
39 displayed. */
40enum mi_cmd_result
41mi_cmd_stack_list_frames (char *command, char **argv, int argc)
42{
43 int frame_low;
44 int frame_high;
45 int i;
6ad4a2cf 46 struct cleanup *cleanup_stack;
fb40c209
AC
47 struct frame_info *fi;
48
fb40c209 49 if (argc > 2 || argc == 1)
8a3fe4f8 50 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
51
52 if (argc == 2)
53 {
54 frame_low = atoi (argv[0]);
55 frame_high = atoi (argv[1]);
56 }
57 else
58 {
59 /* Called with no arguments, it means we want the whole
60 backtrace. */
61 frame_low = -1;
62 frame_high = -1;
63 }
64
65 /* Let's position fi on the frame at which to start the
66 display. Could be the innermost frame if the whole stack needs
67 displaying, or if frame_low is 0. */
68 for (i = 0, fi = get_current_frame ();
69 fi && i < frame_low;
70 i++, fi = get_prev_frame (fi));
71
72 if (fi == NULL)
8a3fe4f8 73 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
fb40c209 74
6ad4a2cf 75 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
fb40c209
AC
76
77 /* Now let;s print the frames up to frame_high, or until there are
78 frames in the stack. */
79 for (;
80 fi && (i <= frame_high || frame_high == -1);
81 i++, fi = get_prev_frame (fi))
82 {
83 QUIT;
0faf0076 84 /* Print the location and the address always, even for level 0.
fb40c209 85 args == 0: don't print the arguments. */
0faf0076 86 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
87 }
88
6ad4a2cf 89 do_cleanups (cleanup_stack);
fb40c209
AC
90
91 return MI_CMD_DONE;
92}
93
94enum mi_cmd_result
95mi_cmd_stack_info_depth (char *command, char **argv, int argc)
96{
97 int frame_high;
98 int i;
99 struct frame_info *fi;
100
fb40c209 101 if (argc > 1)
8a3fe4f8 102 error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
103
104 if (argc == 1)
105 frame_high = atoi (argv[0]);
106 else
107 /* Called with no arguments, it means we want the real depth of
108 the stack. */
109 frame_high = -1;
110
111 for (i = 0, fi = get_current_frame ();
112 fi && (i < frame_high || frame_high == -1);
113 i++, fi = get_prev_frame (fi))
114 QUIT;
115
116 ui_out_field_int (uiout, "depth", i);
117
118 return MI_CMD_DONE;
119}
120
121/* Print a list of the locals for the current frame. With argument of
122 0, print only the names, with argument of 1 print also the
123 values. */
124enum mi_cmd_result
125mi_cmd_stack_list_locals (char *command, char **argv, int argc)
126{
f5ec2042
NR
127 struct frame_info *frame;
128 enum print_values print_values;
129
fb40c209 130 if (argc != 1)
8a3fe4f8 131 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
fb40c209 132
b04f3ab4 133 frame = get_selected_frame (NULL);
f5ec2042
NR
134
135 if (strcmp (argv[0], "0") == 0
1ecb4ee0 136 || strcmp (argv[0], mi_no_values) == 0)
f5ec2042
NR
137 print_values = PRINT_NO_VALUES;
138 else if (strcmp (argv[0], "1") == 0
1ecb4ee0 139 || strcmp (argv[0], mi_all_values) == 0)
f5ec2042
NR
140 print_values = PRINT_ALL_VALUES;
141 else if (strcmp (argv[0], "2") == 0
1ecb4ee0 142 || strcmp (argv[0], mi_simple_values) == 0)
f5ec2042
NR
143 print_values = PRINT_SIMPLE_VALUES;
144 else
1ecb4ee0
DJ
145 error (_("Unknown value for PRINT_VALUES: must be: \
1460 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
147 mi_no_values, mi_all_values, mi_simple_values);
f5ec2042 148 list_args_or_locals (1, print_values, frame);
fb40c209
AC
149 return MI_CMD_DONE;
150}
151
152/* Print a list of the arguments for the current frame. With argument
153 of 0, print only the names, with argument of 1 print also the
154 values. */
155enum mi_cmd_result
156mi_cmd_stack_list_args (char *command, char **argv, int argc)
157{
158 int frame_low;
159 int frame_high;
160 int i;
161 struct frame_info *fi;
6ad4a2cf 162 struct cleanup *cleanup_stack_args;
fb40c209
AC
163
164 if (argc < 1 || argc > 3 || argc == 2)
8a3fe4f8 165 error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
166
167 if (argc == 3)
168 {
169 frame_low = atoi (argv[1]);
170 frame_high = atoi (argv[2]);
171 }
172 else
173 {
174 /* Called with no arguments, it means we want args for the whole
175 backtrace. */
176 frame_low = -1;
177 frame_high = -1;
178 }
179
180 /* Let's position fi on the frame at which to start the
181 display. Could be the innermost frame if the whole stack needs
182 displaying, or if frame_low is 0. */
183 for (i = 0, fi = get_current_frame ();
184 fi && i < frame_low;
185 i++, fi = get_prev_frame (fi));
186
187 if (fi == NULL)
8a3fe4f8 188 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
fb40c209 189
6ad4a2cf 190 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
191
192 /* Now let's print the frames up to frame_high, or until there are
193 frames in the stack. */
194 for (;
195 fi && (i <= frame_high || frame_high == -1);
196 i++, fi = get_prev_frame (fi))
197 {
6ad4a2cf 198 struct cleanup *cleanup_frame;
fb40c209 199 QUIT;
6ad4a2cf 200 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209
AC
201 ui_out_field_int (uiout, "level", i);
202 list_args_or_locals (0, atoi (argv[0]), fi);
6ad4a2cf 203 do_cleanups (cleanup_frame);
fb40c209
AC
204 }
205
6ad4a2cf 206 do_cleanups (cleanup_stack_args);
fb40c209
AC
207
208 return MI_CMD_DONE;
209}
210
211/* Print a list of the locals or the arguments for the currently
212 selected frame. If the argument passed is 0, printonly the names
213 of the variables, if an argument of 1 is passed, print the values
214 as well. */
215static void
216list_args_or_locals (int locals, int values, struct frame_info *fi)
217{
218 struct block *block;
219 struct symbol *sym;
de4f826b
DC
220 struct dict_iterator iter;
221 int nsyms;
6ad4a2cf 222 struct cleanup *cleanup_list;
fb40c209 223 static struct ui_stream *stb = NULL;
f5ec2042 224 struct type *type;
fb40c209
AC
225
226 stb = ui_out_stream_new (uiout);
227
ae767bfb 228 block = get_frame_block (fi, 0);
fb40c209 229
6ad4a2cf 230 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
fb40c209
AC
231
232 while (block != 0)
233 {
de4f826b 234 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 235 {
39bf4652
JB
236 int print_me = 0;
237
fb40c209
AC
238 switch (SYMBOL_CLASS (sym))
239 {
240 default:
241 case LOC_UNDEF: /* catches errors */
242 case LOC_CONST: /* constant */
243 case LOC_TYPEDEF: /* local typedef */
244 case LOC_LABEL: /* local label */
245 case LOC_BLOCK: /* local function */
246 case LOC_CONST_BYTES: /* loc. byte seq. */
247 case LOC_UNRESOLVED: /* unresolved static */
248 case LOC_OPTIMIZED_OUT: /* optimized out */
249 print_me = 0;
250 break;
251
252 case LOC_ARG: /* argument */
253 case LOC_REF_ARG: /* reference arg */
254 case LOC_REGPARM: /* register arg */
255 case LOC_REGPARM_ADDR: /* indirect register arg */
256 case LOC_LOCAL_ARG: /* stack arg */
257 case LOC_BASEREG_ARG: /* basereg arg */
4cf623b6 258 case LOC_COMPUTED_ARG: /* arg with computed location */
fb40c209
AC
259 if (!locals)
260 print_me = 1;
261 break;
262
263 case LOC_LOCAL: /* stack local */
264 case LOC_BASEREG: /* basereg local */
265 case LOC_STATIC: /* static */
266 case LOC_REGISTER: /* register */
4cf623b6 267 case LOC_COMPUTED: /* computed location */
fb40c209
AC
268 if (locals)
269 print_me = 1;
270 break;
271 }
272 if (print_me)
273 {
6ad4a2cf 274 struct cleanup *cleanup_tuple = NULL;
6bb0384f 275 struct symbol *sym2;
9fbcbb40 276 struct value *val;
f5ec2042
NR
277 if (values != PRINT_NO_VALUES)
278 cleanup_tuple =
6ad4a2cf 279 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
f5ec2042 280 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
fb40c209 281
f5ec2042
NR
282 if (!locals)
283 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
284 block, VAR_DOMAIN,
285 (int *) NULL,
286 (struct symtab **) NULL);
287 else
fb40c209 288 sym2 = sym;
f5ec2042
NR
289 switch (values)
290 {
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)
298 {
9fbcbb40
NR
299 val = read_var_value (sym2, fi);
300 common_val_print
301 (val, stb->stream, 0, 1, 0, Val_no_prettyprint);
f5ec2042
NR
302 ui_out_field_stream (uiout, "value", stb);
303 }
304 do_cleanups (cleanup_tuple);
305 break;
306 case PRINT_ALL_VALUES:
9fbcbb40
NR
307 val = read_var_value (sym2, fi);
308 common_val_print
309 (val, stb->stream, 0, 1, 0, Val_no_prettyprint);
fb40c209 310 ui_out_field_stream (uiout, "value", stb);
6ad4a2cf 311 do_cleanups (cleanup_tuple);
f5ec2042 312 break;
fb40c209
AC
313 }
314 }
315 }
316 if (BLOCK_FUNCTION (block))
317 break;
318 else
319 block = BLOCK_SUPERBLOCK (block);
320 }
6ad4a2cf 321 do_cleanups (cleanup_list);
fb40c209
AC
322 ui_out_stream_delete (stb);
323}
324
325enum mi_cmd_result
326mi_cmd_stack_select_frame (char *command, char **argv, int argc)
327{
fcf43932
NR
328 if (argc == 0 || argc > 1)
329 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
fb40c209 330
fcf43932 331 select_frame_command (argv[0], 1 /* not used */ );
fb40c209
AC
332 return MI_CMD_DONE;
333}
64fd8944
NR
334
335enum mi_cmd_result
336mi_cmd_stack_info_frame (char *command, char **argv, int argc)
337{
338 if (argc > 0)
339 error (_("mi_cmd_stack_info_frame: No arguments required"));
340
341 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
342 return MI_CMD_DONE;
343}
This page took 0.671464 seconds and 4 git commands to generate.