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 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 Illegal HTML tag removed : stack frame. The stack frames are allocated in a region of memory called the Illegal HTML tag removed : call stack.

When your program stops, the DDD commands for examining the stack allow you to see all of this information.

One of the stack frames is Illegal HTML tag removed : selected by DDD and many DDD commands refer implicitly to the selected frame. In particular, whenever you ask DDD for the value of a variable in your program, the value is found in the selected frame. There are special DDD commands to select whichever frame you are interested in.

  • Frames: Callers and callees.
  • Backtraces: And you may ask yourself, `how did I get here?'
  • Selecting a frame: Moving up and down.

Node:Frames, Next:Backtraces, Up:Stack

Stack Frames

The call stack is divided up into contiguous pieces called Illegal HTML tag removed : stack frames, or Illegal HTML tag removed : 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 function's 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 Illegal HTML tag removed : initial frame or the Illegal HTML tag removed : 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 Illegal HTML tag removed : 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 Illegal HTML tag removed : 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.

Node:Backtraces, Next:Selecting a frame, Previous:Frames, Up:Stack

Backtraces

DDD provides a Illegal HTML tag removed : backtrace window showing 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.

To enable the backtrace window, select Status => Backtrace.

PICS/ddd-backtrace.jpg

Using GDB, each line in the backtrace shows the frame number and the function name. The program counter value is also shown--unless you use the GDB command set print address off. 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.

Node:Selecting a frame, Previous:Backtraces, Up:Stack

Selecting a Frame

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.25

In the backtrace window, you can select an arbitrary frame to move from one stack frame to another. Just click on the desired frame.

The Up button selects the function that called the current one--that is, it moves one frame up.

The Down button selects the function that was called by the current one--that is, it moves one frame down.

You can also directly type the up and down commands at the debugger prompt. Typing <Ctrl+Up> and <Ctrl+Down>, respectively, will also move you through the stack.

Up and Down actions can be undone via Edit =&gt; Undo.

Node:Undoing Program Execution, Next:Threads, Previous:Stack, Up:Running

results matching ""

    No results matching ""