The GNU debugger is a debugger for linux.
Run GDB with an executable program with no arguments:
gdb <executable>
Then run it with the command line arguments you need:
run <arguments>
If things crash, you can get the stack trace with:
bt
You can set a breakpoint (before running) on a function name (use full classname::methodname if in C++):
break <function name>
After breaking, go to the next instruction with:
step
Go to the next line in the current scope with:
next
You can set a break when any variable gets written with:
watch <varname>
that even works with fancy expressions as shown in print.
You can also set a break whenever any C++ exception is thrown:
catch throw
You can also make a gdb command file and run it with gdb -x <file>
The backtrace shows you the list of frames. You can select one of those
frames with frame <n>
and print the current one with frame
.
You can print code around the current frame with
list
Running list again prints the following lines. Running
list -
lists the previous lines
Print variables with:
p
If you are in a C++ class method, you can type:
p this
to print the current object pointer and
p *this
to print all of the members of the current object.
With a pointer value to an object, you can print it\'s members with:
p *(classname *) 0xXXXXXX
and then even get members like:
p ((classname *) 0xXXXX)->membername
You may have to put the classname in single quotes if it is in a namespace.