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