xref: /freebsd-12.1/contrib/gdb/gdb/stack.c (revision b4b74d61)
1 /* Print and select stack frames for GDB, the GNU debugger.
2 
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
5    Software Foundation, Inc.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23 
24 #include <ctype.h>
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include "value.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "language.h"
32 #include "frame.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "target.h"
36 #include "source.h"
37 #include "breakpoint.h"
38 #include "demangle.h"
39 #include "inferior.h"
40 #include "annotate.h"
41 #include "ui-out.h"
42 #include "block.h"
43 #include "stack.h"
44 #include "gdb_assert.h"
45 #include "dictionary.h"
46 #include "reggroups.h"
47 #include "regcache.h"
48 
49 /* Prototypes for exported functions. */
50 
51 void args_info (char *, int);
52 
53 void locals_info (char *, int);
54 
55 void (*selected_frame_level_changed_hook) (int);
56 
57 void _initialize_stack (void);
58 
59 /* Prototypes for local functions. */
60 
61 static void down_command (char *, int);
62 
63 static void down_silently_base (char *);
64 
65 static void down_silently_command (char *, int);
66 
67 static void up_command (char *, int);
68 
69 static void up_silently_base (char *);
70 
71 static void up_silently_command (char *, int);
72 
73 void frame_command (char *, int);
74 
75 static void current_frame_command (char *, int);
76 
77 static void print_frame_arg_vars (struct frame_info *, struct ui_file *);
78 
79 static void catch_info (char *, int);
80 
81 static void args_plus_locals_info (char *, int);
82 
83 static void print_frame_label_vars (struct frame_info *, int,
84 				    struct ui_file *);
85 
86 static void print_frame_local_vars (struct frame_info *, int,
87 				    struct ui_file *);
88 
89 static int print_block_frame_labels (struct block *, int *,
90 				     struct ui_file *);
91 
92 static int print_block_frame_locals (struct block *,
93 				     struct frame_info *,
94 				     int,
95 				     struct ui_file *);
96 
97 static void print_frame (struct frame_info *fi,
98 			 int level,
99 			 int source,
100 			 int args,
101 			 struct symtab_and_line sal);
102 
103 static void backtrace_command (char *, int);
104 
105 struct frame_info *parse_frame_specification (char *);
106 
107 static void frame_info (char *, int);
108 
109 extern int addressprint;	/* Print addresses, or stay symbolic only? */
110 
111 /* Zero means do things normally; we are interacting directly with the
112    user.  One means print the full filename and linenumber when a
113    frame is printed, and do so in a format emacs18/emacs19.22 can
114    parse.  Two means print similar annotations, but in many more
115    cases and in a slightly different syntax.  */
116 
117 int annotation_level = 0;
118 
119 
120 struct print_stack_frame_args
121   {
122     struct frame_info *fi;
123     int level;
124     int source;
125     int args;
126   };
127 
128 /* Show or print the frame arguments.
129    Pass the args the way catch_errors wants them.  */
130 static int print_stack_frame_stub (void *args);
131 static int
print_stack_frame_stub(void * args)132 print_stack_frame_stub (void *args)
133 {
134   struct print_stack_frame_args *p = (struct print_stack_frame_args *) args;
135 
136   print_frame_info (p->fi, p->level, p->source, p->args);
137   return 0;
138 }
139 
140 /* Show or print a stack frame briefly.  FRAME_INFI should be the frame info
141    and LEVEL should be its level in the stack (or -1 for level not defined).
142    This prints the level, the function executing, the arguments,
143    and the file name and line number.
144    If the pc is not at the beginning of the source line,
145    the actual pc is printed at the beginning.
146 
147    If SOURCE is 1, print the source line as well.
148    If SOURCE is -1, print ONLY the source line.  */
149 
150 void
print_stack_frame(struct frame_info * fi,int level,int source)151 print_stack_frame (struct frame_info *fi, int level, int source)
152 {
153   struct print_stack_frame_args args;
154 
155   args.fi = fi;
156   args.level = level;
157   args.source = source;
158   args.args = 1;
159 
160   catch_errors (print_stack_frame_stub, (char *) &args, "", RETURN_MASK_ALL);
161 }
162 
163 struct print_args_args
164 {
165   struct symbol *func;
166   struct frame_info *fi;
167   struct ui_file *stream;
168 };
169 
170 static int print_args_stub (void *);
171 
172 /* Print nameless args on STREAM.
173    FI is the frameinfo for this frame, START is the offset
174    of the first nameless arg, and NUM is the number of nameless args to
175    print.  FIRST is nonzero if this is the first argument (not just
176    the first nameless arg).  */
177 
178 static void
print_frame_nameless_args(struct frame_info * fi,long start,int num,int first,struct ui_file * stream)179 print_frame_nameless_args (struct frame_info *fi, long start, int num,
180 			   int first, struct ui_file *stream)
181 {
182   int i;
183   CORE_ADDR argsaddr;
184   long arg_value;
185 
186   for (i = 0; i < num; i++)
187     {
188       QUIT;
189       argsaddr = get_frame_args_address (fi);
190       if (!argsaddr)
191 	return;
192       arg_value = read_memory_integer (argsaddr + start, sizeof (int));
193       if (!first)
194 	fprintf_filtered (stream, ", ");
195       fprintf_filtered (stream, "%ld", arg_value);
196       first = 0;
197       start += sizeof (int);
198     }
199 }
200 
201 /* Print the arguments of a stack frame, given the function FUNC
202    running in that frame (as a symbol), the info on the frame,
203    and the number of args according to the stack frame (or -1 if unknown).  */
204 
205 /* References here and elsewhere to "number of args according to the
206    stack frame" appear in all cases to refer to "number of ints of args
207    according to the stack frame".  At least for VAX, i386, isi.  */
208 
209 static void
print_frame_args(struct symbol * func,struct frame_info * fi,int num,struct ui_file * stream)210 print_frame_args (struct symbol *func, struct frame_info *fi, int num,
211 		  struct ui_file *stream)
212 {
213   struct block *b = NULL;
214   int first = 1;
215   struct dict_iterator iter;
216   struct symbol *sym;
217   struct value *val;
218   /* Offset of next stack argument beyond the one we have seen that is
219      at the highest offset.
220      -1 if we haven't come to a stack argument yet.  */
221   long highest_offset = -1;
222   int arg_size;
223   /* Number of ints of arguments that we have printed so far.  */
224   int args_printed = 0;
225   struct cleanup *old_chain, *list_chain;
226   struct ui_stream *stb;
227 
228   stb = ui_out_stream_new (uiout);
229   old_chain = make_cleanup_ui_out_stream_delete (stb);
230 
231   if (func)
232     {
233       b = SYMBOL_BLOCK_VALUE (func);
234 
235       ALL_BLOCK_SYMBOLS (b, iter, sym)
236         {
237 	  QUIT;
238 
239 	  /* Keep track of the highest stack argument offset seen, and
240 	     skip over any kinds of symbols we don't care about.  */
241 
242 	  switch (SYMBOL_CLASS (sym))
243 	    {
244 	    case LOC_ARG:
245 	    case LOC_REF_ARG:
246 	      {
247 		long current_offset = SYMBOL_VALUE (sym);
248 		arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
249 
250 		/* Compute address of next argument by adding the size of
251 		   this argument and rounding to an int boundary.  */
252 		current_offset =
253 		  ((current_offset + arg_size + sizeof (int) - 1)
254 		   & ~(sizeof (int) - 1));
255 
256 		/* If this is the highest offset seen yet, set highest_offset.  */
257 		if (highest_offset == -1
258 		    || (current_offset > highest_offset))
259 		  highest_offset = current_offset;
260 
261 		/* Add the number of ints we're about to print to args_printed.  */
262 		args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
263 	      }
264 
265 	      /* We care about types of symbols, but don't need to keep track of
266 		 stack offsets in them.  */
267 	    case LOC_REGPARM:
268 	    case LOC_REGPARM_ADDR:
269 	    case LOC_LOCAL_ARG:
270 	    case LOC_BASEREG_ARG:
271 	    case LOC_COMPUTED_ARG:
272 	      break;
273 
274 	    /* Other types of symbols we just skip over.  */
275 	    default:
276 	      continue;
277 	    }
278 
279 	  /* We have to look up the symbol because arguments can have
280 	     two entries (one a parameter, one a local) and the one we
281 	     want is the local, which lookup_symbol will find for us.
282 	     This includes gcc1 (not gcc2) on the sparc when passing a
283 	     small structure and gcc2 when the argument type is float
284 	     and it is passed as a double and converted to float by
285 	     the prologue (in the latter case the type of the LOC_ARG
286 	     symbol is double and the type of the LOC_LOCAL symbol is
287 	     float).  */
288 	  /* But if the parameter name is null, don't try it.
289 	     Null parameter names occur on the RS/6000, for traceback tables.
290 	     FIXME, should we even print them?  */
291 
292 	  if (*DEPRECATED_SYMBOL_NAME (sym))
293 	    {
294 	      struct symbol *nsym;
295 	      nsym = lookup_symbol
296 		(DEPRECATED_SYMBOL_NAME (sym),
297 		 b, VAR_DOMAIN, (int *) NULL, (struct symtab **) NULL);
298 	      if (SYMBOL_CLASS (nsym) == LOC_REGISTER)
299 		{
300 		  /* There is a LOC_ARG/LOC_REGISTER pair.  This means that
301 		     it was passed on the stack and loaded into a register,
302 		     or passed in a register and stored in a stack slot.
303 		     GDB 3.x used the LOC_ARG; GDB 4.0-4.11 used the LOC_REGISTER.
304 
305 		     Reasons for using the LOC_ARG:
306 		     (1) because find_saved_registers may be slow for remote
307 		     debugging,
308 		     (2) because registers are often re-used and stack slots
309 		     rarely (never?) are.  Therefore using the stack slot is
310 		     much less likely to print garbage.
311 
312 		     Reasons why we might want to use the LOC_REGISTER:
313 		     (1) So that the backtrace prints the same value as
314 		     "print foo".  I see no compelling reason why this needs
315 		     to be the case; having the backtrace print the value which
316 		     was passed in, and "print foo" print the value as modified
317 		     within the called function, makes perfect sense to me.
318 
319 		     Additional note:  It might be nice if "info args" displayed
320 		     both values.
321 		     One more note:  There is a case with sparc structure passing
322 		     where we need to use the LOC_REGISTER, but this is dealt with
323 		     by creating a single LOC_REGPARM in symbol reading.  */
324 
325 		  /* Leave sym (the LOC_ARG) alone.  */
326 		  ;
327 		}
328 	      else
329 		sym = nsym;
330 	    }
331 
332 	  /* Print the current arg.  */
333 	  if (!first)
334 	    ui_out_text (uiout, ", ");
335 	  ui_out_wrap_hint (uiout, "    ");
336 
337 	  annotate_arg_begin ();
338 
339 	  list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
340 	  fprintf_symbol_filtered (stb->stream, SYMBOL_PRINT_NAME (sym),
341 				   SYMBOL_LANGUAGE (sym), DMGL_PARAMS | DMGL_ANSI);
342 	  ui_out_field_stream (uiout, "name", stb);
343 	  annotate_arg_name_end ();
344 	  ui_out_text (uiout, "=");
345 
346 	  /* Avoid value_print because it will deref ref parameters.  We just
347 	     want to print their addresses.  Print ??? for args whose address
348 	     we do not know.  We pass 2 as "recurse" to val_print because our
349 	     standard indentation here is 4 spaces, and val_print indents
350 	     2 for each recurse.  */
351 	  val = read_var_value (sym, fi);
352 
353 	  annotate_arg_value (val == NULL ? NULL : VALUE_TYPE (val));
354 
355 	  if (val)
356 	    {
357 	      common_val_print (val, stb->stream, 0, 0, 2, Val_no_prettyprint);
358 	      ui_out_field_stream (uiout, "value", stb);
359 	    }
360 	  else
361 	    ui_out_text (uiout, "???");
362 
363 	  /* Invoke ui_out_tuple_end.  */
364 	  do_cleanups (list_chain);
365 
366 	  annotate_arg_end ();
367 
368 	  first = 0;
369 	}
370     }
371 
372   /* Don't print nameless args in situations where we don't know
373      enough about the stack to find them.  */
374   if (num != -1)
375     {
376       long start;
377 
378       if (highest_offset == -1)
379 	start = FRAME_ARGS_SKIP;
380       else
381 	start = highest_offset;
382 
383       print_frame_nameless_args (fi, start, num - args_printed,
384 				 first, stream);
385     }
386   do_cleanups (old_chain);
387 }
388 
389 /* Pass the args the way catch_errors wants them.  */
390 
391 static int
print_args_stub(void * args)392 print_args_stub (void *args)
393 {
394   int numargs;
395   struct print_args_args *p = (struct print_args_args *) args;
396 
397   if (FRAME_NUM_ARGS_P ())
398     {
399       numargs = FRAME_NUM_ARGS (p->fi);
400       gdb_assert (numargs >= 0);
401     }
402   else
403     numargs = -1;
404   print_frame_args (p->func, p->fi, numargs, p->stream);
405   return 0;
406 }
407 
408 /* Print information about a frame for frame "fi" at level "level".
409    Used in "where" output, also used to emit breakpoint or step
410    messages.
411    LEVEL is the level of the frame, or -1 if it is the
412    innermost frame but we don't want to print the level.
413    The meaning of the SOURCE argument is:
414    SRC_LINE: Print only source line
415    LOCATION: Print only location
416    LOC_AND_SRC: Print location and source line.  */
417 
418 void
print_frame_info(struct frame_info * fi,int level,int source,int args)419 print_frame_info (struct frame_info *fi, int level, int source, int args)
420 {
421   struct symtab_and_line sal;
422   int source_print;
423   int location_print;
424 
425   if (get_frame_type (fi) == DUMMY_FRAME
426       || get_frame_type (fi) == SIGTRAMP_FRAME)
427     {
428       struct cleanup *uiout_cleanup
429 	= make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
430 
431       annotate_frame_begin (level == -1 ? 0 : level, get_frame_pc (fi));
432 
433       /* Do this regardless of SOURCE because we don't have any source
434          to list for this frame.  */
435       if (level >= 0)
436         {
437           ui_out_text (uiout, "#");
438           ui_out_field_fmt_int (uiout, 2, ui_left, "level", level);
439         }
440       if (ui_out_is_mi_like_p (uiout))
441         {
442           annotate_frame_address ();
443           ui_out_field_core_addr (uiout, "addr", get_frame_pc (fi));
444           annotate_frame_address_end ();
445         }
446 
447       if (get_frame_type (fi) == DUMMY_FRAME)
448         {
449           annotate_function_call ();
450           ui_out_field_string (uiout, "func", "<function called from gdb>");
451 	}
452       else if (get_frame_type (fi) == SIGTRAMP_FRAME)
453         {
454 	  annotate_signal_handler_caller ();
455           ui_out_field_string (uiout, "func", "<signal handler called>");
456         }
457       ui_out_text (uiout, "\n");
458       annotate_frame_end ();
459 
460       do_cleanups (uiout_cleanup);
461       return;
462     }
463 
464   /* If fi is not the innermost frame, that normally means that fi->pc
465      points to *after* the call instruction, and we want to get the
466      line containing the call, never the next line.  But if the next
467      frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the next frame
468      was not entered as the result of a call, and we want to get the
469      line containing fi->pc.  */
470   find_frame_sal (fi, &sal);
471 
472   location_print = (source == LOCATION
473 		    || source == LOC_AND_ADDRESS
474 		    || source == SRC_AND_LOC);
475 
476   if (location_print || !sal.symtab)
477     print_frame (fi, level, source, args, sal);
478 
479   source_print = (source == SRC_LINE || source == SRC_AND_LOC);
480 
481   if (sal.symtab)
482     set_current_source_symtab_and_line (&sal);
483 
484   if (source_print && sal.symtab)
485     {
486       struct symtab_and_line cursal;
487       int done = 0;
488       int mid_statement = (source == SRC_LINE) && (get_frame_pc (fi) != sal.pc);
489 
490       if (annotation_level)
491 	done = identify_source_line (sal.symtab, sal.line, mid_statement,
492 				     get_frame_pc (fi));
493       if (!done)
494 	{
495 	  if (print_frame_info_listing_hook)
496 	    print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
497 	  else
498 	    {
499 	      /* We used to do this earlier, but that is clearly
500 		 wrong. This function is used by many different
501 		 parts of gdb, including normal_stop in infrun.c,
502 		 which uses this to print out the current PC
503 		 when we stepi/nexti into the middle of a source
504 		 line. Only the command line really wants this
505 		 behavior. Other UIs probably would like the
506 		 ability to decide for themselves if it is desired. */
507 	      if (addressprint && mid_statement)
508 		{
509 		  ui_out_field_core_addr (uiout, "addr", get_frame_pc (fi));
510 		  ui_out_text (uiout, "\t");
511 		}
512 
513 	      print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
514 	    }
515 	}
516       /* Make sure we have at least a default source file */
517       set_default_source_symtab_and_line ();
518       cursal = get_current_source_symtab_and_line ();
519       cursal.line = max (sal.line - get_lines_to_list () / 2, 1);
520       set_current_source_symtab_and_line (&cursal);
521     }
522 
523   if (source != 0)
524     set_default_breakpoint (1, get_frame_pc (fi), sal.symtab, sal.line);
525 
526   annotate_frame_end ();
527 
528   gdb_flush (gdb_stdout);
529 }
530 
531 static void
print_frame(struct frame_info * fi,int level,int source,int args,struct symtab_and_line sal)532 print_frame (struct frame_info *fi,
533 	     int level,
534 	     int source,
535 	     int args,
536 	     struct symtab_and_line sal)
537 {
538   struct symbol *func;
539   char *funname = 0;
540   enum language funlang = language_unknown;
541   struct ui_stream *stb;
542   struct cleanup *old_chain;
543   struct cleanup *list_chain;
544 
545   stb = ui_out_stream_new (uiout);
546   old_chain = make_cleanup_ui_out_stream_delete (stb);
547 
548   func = find_pc_function (get_frame_address_in_block (fi));
549   if (func)
550     {
551       /* In certain pathological cases, the symtabs give the wrong
552          function (when we are in the first function in a file which
553          is compiled without debugging symbols, the previous function
554          is compiled with debugging symbols, and the "foo.o" symbol
555          that is supposed to tell us where the file with debugging symbols
556          ends has been truncated by ar because it is longer than 15
557          characters).  This also occurs if the user uses asm() to create
558          a function but not stabs for it (in a file compiled -g).
559 
560          So look in the minimal symbol tables as well, and if it comes
561          up with a larger address for the function use that instead.
562          I don't think this can ever cause any problems; there shouldn't
563          be any minimal symbols in the middle of a function; if this is
564          ever changed many parts of GDB will need to be changed (and we'll
565          create a find_pc_minimal_function or some such).  */
566 
567       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_address_in_block (fi));
568       if (msymbol != NULL
569 	  && (SYMBOL_VALUE_ADDRESS (msymbol)
570 	      > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
571 	{
572 #if 0
573 	  /* There is no particular reason to think the line number
574 	     information is wrong.  Someone might have just put in
575 	     a label with asm() but left the line numbers alone.  */
576 	  /* In this case we have no way of knowing the source file
577 	     and line number, so don't print them.  */
578 	  sal.symtab = 0;
579 #endif
580 	  /* We also don't know anything about the function besides
581 	     its address and name.  */
582 	  func = 0;
583 	  funname = DEPRECATED_SYMBOL_NAME (msymbol);
584 	  funlang = SYMBOL_LANGUAGE (msymbol);
585 	}
586       else
587 	{
588 	  /* I'd like to use SYMBOL_PRINT_NAME() here, to display the
589 	     demangled name that we already have stored in the symbol
590 	     table, but we stored a version with DMGL_PARAMS turned
591 	     on, and here we don't want to display parameters. So call
592 	     the demangler again, with DMGL_ANSI only. (Yes, I know
593 	     that printf_symbol_filtered() will again try to demangle
594 	     the name on the fly, but the issue is that if
595 	     cplus_demangle() fails here, it'll fail there too. So we
596 	     want to catch the failure ("demangled==NULL" case below)
597 	     here, while we still have our hands on the function
598 	     symbol.) */
599 	  char *demangled;
600 	  funname = DEPRECATED_SYMBOL_NAME (func);
601 	  funlang = SYMBOL_LANGUAGE (func);
602 	  if (funlang == language_cplus)
603 	    {
604 	      demangled = cplus_demangle (funname, DMGL_ANSI);
605 	      if (demangled == NULL)
606 		/* If the demangler fails, try the demangled name from
607 		   the symbol table. This'll have parameters, but
608 		   that's preferable to diplaying a mangled name. */
609 		funname = SYMBOL_PRINT_NAME (func);
610 	    }
611 	}
612     }
613   else
614     {
615       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_address_in_block (fi));
616       if (msymbol != NULL)
617 	{
618 	  funname = DEPRECATED_SYMBOL_NAME (msymbol);
619 	  funlang = SYMBOL_LANGUAGE (msymbol);
620 	}
621     }
622 
623   annotate_frame_begin (level == -1 ? 0 : level, get_frame_pc (fi));
624 
625   list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
626 
627   if (level >= 0)
628     {
629       ui_out_text (uiout, "#");
630       ui_out_field_fmt_int (uiout, 2, ui_left, "level", level);
631     }
632   if (addressprint)
633     if (get_frame_pc (fi) != sal.pc
634 	|| !sal.symtab
635 	|| source == LOC_AND_ADDRESS)
636       {
637 	annotate_frame_address ();
638 	ui_out_field_core_addr (uiout, "addr", get_frame_pc (fi));
639 	annotate_frame_address_end ();
640 	ui_out_text (uiout, " in ");
641       }
642   annotate_frame_function_name ();
643   fprintf_symbol_filtered (stb->stream, funname ? funname : "??", funlang,
644 			   DMGL_ANSI);
645   ui_out_field_stream (uiout, "func", stb);
646   ui_out_wrap_hint (uiout, "   ");
647   annotate_frame_args ();
648 
649   ui_out_text (uiout, " (");
650   if (args)
651     {
652       struct print_args_args args;
653       struct cleanup *args_list_chain;
654       args.fi = fi;
655       args.func = func;
656       args.stream = gdb_stdout;
657       args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
658       catch_errors (print_args_stub, &args, "", RETURN_MASK_ALL);
659       /* FIXME: args must be a list. If one argument is a string it will
660 		 have " that will not be properly escaped.  */
661       /* Invoke ui_out_tuple_end.  */
662       do_cleanups (args_list_chain);
663       QUIT;
664     }
665   ui_out_text (uiout, ")");
666   if (sal.symtab && sal.symtab->filename)
667     {
668       annotate_frame_source_begin ();
669       ui_out_wrap_hint (uiout, "   ");
670       ui_out_text (uiout, " at ");
671       annotate_frame_source_file ();
672       ui_out_field_string (uiout, "file", sal.symtab->filename);
673       annotate_frame_source_file_end ();
674       ui_out_text (uiout, ":");
675       annotate_frame_source_line ();
676       ui_out_field_int (uiout, "line", sal.line);
677       annotate_frame_source_end ();
678     }
679 
680 #ifdef PC_SOLIB
681   if (!funname || (!sal.symtab || !sal.symtab->filename))
682     {
683       char *lib = PC_SOLIB (get_frame_pc (fi));
684       if (lib)
685 	{
686 	  annotate_frame_where ();
687 	  ui_out_wrap_hint (uiout, "  ");
688 	  ui_out_text (uiout, " from ");
689 	  ui_out_field_string (uiout, "from", lib);
690 	}
691     }
692 #endif /* PC_SOLIB */
693 
694   /* do_cleanups will call ui_out_tuple_end() for us.  */
695   do_cleanups (list_chain);
696   ui_out_text (uiout, "\n");
697   do_cleanups (old_chain);
698 }
699 
700 /* Show the frame info.  If this is the tui, it will be shown in
701    the source display otherwise, nothing is done */
702 void
show_stack_frame(struct frame_info * fi)703 show_stack_frame (struct frame_info *fi)
704 {
705 }
706 
707 
708 /* Read a frame specification in whatever the appropriate format is.
709    Call error() if the specification is in any way invalid (i.e.
710    this function never returns NULL).  */
711 
712 struct frame_info *
parse_frame_specification(char * frame_exp)713 parse_frame_specification (char *frame_exp)
714 {
715   int numargs = 0;
716 #define	MAXARGS	4
717   CORE_ADDR args[MAXARGS];
718   int level;
719 
720   if (frame_exp)
721     {
722       char *addr_string, *p;
723       struct cleanup *tmp_cleanup;
724 
725       while (*frame_exp == ' ')
726 	frame_exp++;
727 
728       while (*frame_exp)
729 	{
730 	  if (numargs > MAXARGS)
731 	    error ("Too many args in frame specification");
732 	  /* Parse an argument.  */
733 	  for (p = frame_exp; *p && *p != ' '; p++)
734 	    ;
735 	  addr_string = savestring (frame_exp, p - frame_exp);
736 
737 	  {
738 	    struct value *vp;
739 
740 	    tmp_cleanup = make_cleanup (xfree, addr_string);
741 
742 	    /* NOTE: we call parse_and_eval and then both
743 	       value_as_long and value_as_address rather than calling
744 	       parse_and_eval_long and parse_and_eval_address because
745 	       of the issue of potential side effects from evaluating
746 	       the expression.  */
747 	    vp = parse_and_eval (addr_string);
748 	    if (numargs == 0)
749 	      level = value_as_long (vp);
750 
751 	    args[numargs++] = value_as_address (vp);
752 	    do_cleanups (tmp_cleanup);
753 	  }
754 
755 	  /* Skip spaces, move to possible next arg.  */
756 	  while (*p == ' ')
757 	    p++;
758 	  frame_exp = p;
759 	}
760     }
761 
762   switch (numargs)
763     {
764     case 0:
765       if (deprecated_selected_frame == NULL)
766 	error ("No selected frame.");
767       return deprecated_selected_frame;
768       /* NOTREACHED */
769     case 1:
770       {
771 	struct frame_info *fid =
772 	find_relative_frame (get_current_frame (), &level);
773 	struct frame_info *tfid;
774 
775 	if (level == 0)
776 	  /* find_relative_frame was successful */
777 	  return fid;
778 
779 	/* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
780 	   take at least 2 addresses.  It is important to detect this case
781 	   here so that "frame 100" does not give a confusing error message
782 	   like "frame specification requires two addresses".  This of course
783 	   does not solve the "frame 100" problem for machines on which
784 	   a frame specification can be made with one address.  To solve
785 	   that, we need a new syntax for a specifying a frame by address.
786 	   I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
787 	   two args, etc.), but people might think that is too much typing,
788 	   so I guess *0x23,0x45 would be a possible alternative (commas
789 	   really should be used instead of spaces to delimit; using spaces
790 	   normally works in an expression).  */
791 #ifdef SETUP_ARBITRARY_FRAME
792 	error ("No frame %s", paddr_d (args[0]));
793 #endif
794 
795 	/* If (s)he specifies the frame with an address, he deserves what
796 	   (s)he gets.  Still, give the highest one that matches.  */
797 
798 	for (fid = get_current_frame ();
799 	     fid && get_frame_base (fid) != args[0];
800 	     fid = get_prev_frame (fid))
801 	  ;
802 
803 	if (fid)
804 	  while ((tfid = get_prev_frame (fid)) &&
805 		 (get_frame_base (tfid) == args[0]))
806 	    fid = tfid;
807 
808 	/* We couldn't identify the frame as an existing frame, but
809 	   perhaps we can create one with a single argument.  */
810       }
811 
812     default:
813 #ifdef SETUP_ARBITRARY_FRAME
814       return SETUP_ARBITRARY_FRAME (numargs, args);
815 #else
816       /* Usual case.  Do it here rather than have everyone supply
817          a SETUP_ARBITRARY_FRAME that does this.  */
818       if (numargs == 1)
819 	return create_new_frame (args[0], 0);
820       error ("Too many args in frame specification");
821 #endif
822       /* NOTREACHED */
823     }
824   /* NOTREACHED */
825 }
826 
827 /* Print verbosely the selected frame or the frame at address ADDR.
828    This means absolutely all information in the frame is printed.  */
829 
830 static void
frame_info(char * addr_exp,int from_tty)831 frame_info (char *addr_exp, int from_tty)
832 {
833   struct frame_info *fi;
834   struct symtab_and_line sal;
835   struct symbol *func;
836   struct symtab *s;
837   struct frame_info *calling_frame_info;
838   int i, count, numregs;
839   char *funname = 0;
840   enum language funlang = language_unknown;
841   const char *pc_regname;
842 
843   if (!target_has_stack)
844     error ("No stack.");
845 
846   /* Name of the value returned by get_frame_pc().  Per comments, "pc"
847      is not a good name.  */
848   if (PC_REGNUM >= 0)
849     /* OK, this is weird.  The PC_REGNUM hardware register's value can
850        easily not match that of the internal value returned by
851        get_frame_pc().  */
852     pc_regname = REGISTER_NAME (PC_REGNUM);
853   else
854     /* But then, this is weird to.  Even without PC_REGNUM, an
855        architectures will often have a hardware register called "pc",
856        and that register's value, again, can easily not match
857        get_frame_pc().  */
858     pc_regname = "pc";
859 
860   fi = parse_frame_specification (addr_exp);
861   if (fi == NULL)
862     error ("Invalid frame specified.");
863 
864   find_frame_sal (fi, &sal);
865   func = get_frame_function (fi);
866   /* FIXME: cagney/2002-11-28: Why bother?  Won't sal.symtab contain
867      the same value.  */
868   s = find_pc_symtab (get_frame_pc (fi));
869   if (func)
870     {
871       /* I'd like to use SYMBOL_PRINT_NAME() here, to display
872        * the demangled name that we already have stored in
873        * the symbol table, but we stored a version with
874        * DMGL_PARAMS turned on, and here we don't want
875        * to display parameters. So call the demangler again,
876        * with DMGL_ANSI only. RT
877        * (Yes, I know that printf_symbol_filtered() will
878        * again try to demangle the name on the fly, but
879        * the issue is that if cplus_demangle() fails here,
880        * it'll fail there too. So we want to catch the failure
881        * ("demangled==NULL" case below) here, while we still
882        * have our hands on the function symbol.)
883        */
884       char *demangled;
885       funname = DEPRECATED_SYMBOL_NAME (func);
886       funlang = SYMBOL_LANGUAGE (func);
887       if (funlang == language_cplus)
888 	{
889 	  demangled = cplus_demangle (funname, DMGL_ANSI);
890 	  /* If the demangler fails, try the demangled name
891 	   * from the symbol table. This'll have parameters,
892 	   * but that's preferable to diplaying a mangled name.
893 	   */
894 	  if (demangled == NULL)
895 	    funname = SYMBOL_PRINT_NAME (func);
896 	}
897     }
898   else
899     {
900       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
901       if (msymbol != NULL)
902 	{
903 	  funname = DEPRECATED_SYMBOL_NAME (msymbol);
904 	  funlang = SYMBOL_LANGUAGE (msymbol);
905 	}
906     }
907   calling_frame_info = get_prev_frame (fi);
908 
909   if (!addr_exp && frame_relative_level (deprecated_selected_frame) >= 0)
910     {
911       printf_filtered ("Stack level %d, frame at ",
912 		       frame_relative_level (deprecated_selected_frame));
913       print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
914       printf_filtered (":\n");
915     }
916   else
917     {
918       printf_filtered ("Stack frame at ");
919       print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
920       printf_filtered (":\n");
921     }
922   printf_filtered (" %s = ", pc_regname);
923   print_address_numeric (get_frame_pc (fi), 1, gdb_stdout);
924 
925   wrap_here ("   ");
926   if (funname)
927     {
928       printf_filtered (" in ");
929       fprintf_symbol_filtered (gdb_stdout, funname, funlang,
930 			       DMGL_ANSI | DMGL_PARAMS);
931     }
932   wrap_here ("   ");
933   if (sal.symtab)
934     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
935   puts_filtered ("; ");
936   wrap_here ("    ");
937   printf_filtered ("saved %s ", pc_regname);
938   print_address_numeric (frame_pc_unwind (fi), 1, gdb_stdout);
939   printf_filtered ("\n");
940 
941   {
942     int frameless;
943     frameless = (DEPRECATED_FRAMELESS_FUNCTION_INVOCATION_P ()
944 		 && DEPRECATED_FRAMELESS_FUNCTION_INVOCATION (fi));
945     if (frameless)
946       printf_filtered (" (FRAMELESS),");
947   }
948 
949   if (calling_frame_info)
950     {
951       printf_filtered (" called by frame at ");
952       print_address_numeric (get_frame_base (calling_frame_info),
953 			     1, gdb_stdout);
954     }
955   if (get_next_frame (fi) && calling_frame_info)
956     puts_filtered (",");
957   wrap_here ("   ");
958   if (get_next_frame (fi))
959     {
960       printf_filtered (" caller of frame at ");
961       print_address_numeric (get_frame_base (get_next_frame (fi)), 1,
962 			     gdb_stdout);
963     }
964   if (get_next_frame (fi) || calling_frame_info)
965     puts_filtered ("\n");
966   if (s)
967     printf_filtered (" source language %s.\n",
968 		     language_str (s->language));
969 
970   {
971     /* Address of the argument list for this frame, or 0.  */
972     CORE_ADDR arg_list = get_frame_args_address (fi);
973     /* Number of args for this frame, or -1 if unknown.  */
974     int numargs;
975 
976     if (arg_list == 0)
977       printf_filtered (" Arglist at unknown address.\n");
978     else
979       {
980 	printf_filtered (" Arglist at ");
981 	print_address_numeric (arg_list, 1, gdb_stdout);
982 	printf_filtered (",");
983 
984 	if (!FRAME_NUM_ARGS_P ())
985 	  {
986 	    numargs = -1;
987 	    puts_filtered (" args: ");
988 	  }
989 	else
990 	  {
991 	    numargs = FRAME_NUM_ARGS (fi);
992 	    gdb_assert (numargs >= 0);
993 	    if (numargs == 0)
994 	      puts_filtered (" no args.");
995 	    else if (numargs == 1)
996 	      puts_filtered (" 1 arg: ");
997 	    else
998 	      printf_filtered (" %d args: ", numargs);
999 	  }
1000 	print_frame_args (func, fi, numargs, gdb_stdout);
1001 	puts_filtered ("\n");
1002       }
1003   }
1004   {
1005     /* Address of the local variables for this frame, or 0.  */
1006     CORE_ADDR arg_list = get_frame_locals_address (fi);
1007 
1008     if (arg_list == 0)
1009       printf_filtered (" Locals at unknown address,");
1010     else
1011       {
1012 	printf_filtered (" Locals at ");
1013 	print_address_numeric (arg_list, 1, gdb_stdout);
1014 	printf_filtered (",");
1015       }
1016   }
1017 
1018   if (DEPRECATED_FRAME_INIT_SAVED_REGS_P ()
1019       && deprecated_get_frame_saved_regs (fi) == NULL)
1020     DEPRECATED_FRAME_INIT_SAVED_REGS (fi);
1021   /* Print as much information as possible on the location of all the
1022      registers.  */
1023   {
1024     enum lval_type lval;
1025     int optimized;
1026     CORE_ADDR addr;
1027     int realnum;
1028     int count;
1029     int i;
1030     int need_nl = 1;
1031 
1032     /* The sp is special; what's displayed isn't the save address, but
1033        the value of the previous frame's sp.  This is a legacy thing,
1034        at one stage the frame cached the previous frame's SP instead
1035        of its address, hence it was easiest to just display the cached
1036        value.  */
1037     if (SP_REGNUM >= 0)
1038       {
1039 	/* Find out the location of the saved stack pointer with out
1040            actually evaluating it.  */
1041 	frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
1042 			       &realnum, NULL);
1043 	if (!optimized && lval == not_lval)
1044 	  {
1045 	    char value[MAX_REGISTER_SIZE];
1046 	    CORE_ADDR sp;
1047 	    frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
1048 				   &realnum, value);
1049 	    /* NOTE: cagney/2003-05-22: This is assuming that the
1050                stack pointer was packed as an unsigned integer.  That
1051                may or may not be valid.  */
1052 	    sp = extract_unsigned_integer (value, DEPRECATED_REGISTER_RAW_SIZE (SP_REGNUM));
1053 	    printf_filtered (" Previous frame's sp is ");
1054 	    print_address_numeric (sp, 1, gdb_stdout);
1055 	    printf_filtered ("\n");
1056 	    need_nl = 0;
1057 	  }
1058 	else if (!optimized && lval == lval_memory)
1059 	  {
1060 	    printf_filtered (" Previous frame's sp at ");
1061 	    print_address_numeric (addr, 1, gdb_stdout);
1062 	    printf_filtered ("\n");
1063 	    need_nl = 0;
1064 	  }
1065 	else if (!optimized && lval == lval_register)
1066 	  {
1067 	    printf_filtered (" Previous frame's sp in %s\n",
1068 			     REGISTER_NAME (realnum));
1069 	    need_nl = 0;
1070 	  }
1071 	/* else keep quiet.  */
1072       }
1073 
1074     count = 0;
1075     numregs = NUM_REGS + NUM_PSEUDO_REGS;
1076     for (i = 0; i < numregs; i++)
1077       if (i != SP_REGNUM
1078 	  && gdbarch_register_reggroup_p (current_gdbarch, i, all_reggroup))
1079 	{
1080 	  /* Find out the location of the saved register without
1081              fetching the corresponding value.  */
1082 	  frame_register_unwind (fi, i, &optimized, &lval, &addr, &realnum,
1083 				 NULL);
1084 	  /* For moment, only display registers that were saved on the
1085 	     stack.  */
1086 	  if (!optimized && lval == lval_memory)
1087 	    {
1088 	      if (count == 0)
1089 		puts_filtered (" Saved registers:\n ");
1090 	      else
1091 		puts_filtered (",");
1092 	      wrap_here (" ");
1093 	      printf_filtered (" %s at ", REGISTER_NAME (i));
1094 	      print_address_numeric (addr, 1, gdb_stdout);
1095 	      count++;
1096 	    }
1097 	}
1098     if (count || need_nl)
1099       puts_filtered ("\n");
1100   }
1101 }
1102 
1103 #if 0
1104 /* Set a limit on the number of frames printed by default in a
1105    backtrace.  */
1106 
1107 static int backtrace_limit;
1108 
1109 static void
1110 set_backtrace_limit_command (char *count_exp, int from_tty)
1111 {
1112   int count = parse_and_eval_long (count_exp);
1113 
1114   if (count < 0)
1115     error ("Negative argument not meaningful as backtrace limit.");
1116 
1117   backtrace_limit = count;
1118 }
1119 
1120 static void
1121 backtrace_limit_info (char *arg, int from_tty)
1122 {
1123   if (arg)
1124     error ("\"Info backtrace-limit\" takes no arguments.");
1125 
1126   printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
1127 }
1128 #endif
1129 
1130 /* Print briefly all stack frames or just the innermost COUNT frames.  */
1131 
1132 static void backtrace_command_1 (char *count_exp, int show_locals,
1133 				 int from_tty);
1134 static void
backtrace_command_1(char * count_exp,int show_locals,int from_tty)1135 backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
1136 {
1137   struct frame_info *fi;
1138   int count;
1139   int i;
1140   struct frame_info *trailing;
1141   int trailing_level;
1142 
1143   if (!target_has_stack)
1144     error ("No stack.");
1145 
1146   /* The following code must do two things.  First, it must
1147      set the variable TRAILING to the frame from which we should start
1148      printing.  Second, it must set the variable count to the number
1149      of frames which we should print, or -1 if all of them.  */
1150   trailing = get_current_frame ();
1151 
1152   /* The target can be in a state where there is no valid frames
1153      (e.g., just connected). */
1154   if (trailing == NULL)
1155     error ("No stack.");
1156 
1157   trailing_level = 0;
1158   if (count_exp)
1159     {
1160       count = parse_and_eval_long (count_exp);
1161       if (count < 0)
1162 	{
1163 	  struct frame_info *current;
1164 
1165 	  count = -count;
1166 
1167 	  current = trailing;
1168 	  while (current && count--)
1169 	    {
1170 	      QUIT;
1171 	      current = get_prev_frame (current);
1172 	    }
1173 
1174 	  /* Will stop when CURRENT reaches the top of the stack.  TRAILING
1175 	     will be COUNT below it.  */
1176 	  while (current)
1177 	    {
1178 	      QUIT;
1179 	      trailing = get_prev_frame (trailing);
1180 	      current = get_prev_frame (current);
1181 	      trailing_level++;
1182 	    }
1183 
1184 	  count = -1;
1185 	}
1186     }
1187   else
1188     count = -1;
1189 
1190   if (info_verbose)
1191     {
1192       struct partial_symtab *ps;
1193 
1194       /* Read in symbols for all of the frames.  Need to do this in
1195          a separate pass so that "Reading in symbols for xxx" messages
1196          don't screw up the appearance of the backtrace.  Also
1197          if people have strong opinions against reading symbols for
1198          backtrace this may have to be an option.  */
1199       i = count;
1200       for (fi = trailing;
1201 	   fi != NULL && i--;
1202 	   fi = get_prev_frame (fi))
1203 	{
1204 	  QUIT;
1205 	  ps = find_pc_psymtab (get_frame_address_in_block (fi));
1206 	  if (ps)
1207 	    PSYMTAB_TO_SYMTAB (ps);	/* Force syms to come in */
1208 	}
1209     }
1210 
1211   for (i = 0, fi = trailing;
1212        fi && count--;
1213        i++, fi = get_prev_frame (fi))
1214     {
1215       QUIT;
1216 
1217       /* Don't use print_stack_frame; if an error() occurs it probably
1218          means further attempts to backtrace would fail (on the other
1219          hand, perhaps the code does or could be fixed to make sure
1220          the frame->prev field gets set to NULL in that case).  */
1221       print_frame_info (fi, trailing_level + i, 0, 1);
1222       if (show_locals)
1223 	print_frame_local_vars (fi, 1, gdb_stdout);
1224     }
1225 
1226   /* If we've stopped before the end, mention that.  */
1227   if (fi && from_tty)
1228     printf_filtered ("(More stack frames follow...)\n");
1229 }
1230 
1231 struct backtrace_command_args
1232   {
1233     char *count_exp;
1234     int show_locals;
1235     int from_tty;
1236   };
1237 
1238 /* Stub to call backtrace_command_1 by way of an error catcher.  */
1239 static int
backtrace_command_stub(void * data)1240 backtrace_command_stub (void *data)
1241 {
1242   struct backtrace_command_args *args = (struct backtrace_command_args *)data;
1243   backtrace_command_1 (args->count_exp, args->show_locals, args->from_tty);
1244   return 0;
1245 }
1246 
1247 static void
backtrace_command(char * arg,int from_tty)1248 backtrace_command (char *arg, int from_tty)
1249 {
1250   struct cleanup *old_chain = (struct cleanup *) NULL;
1251   char **argv = (char **) NULL;
1252   int argIndicatingFullTrace = (-1), totArgLen = 0, argc = 0;
1253   char *argPtr = arg;
1254   struct backtrace_command_args btargs;
1255 
1256   if (arg != (char *) NULL)
1257     {
1258       int i;
1259 
1260       argv = buildargv (arg);
1261       old_chain = make_cleanup_freeargv (argv);
1262       argc = 0;
1263       for (i = 0; (argv[i] != (char *) NULL); i++)
1264 	{
1265 	  unsigned int j;
1266 
1267 	  for (j = 0; (j < strlen (argv[i])); j++)
1268 	    argv[i][j] = tolower (argv[i][j]);
1269 
1270 	  if (argIndicatingFullTrace < 0 && subset_compare (argv[i], "full"))
1271 	    argIndicatingFullTrace = argc;
1272 	  else
1273 	    {
1274 	      argc++;
1275 	      totArgLen += strlen (argv[i]);
1276 	    }
1277 	}
1278       totArgLen += argc;
1279       if (argIndicatingFullTrace >= 0)
1280 	{
1281 	  if (totArgLen > 0)
1282 	    {
1283 	      argPtr = (char *) xmalloc (totArgLen + 1);
1284 	      if (!argPtr)
1285 		nomem (0);
1286 	      else
1287 		{
1288 		  memset (argPtr, 0, totArgLen + 1);
1289 		  for (i = 0; (i < (argc + 1)); i++)
1290 		    {
1291 		      if (i != argIndicatingFullTrace)
1292 			{
1293 			  strcat (argPtr, argv[i]);
1294 			  strcat (argPtr, " ");
1295 			}
1296 		    }
1297 		}
1298 	    }
1299 	  else
1300 	    argPtr = (char *) NULL;
1301 	}
1302     }
1303 
1304   btargs.count_exp = argPtr;
1305   btargs.show_locals = (argIndicatingFullTrace >= 0);
1306   btargs.from_tty = from_tty;
1307   catch_errors (backtrace_command_stub, (char *)&btargs, "", RETURN_MASK_ERROR);
1308 
1309   if (argIndicatingFullTrace >= 0 && totArgLen > 0)
1310     xfree (argPtr);
1311 
1312   if (old_chain)
1313     do_cleanups (old_chain);
1314 }
1315 
1316 static void backtrace_full_command (char *arg, int from_tty);
1317 static void
backtrace_full_command(char * arg,int from_tty)1318 backtrace_full_command (char *arg, int from_tty)
1319 {
1320   struct backtrace_command_args btargs;
1321   btargs.count_exp = arg;
1322   btargs.show_locals = 1;
1323   btargs.from_tty = from_tty;
1324   catch_errors (backtrace_command_stub, (char *)&btargs, "", RETURN_MASK_ERROR);
1325 }
1326 
1327 
1328 /* Print the local variables of a block B active in FRAME.
1329    Return 1 if any variables were printed; 0 otherwise.  */
1330 
1331 static int
print_block_frame_locals(struct block * b,struct frame_info * fi,int num_tabs,struct ui_file * stream)1332 print_block_frame_locals (struct block *b, struct frame_info *fi,
1333 			  int num_tabs, struct ui_file *stream)
1334 {
1335   struct dict_iterator iter;
1336   int j;
1337   struct symbol *sym;
1338   int values_printed = 0;
1339 
1340   ALL_BLOCK_SYMBOLS (b, iter, sym)
1341     {
1342       switch (SYMBOL_CLASS (sym))
1343 	{
1344 	case LOC_LOCAL:
1345 	case LOC_REGISTER:
1346 	case LOC_STATIC:
1347 	case LOC_BASEREG:
1348 	case LOC_COMPUTED:
1349 	  values_printed = 1;
1350 	  for (j = 0; j < num_tabs; j++)
1351 	    fputs_filtered ("\t", stream);
1352 	  fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1353 	  fputs_filtered (" = ", stream);
1354 	  print_variable_value (sym, fi, stream);
1355 	  fprintf_filtered (stream, "\n");
1356 	  break;
1357 
1358 	default:
1359 	  /* Ignore symbols which are not locals.  */
1360 	  break;
1361 	}
1362     }
1363   return values_printed;
1364 }
1365 
1366 /* Same, but print labels.  */
1367 
1368 static int
print_block_frame_labels(struct block * b,int * have_default,struct ui_file * stream)1369 print_block_frame_labels (struct block *b, int *have_default,
1370 			  struct ui_file *stream)
1371 {
1372   struct dict_iterator iter;
1373   struct symbol *sym;
1374   int values_printed = 0;
1375 
1376   ALL_BLOCK_SYMBOLS (b, iter, sym)
1377     {
1378       if (DEPRECATED_STREQ (DEPRECATED_SYMBOL_NAME (sym), "default"))
1379 	{
1380 	  if (*have_default)
1381 	    continue;
1382 	  *have_default = 1;
1383 	}
1384       if (SYMBOL_CLASS (sym) == LOC_LABEL)
1385 	{
1386 	  struct symtab_and_line sal;
1387 	  sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1388 	  values_printed = 1;
1389 	  fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1390 	  if (addressprint)
1391 	    {
1392 	      fprintf_filtered (stream, " ");
1393 	      print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
1394 	    }
1395 	  fprintf_filtered (stream, " in file %s, line %d\n",
1396 			    sal.symtab->filename, sal.line);
1397 	}
1398     }
1399   return values_printed;
1400 }
1401 
1402 /* Print on STREAM all the local variables in frame FRAME,
1403    including all the blocks active in that frame
1404    at its current pc.
1405 
1406    Returns 1 if the job was done,
1407    or 0 if nothing was printed because we have no info
1408    on the function running in FRAME.  */
1409 
1410 static void
print_frame_local_vars(struct frame_info * fi,int num_tabs,struct ui_file * stream)1411 print_frame_local_vars (struct frame_info *fi, int num_tabs,
1412 			struct ui_file *stream)
1413 {
1414   struct block *block = get_frame_block (fi, 0);
1415   int values_printed = 0;
1416 
1417   if (block == 0)
1418     {
1419       fprintf_filtered (stream, "No symbol table info available.\n");
1420       return;
1421     }
1422 
1423   while (block != 0)
1424     {
1425       if (print_block_frame_locals (block, fi, num_tabs, stream))
1426 	values_printed = 1;
1427       /* After handling the function's top-level block, stop.
1428          Don't continue to its superblock, the block of
1429          per-file symbols.  */
1430       if (BLOCK_FUNCTION (block))
1431 	break;
1432       block = BLOCK_SUPERBLOCK (block);
1433     }
1434 
1435   if (!values_printed)
1436     {
1437       fprintf_filtered (stream, "No locals.\n");
1438     }
1439 }
1440 
1441 /* Same, but print labels.  */
1442 
1443 static void
print_frame_label_vars(struct frame_info * fi,int this_level_only,struct ui_file * stream)1444 print_frame_label_vars (struct frame_info *fi, int this_level_only,
1445 			struct ui_file *stream)
1446 {
1447   struct blockvector *bl;
1448   struct block *block = get_frame_block (fi, 0);
1449   int values_printed = 0;
1450   int index, have_default = 0;
1451   char *blocks_printed;
1452   CORE_ADDR pc = get_frame_pc (fi);
1453 
1454   if (block == 0)
1455     {
1456       fprintf_filtered (stream, "No symbol table info available.\n");
1457       return;
1458     }
1459 
1460   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1461   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1462   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1463 
1464   while (block != 0)
1465     {
1466       CORE_ADDR end = BLOCK_END (block) - 4;
1467       int last_index;
1468 
1469       if (bl != blockvector_for_pc (end, &index))
1470 	error ("blockvector blotch");
1471       if (BLOCKVECTOR_BLOCK (bl, index) != block)
1472 	error ("blockvector botch");
1473       last_index = BLOCKVECTOR_NBLOCKS (bl);
1474       index += 1;
1475 
1476       /* Don't print out blocks that have gone by.  */
1477       while (index < last_index
1478 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1479 	index++;
1480 
1481       while (index < last_index
1482 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1483 	{
1484 	  if (blocks_printed[index] == 0)
1485 	    {
1486 	      if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
1487 		values_printed = 1;
1488 	      blocks_printed[index] = 1;
1489 	    }
1490 	  index++;
1491 	}
1492       if (have_default)
1493 	return;
1494       if (values_printed && this_level_only)
1495 	return;
1496 
1497       /* After handling the function's top-level block, stop.
1498          Don't continue to its superblock, the block of
1499          per-file symbols.  */
1500       if (BLOCK_FUNCTION (block))
1501 	break;
1502       block = BLOCK_SUPERBLOCK (block);
1503     }
1504 
1505   if (!values_printed && !this_level_only)
1506     {
1507       fprintf_filtered (stream, "No catches.\n");
1508     }
1509 }
1510 
1511 void
locals_info(char * args,int from_tty)1512 locals_info (char *args, int from_tty)
1513 {
1514   if (!deprecated_selected_frame)
1515     error ("No frame selected.");
1516   print_frame_local_vars (deprecated_selected_frame, 0, gdb_stdout);
1517 }
1518 
1519 static void
catch_info(char * ignore,int from_tty)1520 catch_info (char *ignore, int from_tty)
1521 {
1522   struct symtab_and_line *sal;
1523 
1524   /* Check for target support for exception handling */
1525   sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
1526   if (sal)
1527     {
1528       /* Currently not handling this */
1529       /* Ideally, here we should interact with the C++ runtime
1530          system to find the list of active handlers, etc. */
1531       fprintf_filtered (gdb_stdout, "Info catch not supported with this target/compiler combination.\n");
1532 #if 0
1533       if (!deprecated_selected_frame)
1534 	error ("No frame selected.");
1535 #endif
1536     }
1537   else
1538     {
1539       /* Assume g++ compiled code -- old v 4.16 behaviour */
1540       if (!deprecated_selected_frame)
1541 	error ("No frame selected.");
1542 
1543       print_frame_label_vars (deprecated_selected_frame, 0, gdb_stdout);
1544     }
1545 }
1546 
1547 static void
print_frame_arg_vars(struct frame_info * fi,struct ui_file * stream)1548 print_frame_arg_vars (struct frame_info *fi,
1549 		      struct ui_file *stream)
1550 {
1551   struct symbol *func = get_frame_function (fi);
1552   struct block *b;
1553   struct dict_iterator iter;
1554   struct symbol *sym, *sym2;
1555   int values_printed = 0;
1556 
1557   if (func == 0)
1558     {
1559       fprintf_filtered (stream, "No symbol table info available.\n");
1560       return;
1561     }
1562 
1563   b = SYMBOL_BLOCK_VALUE (func);
1564   ALL_BLOCK_SYMBOLS (b, iter, sym)
1565     {
1566       switch (SYMBOL_CLASS (sym))
1567 	{
1568 	case LOC_ARG:
1569 	case LOC_LOCAL_ARG:
1570 	case LOC_REF_ARG:
1571 	case LOC_REGPARM:
1572 	case LOC_REGPARM_ADDR:
1573 	case LOC_BASEREG_ARG:
1574 	case LOC_COMPUTED_ARG:
1575 	  values_printed = 1;
1576 	  fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1577 	  fputs_filtered (" = ", stream);
1578 
1579 	  /* We have to look up the symbol because arguments can have
1580 	     two entries (one a parameter, one a local) and the one we
1581 	     want is the local, which lookup_symbol will find for us.
1582 	     This includes gcc1 (not gcc2) on the sparc when passing a
1583 	     small structure and gcc2 when the argument type is float
1584 	     and it is passed as a double and converted to float by
1585 	     the prologue (in the latter case the type of the LOC_ARG
1586 	     symbol is double and the type of the LOC_LOCAL symbol is
1587 	     float).  There are also LOC_ARG/LOC_REGISTER pairs which
1588 	     are not combined in symbol-reading.  */
1589 
1590 	  sym2 = lookup_symbol (DEPRECATED_SYMBOL_NAME (sym),
1591 		   b, VAR_DOMAIN, (int *) NULL, (struct symtab **) NULL);
1592 	  print_variable_value (sym2, fi, stream);
1593 	  fprintf_filtered (stream, "\n");
1594 	  break;
1595 
1596 	default:
1597 	  /* Don't worry about things which aren't arguments.  */
1598 	  break;
1599 	}
1600     }
1601   if (!values_printed)
1602     {
1603       fprintf_filtered (stream, "No arguments.\n");
1604     }
1605 }
1606 
1607 void
args_info(char * ignore,int from_tty)1608 args_info (char *ignore, int from_tty)
1609 {
1610   if (!deprecated_selected_frame)
1611     error ("No frame selected.");
1612   print_frame_arg_vars (deprecated_selected_frame, gdb_stdout);
1613 }
1614 
1615 
1616 static void
args_plus_locals_info(char * ignore,int from_tty)1617 args_plus_locals_info (char *ignore, int from_tty)
1618 {
1619   args_info (ignore, from_tty);
1620   locals_info (ignore, from_tty);
1621 }
1622 
1623 
1624 /* Select frame FI.  Also print the stack frame and show the source if
1625    this is the tui version.  */
1626 static void
select_and_print_frame(struct frame_info * fi)1627 select_and_print_frame (struct frame_info *fi)
1628 {
1629   select_frame (fi);
1630   if (fi)
1631     {
1632       print_stack_frame (fi, frame_relative_level (fi), 1);
1633     }
1634 }
1635 
1636 /* Return the symbol-block in which the selected frame is executing.
1637    Can return zero under various legitimate circumstances.
1638 
1639    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
1640    code address within the block returned.  We use this to decide
1641    which macros are in scope.  */
1642 
1643 struct block *
get_selected_block(CORE_ADDR * addr_in_block)1644 get_selected_block (CORE_ADDR *addr_in_block)
1645 {
1646   if (!target_has_stack)
1647     return 0;
1648 
1649   /* NOTE: cagney/2002-11-28: Why go to all this effort to not create
1650      a selected/current frame?  Perhaphs this function is called,
1651      indirectly, by WFI in "infrun.c" where avoiding the creation of
1652      an inner most frame is very important (it slows down single
1653      step).  I suspect, though that this was true in the deep dark
1654      past but is no longer the case.  A mindless look at all the
1655      callers tends to support this theory.  I think we should be able
1656      to assume that there is always a selcted frame.  */
1657   /* gdb_assert (deprecated_selected_frame != NULL); So, do you feel
1658      lucky? */
1659   if (!deprecated_selected_frame)
1660     {
1661       CORE_ADDR pc = read_pc ();
1662       if (addr_in_block != NULL)
1663 	*addr_in_block = pc;
1664       return block_for_pc (pc);
1665     }
1666   return get_frame_block (deprecated_selected_frame, addr_in_block);
1667 }
1668 
1669 /* Find a frame a certain number of levels away from FRAME.
1670    LEVEL_OFFSET_PTR points to an int containing the number of levels.
1671    Positive means go to earlier frames (up); negative, the reverse.
1672    The int that contains the number of levels is counted toward
1673    zero as the frames for those levels are found.
1674    If the top or bottom frame is reached, that frame is returned,
1675    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1676    how much farther the original request asked to go.  */
1677 
1678 struct frame_info *
find_relative_frame(struct frame_info * frame,int * level_offset_ptr)1679 find_relative_frame (struct frame_info *frame,
1680 		     int *level_offset_ptr)
1681 {
1682   struct frame_info *prev;
1683   struct frame_info *frame1;
1684 
1685   /* Going up is simple: just do get_prev_frame enough times
1686      or until initial frame is reached.  */
1687   while (*level_offset_ptr > 0)
1688     {
1689       prev = get_prev_frame (frame);
1690       if (prev == 0)
1691 	break;
1692       (*level_offset_ptr)--;
1693       frame = prev;
1694     }
1695   /* Going down is just as simple.  */
1696   if (*level_offset_ptr < 0)
1697     {
1698       while (*level_offset_ptr < 0)
1699 	{
1700 	  frame1 = get_next_frame (frame);
1701 	  if (!frame1)
1702 	    break;
1703 	  frame = frame1;
1704 	  (*level_offset_ptr)++;
1705 	}
1706     }
1707   return frame;
1708 }
1709 
1710 /* The "select_frame" command.  With no arg, NOP.
1711    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1712    valid level.  Otherwise, treat level_exp as an address expression
1713    and select it.  See parse_frame_specification for more info on proper
1714    frame expressions. */
1715 
1716 void
select_frame_command(char * level_exp,int from_tty)1717 select_frame_command (char *level_exp, int from_tty)
1718 {
1719   struct frame_info *frame;
1720   int level = frame_relative_level (deprecated_selected_frame);
1721 
1722   if (!target_has_stack)
1723     error ("No stack.");
1724 
1725   frame = parse_frame_specification (level_exp);
1726 
1727   select_frame (frame);
1728   if (level != frame_relative_level (deprecated_selected_frame))
1729     selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1730 }
1731 
1732 /* The "frame" command.  With no arg, print selected frame briefly.
1733    With arg, behaves like select_frame and then prints the selected
1734    frame.  */
1735 
1736 void
frame_command(char * level_exp,int from_tty)1737 frame_command (char *level_exp, int from_tty)
1738 {
1739   select_frame_command (level_exp, from_tty);
1740   print_stack_frame (deprecated_selected_frame,
1741 		     frame_relative_level (deprecated_selected_frame), 1);
1742 }
1743 
1744 /* The XDB Compatibility command to print the current frame. */
1745 
1746 static void
current_frame_command(char * level_exp,int from_tty)1747 current_frame_command (char *level_exp, int from_tty)
1748 {
1749   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1750     error ("No stack.");
1751   print_stack_frame (deprecated_selected_frame,
1752 			  frame_relative_level (deprecated_selected_frame), 1);
1753 }
1754 
1755 /* Select the frame up one or COUNT stack levels
1756    from the previously selected frame, and print it briefly.  */
1757 
1758 static void
up_silently_base(char * count_exp)1759 up_silently_base (char *count_exp)
1760 {
1761   struct frame_info *fi;
1762   int count = 1, count1;
1763   if (count_exp)
1764     count = parse_and_eval_long (count_exp);
1765   count1 = count;
1766 
1767   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1768     error ("No stack.");
1769 
1770   fi = find_relative_frame (deprecated_selected_frame, &count1);
1771   if (count1 != 0 && count_exp == 0)
1772     error ("Initial frame selected; you cannot go up.");
1773   select_frame (fi);
1774   selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1775 }
1776 
1777 static void
up_silently_command(char * count_exp,int from_tty)1778 up_silently_command (char *count_exp, int from_tty)
1779 {
1780   up_silently_base (count_exp);
1781 }
1782 
1783 static void
up_command(char * count_exp,int from_tty)1784 up_command (char *count_exp, int from_tty)
1785 {
1786   up_silently_base (count_exp);
1787   print_stack_frame (deprecated_selected_frame,
1788 		     frame_relative_level (deprecated_selected_frame), 1);
1789 }
1790 
1791 /* Select the frame down one or COUNT stack levels
1792    from the previously selected frame, and print it briefly.  */
1793 
1794 static void
down_silently_base(char * count_exp)1795 down_silently_base (char *count_exp)
1796 {
1797   struct frame_info *frame;
1798   int count = -1, count1;
1799   if (count_exp)
1800     count = -parse_and_eval_long (count_exp);
1801   count1 = count;
1802 
1803   if (target_has_stack == 0 || deprecated_selected_frame == 0)
1804     error ("No stack.");
1805 
1806   frame = find_relative_frame (deprecated_selected_frame, &count1);
1807   if (count1 != 0 && count_exp == 0)
1808     {
1809 
1810       /* We only do this if count_exp is not specified.  That way "down"
1811          means to really go down (and let me know if that is
1812          impossible), but "down 9999" can be used to mean go all the way
1813          down without getting an error.  */
1814 
1815       error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1816     }
1817 
1818   select_frame (frame);
1819   selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
1820 }
1821 
1822 static void
down_silently_command(char * count_exp,int from_tty)1823 down_silently_command (char *count_exp, int from_tty)
1824 {
1825   down_silently_base (count_exp);
1826 }
1827 
1828 static void
down_command(char * count_exp,int from_tty)1829 down_command (char *count_exp, int from_tty)
1830 {
1831   down_silently_base (count_exp);
1832   print_stack_frame (deprecated_selected_frame,
1833 		     frame_relative_level (deprecated_selected_frame), 1);
1834 }
1835 
1836 void
return_command(char * retval_exp,int from_tty)1837 return_command (char *retval_exp, int from_tty)
1838 {
1839   struct symbol *thisfun;
1840   struct value *return_value = NULL;
1841   const char *query_prefix = "";
1842 
1843   /* FIXME: cagney/2003-10-20: Perform a minimal existance test on the
1844      target.  If that fails, error out.  For the moment don't rely on
1845      get_selected_frame as it's error message is the the singularly
1846      obscure "No registers".  */
1847   if (!target_has_registers)
1848     error ("No selected frame.");
1849   thisfun = get_frame_function (get_selected_frame ());
1850 
1851   /* Compute the return value.  If the computation triggers an error,
1852      let it bail.  If the return type can't be handled, set
1853      RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
1854      message.  */
1855   if (retval_exp)
1856     {
1857       struct type *return_type = NULL;
1858 
1859       /* Compute the return value.  Should the computation fail, this
1860          call throws an error.  */
1861       return_value = parse_and_eval (retval_exp);
1862 
1863       /* Cast return value to the return type of the function.  Should
1864          the cast fail, this call throws an error.  */
1865       if (thisfun != NULL)
1866 	return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1867       if (return_type == NULL)
1868 	return_type = builtin_type_int;
1869       return_value = value_cast (return_type, return_value);
1870 
1871       /* Make sure the value is fully evaluated.  It may live in the
1872          stack frame we're about to pop.  */
1873       if (VALUE_LAZY (return_value))
1874 	value_fetch_lazy (return_value);
1875 
1876       if (TYPE_CODE (return_type) == TYPE_CODE_VOID)
1877 	/* If the return-type is "void", don't try to find the
1878            return-value's location.  However, do still evaluate the
1879            return expression so that, even when the expression result
1880            is discarded, side effects such as "return i++" still
1881            occure.  */
1882 	return_value = NULL;
1883       /* FIXME: cagney/2004-01-17: If the architecture implements both
1884          return_value and extract_returned_value_address, should allow
1885          "return" to work - don't set return_value to NULL.  */
1886       else if (!gdbarch_return_value_p (current_gdbarch)
1887 	       && (TYPE_CODE (return_type) == TYPE_CODE_STRUCT
1888 		   || TYPE_CODE (return_type) == TYPE_CODE_UNION))
1889 	{
1890 	  /* NOTE: cagney/2003-10-20: Compatibility hack for legacy
1891 	     code.  Old architectures don't expect STORE_RETURN_VALUE
1892 	     to be called with with a small struct that needs to be
1893 	     stored in registers.  Don't start doing it now.  */
1894 	  query_prefix = "\
1895 A structure or union return type is not supported by this architecture.\n\
1896 If you continue, the return value that you specified will be ignored.\n";
1897 	  return_value = NULL;
1898 	}
1899       else if (using_struct_return (return_type, 0))
1900 	{
1901 	  query_prefix = "\
1902 The location at which to store the function's return value is unknown.\n\
1903 If you continue, the return value that you specified will be ignored.\n";
1904 	  return_value = NULL;
1905 	}
1906     }
1907 
1908   /* Does an interactive user really want to do this?  Include
1909      information, such as how well GDB can handle the return value, in
1910      the query message.  */
1911   if (from_tty)
1912     {
1913       int confirmed;
1914       if (thisfun == NULL)
1915 	confirmed = query ("%sMake selected stack frame return now? ",
1916 			   query_prefix);
1917       else
1918 	confirmed = query ("%sMake %s return now? ", query_prefix,
1919 			   SYMBOL_PRINT_NAME (thisfun));
1920       if (!confirmed)
1921 	error ("Not confirmed");
1922     }
1923 
1924   /* NOTE: cagney/2003-01-18: Is this silly?  Rather than pop each
1925      frame in turn, should this code just go straight to the relevant
1926      frame and pop that?  */
1927 
1928   /* First discard all frames inner-to the selected frame (making the
1929      selected frame current).  */
1930   {
1931     struct frame_id selected_id = get_frame_id (get_selected_frame ());
1932     while (!frame_id_eq (selected_id, get_frame_id (get_current_frame ())))
1933       {
1934 	if (frame_id_inner (selected_id, get_frame_id (get_current_frame ())))
1935 	  /* Caught in the safety net, oops!  We've gone way past the
1936              selected frame.  */
1937 	  error ("Problem while popping stack frames (corrupt stack?)");
1938 	frame_pop (get_current_frame ());
1939       }
1940   }
1941 
1942   /* Second discard the selected frame (which is now also the current
1943      frame).  */
1944   frame_pop (get_current_frame ());
1945 
1946   /* Store RETURN_VAUE in the just-returned register set.  */
1947   if (return_value != NULL)
1948     {
1949       struct type *return_type = VALUE_TYPE (return_value);
1950       if (!gdbarch_return_value_p (current_gdbarch))
1951 	{
1952 	  STORE_RETURN_VALUE (return_type, current_regcache,
1953 			      VALUE_CONTENTS (return_value));
1954 	}
1955       /* FIXME: cagney/2004-01-17: If extract_returned_value_address
1956          is available and the function is using
1957          RETURN_VALUE_STRUCT_CONVENTION, should use it to find the
1958          address of the returned value so that it can be assigned.  */
1959       else
1960 	{
1961 	  gdb_assert (gdbarch_return_value (current_gdbarch, return_type,
1962 					    NULL, NULL, NULL)
1963 		      == RETURN_VALUE_REGISTER_CONVENTION);
1964 	  gdbarch_return_value (current_gdbarch, return_type,
1965 				current_regcache, NULL /*read*/,
1966 				VALUE_CONTENTS (return_value) /*write*/);
1967 	}
1968     }
1969 
1970   /* If we are at the end of a call dummy now, pop the dummy frame
1971      too.  */
1972   /* NOTE: cagney/2003-01-18: Is this silly?  Instead of popping all
1973      the frames in sequence, should this code just pop the dummy frame
1974      directly?  */
1975 #ifdef DEPRECATED_CALL_DUMMY_HAS_COMPLETED
1976   /* Since all up-to-date architectures return direct to the dummy
1977      breakpoint address, a dummy frame has, by definition, always
1978      completed.  Hence this method is no longer needed.  */
1979   if (DEPRECATED_CALL_DUMMY_HAS_COMPLETED (read_pc(), read_sp (),
1980 					   get_frame_base (get_current_frame ())))
1981     frame_pop (get_current_frame ());
1982 #else
1983   if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
1984     frame_pop (get_current_frame ());
1985 #endif
1986 
1987   /* If interactive, print the frame that is now current.  */
1988   if (from_tty)
1989     frame_command ("0", 1);
1990   else
1991     select_frame_command ("0", 0);
1992 }
1993 
1994 /* Sets the scope to input function name, provided that the
1995    function is within the current stack frame */
1996 
1997 struct function_bounds
1998 {
1999   CORE_ADDR low, high;
2000 };
2001 
2002 static void func_command (char *arg, int from_tty);
2003 static void
func_command(char * arg,int from_tty)2004 func_command (char *arg, int from_tty)
2005 {
2006   struct frame_info *fp;
2007   int found = 0;
2008   struct symtabs_and_lines sals;
2009   int i;
2010   int level = 1;
2011   struct function_bounds *func_bounds = (struct function_bounds *) NULL;
2012 
2013   if (arg != (char *) NULL)
2014     return;
2015 
2016   fp = parse_frame_specification ("0");
2017   sals = decode_line_spec (arg, 1);
2018   func_bounds = (struct function_bounds *) xmalloc (
2019 			      sizeof (struct function_bounds) * sals.nelts);
2020   for (i = 0; (i < sals.nelts && !found); i++)
2021     {
2022       if (sals.sals[i].pc == (CORE_ADDR) 0 ||
2023 	  find_pc_partial_function (sals.sals[i].pc,
2024 				    (char **) NULL,
2025 				    &func_bounds[i].low,
2026 				    &func_bounds[i].high) == 0)
2027 	{
2028 	  func_bounds[i].low =
2029 	    func_bounds[i].high = (CORE_ADDR) NULL;
2030 	}
2031     }
2032 
2033   do
2034     {
2035       for (i = 0; (i < sals.nelts && !found); i++)
2036 	found = (get_frame_pc (fp) >= func_bounds[i].low &&
2037 		 get_frame_pc (fp) < func_bounds[i].high);
2038       if (!found)
2039 	{
2040 	  level = 1;
2041 	  fp = find_relative_frame (fp, &level);
2042 	}
2043     }
2044   while (!found && level == 0);
2045 
2046   if (func_bounds)
2047     xfree (func_bounds);
2048 
2049   if (!found)
2050     printf_filtered ("'%s' not within current stack frame.\n", arg);
2051   else if (fp != deprecated_selected_frame)
2052     select_and_print_frame (fp);
2053 }
2054 
2055 /* Gets the language of the current frame.  */
2056 
2057 enum language
get_frame_language(void)2058 get_frame_language (void)
2059 {
2060   struct symtab *s;
2061   enum language flang;		/* The language of the current frame */
2062 
2063   if (deprecated_selected_frame)
2064     {
2065       /* We determine the current frame language by looking up its
2066          associated symtab.  To retrieve this symtab, we use the frame PC.
2067          However we cannot use the frame pc as is, because it usually points
2068          to the instruction following the "call", which is sometimes the first
2069          instruction of another function.  So we rely on
2070          get_frame_address_in_block(), it provides us with a PC which is
2071          guaranteed to be inside the frame's code block.  */
2072       s = find_pc_symtab (get_frame_address_in_block (deprecated_selected_frame));
2073       if (s)
2074 	flang = s->language;
2075       else
2076 	flang = language_unknown;
2077     }
2078   else
2079     flang = language_unknown;
2080 
2081   return flang;
2082 }
2083 
2084 void
_initialize_stack(void)2085 _initialize_stack (void)
2086 {
2087 #if 0
2088   backtrace_limit = 30;
2089 #endif
2090 
2091   add_com ("return", class_stack, return_command,
2092 	   "Make selected stack frame return to its caller.\n\
2093 Control remains in the debugger, but when you continue\n\
2094 execution will resume in the frame above the one now selected.\n\
2095 If an argument is given, it is an expression for the value to return.");
2096 
2097   add_com ("up", class_stack, up_command,
2098 	   "Select and print stack frame that called this one.\n\
2099 An argument says how many frames up to go.");
2100   add_com ("up-silently", class_support, up_silently_command,
2101 	   "Same as the `up' command, but does not print anything.\n\
2102 This is useful in command scripts.");
2103 
2104   add_com ("down", class_stack, down_command,
2105 	   "Select and print stack frame called by this one.\n\
2106 An argument says how many frames down to go.");
2107   add_com_alias ("do", "down", class_stack, 1);
2108   add_com_alias ("dow", "down", class_stack, 1);
2109   add_com ("down-silently", class_support, down_silently_command,
2110 	   "Same as the `down' command, but does not print anything.\n\
2111 This is useful in command scripts.");
2112 
2113   add_com ("frame", class_stack, frame_command,
2114 	   "Select and print a stack frame.\n\
2115 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
2116 An argument specifies the frame to select.\n\
2117 It can be a stack frame number or the address of the frame.\n\
2118 With argument, nothing is printed if input is coming from\n\
2119 a command file or a user-defined command.");
2120 
2121   add_com_alias ("f", "frame", class_stack, 1);
2122 
2123   if (xdb_commands)
2124     {
2125       add_com ("L", class_stack, current_frame_command,
2126 	       "Print the current stack frame.\n");
2127       add_com_alias ("V", "frame", class_stack, 1);
2128     }
2129   add_com ("select-frame", class_stack, select_frame_command,
2130 	   "Select a stack frame without printing anything.\n\
2131 An argument specifies the frame to select.\n\
2132 It can be a stack frame number or the address of the frame.\n");
2133 
2134   add_com ("backtrace", class_stack, backtrace_command,
2135 	   "Print backtrace of all stack frames, or innermost COUNT frames.\n\
2136 With a negative argument, print outermost -COUNT frames.\n\
2137 Use of the 'full' qualifier also prints the values of the local variables.\n");
2138   add_com_alias ("bt", "backtrace", class_stack, 0);
2139   if (xdb_commands)
2140     {
2141       add_com_alias ("t", "backtrace", class_stack, 0);
2142       add_com ("T", class_stack, backtrace_full_command,
2143 	       "Print backtrace of all stack frames, or innermost COUNT frames \n\
2144 and the values of the local variables.\n\
2145 With a negative argument, print outermost -COUNT frames.\n\
2146 Usage: T <count>\n");
2147     }
2148 
2149   add_com_alias ("where", "backtrace", class_alias, 0);
2150   add_info ("stack", backtrace_command,
2151 	    "Backtrace of the stack, or innermost COUNT frames.");
2152   add_info_alias ("s", "stack", 1);
2153   add_info ("frame", frame_info,
2154 	    "All about selected stack frame, or frame at ADDR.");
2155   add_info_alias ("f", "frame", 1);
2156   add_info ("locals", locals_info,
2157 	    "Local variables of current stack frame.");
2158   add_info ("args", args_info,
2159 	    "Argument variables of current stack frame.");
2160   if (xdb_commands)
2161     add_com ("l", class_info, args_plus_locals_info,
2162 	     "Argument and local variables of current stack frame.");
2163 
2164   if (dbx_commands)
2165     add_com ("func", class_stack, func_command,
2166       "Select the stack frame that contains <func>.\nUsage: func <name>\n");
2167 
2168   add_info ("catch", catch_info,
2169 	    "Exceptions that can be caught in the current stack frame.");
2170 
2171 #if 0
2172   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
2173   "Specify maximum number of frames for \"backtrace\" to print by default.",
2174 	   &setlist);
2175   add_info ("backtrace-limit", backtrace_limit_info,
2176      "The maximum number of frames for \"backtrace\" to print by default.");
2177 #endif
2178 }
2179