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