Debugging: Using ddd


To start the debugger, type

ddd usestock & from an xterm window. Three windows will pop up on your machine. One will be the console window, one will be a window of buttons, and the other window will show the source code usestock.cc. The windows may take a long time to pop up, so be patient.

You should run the program: either type run inside the console window, or click on the run button (in the button window) ddd will run the program and stop when the segmentation fault occurs. There will be an arrow (kind of greenish in color) on the left side of the source code window pointing at the line number that is causing the segementation fault. This information, the name of the file, and the line number is printed in the DDD console window (the console window is running the gdb debugger, ddd is a graphical front end to gdb. You can run gdb without the GUI front end if you're on a computer without the ability to display X windows.)

Displaying items with ddd

To start finding the error, first click on current in the source code window so that it is highlighted. Then click the Display() button. This should pop up the Data Window with a graphical picture of current. Note that the the value of current is shown as 0x0 which is NULL. Thus trying to dereference to get current->next generates a segmentation fault. To trace execution and find the error follow the steps below. Even if you already know what the problem is you should follow these steps to get practice with the debugger.

Fixing the Code

The problem is that to add a new node in order, the code needs to look one node ahead. This is shown in the Tapestry book on page 614 in the function WordList::Update(). Rather than use the loop test below while (current && current->stock.GetSymbolsymbol() <= symbol) you'll need to change to look one node ahead (see the book).

You'll need to modify the code after the loop to link newNode into the list (this will require two statements!). You should start up emacs, make the changes that are necessary (think about this!!, don't just type at random), then recompile. You can reload the source code into ddd from the Source menu. If you run the program from within ddd the newly compiled code will be loaded and run. You may need several iterations of edit, compile, debug to get the program working. You can either step through with the debugger or you can remove the break point (use Clear() to remove a break point) and try the print option when the menu of choices appears in the ddd command window. There are LOTS of stocks, but the last stock should have symbol ZRN. You can also use the option of printing stocks starting with a give letter.

Summary of DDD buttons/commands

Useful Buttons in Source Code Window

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

Frequently Used Buttons in Data Window

To find out more, click on the Help button on the menu bar and consult the DDD manual.