Debugging C/C++ with LLDB

The line that is pointed by the arrow is the next line will be executed.

Short commands

r: run
c: continue
b: break
n: next
Enter: repeat last command
s(step): step into a function
q: quit

How to use LLDB

  1. Generate source-level debug information
    $ gcc -g hello.c

  2. run the lldb on the executable
    $ lldb a.out

  3. then run the program
    (lldb) run

Setting Breakpoints

(lldb) break set -f demo.cpp -l #lineNum
(lldb) br s -f demo.cpp -l #
(lldb) b demo.cpp : #

Breakpoints with Symbols

On a function
(lldb) b square(int)

On a class method
(lldb) b Demo::demo()

Inside a namespace
(lldb) b LLDBDemo::add(int,int)

Manipulating Breakpoints

Listing breakpoints
(lldb) br list

Deleting breakpoints (lldb) br del # (lldb) br del

Stepping Around

Step over
(lldb) next
(lldb) n

Step into
(lldb) step or (lldb) s

Continue
(lldb) c

Inspecting Variables

Print Variable Contents
(lldb) p varname

Frame Variables(see all seeable vars) (lldb) frame variable or fr v

Current Line
(lldb) frame select

Backtrace and Frames

Backtrace
(lldb) bt

Switching Frames

(lldb) frame select 0
(lldb) f 2

Using Watchpoints

Program must be running in order to set watchpoints.
Global variable

(lldb) watchpoint set variable globalVariable
(lldb) watchpoint set variable -w read | write | read_write globalVariable

Member variable

(lldb) b main

(lldb) w s v d.memberVar

Terminating

Kill process
(lldb) kill

Exiting
(lldb) quit

Others

repeat the last command
press Enter

breakpoint at a functon b main or b func_name

look at the variable
print [var_name]

print all variables
fr v