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.
- Place a break point on line 184 (the while loop).
You may have to scroll to display this line. Click on
line 184 (or alternatively highlight the number 184 by clicking and dragging
on it) so that the line number is displayed at the bottom left of
the source code window, click on Break(), and a stop sign
should appear to the left of 184.
- Click the Run button again.
A popup window will appear saying "The program being debugged has been
started already. Start it from the beginning?". Click on Yes.
The program will run until it reaches the line of code where the break
point was set.
-
Look in the Display window.
Note that the value of current in the display window is
different. In the Display window, click on the word
current so that the node is highlighted,
then click on Display*() in the Display window. This shows
a diagram of what *current is (the node current points
to.)
- Click on stock = {...} in the
*current diagram. Then click on Show() to
show the stock. You can drag this larger box around with the mouse
button. Pull it to the right so you can still see current.
What is the value of mySymbol?
Write
the value down.
- Now click on the Next button in the button
window. This executes the program one step at a time.
Press Next several times, but stop before executing the
statement at line 188 which causes the segmentation fault.
-
You will now display the value of newNode.
Highlight newNode, click on Display(), then
use Display*() and Show() to find the symbol that
should be added to the list of stocks. Write the symbol down.
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.
Useful Buttons in Source Code Window
- Break: Set a break point at current line
- Clear: Clear a break point at current line.
- Print: Print the value of current variable.
- Find << Search backwards for current text.
- Find >> Search forwards for current text.
- Run: Start Program from beginning until it ends or hits the first
break point.
- Step: Step into function. Run the next line of code. If the code
is a function call go to first statement in the function call
- Next: Go to Next line. Similar to Step, but if the line is a
function call, Next will run the entire function and stop at the next
line of code in the current function to be excuted.
- Continue: Continue running the program from current spot until it
hits the next break point or ends.
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
- Display*(): See where a pointer points to
- Show/Hide: Show the value of the variable/object or hide it.
Toggles the display ability.
- Delete: Delete the object that is being displayed.
To find out more, click on the Help button on the menu bar and
consult the DDD manual.