1 /* MI Command Set - stack commands.
2    Copyright (C) 2000-2021 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 "gdbsupport/gdb_optional.h"
38 #include "safe-ctype.h"
39 
40 enum what_to_list { locals, arguments, all };
41 
42 static void list_args_or_locals (const frame_print_options &fp_opts,
43 				 enum what_to_list what,
44 				 enum print_values values,
45 				 struct frame_info *fi,
46 				 int skip_unavailable);
47 
48 /* True if we want to allow Python-based frame filters.  */
49 static int frame_filters = 0;
50 
51 void
mi_cmd_enable_frame_filters(const char * command,char ** argv,int argc)52 mi_cmd_enable_frame_filters (const char *command, char **argv, int argc)
53 {
54   if (argc != 0)
55     error (_("-enable-frame-filters: no arguments allowed"));
56   frame_filters = 1;
57 }
58 
59 /* Like apply_ext_lang_frame_filter, but take a print_values */
60 
61 static enum ext_lang_bt_status
mi_apply_ext_lang_frame_filter(struct frame_info * frame,frame_filter_flags flags,enum print_values print_values,struct ui_out * out,int frame_low,int frame_high)62 mi_apply_ext_lang_frame_filter (struct frame_info *frame,
63 				frame_filter_flags flags,
64 				enum print_values print_values,
65 				struct ui_out *out,
66 				int frame_low, int frame_high)
67 {
68   /* ext_lang_frame_args's MI options are compatible with MI print
69      values.  */
70   return apply_ext_lang_frame_filter (frame, flags,
71 				      (enum ext_lang_frame_args) print_values,
72 				      out,
73 				      frame_low, frame_high);
74 }
75 
76 /* Print a list of the stack frames.  Args can be none, in which case
77    we want to print the whole backtrace, or a pair of numbers
78    specifying the frame numbers at which to start and stop the
79    display.  If the two numbers are equal, a single frame will be
80    displayed.  */
81 
82 void
mi_cmd_stack_list_frames(const char * command,char ** argv,int argc)83 mi_cmd_stack_list_frames (const char *command, char **argv, int argc)
84 {
85   int frame_low;
86   int frame_high;
87   int i;
88   struct frame_info *fi;
89   enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
90   int raw_arg = 0;
91   int oind = 0;
92   enum opt
93     {
94       NO_FRAME_FILTERS
95     };
96   static const struct mi_opt opts[] =
97     {
98       {"-no-frame-filters", NO_FRAME_FILTERS, 0},
99       { 0, 0, 0 }
100     };
101 
102   /* Parse arguments.  In this instance we are just looking for
103      --no-frame-filters.  */
104   while (1)
105     {
106       char *oarg;
107       int opt = mi_getopt ("-stack-list-frames", argc, argv,
108 			   opts, &oind, &oarg);
109       if (opt < 0)
110 	break;
111       switch ((enum opt) opt)
112 	{
113 	case NO_FRAME_FILTERS:
114 	  raw_arg = oind;
115 	  break;
116 	}
117     }
118 
119   /* After the last option is parsed, there should either be low -
120      high range, or no further arguments.  */
121   if ((argc - oind != 0) && (argc - oind != 2))
122     error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
123 
124   /* If there is a range, set it.  */
125   if (argc - oind == 2)
126     {
127       frame_low = atoi (argv[0 + oind]);
128       frame_high = atoi (argv[1 + oind]);
129     }
130   else
131     {
132       /* Called with no arguments, it means we want the whole
133 	 backtrace.  */
134       frame_low = -1;
135       frame_high = -1;
136     }
137 
138   /* Let's position fi on the frame at which to start the
139      display. Could be the innermost frame if the whole stack needs
140      displaying, or if frame_low is 0.  */
141   for (i = 0, fi = get_current_frame ();
142        fi && i < frame_low;
143        i++, fi = get_prev_frame (fi));
144 
145   if (fi == NULL)
146     error (_("-stack-list-frames: Not enough frames in stack."));
147 
148   ui_out_emit_list list_emitter (current_uiout, "stack");
149 
150   if (! raw_arg && frame_filters)
151     {
152       frame_filter_flags flags = PRINT_LEVEL | PRINT_FRAME_INFO;
153       int py_frame_low = frame_low;
154 
155       /* We cannot pass -1 to frame_low, as that would signify a
156       relative backtrace from the tail of the stack.  So, in the case
157       of frame_low == -1, assign and increment it.  */
158       if (py_frame_low == -1)
159 	py_frame_low++;
160 
161       result = apply_ext_lang_frame_filter (get_current_frame (), flags,
162 					    NO_VALUES,  current_uiout,
163 					    py_frame_low, frame_high);
164     }
165 
166   /* Run the inbuilt backtrace if there are no filters registered, or
167      if "--no-frame-filters" has been specified from the command.  */
168   if (! frame_filters || raw_arg  || result == EXT_LANG_BT_NO_FILTERS)
169     {
170       /* Now let's print the frames up to frame_high, or until there are
171 	 frames in the stack.  */
172       for (;
173 	   fi && (i <= frame_high || frame_high == -1);
174 	   i++, fi = get_prev_frame (fi))
175 	{
176 	  QUIT;
177 	  /* Print the location and the address always, even for level 0.
178 	     If args is 0, don't print the arguments.  */
179 	  print_frame_info (user_frame_print_options,
180 			    fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
181 	}
182     }
183 }
184 
185 void
mi_cmd_stack_info_depth(const char * command,char ** argv,int argc)186 mi_cmd_stack_info_depth (const char *command, char **argv, int argc)
187 {
188   int frame_high;
189   int i;
190   struct frame_info *fi;
191 
192   if (argc > 1)
193     error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
194 
195   if (argc == 1)
196     frame_high = atoi (argv[0]);
197   else
198     /* Called with no arguments, it means we want the real depth of
199        the stack.  */
200     frame_high = -1;
201 
202   for (i = 0, fi = get_current_frame ();
203        fi && (i < frame_high || frame_high == -1);
204        i++, fi = get_prev_frame (fi))
205     QUIT;
206 
207   current_uiout->field_signed ("depth", i);
208 }
209 
210 /* Print a list of the locals for the current frame.  With argument of
211    0, print only the names, with argument of 1 print also the
212    values.  */
213 
214 void
mi_cmd_stack_list_locals(const char * command,char ** argv,int argc)215 mi_cmd_stack_list_locals (const char *command, char **argv, int argc)
216 {
217   struct frame_info *frame;
218   int raw_arg = 0;
219   enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
220   enum print_values print_value;
221   int oind = 0;
222   int skip_unavailable = 0;
223 
224   if (argc > 1)
225     {
226       enum opt
227       {
228 	NO_FRAME_FILTERS,
229 	SKIP_UNAVAILABLE,
230       };
231       static const struct mi_opt opts[] =
232 	{
233 	  {"-no-frame-filters", NO_FRAME_FILTERS, 0},
234 	  {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
235 	  { 0, 0, 0 }
236 	};
237 
238       while (1)
239 	{
240 	  char *oarg;
241 	  /* Don't parse 'print-values' as an option.  */
242 	  int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
243 			       opts, &oind, &oarg);
244 
245 	  if (opt < 0)
246 	    break;
247 	  switch ((enum opt) opt)
248 	    {
249 	    case NO_FRAME_FILTERS:
250 	      raw_arg = oind;
251 	      break;
252 	    case SKIP_UNAVAILABLE:
253 	      skip_unavailable = 1;
254 	      break;
255 	    }
256 	}
257     }
258 
259   /* After the last option is parsed, there should be only
260      'print-values'.  */
261   if (argc - oind != 1)
262     error (_("-stack-list-locals: Usage: [--no-frame-filters] "
263 	     "[--skip-unavailable] PRINT_VALUES"));
264 
265   frame = get_selected_frame (NULL);
266   print_value = mi_parse_print_values (argv[oind]);
267 
268    if (! raw_arg && frame_filters)
269      {
270        frame_filter_flags flags = PRINT_LEVEL | PRINT_LOCALS;
271 
272        result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
273 						current_uiout, 0, 0);
274      }
275 
276    /* Run the inbuilt backtrace if there are no filters registered, or
277       if "--no-frame-filters" has been specified from the command.  */
278    if (! frame_filters || raw_arg  || result == EXT_LANG_BT_NO_FILTERS)
279      {
280        list_args_or_locals (user_frame_print_options,
281 			    locals, print_value, frame,
282 			    skip_unavailable);
283      }
284 }
285 
286 /* Print a list of the arguments for the current frame.  With argument
287    of 0, print only the names, with argument of 1 print also the
288    values.  */
289 
290 void
mi_cmd_stack_list_args(const char * command,char ** argv,int argc)291 mi_cmd_stack_list_args (const char *command, char **argv, int argc)
292 {
293   int frame_low;
294   int frame_high;
295   int i;
296   struct frame_info *fi;
297   enum print_values print_values;
298   struct ui_out *uiout = current_uiout;
299   int raw_arg = 0;
300   int oind = 0;
301   int skip_unavailable = 0;
302   enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
303   enum opt
304   {
305     NO_FRAME_FILTERS,
306     SKIP_UNAVAILABLE,
307   };
308   static const struct mi_opt opts[] =
309     {
310       {"-no-frame-filters", NO_FRAME_FILTERS, 0},
311       {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
312       { 0, 0, 0 }
313     };
314 
315   while (1)
316     {
317       char *oarg;
318       int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
319 					 opts, &oind, &oarg);
320 
321       if (opt < 0)
322 	break;
323       switch ((enum opt) opt)
324 	{
325 	case NO_FRAME_FILTERS:
326 	  raw_arg = oind;
327 	  break;
328 	case SKIP_UNAVAILABLE:
329 	  skip_unavailable = 1;
330 	  break;
331 	}
332     }
333 
334   if (argc - oind != 1 && argc - oind != 3)
335     error (_("-stack-list-arguments: Usage: "	\
336 	     "[--no-frame-filters] [--skip-unavailable] "
337 	     "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
338 
339   if (argc - oind == 3)
340     {
341       frame_low = atoi (argv[1 + oind]);
342       frame_high = atoi (argv[2 + oind]);
343     }
344   else
345     {
346       /* Called with no arguments, it means we want args for the whole
347 	 backtrace.  */
348       frame_low = -1;
349       frame_high = -1;
350     }
351 
352   print_values = mi_parse_print_values (argv[oind]);
353 
354   /* Let's position fi on the frame at which to start the
355      display. Could be the innermost frame if the whole stack needs
356      displaying, or if frame_low is 0.  */
357   for (i = 0, fi = get_current_frame ();
358        fi && i < frame_low;
359        i++, fi = get_prev_frame (fi));
360 
361   if (fi == NULL)
362     error (_("-stack-list-arguments: Not enough frames in stack."));
363 
364   ui_out_emit_list list_emitter (uiout, "stack-args");
365 
366   if (! raw_arg && frame_filters)
367     {
368       frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS;
369       int py_frame_low = frame_low;
370 
371       /* We cannot pass -1 to frame_low, as that would signify a
372       relative backtrace from the tail of the stack.  So, in the case
373       of frame_low == -1, assign and increment it.  */
374       if (py_frame_low == -1)
375 	py_frame_low++;
376 
377       result = mi_apply_ext_lang_frame_filter (get_current_frame (), flags,
378 					       print_values, current_uiout,
379 					       py_frame_low, frame_high);
380     }
381 
382      /* Run the inbuilt backtrace if there are no filters registered, or
383       if "--no-frame-filters" has been specified from the command.  */
384    if (! frame_filters || raw_arg  || result == EXT_LANG_BT_NO_FILTERS)
385      {
386       /* Now let's print the frames up to frame_high, or until there are
387 	 frames in the stack.  */
388       for (;
389 	   fi && (i <= frame_high || frame_high == -1);
390 	   i++, fi = get_prev_frame (fi))
391 	{
392 	  QUIT;
393 	  ui_out_emit_tuple tuple_emitter (uiout, "frame");
394 	  uiout->field_signed ("level", i);
395 	  list_args_or_locals (user_frame_print_options,
396 			       arguments, print_values, fi, skip_unavailable);
397 	}
398     }
399 }
400 
401 /* Print a list of the local variables (including arguments) for the
402    current frame.  ARGC must be 1 and ARGV[0] specify if only the names,
403    or both names and values of the variables must be printed.  See
404    parse_print_value for possible values.  */
405 
406 void
mi_cmd_stack_list_variables(const char * command,char ** argv,int argc)407 mi_cmd_stack_list_variables (const char *command, char **argv, int argc)
408 {
409   struct frame_info *frame;
410   int raw_arg = 0;
411   enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
412   enum print_values print_value;
413   int oind = 0;
414   int skip_unavailable = 0;
415 
416   if (argc > 1)
417     {
418       enum opt
419       {
420 	NO_FRAME_FILTERS,
421 	SKIP_UNAVAILABLE,
422       };
423       static const struct mi_opt opts[] =
424 	{
425 	  {"-no-frame-filters", NO_FRAME_FILTERS, 0},
426 	  {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
427 	  { 0, 0, 0 }
428 	};
429 
430       while (1)
431 	{
432 	  char *oarg;
433 	  /* Don't parse 'print-values' as an option.  */
434 	  int opt = mi_getopt ("-stack-list-variables", argc - 1,
435 			       argv, opts, &oind, &oarg);
436 	  if (opt < 0)
437 	    break;
438 	  switch ((enum opt) opt)
439 	    {
440 	    case NO_FRAME_FILTERS:
441 	      raw_arg = oind;
442 	      break;
443 	    case SKIP_UNAVAILABLE:
444 	      skip_unavailable = 1;
445 	      break;
446 	    }
447 	}
448     }
449 
450   /* After the last option is parsed, there should be only
451      'print-values'.  */
452   if (argc - oind != 1)
453     error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
454 	     "[--skip-unavailable] PRINT_VALUES"));
455 
456    frame = get_selected_frame (NULL);
457    print_value = mi_parse_print_values (argv[oind]);
458 
459    if (! raw_arg && frame_filters)
460      {
461        frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
462 
463        result = mi_apply_ext_lang_frame_filter (frame, flags,
464 						print_value,
465 						current_uiout, 0, 0);
466      }
467 
468    /* Run the inbuilt backtrace if there are no filters registered, or
469       if "--no-frame-filters" has been specified from the command.  */
470    if (! frame_filters || raw_arg  || result == EXT_LANG_BT_NO_FILTERS)
471      {
472        list_args_or_locals (user_frame_print_options,
473 			    all, print_value, frame,
474 			    skip_unavailable);
475      }
476 }
477 
478 /* Print single local or argument.  ARG must be already read in.  For
479    WHAT and VALUES see list_args_or_locals.
480 
481    Errors are printed as if they would be the parameter value.  Use
482    zeroed ARG iff it should not be printed according to VALUES.  If
483    SKIP_UNAVAILABLE is true, only print ARG if it is available.  */
484 
485 static void
list_arg_or_local(const struct frame_arg * arg,enum what_to_list what,enum print_values values,int skip_unavailable)486 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
487 		   enum print_values values, int skip_unavailable)
488 {
489   struct ui_out *uiout = current_uiout;
490 
491   gdb_assert (!arg->val || !arg->error);
492   gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
493 	       && arg->error == NULL)
494 	      || values == PRINT_SIMPLE_VALUES
495 	      || (values == PRINT_ALL_VALUES
496 		  && (arg->val != NULL || arg->error != NULL)));
497   gdb_assert (arg->entry_kind == print_entry_values_no
498 	      || (arg->entry_kind == print_entry_values_only
499 		  && (arg->val || arg->error)));
500 
501   if (skip_unavailable && arg->val != NULL
502       && (value_entirely_unavailable (arg->val)
503 	  /* A scalar object that does not have all bits available is
504 	     also considered unavailable, because all bits contribute
505 	     to its representation.  */
506 	  || (val_print_scalar_type_p (value_type (arg->val))
507 	      && !value_bytes_available (arg->val,
508 					 value_embedded_offset (arg->val),
509 					 TYPE_LENGTH (value_type (arg->val))))))
510     return;
511 
512   gdb::optional<ui_out_emit_tuple> tuple_emitter;
513   if (values != PRINT_NO_VALUES || what == all)
514     tuple_emitter.emplace (uiout, nullptr);
515 
516   string_file stb;
517 
518   stb.puts (arg->sym->print_name ());
519   if (arg->entry_kind == print_entry_values_only)
520     stb.puts ("@entry");
521   uiout->field_stream ("name", stb);
522 
523   if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
524     uiout->field_signed ("arg", 1);
525 
526   if (values == PRINT_SIMPLE_VALUES)
527     {
528       check_typedef (arg->sym->type);
529       type_print (arg->sym->type, "", &stb, -1);
530       uiout->field_stream ("type", stb);
531     }
532 
533   if (arg->val || arg->error)
534     {
535       if (arg->error)
536 	stb.printf (_("<error reading variable: %s>"), arg->error.get ());
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 (arg->sym->language ()));
547 	    }
548 	  catch (const gdb_exception_error &except)
549 	    {
550 	      stb.printf (_("<error reading variable: %s>"),
551 			  except.what ());
552 	    }
553 	}
554       uiout->field_stream ("value", stb);
555     }
556 }
557 
558 /* Print a list of the objects for the frame FI in a certain form,
559    which is determined by VALUES.  The objects can be locals,
560    arguments or both, which is determined by WHAT.  If SKIP_UNAVAILABLE
561    is true, only print the arguments or local variables whose values
562    are available.  */
563 
564 static void
list_args_or_locals(const frame_print_options & fp_opts,enum what_to_list what,enum print_values values,struct frame_info * fi,int skip_unavailable)565 list_args_or_locals (const frame_print_options &fp_opts,
566 		     enum what_to_list what, enum print_values values,
567 		     struct frame_info *fi, int skip_unavailable)
568 {
569   const struct block *block;
570   struct symbol *sym;
571   struct block_iterator iter;
572   struct type *type;
573   const char *name_of_result;
574   struct ui_out *uiout = current_uiout;
575 
576   block = get_frame_block (fi, 0);
577 
578   switch (what)
579     {
580     case locals:
581       name_of_result = "locals";
582       break;
583     case arguments:
584       name_of_result = "args";
585       break;
586     case all:
587       name_of_result = "variables";
588       break;
589     default:
590       internal_error (__FILE__, __LINE__,
591 		      "unexpected what_to_list: %d", (int) what);
592     }
593 
594   ui_out_emit_list list_emitter (uiout, name_of_result);
595 
596   while (block != 0)
597     {
598       ALL_BLOCK_SYMBOLS (block, iter, sym)
599 	{
600 	  int print_me = 0;
601 
602 	  switch (SYMBOL_CLASS (sym))
603 	    {
604 	    default:
605 	    case LOC_UNDEF:	/* catches errors        */
606 	    case LOC_CONST:	/* constant              */
607 	    case LOC_TYPEDEF:	/* local typedef         */
608 	    case LOC_LABEL:	/* local label           */
609 	    case LOC_BLOCK:	/* local function        */
610 	    case LOC_CONST_BYTES:	/* loc. byte seq.        */
611 	    case LOC_UNRESOLVED:	/* unresolved static     */
612 	    case LOC_OPTIMIZED_OUT:	/* optimized out         */
613 	      print_me = 0;
614 	      break;
615 
616 	    case LOC_ARG:	/* argument              */
617 	    case LOC_REF_ARG:	/* reference arg         */
618 	    case LOC_REGPARM_ADDR:	/* indirect register arg */
619 	    case LOC_LOCAL:	/* stack local           */
620 	    case LOC_STATIC:	/* static                */
621 	    case LOC_REGISTER:	/* register              */
622 	    case LOC_COMPUTED:	/* computed location     */
623 	      if (what == all)
624 		print_me = 1;
625 	      else if (what == locals)
626 		print_me = !SYMBOL_IS_ARGUMENT (sym);
627 	      else
628 		print_me = SYMBOL_IS_ARGUMENT (sym);
629 	      break;
630 	    }
631 	  if (print_me)
632 	    {
633 	      struct symbol *sym2;
634 	      struct frame_arg arg, entryarg;
635 
636 	      if (SYMBOL_IS_ARGUMENT (sym))
637 		sym2 = lookup_symbol_search_name (sym->search_name (),
638 						  block, VAR_DOMAIN).symbol;
639 	      else
640 		sym2 = sym;
641 	      gdb_assert (sym2 != NULL);
642 
643 	      arg.sym = sym2;
644 	      arg.entry_kind = print_entry_values_no;
645 	      entryarg.sym = sym2;
646 	      entryarg.entry_kind = print_entry_values_no;
647 
648 	      switch (values)
649 		{
650 		case PRINT_SIMPLE_VALUES:
651 		  type = check_typedef (sym2->type);
652 		  if (type->code () != TYPE_CODE_ARRAY
653 		      && type->code () != TYPE_CODE_STRUCT
654 		      && type->code () != TYPE_CODE_UNION)
655 		    {
656 		case PRINT_ALL_VALUES:
657 		  if (SYMBOL_IS_ARGUMENT (sym))
658 		    read_frame_arg (fp_opts, sym2, fi, &arg, &entryarg);
659 		  else
660 		    read_frame_local (sym2, fi, &arg);
661 		    }
662 		  break;
663 		}
664 
665 	      if (arg.entry_kind != print_entry_values_only)
666 		list_arg_or_local (&arg, what, values, skip_unavailable);
667 	      if (entryarg.entry_kind != print_entry_values_no)
668 		list_arg_or_local (&entryarg, what, values, skip_unavailable);
669 	    }
670 	}
671 
672       if (BLOCK_FUNCTION (block))
673 	break;
674       else
675 	block = BLOCK_SUPERBLOCK (block);
676     }
677 }
678 
679 /* Read a frame specification from FRAME_EXP and return the selected frame.
680    Call error() if the specification is in any way invalid (so this
681    function never returns NULL).
682 
683    The frame specification is usually an integer level number, however if
684    the number does not match a valid frame level then it will be treated as
685    a frame address.  The frame address will then be used to find a matching
686    frame in the stack.  If no matching frame is found then a new frame will
687    be created.
688 
689    The use of FRAME_EXP as an address is undocumented in the GDB user
690    manual, this feature is supported here purely for backward
691    compatibility.  */
692 
693 static struct frame_info *
parse_frame_specification(const char * frame_exp)694 parse_frame_specification (const char *frame_exp)
695 {
696   gdb_assert (frame_exp != NULL);
697 
698   /* NOTE: Parse and evaluate expression, but do not use
699      functions such as parse_and_eval_long or
700      parse_and_eval_address to also extract the value.
701      Instead value_as_long and value_as_address are used.
702      This avoids problems with expressions that contain
703      side-effects.  */
704   struct value *arg = parse_and_eval (frame_exp);
705 
706   /* Assume ARG is an integer, and try using that to select a frame.  */
707   struct frame_info *fid;
708   int level = value_as_long (arg);
709 
710   fid = find_relative_frame (get_current_frame (), &level);
711   if (level == 0)
712     /* find_relative_frame was successful.  */
713     return fid;
714 
715   /* Convert the value into a corresponding address.  */
716   CORE_ADDR addr = value_as_address (arg);
717 
718   /* Assume that ADDR is an address, use that to identify a frame with a
719      matching ID.  */
720   struct frame_id id = frame_id_build_wild (addr);
721 
722   /* If (s)he specifies the frame with an address, he deserves
723      what (s)he gets.  Still, give the highest one that matches.
724      (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
725      know).  */
726   for (fid = get_current_frame ();
727        fid != NULL;
728        fid = get_prev_frame (fid))
729     {
730       if (frame_id_eq (id, get_frame_id (fid)))
731 	{
732 	  struct frame_info *prev_frame;
733 
734 	  while (1)
735 	    {
736 	      prev_frame = get_prev_frame (fid);
737 	      if (!prev_frame
738 		  || !frame_id_eq (id, get_frame_id (prev_frame)))
739 		break;
740 	      fid = prev_frame;
741 	    }
742 	  return fid;
743 	}
744     }
745 
746   /* We couldn't identify the frame as an existing frame, but
747      perhaps we can create one with a single argument.  */
748   return create_new_frame (addr, 0);
749 }
750 
751 /* Implement the -stack-select-frame MI command.  */
752 
753 void
mi_cmd_stack_select_frame(const char * command,char ** argv,int argc)754 mi_cmd_stack_select_frame (const char *command, char **argv, int argc)
755 {
756   if (argc == 0 || argc > 1)
757     error (_("-stack-select-frame: Usage: FRAME_SPEC"));
758 
759   select_frame_for_mi (parse_frame_specification (argv[0]));
760 }
761 
762 void
mi_cmd_stack_info_frame(const char * command,char ** argv,int argc)763 mi_cmd_stack_info_frame (const char *command, char **argv, int argc)
764 {
765   if (argc > 0)
766     error (_("-stack-info-frame: No arguments allowed"));
767 
768   print_frame_info (user_frame_print_options,
769 		    get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);
770 }
771