* gdb.texinfo (GDB/MI Stack Manipulation): Document
[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 34
daf3c977
VP
35
36enum what_to_list { locals, arguments, all };
37
38static void list_args_or_locals (enum what_to_list what,
39 int values, struct frame_info *fi);
fb40c209
AC
40
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
45 displayed. */
ce8f13f8 46void
fb40c209
AC
47mi_cmd_stack_list_frames (char *command, char **argv, int argc)
48{
49 int frame_low;
50 int frame_high;
51 int i;
6ad4a2cf 52 struct cleanup *cleanup_stack;
fb40c209
AC
53 struct frame_info *fi;
54
fb40c209 55 if (argc > 2 || argc == 1)
8a3fe4f8 56 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
57
58 if (argc == 2)
59 {
60 frame_low = atoi (argv[0]);
61 frame_high = atoi (argv[1]);
62 }
63 else
64 {
65 /* Called with no arguments, it means we want the whole
66 backtrace. */
67 frame_low = -1;
68 frame_high = -1;
69 }
70
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 ();
75 fi && i < frame_low;
76 i++, fi = get_prev_frame (fi));
77
78 if (fi == NULL)
8a3fe4f8 79 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
fb40c209 80
6ad4a2cf 81 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
fb40c209
AC
82
83 /* Now let;s print the frames up to frame_high, or until there are
84 frames in the stack. */
85 for (;
86 fi && (i <= frame_high || frame_high == -1);
87 i++, fi = get_prev_frame (fi))
88 {
89 QUIT;
0faf0076 90 /* Print the location and the address always, even for level 0.
fb40c209 91 args == 0: don't print the arguments. */
0faf0076 92 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
93 }
94
6ad4a2cf 95 do_cleanups (cleanup_stack);
fb40c209
AC
96}
97
ce8f13f8 98void
fb40c209
AC
99mi_cmd_stack_info_depth (char *command, char **argv, int argc)
100{
101 int frame_high;
102 int i;
103 struct frame_info *fi;
104
fb40c209 105 if (argc > 1)
8a3fe4f8 106 error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
107
108 if (argc == 1)
109 frame_high = atoi (argv[0]);
110 else
111 /* Called with no arguments, it means we want the real depth of
112 the stack. */
113 frame_high = -1;
114
115 for (i = 0, fi = get_current_frame ();
116 fi && (i < frame_high || frame_high == -1);
117 i++, fi = get_prev_frame (fi))
118 QUIT;
119
120 ui_out_field_int (uiout, "depth", i);
fb40c209
AC
121}
122
8b777f02
VP
123static enum print_values
124parse_print_values (char *name)
125{
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;
135 else
136 error (_("Unknown value for PRINT_VALUES: must be: \
1370 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
138 mi_no_values, mi_all_values, mi_simple_values);
139}
140
fb40c209
AC
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
143 values. */
ce8f13f8 144void
fb40c209
AC
145mi_cmd_stack_list_locals (char *command, char **argv, int argc)
146{
f5ec2042
NR
147 struct frame_info *frame;
148 enum print_values print_values;
149
fb40c209 150 if (argc != 1)
8a3fe4f8 151 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
fb40c209 152
b04f3ab4 153 frame = get_selected_frame (NULL);
f5ec2042 154
daf3c977 155 list_args_or_locals (locals, parse_print_values (argv[0]), frame);
fb40c209
AC
156}
157
158/* Print a list of the arguments for the current frame. With argument
159 of 0, print only the names, with argument of 1 print also the
160 values. */
ce8f13f8 161void
fb40c209
AC
162mi_cmd_stack_list_args (char *command, char **argv, int argc)
163{
164 int frame_low;
165 int frame_high;
166 int i;
167 struct frame_info *fi;
6ad4a2cf 168 struct cleanup *cleanup_stack_args;
8b777f02 169 enum print_values print_values;
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
8b777f02
VP
187 print_values = parse_print_values (argv[0]);
188
fb40c209
AC
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 ();
193 fi && i < frame_low;
194 i++, fi = get_prev_frame (fi));
195
196 if (fi == NULL)
8a3fe4f8 197 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
fb40c209 198
6ad4a2cf 199 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
200
201 /* Now let's print the frames up to frame_high, or until there are
202 frames in the stack. */
203 for (;
204 fi && (i <= frame_high || frame_high == -1);
205 i++, fi = get_prev_frame (fi))
206 {
6ad4a2cf 207 struct cleanup *cleanup_frame;
fb40c209 208 QUIT;
6ad4a2cf 209 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209 210 ui_out_field_int (uiout, "level", i);
daf3c977 211 list_args_or_locals (arguments, print_values, fi);
6ad4a2cf 212 do_cleanups (cleanup_frame);
fb40c209
AC
213 }
214
6ad4a2cf 215 do_cleanups (cleanup_stack_args);
fb40c209
AC
216}
217
daf3c977
VP
218/* Print a list of the local variables (including arguments) for the
219 current frame. With argument of 0, print only the names, with
220 argument of 1 print also the values. */
221void
222mi_cmd_stack_list_variables (char *command, char **argv, int argc)
223{
224 struct frame_info *frame;
225 enum print_values print_values;
226
227 if (argc != 1)
228 error (_("Usage: PRINT_VALUES"));
229
230 frame = get_selected_frame (NULL);
231
232 list_args_or_locals (all, parse_print_values (argv[0]), frame);
233}
234
235
fb40c209
AC
236/* Print a list of the locals or the arguments for the currently
237 selected frame. If the argument passed is 0, printonly the names
238 of the variables, if an argument of 1 is passed, print the values
239 as well. */
240static void
daf3c977 241list_args_or_locals (enum what_to_list what, int values, struct frame_info *fi)
fb40c209
AC
242{
243 struct block *block;
244 struct symbol *sym;
de4f826b
DC
245 struct dict_iterator iter;
246 int nsyms;
6ad4a2cf 247 struct cleanup *cleanup_list;
fb40c209 248 static struct ui_stream *stb = NULL;
f5ec2042 249 struct type *type;
daf3c977 250 char *name_of_result;
fb40c209
AC
251
252 stb = ui_out_stream_new (uiout);
253
ae767bfb 254 block = get_frame_block (fi, 0);
fb40c209 255
daf3c977
VP
256 switch (what)
257 {
258 case locals:
259 name_of_result = "locals"; break;
260 case arguments:
261 name_of_result = "args"; break;
262 case all:
263 name_of_result = "variables"; break;
264 }
265
266 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
fb40c209
AC
267
268 while (block != 0)
269 {
de4f826b 270 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 271 {
39bf4652
JB
272 int print_me = 0;
273
fb40c209
AC
274 switch (SYMBOL_CLASS (sym))
275 {
276 default:
277 case LOC_UNDEF: /* catches errors */
278 case LOC_CONST: /* constant */
279 case LOC_TYPEDEF: /* local typedef */
280 case LOC_LABEL: /* local label */
281 case LOC_BLOCK: /* local function */
282 case LOC_CONST_BYTES: /* loc. byte seq. */
283 case LOC_UNRESOLVED: /* unresolved static */
284 case LOC_OPTIMIZED_OUT: /* optimized out */
285 print_me = 0;
286 break;
287
288 case LOC_ARG: /* argument */
289 case LOC_REF_ARG: /* reference arg */
fb40c209 290 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 291 case LOC_LOCAL: /* stack local */
fb40c209
AC
292 case LOC_STATIC: /* static */
293 case LOC_REGISTER: /* register */
4cf623b6 294 case LOC_COMPUTED: /* computed location */
daf3c977 295 if (what == all)
fb40c209 296 print_me = 1;
daf3c977
VP
297 else if (what == locals)
298 print_me = !SYMBOL_IS_ARGUMENT (sym);
299 else
300 print_me = SYMBOL_IS_ARGUMENT (sym);
fb40c209
AC
301 break;
302 }
303 if (print_me)
304 {
6ad4a2cf 305 struct cleanup *cleanup_tuple = NULL;
6bb0384f 306 struct symbol *sym2;
9fbcbb40 307 struct value *val;
f5ec2042
NR
308 if (values != PRINT_NO_VALUES)
309 cleanup_tuple =
6ad4a2cf 310 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
f5ec2042 311 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
fb40c209 312
daf3c977 313 if (SYMBOL_IS_ARGUMENT (sym))
f5ec2042
NR
314 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
315 block, VAR_DOMAIN,
2570f2b7 316 (int *) NULL);
f5ec2042 317 else
2a2d4dc3 318 sym2 = sym;
f5ec2042
NR
319 switch (values)
320 {
321 case PRINT_SIMPLE_VALUES:
322 type = check_typedef (sym2->type);
323 type_print (sym2->type, "", stb->stream, -1);
324 ui_out_field_stream (uiout, "type", stb);
325 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
326 && TYPE_CODE (type) != TYPE_CODE_STRUCT
327 && TYPE_CODE (type) != TYPE_CODE_UNION)
328 {
79a45b7d 329 struct value_print_options opts;
9fbcbb40 330 val = read_var_value (sym2, fi);
79a45b7d
TT
331 get_raw_print_options (&opts);
332 opts.deref_ref = 1;
9fbcbb40 333 common_val_print
79a45b7d 334 (val, stb->stream, 0, &opts,
d8ca156b 335 language_def (SYMBOL_LANGUAGE (sym2)));
f5ec2042
NR
336 ui_out_field_stream (uiout, "value", stb);
337 }
338 do_cleanups (cleanup_tuple);
339 break;
340 case PRINT_ALL_VALUES:
79a45b7d
TT
341 {
342 struct value_print_options opts;
343 val = read_var_value (sym2, fi);
344 get_raw_print_options (&opts);
345 opts.deref_ref = 1;
346 common_val_print
347 (val, stb->stream, 0, &opts,
348 language_def (SYMBOL_LANGUAGE (sym2)));
349 ui_out_field_stream (uiout, "value", stb);
350 do_cleanups (cleanup_tuple);
351 }
f5ec2042 352 break;
fb40c209
AC
353 }
354 }
355 }
356 if (BLOCK_FUNCTION (block))
357 break;
358 else
359 block = BLOCK_SUPERBLOCK (block);
360 }
6ad4a2cf 361 do_cleanups (cleanup_list);
fb40c209
AC
362 ui_out_stream_delete (stb);
363}
364
ce8f13f8 365void
fb40c209
AC
366mi_cmd_stack_select_frame (char *command, char **argv, int argc)
367{
fcf43932
NR
368 if (argc == 0 || argc > 1)
369 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
fb40c209 370
fcf43932 371 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 372}
64fd8944 373
ce8f13f8 374void
64fd8944
NR
375mi_cmd_stack_info_frame (char *command, char **argv, int argc)
376{
377 if (argc > 0)
378 error (_("mi_cmd_stack_info_frame: No arguments required"));
ce8f13f8 379
64fd8944 380 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 381}
This page took 0.914686 seconds and 4 git commands to generate.