CS 127 Lab: Debugging

I. The gdb debugger

Your goal here is to get to know gdb, 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 gdb 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.Save the program http://www.mathcs.carleton.edu/faculty/dmusican/cs127f01/odds.cpp in your account, and compile it as follows:

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

    Here's a description of what this does:
    g++: Run the g++ compiler.
    odds.cpp: That's your source code.
    -g: Include debugging information in the executable.
    -Wall: Enable "all" Warnings.
    -o odds: Name your executable file "odds".

2. Open odds.cpp in your editor, and move the window off to the side.

3. In the terminal window, run the command

    gdb odds

4. At the gdb prompt, type "list". You should see the first 10 lines of your program. Type "list" again. You should see the next 10 lines. Next, type "list 8". You should see the lines of code around line 8.

5. Line 7 should be the one that reads "int N = 10". Set a breakpoint on this line by typing "break 7" at the gdb prompt.

6. Run the program by typing "run" at the gdb prompt. The program will run until your breakpoint and stop there.

7. Type "print N" at the gdb prompt. Also do "print sum" at the gdb prompt. What values are currently in N and sum? Why?

8. At the gdb prompt, type "step". What happens? What is the value of N now? What is the value of "sum" now?

9. Keep stepping your code. How do N and sum change as you move through your code?

10. Run the program again, but this time type "next" instead of "step" to move through your program. What is the difference between "Next" and "Step"? Why is this useful for debugging?

11. 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, type "cont" at the gdb prompt. What does this button do? Why is this useful for debugging?

II. Insure

Your goal here is to get to know insure, another debugger tool that will help you find memory errors in your programs.

1. Save the program http://www.mathcs.carleton.edu/faculty/dmusican/cs127f01/lotsobugs.cpp in your account, and compile it as follows:

    g++ lotsobugs.cpp -g -Wall -o lotsobugs

2. Run the program by typing "lotsobugs" at the command prompt. What happens? Why?
3. Setup insure (by Parasoft Corporation) by typing

    source /usr/local/bin/setup-parasoft

You'll need to do this once inside any terminal window for which you want to run insure.

4. Next, recompile your program using insure instead of g++ as follows:

    insure lotsobugs.cpp -g -Wall -o lotsobugs

    You'll see the Insure "insra" window popup with information relating to compilation of your code.

5. Run your program by typing "lotsobugs" at the terminal window. What happens? Using the information you get from insure, fix the bugs in the code one by one, recompiling each time. What kinds of errors does insure tell you about?

III. Makefiles

1. Read through the description of makefiles at http://www.mathcs.carleton.edu/courses/course_resources/cs117/project-management/index.html.

2. Set up a makefile for lotsobugs.cpp that has three options: compiling without debugging flags, compiling with debugging flags, and using insure.

3. Set up a makefile for your minibrowse assignment.

IV. Compiler error scavenger hunt

For this part of the lab, your goal as a class is to find as many different insure 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. Write down the error you introduced, and the error message insure generated. We'll make a master list on the white board.

Be creative.


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