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