CS 117 Lab: Debugging

I. The ddd debugger

Your goal here is to get to know ddd, a "debugger" program that can help you find the problems with your programs. Debuggers can't help you until your program compiles, but once you have eliminated all compiler errors, a debugger can be one of your most valuable tools.

Today, you'll use ddd to (1) watch a variable, (2) single-step through a program, and (3) set breakpoints. All three of these operations should become clear shortly.

  1. To use ddd in the first place, you first have to do a weird thing. Use gEdit to open a file called .cshrc.linux that you will find in your account. Don't change anything that is already there, but add the following line to the end of the file:

  2. setenv LD_LIBRARY_PATH "/usr/local/lib:/usr/lib"

    Save and then close the file. Finally, close any terminal windows you have open and open a new one. You should be ready to go.

  3. Save the program http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/odds.cpp in your account, and compile it as follows:

    g++ -g -Wall -o odds odds.cpp

    The "-g" tells the compiler to include debugging information in the executable.

  4. Run the command

  5. ddd odds

  6. From the View menu, select Data Window. A grid should appear at the top of the ddd window.

  7. Set a breakpoint: In the source code part of the ddd window, click anywhere on the line that reads "int N = 10". Then click on the "Breakpoint" button. A small stop sign should appear in the source code.

  8. Run the program: Click "Run" in the little palette window. The program will run until your breakpoint and stop there.

  9. Watch: Double-click on N. A small window with N's name and value will appear in the data grid. Move it to a convenient spot. Double click on sum, so that you can watch it as well. What values are currently in N and sum? Why?

  10.  
  11. Single stepping. The single-stepping feature of ddd allows you to watch how your variables evolve while the program executes one instruction at a time. Click on the "Step" button in the palette. The arrow on the left will show you what line of code will execute next. Keep doing so until you step into the SumOfOdds function. Once you enter the SumOfOdds function, the variables in the data window disappear. Why?

  12.  
  13. Double click on the variables in the function so that you can see them in the data window. Continue stepping through the program until it ends, and watch the variables. If you receive an error message when you click "Step" on the cout statement, click "Next" instead.

  14.  
  15. Run the program again, but this time use the "Next" button to step through your program instead of the "Step" button. What is the difference between "Next" and "Step"? Why is this useful for debugging?

  16.  
  17. Run the program again, and this time use "Step" again to step into the SumOfOdds function. After you have stepped through a couple of iterations of the while loop, click the "Cont" button. What does this button do? Why is this useful for debugging?

  18.  
  19. Run the program again, and this time use "Step" again to step into the SumOfOdds function. After you have stepped through a couple of iterations of the while loop, click the "Finish" button. What does this button do? Why is this useful for debugging?

  20.  

II. Compiler error scavenger hunt

For this part of the lab, your goal as a class is to find as many different compiler error messages as possible, and list them along with their causes.

Start with one of the programs you have written or one of the lab exercise programs. Introduce a small error into it. Remove a semi-colon, add an extra brace (try { one time, } the next), delete a return statement, or whatever. Try to compile the program. Write down the error you introduced, and the first error message the compiler generated. We'll make a master list on the white board.

Be creative. There are lots of ways to confuse a compiler with a tiny mistake.
 


Dave Musicant, Department of Mathematics and Computer Science, Carleton College, Northfield, MN 55057, (507) 646-4364,dmusican@carleton.edu
Based on an original document by Mark Peralta and modified by Jeff Ondich.