$npx -y skills add mohitmishra786/low-level-dev-skills --skill gdbGDB 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
| 1 | # GDB |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Walk agents through GDB sessions from first launch to advanced workflows: crash diagnosis, reverse debugging, remote debugging, and multi-thread inspection. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "My program segfaults / crashes — how do I debug it?" |
| 10 | - "How do I set a breakpoint on condition X?" |
| 11 | - "How do I inspect memory / variables in GDB?" |
| 12 | - "How do I debug a remote embedded target?" |
| 13 | - "GDB shows `??` frames / no source" |
| 14 | - "How do I replay a bug deterministically?" (record/replay) |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Prerequisite: compile with debug info |
| 19 | |
| 20 | Always compile with `-g` (GCC/Clang). Use `-Og` or `-O0` for most debuggable code. |
| 21 | |
| 22 | ```bash |
| 23 | gcc -g -Og -o prog main.c |
| 24 | ``` |
| 25 | |
| 26 | For release builds: use `-g -O2` and keep the binary with symbols (strip separately with `objcopy`). |
| 27 | |
| 28 | ### 2. Start GDB |
| 29 | |
| 30 | ```bash |
| 31 | gdb ./prog # load binary |
| 32 | gdb ./prog core # load with core dump |
| 33 | gdb -p 12345 # attach to running process |
| 34 | gdb --args ./prog arg1 arg2 # pass arguments |
| 35 | gdb -batch -ex 'run' -ex 'bt' ./prog # non-interactive (CI) |
| 36 | ``` |
| 37 | |
| 38 | ### 3. Essential commands |
| 39 | |
| 40 | | Command | Shortcut | Effect | |
| 41 | |---------|----------|--------| |
| 42 | | `run [args]` | `r` | Start the program | |
| 43 | | `continue` | `c` | Resume after break | |
| 44 | | `next` | `n` | Step over (source line) | |
| 45 | | `step` | `s` | Step into | |
| 46 | | `nexti` | `ni` | Step over (instruction) | |
| 47 | | `stepi` | `si` | Step into (instruction) | |
| 48 | | `finish` | | Run to end of current function | |
| 49 | | `until N` | | Run to line N | |
| 50 | | `return [val]` | | Force return from function | |
| 51 | | `quit` | `q` | Exit GDB | |
| 52 | |
| 53 | ### 4. Breakpoints and watchpoints |
| 54 | |
| 55 | ```gdb |
| 56 | break main # break at function |
| 57 | break file.c:42 # break at line |
| 58 | break *0x400abc # break at address |
| 59 | break foo if x > 10 # conditional break |
| 60 | tbreak foo # temporary breakpoint (fires once) |
| 61 | rbreak ^mylib_.* # regex breakpoint on all matching functions |
| 62 | |
| 63 | watch x # watchpoint: break when x changes |
| 64 | watch *(int*)0x601060 # watch memory address |
| 65 | rwatch x # break when x is read |
| 66 | awatch x # break on read or write |
| 67 | |
| 68 | info breakpoints # list all breakpoints |
| 69 | delete 3 # delete breakpoint 3 |
| 70 | disable 3 # disable without deleting |
| 71 | enable 3 |
| 72 | ``` |
| 73 | |
| 74 | ### 5. Inspect state |
| 75 | |
| 76 | ```gdb |
| 77 | print x # print variable |
| 78 | print/x x # print in hex |
| 79 | print *ptr # dereference pointer |
| 80 | print arr[0]@10 # print 10 elements of array |
| 81 | display x # auto-print x on every stop |
| 82 | undisplay 1 |
| 83 | |
| 84 | info locals # all local variables |
| 85 | info args # function arguments |
| 86 | info registers # all CPU registers |
| 87 | info registers rip rsp rbp # specific registers |
| 88 | x/10wx 0x7fff0000 # examine 10 words at address |
| 89 | x/s 0x400abc # examine as string |
| 90 | x/i $rip # examine current instruction |
| 91 | |
| 92 | backtrace # call stack (bt) |
| 93 | bt full # bt + local vars |
| 94 | frame 2 # switch to frame 2 |
| 95 | up / down # move up/down the stack |
| 96 | ``` |
| 97 | |
| 98 | ### 6. Multi-thread debugging |
| 99 | |
| 100 | ```gdb |
| 101 | info threads # list threads |
| 102 | thread 3 # switch to thread 3 |
| 103 | thread apply all bt # backtrace all threads |
| 104 | thread apply all bt full # full bt all threads |
| 105 | set scheduler-locking on # pause other threads while stepping |
| 106 | ``` |
| 107 | |
| 108 | ### 7. Reverse debugging (record/replay) |
| 109 | |
| 110 | Record requires `target record-full` or `target record-btrace` (Intel PT): |
| 111 | |
| 112 | ```gdb |
| 113 | # Software record (slow but universal) |
| 114 | record # start recording |
| 115 | run |
| 116 | # ... trigger the bug ... |
| 117 | reverse-continue # go back to last break |
| 118 | reverse-next # step backwards |
| 119 | reverse-step |
| 120 | reverse-finish |
| 121 | |
| 122 | # Intel Processor Trace (fast, hardware) |
| 123 | target record-btrace pt |
| 124 | run |
| 125 | # view instruction history |
| 126 | record instruction-history |
| 127 | ``` |
| 128 | |
| 129 | ### 8. Remote debugging with gdbserver |
| 130 | |
| 131 | On target: |
| 132 | |
| 133 | ```bash |
| 134 | gdbserver :1234 ./prog |
| 135 | # Or attach: |
| 136 | gdbserver :1234 --attach 5678 |
| 137 | ``` |
| 138 | |
| 139 | On host: |
| 140 | |
| 141 | ```bash |
| 142 | gdb ./prog |
| 143 | (gdb) target remote 192.168.1.10:1234 |
| 144 | (gdb) break main |
| 145 | (gdb) continue |
| 146 | ``` |
| 147 | |
| 148 | For cross-compilation: use `a |