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