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