GNU Debugger (GDB)
The GNU Debugger (gdb) is a freely available
debugger for C, C++, and Fortran program compiler produced by the Free Software
Foundation and is available for a wide range of computers. To use the GNU
debugger (gdb) with C/C++ programs all the source code must be compiled with the
"-g
" debugger option that includes additional information in the
excutable, allowing the debugger map the locations of memory address back into
meaningful things like variable names and program line numbers.
g++ -g -o file file.cpp
- Compiles the C++ program (file.cpp) with debugger option on
gdb file
- Starts GNU debugger (gdb) and loads the executable
file
into
memory.
Basic GNU debugger commands:
quit
- Quits gdb and returns back to the user prompt. GDB may ask you if you want
to exit if the program being debugged was not completed executing.
run
- Starts the execution of the program being debugged.
- control-c
- Interrupts the execution of the program being debugged. This is useful if
the program appears to be stuck in a loop.
where
- Shows where the program was stopped. A trace showing each active
function's argument list, the file that the function came from, and the next
line in the code that will be executed in the function is displayed.
up
- Moves up the trace to function that called the current function.
down
- Moves down the trace to function that is called by the current function.
print c
- Prints out value of the variable
c
.
print c+x
- Prints out value of the sum of the variables
c
and
x
. Note that any regular C expression can be evaluated by
print
.
list
- Show source code around place that program is currently stopped.
break #line-number
- Sets a breakpoint at the specified line number of the file.
break [file:]function
- Sets a breakpoint at the specified function of the file.
info breakpoints
- Show the status of user-settable breakpoints
clear
- Removes the breakpoint
cont
- Continues the execution of a program after a control-c or a break point.
step
- Executes a single statement of program and returns to the debugger.
next
- Executes a subroutine and treats it as one instruction. debugger.