merge from gcc
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
28e7fd62 2 Copyright (C) 2000-2013 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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
19
20#include "defs.h"
21#include "target.h"
22#include "frame.h"
23#include "value.h"
24#include "mi-cmds.h"
25#include "ui-out.h"
e88c90f2 26#include "symtab.h"
fe898f56 27#include "block.h"
b9362cc7 28#include "stack.h"
de4f826b 29#include "dictionary.h"
f5ec2042 30#include "gdb_string.h"
d8ca156b 31#include "language.h"
79a45b7d 32#include "valprint.h"
875b4ff5 33#include "exceptions.h"
daf3c977
VP
34
35enum what_to_list { locals, arguments, all };
36
37static void list_args_or_locals (enum what_to_list what,
bdaf8d4a
JK
38 enum print_values values,
39 struct frame_info *fi);
fb40c209 40
2b03b41d 41/* Print a list of the stack frames. Args can be none, in which case
fb40c209
AC
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
2b03b41d
SS
44 display. If the two numbers are equal, a single frame will be
45 displayed. */
46
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
2b03b41d 67 backtrace. */
fb40c209
AC
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
2b03b41d 74 displaying, or if frame_low is 0. */
fb40c209
AC
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 83
2b03b41d
SS
84 /* Now let's print the frames up to frame_high, or until there are
85 frames in the stack. */
fb40c209
AC
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.
2b03b41d 92 If args is 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
2b03b41d 113 the stack. */
fb40c209
AC
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 143 0, print only the names, with argument of 1 print also the
2b03b41d
SS
144 values. */
145
ce8f13f8 146void
fb40c209
AC
147mi_cmd_stack_list_locals (char *command, char **argv, int argc)
148{
f5ec2042 149 struct frame_info *frame;
f5ec2042 150
fb40c209 151 if (argc != 1)
1b05df00 152 error (_("-stack-list-locals: Usage: PRINT_VALUES"));
fb40c209 153
b04f3ab4 154 frame = get_selected_frame (NULL);
f5ec2042 155
daf3c977 156 list_args_or_locals (locals, parse_print_values (argv[0]), frame);
fb40c209
AC
157}
158
7a93fb82 159/* Print a list of the arguments for the current frame. With argument
fb40c209 160 of 0, print only the names, with argument of 1 print also the
2b03b41d
SS
161 values. */
162
ce8f13f8 163void
fb40c209
AC
164mi_cmd_stack_list_args (char *command, char **argv, int argc)
165{
166 int frame_low;
167 int frame_high;
168 int i;
169 struct frame_info *fi;
6ad4a2cf 170 struct cleanup *cleanup_stack_args;
8b777f02 171 enum print_values print_values;
79a45e25 172 struct ui_out *uiout = current_uiout;
fb40c209
AC
173
174 if (argc < 1 || argc > 3 || argc == 2)
1b05df00 175 error (_("-stack-list-arguments: Usage: "
9a2b4c1b 176 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
177
178 if (argc == 3)
179 {
180 frame_low = atoi (argv[1]);
181 frame_high = atoi (argv[2]);
182 }
183 else
184 {
185 /* Called with no arguments, it means we want args for the whole
2b03b41d 186 backtrace. */
fb40c209
AC
187 frame_low = -1;
188 frame_high = -1;
189 }
190
8b777f02
VP
191 print_values = parse_print_values (argv[0]);
192
fb40c209
AC
193 /* Let's position fi on the frame at which to start the
194 display. Could be the innermost frame if the whole stack needs
2b03b41d 195 displaying, or if frame_low is 0. */
fb40c209
AC
196 for (i = 0, fi = get_current_frame ();
197 fi && i < frame_low;
198 i++, fi = get_prev_frame (fi));
199
200 if (fi == NULL)
1b05df00 201 error (_("-stack-list-arguments: Not enough frames in stack."));
fb40c209 202
9a2b4c1b
MS
203 cleanup_stack_args
204 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
205
206 /* Now let's print the frames up to frame_high, or until there are
2b03b41d 207 frames in the stack. */
fb40c209
AC
208 for (;
209 fi && (i <= frame_high || frame_high == -1);
210 i++, fi = get_prev_frame (fi))
211 {
6ad4a2cf 212 struct cleanup *cleanup_frame;
102040f0 213
fb40c209 214 QUIT;
6ad4a2cf 215 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209 216 ui_out_field_int (uiout, "level", i);
daf3c977 217 list_args_or_locals (arguments, print_values, fi);
6ad4a2cf 218 do_cleanups (cleanup_frame);
fb40c209
AC
219 }
220
6ad4a2cf 221 do_cleanups (cleanup_stack_args);
fb40c209
AC
222}
223
daf3c977 224/* Print a list of the local variables (including arguments) for the
7a93fb82
VP
225 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
226 or both names and values of the variables must be printed. See
227 parse_print_value for possible values. */
2b03b41d 228
daf3c977
VP
229void
230mi_cmd_stack_list_variables (char *command, char **argv, int argc)
231{
232 struct frame_info *frame;
daf3c977
VP
233
234 if (argc != 1)
235 error (_("Usage: PRINT_VALUES"));
236
7a93fb82 237 frame = get_selected_frame (NULL);
daf3c977 238
7a93fb82 239 list_args_or_locals (all, parse_print_values (argv[0]), frame);
daf3c977
VP
240}
241
2b03b41d
SS
242/* Print single local or argument. ARG must be already read in. For
243 WHAT and VALUES see list_args_or_locals.
93d86cef 244
2b03b41d
SS
245 Errors are printed as if they would be the parameter value. Use
246 zeroed ARG iff it should not be printed according to VALUES. */
93d86cef
JK
247
248static void
249list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
250 enum print_values values)
251{
f99d8bf4 252 struct cleanup *old_chain;
93d86cef
JK
253 struct cleanup *cleanup_tuple = NULL;
254 struct ui_out *uiout = current_uiout;
f99d8bf4
PA
255 struct ui_file *stb;
256
257 stb = mem_fileopen ();
258 old_chain = make_cleanup_ui_file_delete (stb);
93d86cef
JK
259
260 gdb_assert (!arg->val || !arg->error);
261 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
262 && arg->error == NULL)
263 || values == PRINT_SIMPLE_VALUES
264 || (values == PRINT_ALL_VALUES
265 && (arg->val != NULL || arg->error != NULL)));
e18b2753
JK
266 gdb_assert (arg->entry_kind == print_entry_values_no
267 || (arg->entry_kind == print_entry_values_only
268 && (arg->val || arg->error)));
93d86cef
JK
269
270 if (values != PRINT_NO_VALUES || what == all)
271 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
272
f99d8bf4 273 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
e18b2753 274 if (arg->entry_kind == print_entry_values_only)
f99d8bf4 275 fputs_filtered ("@entry", stb);
e18b2753 276 ui_out_field_stream (uiout, "name", stb);
93d86cef
JK
277
278 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
279 ui_out_field_int (uiout, "arg", 1);
280
281 if (values == PRINT_SIMPLE_VALUES)
282 {
283 check_typedef (arg->sym->type);
f99d8bf4 284 type_print (arg->sym->type, "", stb, -1);
93d86cef
JK
285 ui_out_field_stream (uiout, "type", stb);
286 }
287
288 if (arg->val || arg->error)
289 {
290 volatile struct gdb_exception except;
291
292 if (arg->error)
293 except.message = arg->error;
294 else
295 {
296 /* TRY_CATCH has two statements, wrap it in a block. */
297
298 TRY_CATCH (except, RETURN_MASK_ERROR)
299 {
300 struct value_print_options opts;
301
302 get_raw_print_options (&opts);
303 opts.deref_ref = 1;
f99d8bf4 304 common_val_print (arg->val, stb, 0, &opts,
93d86cef
JK
305 language_def (SYMBOL_LANGUAGE (arg->sym)));
306 }
307 }
308 if (except.message)
f99d8bf4 309 fprintf_filtered (stb, _("<error reading variable: %s>"),
93d86cef
JK
310 except.message);
311 ui_out_field_stream (uiout, "value", stb);
312 }
313
93d86cef
JK
314 if (values != PRINT_NO_VALUES || what == all)
315 do_cleanups (cleanup_tuple);
f99d8bf4 316 do_cleanups (old_chain);
93d86cef 317}
daf3c977 318
fb40c209
AC
319/* Print a list of the locals or the arguments for the currently
320 selected frame. If the argument passed is 0, printonly the names
321 of the variables, if an argument of 1 is passed, print the values
2b03b41d
SS
322 as well. */
323
fb40c209 324static void
bdaf8d4a
JK
325list_args_or_locals (enum what_to_list what, enum print_values values,
326 struct frame_info *fi)
fb40c209
AC
327{
328 struct block *block;
329 struct symbol *sym;
8157b174 330 struct block_iterator iter;
6ad4a2cf 331 struct cleanup *cleanup_list;
f5ec2042 332 struct type *type;
daf3c977 333 char *name_of_result;
79a45e25 334 struct ui_out *uiout = current_uiout;
fb40c209 335
ae767bfb 336 block = get_frame_block (fi, 0);
fb40c209 337
daf3c977
VP
338 switch (what)
339 {
340 case locals:
d6fd4674
PA
341 name_of_result = "locals";
342 break;
daf3c977 343 case arguments:
d6fd4674
PA
344 name_of_result = "args";
345 break;
daf3c977 346 case all:
d6fd4674
PA
347 name_of_result = "variables";
348 break;
654e7c1f 349 default:
d6fd4674
PA
350 internal_error (__FILE__, __LINE__,
351 "unexpected what_to_list: %d", (int) what);
daf3c977
VP
352 }
353
354 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
fb40c209
AC
355
356 while (block != 0)
357 {
de4f826b 358 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 359 {
39bf4652
JB
360 int print_me = 0;
361
fb40c209
AC
362 switch (SYMBOL_CLASS (sym))
363 {
364 default:
365 case LOC_UNDEF: /* catches errors */
366 case LOC_CONST: /* constant */
367 case LOC_TYPEDEF: /* local typedef */
368 case LOC_LABEL: /* local label */
369 case LOC_BLOCK: /* local function */
370 case LOC_CONST_BYTES: /* loc. byte seq. */
371 case LOC_UNRESOLVED: /* unresolved static */
372 case LOC_OPTIMIZED_OUT: /* optimized out */
373 print_me = 0;
374 break;
375
376 case LOC_ARG: /* argument */
377 case LOC_REF_ARG: /* reference arg */
fb40c209 378 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 379 case LOC_LOCAL: /* stack local */
fb40c209
AC
380 case LOC_STATIC: /* static */
381 case LOC_REGISTER: /* register */
4cf623b6 382 case LOC_COMPUTED: /* computed location */
daf3c977 383 if (what == all)
fb40c209 384 print_me = 1;
daf3c977
VP
385 else if (what == locals)
386 print_me = !SYMBOL_IS_ARGUMENT (sym);
387 else
388 print_me = SYMBOL_IS_ARGUMENT (sym);
fb40c209
AC
389 break;
390 }
391 if (print_me)
392 {
6bb0384f 393 struct symbol *sym2;
e18b2753 394 struct frame_arg arg, entryarg;
fb40c209 395
daf3c977 396 if (SYMBOL_IS_ARGUMENT (sym))
f7e44f65 397 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
f5ec2042 398 block, VAR_DOMAIN,
1993b719 399 NULL);
f5ec2042 400 else
2a2d4dc3 401 sym2 = sym;
f7e44f65 402 gdb_assert (sym2 != NULL);
93d86cef
JK
403
404 memset (&arg, 0, sizeof (arg));
405 arg.sym = sym2;
e18b2753
JK
406 arg.entry_kind = print_entry_values_no;
407 memset (&entryarg, 0, sizeof (entryarg));
408 entryarg.sym = sym2;
409 entryarg.entry_kind = print_entry_values_no;
93d86cef 410
f5ec2042
NR
411 switch (values)
412 {
413 case PRINT_SIMPLE_VALUES:
414 type = check_typedef (sym2->type);
f5ec2042
NR
415 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
416 && TYPE_CODE (type) != TYPE_CODE_STRUCT
417 && TYPE_CODE (type) != TYPE_CODE_UNION)
418 {
f5ec2042 419 case PRINT_ALL_VALUES:
e18b2753 420 read_frame_arg (sym2, fi, &arg, &entryarg);
93d86cef 421 }
f5ec2042 422 break;
fb40c209 423 }
7a93fb82 424
e18b2753
JK
425 if (arg.entry_kind != print_entry_values_only)
426 list_arg_or_local (&arg, what, values);
427 if (entryarg.entry_kind != print_entry_values_no)
428 list_arg_or_local (&entryarg, what, values);
93d86cef 429 xfree (arg.error);
e18b2753 430 xfree (entryarg.error);
fb40c209
AC
431 }
432 }
2b03b41d 433
fb40c209
AC
434 if (BLOCK_FUNCTION (block))
435 break;
436 else
437 block = BLOCK_SUPERBLOCK (block);
438 }
6ad4a2cf 439 do_cleanups (cleanup_list);
fb40c209
AC
440}
441
ce8f13f8 442void
fb40c209
AC
443mi_cmd_stack_select_frame (char *command, char **argv, int argc)
444{
fcf43932 445 if (argc == 0 || argc > 1)
1b05df00 446 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
fb40c209 447
fcf43932 448 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 449}
64fd8944 450
ce8f13f8 451void
64fd8944
NR
452mi_cmd_stack_info_frame (char *command, char **argv, int argc)
453{
454 if (argc > 0)
2b03b41d 455 error (_("-stack-info-frame: No arguments allowed"));
ce8f13f8 456
64fd8944 457 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 458}
This page took 1.535315 seconds and 4 git commands to generate.