$npx -y skills add mohitmishra786/low-level-dev-skills --skill lldbLLDB debugger skill for C/C++/Swift/Objective-C programs. Use when debugging with LLDB on macOS, FreeBSD, or Linux-clang environments, mapping GDB mental models to LLDB commands, using LLDB in Xcode or VS Code, or debugging Swift/Objective-C. Activates on queries about LLDB comma
| 1 | # LLDB |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through LLDB sessions and map existing GDB knowledge to LLDB. Covers command differences, Apple specifics, Python scripting, and IDE integration. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "I'm on macOS and need to debug a C++ program" |
| 10 | - "How does LLDB differ from GDB?" |
| 11 | - "How do I do [GDB command] in LLDB?" |
| 12 | - "LLDB shows `<unavailable>` for variables" |
| 13 | - "How do I use LLDB in VS Code?" |
| 14 | - "How do I write an LLDB Python script?" |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Start LLDB |
| 19 | |
| 20 | ```bash |
| 21 | lldb ./prog # load binary |
| 22 | lldb ./prog -- arg1 arg2 # with arguments |
| 23 | lldb -p 12345 # attach to PID |
| 24 | lldb -c core.1234 # load core dump |
| 25 | lldb ./prog core.1234 # binary + core |
| 26 | ``` |
| 27 | |
| 28 | ### 2. GDB → LLDB command map |
| 29 | |
| 30 | Source: <https://lldb.llvm.org/use/map.html> |
| 31 | |
| 32 | | GDB | LLDB | Notes | |
| 33 | |-----|------|-------| |
| 34 | | `run [args]` | `process launch [args]` / `r` | | |
| 35 | | `continue` | `process continue` / `c` | | |
| 36 | | `next` | `thread step-over` / `n` | | |
| 37 | | `step` | `thread step-in` / `s` | | |
| 38 | | `nexti` | `thread step-inst-over` / `ni` | | |
| 39 | | `stepi` | `thread step-inst` / `si` | | |
| 40 | | `finish` | `thread step-out` / `finish` | | |
| 41 | | `break main` | `breakpoint set -n main` / `b main` | | |
| 42 | | `break file.c:42` | `breakpoint set -f file.c -l 42` / `b file.c:42` | | |
| 43 | | `break *0x400abc` | `breakpoint set -a 0x400abc` / `b -a 0x400abc` | | |
| 44 | | `watch x` | `watchpoint set variable x` / `wa s v x` | | |
| 45 | | `print x` | `frame variable x` / `p x` | | |
| 46 | | `print/x x` | `p/x x` | | |
| 47 | | `info locals` | `frame variable` / `fr v` | | |
| 48 | | `info args` | `frame variable --arguments` | | |
| 49 | | `backtrace` | `thread backtrace` / `bt` | | |
| 50 | | `frame N` | `frame select N` / `f N` | | |
| 51 | | `info threads` | `thread list` | | |
| 52 | | `thread N` | `thread select N` | | |
| 53 | | `thread apply all bt` | `thread backtrace all` | | |
| 54 | | `x/10wx addr` | `memory read -s4 -fx -c10 addr` / `x/10xw addr` | | |
| 55 | | `set var = 42` | `expression var = 42` / `expr var = 42` | | |
| 56 | | `quit` | `quit` / `q` | | |
| 57 | |
| 58 | ### 3. Breakpoints |
| 59 | |
| 60 | ```lldb |
| 61 | # By name |
| 62 | b main |
| 63 | breakpoint set --name foo |
| 64 | breakpoint set --name foo --condition 'x > 0' |
| 65 | |
| 66 | # By file:line |
| 67 | b file.c:42 |
| 68 | breakpoint set --file file.c --line 42 |
| 69 | |
| 70 | # By address |
| 71 | b -a 0x100003f20 |
| 72 | |
| 73 | # By regex |
| 74 | breakpoint set --func-regex '^MyClass::' |
| 75 | |
| 76 | # List |
| 77 | breakpoint list / br l |
| 78 | |
| 79 | # Delete |
| 80 | breakpoint delete 2 |
| 81 | |
| 82 | # Disable/enable |
| 83 | breakpoint disable 1 |
| 84 | breakpoint enable 1 |
| 85 | |
| 86 | # Commands on hit |
| 87 | breakpoint command add 1 |
| 88 | > p x |
| 89 | > continue |
| 90 | > DONE |
| 91 | ``` |
| 92 | |
| 93 | ### 4. Inspect state |
| 94 | |
| 95 | ```lldb |
| 96 | # Print variable |
| 97 | p x |
| 98 | frame variable x |
| 99 | p *ptr |
| 100 | p arr[0] |
| 101 | |
| 102 | # Print expression |
| 103 | expression x * 2 + 1 |
| 104 | expr (int)sqrt(9.0) |
| 105 | |
| 106 | # All locals |
| 107 | frame variable |
| 108 | fr v -a # include arguments |
| 109 | |
| 110 | # Registers |
| 111 | register read |
| 112 | register read rip rsp |
| 113 | |
| 114 | # Memory |
| 115 | memory read --size 4 --format x --count 10 0x7fff0000 |
| 116 | x/10xw 0x7fff0000 # GDB-compatible syntax |
| 117 | |
| 118 | # Type info |
| 119 | image lookup --type MyClass |
| 120 | type lookup MyClass |
| 121 | ``` |
| 122 | |
| 123 | ### 5. Watchpoints |
| 124 | |
| 125 | ```lldb |
| 126 | watchpoint set variable x # write watchpoint |
| 127 | watchpoint set variable -w read x # read watchpoint |
| 128 | watchpoint set variable -w read_write x |
| 129 | watchpoint set expression -- &x # by address |
| 130 | |
| 131 | watchpoint list |
| 132 | watchpoint delete 1 |
| 133 | ``` |
| 134 | |
| 135 | ### 6. Threads |
| 136 | |
| 137 | ```lldb |
| 138 | thread list |
| 139 | thread select 3 |
| 140 | thread backtrace all |
| 141 | thread backtrace --count 5 # limit depth |
| 142 | |
| 143 | # Per-thread stepping |
| 144 | thread step-over # step this thread only |
| 145 | ``` |
| 146 | |
| 147 | ### 7. macOS / Apple specifics |
| 148 | |
| 149 | ```lldb |
| 150 | # Symbol lookup in shared cache |
| 151 | image lookup --address 0x18ab12345 |
| 152 | image lookup --name objc_msgSend |
| 153 | |
| 154 | # Objective-C method breakpoint |
| 155 | b "-[NSArray objectAtIndex:]" |
| 156 | b "+[NSString stringWithFormat:]" |
| 157 | |
| 158 | # Inspect Objective-C object |
| 159 | po myObject # print-object (calls -description) |
| 160 | po [arr count] |
| 161 | |
| 162 | # Show loaded libraries |
| 163 | image list |
| 164 | image list -b # brief (names only) |
| 165 | ``` |
| 166 | |
| 167 | ### 8. VS Code integration |
| 168 | |
| 169 | Install the `CodeLLDB` extension. `.vscode/launch.json`: |
| 170 | |
| 171 | ```json |
| 172 | { |
| 173 | "version": "0.2.0", |
| 174 | "configurations": [ |
| 175 | { |
| 176 | "name": "Debug (lldb)", |
| 177 | "type": "lldb", |
| 178 | "request": "launch", |
| 179 | "program": "${workspaceFolder}/build/prog", |
| 180 | "args": [], |
| 181 | "cwd": "${workspaceFolder}", |
| 182 | "preLaunchTask": "build" |
| 183 | } |
| 184 | ] |
| 185 | } |
| 186 | ``` |
| 187 | |
| 188 | ### 9. LLDB Python scripting |
| 189 | |
| 190 | ```python |
| 191 | import lldb |
| 192 | |
| 193 | def print_all_threads(debugger, command, result, internal_dict): |