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