Using DDD


This simple exercise will hopefully make you more proficient at using DDD the Data Display Debugger. DDD provides an X front end to gdb, the GNU Debugger. The main advantage of using DDD over GDB is that one can visualize data using DDD; of course, another advantage is that one does not need to learn esoteric GDB commands (although this is not necessarily a bad thing). The advantage of visualization can be helpful in debugging your software because the program data is displayed through a presentable interface. This facilitates finding and eradicating bugs. Debugging.

After completing this tutorial, you should be able to:

Getting Started

In order to complete this exercise in using DDD, you'll need the following files, since you will use them to perform various steps in this sort of tutorial. For the first part of this tutorial, you need:
  For the second part of this tutorial, using the Bookinfo program, you need the following files: To get these files on acpub, change into your "cps100e" directory using the "cd" command and create another directory called "tutorial" using the "mkdir" command. Change into the "tutorial" directory. You can then copy the files to this directory by typing command (don't forget the trailing period, or dot):
       cp  ~rodger/cps100e/ddd/*  .

Once you have obtained the docount code (including the Makefile), and have looked over the code for these programs to see what they do, you're ready to get started using DDD. First, compile the code, type make docount and then make bookinfo

Now, start up ddd! Type ddd & at an xterm prompt and press enter (remember the '&' starts ddd in the background so you can still use the xterm).

Wait for three windows to pop up on screen. These are the main facilities of DDD. You should have one toolbar, one "Debugger Console", and the largest window should be blank. You'll want to load the compiled program into DDD so that you can start the debugging. Choose "open program" from the file menu of the Debugger Console window, and when the dialog pops up, choose the name of your program ("docount" in this case) from the list of files and then click on the Open button. DDD should then display the source code of the main function in the largest window. Now, you're ready to proceed.


The purpose of setting breakpoints is to be able to stop your program in the midst of execution to examine the current state of variables and data structures. Essentially, you're able to pause your program so that you can see what happens as it runs. The good thing about breakpoints is that it doesn't halt the program entirely; you can continue from where you set the breakpoint to finish program execution.

Now, you'll want to set a breakpoint. For no particular reason at all, let's choose line 16 of (this is the file that should already be loaded into the source code viewer window). We'll set our breakpoint at line 16 It's important to remember that DDD will break execution before it executes the line you set the breakpoint on. To set the breakpoint, move the mouse over the #16 that denotes the line number, and right click. A small menu should pop up. Choose the "set breakpoint" option. A tiny red stop sign will appear next to line 16. Now, DDD should break the program before anything is ever printed to cout. To see this in action, click on the green RUN button on the toolbar. The display in the Debugger console should say "Starting program ... " etc. Then, a line such as "Breakpoint 1, main() at" should appear. This means the breakpoint has been reached. Normal program output should be outputted to the debugger console, but note that nothing has appeared yet. This is because you set the breakpoint on line 16. Thus, the code on line 16 has not been executed.

The breakpoints you set can be deleted or disabled by right-clicking on the line just as before. Except this time, you'll choose either the "disable breakpoint" or "delete breakpoint" options.

In order to set breakpoints in other files (ie, not in the main() function), choose the "Open Source" option from the File menu of DDD. The file dialog should appear; choose Setting a breakpoint in this class implementation file is identical to setting breakpoints in the main() function. Set a breakpoint at line 43 in using the method described above. By clicking on Run again (restart the program as necessary), the program should break, once again, at the breakpoint.

Tracing/Stepping through Programs

Open the program 'bookinfo' using DDD. Set a breakpoint at line 51 in the file '' Run the program. 'bookinfo' will halt immediately before dumping any output to cout.  From this point, you can trace forward and line-by-line execute the remaining code. To do this, press the 'next' button in the pop-up toolbar. You will see your current position in the source code denoted by the green arrow.  It should indicate that you are halted on line 54 of the file,  just inside the for loop. Hit 'next' again. DDD will then execute line 54, and you will see the first line of output appear in the Debugger Console window. The green arrow should indicate your current position at line 55. As you continue to hit the 'next' button, DDD will repeatedly loop through lines 54 and 55 displaying lines of output. When the for loop has finished, pressing 'next' will bring you to line 56, and eventually to the end of the program itself.

While inside the loop, in the depths of the PrintBooks() function, you may choose to look at your position in the previous reference frame, i.e., the function from which you called PrintBooks(). To do this, press the 'Up' button. You will see that you are currently halted on line 64, on the line calling the function PrintBooks() within the function main(). You cannot go up any furhter than the main function. To descend back to your initial reference frame, use the 'Down' button.

At any point while stepping through the for loop, you may choose to forgo tracing through your program by pressing the 'Cont' (read: continue) button. Pressing the continue button will finish execution of your program.

Congrats. Now you know how to trace through programs.

Examining Data

In the course of tracing through a program, you may have the undesirable urge to examine the contents of a variable. You can do this by right-clicking on a variable name in the DDD window. Upon right-clicking, select "Display". In some cases, you may want to display the value of a pointer. In this case, use the "Display*" menu item. Now, load up the bookinfo program, and set the breakpoint at line 64. Then "RUN" the program.

Open the "Program Data" window by choosing the "Program Data" from the "Windows" menu; a window should appear.  When the program breaks, click on the variable "books" on line 64 and click on the "Display()" button.  Now a box should appear in the "Program Data" window. It is called "books" and display some other variables, such as myCapacity, mySize, myList. Click on the "books" box so that it is highlighted and then click on the "Display*()" button. (Note the asterisk). A second window should appear in the "Program Data" window. Select this new box and click on "Show()" to display it's contents. Here, you should see the title and the author of the first book in the vector called "books".

You can view all the other elements in the vector as well, for example, the second item. To do this, click the "New Display" button and type in "books.myList[1]" (remove the preceding asterisk if it exists). A new box will appear that has the data contained in the second vector element of books. You can use the "Show()" button on this new box as well. You can make other subsequent bits of data contained in the vector "books" appear by using the same method, but substitute a new number for "1" in the "books.myList[1]".

And now you're done with learning the basics of DDD. Hopefully, you'll like it and experiment around with it some more.

Send questions to Prof. Rodger.