Debugging Lab
In this lab, you will learn to use debugging tools such as gdb,
assert(), and -D_GLIBCXX_DEBUG
. You will modify a program
bad.cc
, and turn in the modified version.
                
Consider the following program, bad.cc
. It opens resolv.conf
,
and prints out the line that starts with “search”. It has several errors
(don’t rob others of the joy of discovery—keep it to yourself):
                
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
int main() {
// First, some stupid code just to introduce an error:
vector<int> v = {2,42,165,253,1003};
cout << "My favorite number is " << v[10000003] << '\n';
string filename = "\etc\resolv.conf";
ifstream in(filename);
assert(in.is_open());
for (string s; getline(in, s); ) {
string prefix = s.substr(1,6);
if (prefix == "search")
cout << s << '\n';
}
return 0;
}
When the program works, and is run on a
CS Dept. computer,
it prints something like this:
                
My favorite number is 253
search cs.colostate.edu colostate.edu
Handy gdb commands
Here are some gdb commands that you may find useful:
                
Command | Description |
r optional-arguments | run: run the program |
q | quit: exit gdb |
bt | backtrace: show what function called what |
l | list: show the next 10 lines of source code |
l line-number | list: show the source code around that line |
b line-number | breakpoint: stop when you come to this line |
p | print: show all local variables |
p variable | print: show the value of that variable |
For this lab, you should:
- Copy & paste the program to
bad.cc
.
If you don’t know how to copy & paste, then this is a fine opportunity
to learn.
- Compile it, like this:
g++ bad.cc
- Execute it, observe the segmentation fault,
obviously caused by
v[100000003]
- Compile it, with the C++ library debugging flag, like this:
g++ -D_GLIBCXX_DEBUG bad.cc
Yes, the underscores (_
) matter.
- Execute it, observe the more helpful error message.
It’s not perfect, but it beats “Segmentation fault”.
At least it gives a hint as to which container is having problems.
- Fix the offending line by changing
[100000003]
to [3]
.
- Compile & execute, see the assert() failure.
- Compile it, like this:
g++ -DNDEBUG bad.cc
- Execute it, observe that the assertion failure vanished
(and the program still doesn’t work).
- Compile for the debugger gdb:
g++ -ggdb bad.cc
- Execute the debugger:
gdb ./a.out
- Set a breakpoint at the assertion:
b 16
(your line number may vary)
- Run the program:
r
- Print the value of
filename
, like this: p filename
- Figure out why
filename
has that value. Fix the source.
- Recompile for the debugger.
- Set a breakpoint at
if (prefix == "search")
line.
- Run the program to that breakpoint, inspect
s
and prefix
the same way that you printed the value of filename
.
- Fix the call to string::substr().
- Recompile, see that the program works correctly.
For extra fame & glory:
                
- Debug the program using ddd.
How to submit your work:
In Canvas, check in the
file
bad.cc
to the assignment
“Lab06”.
                
How to receive negative points:
Turn in someone else’s work.