GDB
GDB is a C debugger for the cli, it allows stepping through a program, setting breakpoints, watching variables and many other more complex things too.
Compiling for GDB
gcc -g program.c -o program
Use the -g flag to keep the debug symbols in your output binary.
Running GDB
gdb ./program
Using!
Once you launch gdb you will be presented with a basic prompt. Your program wont run untill you type run at the prompt.
Getting a sensible view of your code
At the prompt type d layout src }} to get a view of your code where you can see the debugger progress. Other layouts are available:
next - Display the next layout prev - Display the previous layout src - Display the source and command windows asm - display the assembly and command windows split - display the src, asm and ccommand windows regs - display the register window along with src, asm or split.
Setting a breakpoint
Many ways, here are a few:
break file.c:17 <- set a breakpoint on line 17 in file.c break func_name <- set a breakpoint at a function start break (+/-)3 <- set a breakpoint offset from current location
To continure execution after a breakpoint type
continue
at the prompt.
Stepping
Type an
n
at the prompt to step to the next line. Type an
s
to step into a function.
Viewing variables
print variable_name
Topics:
{linux}{programming}