]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-cmd-stack.c
daily update
[thirdparty/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"
1e611234
PM
34#include "utils.h"
35#include "mi-getopt.h"
36#include "python/python.h"
37#include <ctype.h>
daf3c977
VP
38
39enum what_to_list { locals, arguments, all };
40
41static void list_args_or_locals (enum what_to_list what,
bdaf8d4a
JK
42 enum print_values values,
43 struct frame_info *fi);
fb40c209 44
1e611234
PM
45/* True if we want to allow Python-based frame filters. */
46static int frame_filters = 0;
47
48void
49mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
50{
51 if (argc != 0)
52 error (_("-enable-frame-filters: no arguments allowed"));
53 frame_filters = 1;
54}
55
56/* Parse the --no-frame-filters option in commands where we cannot use
57 mi_getopt. */
58static int
59parse_no_frames_option (const char *arg)
60{
61 if (arg && (strcmp (arg, "--no-frame-filters") == 0))
62 return 1;
63
64 return 0;
65}
66
2b03b41d 67/* Print a list of the stack frames. Args can be none, in which case
fb40c209
AC
68 we want to print the whole backtrace, or a pair of numbers
69 specifying the frame numbers at which to start and stop the
2b03b41d
SS
70 display. If the two numbers are equal, a single frame will be
71 displayed. */
72
ce8f13f8 73void
fb40c209
AC
74mi_cmd_stack_list_frames (char *command, char **argv, int argc)
75{
76 int frame_low;
77 int frame_high;
78 int i;
6ad4a2cf 79 struct cleanup *cleanup_stack;
fb40c209 80 struct frame_info *fi;
1e611234
PM
81 enum py_bt_status result = PY_BT_ERROR;
82 int raw_arg = 0;
83 int oind = 0;
84 enum opt
85 {
86 NO_FRAME_FILTERS
87 };
88 static const struct mi_opt opts[] =
89 {
90 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
91 { 0, 0, 0 }
92 };
93
94 /* Parse arguments. In this instance we are just looking for
95 --no-frame-filters. */
96 while (1)
97 {
98 char *oarg;
99 int opt = mi_getopt ("-stack-list-frames", argc, argv,
100 opts, &oind, &oarg);
101 if (opt < 0)
102 break;
103 switch ((enum opt) opt)
104 {
105 case NO_FRAME_FILTERS:
106 raw_arg = oind;
107 break;
108 }
109 }
fb40c209 110
1e611234
PM
111 /* After the last option is parsed, there should either be low -
112 high range, or no further arguments. */
113 if ((argc - oind != 0) && (argc - oind != 2))
114 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
fb40c209 115
1e611234
PM
116 /* If there is a range, set it. */
117 if (argc - oind == 2)
fb40c209 118 {
1e611234
PM
119 frame_low = atoi (argv[0 + oind]);
120 frame_high = atoi (argv[1 + oind]);
fb40c209
AC
121 }
122 else
123 {
124 /* Called with no arguments, it means we want the whole
2b03b41d 125 backtrace. */
fb40c209
AC
126 frame_low = -1;
127 frame_high = -1;
128 }
129
130 /* Let's position fi on the frame at which to start the
131 display. Could be the innermost frame if the whole stack needs
2b03b41d 132 displaying, or if frame_low is 0. */
fb40c209
AC
133 for (i = 0, fi = get_current_frame ();
134 fi && i < frame_low;
135 i++, fi = get_prev_frame (fi));
136
137 if (fi == NULL)
1b05df00 138 error (_("-stack-list-frames: Not enough frames in stack."));
fb40c209 139
79a45e25 140 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
fb40c209 141
1e611234 142 if (! raw_arg && frame_filters)
fb40c209 143 {
1e611234
PM
144 int flags = PRINT_LEVEL | PRINT_FRAME_INFO;
145 int py_frame_low = frame_low;
146
147 /* We cannot pass -1 to frame_low, as that would signify a
148 relative backtrace from the tail of the stack. So, in the case
149 of frame_low == -1, assign and increment it. */
150 if (py_frame_low == -1)
151 py_frame_low++;
152
153 result = apply_frame_filter (get_current_frame (), flags,
154 NO_VALUES, current_uiout,
155 py_frame_low, frame_high);
156 }
157
158 /* Run the inbuilt backtrace if there are no filters registered, or
159 if "--no-frame-filters" has been specified from the command. */
160 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
161 {
162 /* Now let's print the frames up to frame_high, or until there are
163 frames in the stack. */
164 for (;
165 fi && (i <= frame_high || frame_high == -1);
166 i++, fi = get_prev_frame (fi))
167 {
168 QUIT;
169 /* Print the location and the address always, even for level 0.
170 If args is 0, don't print the arguments. */
171 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
172 }
fb40c209
AC
173 }
174
6ad4a2cf 175 do_cleanups (cleanup_stack);
fb40c209
AC
176}
177
ce8f13f8 178void
fb40c209
AC
179mi_cmd_stack_info_depth (char *command, char **argv, int argc)
180{
181 int frame_high;
182 int i;
183 struct frame_info *fi;
184
fb40c209 185 if (argc > 1)
1b05df00 186 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
187
188 if (argc == 1)
189 frame_high = atoi (argv[0]);
190 else
191 /* Called with no arguments, it means we want the real depth of
2b03b41d 192 the stack. */
fb40c209
AC
193 frame_high = -1;
194
195 for (i = 0, fi = get_current_frame ();
196 fi && (i < frame_high || frame_high == -1);
197 i++, fi = get_prev_frame (fi))
198 QUIT;
199
79a45e25 200 ui_out_field_int (current_uiout, "depth", i);
fb40c209
AC
201}
202
8b777f02
VP
203static enum print_values
204parse_print_values (char *name)
205{
206 if (strcmp (name, "0") == 0
207 || strcmp (name, mi_no_values) == 0)
208 return PRINT_NO_VALUES;
209 else if (strcmp (name, "1") == 0
210 || strcmp (name, mi_all_values) == 0)
211 return PRINT_ALL_VALUES;
212 else if (strcmp (name, "2") == 0
213 || strcmp (name, mi_simple_values) == 0)
214 return PRINT_SIMPLE_VALUES;
215 else
216 error (_("Unknown value for PRINT_VALUES: must be: \
2170 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
218 mi_no_values, mi_all_values, mi_simple_values);
219}
220
7a93fb82 221/* Print a list of the locals for the current frame. With argument of
fb40c209 222 0, print only the names, with argument of 1 print also the
2b03b41d
SS
223 values. */
224
ce8f13f8 225void
fb40c209
AC
226mi_cmd_stack_list_locals (char *command, char **argv, int argc)
227{
f5ec2042 228 struct frame_info *frame;
1e611234
PM
229 int raw_arg = 0;
230 enum py_bt_status result = PY_BT_ERROR;
231 int print_value;
f5ec2042 232
1e611234
PM
233 if (argc > 0)
234 raw_arg = parse_no_frames_option (argv[0]);
fb40c209 235
1e611234
PM
236 if (argc < 1 || argc > 2 || (argc == 2 && ! raw_arg)
237 || (argc == 1 && raw_arg))
238 error (_("-stack-list-locals: Usage: [--no-frame-filters] PRINT_VALUES"));
f5ec2042 239
1e611234
PM
240 frame = get_selected_frame (NULL);
241 print_value = parse_print_values (argv[raw_arg]);
242
243 if (! raw_arg && frame_filters)
244 {
245 int flags = PRINT_LEVEL | PRINT_LOCALS;
246
247 result = apply_frame_filter (frame, flags, print_value,
248 current_uiout, 0, 0);
249 }
250
251 /* Run the inbuilt backtrace if there are no filters registered, or
252 if "--no-frame-filters" has been specified from the command. */
253 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
254 {
255 list_args_or_locals (locals, print_value, frame);
256 }
fb40c209
AC
257}
258
7a93fb82 259/* Print a list of the arguments for the current frame. With argument
fb40c209 260 of 0, print only the names, with argument of 1 print also the
2b03b41d
SS
261 values. */
262
ce8f13f8 263void
fb40c209
AC
264mi_cmd_stack_list_args (char *command, char **argv, int argc)
265{
266 int frame_low;
267 int frame_high;
268 int i;
269 struct frame_info *fi;
6ad4a2cf 270 struct cleanup *cleanup_stack_args;
8b777f02 271 enum print_values print_values;
79a45e25 272 struct ui_out *uiout = current_uiout;
1e611234
PM
273 int raw_arg = 0;
274 enum py_bt_status result = PY_BT_ERROR;
fb40c209 275
1e611234
PM
276 if (argc > 0)
277 raw_arg = parse_no_frames_option (argv[0]);
fb40c209 278
1e611234
PM
279 if (argc < 1 || (argc > 3 && ! raw_arg) || (argc == 2 && ! raw_arg))
280 error (_("-stack-list-arguments: Usage: " \
281 "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
282
283 if (argc >= 3)
fb40c209 284 {
1e611234
PM
285 frame_low = atoi (argv[1 + raw_arg]);
286 frame_high = atoi (argv[2 + raw_arg]);
fb40c209
AC
287 }
288 else
289 {
290 /* Called with no arguments, it means we want args for the whole
2b03b41d 291 backtrace. */
fb40c209
AC
292 frame_low = -1;
293 frame_high = -1;
294 }
295
1e611234 296 print_values = parse_print_values (argv[raw_arg]);
8b777f02 297
fb40c209
AC
298 /* Let's position fi on the frame at which to start the
299 display. Could be the innermost frame if the whole stack needs
2b03b41d 300 displaying, or if frame_low is 0. */
fb40c209
AC
301 for (i = 0, fi = get_current_frame ();
302 fi && i < frame_low;
303 i++, fi = get_prev_frame (fi));
304
305 if (fi == NULL)
1b05df00 306 error (_("-stack-list-arguments: Not enough frames in stack."));
fb40c209 307
9a2b4c1b
MS
308 cleanup_stack_args
309 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209 310
1e611234 311 if (! raw_arg && frame_filters)
fb40c209 312 {
1e611234
PM
313 int flags = PRINT_LEVEL | PRINT_ARGS;
314 int py_frame_low = frame_low;
315
316 /* We cannot pass -1 to frame_low, as that would signify a
317 relative backtrace from the tail of the stack. So, in the case
318 of frame_low == -1, assign and increment it. */
319 if (py_frame_low == -1)
320 py_frame_low++;
321
322 result = apply_frame_filter (get_current_frame (), flags,
323 print_values, current_uiout,
324 py_frame_low, frame_high);
fb40c209
AC
325 }
326
1e611234
PM
327 /* Run the inbuilt backtrace if there are no filters registered, or
328 if "--no-frame-filters" has been specified from the command. */
329 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
330 {
331 /* Now let's print the frames up to frame_high, or until there are
332 frames in the stack. */
333 for (;
334 fi && (i <= frame_high || frame_high == -1);
335 i++, fi = get_prev_frame (fi))
336 {
337 struct cleanup *cleanup_frame;
338
339 QUIT;
340 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
341 ui_out_field_int (uiout, "level", i);
342 list_args_or_locals (arguments, print_values, fi);
343 do_cleanups (cleanup_frame);
344 }
345 }
6ad4a2cf 346 do_cleanups (cleanup_stack_args);
fb40c209
AC
347}
348
daf3c977 349/* Print a list of the local variables (including arguments) for the
7a93fb82
VP
350 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
351 or both names and values of the variables must be printed. See
352 parse_print_value for possible values. */
2b03b41d 353
daf3c977
VP
354void
355mi_cmd_stack_list_variables (char *command, char **argv, int argc)
356{
357 struct frame_info *frame;
1e611234
PM
358 int raw_arg = 0;
359 enum py_bt_status result = PY_BT_ERROR;
360 int print_value;
daf3c977 361
1e611234
PM
362 if (argc > 0)
363 raw_arg = parse_no_frames_option (argv[0]);
daf3c977 364
1e611234
PM
365 if (argc < 1 || argc > 2 || (argc == 2 && ! raw_arg)
366 || (argc == 1 && raw_arg))
367 error (_("-stack-list-variables: Usage: " \
368 "[--no-frame-filters] PRINT_VALUES"));
daf3c977 369
1e611234
PM
370 frame = get_selected_frame (NULL);
371 print_value = parse_print_values (argv[raw_arg]);
372
373 if (! raw_arg && frame_filters)
374 {
375 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
376
377 result = apply_frame_filter (frame, flags, print_value,
378 current_uiout, 0, 0);
379 }
380
381 /* Run the inbuilt backtrace if there are no filters registered, or
382 if "--no-frame-filters" has been specified from the command. */
383 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
384 {
385 list_args_or_locals (all, print_value, frame);
386 }
daf3c977
VP
387}
388
2b03b41d
SS
389/* Print single local or argument. ARG must be already read in. For
390 WHAT and VALUES see list_args_or_locals.
93d86cef 391
2b03b41d
SS
392 Errors are printed as if they would be the parameter value. Use
393 zeroed ARG iff it should not be printed according to VALUES. */
93d86cef
JK
394
395static void
396list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
397 enum print_values values)
398{
f99d8bf4 399 struct cleanup *old_chain;
93d86cef 400 struct ui_out *uiout = current_uiout;
f99d8bf4
PA
401 struct ui_file *stb;
402
403 stb = mem_fileopen ();
404 old_chain = make_cleanup_ui_file_delete (stb);
93d86cef
JK
405
406 gdb_assert (!arg->val || !arg->error);
407 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
408 && arg->error == NULL)
409 || values == PRINT_SIMPLE_VALUES
410 || (values == PRINT_ALL_VALUES
411 && (arg->val != NULL || arg->error != NULL)));
e18b2753
JK
412 gdb_assert (arg->entry_kind == print_entry_values_no
413 || (arg->entry_kind == print_entry_values_only
414 && (arg->val || arg->error)));
93d86cef
JK
415
416 if (values != PRINT_NO_VALUES || what == all)
cd82eddc 417 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
93d86cef 418
f99d8bf4 419 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
e18b2753 420 if (arg->entry_kind == print_entry_values_only)
f99d8bf4 421 fputs_filtered ("@entry", stb);
e18b2753 422 ui_out_field_stream (uiout, "name", stb);
93d86cef
JK
423
424 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
425 ui_out_field_int (uiout, "arg", 1);
426
427 if (values == PRINT_SIMPLE_VALUES)
428 {
429 check_typedef (arg->sym->type);
f99d8bf4 430 type_print (arg->sym->type, "", stb, -1);
93d86cef
JK
431 ui_out_field_stream (uiout, "type", stb);
432 }
433
434 if (arg->val || arg->error)
435 {
436 volatile struct gdb_exception except;
437
438 if (arg->error)
439 except.message = arg->error;
440 else
441 {
442 /* TRY_CATCH has two statements, wrap it in a block. */
443
444 TRY_CATCH (except, RETURN_MASK_ERROR)
445 {
446 struct value_print_options opts;
447
448 get_raw_print_options (&opts);
449 opts.deref_ref = 1;
f99d8bf4 450 common_val_print (arg->val, stb, 0, &opts,
93d86cef
JK
451 language_def (SYMBOL_LANGUAGE (arg->sym)));
452 }
453 }
454 if (except.message)
f99d8bf4 455 fprintf_filtered (stb, _("<error reading variable: %s>"),
93d86cef
JK
456 except.message);
457 ui_out_field_stream (uiout, "value", stb);
458 }
459
f99d8bf4 460 do_cleanups (old_chain);
93d86cef 461}
daf3c977 462
fb40c209
AC
463/* Print a list of the locals or the arguments for the currently
464 selected frame. If the argument passed is 0, printonly the names
465 of the variables, if an argument of 1 is passed, print the values
2b03b41d
SS
466 as well. */
467
fb40c209 468static void
bdaf8d4a
JK
469list_args_or_locals (enum what_to_list what, enum print_values values,
470 struct frame_info *fi)
fb40c209
AC
471{
472 struct block *block;
473 struct symbol *sym;
8157b174 474 struct block_iterator iter;
6ad4a2cf 475 struct cleanup *cleanup_list;
f5ec2042 476 struct type *type;
daf3c977 477 char *name_of_result;
79a45e25 478 struct ui_out *uiout = current_uiout;
fb40c209 479
ae767bfb 480 block = get_frame_block (fi, 0);
fb40c209 481
daf3c977
VP
482 switch (what)
483 {
484 case locals:
d6fd4674
PA
485 name_of_result = "locals";
486 break;
daf3c977 487 case arguments:
d6fd4674
PA
488 name_of_result = "args";
489 break;
daf3c977 490 case all:
d6fd4674
PA
491 name_of_result = "variables";
492 break;
654e7c1f 493 default:
d6fd4674
PA
494 internal_error (__FILE__, __LINE__,
495 "unexpected what_to_list: %d", (int) what);
daf3c977
VP
496 }
497
498 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
fb40c209
AC
499
500 while (block != 0)
501 {
de4f826b 502 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 503 {
39bf4652
JB
504 int print_me = 0;
505
fb40c209
AC
506 switch (SYMBOL_CLASS (sym))
507 {
508 default:
509 case LOC_UNDEF: /* catches errors */
510 case LOC_CONST: /* constant */
511 case LOC_TYPEDEF: /* local typedef */
512 case LOC_LABEL: /* local label */
513 case LOC_BLOCK: /* local function */
514 case LOC_CONST_BYTES: /* loc. byte seq. */
515 case LOC_UNRESOLVED: /* unresolved static */
516 case LOC_OPTIMIZED_OUT: /* optimized out */
517 print_me = 0;
518 break;
519
520 case LOC_ARG: /* argument */
521 case LOC_REF_ARG: /* reference arg */
fb40c209 522 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 523 case LOC_LOCAL: /* stack local */
fb40c209
AC
524 case LOC_STATIC: /* static */
525 case LOC_REGISTER: /* register */
4cf623b6 526 case LOC_COMPUTED: /* computed location */
daf3c977 527 if (what == all)
fb40c209 528 print_me = 1;
daf3c977
VP
529 else if (what == locals)
530 print_me = !SYMBOL_IS_ARGUMENT (sym);
531 else
532 print_me = SYMBOL_IS_ARGUMENT (sym);
fb40c209
AC
533 break;
534 }
535 if (print_me)
536 {
6bb0384f 537 struct symbol *sym2;
e18b2753 538 struct frame_arg arg, entryarg;
fb40c209 539
daf3c977 540 if (SYMBOL_IS_ARGUMENT (sym))
f7e44f65 541 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
f5ec2042 542 block, VAR_DOMAIN,
1993b719 543 NULL);
f5ec2042 544 else
2a2d4dc3 545 sym2 = sym;
f7e44f65 546 gdb_assert (sym2 != NULL);
93d86cef
JK
547
548 memset (&arg, 0, sizeof (arg));
549 arg.sym = sym2;
e18b2753
JK
550 arg.entry_kind = print_entry_values_no;
551 memset (&entryarg, 0, sizeof (entryarg));
552 entryarg.sym = sym2;
553 entryarg.entry_kind = print_entry_values_no;
93d86cef 554
f5ec2042
NR
555 switch (values)
556 {
557 case PRINT_SIMPLE_VALUES:
558 type = check_typedef (sym2->type);
f5ec2042
NR
559 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
560 && TYPE_CODE (type) != TYPE_CODE_STRUCT
561 && TYPE_CODE (type) != TYPE_CODE_UNION)
562 {
f5ec2042 563 case PRINT_ALL_VALUES:
e18b2753 564 read_frame_arg (sym2, fi, &arg, &entryarg);
93d86cef 565 }
f5ec2042 566 break;
fb40c209 567 }
7a93fb82 568
e18b2753
JK
569 if (arg.entry_kind != print_entry_values_only)
570 list_arg_or_local (&arg, what, values);
571 if (entryarg.entry_kind != print_entry_values_no)
572 list_arg_or_local (&entryarg, what, values);
93d86cef 573 xfree (arg.error);
e18b2753 574 xfree (entryarg.error);
fb40c209
AC
575 }
576 }
2b03b41d 577
fb40c209
AC
578 if (BLOCK_FUNCTION (block))
579 break;
580 else
581 block = BLOCK_SUPERBLOCK (block);
582 }
6ad4a2cf 583 do_cleanups (cleanup_list);
fb40c209
AC
584}
585
ce8f13f8 586void
fb40c209
AC
587mi_cmd_stack_select_frame (char *command, char **argv, int argc)
588{
fcf43932 589 if (argc == 0 || argc > 1)
1b05df00 590 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
fb40c209 591
fcf43932 592 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 593}
64fd8944 594
ce8f13f8 595void
64fd8944
NR
596mi_cmd_stack_info_frame (char *command, char **argv, int argc)
597{
598 if (argc > 0)
2b03b41d 599 error (_("-stack-info-frame: No arguments allowed"));
ce8f13f8 600
64fd8944 601 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 602}