* stabsread.c (rs6000_builtin_type): Make logical types be
[external/binutils.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "language.h"
26 #include "frame.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "breakpoint.h"
31 #include "demangle.h"
32 #include "inferior.h"
33
34 static void
35 return_command PARAMS ((char *, int));
36
37 static void
38 down_command PARAMS ((char *, int));
39
40 static void
41 down_silently_command PARAMS ((char *, int));
42
43 static void
44 up_command PARAMS ((char *, int));
45
46 static void
47 up_silently_command PARAMS ((char *, int));
48
49 static void
50 frame_command PARAMS ((char *, int));
51
52 static void
53 select_frame_command PARAMS ((char *, int));
54
55 static void
56 args_info PARAMS ((char *, int));
57
58 static void
59 print_frame_arg_vars PARAMS ((FRAME, FILE *));
60
61 static void
62 catch_info PARAMS ((char *, int));
63
64 static void
65 locals_info PARAMS ((char *, int));
66
67 static void
68 print_frame_label_vars PARAMS ((FRAME, int, FILE *));
69
70 static void
71 print_frame_local_vars PARAMS ((FRAME, FILE *));
72
73 static int
74 print_block_frame_labels PARAMS ((struct block *, int *, FILE *));
75
76 static int
77 print_block_frame_locals PARAMS ((struct block *, FRAME, FILE *));
78
79 static void
80 backtrace_command PARAMS ((char *, int));
81
82 static FRAME
83 parse_frame_specification PARAMS ((char *));
84
85 static void
86 frame_info PARAMS ((char *, int));
87
88
89 extern int addressprint;        /* Print addresses, or stay symbolic only? */
90 extern int info_verbose;        /* Verbosity of symbol reading msgs */
91 extern int lines_to_list;       /* # of lines "list" command shows by default */
92
93 /* The "selected" stack frame is used by default for local and arg access.
94    May be zero, for no selected frame.  */
95
96 FRAME selected_frame;
97
98 /* Level of the selected frame:
99    0 for innermost, 1 for its caller, ...
100    or -1 for frame specified by address with no defined level.  */
101
102 int selected_frame_level;
103
104 /* Nonzero means print the full filename and linenumber
105    when a frame is printed, and do so in a format programs can parse.  */
106
107 int frame_file_full_name = 0;
108
109 \f
110 /* Print a stack frame briefly.  FRAME should be the frame id
111    and LEVEL should be its level in the stack (or -1 for level not defined).
112    This prints the level, the function executing, the arguments,
113    and the file name and line number.
114    If the pc is not at the beginning of the source line,
115    the actual pc is printed at the beginning.
116
117    If SOURCE is 1, print the source line as well.
118    If SOURCE is -1, print ONLY the source line.  */
119
120 void
121 print_stack_frame (frame, level, source)
122      FRAME frame;
123      int level;
124      int source;
125 {
126   struct frame_info *fi;
127
128   fi = get_frame_info (frame);
129
130   print_frame_info (fi, level, source, 1);
131 }
132
133 struct print_args_args {
134   struct symbol *func;
135   struct frame_info *fi;
136 };
137
138 static int print_args_stub PARAMS ((char *));
139
140 /* Pass the args the way catch_errors wants them.  */
141 static int
142 print_args_stub (args)
143      char *args;
144 {
145   int numargs;
146   struct print_args_args *p = (struct print_args_args *)args;
147   FRAME_NUM_ARGS (numargs, (p->fi));
148   print_frame_args (p->func, p->fi, numargs, stdout);
149   return 0;
150 }
151
152 void
153 print_frame_info (fi, level, source, args)
154      struct frame_info *fi;
155      register int level;
156      int source;
157      int args;
158 {
159   struct symtab_and_line sal;
160   struct symbol *func;
161   register char *funname = 0;
162   enum language funlang = language_unknown;
163   int numargs;
164
165   if (PC_IN_CALL_DUMMY (fi->pc, read_sp (), fi->frame))
166     {
167       /* Do this regardless of SOURCE because we don't have any source
168          to list for this frame.  */
169       if (level >= 0)
170         printf_filtered ("#%-2d ", level);
171       printf_filtered ("<function called from gdb>\n");
172       return;
173     }
174   if (fi->signal_handler_caller)
175     {
176       /* Do this regardless of SOURCE because we don't have any source
177          to list for this frame.  */
178       if (level >= 0)
179         printf_filtered ("#%-2d ", level);
180       printf_filtered ("<signal handler called>\n");
181       return;
182     }
183
184   sal = find_pc_line (fi->pc, fi->next != NULL);
185   func = find_pc_function (fi->pc);
186   if (func)
187     {
188       /* In certain pathological cases, the symtabs give the wrong
189          function (when we are in the first function in a file which
190          is compiled without debugging symbols, the previous function
191          is compiled with debugging symbols, and the "foo.o" symbol
192          that is supposed to tell us where the file with debugging symbols
193          ends has been truncated by ar because it is longer than 15
194          characters).
195
196          So look in the minimal symbol tables as well, and if it comes
197          up with a larger address for the function use that instead.
198          I don't think this can ever cause any problems; there shouldn't
199          be any minimal symbols in the middle of a function.
200          FIXME:  (Not necessarily true.  What about text labels) */
201
202       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
203       if (msymbol != NULL
204           && (SYMBOL_VALUE_ADDRESS (msymbol) 
205               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
206         {
207           /* In this case we have no way of knowing the source file
208              and line number, so don't print them.  */
209           sal.symtab = 0;
210           /* We also don't know anything about the function besides
211              its address and name.  */
212           func = 0;
213           funname = SYMBOL_NAME (msymbol);
214           funlang = SYMBOL_LANGUAGE (msymbol);
215         }
216       else
217         {
218           funname = SYMBOL_NAME (func);
219           funlang = SYMBOL_LANGUAGE (func);
220         }
221     }
222   else
223     {
224       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
225       if (msymbol != NULL)
226         {
227           funname = SYMBOL_NAME (msymbol);
228           funlang = SYMBOL_LANGUAGE (msymbol);
229         }
230     }
231
232   if (source >= 0 || !sal.symtab)
233     {
234       if (level >= 0)
235         printf_filtered ("#%-2d ", level);
236       if (addressprint)
237         if (fi->pc != sal.pc || !sal.symtab)
238           printf_filtered ("%s in ", local_hex_string(fi->pc));
239       fprintf_symbol_filtered (stdout, funname ? funname : "??", funlang,
240                                DMGL_NO_OPTS);
241       wrap_here ("   ");
242       fputs_filtered (" (", stdout);
243       if (args)
244         {
245           struct print_args_args args;
246           args.fi = fi;
247           args.func = func;
248           catch_errors (print_args_stub, (char *)&args, "", RETURN_MASK_ERROR);
249         }
250       printf_filtered (")");
251       if (sal.symtab && sal.symtab->filename)
252         {
253           wrap_here ("   ");
254           printf_filtered (" at %s:%d", sal.symtab->filename, sal.line);
255         }
256
257 #ifdef PC_LOAD_SEGMENT
258      /* If we couldn't print out function name but if can figure out what
259         load segment this pc value is from, at least print out some info
260         about its load segment. */
261       if (!funname) {
262         wrap_here ("  ");
263         printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
264       }
265 #endif
266       printf_filtered ("\n");
267     }
268
269   if ((source != 0) && sal.symtab)
270     {
271       int done = 0;
272       int mid_statement = source < 0 && fi->pc != sal.pc;
273       if (frame_file_full_name)
274         done = identify_source_line (sal.symtab, sal.line, mid_statement,
275                                      fi->pc);
276       if (!done)
277         {
278           if (addressprint && mid_statement)
279             printf_filtered ("%s\t", local_hex_string(fi->pc));
280           print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
281         }
282       current_source_line = max (sal.line - lines_to_list/2, 1);
283     }
284   if (source != 0)
285     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
286
287   fflush (stdout);
288 }
289
290 /*
291  * Read a frame specification in whatever the appropriate format is.
292  * Call error() if the specification is in any way invalid (i.e.
293  * this function never returns NULL).
294  */
295 static FRAME
296 parse_frame_specification (frame_exp)
297      char *frame_exp;
298 {
299   int numargs = 0;
300   int arg1, arg2, arg3;
301 #define MAXARGS 4
302   int args[MAXARGS];
303   
304   if (frame_exp)
305     {
306       char *addr_string, *p;
307       struct cleanup *tmp_cleanup;
308
309       while (*frame_exp == ' ') frame_exp++;
310
311       while (*frame_exp)
312         {
313           if (numargs > MAXARGS)
314             error ("Too many args in frame specification");
315           /* Parse an argument.  */
316           for (p = frame_exp; *p && *p != ' '; p++)
317             ;
318           addr_string = savestring(frame_exp, p - frame_exp);
319
320           {
321             tmp_cleanup = make_cleanup (free, addr_string);
322             args[numargs++] = parse_and_eval_address (addr_string);
323             do_cleanups (tmp_cleanup);
324           }
325
326           /* Skip spaces, move to possible next arg.  */
327           while (*p == ' ') p++;
328           frame_exp = p;
329         }
330     }
331
332   switch (numargs)
333     {
334     case 0:
335       if (selected_frame == NULL)
336         error ("No selected frame.");
337       return selected_frame;
338       /* NOTREACHED */
339     case 1:
340       {
341         int level = args[0];
342         FRAME fid = find_relative_frame (get_current_frame (), &level);
343         FRAME tfid;
344
345         if (level == 0)
346           /* find_relative_frame was successful */
347           return fid;
348
349         /* If (s)he specifies the frame with an address, he deserves what
350            (s)he gets.  Still, give the highest one that matches.  */
351
352         for (fid = get_current_frame ();
353              fid && FRAME_FP (fid) != args[0];
354              fid = get_prev_frame (fid))
355           ;
356
357         if (fid)
358           while ((tfid = get_prev_frame (fid)) &&
359                  (FRAME_FP (tfid) == args[0]))
360             fid = tfid;
361           
362         /* We couldn't identify the frame as an existing frame, but
363            perhaps we can create one with a single argument.
364            Fall through to default case; it's up to SETUP_ARBITRARY_FRAME
365            to complain if it doesn't like a single arg.  */
366       }
367
368      default:
369 #ifdef SETUP_ARBITRARY_FRAME
370       return SETUP_ARBITRARY_FRAME (numargs, args);
371 #else
372       /* Usual case.  Do it here rather than have everyone supply
373          a SETUP_ARBITRARY_FRAME that does this.  */
374       if (numargs == 1)
375         return create_new_frame (args[0], 0);
376       error ("Too many args in frame specification");
377 #endif
378       /* NOTREACHED */
379     }
380   /* NOTREACHED */
381 }
382
383 /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
384    that if it is unsure about the answer, it returns 0
385    instead of guessing (this happens on the VAX and i960, for example).
386
387    On most machines, we never have to guess about the args address,
388    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
389 #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
390 #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
391 #endif
392
393 /* Print verbosely the selected frame or the frame at address ADDR.
394    This means absolutely all information in the frame is printed.  */
395
396 static void
397 frame_info (addr_exp, from_tty)
398      char *addr_exp;
399      int from_tty;
400 {
401   FRAME frame;
402   struct frame_info *fi;
403   struct frame_saved_regs fsr;
404   struct symtab_and_line sal;
405   struct symbol *func;
406   struct symtab *s;
407   FRAME calling_frame;
408   int i, count;
409   char *funname = 0;
410   enum language funlang = language_unknown;
411
412   if (!target_has_stack)
413     error ("No stack.");
414
415   frame = parse_frame_specification (addr_exp);
416   if (!frame)
417     error ("Invalid frame specified.");
418
419   fi = get_frame_info (frame);
420   sal = find_pc_line (fi->pc, fi->next != NULL);
421   func = get_frame_function (frame);
422   s = find_pc_symtab(fi->pc);
423   if (func)
424     {
425       funname = SYMBOL_NAME (func);
426       funlang = SYMBOL_LANGUAGE (func);
427     }
428   else
429     {
430       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
431       if (msymbol != NULL)
432         {
433           funname = SYMBOL_NAME (msymbol);
434           funlang = SYMBOL_LANGUAGE (msymbol);
435         }
436     }
437   calling_frame = get_prev_frame (frame);
438
439   if (!addr_exp && selected_frame_level >= 0) {
440     printf_filtered ("Stack level %d, frame at %s:\n",
441                      selected_frame_level, 
442                      local_hex_string(FRAME_FP(frame)));
443   } else {
444     printf_filtered ("Stack frame at %s:\n",
445                      local_hex_string(FRAME_FP(frame)));
446   }
447   printf_filtered (" %s = %s",
448                    reg_names[PC_REGNUM], 
449                    local_hex_string(fi->pc));
450
451   wrap_here ("   ");
452   if (funname)
453     {
454       printf_filtered (" in ");
455       fprintf_symbol_filtered (stdout, funname, funlang,
456                                DMGL_ANSI | DMGL_PARAMS);
457     }
458   wrap_here ("   ");
459   if (sal.symtab)
460     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
461   puts_filtered ("; ");
462   wrap_here ("    ");
463   printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM],
464                    local_hex_string(FRAME_SAVED_PC (frame)));
465
466   {
467     int frameless = 0;
468 #ifdef FRAMELESS_FUNCTION_INVOCATION
469     FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
470 #endif
471     if (frameless)
472       printf_filtered (" (FRAMELESS),");
473   }
474
475   if (calling_frame)
476     printf_filtered (" called by frame at %s", 
477                      local_hex_string(FRAME_FP (calling_frame)));
478   if (fi->next && calling_frame)
479     puts_filtered (",");
480   wrap_here ("   ");
481   if (fi->next)
482     printf_filtered (" caller of frame at %s",
483                      local_hex_string (fi->next->frame));
484   if (fi->next || calling_frame)
485     puts_filtered ("\n");
486   if (s)
487      printf_filtered(" source language %s.\n", language_str(s->language));
488
489 #ifdef PRINT_EXTRA_FRAME_INFO
490   PRINT_EXTRA_FRAME_INFO (fi);
491 #endif
492
493   {
494     /* Address of the argument list for this frame, or 0.  */
495     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
496     /* Number of args for this frame, or -1 if unknown.  */
497     int numargs;
498
499     if (arg_list == 0)
500         printf_filtered (" Arglist at unknown address.\n");
501     else
502       {
503         printf_filtered (" Arglist at %s,", local_hex_string(arg_list));
504
505         FRAME_NUM_ARGS (numargs, fi);
506         if (numargs < 0)
507           puts_filtered (" args: ");
508         else if (numargs == 0)
509           puts_filtered (" no args.");
510         else if (numargs == 1)
511           puts_filtered (" 1 arg: ");
512         else
513           printf_filtered (" %d args: ", numargs);
514         print_frame_args (func, fi, numargs, stdout);
515         puts_filtered ("\n");
516       }
517   }
518   {
519     /* Address of the local variables for this frame, or 0.  */
520     CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
521
522     if (arg_list == 0)
523         printf_filtered (" Locals at unknown address,");
524     else
525         printf_filtered (" Locals at %s,", local_hex_string(arg_list));
526   }
527
528 #if defined (FRAME_FIND_SAVED_REGS)  
529   get_frame_saved_regs (fi, &fsr);
530   /* The sp is special; what's returned isn't the save address, but
531      actually the value of the previous frame's sp.  */
532   printf_filtered (" Previous frame's sp is %s\n", 
533                    local_hex_string(fsr.regs[SP_REGNUM]));
534   count = 0;
535   for (i = 0; i < NUM_REGS; i++)
536     if (fsr.regs[i] && i != SP_REGNUM)
537       {
538         if (count == 0)
539           puts_filtered (" Saved registers:\n ");
540         else
541           puts_filtered (",");
542         wrap_here (" ");
543         printf_filtered (" %s at %s", reg_names[i], 
544                          local_hex_string(fsr.regs[i]));
545         count++;
546       }
547   if (count)
548     puts_filtered ("\n");
549 #endif /* Have FRAME_FIND_SAVED_REGS.  */
550 }
551
552 #if 0
553 /* Set a limit on the number of frames printed by default in a
554    backtrace.  */
555
556 static int backtrace_limit;
557
558 static void
559 set_backtrace_limit_command (count_exp, from_tty)
560      char *count_exp;
561      int from_tty;
562 {
563   int count = parse_and_eval_address (count_exp);
564
565   if (count < 0)
566     error ("Negative argument not meaningful as backtrace limit.");
567
568   backtrace_limit = count;
569 }
570
571 static void
572 backtrace_limit_info (arg, from_tty)
573      char *arg;
574      int from_tty;
575 {
576   if (arg)
577     error ("\"Info backtrace-limit\" takes no arguments.");
578
579   printf ("Backtrace limit: %d.\n", backtrace_limit);
580 }
581 #endif
582
583 /* Print briefly all stack frames or just the innermost COUNT frames.  */
584
585 static void
586 backtrace_command (count_exp, from_tty)
587      char *count_exp;
588      int from_tty;
589 {
590   struct frame_info *fi;
591   register int count;
592   register FRAME frame;
593   register int i;
594   register FRAME trailing;
595   register int trailing_level;
596
597   if (!target_has_stack)
598     error ("No stack.");
599
600   /* The following code must do two things.  First, it must
601      set the variable TRAILING to the frame from which we should start
602      printing.  Second, it must set the variable count to the number
603      of frames which we should print, or -1 if all of them.  */
604   trailing = get_current_frame ();
605   trailing_level = 0;
606   if (count_exp)
607     {
608       count = parse_and_eval_address (count_exp);
609       if (count < 0)
610         {
611           FRAME current;
612
613           count = -count;
614
615           current = trailing;
616           while (current && count--)
617             {
618               QUIT;
619               current = get_prev_frame (current);
620             }
621           
622           /* Will stop when CURRENT reaches the top of the stack.  TRAILING
623              will be COUNT below it.  */
624           while (current)
625             {
626               QUIT;
627               trailing = get_prev_frame (trailing);
628               current = get_prev_frame (current);
629               trailing_level++;
630             }
631           
632           count = -1;
633         }
634     }
635   else
636     count = -1;
637
638   if (info_verbose)
639     {
640       struct partial_symtab *ps;
641       
642       /* Read in symbols for all of the frames.  Need to do this in
643          a separate pass so that "Reading in symbols for xxx" messages
644          don't screw up the appearance of the backtrace.  Also
645          if people have strong opinions against reading symbols for
646          backtrace this may have to be an option.  */
647       i = count;
648       for (frame = trailing;
649            frame != NULL && i--;
650            frame = get_prev_frame (frame))
651         {
652           QUIT;
653           fi = get_frame_info (frame);
654           ps = find_pc_psymtab (fi->pc);
655           if (ps)
656             PSYMTAB_TO_SYMTAB (ps);     /* Force syms to come in */
657         }
658     }
659
660   for (i = 0, frame = trailing;
661        frame && count--;
662        i++, frame = get_prev_frame (frame))
663     {
664       QUIT;
665       fi = get_frame_info (frame);
666       print_frame_info (fi, trailing_level + i, 0, 1);
667     }
668
669   /* If we've stopped before the end, mention that.  */
670   if (frame && from_tty)
671     printf_filtered ("(More stack frames follow...)\n");
672 }
673 \f
674 /* Print the local variables of a block B active in FRAME.
675    Return 1 if any variables were printed; 0 otherwise.  */
676
677 static int
678 print_block_frame_locals (b, frame, stream)
679      struct block *b;
680      register FRAME frame;
681      register FILE *stream;
682 {
683   int nsyms;
684   register int i;
685   register struct symbol *sym;
686   register int values_printed = 0;
687
688   nsyms = BLOCK_NSYMS (b);
689
690   for (i = 0; i < nsyms; i++)
691     {
692       sym = BLOCK_SYM (b, i);
693       if (SYMBOL_CLASS (sym) == LOC_LOCAL
694           || SYMBOL_CLASS (sym) == LOC_REGISTER
695           || SYMBOL_CLASS (sym) == LOC_STATIC)
696         {
697           values_printed = 1;
698           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
699           fputs_filtered (" = ", stream);
700           print_variable_value (sym, frame, stream);
701           fprintf_filtered (stream, "\n");
702         }
703     }
704   return values_printed;
705 }
706
707 /* Same, but print labels.  */
708
709 static int
710 print_block_frame_labels (b, have_default, stream)
711      struct block *b;
712      int *have_default;
713      register FILE *stream;
714 {
715   int nsyms;
716   register int i;
717   register struct symbol *sym;
718   register int values_printed = 0;
719
720   nsyms = BLOCK_NSYMS (b);
721
722   for (i = 0; i < nsyms; i++)
723     {
724       sym = BLOCK_SYM (b, i);
725       if (STREQ (SYMBOL_NAME (sym), "default"))
726         {
727           if (*have_default)
728             continue;
729           *have_default = 1;
730         }
731       if (SYMBOL_CLASS (sym) == LOC_LABEL)
732         {
733           struct symtab_and_line sal;
734           sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
735           values_printed = 1;
736           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
737           if (addressprint)
738             fprintf_filtered (stream, " %s", 
739                               local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
740           fprintf_filtered (stream, " in file %s, line %d\n",
741                             sal.symtab->filename, sal.line);
742         }
743     }
744   return values_printed;
745 }
746
747 /* Print on STREAM all the local variables in frame FRAME,
748    including all the blocks active in that frame
749    at its current pc.
750
751    Returns 1 if the job was done,
752    or 0 if nothing was printed because we have no info
753    on the function running in FRAME.  */
754
755 static void
756 print_frame_local_vars (frame, stream)
757      register FRAME frame;
758      register FILE *stream;
759 {
760   register struct block *block = get_frame_block (frame);
761   register int values_printed = 0;
762
763   if (block == 0)
764     {
765       fprintf_filtered (stream, "No symbol table info available.\n");
766       return;
767     }
768   
769   while (block != 0)
770     {
771       if (print_block_frame_locals (block, frame, stream))
772         values_printed = 1;
773       /* After handling the function's top-level block, stop.
774          Don't continue to its superblock, the block of
775          per-file symbols.  */
776       if (BLOCK_FUNCTION (block))
777         break;
778       block = BLOCK_SUPERBLOCK (block);
779     }
780
781   if (!values_printed)
782     {
783       fprintf_filtered (stream, "No locals.\n");
784     }
785 }
786
787 /* Same, but print labels.  */
788
789 static void
790 print_frame_label_vars (frame, this_level_only, stream)
791      register FRAME frame;
792      int this_level_only;
793      register FILE *stream;
794 {
795   register struct blockvector *bl;
796   register struct block *block = get_frame_block (frame);
797   register int values_printed = 0;
798   int index, have_default = 0;
799   char *blocks_printed;
800   struct frame_info *fi = get_frame_info (frame);
801   CORE_ADDR pc = fi->pc;
802
803   if (block == 0)
804     {
805       fprintf_filtered (stream, "No symbol table info available.\n");
806       return;
807     }
808
809   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
810   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
811   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
812
813   while (block != 0)
814     {
815       CORE_ADDR end = BLOCK_END (block) - 4;
816       int last_index;
817
818       if (bl != blockvector_for_pc (end, &index))
819         error ("blockvector blotch");
820       if (BLOCKVECTOR_BLOCK (bl, index) != block)
821         error ("blockvector botch");
822       last_index = BLOCKVECTOR_NBLOCKS (bl);
823       index += 1;
824
825       /* Don't print out blocks that have gone by.  */
826       while (index < last_index
827              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
828         index++;
829
830       while (index < last_index
831              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
832         {
833           if (blocks_printed[index] == 0)
834             {
835               if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
836                 values_printed = 1;
837               blocks_printed[index] = 1;
838             }
839           index++;
840         }
841       if (have_default)
842         return;
843       if (values_printed && this_level_only)
844         return;
845
846       /* After handling the function's top-level block, stop.
847          Don't continue to its superblock, the block of
848          per-file symbols.  */
849       if (BLOCK_FUNCTION (block))
850         break;
851       block = BLOCK_SUPERBLOCK (block);
852     }
853
854   if (!values_printed && !this_level_only)
855     {
856       fprintf_filtered (stream, "No catches.\n");
857     }
858 }
859
860 /* ARGSUSED */
861 static void
862 locals_info (args, from_tty)
863      char *args;
864      int from_tty;
865 {
866   if (!selected_frame)
867     error ("No frame selected.");
868   print_frame_local_vars (selected_frame, stdout);
869 }
870
871 static void
872 catch_info (ignore, from_tty)
873      char *ignore;
874      int from_tty;
875 {
876   if (!selected_frame)
877     error ("No frame selected.");
878   print_frame_label_vars (selected_frame, 0, stdout);
879 }
880
881 static void
882 print_frame_arg_vars (frame, stream)
883      register FRAME frame;
884      register FILE *stream;
885 {
886   struct symbol *func = get_frame_function (frame);
887   register struct block *b;
888   int nsyms;
889   register int i;
890   register struct symbol *sym, *sym2;
891   register int values_printed = 0;
892
893   if (func == 0)
894     {
895       fprintf_filtered (stream, "No symbol table info available.\n");
896       return;
897     }
898
899   b = SYMBOL_BLOCK_VALUE (func);
900   nsyms = BLOCK_NSYMS (b);
901
902   for (i = 0; i < nsyms; i++)
903     {
904       sym = BLOCK_SYM (b, i);
905       if (SYMBOL_CLASS (sym) == LOC_ARG
906           || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
907           || SYMBOL_CLASS (sym) == LOC_REF_ARG
908           || SYMBOL_CLASS (sym) == LOC_REGPARM
909           || SYMBOL_CLASS (sym) == LOC_REGPARM_ADDR)
910         {
911           values_printed = 1;
912           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
913           fputs_filtered (" = ", stream);
914
915           /* We have to look up the symbol because arguments can have
916              two entries (one a parameter, one a local) and the one we
917              want is the local, which lookup_symbol will find for us.
918              This includes gcc1 (not gcc2) on the sparc when passing a
919              small structure and gcc2 when the argument type is float
920              and it is passed as a double and converted to float by
921              the prologue (in the latter case the type of the LOC_ARG
922              symbol is double and the type of the LOC_LOCAL symbol is
923              float).  There are also LOC_ARG/LOC_REGISTER pairs which
924              are not combined in symbol-reading.  */
925
926           sym2 = lookup_symbol (SYMBOL_NAME (sym),
927                         b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
928           print_variable_value (sym2, frame, stream);
929           fprintf_filtered (stream, "\n");
930         }
931     }
932
933   if (!values_printed)
934     {
935       fprintf_filtered (stream, "No arguments.\n");
936     }
937 }
938
939 static void
940 args_info (ignore, from_tty)
941      char *ignore;
942      int from_tty;
943 {
944   if (!selected_frame)
945     error ("No frame selected.");
946   print_frame_arg_vars (selected_frame, stdout);
947 }
948 \f
949 /* Select frame FRAME, and note that its stack level is LEVEL.
950    LEVEL may be -1 if an actual level number is not known.  */
951
952 void
953 select_frame (frame, level)
954      FRAME frame;
955      int level;
956 {
957   register struct symtab *s;
958
959   selected_frame = frame;
960   selected_frame_level = level;
961
962   /* Ensure that symbols for this frame are read in.  Also, determine the
963      source language of this frame, and switch to it if desired.  */
964   if (frame)
965   {
966     s = find_pc_symtab (get_frame_info (frame)->pc);
967     if (s 
968         && s->language != current_language->la_language
969         && s->language != language_unknown
970         && language_mode == language_mode_auto) {
971       set_language(s->language);
972     }
973   }
974 }
975
976 /* Store the selected frame and its level into *FRAMEP and *LEVELP.
977    If there is no selected frame, *FRAMEP is set to NULL.  */
978
979 void
980 record_selected_frame (frameaddrp, levelp)
981      FRAME_ADDR *frameaddrp;
982      int *levelp;
983 {
984   *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : 0;
985   *levelp = selected_frame_level;
986 }
987
988 /* Return the symbol-block in which the selected frame is executing.
989    Can return zero under various legitimate circumstances.  */
990
991 struct block *
992 get_selected_block ()
993 {
994   if (!target_has_stack)
995     return 0;
996
997   if (!selected_frame)
998     return get_current_block ();
999   return get_frame_block (selected_frame);
1000 }
1001
1002 /* Find a frame a certain number of levels away from FRAME.
1003    LEVEL_OFFSET_PTR points to an int containing the number of levels.
1004    Positive means go to earlier frames (up); negative, the reverse.
1005    The int that contains the number of levels is counted toward
1006    zero as the frames for those levels are found.
1007    If the top or bottom frame is reached, that frame is returned,
1008    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1009    how much farther the original request asked to go.  */
1010
1011 FRAME
1012 find_relative_frame (frame, level_offset_ptr)
1013      register FRAME frame;
1014      register int* level_offset_ptr;
1015 {
1016   register FRAME prev;
1017   register FRAME frame1;
1018
1019   /* Going up is simple: just do get_prev_frame enough times
1020      or until initial frame is reached.  */
1021   while (*level_offset_ptr > 0)
1022     {
1023       prev = get_prev_frame (frame);
1024       if (prev == 0)
1025         break;
1026       (*level_offset_ptr)--;
1027       frame = prev;
1028     }
1029   /* Going down is just as simple.  */
1030   if (*level_offset_ptr < 0)
1031     {
1032       while (*level_offset_ptr < 0) {
1033         frame1 = get_next_frame (frame);
1034         if (!frame1)
1035           break;
1036         frame = frame1;
1037         (*level_offset_ptr)++;
1038       }
1039     }
1040   return frame;
1041 }
1042
1043 /* The "select_frame" command.  With no arg, NOP.
1044    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1045    valid level.  Otherwise, treat level_exp as an address expression
1046    and select it.  See parse_frame_specification for more info on proper
1047    frame expressions. */
1048
1049 /* ARGSUSED */
1050 static void
1051 select_frame_command (level_exp, from_tty)
1052      char *level_exp;
1053      int from_tty;
1054 {
1055   register FRAME frame, frame1;
1056   unsigned int level = 0;
1057
1058   if (!target_has_stack)
1059     error ("No stack.");
1060
1061   frame = parse_frame_specification (level_exp);
1062
1063   /* Try to figure out what level this frame is.  But if there is
1064      no current stack, don't error out -- let the user set one.  */
1065   frame1 = 0;
1066   if (get_current_frame()) {
1067     for (frame1 = get_prev_frame (0);
1068          frame1 && frame1 != frame;
1069          frame1 = get_prev_frame (frame1))
1070       level++;
1071   }
1072
1073   if (!frame1)
1074     level = 0;
1075
1076   select_frame (frame, level);
1077 }
1078
1079 /* The "frame" command.  With no arg, print selected frame briefly.
1080    With arg, behaves like select_frame and then prints the selected
1081    frame.  */
1082
1083 static void
1084 frame_command (level_exp, from_tty)
1085      char *level_exp;
1086      int from_tty;
1087 {
1088   select_frame_command (level_exp, from_tty);
1089   print_stack_frame (selected_frame, selected_frame_level, 1);
1090 }
1091
1092 /* Select the frame up one or COUNT stack levels
1093    from the previously selected frame, and print it briefly.  */
1094
1095 /* ARGSUSED */
1096 static void
1097 up_silently_command (count_exp, from_tty)
1098      char *count_exp;
1099      int from_tty;
1100 {
1101   register FRAME frame;
1102   int count = 1, count1;
1103   if (count_exp)
1104     count = parse_and_eval_address (count_exp);
1105   count1 = count;
1106   
1107   if (target_has_stack == 0 || selected_frame == 0)
1108     error ("No stack.");
1109
1110   frame = find_relative_frame (selected_frame, &count1);
1111   if (count1 != 0 && count_exp == 0)
1112     error ("Initial frame selected; you cannot go up.");
1113   select_frame (frame, selected_frame_level + count - count1);
1114 }
1115
1116 static void
1117 up_command (count_exp, from_tty)
1118      char *count_exp;
1119      int from_tty;
1120 {
1121   up_silently_command (count_exp, from_tty);
1122   print_stack_frame (selected_frame, selected_frame_level, 1);
1123 }
1124
1125 /* Select the frame down one or COUNT stack levels
1126    from the previously selected frame, and print it briefly.  */
1127
1128 /* ARGSUSED */
1129 static void
1130 down_silently_command (count_exp, from_tty)
1131      char *count_exp;
1132      int from_tty;
1133 {
1134   register FRAME frame;
1135   int count = -1, count1;
1136   if (count_exp)
1137     count = - parse_and_eval_address (count_exp);
1138   count1 = count;
1139   
1140   if (target_has_stack == 0 || selected_frame == 0)
1141     error ("No stack.");
1142
1143   frame = find_relative_frame (selected_frame, &count1);
1144   if (count1 != 0 && count_exp == 0)
1145     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1146   select_frame (frame, selected_frame_level + count - count1);
1147 }
1148
1149
1150 static void
1151 down_command (count_exp, from_tty)
1152      char *count_exp;
1153      int from_tty;
1154 {
1155   down_silently_command (count_exp, from_tty);
1156   print_stack_frame (selected_frame, selected_frame_level, 1);
1157 }
1158 \f
1159 static void
1160 return_command (retval_exp, from_tty)
1161      char *retval_exp;
1162      int from_tty;
1163 {
1164   struct symbol *thisfun;
1165   FRAME_ADDR selected_frame_addr;
1166   CORE_ADDR selected_frame_pc;
1167   FRAME frame;
1168   value return_value;
1169
1170   if (selected_frame == NULL)
1171     error ("No selected frame.");
1172   thisfun = get_frame_function (selected_frame);
1173   selected_frame_addr = FRAME_FP (selected_frame);
1174   selected_frame_pc = (get_frame_info (selected_frame))->pc;
1175
1176   /* Compute the return value (if any -- possibly getting errors here).
1177      Call VALUE_CONTENTS to make sure we have fully evaluated it, since
1178      it might live in the stack frame we're about to pop.  */
1179
1180   if (retval_exp)
1181     {
1182       return_value = parse_and_eval (retval_exp);
1183       VALUE_CONTENTS (return_value);
1184     }
1185
1186   /* If interactive, require confirmation.  */
1187
1188   if (from_tty)
1189     {
1190       if (thisfun != 0)
1191         {
1192           if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1193             {
1194               error ("Not confirmed.");
1195               /* NOTREACHED */
1196             }
1197         }
1198       else
1199         if (!query ("Make selected stack frame return now? "))
1200           error ("Not confirmed.");
1201     }
1202
1203   /* Do the real work.  Pop until the specified frame is current.  We
1204      use this method because the selected_frame is not valid after
1205      a POP_FRAME.  The pc comparison makes this work even if the
1206      selected frame shares its fp with another frame.  */
1207
1208   while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
1209        || selected_frame_pc   != (get_frame_info (frame))->pc  )
1210     POP_FRAME;
1211
1212   /* Then pop that frame.  */
1213
1214   POP_FRAME;
1215
1216   /* Compute the return value (if any) and store in the place
1217      for return values.  */
1218
1219   if (retval_exp)
1220     set_return_value (return_value);
1221
1222   /* If interactive, print the frame that is now current.  */
1223
1224   if (from_tty)
1225     frame_command ("0", 1);
1226 }
1227
1228 /* Gets the language of the current frame. */
1229 enum language
1230 get_frame_language()
1231 {
1232    register struct symtab *s;
1233    FRAME fr;
1234    enum language flang;         /* The language of the current frame */
1235    
1236    fr = get_frame_info(selected_frame);
1237    if(fr)
1238    {
1239       s = find_pc_symtab(fr->pc);
1240       if(s)
1241          flang = s->language;
1242       else
1243          flang = language_unknown;
1244    }
1245    else
1246       flang = language_unknown;
1247
1248    return flang;
1249 }
1250 \f
1251 void
1252 _initialize_stack ()
1253 {
1254 #if 0  
1255   backtrace_limit = 30;
1256 #endif
1257
1258   add_com ("return", class_stack, return_command,
1259            "Make selected stack frame return to its caller.\n\
1260 Control remains in the debugger, but when you continue\n\
1261 execution will resume in the frame above the one now selected.\n\
1262 If an argument is given, it is an expression for the value to return.");
1263
1264   add_com ("up", class_stack, up_command,
1265            "Select and print stack frame that called this one.\n\
1266 An argument says how many frames up to go.");
1267   add_com ("up-silently", class_support, up_silently_command,
1268            "Same as the `up' command, but does not print anything.\n\
1269 This is useful in command scripts.");
1270
1271   add_com ("down", class_stack, down_command,
1272            "Select and print stack frame called by this one.\n\
1273 An argument says how many frames down to go.");
1274   add_com_alias ("do", "down", class_stack, 1);
1275   add_com_alias ("dow", "down", class_stack, 1);
1276   add_com ("down-silently", class_support, down_silently_command,
1277            "Same as the `down' command, but does not print anything.\n\
1278 This is useful in command scripts.");
1279
1280   add_com ("frame", class_stack, frame_command,
1281            "Select and print a stack frame.\n\
1282 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
1283 An argument specifies the frame to select.\n\
1284 It can be a stack frame number or the address of the frame.\n\
1285 With argument, nothing is printed if input is coming from\n\
1286 a command file or a user-defined command.");
1287
1288   add_com_alias ("f", "frame", class_stack, 1);
1289
1290   add_com ("select-frame", class_stack, select_frame_command,
1291            "Select a stack frame without printing anything.\n\
1292 An argument specifies the frame to select.\n\
1293 It can be a stack frame number or the address of the frame.\n");
1294
1295   add_com ("backtrace", class_stack, backtrace_command,
1296            "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1297 With a negative argument, print outermost -COUNT frames.");
1298   add_com_alias ("bt", "backtrace", class_stack, 0);
1299   add_com_alias ("where", "backtrace", class_alias, 0);
1300   add_info ("stack", backtrace_command,
1301             "Backtrace of the stack, or innermost COUNT frames.");
1302   add_info_alias ("s", "stack", 1);
1303   add_info ("frame", frame_info,
1304             "All about selected stack frame, or frame at ADDR.");
1305   add_info_alias ("f", "frame", 1);
1306   add_info ("locals", locals_info,
1307             "Local variables of current stack frame.");
1308   add_info ("args", args_info,
1309             "Argument variables of current stack frame.");
1310   add_info ("catch", catch_info,
1311             "Exceptions that can be caught in the current stack frame.");
1312
1313 #if 0
1314   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
1315            "Specify maximum number of frames for \"backtrace\" to print by default.",
1316            &setlist);
1317   add_info ("backtrace-limit", backtrace_limit_info,
1318             "The maximum number of frames for \"backtrace\" to print by default.");
1319 #endif
1320 }