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