
Gdb
- 511 installs
- 155 repo stars
- Updated June 27, 2026
- mohitmishra786/low-level-dev-skills
gdb is an agent skill that diagnoses crashes, segfaults, deadlocks, and incorrect state in native C/C++ binaries using breakpoints and backtraces for developers who debug systems code during development and incidents.
About
gdb is a low-level developer skill from mohitmishra786/low-level-dev-skills that guides GNU Debugger workflows for native C and C++ binaries. The skill helps developers set breakpoints, inspect stacks, read memory and registers, trace thread deadlocks, and analyze segfaults when programs crash during development or after release. Developers reach for gdb when core dumps, intermittent crashes, or corrupted state require interactive debugging beyond printf logging or sanitizer reports alone. The workflow covers launching gdb against failing binaries, capturing backtraces at fault points, stepping through suspect functions, and correlating register and heap state with source lines. Use it during bring-up of systems software, game engines, embedded firmware, or any native module where production incidents demand fast root-cause isolation.
- Breakpoint and watchpoint workflows
- Core dump and live-process attach
- Backtrace and frame-local variable inspection
- Conditional breaks for race reproduction
- Remote gdbserver debugging
Gdb by the numbers
- 511 all-time installs (skills.sh)
- +39 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #84 of 596 Debugging skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill gdbAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 511 |
|---|---|
| repo stars | ★ 155 |
| Last updated | June 27, 2026 |
| Repository | mohitmishra786/low-level-dev-skills ↗ |
How do you debug C/C++ segfaults with GDB?
Diagnose crashes, segfaults, deadlocks, and incorrect state in native C/C++ binaries during development and post-release incident response with breakpoints and backtraces.
Who is it for?
Systems and native C/C++ developers investigating crashes, deadlocks, or memory corruption in binaries or core dumps.
Skip if: Web, mobile, or interpreted-language debugging where GDB does not attach to the runtime.
When should I use this skill?
The user reports segfaults, deadlocks, core dumps, or incorrect native C/C++ state and needs GDB breakpoint or backtrace analysis.
What you get
Root-cause backtraces, breakpoint traces, register and memory snapshots, and a fixed native binary patch plan
- Backtrace report
- Breakpoint reproduction steps
- Root-cause analysis notes
Files
GDB
Purpose
Walk agents through GDB sessions from first launch to advanced workflows: crash diagnosis, reverse debugging, remote debugging, and multi-thread inspection.
Triggers
- "My program segfaults / crashes — how do I debug it?"
- "How do I set a breakpoint on condition X?"
- "How do I inspect memory / variables in GDB?"
- "How do I debug a remote embedded target?"
- "GDB shows
??frames / no source" - "How do I replay a bug deterministically?" (record/replay)
Workflow
1. Prerequisite: compile with debug info
Always compile with -g (GCC/Clang). Use -Og or -O0 for most debuggable code.
gcc -g -Og -o prog main.cFor release builds: use -g -O2 and keep the binary with symbols (strip separately with objcopy).
2. Start GDB
gdb ./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)3. Essential commands
| Command | Shortcut | Effect |
|---|---|---|
run [args] | r | Start the program |
continue | c | Resume after break |
next | n | Step over (source line) |
step | s | Step into |
nexti | ni | Step over (instruction) |
stepi | si | Step into (instruction) |
finish | Run to end of current function | |
until N | Run to line N | |
return [val] | Force return from function | |
quit | q | Exit GDB |
4. Breakpoints and watchpoints
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 35. Inspect state
print 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 stack6. Multi-thread debugging
info 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 stepping7. Reverse debugging (record/replay)
Record requires target record-full or target record-btrace (Intel PT):
# 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-history8. Remote debugging with gdbserver
On target:
gdbserver :1234 ./prog
# Or attach:
gdbserver :1234 --attach 5678On host:
gdb ./prog
(gdb) target remote 192.168.1.10:1234
(gdb) break main
(gdb) continueFor cross-compilation: use aarch64-linux-gnu-gdb on the host.
9. Common problems
| Symptom | Cause | Fix |
|---|---|---|
No symbol table | Binary not compiled with -g | Recompile with -g |
?? frames in backtrace | Missing debug info or stack corruption | Install debuginfo package; check for stack smash |
Cannot access memory at address | Null dereference / freed memory | Check pointer before deref; use ASan |
SIGABRT in backtrace | abort() or assertion failure | Go up frames to find the assertion |
GDB hangs on run | Binary waiting for input | Redirect stdin: run < /dev/null |
| Breakpoint in wrong place | Optimiser moved code | Compile with -Og; or use nexti |
10. GDB init file (~/.gdbinit)
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 offFor a command cheatsheet, see references/cheatsheet.md. For pretty-printers and Python scripting, see references/scripting.md.
Related skills
- Use
skills/debuggers/core-dumpsfor loading core files - Use
skills/debuggers/lldbfor LLDB-based workflows - Use
skills/runtimes/sanitizersto catch bugs before needing the debugger - Use
skills/compilers/gccfor-gflag details
GDB Command Cheatsheet
Source: <https://sourceware.org/gdb/documentation/> Source: <https://aaronbloomfield.github.io/pdr/docs/gdb_vs_lldb.html>
Table of Contents
1. Startup 2. Execution control 3. Breakpoints & watchpoints 4. Inspection 5. Stack 6. Memory 7. Threads 8. Reverse debugging 9. Display formats
---
Startup
gdb prog Load binary
gdb prog core Load with core
gdb -p PID Attach to process
gdb --args prog a b c Pass arguments
gdb -batch -ex 'run' -ex 'bt' Non-interactive
set args a b c Set args after load---
Execution control
run / r [args] Start
continue / c Resume
next / n Step over (source)
step / s Step into (source)
nexti / ni Step over (instruction)
stepi / si Step into (instruction)
finish Run to end of function
until N Run to line N in current file
until file.c:N Run to specific line
advance foo Run to next call of foo
return [expr] Force return value
signal SIGUSR1 Deliver signal
kill Kill program---
Breakpoints & watchpoints
break main Function
break file.c:42 Line
break *0x400abc Address
break foo if x > 0 Conditional
tbreak foo Temporary (once)
rbreak regex All matching functions
catch throw C++ exception thrown
catch syscall mmap Syscall breakpoint
watch var Write watchpoint
rwatch var Read watchpoint
awatch var Read/write watchpoint
watch *(int*)addr Memory watchpoint
info breakpoints / info watch List
delete N Remove breakpoint N
disable N / enable N Toggle
ignore N count Skip N times
commands N Run commands on hit---
Inspection
print expr / p expr Print expression
print/x expr Print hex
print/t expr Print binary
print/f expr Print float
print/c expr Print char
print/d expr Print signed int
print/u expr Print unsigned int
print/a expr Print as address
print arr@N Print N elements
print *ptr Dereference
print *((int*)ptr) Cast and deref
display expr Print on every stop
undisplay N Remove auto-display
info display List
ptype var Print type of var
whatis var Brief type info
info locals All locals
info args Function arguments
info variables All global variables---
Stack
backtrace / bt Call stack
bt N Top N frames
bt full Frames + locals
bt -N Bottom N frames
frame N / f N Select frame N
up / down Move in stack
info frame Frame info
info args Current frame args
info locals Current frame locals---
Memory
x/Nuf addr Examine memory
N = count
u = unit: b(byte) h(half=2) w(word=4) g(giant=8)
f = format: x(hex) d(dec) u(uint) o(octal) t(bin) f(float) s(string) i(insn)
Examples:
x/10wx 0x7fff0000 10 words (hex)
x/4gx $rsp 4 giants at stack top
x/20i $rip 20 instructions from RIP
x/s 0x400abc String
x/b &var Single byte
set {int}0xaddr = 42 Write memory
set var = 42 Set variable---
Threads
info threads List threads
thread N Switch to thread N
thread apply all bt Backtrace all
thread apply all bt full
thread apply 1 2 print x Apply to specific threads
set scheduler-locking on/off Lock/unlock other threads during step---
Reverse debugging
record Start software record
record btrace Start hardware trace
record stop Stop recording
reverse-continue / rc Go backward to last event
reverse-next / rn Reverse step-over
reverse-step / rs Reverse step-into
reverse-finish Reverse finish
set exec-direction reverse Make n/s go backward
set exec-direction forward Restore
record instruction-history Show recorded instructions (btrace)
record function-call-history Show call history (btrace)---
Display formats
| Format | Code | Example |
|---|---|---|
| Hex | /x | p/x var |
| Decimal | /d | p/d var |
| Binary | /t | p/t var |
| Float | /f | p/f var |
| Char | /c | p/c var |
| String | /s | x/s ptr |
| Instruction | /i | x/5i $pc |
GDB Python Scripting and Pretty-Printers
Source: <https://sourceware.org/gdb/current/onlinedocs/gdb.html/Python.html>
GDB Python API basics
GDB embeds a Python interpreter. Scripts can be loaded with source script.py or placed in ~/.gdb_scripts/.
import gdb
# Execute GDB command
gdb.execute('bt full')
# Evaluate expression
val = gdb.parse_and_eval('x')
print(val)
# Read inferior memory
inf = gdb.selected_inferior()
mem = inf.read_memory(0x601060, 16)
# List threads
for thr in gdb.selected_inferior().threads():
print(thr.num, thr.name)Custom pretty-printer
import gdb
import gdb.printing
class MyVectorPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
data = self.val['_data']
size = int(self.val['_size'])
return f'MyVector of {size} elements'
def children(self):
data = self.val['_data']
size = int(self.val['_size'])
for i in range(size):
yield f'[{i}]', (data + i).dereference()
def display_hint(self):
return 'array'
def build_printer(val):
t = val.type.strip_typedefs()
if t.name == 'MyVector':
return MyVectorPrinter(val)
return None
gdb.pretty_printers.append(build_printer)Load in GDB:
source /path/to/printer.pySTL pretty-printers (libstdc++)
# Install python3-gdb package (Debian/Ubuntu)
sudo apt install python3-gdb
# Or use the GCC source tree printers
# They load automatically if libstdc++ was built with GDB supportBreakpoint commands
break foo
commands
silent
print "hit foo, x =", x
continue
endConvenience variables
set $i = 0
while $i < 10
print arr[$i]
set $i = $i + 1
endUseful define aliases
define pbt
thread apply all bt full
end
define hex
print/x $arg0
endPlace in ~/.gdbinit.
Related skills
FAQ
What problems does the gdb skill address?
The gdb skill addresses native C and C++ crashes, segfaults, deadlocks, and incorrect runtime state using GNU Debugger breakpoints, backtraces, and interactive inspection during development and incident response.
When should developers invoke the gdb skill?
Developers should invoke the gdb skill when a native binary crashes, produces a core dump, deadlocks across threads, or enters incorrect state that requires GDB backtrace and breakpoint analysis beyond logging alone.