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