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.

  1. 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.
  2. 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.
  3. 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.

Step 5: Viewing the values of variables

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