Implement -exec-jump.
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
0fb0cc75 2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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"
79a45b7d 33#include "valprint.h"
fb40c209
AC
34
35static void list_args_or_locals (int locals, int values, struct frame_info *fi);
36
37/* Print a list of the stack frames. Args can be none, in which case
38 we want to print the whole backtrace, or a pair of numbers
39 specifying the frame numbers at which to start and stop the
40 display. If the two numbers are equal, a single frame will be
41 displayed. */
ce8f13f8 42void
fb40c209
AC
43mi_cmd_stack_list_frames (char *command, char **argv, int argc)
44{
45 int frame_low;
46 int frame_high;
47 int i;
6ad4a2cf 48 struct cleanup *cleanup_stack;
fb40c209
AC
49 struct frame_info *fi;
50
fb40c209 51 if (argc > 2 || argc == 1)
8a3fe4f8 52 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
53
54 if (argc == 2)
55 {
56 frame_low = atoi (argv[0]);
57 frame_high = atoi (argv[1]);
58 }
59 else
60 {
61 /* Called with no arguments, it means we want the whole
62 backtrace. */
63 frame_low = -1;
64 frame_high = -1;
65 }
66
67 /* Let's position fi on the frame at which to start the
68 display. Could be the innermost frame if the whole stack needs
69 displaying, or if frame_low is 0. */
70 for (i = 0, fi = get_current_frame ();
71 fi && i < frame_low;
72 i++, fi = get_prev_frame (fi));
73
74 if (fi == NULL)
8a3fe4f8 75 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
fb40c209 76
6ad4a2cf 77 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
fb40c209
AC
78
79 /* Now let;s print the frames up to frame_high, or until there are
80 frames in the stack. */
81 for (;
82 fi && (i <= frame_high || frame_high == -1);
83 i++, fi = get_prev_frame (fi))
84 {
85 QUIT;
0faf0076 86 /* Print the location and the address always, even for level 0.
fb40c209 87 args == 0: don't print the arguments. */
0faf0076 88 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
89 }
90
6ad4a2cf 91 do_cleanups (cleanup_stack);
fb40c209
AC
92}
93
ce8f13f8 94void
fb40c209
AC
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);
fb40c209
AC
117}
118
119/* Print a list of the locals for the current frame. With argument of
120 0, print only the names, with argument of 1 print also the
121 values. */
ce8f13f8 122void
fb40c209
AC
123mi_cmd_stack_list_locals (char *command, char **argv, int argc)
124{
f5ec2042
NR
125 struct frame_info *frame;
126 enum print_values print_values;
127
fb40c209 128 if (argc != 1)
8a3fe4f8 129 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
fb40c209 130
b04f3ab4 131 frame = get_selected_frame (NULL);
f5ec2042
NR
132
133 if (strcmp (argv[0], "0") == 0
1ecb4ee0 134 || strcmp (argv[0], mi_no_values) == 0)
f5ec2042
NR
135 print_values = PRINT_NO_VALUES;
136 else if (strcmp (argv[0], "1") == 0
1ecb4ee0 137 || strcmp (argv[0], mi_all_values) == 0)
f5ec2042
NR
138 print_values = PRINT_ALL_VALUES;
139 else if (strcmp (argv[0], "2") == 0
1ecb4ee0 140 || strcmp (argv[0], mi_simple_values) == 0)
f5ec2042
NR
141 print_values = PRINT_SIMPLE_VALUES;
142 else
1ecb4ee0
DJ
143 error (_("Unknown value for PRINT_VALUES: must be: \
1440 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
145 mi_no_values, mi_all_values, mi_simple_values);
f5ec2042 146 list_args_or_locals (1, print_values, frame);
fb40c209
AC
147}
148
149/* Print a list of the arguments for the current frame. With argument
150 of 0, print only the names, with argument of 1 print also the
151 values. */
ce8f13f8 152void
fb40c209
AC
153mi_cmd_stack_list_args (char *command, char **argv, int argc)
154{
155 int frame_low;
156 int frame_high;
157 int i;
158 struct frame_info *fi;
6ad4a2cf 159 struct cleanup *cleanup_stack_args;
fb40c209
AC
160
161 if (argc < 1 || argc > 3 || argc == 2)
8a3fe4f8 162 error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
163
164 if (argc == 3)
165 {
166 frame_low = atoi (argv[1]);
167 frame_high = atoi (argv[2]);
168 }
169 else
170 {
171 /* Called with no arguments, it means we want args for the whole
172 backtrace. */
173 frame_low = -1;
174 frame_high = -1;
175 }
176
177 /* Let's position fi on the frame at which to start the
178 display. Could be the innermost frame if the whole stack needs
179 displaying, or if frame_low is 0. */
180 for (i = 0, fi = get_current_frame ();
181 fi && i < frame_low;
182 i++, fi = get_prev_frame (fi));
183
184 if (fi == NULL)
8a3fe4f8 185 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
fb40c209 186
6ad4a2cf 187 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
188
189 /* Now let's print the frames up to frame_high, or until there are
190 frames in the stack. */
191 for (;
192 fi && (i <= frame_high || frame_high == -1);
193 i++, fi = get_prev_frame (fi))
194 {
6ad4a2cf 195 struct cleanup *cleanup_frame;
fb40c209 196 QUIT;
6ad4a2cf 197 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209
AC
198 ui_out_field_int (uiout, "level", i);
199 list_args_or_locals (0, atoi (argv[0]), fi);
6ad4a2cf 200 do_cleanups (cleanup_frame);
fb40c209
AC
201 }
202
6ad4a2cf 203 do_cleanups (cleanup_stack_args);
fb40c209
AC
204}
205
206/* Print a list of the locals or the arguments for the currently
207 selected frame. If the argument passed is 0, printonly the names
208 of the variables, if an argument of 1 is passed, print the values
209 as well. */
210static void
211list_args_or_locals (int locals, int values, struct frame_info *fi)
212{
213 struct block *block;
214 struct symbol *sym;
de4f826b
DC
215 struct dict_iterator iter;
216 int nsyms;
6ad4a2cf 217 struct cleanup *cleanup_list;
fb40c209 218 static struct ui_stream *stb = NULL;
f5ec2042 219 struct type *type;
fb40c209
AC
220
221 stb = ui_out_stream_new (uiout);
222
ae767bfb 223 block = get_frame_block (fi, 0);
fb40c209 224
6ad4a2cf 225 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
fb40c209
AC
226
227 while (block != 0)
228 {
de4f826b 229 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 230 {
39bf4652
JB
231 int print_me = 0;
232
fb40c209
AC
233 switch (SYMBOL_CLASS (sym))
234 {
235 default:
236 case LOC_UNDEF: /* catches errors */
237 case LOC_CONST: /* constant */
238 case LOC_TYPEDEF: /* local typedef */
239 case LOC_LABEL: /* local label */
240 case LOC_BLOCK: /* local function */
241 case LOC_CONST_BYTES: /* loc. byte seq. */
242 case LOC_UNRESOLVED: /* unresolved static */
243 case LOC_OPTIMIZED_OUT: /* optimized out */
244 print_me = 0;
245 break;
246
247 case LOC_ARG: /* argument */
248 case LOC_REF_ARG: /* reference arg */
fb40c209 249 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 250 case LOC_LOCAL: /* stack local */
fb40c209
AC
251 case LOC_STATIC: /* static */
252 case LOC_REGISTER: /* register */
4cf623b6 253 case LOC_COMPUTED: /* computed location */
2a2d4dc3 254 if (SYMBOL_IS_ARGUMENT (sym) ? !locals : locals)
fb40c209
AC
255 print_me = 1;
256 break;
257 }
258 if (print_me)
259 {
6ad4a2cf 260 struct cleanup *cleanup_tuple = NULL;
6bb0384f 261 struct symbol *sym2;
9fbcbb40 262 struct value *val;
f5ec2042
NR
263 if (values != PRINT_NO_VALUES)
264 cleanup_tuple =
6ad4a2cf 265 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
f5ec2042 266 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
fb40c209 267
f5ec2042
NR
268 if (!locals)
269 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
270 block, VAR_DOMAIN,
2570f2b7 271 (int *) NULL);
f5ec2042 272 else
2a2d4dc3 273 sym2 = sym;
f5ec2042
NR
274 switch (values)
275 {
276 case PRINT_SIMPLE_VALUES:
277 type = check_typedef (sym2->type);
278 type_print (sym2->type, "", stb->stream, -1);
279 ui_out_field_stream (uiout, "type", stb);
280 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
281 && TYPE_CODE (type) != TYPE_CODE_STRUCT
282 && TYPE_CODE (type) != TYPE_CODE_UNION)
283 {
79a45b7d 284 struct value_print_options opts;
9fbcbb40 285 val = read_var_value (sym2, fi);
79a45b7d
TT
286 get_raw_print_options (&opts);
287 opts.deref_ref = 1;
9fbcbb40 288 common_val_print
79a45b7d 289 (val, stb->stream, 0, &opts,
d8ca156b 290 language_def (SYMBOL_LANGUAGE (sym2)));
f5ec2042
NR
291 ui_out_field_stream (uiout, "value", stb);
292 }
293 do_cleanups (cleanup_tuple);
294 break;
295 case PRINT_ALL_VALUES:
79a45b7d
TT
296 {
297 struct value_print_options opts;
298 val = read_var_value (sym2, fi);
299 get_raw_print_options (&opts);
300 opts.deref_ref = 1;
301 common_val_print
302 (val, stb->stream, 0, &opts,
303 language_def (SYMBOL_LANGUAGE (sym2)));
304 ui_out_field_stream (uiout, "value", stb);
305 do_cleanups (cleanup_tuple);
306 }
f5ec2042 307 break;
fb40c209
AC
308 }
309 }
310 }
311 if (BLOCK_FUNCTION (block))
312 break;
313 else
314 block = BLOCK_SUPERBLOCK (block);
315 }
6ad4a2cf 316 do_cleanups (cleanup_list);
fb40c209
AC
317 ui_out_stream_delete (stb);
318}
319
ce8f13f8 320void
fb40c209
AC
321mi_cmd_stack_select_frame (char *command, char **argv, int argc)
322{
fcf43932
NR
323 if (argc == 0 || argc > 1)
324 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
fb40c209 325
fcf43932 326 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 327}
64fd8944 328
ce8f13f8 329void
64fd8944
NR
330mi_cmd_stack_info_frame (char *command, char **argv, int argc)
331{
332 if (argc > 0)
333 error (_("mi_cmd_stack_info_frame: No arguments required"));
ce8f13f8 334
64fd8944 335 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 336}
This page took 0.722198 seconds and 4 git commands to generate.