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