gdb
Gdb is the Unix debugger for C++. It's very helpful once you know
how to use it. Unfortunately, it's very difficult to learn how to use
it until you really need to use it (i.e. you have a program with
something wrong in it and you just don't know what it is). You can
find out about it here.
Remember that in order to use it, you must compile your C++ files
with the -g flag, i.e. g++ -g names.
Below are some of the most-used gdb commands. Most of them have an
abbreviation that also works; it is given in parentheses.
- gdb name - run gdb on the executable file name.
- run (r) - run the program.
- run name - run the program with name name.
- help (h) - help!
- break (b) - set a breakpoint - a place where you want to be able
to look at what is going on. You can set a breakpoint at a specific
line in the source code file (b filename:line-no) or at
a specific function (b function-name).
- step (s) - step through the program (use it from a breakpoint).
- next (n) - go to the next instruction in the program.
- continue (c) - stop stepping.
- quit (q) - quit gdb.
Gdb is only useful if you can get your program to compile. If you
have compilation or linker errors, you cannot use Gdb. If you have
run-time errors, you can use Gdb.
A good way to learn to use Gdb is to use a simple program and try out
all the (basic) commands on it. Here is a program you can use:
/*
* l172.cc
*
* Amanda Stent, stent@cs.rochester.edu, 25 Aug 1997
* Time-stamp:
*/
#include
int do_sum(int);
// do-while, while, for, if-else, switch, break, continue
void main() {
int a = 0;
cout << "This program will sum the ASCII values of the letters in your name.\n";
do {
cout << "Input the number of letters in your name:\n";
cin >> a;
} while (a <= 0);
a = do_sum(a);
cout << "The letters in your name sum to: " << a << endl;
}
int do_sum(int number) {
char temp;
long int total = 0;
for (int b = 0; b < number; b++) {// declare here
switch(b) {
case 0 : cout << "Input the first letter:\n"; break;
default : cout << "Input the next letter:\n"; break;
}
while(1) {
cin >> temp;
cin.get();
if(((temp >= 97) && (temp <= 122)) || ((temp >= 65) && (temp <= 90))) {
total += (int)temp;
break; // out of while
}
else {
cout << "Your input, " << temp << ", is not a letter!\n";
continue; // back to top of while
}
}
}
return total;
}
Type this program in to a file, save it, and compile it using the -g
flag. Start gdb on a.out, and try setting a breakpoint at do_sum
(b do_sum). Run the program, and when you get to the
breakpoint try stepping. Try asking for help. Try any other commands
you want to try.
|