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