The debugger installed for Unix/Linux systems is gdb. It is available on Cslab and multilab. The gdb debugger has a user’s guide: http://www.gnu.org/manual/
Any source code that is to be debugged must be compiled with the –g option:
g++ -c –g spartan.cpp
Prepare all cpp code:
G++ -c –g *.cpp
Then linked into an executable program:
g++ -o spartan*.o
or do in one step:
g++ -g –o spartan *.cpp
You start the gdb debugger passing it the program to debug:
gdb spartan
Execute a program and pass it command line parameters
You start the gdb debugger passing it the program to debug:
gdb spartan
When you run the program, pass it the parameters:
run parm1 parm2 (and so on)
Insert a breakpoint (bp) at the first statement in main by:
break main (or at the start of any function, enter the function name)
You can now run your program by entering:
run or r
If the program is passed parameters:
run parm1 parm2 and so on
The program will run to the first executable statement in main (or to your first bp), which is displayed.
You can also set a bp at a specific line number:
break linenum (linenum = 5, 9, 46 etc.)
There are also ways of setting conditional bps (program is only stopped if a condition is met).
To set a breakpoint on entry to a function:
break ‘classname::memberfunctionname()’
If the function is not part of a class:
break functionname
You remove bps by the clear command:
clear function_name (clear bp at start of this function)
clear line_num (clear bp at this line number)
clear (if no value, clears the bp at the next statement to execute)
You can list bps in your program:
info break
You continue your program that is stopped at a bp:
continue or c (continue to the next bp or end of program)
step (execute the next statement. If it is a function call, enter that function)
step count (execute count statements)
next (execute next statement in this function – does not step into other functions)
finish (continue running until just after function returns)
You quit the debugger by:
quit
The list command is used:
list print the next 10 lines starting at the current line. If you enter list again, it prints the next 10 lines.
list linenum print lines centered on linenum
list function print lines centered at strat of function
list - print lines just before last lines printed
Display variables with the print command:
print variable (any program variable)
p variable
If you specify an array, the entire array is displayed:
Int a1[4] = {1,2,3,4};
p a1 displays
{1,2,3,4}
To display one element:
p a1[0]
To display 2 elements starting at a1[2]:
p a1[2]@2
To use indirect addressing (what a variable is pointing to):
char* a2[10];
p *a2[0]
To display a variable every time your program stops (reaches a bp):
display variable
If your program generates an unhandled exception, you can see the statement that caused it. Run it under gdb. After the exception, enter bt (backtrace) this displays the trace of function calls (and statement numbers of function calls) that caused the exception. The last one listed may be from a system function (like strcpy). However as you look at the list of calls, you should see names of functions in your program along with the statement numbers of them in your program.