OSDN Git Service

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