CPS 100, Sep. 1, 2002

CPS 100 - Fall 2002 - Rodger

Sep. 1, 2002
Debugging

This tutorial has both ddd and gdb. I haven't tried the ddd yet (it's interface might have changed slightly if there is a new version) but I did try the gdb which is below. You can do either or both.

Setup

For this tutorial, you will need to create a directory called debug in your cps100 directory. Type the following commands:

  cd cps100
  mkdir debug
  cd debug

You should be in your debug directory. Type pwd just to make sure.

Next you need to copy files for the tutorial Type the following (don't forget the dot):

   cp ~rodger/cps100/debug/*  .

Type the command ls to list the files. You should see the following files:


Part 1: Debugging with DDD

When you can't figure out what is wrong with your program, a debugger is useful for "watching" the execution of your program to see what it is doing, and to check the values of variables. In this part of the lab you'll learn how to use the debugger ddd to find errors in the program arith.cpp.

First compile arith using make. Then to start the debugger, type

ddd arith & from an xterm window. Two windows will pop up on your machine. One will be a window of buttons, and the other has the source code for arith.cpp and the console window below the source code. The windows may take a long time to pop up, so be patient.

You should run the program: either type run at the bottom of the console window at the (gdb) prompt, or click on the run button (in the button window) ddd will run the program. In the console it will ask for input. Type in the input and you will see that the output is not correct. Do the following:

Summary of DDD buttons/commands

Useful Buttons

For more information, you can click on the Help option in the menu and look it up in the ddd manual.

NOTE: Occasionally code will appear in the console window from a library file, just click on "cont" to go back to your program. You may have to put in break points to focus on the parts of your program that are not working.


Part 2: Debugging with GDB

DDD is a visual debugger and sometimes takes a long time. You can also run gdb, a textual based debugger that runs faster, but doesn't have a visual interface. (ddd actually uses gdb, so the commands are similar).

To run gdb on the above file just type: gdb arith

Note, if you modified it and did the ddd tutorial, just use the cp command again to copy over it, and then the make command again to compile it.

First compile arith using make. Then to start the debugger, type

gdb arith

You should run the program: type run at the (gdb) prompt

Type in the input and you will see that the output is not correct. Do the following: