Debugging Tutorial
The purpose of this tutorial is to get you started debugging C programs
using the DDD debugger.
Step 1: Compiling for debugging
The program you wish to debug must be compiled with the
-g option. The debugger needs extra information
(symbol table) from the compiler to associate machine instructions
with source code lines.
We will use the sample program provided in the DDD
documentation as a running example. So, make a copy of sample.c and compile it as follows:
gcc -g -o sample sample.c
Step 2: Starting DDD
You can start DDD by simply typing ddd at the command line.
Then to load the executable select File->Open Program and
navigate to the executable file you want to debug (sample in
our example).
Alternatively, you can start DDD by typing ddd sample
(assuming sample is in the current directory).
At this point you should see the source code for sample.c
Step 3: Setting a breakpoint
One of the most common reasons to use a debugger is to step through the
code one line at at time to see exactly how variables are changing.
The next thing we need to do is to tell DDD where we want to stop
when running the program. This is called a breakpoint
There are several ways to set a breakpoint.
- Left click on the line of code you want to stop on, then click on the
stop sign button labeled Break in the tool bar. A stop sign should
appear on the line of code you positioned the cursor on.
- Position the mouse pointer over the line of code you want to stop on
and hold the right mouse button down. You should see a menu in
which you can select the "Set breakpoint" entry. Again, a stop sign
should appear on the appropriate line of code.
- Select the name of a function and click on the stop sign in the tool
bar. A stop sign will appear beside the first executable line in
that function.
Step 4: Running the program
This one is easy: click the Run button on the detached tool bar. The
program will be run until the first breakpoint is reached. An arrow
will appear beside the line that is to be executed next.
You can step through the program using the buttons on the detached
tool bar or in the Program menu.
- Next Execute the next line of code stepping over
functions.
- Step Execute the next line of code. If a function call,
step into the function.
- Cont Continue executing the program until the next
breakpoint is hit or the program terminates.
Step 5: Viewing the values of variables
- Roll the mouse over a variable and a box appears with the value of
the variable in it.
- You can display the value of a variable by holding the right
mouse button down and selecting the Display line. A new
region of the main window appears with a box contain the name and
value of the variable you selected. You can drag the boxes around
in this window.
- If the variable you want to view is a pointer, you can display the
dereferenced variable, by selecting Display *.
- You can view an element of an array, say a[i] by dragging the
mouse across the whole variable and clicking on the display
button.
- You can also display something by typing the variable into the box on
the tool bar and clicking on the display button.
The Test
Find the bugs in the sample program by using the debugger. First you need
to figure out what the program is supposed to do, and run the program
to see what it is getting wrong. Hint: the DDD Reference in the Help
menu walks you through finding the bug.
P.S.
This tutorial was written rather quickly. If you find errors, or if
you think I have missed something important, please let me know.
Last modified: Thu Sep 13 19:32:55 EDT 2001