Loading...
Loading...
GDB debugger skill for C/C++ programs. Use when starting a GDB session, setting breakpoints, stepping through code, inspecting variables, debugging crashes, using reverse debugging (record/replay), remote debugging with gdbserver, or loading core dumps. Activates on queries about GDB commands, segfaults, hangs, watchpoints, conditional breakpoints, pretty-printers, Python GDB scripting, or multi-threaded debugging.
npx skill4agent add mohitmishra786/low-level-dev-skills gdb??-g-Og-O0gcc -g -Og -o prog main.c-g -O2objcopygdb ./prog # load binary
gdb ./prog core # load with core dump
gdb -p 12345 # attach to running process
gdb --args ./prog arg1 arg2 # pass arguments
gdb -batch -ex 'run' -ex 'bt' ./prog # non-interactive (CI)| Command | Shortcut | Effect |
|---|---|---|
| | Start the program |
| | Resume after break |
| | Step over (source line) |
| | Step into |
| | Step over (instruction) |
| | Step into (instruction) |
| Run to end of current function | |
| Run to line N | |
| Force return from function | |
| | Exit GDB |
break main # break at function
break file.c:42 # break at line
break *0x400abc # break at address
break foo if x > 10 # conditional break
tbreak foo # temporary breakpoint (fires once)
rbreak ^mylib_.* # regex breakpoint on all matching functions
watch x # watchpoint: break when x changes
watch *(int*)0x601060 # watch memory address
rwatch x # break when x is read
awatch x # break on read or write
info breakpoints # list all breakpoints
delete 3 # delete breakpoint 3
disable 3 # disable without deleting
enable 3print x # print variable
print/x x # print in hex
print *ptr # dereference pointer
print arr[0]@10 # print 10 elements of array
display x # auto-print x on every stop
undisplay 1
info locals # all local variables
info args # function arguments
info registers # all CPU registers
info registers rip rsp rbp # specific registers
x/10wx 0x7fff0000 # examine 10 words at address
x/s 0x400abc # examine as string
x/i $rip # examine current instruction
backtrace # call stack (bt)
bt full # bt + local vars
frame 2 # switch to frame 2
up / down # move up/down the stackinfo threads # list threads
thread 3 # switch to thread 3
thread apply all bt # backtrace all threads
thread apply all bt full # full bt all threads
set scheduler-locking on # pause other threads while steppingtarget record-fulltarget record-btrace# Software record (slow but universal)
record # start recording
run
# ... trigger the bug ...
reverse-continue # go back to last break
reverse-next # step backwards
reverse-step
reverse-finish
# Intel Processor Trace (fast, hardware)
target record-btrace pt
run
# view instruction history
record instruction-historygdbserver :1234 ./prog
# Or attach:
gdbserver :1234 --attach 5678gdb ./prog
(gdb) target remote 192.168.1.10:1234
(gdb) break main
(gdb) continueaarch64-linux-gnu-gdb| Symptom | Cause | Fix |
|---|---|---|
| Binary not compiled with | Recompile with |
| Missing debug info or stack corruption | Install debuginfo package; check for stack smash |
| Null dereference / freed memory | Check pointer before deref; use ASan |
| | Go up frames to find the assertion |
GDB hangs on | Binary waiting for input | Redirect stdin: |
| Breakpoint in wrong place | Optimiser moved code | Compile with |
set history save on
set history size 1000
set print pretty on
set print array on
set print array-indexes on
set pagination off
set confirm offskills/debuggers/core-dumpsskills/debuggers/lldbskills/runtimes/sanitizersskills/compilers/gcc-g