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