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