CPSC 225, 8 February 2002:
Using the kdbg Debugger

On February 8, you will spend most of the class in the Lansing 310 computer lab, learning about the kdbg debugger. A debugger is a program that can control another program. At a minimum, it should let you stop the program at any desired point and inspect the current values of variables. The kdbg program is a debugger with a graphical user interface.

In Linux, to use a debugger with a C++ program, the program must be compiled with the "-g" option. This option adds extra information to the executable program. This information must be present for the debugger to function properly. (Note that a debugger is for compiled programs only -- it can't help you with syntax errors.)


To start debugging a program, you can either

Assuming that the program was compiled with the "-g" option, and that the source code file is still available, you will see the source code in kdbg's main window.

As an example, select the selection_sort program from the directory /home/cs225/debug.

To start debugging, you should first add a "breakpoint" to the program. To do this, click in the left-hand margin of the source code next to an executable statement. (A breakpoint on a non-executable line such as a variable declaration or a curly bracket will have no effect.) A red dot should appear. You can remove a breakpoint by clicking the red dot. When you run the program, it will pause when it gets to the breakpoint. Try setting a breakpoint on the line that reads srand(time(0)).

You can start the program running by hitting F5, or by choosing the "Run" command from the "Execution" menu. Try it. The computer should pause at the breakpoint. Also, a window named "Program Output" will appear. This window is used by the program for both input (cin) and output (cout).

To use the debugger effectively, you will want to open at least one or two more windows. These windows can be opened using the "View" menu. Select "Locals" from this menu, and arrange the new window on the screen. (Any windows that you leave open when you quit the debugger will be there the next time you start it.) The Locals window lets you view the values of local variables. In this case, it shows the array, A. There is a plus sign next to the variable name. If you click this, the array will be opened, so you can see the number in each array location.

At this point, you can start stepping through the program using commands in the "Execution" menu. Here are the most useful commands:

Note that you can also execute these commands by clicking on toolbar icons. Try it. Step through the selection_sort program and see how the local variables change. When a step changes the value of a variable, that value is shown in red.

During class, I would like you to look at the other executable programs in /home/cs225/debug:


To fully appreciate the debugger, you need to know what some of its other windows are for.

The Stack window is useful when the program is in a function. This window shows a list of the functions that have been called. (There can be more than one, because one function can call another.) If you click on one of the functions in this list, the "Locals" window will change to show the variables in that function, instead of the variables in the current function.

The Watched Expressions window lets you type in an expression. The value of the expression will be displayed and will change as you step through the program. The only way I have found of tracking the value of a global variable is to type it into this window.

The Breakpoints window lets you view the breakpoints that have been set in the program. More important, it lets you make the breakpoint "Conditional". A conditional breakpoint will only pause the program if some condition is true. For example, you might suspect that the first 100 iterations of a for loop work fine, but there are problems after that. Rather than step through the loop 100 times, you can add a breakpoint to the loop and attach the condition "index >100", where index is the loop control variable. This breakpoint will have no effect until the value of index exceeds 100. To make a breakpoint conditional, just select the breakpoint in the list and click the "Conditional" button.

The Memory button lets you view part of the computer's memory. Enter an address in the box at the top of the window. Usually, the address is given in the form &var where var is a variable name, or &func where func is a function name, or ptr where ptr is the name of a pointer variable. An array parameter name is treated as a pointer, so if you want to see the contents of an array parameter, you can use the Memory window and enter the array parameter name. You can view the contents of memory in several different formats. If the right format is not selected automatically, right-click on the Memory window to get a pop-up menu of possible formats.