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