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