2008-05-27 Martin Schwidefsky <schwidefsky@de.ibm.com>
[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"
d8ca156b 32#include "language.h"
fb40c209
AC
33
34static void list_args_or_locals (int locals, int values, struct frame_info *fi);
35
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
40 displayed. */
41enum mi_cmd_result
42mi_cmd_stack_list_frames (char *command, char **argv, int argc)
43{
44 int frame_low;
45 int frame_high;
46 int i;
6ad4a2cf 47 struct cleanup *cleanup_stack;
fb40c209
AC
48 struct frame_info *fi;
49
fb40c209 50 if (argc > 2 || argc == 1)
8a3fe4f8 51 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
52
53 if (argc == 2)
54 {
55 frame_low = atoi (argv[0]);
56 frame_high = atoi (argv[1]);
57 }
58 else
59 {
60 /* Called with no arguments, it means we want the whole
61 backtrace. */
62 frame_low = -1;
63 frame_high = -1;
64 }
65
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 ();
70 fi && i < frame_low;
71 i++, fi = get_prev_frame (fi));
72
73 if (fi == NULL)
8a3fe4f8 74 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
fb40c209 75
6ad4a2cf 76 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
fb40c209
AC
77
78 /* Now let;s print the frames up to frame_high, or until there are
79 frames in the stack. */
80 for (;
81 fi && (i <= frame_high || frame_high == -1);
82 i++, fi = get_prev_frame (fi))
83 {
84 QUIT;
0faf0076 85 /* Print the location and the address always, even for level 0.
fb40c209 86 args == 0: don't print the arguments. */
0faf0076 87 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
88 }
89
6ad4a2cf 90 do_cleanups (cleanup_stack);
fb40c209
AC
91
92 return MI_CMD_DONE;
93}
94
95enum mi_cmd_result
96mi_cmd_stack_info_depth (char *command, char **argv, int argc)
97{
98 int frame_high;
99 int i;
100 struct frame_info *fi;
101
fb40c209 102 if (argc > 1)
8a3fe4f8 103 error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
104
105 if (argc == 1)
106 frame_high = atoi (argv[0]);
107 else
108 /* Called with no arguments, it means we want the real depth of
109 the stack. */
110 frame_high = -1;
111
112 for (i = 0, fi = get_current_frame ();
113 fi && (i < frame_high || frame_high == -1);
114 i++, fi = get_prev_frame (fi))
115 QUIT;
116
117 ui_out_field_int (uiout, "depth", i);
118
119 return MI_CMD_DONE;
120}
121
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
124 values. */
125enum mi_cmd_result
126mi_cmd_stack_list_locals (char *command, char **argv, int argc)
127{
f5ec2042
NR
128 struct frame_info *frame;
129 enum print_values print_values;
130
fb40c209 131 if (argc != 1)
8a3fe4f8 132 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
fb40c209 133
b04f3ab4 134 frame = get_selected_frame (NULL);
f5ec2042
NR
135
136 if (strcmp (argv[0], "0") == 0
1ecb4ee0 137 || strcmp (argv[0], mi_no_values) == 0)
f5ec2042
NR
138 print_values = PRINT_NO_VALUES;
139 else if (strcmp (argv[0], "1") == 0
1ecb4ee0 140 || strcmp (argv[0], mi_all_values) == 0)
f5ec2042
NR
141 print_values = PRINT_ALL_VALUES;
142 else if (strcmp (argv[0], "2") == 0
1ecb4ee0 143 || strcmp (argv[0], mi_simple_values) == 0)
f5ec2042
NR
144 print_values = PRINT_SIMPLE_VALUES;
145 else
1ecb4ee0
DJ
146 error (_("Unknown value for PRINT_VALUES: must be: \
1470 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
148 mi_no_values, mi_all_values, mi_simple_values);
f5ec2042 149 list_args_or_locals (1, print_values, frame);
fb40c209
AC
150 return MI_CMD_DONE;
151}
152
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
155 values. */
156enum mi_cmd_result
157mi_cmd_stack_list_args (char *command, char **argv, int argc)
158{
159 int frame_low;
160 int frame_high;
161 int i;
162 struct frame_info *fi;
6ad4a2cf 163 struct cleanup *cleanup_stack_args;
fb40c209
AC
164
165 if (argc < 1 || argc > 3 || argc == 2)
8a3fe4f8 166 error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
167
168 if (argc == 3)
169 {
170 frame_low = atoi (argv[1]);
171 frame_high = atoi (argv[2]);
172 }
173 else
174 {
175 /* Called with no arguments, it means we want args for the whole
176 backtrace. */
177 frame_low = -1;
178 frame_high = -1;
179 }
180
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 ();
185 fi && i < frame_low;
186 i++, fi = get_prev_frame (fi));
187
188 if (fi == NULL)
8a3fe4f8 189 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
fb40c209 190
6ad4a2cf 191 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
192
193 /* Now let's print the frames up to frame_high, or until there are
194 frames in the stack. */
195 for (;
196 fi && (i <= frame_high || frame_high == -1);
197 i++, fi = get_prev_frame (fi))
198 {
6ad4a2cf 199 struct cleanup *cleanup_frame;
fb40c209 200 QUIT;
6ad4a2cf 201 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209
AC
202 ui_out_field_int (uiout, "level", i);
203 list_args_or_locals (0, atoi (argv[0]), fi);
6ad4a2cf 204 do_cleanups (cleanup_frame);
fb40c209
AC
205 }
206
6ad4a2cf 207 do_cleanups (cleanup_stack_args);
fb40c209
AC
208
209 return MI_CMD_DONE;
210}
211
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
215 as well. */
216static void
217list_args_or_locals (int locals, int values, struct frame_info *fi)
218{
219 struct block *block;
220 struct symbol *sym;
de4f826b
DC
221 struct dict_iterator iter;
222 int nsyms;
6ad4a2cf 223 struct cleanup *cleanup_list;
fb40c209 224 static struct ui_stream *stb = NULL;
f5ec2042 225 struct type *type;
fb40c209
AC
226
227 stb = ui_out_stream_new (uiout);
228
ae767bfb 229 block = get_frame_block (fi, 0);
fb40c209 230
6ad4a2cf 231 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
fb40c209
AC
232
233 while (block != 0)
234 {
de4f826b 235 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 236 {
39bf4652
JB
237 int print_me = 0;
238
fb40c209
AC
239 switch (SYMBOL_CLASS (sym))
240 {
241 default:
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 */
250 print_me = 0;
251 break;
252
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 */
4cf623b6 257 case LOC_COMPUTED_ARG: /* arg with computed location */
fb40c209
AC
258 if (!locals)
259 print_me = 1;
260 break;
261
262 case LOC_LOCAL: /* stack local */
fb40c209
AC
263 case LOC_STATIC: /* static */
264 case LOC_REGISTER: /* register */
4cf623b6 265 case LOC_COMPUTED: /* computed location */
fb40c209
AC
266 if (locals)
267 print_me = 1;
268 break;
269 }
270 if (print_me)
271 {
6ad4a2cf 272 struct cleanup *cleanup_tuple = NULL;
6bb0384f 273 struct symbol *sym2;
9fbcbb40 274 struct value *val;
f5ec2042
NR
275 if (values != PRINT_NO_VALUES)
276 cleanup_tuple =
6ad4a2cf 277 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
f5ec2042 278 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
fb40c209 279
f5ec2042
NR
280 if (!locals)
281 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
282 block, VAR_DOMAIN,
2570f2b7 283 (int *) NULL);
f5ec2042 284 else
fb40c209 285 sym2 = sym;
f5ec2042
NR
286 switch (values)
287 {
288 case PRINT_SIMPLE_VALUES:
289 type = check_typedef (sym2->type);
290 type_print (sym2->type, "", stb->stream, -1);
291 ui_out_field_stream (uiout, "type", stb);
292 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
293 && TYPE_CODE (type) != TYPE_CODE_STRUCT
294 && TYPE_CODE (type) != TYPE_CODE_UNION)
295 {
9fbcbb40
NR
296 val = read_var_value (sym2, fi);
297 common_val_print
d8ca156b
JB
298 (val, stb->stream, 0, 1, 0, Val_no_prettyprint,
299 language_def (SYMBOL_LANGUAGE (sym2)));
f5ec2042
NR
300 ui_out_field_stream (uiout, "value", stb);
301 }
302 do_cleanups (cleanup_tuple);
303 break;
304 case PRINT_ALL_VALUES:
9fbcbb40
NR
305 val = read_var_value (sym2, fi);
306 common_val_print
d8ca156b
JB
307 (val, stb->stream, 0, 1, 0, Val_no_prettyprint,
308 language_def (SYMBOL_LANGUAGE (sym2)));
fb40c209 309 ui_out_field_stream (uiout, "value", stb);
6ad4a2cf 310 do_cleanups (cleanup_tuple);
f5ec2042 311 break;
fb40c209
AC
312 }
313 }
314 }
315 if (BLOCK_FUNCTION (block))
316 break;
317 else
318 block = BLOCK_SUPERBLOCK (block);
319 }
6ad4a2cf 320 do_cleanups (cleanup_list);
fb40c209
AC
321 ui_out_stream_delete (stb);
322}
323
324enum mi_cmd_result
325mi_cmd_stack_select_frame (char *command, char **argv, int argc)
326{
fcf43932
NR
327 if (argc == 0 || argc > 1)
328 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
fb40c209 329
fcf43932 330 select_frame_command (argv[0], 1 /* not used */ );
fb40c209
AC
331 return MI_CMD_DONE;
332}
64fd8944
NR
333
334enum mi_cmd_result
335mi_cmd_stack_info_frame (char *command, char **argv, int argc)
336{
337 if (argc > 0)
338 error (_("mi_cmd_stack_info_frame: No arguments required"));
339
340 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
341 return MI_CMD_DONE;
342}
This page took 0.611788 seconds and 4 git commands to generate.