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