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 /* Nonzero means print the full filename and linenumber
43 when a frame is printed, and do so in a format programs can parse. */
45 int frame_file_full_name
= 0;
47 static void select_calling_frame ();
49 void print_frame_info ();
51 /* Print a stack frame briefly. FRAME should be the frame address
52 and LEVEL should be its level in the stack (or -1 for level not defined).
53 This prints the level, the function executing, the arguments,
54 and the file name and line number.
55 If the pc is not at the beginning of the source line,
56 the actual pc is printed at the beginning.
58 If SOURCE is 1, print the source line as well.
59 If SOURCE is -1, print ONLY the source line. */
62 print_stack_frame (frame
, level
, source
)
69 fi
= get_frame_info (frame
);
71 print_frame_info (&fi
, level
, source
, 1);
75 print_frame_info (fi
, level
, source
, args
)
76 struct frame_info
*fi
;
81 register FRAME frame
= fi
->frame
;
82 struct symtab_and_line sal
;
84 register char *funname
= 0;
87 sal
= find_pc_line (fi
->pc
, fi
->next_frame
);
88 func
= get_frame_function (frame
);
90 funname
= SYMBOL_NAME (func
);
93 register int misc_index
= find_pc_misc_function (fi
->pc
);
95 funname
= misc_function_vector
[misc_index
].name
;
98 if (source
>= 0 || !sal
.symtab
)
100 /* This avoids a bug in cc on the sun. */
101 struct frame_info tem
;
105 printf ("#%-2d ", level
);
106 if (fi
->pc
!= sal
.pc
|| !sal
.symtab
)
107 printf ("0x%x in ", fi
->pc
);
108 printf ("%s (", funname
? funname
: "??");
111 FRAME_NUM_ARGS (numargs
, tem
);
112 print_frame_args (func
, FRAME_ARGS_ADDRESS (tem
), numargs
, stdout
);
116 printf (" (%s line %d)", sal
.symtab
->filename
, sal
.line
);
120 if (source
!= 0 && sal
.symtab
)
123 if (source
< 0 && fi
->pc
!= sal
.pc
)
124 printf ("0x%x\t", fi
->pc
);
125 if (frame_file_full_name
)
126 done
= identify_source_line (sal
.symtab
, sal
.line
);
128 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1);
129 current_source_line
= max (sal
.line
- 5, 1);
132 set_default_breakpoint (1, fi
->pc
, sal
.symtab
, sal
.line
);
137 /* Call here to print info on selected frame, after a trap. */
140 print_sel_frame (just_source
)
143 print_stack_frame (selected_frame
, -1, just_source
? -1 : 1);
146 /* Print info on the selected frame, including level number
149 print_selected_frame ()
151 print_stack_frame (selected_frame
, selected_frame_level
, 0);
154 /* Print verbosely the selected frame or the frame at address ADDR.
155 This means absolutely all information in the frame is printed. */
158 frame_info (addr_exp
)
161 FRAME frame
= addr_exp
? parse_and_eval_address (addr_exp
) : selected_frame
;
162 struct frame_info fi
;
163 struct frame_saved_regs fsr
;
164 struct symtab_and_line sal
;
171 fi
= get_frame_info (frame
);
172 get_frame_saved_regs (&fi
, &fsr
);
173 sal
= find_pc_line (fi
.pc
, fi
.next_frame
);
174 func
= get_frame_function (frame
);
176 funname
= SYMBOL_NAME (func
);
179 register int misc_index
= find_pc_misc_function (fi
.pc
);
181 funname
= misc_function_vector
[misc_index
].name
;
183 calling_frame
= get_prev_frame (frame
);
185 if (!addr_exp
&& selected_frame_level
>= 0)
186 printf ("Stack level %d, frame at 0x%x:\n pc = 0x%x",
187 selected_frame_level
, frame
, fi
.pc
);
189 printf ("Stack frame at 0x%x:\n pc = 0x%x",
193 printf (" in %s", funname
);
195 printf (" (%s line %d)", sal
.symtab
->filename
, sal
.line
);
196 printf ("; saved pc 0x%x\n", FRAME_SAVED_PC (frame
));
198 printf (" called by frame at 0x%x", calling_frame
);
199 if (fi
.next_frame
&& calling_frame
)
202 printf (" caller of frame at 0x%x", fi
.next_frame
);
203 if (fi
.next_frame
|| calling_frame
)
205 printf (" Arglist at 0x%x,", FRAME_ARGS_ADDRESS (fi
));
206 FRAME_NUM_ARGS (i
, fi
);
210 printf (" no args.");
214 printf (" %d args: ", i
);
216 FRAME_NUM_ARGS (numargs
, fi
);
217 print_frame_args (func
, FRAME_ARGS_ADDRESS (fi
), numargs
, stdout
);
220 for (i
= 0; i
< NUM_REGS
; i
++)
228 printf (" Saved registers:");
231 printf ("%s at 0x%x", reg_names
[i
], fsr
.regs
[i
]);
238 /* Print briefly all stack frames or just the innermost COUNT frames. */
241 backtrace_command (count_exp
)
244 struct frame_info fi
;
246 register FRAME frame
;
250 count
= parse_and_eval_address (count_exp
);
254 for (i
= 0, frame
= get_current_frame (), fi
= get_frame_info (frame
);
256 i
++, fi
= get_prev_frame_info (fi
.frame
), frame
= fi
.frame
)
259 print_frame_info (&fi
, i
, 0, 1);
263 /* Print the local variables of a block B active in FRAME. */
266 print_block_frame_locals (b
, frame
, stream
)
268 register FRAME frame
;
269 register FILE *stream
;
273 register struct symbol
*sym
;
275 nsyms
= BLOCK_NSYMS (b
);
277 for (i
= 0; i
< nsyms
; i
++)
279 sym
= BLOCK_SYM (b
, i
);
280 if (SYMBOL_CLASS (sym
) == LOC_LOCAL
281 || SYMBOL_CLASS (sym
) == LOC_REGISTER
)
283 fprintf (stream
, "%s = ", SYMBOL_NAME (sym
));
284 print_variable_value (sym
, frame
, stream
);
285 fprintf (stream
, "\n");
291 /* Print on STREAM all the local variables in frame FRAME,
292 including all the blocks active in that frame
295 Returns 1 if the job was done,
296 or 0 if nothing was printed because we have no info
297 on the function running in FRAME. */
300 print_frame_local_vars (frame
, stream
)
301 register FRAME frame
;
302 register FILE *stream
;
304 register struct block
*block
= get_frame_block (frame
);
309 print_block_frame_locals (block
, frame
, stream
);
310 /* After handling the function's top-level block, stop.
311 Don't continue to its superblock, the block of
313 if (BLOCK_FUNCTION (block
))
315 block
= BLOCK_SUPERBLOCK (block
);
323 print_frame_local_vars (selected_frame
, stdout
);
327 print_frame_arg_vars (frame
, stream
)
328 register FRAME frame
;
329 register FILE *stream
;
331 struct symbol
*func
= get_frame_function (frame
);
332 register struct block
*b
;
335 register struct symbol
*sym
;
340 b
= SYMBOL_BLOCK_VALUE (func
);
341 nsyms
= BLOCK_NSYMS (b
);
343 for (i
= 0; i
< nsyms
; i
++)
345 sym
= BLOCK_SYM (b
, i
);
346 if (SYMBOL_CLASS (sym
) == LOC_ARG
)
348 fprintf (stream
, "%s = ", SYMBOL_NAME (sym
));
349 print_variable_value (sym
, frame
, stream
);
350 fprintf (stream
, "\n");
361 print_frame_arg_vars (selected_frame
, stdout
);
364 /* Select frame FRAME, and note that its stack level is LEVEL.
365 LEVEL may be -1 if an actual level number is not known. */
368 select_frame (frame
, level
)
372 selected_frame
= frame
;
373 selected_frame_level
= level
;
376 /* Store the selected frame and its level into *FRAMEP and *LEVELP. */
379 record_selected_frame (framep
, levelp
)
383 *framep
= selected_frame
;
384 *levelp
= selected_frame_level
;
387 /* Return the symbol-block in which the selected frame is executing.
388 Can return zero under various legitimate circumstances. */
391 get_selected_block ()
393 if (!have_inferior_p () && !have_core_file_p ())
397 return get_current_block ();
398 return get_frame_block (selected_frame
);
401 /* Find a frame a certain number of levels away from FRAME.
402 LEVEL_OFFSET_PTR points to an int containing the number of levels.
403 Positive means go to earlier frames (up); negative, the reverse.
404 The int that contains the number of levels is counted toward
405 zero as the frames for those levels are found.
406 If the top or bottom frame is reached, that frame is returned,
407 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
408 how much farther the original request asked to go. */
411 find_relative_frame (frame
, level_offset_ptr
)
412 register FRAME frame
;
413 register int* level_offset_ptr
;
416 struct frame_info fi
;
417 register FRAME frame1
, frame2
;
419 /* Going up is simple: just do get_prev_frame enough times
420 or until initial frame is reached. */
421 while (*level_offset_ptr
> 0)
423 prev
= get_prev_frame (frame
);
426 (*level_offset_ptr
)--;
429 /* Going down could be done by iterating get_frame_info to
430 find the next frame, but that would be quadratic
431 since get_frame_info must scan all the way from the current frame.
432 The following algotithm is linear. */
433 if (*level_offset_ptr
< 0)
435 /* First put frame1 at innermost frame
436 and frame2 N levels up from there. */
437 frame1
= get_current_frame ();
439 while (*level_offset_ptr
< 0 && frame2
!= frame
)
441 frame2
= get_prev_frame (frame2
);
442 (*level_offset_ptr
) ++;
444 /* Then slide frame1 and frame2 up in synchrony
445 and when frame2 reaches our starting point
446 frame1 must be N levels down from there. */
447 while (frame2
!= frame
)
449 frame1
= get_prev_frame (frame1
);
450 frame2
= get_prev_frame (frame2
);
457 /* The "frame" command. With no arg, print selected frame briefly.
458 With arg LEVEL, select the frame at level LEVEL and print it.
459 With arg larger than 100000, use it as address of frame to select.
460 If from command file or user-defined command, don't print anything
461 if we have an argument. */
464 frame_command (level_exp
, from_tty
)
469 register FRAME frame
;
470 unsigned int level
, level1
;
474 level1
= level
= parse_and_eval_address (level_exp
);
477 select_frame (level
, -1);
482 frame
= find_relative_frame (get_current_frame (), &level1
);
484 error ("Stack level %d is out of range.", level
);
485 select_frame (frame
, level
);
490 print_stack_frame (selected_frame
, selected_frame_level
, 1);
493 /* Select the frame up one or COUNT stack levels
494 from the previously selected frame, and print it briefly. */
497 up_command (count_exp
)
500 register FRAME frame
;
501 int count
= 1, count1
;
503 count
= parse_and_eval_address (count_exp
);
506 frame
= find_relative_frame (selected_frame
, &count1
);
507 if (count1
!= 0 && count_exp
== 0)
508 error ("Initial frame selected; you cannot go up.");
509 select_frame (frame
, selected_frame_level
+ count
- count1
);
511 print_stack_frame (selected_frame
, selected_frame_level
, 1);
514 /* Select the frame down one or COUNT stack levels
515 from the previously selected frame, and print it briefly. */
518 down_command (count_exp
)
521 register FRAME frame
;
522 int count
= -1, count1
;
524 count
= - parse_and_eval_address (count_exp
);
527 frame
= find_relative_frame (selected_frame
, &count1
);
528 if (count1
!= 0 && count_exp
== 0)
529 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
530 select_frame (frame
, selected_frame_level
+ count
- count1
);
532 print_stack_frame (selected_frame
, selected_frame_level
, 1);
536 return_command (retval_exp
, from_tty
)
540 struct symbol
*thisfun
= get_frame_function (selected_frame
);
542 /* If interactive, require confirmation. */
548 if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun
)))
549 error ("Not confirmed.");
552 if (!query ("Make selected stack frame return now? "))
553 error ("Not confirmed.");
556 /* Do the real work. Pop until the specified frame is current. */
558 while (selected_frame
!= get_current_frame ())
561 /* Then pop that frame. */
565 /* Compute the return value (if any) and store in the place
566 for return values. */
569 set_return_value (parse_and_eval (retval_exp
));
571 /* If interactive, print the frame that is now current. */
574 frame_command ("0", 1);
580 add_com ("return", class_stack
, return_command
,
581 "Make selected stack frame return to its caller.\n\
582 Control remains in the debugger, but when you continue\n\
583 execution will resume in the frame above the one now selected.\n\
584 If an argument is given, it is an expression for the value to return.");
586 add_com ("up", class_stack
, up_command
,
587 "Select and print stack frame that called this one.\n\
588 An argument says how many frames up to go.");
590 add_com ("down", class_stack
, down_command
,
591 "Select and print stack frame called by this one.\n\
592 An argument says how many frames down to go.");
593 add_com_alias ("do", "down", class_stack
, 1);
595 add_com ("frame", class_stack
, frame_command
,
596 "Select and print a stack frame.\n\
597 With no argument, print the selected stack frame. (See also \"info frame\").\n\
598 An argument specifies the frame to select.\n\
599 It can be a stack frame number or the address of the frame.\n\
600 With argument, nothing is printed if input is coming from\n\
601 a command file or a user-defined command.");
603 add_com_alias ("f", "frame", class_stack
, 1);
605 add_com ("backtrace", class_stack
, backtrace_command
,
606 "Print backtrace of all stack frames, or innermost COUNT frames.");
607 add_com_alias ("bt", "backtrace", class_stack
, 0);
608 add_com_alias ("where", "backtrace", class_alias
, 0);
609 add_info ("stack", backtrace_command
,
610 "Backtrace of the stack, or innermost COUNT frames.");
611 add_info_alias ("s", "stack", 1);
612 add_info ("frame", frame_info
,
613 "All about selected stack frame, or frame at ADDR.");
614 add_info_alias ("f", "frame", 1);
615 add_info ("locals", locals_info
,
616 "Local variables of current stack frame.");
617 add_info ("args", args_info
,
618 "Argument variables of current stack frame.");
This page took 0.043309 seconds and 5 git commands to generate.