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