daily update
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2014 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 "language.h"
31 #include "valprint.h"
32 #include "exceptions.h"
33 #include "utils.h"
34 #include "mi-getopt.h"
35 #include "extension.h"
36 #include <ctype.h>
37 #include "mi-parse.h"
38
39 enum what_to_list { locals, arguments, all };
40
41 static void list_args_or_locals (enum what_to_list what,
42 enum print_values values,
43 struct frame_info *fi,
44 int skip_unavailable);
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 ext_lang_bt_status result = EXT_LANG_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_ext_lang_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 == EXT_LANG_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 */, 0);
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 ext_lang_bt_status result = EXT_LANG_BT_ERROR;
203 int print_value;
204 int oind = 0;
205 int skip_unavailable = 0;
206 int i;
207
208 if (argc > 1)
209 {
210 int i;
211 enum opt
212 {
213 NO_FRAME_FILTERS,
214 SKIP_UNAVAILABLE,
215 };
216 static const struct mi_opt opts[] =
217 {
218 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
219 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
220 { 0, 0, 0 }
221 };
222
223 while (1)
224 {
225 char *oarg;
226 /* Don't parse 'print-values' as an option. */
227 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
228 opts, &oind, &oarg);
229
230 if (opt < 0)
231 break;
232 switch ((enum opt) opt)
233 {
234 case NO_FRAME_FILTERS:
235 raw_arg = oind;
236 case SKIP_UNAVAILABLE:
237 skip_unavailable = 1;
238 break;
239 }
240 }
241 }
242
243 /* After the last option is parsed, there should be only
244 'print-values'. */
245 if (argc - oind != 1)
246 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
247 "[--skip-unavailable] PRINT_VALUES"));
248
249 frame = get_selected_frame (NULL);
250 print_value = mi_parse_print_values (argv[oind]);
251
252 if (! raw_arg && frame_filters)
253 {
254 int flags = PRINT_LEVEL | PRINT_LOCALS;
255
256 result = apply_ext_lang_frame_filter (frame, flags, print_value,
257 current_uiout, 0, 0);
258 }
259
260 /* Run the inbuilt backtrace if there are no filters registered, or
261 if "--no-frame-filters" has been specified from the command. */
262 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
263 {
264 list_args_or_locals (locals, print_value, frame,
265 skip_unavailable);
266 }
267 }
268
269 /* Print a list of the arguments for the current frame. With argument
270 of 0, print only the names, with argument of 1 print also the
271 values. */
272
273 void
274 mi_cmd_stack_list_args (char *command, char **argv, int argc)
275 {
276 int frame_low;
277 int frame_high;
278 int i;
279 struct frame_info *fi;
280 struct cleanup *cleanup_stack_args;
281 enum print_values print_values;
282 struct ui_out *uiout = current_uiout;
283 int raw_arg = 0;
284 int oind = 0;
285 int skip_unavailable = 0;
286 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
287 enum opt
288 {
289 NO_FRAME_FILTERS,
290 SKIP_UNAVAILABLE,
291 };
292 static const struct mi_opt opts[] =
293 {
294 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
295 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
296 { 0, 0, 0 }
297 };
298
299 while (1)
300 {
301 char *oarg;
302 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
303 opts, &oind, &oarg);
304
305 if (opt < 0)
306 break;
307 switch ((enum opt) opt)
308 {
309 case NO_FRAME_FILTERS:
310 raw_arg = oind;
311 break;
312 case SKIP_UNAVAILABLE:
313 skip_unavailable = 1;
314 break;
315 }
316 }
317
318 if (argc - oind != 1 && argc - oind != 3)
319 error (_("-stack-list-arguments: Usage: " \
320 "[--no-frame-filters] [--skip-unavailable] "
321 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
322
323 if (argc - oind == 3)
324 {
325 frame_low = atoi (argv[1 + oind]);
326 frame_high = atoi (argv[2 + oind]);
327 }
328 else
329 {
330 /* Called with no arguments, it means we want args for the whole
331 backtrace. */
332 frame_low = -1;
333 frame_high = -1;
334 }
335
336 print_values = mi_parse_print_values (argv[oind]);
337
338 /* Let's position fi on the frame at which to start the
339 display. Could be the innermost frame if the whole stack needs
340 displaying, or if frame_low is 0. */
341 for (i = 0, fi = get_current_frame ();
342 fi && i < frame_low;
343 i++, fi = get_prev_frame (fi));
344
345 if (fi == NULL)
346 error (_("-stack-list-arguments: Not enough frames in stack."));
347
348 cleanup_stack_args
349 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
350
351 if (! raw_arg && frame_filters)
352 {
353 int flags = PRINT_LEVEL | PRINT_ARGS;
354 int py_frame_low = frame_low;
355
356 /* We cannot pass -1 to frame_low, as that would signify a
357 relative backtrace from the tail of the stack. So, in the case
358 of frame_low == -1, assign and increment it. */
359 if (py_frame_low == -1)
360 py_frame_low++;
361
362 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
363 print_values, current_uiout,
364 py_frame_low, frame_high);
365 }
366
367 /* Run the inbuilt backtrace if there are no filters registered, or
368 if "--no-frame-filters" has been specified from the command. */
369 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
370 {
371 /* Now let's print the frames up to frame_high, or until there are
372 frames in the stack. */
373 for (;
374 fi && (i <= frame_high || frame_high == -1);
375 i++, fi = get_prev_frame (fi))
376 {
377 struct cleanup *cleanup_frame;
378
379 QUIT;
380 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
381 ui_out_field_int (uiout, "level", i);
382 list_args_or_locals (arguments, print_values, fi, skip_unavailable);
383 do_cleanups (cleanup_frame);
384 }
385 }
386 do_cleanups (cleanup_stack_args);
387 }
388
389 /* Print a list of the local variables (including arguments) for the
390 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
391 or both names and values of the variables must be printed. See
392 parse_print_value for possible values. */
393
394 void
395 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
396 {
397 struct frame_info *frame;
398 int raw_arg = 0;
399 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
400 int print_value;
401 int oind = 0;
402 int skip_unavailable = 0;
403
404 if (argc > 1)
405 {
406 int i;
407 enum opt
408 {
409 NO_FRAME_FILTERS,
410 SKIP_UNAVAILABLE,
411 };
412 static const struct mi_opt opts[] =
413 {
414 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
415 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
416 { 0, 0, 0 }
417 };
418
419 while (1)
420 {
421 char *oarg;
422 /* Don't parse 'print-values' as an option. */
423 int opt = mi_getopt ("-stack-list-variables", argc - 1,
424 argv, opts, &oind, &oarg);
425 if (opt < 0)
426 break;
427 switch ((enum opt) opt)
428 {
429 case NO_FRAME_FILTERS:
430 raw_arg = oind;
431 break;
432 case SKIP_UNAVAILABLE:
433 skip_unavailable = 1;
434 break;
435 }
436 }
437 }
438
439 /* After the last option is parsed, there should be only
440 'print-values'. */
441 if (argc - oind != 1)
442 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
443 "[--skip-unavailable] PRINT_VALUES"));
444
445 frame = get_selected_frame (NULL);
446 print_value = mi_parse_print_values (argv[oind]);
447
448 if (! raw_arg && frame_filters)
449 {
450 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
451
452 result = apply_ext_lang_frame_filter (frame, flags, print_value,
453 current_uiout, 0, 0);
454 }
455
456 /* Run the inbuilt backtrace if there are no filters registered, or
457 if "--no-frame-filters" has been specified from the command. */
458 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
459 {
460 list_args_or_locals (all, print_value, frame,
461 skip_unavailable);
462 }
463 }
464
465 /* Print single local or argument. ARG must be already read in. For
466 WHAT and VALUES see list_args_or_locals.
467
468 Errors are printed as if they would be the parameter value. Use
469 zeroed ARG iff it should not be printed according to VALUES. If
470 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
471
472 static void
473 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
474 enum print_values values, int skip_unavailable)
475 {
476 struct cleanup *old_chain;
477 struct ui_out *uiout = current_uiout;
478 struct ui_file *stb;
479
480 gdb_assert (!arg->val || !arg->error);
481 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
482 && arg->error == NULL)
483 || values == PRINT_SIMPLE_VALUES
484 || (values == PRINT_ALL_VALUES
485 && (arg->val != NULL || arg->error != NULL)));
486 gdb_assert (arg->entry_kind == print_entry_values_no
487 || (arg->entry_kind == print_entry_values_only
488 && (arg->val || arg->error)));
489
490 if (skip_unavailable && arg->val != NULL
491 && (value_entirely_unavailable (arg->val)
492 /* A scalar object that does not have all bits available is
493 also considered unavailable, because all bits contribute
494 to its representation. */
495 || (val_print_scalar_type_p (value_type (arg->val))
496 && !value_bytes_available (arg->val,
497 value_embedded_offset (arg->val),
498 TYPE_LENGTH (value_type (arg->val))))))
499 return;
500
501 stb = mem_fileopen ();
502 old_chain = make_cleanup_ui_file_delete (stb);
503
504 if (values != PRINT_NO_VALUES || what == all)
505 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
506
507 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
508 if (arg->entry_kind == print_entry_values_only)
509 fputs_filtered ("@entry", stb);
510 ui_out_field_stream (uiout, "name", stb);
511
512 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
513 ui_out_field_int (uiout, "arg", 1);
514
515 if (values == PRINT_SIMPLE_VALUES)
516 {
517 check_typedef (arg->sym->type);
518 type_print (arg->sym->type, "", stb, -1);
519 ui_out_field_stream (uiout, "type", stb);
520 }
521
522 if (arg->val || arg->error)
523 {
524 volatile struct gdb_exception except;
525
526 if (arg->error)
527 except.message = arg->error;
528 else
529 {
530 /* TRY_CATCH has two statements, wrap it in a block. */
531
532 TRY_CATCH (except, RETURN_MASK_ERROR)
533 {
534 struct value_print_options opts;
535
536 get_no_prettyformat_print_options (&opts);
537 opts.deref_ref = 1;
538 common_val_print (arg->val, stb, 0, &opts,
539 language_def (SYMBOL_LANGUAGE (arg->sym)));
540 }
541 }
542 if (except.message)
543 fprintf_filtered (stb, _("<error reading variable: %s>"),
544 except.message);
545 ui_out_field_stream (uiout, "value", stb);
546 }
547
548 do_cleanups (old_chain);
549 }
550
551 /* Print a list of the objects for the frame FI in a certain form,
552 which is determined by VALUES. The objects can be locals,
553 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
554 is true, only print the arguments or local variables whose values
555 are available. */
556
557 static void
558 list_args_or_locals (enum what_to_list what, enum print_values values,
559 struct frame_info *fi, int skip_unavailable)
560 {
561 const struct block *block;
562 struct symbol *sym;
563 struct block_iterator iter;
564 struct cleanup *cleanup_list;
565 struct type *type;
566 char *name_of_result;
567 struct ui_out *uiout = current_uiout;
568
569 block = get_frame_block (fi, 0);
570
571 switch (what)
572 {
573 case locals:
574 name_of_result = "locals";
575 break;
576 case arguments:
577 name_of_result = "args";
578 break;
579 case all:
580 name_of_result = "variables";
581 break;
582 default:
583 internal_error (__FILE__, __LINE__,
584 "unexpected what_to_list: %d", (int) what);
585 }
586
587 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
588
589 while (block != 0)
590 {
591 ALL_BLOCK_SYMBOLS (block, iter, sym)
592 {
593 int print_me = 0;
594
595 switch (SYMBOL_CLASS (sym))
596 {
597 default:
598 case LOC_UNDEF: /* catches errors */
599 case LOC_CONST: /* constant */
600 case LOC_TYPEDEF: /* local typedef */
601 case LOC_LABEL: /* local label */
602 case LOC_BLOCK: /* local function */
603 case LOC_CONST_BYTES: /* loc. byte seq. */
604 case LOC_UNRESOLVED: /* unresolved static */
605 case LOC_OPTIMIZED_OUT: /* optimized out */
606 print_me = 0;
607 break;
608
609 case LOC_ARG: /* argument */
610 case LOC_REF_ARG: /* reference arg */
611 case LOC_REGPARM_ADDR: /* indirect register arg */
612 case LOC_LOCAL: /* stack local */
613 case LOC_STATIC: /* static */
614 case LOC_REGISTER: /* register */
615 case LOC_COMPUTED: /* computed location */
616 if (what == all)
617 print_me = 1;
618 else if (what == locals)
619 print_me = !SYMBOL_IS_ARGUMENT (sym);
620 else
621 print_me = SYMBOL_IS_ARGUMENT (sym);
622 break;
623 }
624 if (print_me)
625 {
626 struct symbol *sym2;
627 struct frame_arg arg, entryarg;
628
629 if (SYMBOL_IS_ARGUMENT (sym))
630 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
631 block, VAR_DOMAIN,
632 NULL);
633 else
634 sym2 = sym;
635 gdb_assert (sym2 != NULL);
636
637 memset (&arg, 0, sizeof (arg));
638 arg.sym = sym2;
639 arg.entry_kind = print_entry_values_no;
640 memset (&entryarg, 0, sizeof (entryarg));
641 entryarg.sym = sym2;
642 entryarg.entry_kind = print_entry_values_no;
643
644 switch (values)
645 {
646 case PRINT_SIMPLE_VALUES:
647 type = check_typedef (sym2->type);
648 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
649 && TYPE_CODE (type) != TYPE_CODE_STRUCT
650 && TYPE_CODE (type) != TYPE_CODE_UNION)
651 {
652 case PRINT_ALL_VALUES:
653 if (SYMBOL_IS_ARGUMENT (sym))
654 read_frame_arg (sym2, fi, &arg, &entryarg);
655 else
656 read_frame_local (sym2, fi, &arg);
657 }
658 break;
659 }
660
661 if (arg.entry_kind != print_entry_values_only)
662 list_arg_or_local (&arg, what, values, skip_unavailable);
663 if (entryarg.entry_kind != print_entry_values_no)
664 list_arg_or_local (&entryarg, what, values, skip_unavailable);
665 xfree (arg.error);
666 xfree (entryarg.error);
667 }
668 }
669
670 if (BLOCK_FUNCTION (block))
671 break;
672 else
673 block = BLOCK_SUPERBLOCK (block);
674 }
675 do_cleanups (cleanup_list);
676 }
677
678 void
679 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
680 {
681 if (argc == 0 || argc > 1)
682 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
683
684 select_frame_command (argv[0], 1 /* not used */ );
685 }
686
687 void
688 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
689 {
690 if (argc > 0)
691 error (_("-stack-info-frame: No arguments allowed"));
692
693 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);
694 }
This page took 0.045303 seconds and 4 git commands to generate.