GDB Is the command line debugger in linux. This is a cheatsheet of most used / useful commands.
The fastest way to get started is to use gdb <your executable>. This will cause gdb to load the symbols from the executable if they exist. If they do not exist then you will need to recompile the program with debugging symbols eg add -ggdb to the gcc command line when you are building your program. Typically tab works the same in gdb as it does on the shell.
Attaching / Running / Breaking
"attach" or "att" for short will attach to a running program eg. attach <process id>. you can normally find the process id of an already running program with the comand "ps axfu | grep myprogram"
"run" Will start a new process of your program. This works extactly like it does on the command line eg except you don't need the name of your program eg run <arg1> <arg2> * etc..
At any time you are attached to a running program you can break it with "Ctrl + c".
To make the program run again use the command "continue" or "c" for short.
You can also use "finish" to run to the end of the current function.
The command "kill" will kill your program at any time
break <function-name> - Puts a breakpoint on a function eg "break main" and "run" will cause the program to stop at the start of the function main
break +/-offset - Puts a breakpoint on on the realitive position of where your program is currently being debugged from. eg "break +10"
break filename:function - Puts a break point in filename at the named function. In C++ its possible to have multiple functions in different classes named the
break <address> - Puts a break point on the address of the function.
Use "tbreak" for any of the above to only break once.
info breakpoints - Lists all breakpoints
(gdb) info breakpoints
Num Type Disp Enb Address What
2 breakpoint keep y 0x0804906c in main at fake-sshd.c:92
clear - Deletes current breakpoint
delete <num> - Deletes breakpoint by number
disable <num> - disable breakpoint by number
enable <num> - enable breakpoint by number
Controlling Program Flow
step or "s" for short will step into the next function or to the next line of code.
next or "n" for short will execute the next line of code and step over any functions.
until <line> - Keep running until line number.
where - show current line number and function
list - Will list the source code at the current location. you can also "list <function>", "list filename:function" if you append a "-" it will list source code reading backwards though the file instead of forwards.
set listsize <count> - Set the number of lines to be listed when the list command is being used
backtrace or "bt" for short will show the current stack
frame <number> or "f" for short will jump to the stack frame listed in the backtrace
print <name> or "p" for short will print any variable contents.
p/x - Prints as hex
p/d - Prints a signed int.
p/u - Prints a unsigned int.
p/o - Prints as octal.
p/t - Prints as binary.
p/c - Prints as char.
p/f - Prints as floating point.
p/a - Prints as hex address.
p/s - Prints as string.