C Command Line Debugging Cheatsheet
Help
- List available command classes from the debugger:
help - List available commands in a class from the debugger:
help <class_name> - List all available commands from the debugger:
help all
Start the debugger
- Compile your source with the gcc
-goption:$ gcc -g -o <bin_name> <source> - Run the debugger:
$ gdb <bin_name> - Start the program in the debugger until the first breakpoint or completion:
run(abbrr) - Start the program in the debugger until the main procedure:
start
Attach the debugger
$ gdb -p <pid>
pid can be found by running ps -aux | grep <bin_search>
Breakpoints
- Set static/code breakpoints:
#include <signal.h>
/* ... code ... */
raise(SIGINT);In gdb when this breakpoint is hit, the top of the stack is the libc shared object file. Go up a frame in the stack from gdb by running up
- Set static/code conditional breakpoints: same as above, but with a conditional statement surrounding the
raise()call
💡 The following commands pertain to the debugger when a breakpoint is met or code is paused
- Set a breakpoint on the current line:
break(abbrb) - Set a breakpoint on line_number in the current file:
break <line_number>(abbrb <line_number>) - Set a breakpoint on line_number in file_name:
break <file_name>:<line_number>(abbrb <file_name>:<line_number>) - Set a breakpoint on a function:
break <function_name>(abbrb <function_name>) - Set a breakpoint on a function in a file_name:
break <file_name>:<function_name>(abbrb <file_name>:<function_name>) - Remove a breakpoint:
delete <breakpoint_number> - List all breakpoints:
info break(abbrinfo b)
Control
💡 The following commands pertain to the debugger when a breakpoint is met or code is paused
- Continue until next breakpoint:
continue(abbrc) - Step to the next statement in the current function:
next(abbrn) - Step in to the next statement or into the next function:
step(abbrs) - Step out of the current function on the stack:
finish(abbrf) - Continue to given location:
advance
Analysis
💡 The following commands pertain to the debugger when a breakpoint is met or code is paused
- List the current source:
list(abbrl) - Run an expression in the current context:
print <expr>(abbrp <expr>) - Watch an expression:
watch <expr> - List the current stacktrace:
backtrace(abbrbt) - Go up one frame in the stack (the one that ran the current frame):
up - Go down one frame in the stack (the one called by the current frame):
down
Global control
- Terminate the program:
kill - Terminate the program and exit the debugger:
quit(abbrq)