Debugging with GDB (February 2008)

Table Of Contents
Chapter 6: Examining the Stack 51
6 Examining the Stack
When your program has stopped, the first thing you need to know is where it stopped
and how it got there.
Each time your program performs a function call, information about the call is gener-
ated.The 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 stack frame. The stack frames are allocated in a region of memory
called the call stack.
The GDB commands for examining the stack allow you to view all of this information.
6.1 Stack frames
The call stack is divided up into contiguous pieces called stack frames, or frames for
short; each frame is the data associated with one call to one function. The frame contains
the arguments given to the function, the local variables, and the address at which the
function is executing.
When your program is started, the stack has only one frame, that of the function main.
This is called the initial frame or the outermost 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 innermost
frame. This is the most recently created of all the stack frames that still exist.
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 frame pointer register while execution is going
on in that frame.
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.
One of the stack frames is selected by GDB and the 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 6.5 [Selecting a frame], page 53.
When your program stops, GDB automatically selects the currently executing frame and
describes it briefly, similar to the frame command (see Section 6.6 [Information about a
frame], page 54).
6.2 Stacks Without frames
Some compilers provide a way to compile functions so that they operate without stack
frames. (For example, the gcc option