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