gdb_7.html   [plain text]


<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from /mnt/apple/gdb/source/gdb.apple/source/gdb/gdb/doc/gdb.texinfo on 23 November 1999 -->

<TITLE>Debugging with GDB - Examining the Stack</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_6.html">previous</A>, <A HREF="gdb_8.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC40" HREF="gdb_toc.html#TOC40">Examining the Stack</A></H1>

<P>
When your program has stopped, the first thing you need to know is where it
stopped and how it got there.

</P>
<P>
<A NAME="IDX188"></A>
Each time your program performs a function call, information about the call 
is generated. 
That information includes the location of the call in your program, 
the arguments of the call, 
and the local variables of the function being called.
The information is saved in a block of data called a <STRONG>stack frame</STRONG>.  
The stack frames are allocated in a region of memory called the <STRONG>call
stack</STRONG>.

</P>
<P>
When your program stops, the GDB commands for examining the
stack allow you to see all of this information.

</P>
<P>
<A NAME="IDX189"></A>
One of the stack frames is <STRONG>selected</STRONG> by GDB and many
GDB commands refer implicitly to the selected frame.  In
particular, whenever you ask GDB for the value of a variable in
your program, the value is found in the selected frame.  There are
special GDB commands to select whichever frame you are
interested in. See section <A HREF="gdb_7.html#SEC43">Selecting a frame</A>.

</P>
<P>
When your program stops, GDB automatically selects the
currently executing frame and describes it briefly, similar to the 
<CODE>frame</CODE> command (see section <A HREF="gdb_7.html#SEC44">Information about a frame</A>).

</P>



<H2><A NAME="SEC41" HREF="gdb_toc.html#TOC41">Stack frames</A></H2>

<P>
<A NAME="IDX190"></A>
<A NAME="IDX191"></A>
The call stack is divided up into contiguous pieces called <STRONG>stack
frames</STRONG>, or <STRONG>frames</STRONG> for short; each frame is the data associated
with one call to one function.  The frame contains the arguments given
to the function, the function's local variables, and the address at
which the function is executing.

</P>
<P>
<A NAME="IDX192"></A>
<A NAME="IDX193"></A>
<A NAME="IDX194"></A>
When your program is started, the stack has only one frame, that of the
function <CODE>main</CODE>.  This is called the <STRONG>initial</STRONG> frame or the
<STRONG>outermost</STRONG> frame.  Each time a function is called, a new frame is
made.  Each time a function returns, the frame for that function invocation
is eliminated.  If a function is recursive, there can be many frames for
the same function.  The frame for the function in which execution is
actually occurring is called the <STRONG>innermost</STRONG> frame.  This is the most
recently created of all the stack frames that still exist.

</P>
<P>
<A NAME="IDX195"></A>
Inside your program, stack frames are identified by their addresses.  A
stack frame consists of many bytes, each of which has its own address; each
kind of computer has a convention for choosing one byte whose
address serves as the address of the frame.  Usually this address is kept
in a register called the <STRONG>frame pointer register</STRONG> while execution is
going on in that frame.

</P>
<P>
<A NAME="IDX196"></A>
GDB assigns numbers to all existing stack frames, starting with
zero for the innermost frame, one for the frame that called it,
and so on upward.  These numbers do not really exist in your program;
they are assigned by GDB to give you a way of designating stack
frames in GDB commands.

