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