]> Git Repo - binutils.git/blob - gdb/stack.c
remove parentdir support
[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_register (SP_REGNUM), 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_frame);
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, "");
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_frame);
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_frame && calling_frame)
479     puts_filtered (",");
480   wrap_here ("   ");
481   if (fi->next_frame)
482     printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame));
483   if (fi->next_frame || calling_frame)
484     puts_filtered ("\n");
485   if (s)
486      printf_filtered(" source language %s.\n", language_str(s->language));
487
488 #ifdef PRINT_EXTRA_FRAME_INFO
489   PRINT_EXTRA_FRAME_INFO (fi);
490 #endif
491
492   {
493     /* Address of the argument list for this frame, or 0.  */
494     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
495     /* Number of args for this frame, or -1 if unknown.  */
496     int numargs;
497
498     if (arg_list == 0)
499         printf_filtered (" Arglist at unknown address.\n");
500     else
501       {
502         printf_filtered (" Arglist at %s,", local_hex_string(arg_list));
503
504         FRAME_NUM_ARGS (numargs, fi);
505         if (numargs < 0)
506           puts_filtered (" args: ");
507         else if (numargs == 0)
508           puts_filtered (" no args.");
509         else if (numargs == 1)
510           puts_filtered (" 1 arg: ");
511         else
512           printf_filtered (" %d args: ", numargs);
513         print_frame_args (func, fi, numargs, stdout);
514         puts_filtered ("\n");
515       }
516   }
517   {
518     /* Address of the local variables for this frame, or 0.  */
519     CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
520
521     if (arg_list == 0)
522         printf_filtered (" Locals at unknown address,");
523     else
524         printf_filtered (" Locals at %s,", local_hex_string(arg_list));
525   }
526
527 #if defined (FRAME_FIND_SAVED_REGS)  
528   get_frame_saved_regs (fi, &fsr);
529   /* The sp is special; what's returned isn't the save address, but
530      actually the value of the previous frame's sp.  */
531   printf_filtered (" Previous frame's sp is %s\n", 
532                    local_hex_string(fsr.regs[SP_REGNUM]));
533   count = 0;
534   for (i = 0; i < NUM_REGS; i++)
535     if (fsr.regs[i] && i != SP_REGNUM)
536       {
537         if (count == 0)
538           puts_filtered (" Saved registers:\n ");
539         else
540           puts_filtered (",");
541         wrap_here (" ");
542         printf_filtered (" %s at %s", reg_names[i], 
543                          local_hex_string(fsr.regs[i]));
544         count++;
545       }
546   if (count)
547     puts_filtered ("\n");
548 #endif /* Have FRAME_FIND_SAVED_REGS.  */
549 }
550
551 #if 0
552 /* Set a limit on the number of frames printed by default in a
553    backtrace.  */
554
555 static int backtrace_limit;
556
557 static void
558 set_backtrace_limit_command (count_exp, from_tty)
559      char *count_exp;
560      int from_tty;
561 {
562   int count = parse_and_eval_address (count_exp);
563
564   if (count < 0)
565     error ("Negative argument not meaningful as backtrace limit.");
566
567   backtrace_limit = count;
568 }
569
570 static void
571 backtrace_limit_info (arg, from_tty)
572      char *arg;
573      int from_tty;
574 {
575   if (arg)
576     error ("\"Info backtrace-limit\" takes no arguments.");
577
578   printf ("Backtrace limit: %d.\n", backtrace_limit);
579 }
580 #endif
581
582 /* Print briefly all stack frames or just the innermost COUNT frames.  */
583
584 static void
585 backtrace_command (count_exp, from_tty)
586      char *count_exp;
587      int from_tty;
588 {
589   struct frame_info *fi;
590   register int count;
591   register FRAME frame;
592   register int i;
593   register FRAME trailing;
594   register int trailing_level;
595
596   if (!target_has_stack)
597     error ("No stack.");
598
599   /* The following code must do two things.  First, it must
600      set the variable TRAILING to the frame from which we should start
601      printing.  Second, it must set the variable count to the number
602      of frames which we should print, or -1 if all of them.  */
603   trailing = get_current_frame ();
604   trailing_level = 0;
605   if (count_exp)
606     {
607       count = parse_and_eval_address (count_exp);
608       if (count < 0)
609         {
610           FRAME current;
611
612           count = -count;
613
614           current = trailing;
615           while (current && count--)
616             {
617               QUIT;
618               current = get_prev_frame (current);
619             }
620           
621           /* Will stop when CURRENT reaches the top of the stack.  TRAILING
622              will be COUNT below it.  */
623           while (current)
624             {
625               QUIT;
626               trailing = get_prev_frame (trailing);
627               current = get_prev_frame (current);
628               trailing_level++;
629             }
630           
631           count = -1;
632         }
633     }
634   else
635     count = -1;
636
637   if (info_verbose)
638     {
639       struct partial_symtab *ps;
640       
641       /* Read in symbols for all of the frames.  Need to do this in
642          a separate pass so that "Reading in symbols for xxx" messages
643          don't screw up the appearance of the backtrace.  Also
644          if people have strong opinions against reading symbols for
645          backtrace this may have to be an option.  */
646       i = count;
647       for (frame = trailing;
648            frame != NULL && i--;
649            frame = get_prev_frame (frame))
650         {
651           QUIT;
652           fi = get_frame_info (frame);
653           ps = find_pc_psymtab (fi->pc);
654           if (ps)
655             PSYMTAB_TO_SYMTAB (ps);     /* Force syms to come in */
656         }
657     }
658
659   for (i = 0, frame = trailing;
660        frame && count--;
661        i++, frame = get_prev_frame (frame))
662     {
663       QUIT;
664       fi = get_frame_info (frame);
665       print_frame_info (fi, trailing_level + i, 0, 1);
666     }
667
668   /* If we've stopped before the end, mention that.  */
669   if (frame && from_tty)
670     printf_filtered ("(More stack frames follow...)\n");
671 }
672 \f
673 /* Print the local variables of a block B active in FRAME.
674    Return 1 if any variables were printed; 0 otherwise.  */
675
676 static int
677 print_block_frame_locals (b, frame, stream)
678      struct block *b;
679      register FRAME frame;
680      register FILE *stream;
681 {
682   int nsyms;
683   register int i;
684   register struct symbol *sym;
685   register int values_printed = 0;
686
687   nsyms = BLOCK_NSYMS (b);
688
689   for (i = 0; i < nsyms; i++)
690     {
691       sym = BLOCK_SYM (b, i);
692       if (SYMBOL_CLASS (sym) == LOC_LOCAL
693           || SYMBOL_CLASS (sym) == LOC_REGISTER
694           || SYMBOL_CLASS (sym) == LOC_STATIC)
695         {
696           values_printed = 1;
697           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
698           fputs_filtered (" = ", stream);
699           print_variable_value (sym, frame, stream);
700           fprintf_filtered (stream, "\n");
701         }
702     }
703   return values_printed;
704 }
705
706 /* Same, but print labels.  */
707
708 static int
709 print_block_frame_labels (b, have_default, stream)
710      struct block *b;
711      int *have_default;
712      register FILE *stream;
713 {
714   int nsyms;
715   register int i;
716   register struct symbol *sym;
717   register int values_printed = 0;
718
719   nsyms = BLOCK_NSYMS (b);
720
721   for (i = 0; i < nsyms; i++)
722     {
723       sym = BLOCK_SYM (b, i);
724       if (STREQ (SYMBOL_NAME (sym), "default"))
725         {
726           if (*have_default)
727             continue;
728           *have_default = 1;
729         }
730       if (SYMBOL_CLASS (sym) == LOC_LABEL)
731         {
732           struct symtab_and_line sal;
733           sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
734           values_printed = 1;
735           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
736           if (addressprint)
737             fprintf_filtered (stream, " %s", 
738                               local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
739           fprintf_filtered (stream, " in file %s, line %d\n",
740                             sal.symtab->filename, sal.line);
741         }
742     }
743   return values_printed;
744 }
745
746 /* Print on STREAM all the local variables in frame FRAME,
747    including all the blocks active in that frame
748    at its current pc.
749
750    Returns 1 if the job was done,
751    or 0 if nothing was printed because we have no info
752    on the function running in FRAME.  */
753
754 static void
755 print_frame_local_vars (frame, stream)
756      register FRAME frame;
757      register FILE *stream;
758 {
759   register struct block *block = get_frame_block (frame);
760   register int values_printed = 0;
761
762   if (block == 0)
763     {
764       fprintf_filtered (stream, "No symbol table info available.\n");
765       return;
766     }
767   
768   while (block != 0)
769     {
770       if (print_block_frame_locals (block, frame, stream))
771         values_printed = 1;
772       /* After handling the function's top-level block, stop.
773          Don't continue to its superblock, the block of
774          per-file symbols.  */
775       if (BLOCK_FUNCTION (block))
776         break;
777       block = BLOCK_SUPERBLOCK (block);
778     }
779
780   if (!values_printed)
781     {
782       fprintf_filtered (stream, "No locals.\n");
783     }
784 }
785
786 /* Same, but print labels.  */
787
788 static void
789 print_frame_label_vars (frame, this_level_only, stream)
790      register FRAME frame;
791      int this_level_only;
792      register FILE *stream;
793 {
794   register struct blockvector *bl;
795   register struct block *block = get_frame_block (frame);
796   register int values_printed = 0;
797   int index, have_default = 0;
798   char *blocks_printed;
799   struct frame_info *fi = get_frame_info (frame);
800   CORE_ADDR pc = fi->pc;
801
802   if (block == 0)
803     {
804       fprintf_filtered (stream, "No symbol table info available.\n");
805       return;
806     }
807
808   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
809   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
810   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
811
812   while (block != 0)
813     {
814       CORE_ADDR end = BLOCK_END (block) - 4;
815       int last_index;
816
817       if (bl != blockvector_for_pc (end, &index))
818         error ("blockvector blotch");
819       if (BLOCKVECTOR_BLOCK (bl, index) != block)
820         error ("blockvector botch");
821       last_index = BLOCKVECTOR_NBLOCKS (bl);
822       index += 1;
823
824       /* Don't print out blocks that have gone by.  */
825       while (index < last_index
826              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
827         index++;
828
829       while (index < last_index
830              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
831         {
832           if (blocks_printed[index] == 0)
833             {
834               if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
835                 values_printed = 1;
836               blocks_printed[index] = 1;
837             }
838           index++;
839         }
840       if (have_default)
841         return;
842       if (values_printed && this_level_only)
843         return;
844
845       /* After handling the function's top-level block, stop.
846          Don't continue to its superblock, the block of
847          per-file symbols.  */
848       if (BLOCK_FUNCTION (block))
849         break;
850       block = BLOCK_SUPERBLOCK (block);
851     }
852
853   if (!values_printed && !this_level_only)
854     {
855       fprintf_filtered (stream, "No catches.\n");
856     }
857 }
858
859 /* ARGSUSED */
860 static void
861 locals_info (args, from_tty)
862      char *args;
863      int from_tty;
864 {
865   if (!selected_frame)
866     error ("No frame selected.");
867   print_frame_local_vars (selected_frame, stdout);
868 }
869
870 static void
871 catch_info (ignore, from_tty)
872      char *ignore;
873      int from_tty;
874 {
875   if (!selected_frame)
876     error ("No frame selected.");
877   print_frame_label_vars (selected_frame, 0, stdout);
878 }
879
880 static void
881 print_frame_arg_vars (frame, stream)
882      register FRAME frame;
883      register FILE *stream;
884 {
885   struct symbol *func = get_frame_function (frame);
886   register struct block *b;
887   int nsyms;
888   register int i;
889   register struct symbol *sym, *sym2;
890   register int values_printed = 0;
891
892   if (func == 0)
893     {
894       fprintf_filtered (stream, "No symbol table info available.\n");
895       return;
896     }
897
898   b = SYMBOL_BLOCK_VALUE (func);
899   nsyms = BLOCK_NSYMS (b);
900
901   for (i = 0; i < nsyms; i++)
902     {
903       sym = BLOCK_SYM (b, i);
904       if (SYMBOL_CLASS (sym) == LOC_ARG
905           || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
906           || SYMBOL_CLASS (sym) == LOC_REF_ARG
907           || SYMBOL_CLASS (sym) == LOC_REGPARM
908           || SYMBOL_CLASS (sym) == LOC_REGPARM_ADDR)
909         {
910           values_printed = 1;
911           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
912           fputs_filtered (" = ", stream);
913
914           /* We have to look up the symbol because arguments can have
915              two entries (one a parameter, one a local) and the one we
916              want is the local, which lookup_symbol will find for us.
917              This includes gcc1 (not gcc2) on the sparc when passing a
918              small structure and gcc2 when the argument type is float
919              and it is passed as a double and converted to float by
920              the prologue (in the latter case the type of the LOC_ARG
921              symbol is double and the type of the LOC_LOCAL symbol is
922              float).  There are also LOC_ARG/LOC_REGISTER pairs which
923              are not combined in symbol-reading.  */
924
925           sym2 = lookup_symbol (SYMBOL_NAME (sym),
926                         b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
927           print_variable_value (sym2, frame, stream);
928           fprintf_filtered (stream, "\n");
929         }
930     }
931
932   if (!values_printed)
933     {
934       fprintf_filtered (stream, "No arguments.\n");
935     }
936 }
937
938 static void
939 args_info (ignore, from_tty)
940      char *ignore;
941      int from_tty;
942 {
943   if (!selected_frame)
944     error ("No frame selected.");
945   print_frame_arg_vars (selected_frame, stdout);
946 }
947 \f
948 /* Select frame FRAME, and note that its stack level is LEVEL.
949    LEVEL may be -1 if an actual level number is not known.  */
950
951 void
952 select_frame (frame, level)
953      FRAME frame;
954      int level;
955 {
956   register struct symtab *s;
957
958   selected_frame = frame;
959   selected_frame_level = level;
960
961   /* Ensure that symbols for this frame are read in.  Also, determine the
962      source language of this frame, and switch to it if desired.  */
963   if (frame)
964   {
965     s = find_pc_symtab (get_frame_info (frame)->pc);
966     if (s 
967         && s->language != current_language->la_language
968         && s->language != language_unknown
969         && language_mode == language_mode_auto) {
970       set_language(s->language);
971     }
972   }
973 }
974
975 /* Store the selected frame and its level into *FRAMEP and *LEVELP.
976    If there is no selected frame, *FRAMEP is set to NULL.  */
977
978 void
979 record_selected_frame (frameaddrp, levelp)
980      FRAME_ADDR *frameaddrp;
981      int *levelp;
982 {
983   *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : 0;
984   *levelp = selected_frame_level;
985 }
986
987 /* Return the symbol-block in which the selected frame is executing.
988    Can return zero under various legitimate circumstances.  */
989
990 struct block *
991 get_selected_block ()
992 {
993   if (!target_has_stack)
994     return 0;
995
996   if (!selected_frame)
997     return get_current_block ();
998   return get_frame_block (selected_frame);
999 }
1000
1001 /* Find a frame a certain number of levels away from FRAME.
1002    LEVEL_OFFSET_PTR points to an int containing the number of levels.
1003    Positive means go to earlier frames (up); negative, the reverse.
1004    The int that contains the number of levels is counted toward
1005    zero as the frames for those levels are found.
1006    If the top or bottom frame is reached, that frame is returned,
1007    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1008    how much farther the original request asked to go.  */
1009
1010 FRAME
1011 find_relative_frame (frame, level_offset_ptr)
1012      register FRAME frame;
1013      register int* level_offset_ptr;
1014 {
1015   register FRAME prev;
1016   register FRAME frame1;
1017
1018   /* Going up is simple: just do get_prev_frame enough times
1019      or until initial frame is reached.  */
1020   while (*level_offset_ptr > 0)
1021     {
1022       prev = get_prev_frame (frame);
1023       if (prev == 0)
1024         break;
1025       (*level_offset_ptr)--;
1026       frame = prev;
1027     }
1028   /* Going down is just as simple.  */
1029   if (*level_offset_ptr < 0)
1030     {
1031       while (*level_offset_ptr < 0) {
1032         frame1 = get_next_frame (frame);
1033         if (!frame1)
1034           break;
1035         frame = frame1;
1036         (*level_offset_ptr)++;
1037       }
1038     }
1039   return frame;
1040 }
1041
1042 /* The "select_frame" command.  With no arg, NOP.
1043    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1044    valid level.  Otherwise, treat level_exp as an address expression
1045    and select it.  See parse_frame_specification for more info on proper
1046    frame expressions. */
1047
1048 /* ARGSUSED */
1049 static void
1050 select_frame_command (level_exp, from_tty)
1051      char *level_exp;
1052      int from_tty;
1053 {
1054   register FRAME frame, frame1;
1055   unsigned int level = 0;
1056
1057   if (!target_has_stack)
1058     error ("No stack.");
1059
1060   frame = parse_frame_specification (level_exp);
1061
1062   /* Try to figure out what level this frame is.  But if there is
1063      no current stack, don't error out -- let the user set one.  */
1064   frame1 = 0;
1065   if (get_current_frame()) {
1066     for (frame1 = get_prev_frame (0);
1067          frame1 && frame1 != frame;
1068          frame1 = get_prev_frame (frame1))
1069       level++;
1070   }
1071
1072   if (!frame1)
1073     level = 0;
1074
1075   select_frame (frame, level);
1076 }
1077
1078 /* The "frame" command.  With no arg, print selected frame briefly.
1079    With arg, behaves like select_frame and then prints the selected
1080    frame.  */
1081
1082 static void
1083 frame_command (level_exp, from_tty)
1084      char *level_exp;
1085      int from_tty;
1086 {
1087   select_frame_command (level_exp, from_tty);
1088   print_stack_frame (selected_frame, selected_frame_level, 1);
1089 }
1090
1091 /* Select the frame up one or COUNT stack levels
1092    from the previously selected frame, and print it briefly.  */
1093
1094 /* ARGSUSED */
1095 static void
1096 up_silently_command (count_exp, from_tty)
1097      char *count_exp;
1098      int from_tty;
1099 {
1100   register FRAME frame;
1101   int count = 1, count1;
1102   if (count_exp)
1103     count = parse_and_eval_address (count_exp);
1104   count1 = count;
1105   
1106   if (target_has_stack == 0 || selected_frame == 0)
1107     error ("No stack.");
1108
1109   frame = find_relative_frame (selected_frame, &count1);
1110   if (count1 != 0 && count_exp == 0)
1111     error ("Initial frame selected; you cannot go up.");
1112   select_frame (frame, selected_frame_level + count - count1);
1113 }
1114
1115 static void
1116 up_command (count_exp, from_tty)
1117      char *count_exp;
1118      int from_tty;
1119 {
1120   up_silently_command (count_exp, from_tty);
1121   print_stack_frame (selected_frame, selected_frame_level, 1);
1122 }
1123
1124 /* Select the frame down one or COUNT stack levels
1125    from the previously selected frame, and print it briefly.  */
1126
1127 /* ARGSUSED */
1128 static void
1129 down_silently_command (count_exp, from_tty)
1130      char *count_exp;
1131      int from_tty;
1132 {
1133   register FRAME frame;
1134   int count = -1, count1;
1135   if (count_exp)
1136     count = - parse_and_eval_address (count_exp);
1137   count1 = count;
1138   
1139   if (target_has_stack == 0 || selected_frame == 0)
1140     error ("No stack.");
1141
1142   frame = find_relative_frame (selected_frame, &count1);
1143   if (count1 != 0 && count_exp == 0)
1144     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1145   select_frame (frame, selected_frame_level + count - count1);
1146 }
1147
1148
1149 static void
1150 down_command (count_exp, from_tty)
1151      char *count_exp;
1152      int from_tty;
1153 {
1154   down_silently_command (count_exp, from_tty);
1155   print_stack_frame (selected_frame, selected_frame_level, 1);
1156 }
1157 \f
1158 static void
1159 return_command (retval_exp, from_tty)
1160      char *retval_exp;
1161      int from_tty;
1162 {
1163   struct symbol *thisfun;
1164   FRAME_ADDR selected_frame_addr;
1165   CORE_ADDR selected_frame_pc;
1166   FRAME frame;
1167   value return_value;
1168
1169   if (selected_frame == NULL)
1170     error ("No selected frame.");
1171   thisfun = get_frame_function (selected_frame);
1172   selected_frame_addr = FRAME_FP (selected_frame);
1173   selected_frame_pc = (get_frame_info (selected_frame))->pc;
1174
1175   /* Compute the return value (if any -- possibly getting errors here).
1176      Call VALUE_CONTENTS to make sure we have fully evaluated it, since
1177      it might live in the stack frame we're about to pop.  */
1178
1179   if (retval_exp)
1180     {
1181       return_value = parse_and_eval (retval_exp);
1182       VALUE_CONTENTS (return_value);
1183     }
1184
1185   /* If interactive, require confirmation.  */
1186
1187   if (from_tty)
1188     {
1189       if (thisfun != 0)
1190         {
1191           if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1192             {
1193               error ("Not confirmed.");
1194               /* NOTREACHED */
1195             }
1196         }
1197       else
1198         if (!query ("Make selected stack frame return now? "))
1199           error ("Not confirmed.");
1200     }
1201
1202   /* Do the real work.  Pop until the specified frame is current.  We
1203      use this method because the selected_frame is not valid after
1204      a POP_FRAME.  The pc comparison makes this work even if the
1205      selected frame shares its fp with another frame.  */
1206
1207   while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
1208        || selected_frame_pc   != (get_frame_info (frame))->pc  )
1209     POP_FRAME;
1210
1211   /* Then pop that frame.  */
1212
1213   POP_FRAME;
1214
1215   /* Compute the return value (if any) and store in the place
1216      for return values.  */
1217
1218   if (retval_exp)
1219     set_return_value (return_value);
1220
1221   /* If interactive, print the frame that is now current.  */
1222
1223   if (from_tty)
1224     frame_command ("0", 1);
1225 }
1226
1227 /* Gets the language of the current frame. */
1228 enum language
1229 get_frame_language()
1230 {
1231    register struct symtab *s;
1232    FRAME fr;
1233    enum language flang;         /* The language of the current frame */
1234    
1235    fr = get_frame_info(selected_frame);
1236    if(fr)
1237    {
1238       s = find_pc_symtab(fr->pc);
1239       if(s)
1240          flang = s->language;
1241       else
1242          flang = language_unknown;
1243    }
1244    else
1245       flang = language_unknown;
1246
1247    return flang;
1248 }
1249 \f
1250 void
1251 _initialize_stack ()
1252 {
1253 #if 0  
1254   backtrace_limit = 30;
1255 #endif
1256
1257   add_com ("return", class_stack, return_command,
1258            "Make selected stack frame return to its caller.\n\
1259 Control remains in the debugger, but when you continue\n\
1260 execution will resume in the frame above the one now selected.\n\
1261 If an argument is given, it is an expression for the value to return.");
1262
1263   add_com ("up", class_stack, up_command,
1264            "Select and print stack frame that called this one.\n\
1265 An argument says how many frames up to go.");
1266   add_com ("up-silently", class_support, up_silently_command,
1267            "Same as the `up' command, but does not print anything.\n\
1268 This is useful in command scripts.");
1269
1270   add_com ("down", class_stack, down_command,
1271            "Select and print stack frame called by this one.\n\
1272 An argument says how many frames down to go.");
1273   add_com_alias ("do", "down", class_stack, 1);
1274   add_com_alias ("dow", "down", class_stack, 1);
1275   add_com ("down-silently", class_support, down_silently_command,
1276            "Same as the `down' command, but does not print anything.\n\
1277 This is useful in command scripts.");
1278
1279   add_com ("frame", class_stack, frame_command,
1280            "Select and print a stack frame.\n\
1281 With no argument, print the selected stack frame.  (See also \"info frame\").\n\
1282 An argument specifies the frame to select.\n\
1283 It can be a stack frame number or the address of the frame.\n\
1284 With argument, nothing is printed if input is coming from\n\
1285 a command file or a user-defined command.");
1286
1287   add_com_alias ("f", "frame", class_stack, 1);
1288
1289   add_com ("select-frame", class_stack, select_frame_command,
1290            "Select a stack frame without printing anything.\n\
1291 An argument specifies the frame to select.\n\
1292 It can be a stack frame number or the address of the frame.\n");
1293
1294   add_com ("backtrace", class_stack, backtrace_command,
1295            "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1296 With a negative argument, print outermost -COUNT frames.");
1297   add_com_alias ("bt", "backtrace", class_stack, 0);
1298   add_com_alias ("where", "backtrace", class_alias, 0);
1299   add_info ("stack", backtrace_command,
1300             "Backtrace of the stack, or innermost COUNT frames.");
1301   add_info_alias ("s", "stack", 1);
1302   add_info ("frame", frame_info,
1303             "All about selected stack frame, or frame at ADDR.");
1304   add_info_alias ("f", "frame", 1);
1305   add_info ("locals", locals_info,
1306             "Local variables of current stack frame.");
1307   add_info ("args", args_info,
1308             "Argument variables of current stack frame.");
1309   add_info ("catch", catch_info,
1310             "Exceptions that can be caught in the current stack frame.");
1311
1312 #if 0
1313   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
1314            "Specify maximum number of frames for \"backtrace\" to print by default.",
1315            &setlist);
1316   add_info ("backtrace-limit", backtrace_limit_info,
1317             "The maximum number of frames for \"backtrace\" to print by default.");
1318 #endif
1319 }
This page took 0.133086 seconds and 4 git commands to generate.