gdb/
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
7b6bb8da 2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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"
875b4ff5 34#include "exceptions.h"
daf3c977
VP
35
36enum what_to_list { locals, arguments, all };
37
38static void list_args_or_locals (enum what_to_list what,
bdaf8d4a
JK
39 enum print_values values,
40 struct frame_info *fi);
fb40c209
AC
41
42/* Print a list of the stack frames. Args can be none, in which case
43 we want to print the whole backtrace, or a pair of numbers
44 specifying the frame numbers at which to start and stop the
45 display. If the two numbers are equal, a single frame will be
46 displayed. */
ce8f13f8 47void
fb40c209
AC
48mi_cmd_stack_list_frames (char *command, char **argv, int argc)
49{
50 int frame_low;
51 int frame_high;
52 int i;
6ad4a2cf 53 struct cleanup *cleanup_stack;
fb40c209
AC
54 struct frame_info *fi;
55
fb40c209 56 if (argc > 2 || argc == 1)
1b05df00 57 error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
58
59 if (argc == 2)
60 {
61 frame_low = atoi (argv[0]);
62 frame_high = atoi (argv[1]);
63 }
64 else
65 {
66 /* Called with no arguments, it means we want the whole
67 backtrace. */
68 frame_low = -1;
69 frame_high = -1;
70 }
71
72 /* Let's position fi on the frame at which to start the
73 display. Could be the innermost frame if the whole stack needs
74 displaying, or if frame_low is 0. */
75 for (i = 0, fi = get_current_frame ();
76 fi && i < frame_low;
77 i++, fi = get_prev_frame (fi));
78
79 if (fi == NULL)
1b05df00 80 error (_("-stack-list-frames: Not enough frames in stack."));
fb40c209 81
79a45e25 82 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
fb40c209
AC
83
84 /* Now let;s print the frames up to frame_high, or until there are
85 frames in the stack. */
86 for (;
87 fi && (i <= frame_high || frame_high == -1);
88 i++, fi = get_prev_frame (fi))
89 {
90 QUIT;
0faf0076 91 /* Print the location and the address always, even for level 0.
fb40c209 92 args == 0: don't print the arguments. */
0faf0076 93 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
94 }
95
6ad4a2cf 96 do_cleanups (cleanup_stack);
fb40c209
AC
97}
98
ce8f13f8 99void
fb40c209
AC
100mi_cmd_stack_info_depth (char *command, char **argv, int argc)
101{
102 int frame_high;
103 int i;
104 struct frame_info *fi;
105
fb40c209 106 if (argc > 1)
1b05df00 107 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
108
109 if (argc == 1)
110 frame_high = atoi (argv[0]);
111 else
112 /* Called with no arguments, it means we want the real depth of
113 the stack. */
114 frame_high = -1;
115
116 for (i = 0, fi = get_current_frame ();
117 fi && (i < frame_high || frame_high == -1);
118 i++, fi = get_prev_frame (fi))
119 QUIT;
120
79a45e25 121 ui_out_field_int (current_uiout, "depth", i);
fb40c209
AC
122}
123
8b777f02
VP
124static enum print_values
125parse_print_values (char *name)
126{
127 if (strcmp (name, "0") == 0
128 || strcmp (name, mi_no_values) == 0)
129 return PRINT_NO_VALUES;
130 else if (strcmp (name, "1") == 0
131 || strcmp (name, mi_all_values) == 0)
132 return PRINT_ALL_VALUES;
133 else if (strcmp (name, "2") == 0
134 || strcmp (name, mi_simple_values) == 0)
135 return PRINT_SIMPLE_VALUES;
136 else
137 error (_("Unknown value for PRINT_VALUES: must be: \
1380 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
139 mi_no_values, mi_all_values, mi_simple_values);
140}
141
7a93fb82 142/* Print a list of the locals for the current frame. With argument of
fb40c209
AC
143 0, print only the names, with argument of 1 print also the
144 values. */
ce8f13f8 145void
fb40c209
AC
146mi_cmd_stack_list_locals (char *command, char **argv, int argc)
147{
f5ec2042 148 struct frame_info *frame;
f5ec2042 149
fb40c209 150 if (argc != 1)
1b05df00 151 error (_("-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
7a93fb82 158/* Print a list of the arguments for the current frame. With argument
fb40c209
AC
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;
79a45e25 170 struct ui_out *uiout = current_uiout;
fb40c209
AC
171
172 if (argc < 1 || argc > 3 || argc == 2)
1b05df00 173 error (_("-stack-list-arguments: Usage: "
9a2b4c1b 174 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
175
176 if (argc == 3)
177 {
178 frame_low = atoi (argv[1]);
179 frame_high = atoi (argv[2]);
180 }
181 else
182 {
183 /* Called with no arguments, it means we want args for the whole
184 backtrace. */
185 frame_low = -1;
186 frame_high = -1;
187 }
188
8b777f02
VP
189 print_values = parse_print_values (argv[0]);
190
fb40c209
AC
191 /* Let's position fi on the frame at which to start the
192 display. Could be the innermost frame if the whole stack needs
193 displaying, or if frame_low is 0. */
194 for (i = 0, fi = get_current_frame ();
195 fi && i < frame_low;
196 i++, fi = get_prev_frame (fi));
197
198 if (fi == NULL)
1b05df00 199 error (_("-stack-list-arguments: Not enough frames in stack."));
fb40c209 200
9a2b4c1b
MS
201 cleanup_stack_args
202 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
203
204 /* Now let's print the frames up to frame_high, or until there are
205 frames in the stack. */
206 for (;
207 fi && (i <= frame_high || frame_high == -1);
208 i++, fi = get_prev_frame (fi))
209 {
6ad4a2cf 210 struct cleanup *cleanup_frame;
102040f0 211
fb40c209 212 QUIT;
6ad4a2cf 213 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209 214 ui_out_field_int (uiout, "level", i);
daf3c977 215 list_args_or_locals (arguments, print_values, fi);
6ad4a2cf 216 do_cleanups (cleanup_frame);
fb40c209
AC
217 }
218
6ad4a2cf 219 do_cleanups (cleanup_stack_args);
fb40c209
AC
220}
221
daf3c977 222/* Print a list of the local variables (including arguments) for the
7a93fb82
VP
223 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
224 or both names and values of the variables must be printed. See
225 parse_print_value for possible values. */
daf3c977
VP
226void
227mi_cmd_stack_list_variables (char *command, char **argv, int argc)
228{
229 struct frame_info *frame;
daf3c977
VP
230
231 if (argc != 1)
232 error (_("Usage: PRINT_VALUES"));
233
7a93fb82 234 frame = get_selected_frame (NULL);
daf3c977 235
7a93fb82 236 list_args_or_locals (all, parse_print_values (argv[0]), frame);
daf3c977
VP
237}
238
93d86cef
JK
239/* Print single local or argument. ARG must be already read in. For WHAT and
240 VALUES see list_args_or_locals.
241
242 Errors are printed as if they would be the parameter value. Use zeroed ARG
243 iff it should not be printed accoring to VALUES. */
244
245static void
246list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
247 enum print_values values)
248{
249 struct cleanup *cleanup_tuple = NULL;
250 struct ui_out *uiout = current_uiout;
251 struct ui_stream *stb = ui_out_stream_new (uiout);
252
253 gdb_assert (!arg->val || !arg->error);
254 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
255 && arg->error == NULL)
256 || values == PRINT_SIMPLE_VALUES
257 || (values == PRINT_ALL_VALUES
258 && (arg->val != NULL || arg->error != NULL)));
259
260 if (values != PRINT_NO_VALUES || what == all)
261 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
262
263 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (arg->sym));
264
265 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
266 ui_out_field_int (uiout, "arg", 1);
267
268 if (values == PRINT_SIMPLE_VALUES)
269 {
270 check_typedef (arg->sym->type);
271 type_print (arg->sym->type, "", stb->stream, -1);
272 ui_out_field_stream (uiout, "type", stb);
273 }
274
275 if (arg->val || arg->error)
276 {
277 volatile struct gdb_exception except;
278
279 if (arg->error)
280 except.message = arg->error;
281 else
282 {
283 /* TRY_CATCH has two statements, wrap it in a block. */
284
285 TRY_CATCH (except, RETURN_MASK_ERROR)
286 {
287 struct value_print_options opts;
288
289 get_raw_print_options (&opts);
290 opts.deref_ref = 1;
291 common_val_print (arg->val, stb->stream, 0, &opts,
292 language_def (SYMBOL_LANGUAGE (arg->sym)));
293 }
294 }
295 if (except.message)
296 fprintf_filtered (stb->stream, _("<error reading variable: %s>"),
297 except.message);
298 ui_out_field_stream (uiout, "value", stb);
299 }
300
301 ui_out_stream_delete (stb);
302 if (values != PRINT_NO_VALUES || what == all)
303 do_cleanups (cleanup_tuple);
304}
daf3c977 305
fb40c209
AC
306/* Print a list of the locals or the arguments for the currently
307 selected frame. If the argument passed is 0, printonly the names
308 of the variables, if an argument of 1 is passed, print the values
309 as well. */
310static void
bdaf8d4a
JK
311list_args_or_locals (enum what_to_list what, enum print_values values,
312 struct frame_info *fi)
fb40c209
AC
313{
314 struct block *block;
315 struct symbol *sym;
de4f826b 316 struct dict_iterator iter;
6ad4a2cf 317 struct cleanup *cleanup_list;
cb0fd152 318 struct ui_stream *stb;
f5ec2042 319 struct type *type;
daf3c977 320 char *name_of_result;
79a45e25 321 struct ui_out *uiout = current_uiout;
fb40c209
AC
322
323 stb = ui_out_stream_new (uiout);
324
ae767bfb 325 block = get_frame_block (fi, 0);
fb40c209 326
daf3c977
VP
327 switch (what)
328 {
329 case locals:
d6fd4674
PA
330 name_of_result = "locals";
331 break;
daf3c977 332 case arguments:
d6fd4674
PA
333 name_of_result = "args";
334 break;
daf3c977 335 case all:
d6fd4674
PA
336 name_of_result = "variables";
337 break;
654e7c1f 338 default:
d6fd4674
PA
339 internal_error (__FILE__, __LINE__,
340 "unexpected what_to_list: %d", (int) what);
daf3c977
VP
341 }
342
343 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
fb40c209
AC
344
345 while (block != 0)
346 {
de4f826b 347 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 348 {
39bf4652
JB
349 int print_me = 0;
350
fb40c209
AC
351 switch (SYMBOL_CLASS (sym))
352 {
353 default:
354 case LOC_UNDEF: /* catches errors */
355 case LOC_CONST: /* constant */
356 case LOC_TYPEDEF: /* local typedef */
357 case LOC_LABEL: /* local label */
358 case LOC_BLOCK: /* local function */
359 case LOC_CONST_BYTES: /* loc. byte seq. */
360 case LOC_UNRESOLVED: /* unresolved static */
361 case LOC_OPTIMIZED_OUT: /* optimized out */
362 print_me = 0;
363 break;
364
365 case LOC_ARG: /* argument */
366 case LOC_REF_ARG: /* reference arg */
fb40c209 367 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 368 case LOC_LOCAL: /* stack local */
fb40c209
AC
369 case LOC_STATIC: /* static */
370 case LOC_REGISTER: /* register */
4cf623b6 371 case LOC_COMPUTED: /* computed location */
daf3c977 372 if (what == all)
fb40c209 373 print_me = 1;
daf3c977
VP
374 else if (what == locals)
375 print_me = !SYMBOL_IS_ARGUMENT (sym);
376 else
377 print_me = SYMBOL_IS_ARGUMENT (sym);
fb40c209
AC
378 break;
379 }
380 if (print_me)
381 {
6bb0384f 382 struct symbol *sym2;
93d86cef 383 struct frame_arg arg;
fb40c209 384
daf3c977 385 if (SYMBOL_IS_ARGUMENT (sym))
f5ec2042
NR
386 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
387 block, VAR_DOMAIN,
2570f2b7 388 (int *) NULL);
f5ec2042 389 else
2a2d4dc3 390 sym2 = sym;
93d86cef
JK
391
392 memset (&arg, 0, sizeof (arg));
393 arg.sym = sym2;
394
f5ec2042
NR
395 switch (values)
396 {
397 case PRINT_SIMPLE_VALUES:
398 type = check_typedef (sym2->type);
f5ec2042
NR
399 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
400 && TYPE_CODE (type) != TYPE_CODE_STRUCT
401 && TYPE_CODE (type) != TYPE_CODE_UNION)
402 {
f5ec2042 403 case PRINT_ALL_VALUES:
93d86cef
JK
404 read_frame_arg (sym2, fi, &arg);
405 }
f5ec2042 406 break;
fb40c209 407 }
7a93fb82 408
93d86cef
JK
409 list_arg_or_local (&arg, what, values);
410 xfree (arg.error);
fb40c209
AC
411 }
412 }
413 if (BLOCK_FUNCTION (block))
414 break;
415 else
416 block = BLOCK_SUPERBLOCK (block);
417 }
6ad4a2cf 418 do_cleanups (cleanup_list);
fb40c209
AC
419 ui_out_stream_delete (stb);
420}
421
ce8f13f8 422void
fb40c209
AC
423mi_cmd_stack_select_frame (char *command, char **argv, int argc)
424{
fcf43932 425 if (argc == 0 || argc > 1)
1b05df00 426 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
fb40c209 427
fcf43932 428 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 429}
64fd8944 430
ce8f13f8 431void
64fd8944
NR
432mi_cmd_stack_info_frame (char *command, char **argv, int argc)
433{
434 if (argc > 0)
1b05df00 435 error (_("-stack-info-frame: No arguments required"));
ce8f13f8 436
64fd8944 437 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 438}
This page took 0.8901 seconds and 4 git commands to generate.