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