Debugger
Updated 2025-01-01T00:00:00.000ZLLDB Debugger
LLDB debugger for C++ — essential commands, breakpoints, watchpoints, expression evaluation, Python scripting, and LLDB vs GDB command mapping.
TL;DR
LLDB is the LLVM-project debugger, native on macOS and preferred with Clang on Linux. Same capabilities as GDB — different command syntax. If you know GDB, see the mapping table at the bottom.
Starting LLDB
bash
# Compile with debug info
clang++ -g -O0 myapp.cpp -o myapp
# Launch
lldb myapp
lldb -- myapp arg1 arg2 # with arguments
lldb -p <PID> # attach to running process
lldb myapp -c core.dump # open core dumpEssential Commands
cpp
(lldb) run [args] start the program
(lldb) continue continue after stop
(lldb) next step over (source line)
(lldb) step step into
(lldb) finish step out of current function
(lldb) quit exit LLDB
(lldb) breakpoint set -n main break at function main
(lldb) breakpoint set -f file.cpp -l 42 break at file:line
(lldb) breakpoint set -n "ClassName::method"
(lldb) breakpoint list show all breakpoints
(lldb) breakpoint delete 1 delete breakpoint #1
(lldb) breakpoint disable 2 disable without deleting
(lldb) watchpoint set variable myVar stop when myVar changes
(lldb) watchpoint set expression -w read_write -- &obj.field
(lldb) bt backtrace (call stack)
(lldb) bt all backtrace all threads
(lldb) frame select 2 select frame 2
(lldb) frame info info about current frame
(lldb) print expr evaluate and print expression
(lldb) p myvar shorthand for print
(lldb) po obj print object (calls description)
(lldb) expression myvar = 42 modify variable in debuggeeInspecting Variables
cpp
(lldb) frame variable all local variables
(lldb) frame variable myvar specific variable
(lldb) frame variable -r myobj recursive (expand object)
(lldb) p *ptr dereference pointer
(lldb) p arr[0] array element
(lldb) memory read --size 4 --count 10 0x7fff5fc0 read raw memoryThread Commands
cpp
(lldb) thread list all threads
(lldb) thread select 2 switch to thread 2
(lldb) thread backtrace all all thread stacks
(lldb) thread info current thread infoConditional Breakpoints
cpp
(lldb) breakpoint set -n compute -c "(x > 100)"
(lldb) breakpoint modify -c "(errno != 0)" 1
(lldb) breakpoint command add 1
Enter commands, end with 'DONE':
> p x
> continue
> DONEExpression Language
cpp
(lldb) p (int)myChar cast expression
(lldb) p &myVar address of variable
(lldb) p sizeof(MyStruct) sizeof
(lldb) expression --language c++ -- std::vector<int>{1,2,3}.size()LLDB vs GDB Command Mapping
| Action | GDB | LLDB |
|---|---|---|
| Start | run | run |
| Breakpoint on function | break main | breakpoint set -n main or b main |
| Breakpoint on line | break file.cpp:42 | breakpoint set -f file.cpp -l 42 |
| Print variable | print x | print x or p x |
| Backtrace | bt | bt |
| Next line | next | next |
| Step in | step | step |
| Watch variable | watch x | watchpoint set variable x |
| List breakpoints | info breakpoints | breakpoint list |
| Delete breakpoint | delete 1 | breakpoint delete 1 |
| All threads bt | thread apply all bt | bt all |
.lldbinit Configuration
python
# ~/.lldbinit
settings set target.x86-disassembly-flavor intel
settings set thread-format "thread #${thread.index}: ${thread.name}\n"
# Alias GDB-style commands
command alias gdb-bt btEdit on GitHubUpdated 2025-01-01T00:00:00.000Z