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