1 /* Print and select stack frames for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
24 #include "initialize.h"
31 /* Thie "selected" stack frame is used by default for local and arg access.
32 May be zero, for no selected frame. */
36 /* Level of the selected frame:
37 0 for innermost, 1 for its caller, ...
38 or -1 for frame specified by address with no defined level. */
40 int selected_frame_level
;
42 static void select_calling_frame ();
44 void print_frame_info ();
46 /* Print a stack frame briefly. FRAME should be the frame address
47 and LEVEL should be its level in the stack (or -1 for level not defined).
48 This prints the level, the function executing, the arguments,
49 and the file name and line number.
50 If the pc is not at the beginning of the source line,
51 the actual pc is printed at the beginning.
53 If SOURCE is 1, print the source line as well.
54 If SOURCE is -1, print ONLY the source line. */
57 print_stack_frame (frame
, level
, source
)
64 fi
= get_frame_info (frame
);
66 print_frame_info (&fi
, level
, source
, 1);
70 print_frame_info (fi
, level
, source
, args
)
71 struct frame_info
*fi
;
76 register FRAME frame
= fi
->frame
;
77 struct symtab_and_line sal
;
79 register char *funname
= 0;
82 sal
= find_pc_line (fi
->pc
, fi
->next_frame
);
83 func
= get_frame_function (frame
);
85 funname
= SYMBOL_NAME (func
);
88 register int misc_index
= find_pc_misc_function (fi
->pc
);
90 funname
= misc_function_vector
[misc_index
].name
;
93 if (source
>= 0 || !sal
.symtab
)
95 /* This avoids a bug in cc on the sun. */
96 struct frame_info tem
;
100 printf ("#%-2d ", level
);
101 if (fi
->pc
!= sal
.pc
|| !sal
.symtab
)
102 printf ("0x%x in ", fi
->pc
);
103 printf ("%s (", funname
? funname
: "??");
106 FRAME_NUM_ARGS (numargs
, tem
);
107 print_frame_args (func
, FRAME_ARGS_ADDRESS (tem
), numargs
, stdout
);
111 printf (" (%s line %d)", sal
.symtab
->filename
, sal
.line
);
115 if (source
!= 0 && sal
.symtab
)
117 if (source
< 0 && fi
->pc
!= sal
.pc
)
118 printf ("0x%x\t", fi
->pc
);
119 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1);
120 current_source_line
= max (sal
.line
- 5, 1);
123 set_default_breakpoint (1, fi
->pc
, sal
.symtab
, sal
.line
);
128 /* Call here to print info on selected frame, after a trap. */
131 print_sel_frame (just_source
)
134 print_stack_frame (selected_frame
, -1, just_source
? -1 : 1);
137 /* Print info on the selected frame, including level number
140 print_selected_frame ()
142 print_stack_frame (selected_frame
, selected_frame_level
, 0);
145 /* Print verbosely the selected frame or the frame at address ADDR.
146 This means absolutely all information in the frame is printed. */
149 frame_info (addr_exp
)
152 FRAME frame
= addr_exp
? parse_and_eval_address (addr_exp
) : selected_frame
;
153 struct frame_info fi
;
154 struct frame_saved_regs fsr
;
155 struct symtab_and_line sal
;
162 fi
= get_frame_info (frame
);
163 get_frame_saved_regs (&fi
, &fsr
);
164 sal
= find_pc_line (fi
.pc
, fi
.next_frame
);
165 func
= get_frame_function (frame
);
167 funname
= SYMBOL_NAME (func
);
170 register int misc_index
= find_pc_misc_function (fi
.pc
);
172 funname
= misc_function_vector
[misc_index
].name
;
174 calling_frame
= get_prev_frame (frame
);
176 if (!addr_exp
&& selected_frame_level
>= 0)
177 printf ("Stack level %d, frame at 0x%x:\n pc = 0x%x",
178 selected_frame_level
, frame
, fi
.pc
);
180 printf ("Stack frame at 0x%x:\n pc = 0x%x",
184 printf (" in %s", funname
);
186 printf (" (%s line %d)", sal
.symtab
->filename
, sal
.line
);
187 printf ("; saved pc 0x%x\n", FRAME_SAVED_PC (frame
));
189 printf (" called by frame at 0x%x", calling_frame
);
190 if (fi
.next_frame
&& calling_frame
)
193 printf (" caller of frame at 0x%x", fi
.next_frame
);
194 if (fi
.next_frame
|| calling_frame
)
196 printf (" Arglist at 0x%x,", FRAME_ARGS_ADDRESS (fi
));
197 FRAME_NUM_ARGS (i
, fi
);
201 printf (" no args.");
205 printf (" %d args: ", i
);
207 FRAME_NUM_ARGS (numargs
, fi
);
208 print_frame_args (func
, FRAME_ARGS_ADDRESS (fi
), numargs
, stdout
);
211 for (i
= 0; i
< NUM_REGS
; i
++)
219 printf (" Saved registers:");
222 printf ("%s at 0x%x", reg_names
[i
], fsr
.regs
[i
]);
229 /* Print briefly all stack frames or just the innermost COUNT frames. */
232 backtrace_command (count_exp
)
235 struct frame_info fi
;
237 register FRAME frame
;
241 count
= parse_and_eval_address (count_exp
);
245 for (i
= 0, frame
= get_current_frame (), fi
= get_frame_info (frame
);
247 i
++, fi
= get_prev_frame_info (fi
.frame
), frame
= fi
.frame
)
250 print_frame_info (&fi
, i
, 0, 1);
254 /* Print the local variables of a block B active in FRAME. */
257 print_block_frame_locals (b
, frame
, stream
)
259 register FRAME frame
;
260 register FILE *stream
;
264 register struct symbol
*sym
;
266 nsyms
= BLOCK_NSYMS (b
);
268 for (i
= 0; i
< nsyms
; i
++)
270 sym
= BLOCK_SYM (b
, i
);
271 if (SYMBOL_CLASS (sym
) == LOC_LOCAL
272 || SYMBOL_CLASS (sym
) == LOC_REGISTER
)
274 fprintf (stream
, "%s = ", SYMBOL_NAME (sym
));
275 print_variable_value (sym
, frame
, stream
);
276 fprintf (stream
, "\n");
282 /* Print on STREAM all the local variables in frame FRAME,
283 including all the blocks active in that frame
286 Returns 1 if the job was done,
287 or 0 if nothing was printed because we have no info
288 on the function running in FRAME. */
291 print_frame_local_vars (frame
, stream
)
292 register FRAME frame
;
293 register FILE *stream
;
295 register struct block
*block
= get_frame_block (frame
);
300 print_block_frame_locals (block
, frame
, stream
);
301 /* After handling the function's top-level block, stop.
302 Don't continue to its superblock, the block of
304 if (BLOCK_FUNCTION (block
))
306 block
= BLOCK_SUPERBLOCK (block
);
314 print_frame_local_vars (selected_frame
, stdout
);
318 print_frame_arg_vars (frame
, stream
)
319 register FRAME frame
;
320 register FILE *stream
;
322 struct symbol
*func
= get_frame_function (frame
);
323 register struct block
*b
;
326 register struct symbol
*sym
;
331 b
= SYMBOL_BLOCK_VALUE (func
);
332 nsyms
= BLOCK_NSYMS (b
);
334 for (i
= 0; i
< nsyms
; i
++)
336 sym
= BLOCK_SYM (b
, i
);
337 if (SYMBOL_CLASS (sym
) == LOC_ARG
)
339 fprintf (stream
, "%s = ", SYMBOL_NAME (sym
));
340 print_variable_value (sym
, frame
, stream
);
341 fprintf (stream
, "\n");
352 print_frame_arg_vars (selected_frame
, stdout
);
355 /* Select frame FRAME, and note that its stack level is LEVEL.
356 LEVEL may be -1 if an actual level number is not known. */
359 select_frame (frame
, level
)
363 selected_frame
= frame
;
364 selected_frame_level
= level
;
367 /* Store the selected frame and its level into *FRAMEP and *LEVELP. */
370 record_selected_frame (framep
, levelp
)
374 *framep
= selected_frame
;
375 *levelp
= selected_frame_level
;
378 /* Return the symbol-block in which the selected frame is executing.
379 Can return zero under various legitimate circumstances. */
382 get_selected_block ()
384 if (!have_inferior_p () && !have_core_file_p ())
388 return get_current_block ();
389 return get_frame_block (selected_frame
);
392 /* Find a frame a certain number of levels away from FRAME.
393 LEVEL_OFFSET_PTR points to an int containing the number of levels.
394 Positive means go to earlier frames (up); negative, the reverse.
395 The int that contains the number of levels is counted toward
396 zero as the frames for those levels are found.
397 If the top or bottom frame is reached, that frame is returned,
398 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
399 how much farther the original request asked to go. */
402 find_relative_frame (frame
, level_offset_ptr
)
403 register FRAME frame
;
404 register int* level_offset_ptr
;
407 struct frame_info fi
;
408 register FRAME frame1
, frame2
;
410 /* Going up is simple: just do get_prev_frame enough times
411 or until initial frame is reached. */
412 while (*level_offset_ptr
> 0)
414 prev
= get_prev_frame (frame
);
417 (*level_offset_ptr
)--;
420 /* Going down could be done by iterating get_frame_info to
421 find the next frame, but that would be quadratic
422 since get_frame_info must scan all the way from the current frame.
423 The following algotithm is linear. */
424 if (*level_offset_ptr
< 0)
426 /* First put frame1 at innermost frame
427 and frame2 N levels up from there. */
428 frame1
= get_current_frame ();
430 while (*level_offset_ptr
< 0 && frame2
!= frame
)
432 frame2
= get_prev_frame (frame2
);
433 (*level_offset_ptr
) ++;
435 /* Then slide frame1 and frame2 up in synchrony
436 and when frame2 reaches our starting point
437 frame1 must be N levels down from there. */
438 while (frame2
!= frame
)
440 frame1
= get_prev_frame (frame1
);
441 frame2
= get_prev_frame (frame2
);
448 /* The "frame" command. With no arg, print selected frame briefly.
449 With arg LEVEL, select the frame at level LEVEL and print it.
450 With arg larger than 100000, use it as address of frame to select.
451 If from command file or user-defined command, don't print anything
452 if we have an argument. */
455 frame_command (level_exp
, from_tty
)
460 register FRAME frame
;
461 unsigned int level
, level1
;
465 level1
= level
= parse_and_eval_address (level_exp
);
468 select_frame (level
, -1);
473 frame
= find_relative_frame (get_current_frame (), &level1
);
475 error ("Stack level %d is out of range.", level
);
476 select_frame (frame
, level
);
481 print_stack_frame (selected_frame
, selected_frame_level
, 1);
484 /* Select the frame up one or COUNT stack levels
485 from the previously selected frame, and print it briefly. */
488 up_command (count_exp
)
491 register FRAME frame
;
492 int count
= 1, count1
;
494 count
= parse_and_eval_address (count_exp
);
497 frame
= find_relative_frame (selected_frame
, &count1
);
498 if (count1
!= 0 && count_exp
== 0)
499 error ("Initial frame selected; you cannot go up.");
500 select_frame (frame
, selected_frame_level
+ count
- count1
);
502 print_stack_frame (selected_frame
, selected_frame_level
, 1);
505 /* Select the frame down one or COUNT stack levels
506 from the previously selected frame, and print it briefly. */
509 down_command (count_exp
)
512 register FRAME frame
;
513 int count
= -1, count1
;
515 count
= - parse_and_eval_address (count_exp
);
518 frame
= find_relative_frame (selected_frame
, &count1
);
519 if (count1
!= 0 && count_exp
== 0)
520 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
521 select_frame (frame
, selected_frame_level
+ count
- count1
);
523 print_stack_frame (selected_frame
, selected_frame_level
, 1);
527 return_command (retval_exp
, from_tty
)
531 struct symbol
*thisfun
= get_frame_function (selected_frame
);
533 /* If interactive, require confirmation. */
539 if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun
)))
540 error ("Not confirmed.");
543 if (!query ("Make selected stack frame return now? "))
544 error ("Not confirmed.");
547 /* Do the real work. Pop until the specified frame is current. */
549 while (selected_frame
!= get_current_frame ())
552 /* Then pop that frame. */
556 /* Compute the return value (if any) and store in the place
557 for return values. */
560 set_return_value (parse_and_eval (retval_exp
));
562 /* If interactive, print the frame that is now current. */
565 frame_command ("0", 1);
571 add_com ("return", class_stack
, return_command
,
572 "Make selected stack frame return to its caller.\n\
573 Control remains in the debugger, but when you continue\n\
574 execution will resume in the frame above the one now selected.\n\
575 If an argument is given, it is an expression for the value to return.");
577 add_com ("up", class_stack
, up_command
,
578 "Select and print stack frame that called this one.\n\
579 An argument says how many frames up to go.");
581 add_com ("down", class_stack
, down_command
,
582 "Select and print stack frame called by this one.\n\
583 An argument says how many frames down to go.");
584 add_com_alias ("do", "down", class_stack
, 1);
586 add_com ("frame", class_stack
, frame_command
,
587 "Select and print a stack frame.\n\
588 With no argument, print the selected stack frame. (See also \"info frame\").\n\
589 An argument specifies the frame to select.\n\
590 It can be a stack frame number or the address of the frame.\n\
591 With argument, nothing is printed if input is coming from\n\
592 a command file or a user-defined command.");
594 add_com_alias ("f", "frame", class_stack
, 1);
596 add_com ("backtrace", class_stack
, backtrace_command
,
597 "Print backtrace of all stack frames, or innermost COUNT frames.");
598 add_com_alias ("bt", "backtrace", class_stack
, 0);
599 add_com_alias ("where", "backtrace", class_alias
, 0);
600 add_info ("stack", backtrace_command
,
601 "Backtrace of the stack, or innermost COUNT frames.");
602 add_info_alias ("s", "stack", 1);
603 add_info ("frame", frame_info
,
604 "All about selected stack frame, or frame at ADDR.");
605 add_info_alias ("f", "frame", 1);
606 add_info ("locals", locals_info
,
607 "Local variables of current stack frame.");
608 add_info ("args", args_info
,
609 "Argument variables of current stack frame.");
This page took 0.04634 seconds and 5 git commands to generate.