Skip to content
C++
Debugger
Updated 2025-01-01T00:00:00.000Z

LLDB 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 dump

Essential 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 debuggee

Inspecting 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 memory

Thread 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 info

Conditional 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
  > DONE

Expression 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

ActionGDBLLDB
Startrunrun
Breakpoint on functionbreak mainbreakpoint set -n main or b main
Breakpoint on linebreak file.cpp:42breakpoint set -f file.cpp -l 42
Print variableprint xprint x or p x
Backtracebtbt
Next linenextnext
Step instepstep
Watch variablewatch xwatchpoint set variable x
List breakpointsinfo breakpointsbreakpoint list
Delete breakpointdelete 1breakpoint delete 1
All threads btthread apply all btbt 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 bt
Edit on GitHubUpdated 2025-01-01T00:00:00.000Z