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