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