</P>
<P>
<A NAME="IDX197"></A>
Some compilers provide a way to compile functions so that they operate
without stack frames.  (For example, the <CODE>gcc</CODE> option
<SAMP>`-fomit-frame-pointer'</SAMP> generates functions without a frame.)
This is occasionally done with heavily used library functions to save
the frame setup time.  GDB has limited facilities for dealing
with these function invocations.  If the innermost function invocation
has no stack frame, GDB nevertheless regards it as though
it had a separate frame, which is numbered zero as usual, allowing
correct tracing of the function call chain.  However, GDB has
no provision for frameless functions elsewhere in the stack.

</P>
<DL COMPACT>

<DT><CODE>frame <VAR>args</VAR></CODE>
<DD>
<A NAME="IDX198"></A>
 
The <CODE>frame</CODE> command allows you to move from one stack frame to another, 
and to print the stack frame you select.  <VAR>args</VAR> may be either the
address of the frame or the stack frame number.  Without an argument, 
<CODE>frame</CODE> prints the current stack frame.  

<A NAME="IDX199"></A>
<DT><CODE>select-frame</CODE>
<DD>
The <CODE>select-frame</CODE> command allows you to move from one stack frame
to another without printing the frame.  This is the silent version of
<CODE>frame</CODE>.
</DL>



<H2><A NAME="SEC42" HREF="gdb_toc.html#TOC42">Backtraces</A></H2>

<P>
<A NAME="IDX200"></A>
<A NAME="IDX201"></A>
<A NAME="IDX202"></A>
A backtrace is a summary of how your program got where it is.  It shows one
line per frame, for many frames, starting with the currently executing
frame (frame zero), followed by its caller (frame one), and on up the
stack.

</P>
<DL COMPACT>

<DT><CODE>backtrace</CODE>
<DD>
<A NAME="IDX203"></A>
 <A NAME="IDX204"></A>
 
<DT><CODE>bt</CODE>
<DD>
Print a backtrace of the entire stack: one line per frame for all
frames in the stack.

You can stop the backtrace at any time by typing the system interrupt
character, normally <KBD>C-c</KBD>.

<DT><CODE>backtrace <VAR>n</VAR></CODE>
<DD>
<DT><CODE>bt <VAR>n</VAR></CODE>
<DD>
Similar, but print only the innermost <VAR>n</VAR> frames.

<DT><CODE>backtrace -<VAR>n</VAR></CODE>
<DD>
<DT><CODE>bt -<VAR>n</VAR></CODE>
<DD>
Similar, but print only the outermost <VAR>n</VAR> frames.
</DL>

<P>
<A NAME="IDX205"></A>
<A NAME="IDX206"></A>
<A NAME="IDX207"></A>
The names <CODE>where</CODE> and <CODE>info stack</CODE> (abbreviated <CODE>info s</CODE>)
are additional aliases for <CODE>backtrace</CODE>.

</P>
<P>
Each line in the backtrace shows the frame number and the function name.
The program counter value is also shown--unless you use <CODE>set
print address off</CODE>.  The backtrace also shows the source file name and
line number, as well as the arguments to the function.  The program
counter value is omitted if it is at the beginning of the code for that
line number.

</P>
<P>
Here is an example of a backtrace.  It was made with the command
<SAMP>`bt 3'</SAMP>, so it shows the innermost three frames.

</P>

<PRE>
#0  m4_traceon (obs=0x24eb0, argc=1, argv=0x2b8c8) 
    at builtin.c:993
#1  0x6e38 in expand_macro (sym=0x2b600) at macro.c:242
#2  0x6840 in expand_token (obs=0x0, t=177664, td=0xf7fffb08)
    at macro.c:71
(More stack frames follow...)
</PRE>

<P>
The display for frame zero does not begin with a program counter
value, indicating that your program has stopped at the beginning of the
code for line <CODE>993</CODE> of <CODE>builtin.c</CODE>.

</P>


<H2><A NAME="SEC43" HREF="gdb_toc.html#TOC43">Selecting a frame</A></H2>

<P>
Most commands for examining the stack and other data in your program work on
whichever stack frame is selected at the moment.  Here are the commands for
selecting a stack frame; all of them finish by printing a brief description
of the stack frame just selected.

</P>
<DL COMPACT>

<DT><CODE>frame <VAR>n</VAR></CODE>
<DD>
<A NAME="IDX208"></A>
 <A NAME="IDX209"></A>
 
<DT><CODE>f <VAR>n</VAR></CODE>
<DD>
Select frame number <VAR>n</VAR>.  Recall that frame zero is the innermost
(currently executing) frame, frame one is the frame that called the
innermost one, and so on.  The highest-numbered frame is the one for
<CODE>main</CODE>.

<DT><CODE>frame <VAR>addr</VAR></CODE>
<DD>
<DT><CODE>f <VAR>addr</VAR></CODE>
<DD>
Select the frame at address <VAR>addr</VAR>.  This is useful mainly if the
chaining of stack frames has been damaged by a bug, making it
impossible for GDB to assign numbers properly to all frames.  In
addition, this can be useful when your program has multiple stacks and
switches between them.

On the SPARC architecture, <CODE>frame</CODE> needs two addresses to
select an arbitrary frame: a frame pointer and a stack pointer.

On the MIPS and Alpha architecture, it needs two addresses: a stack
pointer and a program counter.

On the 29k architecture, it needs three addresses: a register stack
pointer, a program counter, and a memory stack pointer.

<A NAME="IDX210"></A>
<DT><CODE>up <VAR>n</VAR></CODE>
<DD>
Move <VAR>n</VAR> frames up the stack.  For positive numbers <VAR>n</VAR>, this
advances toward the outermost frame, to higher frame numbers, to frames
that have existed longer.  <VAR>n</VAR> defaults to one.

<A NAME="IDX211"></A>
<A NAME="IDX212"></A>
<DT><CODE>down <VAR>n</VAR></CODE>
<DD>
Move <VAR>n</VAR> frames down the stack.  For positive numbers <VAR>n</VAR>, this
advances toward the innermost frame, to lower frame numbers, to frames
that were created more recently.  <VAR>n</VAR> defaults to one.  You may
abbreviate <CODE>down</CODE> as <CODE>do</CODE>.
</DL>

<P>
All of these commands end by printing two lines of output describing the
frame.  The first line shows the frame number, the function name, the
arguments, and the source file and line number of execution in that
frame.  The second line shows the text of that source line.  

</P>
<P>
For example:

</P>

<PRE>
(gdb) up
#1  0x22f0 in main (argc=1, argv=0xf7fffbf4, env=0xf7fffbfc)
    at env.c:10
10              read_input_file (argv[i]);
</PRE>

<P>
After such a printout, the <CODE>list</CODE> command with no arguments
prints ten lines centered on the point of execution in the frame.
See section <A HREF="gdb_8.html#SEC47">Printing source lines</A>.

</P>
<DL COMPACT>

<DT><CODE>up-silently <VAR>n</VAR></CODE>
<DD>
<A NAME="IDX213"></A>
 <A NAME="IDX214"></A>
 
<DT><CODE>down-silently <VAR>n</VAR></CODE>
<DD>
These two commands are variants of <CODE>up</CODE> and <CODE>down</CODE>,
respectively; they differ in that they do their work silently, without
causing display of the new frame.  They are intended primarily for use
in GDB command scripts, where the output might be unnecessary and
distracting.
</DL>



<H2><A NAME="SEC44" HREF="gdb_toc.html#TOC44">Information about a frame</A></H2>

<P>
There are several other commands to print information about the selected
stack frame.

</P>
<DL COMPACT>

<DT><CODE>frame</CODE>
<DD>
<DT><CODE>f</CODE>
<DD>
When used without any argument, this command does not change which
frame is selected, but prints a brief description of the currently
selected stack frame.  It can be abbreviated <CODE>f</CODE>.  With an
argument, this command is used to select a stack frame.
See section <A HREF="gdb_7.html#SEC43">Selecting a frame</A>.

<A NAME="IDX215"></A>
<A NAME="IDX216"></A>
<DT><CODE>info frame</CODE>
<DD>
<DT><CODE>info f</CODE>
<DD>
This command prints a verbose description of the selected stack frame,
including:


<UL>
<LI>

the address of the frame 
<LI>

the address of the next frame down (called by this frame)
<LI>

the address of the next frame up (caller of this frame)
<LI>

the language in which the source code corresponding to this frame is written
<LI>

the address of the frame's arguments
<LI>

the program counter saved in it (the address of execution in the caller frame)
<LI>

which registers were saved in the frame
</UL>

The verbose description is useful when
something has gone wrong that has made the stack format fail to fit
the usual conventions.

<DT><CODE>info frame <VAR>addr</VAR></CODE>
<DD>
<DT><CODE>info f <VAR>addr</VAR></CODE>
<DD>
Print a verbose description of the frame at address <VAR>addr</VAR>, without
selecting that frame.  The selected frame remains unchanged by this
command.  This requires the same kind of address (more than one for some
architectures) that you specify in the <CODE>frame</CODE> command.
See section <A HREF="gdb_7.html#SEC43">Selecting a frame</A>.

<A NAME="IDX217"></A>
<DT><CODE>info args</CODE>
<DD>
Print the arguments of the selected frame, each on a separate line.

<DT><CODE>info locals</CODE>
<DD>
<A NAME="IDX218"></A>
Print the local variables of the selected frame, each on a separate
line.  These are all variables (declared either static or automatic)
accessible at the point of execution of the selected frame.

<A NAME="IDX219"></A>
<A NAME="IDX220"></A>
<A NAME="IDX221"></A>
<DT><CODE>info catch</CODE>
<DD>
Print a list of all the exception handlers that are active in the
current stack frame at the current point of execution.  To see other
exception handlers, visit the associated frame (using the <CODE>up</CODE>,
<CODE>down</CODE>, or <CODE>frame</CODE> commands); then type <CODE>info catch</CODE>.
See section <A HREF="gdb_6.html#SEC31">Setting catchpoints</A>.
</DL>



<H2><A NAME="SEC45" HREF="gdb_toc.html#TOC45">MIPS/Alpha machines and the function stack</A></H2>

<P>
<A NAME="IDX222"></A>
<A NAME="IDX223"></A>
<A NAME="IDX224"></A>
<A NAME="IDX225"></A>
Alpha- and MIPS-based computers use an unusual stack frame, which
sometimes requires GDB to search backward in the object code to
find the beginning of a function.

</P>
<P>
<A NAME="IDX226"></A>
To improve response time (especially for embedded applications, where
GDB may be restricted to a slow serial line for this search)
you may want to limit the size of this search, using one of these
commands:

</P>
<DL COMPACT>

<DT><CODE>set heuristic-fence-post <VAR>limit</VAR></CODE>
<DD>
<A NAME="IDX227"></A>
 
Restrict GDB to examining at most <VAR>limit</VAR> bytes in its search
for the beginning of a function.  A value of <VAR>0</VAR> (the default)
means there is no limit.  However, except for <VAR>0</VAR>, the larger the
limit the more bytes <CODE>heuristic-fence-post</CODE> must search and 
therefore the longer it takes to run.

<DT><CODE>show heuristic-fence-post</CODE>
<DD>
Display the current limit.
</DL>

<P>
These commands are available <EM>only</EM> when GDB is configured
for debugging programs on Alpha or MIPS processors.

</P>

<P><HR><P>
Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_6.html">previous</A>, <A HREF="gdb_8.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>.
</BODY>
</HTML>