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/cs127w02/debuglab/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?
1. Save the program http://www.mathcs.carleton.edu/faculty/dmusican/cs127w02/debuglab/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?
Note: If you wish to see the Insure errors in the terminal window instead of in the "insra" window, create a file called ".psrc" in the same directory as your program and put the following line in it:
insure++.report_file stderr
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.
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.