Examining stack feature state

The most convenient way to inspect a frame's arguments and local variables is to use the "frame variable" command:
(lldb) frame variable
self = (SKTGraphicView *) 0x0000000100208b40
_cmd = (struct objc_selector *) 0x000000010001bae1
sender = (id) 0x00000001001264e0
selection = (NSArray *) 0x00000001001264e0
i = (NSUInteger) 0x00000001001264e0
c = (NSUInteger) 0x00000001001253b0
As you see above, if you don't specify any variable names, all arguments and locals will be shown. If you call "frame variable" passing in the names of a particular local(s), only those variables will be printed. For instance:
(lldb) frame variable self
(SKTGraphicView *) self = 0x0000000100208b40
You can also pass in a path to some subelement of one of the available locals, and that sub-element will be printed. For instance:

(lldb) frame variable self.isa
(struct objc_class *) self.isa = 0x0000000100023730
The "frame variable" command is not a full expression parser but it does support a few simple operations like &, *, ->, [] (no overloaded operators). The array brackets can be used on pointers to treat pointers as arrays:
(lldb) frame variable *self
(SKTGraphicView *) self = 0x0000000100208b40
(NSView) NSView = {
(NSResponder) NSResponder = {
...

(lldb) frame variable &self
(SKTGraphicView **) &self = 0x0000000100304ab

(lldb) frame variable argv[0]
(char const *) argv[0] = 0x00007fff5fbffaf8 "/Projects/Sketch/build/Debug/Sketch.app/Contents/MacOS/Sketch"
The frame variable command will also perform "object printing" operations on variables (currently we only support ObjC printing, using the object's "description" method. Turn this on by passing the -o flag to frame variable:
(lldb) frame variable -o self (SKTGraphicView *) self = 0x0000000100208b40 <SKTGraphicView: 0x100208b40> You can select another frame to view with the "frame select" command
(lldb) frame select 9
frame #9: 0x0000000100015ae3, where = Sketch`function1 + 33 at /Projects/Sketch/SKTFunctions.m:11
You can also move up and down the stack by passing the "--relative" ("-r") option. And we have built-in aliases "u" and "d" which behave like their gdb equivalents.
If you need to view more complex data or change program data, you can use the general "expression" command. It takes an expression and evaluates it in the scope of the currently selected frame. For instance:
(lldb) expr self
$0 = (SKTGraphicView *) 0x0000000100135430
(lldb) expr self = 0x00
$1 = (SKTGraphicView *) 0x0000000000000000
(lldb) frame var self
(SKTGraphicView *) self = 0x0000000000000000
You can also call functions:
(lldb) expr (int) printf ("I have a pointer 0x%llx.\n", self)
$2 = (int) 22
I have a pointer 0x0.
As I said above, "expression" is one of the "raw" commands. So you don't have to quote your whole expression, nor backslash protect quotes, etc...
Finally, the results of the expressions are stored in persistent variables (of the form $[0-9]+) that you can use in further expressions, like:
(lldb) expr self = $0
$4 = (SKTGraphicView *) 0x0000000100135430

Examining thread state

Once you've stopped, lldb will choose a current thread, usually the one that stopped "for a reason", and a current frame in that thread (on stop this is always the bottom-most frame). Many the commands for inspecting state work on this current thread/frame.
To inspect the current state of your process, you can start with the threads:
(lldb) thread list
Process 46915 state is Stopped
* thread #1: tid = 0x2c03, 0x00007fff85cac76a, where = libSystem.B.dylib`__getdirentries64 + 10, stop reason = signal = SIGSTOP, queue = com.apple.main-thread
  thread #2: tid = 0x2e03, 0x00007fff85cbb08a, where = libSystem.B.dylib`kevent + 10, queue = com.apple.libdispatch-manager
  thread #3: tid = 0x2f03, 0x00007fff85cbbeaa, where = libSystem.B.dylib`__workq_kernreturn + 10
The * indicates that Thread 1 is the current thread. To get a backtrace for that thread, do:
(lldb) thread backtrace
thread #1: tid = 0x2c03, stop reason = breakpoint 1.1, queue = com.apple.main-thread
 frame #0: 0x0000000100010d5b, where = Sketch`-[SKTGraphicView alignLeftEdges:] + 33 at /Projects/Sketch/SKTGraphicView.m:1405
 frame #1: 0x00007fff8602d152, where = AppKit`-[NSApplication sendAction:to:from:] + 95
 frame #2: 0x00007fff860516be, where = AppKit`-[NSMenuItem _corePerformAction] + 365
 frame #3: 0x00007fff86051428, where = AppKit`-[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 121
 frame #4: 0x00007fff860370c1, where = AppKit`-[NSMenu performKeyEquivalent:] + 272
 frame #5: 0x00007fff86035e69, where = AppKit`-[NSApplication _handleKeyEquivalent:] + 559
 frame #6: 0x00007fff85f06aa1, where = AppKit`-[NSApplication sendEvent:] + 3630
 frame #7: 0x00007fff85e9d922, where = AppKit`-[NSApplication run] + 474
 frame #8: 0x00007fff85e965f8, where = AppKit`NSApplicationMain + 364
 frame #9: 0x0000000100015ae3, where = Sketch`main + 33 at /Projects/Sketch/SKTMain.m:11
 frame #10: 0x0000000100000f20, where = Sketch`start + 52
You can also provide a list of threads to backtrace, or the keyword "all" to see all threads:
(lldb) thread backtrace all You can select the current thread, which will be used by default in all the commands in the next section, with the "thread select" command:
(lldb) thread select 2 where the thread index is just the one shown in the "thread list" listing.

Controlling your program

After launching, we can continue until we hit our breakpoint. The primitive commands for process control all exist under the "thread" command:
(lldb) thread continue
Resuming thread 0x2c03 in process 46915
Resuming process 46915
(lldb)
At present you can only operate on one thread at a time, but the design will ultimately support saying "step over the function in Thread 1, and step into the function in Thread 2, and continue Thread 3" etc. When we eventually support keeping some threads running while others are stopped this will be particularly important. For convenience, however, all the stepping commands have easy aliases. So "thread continue" is just "c", etc.
The other program stepping commands are pretty much the same as in gdb. You've got:
(lldb) thread step-in    // The same as gdb's "step" or "s" 
(lldb) thread step-over  // The same as gdb's "next" or "n"
(lldb) thread step-out   // The same as gdb's "finish" or "f"
By default, lldb does defined aliases to all common gdb process control commands ("s", "step", "n", "next", "finish"). If we have missed any, please add them to your ~/.lldbinit file using the "command alias" command.
lldb also supported the step by instruction versions:
(lldb) thread step-inst       // The same as gdb's "stepi" / "si"
(lldb) thread step-over-inst  // The same as gdb's "nexti" / "ni"
Finally, lldb has a run until line or frame exit stepping mode:
(lldb) thread until 100 This command will run the thread in the current frame till it reaches line 100 in this frame or stops if it leaves the current frame. This is a pretty close equivalent to gdb's "until" command.
A process, by default, will shared the lldb terminal with the inferior process. When in this mode, much like when debugging with gdb, when the process is running anything you type will go to the STDIN of the inferior process. To interrupt your inferior program, type CTRL+C.
If you attach to a process, or launch a process with the "--no-stdin" option, the command interpreter is always available to enter commands. This might be a little disconcerting to gdb users when always have an (lldb) prompt. This allows you to set a breakpoint, etc without having to explicitly interrupt the program you are debugging:
(lldb) process continue
(lldb) breakpoint set --name stop_here
There are many commands that won't work while running, and the command interpreter should do a good job of letting you know when this is the case. If you find any instances where the command interpreter isn't doing its job, please file a bug. This way of operation will set us up for a future debugging mode called thread centric debugging. This mode will allow us to run all threads and only stop the threads that are at breakpoints or have exceptions or signals.
The commands that currently work while running include interrupting the process to halt execution ("process interrupt"), getting the process status ("process status"), breakpoint setting and clearing (" breakpoint [set|clear|enable|disable|list] ..."), and memory reading and writing (" memory [read|write] ...").
The question of disabling stdio when running brings up a good opportunity to show how to set debugger properties in general. If you always want to run in the --no-stdin mode, you can set this as a generic process property using the lldb "settings&qout; command, which is equivalent to gdb's "set" command. For instance, in this case you would say:
(lldb) settings set target.process.disable-stdio true Over time, gdb's "set command became a wilderness of disordered options, so that there were useful options that even experienced gdb users didn't know about because they were too hard to find. We tried to organize the settings hierarchically using the structure of the basic entities in the debugger. For the most part anywhere you can specify a setting on a generic entity (threads, for example) you can also apply the option to a particular instance, which can also be convenient at times. You can view the available settings with "settings list" and there is help on the settings command explaining how it works more generally.

LLDB - Starting or attaching to your program

To launch a program in lldb we use the "process launch" command or one of its built in aliases:
(lldb) process launch
(lldb) run
(lldb) r
You can also attach to a process by process ID or process name. When attaching to a process by name, lldb also supports the "--waitfor" option which waits for the next process that has that name to show up, and attaches to it
(lldb) process attach --pid 123
(lldb) process attach --name Sketch
(lldb) process attach --name Sketch --waitfor
After you launch or attach to a process, your process might stop somewhere:
(lldb) process attach -p 12345
Process 46915 Attaching
Process 46915 Stopped
1 of 3 threads stopped with reasons:
* thread #1: tid = 0x2c03, 0x00007fff85cac76a, where = libSystem.B.dylib`__getdirentries64 + 10, stop reason = signal = SIGSTOP, queue = com.apple.main-thread
Note the line that says "1 of 3 threads stopped with reasons:" and the lines that follow it. In a multi-threaded environment it is very common for more than one thread to hit your breakpoint(s) before the kernel actually returns control to the debugger. In that case, you will see all the threads that stopped for some interesting reason listed in the stop message.

LLDB - Setting watch points

In addition to breakpoints, you can use help watchpoint to see all the commands for watchpoint manipulations. For instance, we might do the following to watch a variable called 'global' for write operation, but only stop if the condition '(global==5)' is true:
(lldb) watch set var global
Watchpoint created: Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
    declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
(lldb) watch modify -c '(global==5)'
(lldb) watch list
Current watchpoints:
Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
    declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
    condition = '(global==5)'
(lldb) c
Process 15562 resuming
(lldb) about to write to 'global'...
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped
* thread #1: tid = 0x1c03, 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16, stop reason = watchpoint 1
    frame #0: 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16
   13   
   14   static void modify(int32_t &var) {
   15       ++var;
-> 16   }
   17   
   18   int main(int argc, char** argv) {
   19       int local = 0;
(lldb) bt
* thread #1: tid = 0x1c03, 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16, stop reason = watchpoint 1
    frame #0: 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16
    frame #1: 0x0000000100000eac a.out`main + 108 at main.cpp:25
    frame #2: 0x00007fff8ac9c7e1 libdyld.dylib`start + 1
(lldb) frame var global
(int32_t) global = 5
(lldb) watch list -v
Current watchpoints:
Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
    declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
    condition = '(global==5)'
    hw_index = 0  hit_count = 5     ignore_count = 0   
(lldb) 

LLDB - Setting break points

We've discussed how to set breakpoints above. You can use help breakpoint set to see all the options for breakpoint setting. For instance, we might do:
(lldb) breakpoint set --selector alignLeftEdges:
Breakpoint created: 1: name = 'alignLeftEdges:', locations = 1, resolved = 1
You can find out about the breakpoints you've set with:
(lldb) breakpoint list
Current breakpoints:
1: name = 'alignLeftEdges:', locations = 1, resolved = 1
  1.1: where = Sketch`-[SKTGraphicView alignLeftEdges:] + 33 at /Projects/Sketch/SKTGraphicView.m:1405, address = 0x0000000100010d5b, resolved, hit count = 0 
Note that setting a breakpoint creates a logical breakpoint, which could resolve to one or more locations. For instance, break by selector would set a breakpoint on all the methods that implement that selector in the classes in your program. Similarly, a file and line breakpoint might result in multiple locations if that file and line were inlined in different places in your code.
The logical breakpoint has an integer id, and it's locations have an id within their parent breakpoint (the two are joined by a ".", e.g. 1.1 in the example above.)
Also the logical breakpoints remain live so that if another shared library were to be loaded that had another implementation of the "alignLeftEdges:" selector, the new location would be added to breakpoint 1 (e.g. a "1.2" breakpoint would be set on the newly loaded selector).
The other piece of information in the breakpoint listing is whether the breakpoint location was resolved or not. A location gets resolved when the file address it corresponds to gets loaded into the program you are debugging. For instance if you set a breakpoint in a shared library that then gets unloaded, that breakpoint location will remain, but it will no longer be resolved.
One other thing to note for gdb users is that lldb acts like gdb with:
(gdb) set breakpoint pending on That is, lldb will always make a breakpoint from your specification, even if it couldn't find any locations that match the specification. You can tell whether the expression was resolved or not by checking the locations field in "breakpoint list", and we report the breakpoint as "pending" when you set it so you can tell you've made a typo more easily, if that was indeed the reason no locations were found:
(lldb) breakpoint set --file foo.c --line 12
Breakpoint created: 2: file ='foo.c', line = 12, locations = 0 (pending)
WARNING: Unable to resolve breakpoint to any actual locations.
You can delete, disable, set conditions and ignore counts either on all the locations generated by your logical breakpoint, or on any one of the particular locations your specification resolved to. For instance if we wanted to add a command to print a backtrace when we hit this breakpoint we could do:
(lldb) breakpoint command add 1.1
Enter your debugger command(s). Type 'DONE' to end.
> bt
> DONE
By default, the breakpoint command add command takes lldb command line commands. You can also specify this explicitly by passing the "--command" option. Use "--script" if you want to implement your breakpoint command using the Python script instead.
This is an convenient point to bring up another feature of the lldb command help. Do:
(lldb) help break command add
Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.

Syntax: breakpoint command add <cmd-options> <breakpt-id>
etc...
When you see arguments to commands specified in the Syntax in angle brackets like <breakpt-id>, that indicates that that is some common argument type that you can get further help on from the command system. So in this case you could do:
(lldb) help <breakpt-id>
<breakpt-id> -- Breakpoint ID's consist major and minor numbers; the major
etc...

Loading a program to LLDB

First we need to set the program to debug. As with gdb, you can start lldb and specify the file you wish to debug on the command line:
$ lldb /Projects/Sketch/build/Debug/Sketch.app
Current executable set to '/Projects/Sketch/build/Debug/Sketch.app' (x86_64).
or you can specify it after the fact with the "file" command:
$ lldb
(lldb) file /Projects/Sketch/build/Debug/Sketch.app
Current executable set to '/Projects/Sketch/build/Debug/Sketch.app' (x86_64).

LLDB - Examining thread state

Show the stack backtrace for the current thread.

(lldb) thread backtrace
(lldb) bt

Show the stack backtraces for all threads.

(lldb) thread backtrace all
(lldb) bt all 

Backtrace the first five frames of the current thread.

(lldb) thread backtrace -c 5
(lldb) bt 5 (lldb-169 and later)
(lldb) bt -c 5 (lldb-168 and earlier

Select a different stack frame by index for the current thread.

(lldb) frame select 12
(lldb) fr s 12
(lldb) f 12

List information about the currently selected frame in the current thread.

(lldb) frame info

Select the stack frame that called the current stack frame.

(lldb) up
(lldb) frame select --relative=1

Select the stack frame that is called by the current stack frame.

(lldb) down
(lldb) frame select --relative=-1
(lldb) fr s -r-1

Select a different stack frame using a relative offset.

(lldb) frame select --relative 2
(lldb) fr s -r2

(lldb) frame select --relative -3
(lldb) fr s -r-3

Show the general purpose registers for the current thread.

(lldb) register read

Write a new decimal value '123' to the current thread register 'rax'.

(lldb) register write rax 123

Disassemble the current function for the current frame.

(lldb) disassemble --frame
(lldb) di -f 

Disassemble any functions named main.

(lldb) disassemble --name main
(lldb) di -n main 


LLDB - Evaluating expressions

Evaluating a generalized expression in the current frame.

(lldb) expr (int) printf ("Print nine: %d.", 4 + 5)
or using the print alias:
(lldb) print (int) printf ("Print nine: %d.", 4 + 5)

Creating and assigning a value to a convenience variable.

In lldb you evaluate a variable declaration expression as you would write it in C:
(lldb) expr unsigned int $foo = 5

Printing the ObjC "description" of an object.

(lldb) expr -o -- [SomeClass returnAnObject]
or using the po alias:
(lldb) po [SomeClass returnAnObject]

Print the dynamic type of the result of an expression.

(lldb) expr -d 1 -- [SomeClass returnAnObject]
(lldb) expr -d 1 -- someCPPObjectPtrOrReference
or set dynamic type printing to be the default: (lldb) settings set target.prefer-dynamic run-target

Calling a function so you can stop at a breakpoint in the function.

(lldb) expr -i 0 -- function_with_a_breakpoint()

Calling a function that crashes, and stopping when the function crashes.

(lldb) expr -u 0 -- function_which_crashes()

LLDB - Examining Variables

Show the arguments and local variables for the current frame.

(lldb) frame variable
(lldb) fr v

Show the local variables for the current frame.

(lldb) frame variable --no-args
(lldb) fr v -a

Show the contents of local variable "bar".

(lldb) frame variable bar
(lldb) fr v bar
(lldb) p bar 

Show the contents of local variable "bar" formatted as hex.

(lldb) frame variable --format x bar
(lldb) fr v -f x bar 

Show the contents of global variable "baz".

(lldb) target variable baz
(lldb) ta v baz 

Show the global/static variables defined in the current source file.

(lldb) target variable
(lldb) ta v 

Display a the variable "argc" and "argv" every time you stop.

(lldb) target stop-hook add --one-liner "frame variable argc argv"
(lldb) ta st a -o "fr v argc argv"
(lldb) display argc
(lldb) display argv

Display a the variable "argc" and "argv" only when you stop in the function named main.

(lldb) target stop-hook add --name main --one-liner "frame variable argc argv"
(lldb) ta st a -n main -o "fr v argc argv"

Display the variable "*this" only when you stop in c class named MyClass.

(lldb) target stop-hook add --classname MyClass --one-liner "frame variable *this"
(lldb) ta st a -c MyClass -o "fr v *this"

LLDB - Watch point commands

Set a watchpoint on a variable when it is written to.

(lldb) watchpoint set variable global_var
(lldb) wa s v global_var

Set a watchpoint on a memory location when it is written into.

(lldb) watchpoint set expression -- my_ptr
(lldb) wa s e -- my_ptr

Set a condition on a watchpoint.

(lldb) watch set var global
(lldb) watchpoint modify -c '(global==5)'
(lldb) c

List all watchpoints.

(lldb) watchpoint list
(lldb) watch l

Delete a watchpoint.

(lldb) watchpoint delete 1
(lldb) watch del 1
 

LLDB - Breakpoint Commands

Set a breakpoint at all functions named main.

(lldb) breakpoint set --name main
(lldb) br s -n main
(lldb) b main 

Set a breakpoint in file test.c at line 12.

(lldb) breakpoint set --file test.c --line 12
(lldb) br s -f test.c -l 12
(lldb) b test.c:12 

Set a breakpoint at all C++ methods whose basename is main.

(lldb) breakpoint set --method main
(lldb) br s -M main

Set a breakpoint at and object C function: -[NSString stringWithFormat:].

(lldb) breakpoint set --name "-[NSString stringWithFormat:]"
(lldb) b -[NSString stringWithFormat:]

Set a breakpoint at all Objective C methods whose selector is count.

(lldb) breakpoint set --selector count
(lldb) br s -S count

Set a breakpoint by regular expression on function name.

(lldb) breakpoint set --func-regex regular-expression
(lldb) br s -r regular-expression

Ensure that breakpoints by file and line work for #included .c/.cpp/.m files.

(lldb) settings set target.inline-breakpoint-strategy always
(lldb) br s -f foo.c -l 12

Set a breakpoint by regular expression on source file contents.

(lldb) breakpoint set --source-pattern regular-expression --file SourceFile
(lldb) br s -p regular-expression -f file

List all breakpoints.

(lldb) breakpoint list
(lldb) br l

Delete a breakpoint.

(lldb) breakpoint delete 1
(lldb) br del 1
 

LLDB - Execution Commands

Launch a process no arguments.

(lldb) process launch
(lldb) run
(lldb)

Launch a process with arguments <args>.

(lldb) process launch -- <args>
(lldb) r <args> 

Launch a process for with arguments a.out 1 2 3 without having to supply the args every time.

% lldb -- a.out 1 2 3
(lldb) run
...
(lldb) run
...

Set environment variables for process before launching.

(lldb) settings set target.env-vars DEBUG=1
(lldb) set se target.env-vars DEBUG=1
(lldb) env DEBUG=1

Set environment variables for process and launch process in one command.

(lldb) process launch -v DEBUG=1

Attach to a process with process ID 123.

(lldb) process attach --pid 123
(lldb) attach -p 123 

Attach to a process named "a.out".

(lldb) process attach --name a.out
(lldb) pro at -n a.out 

Wait for a process named "a.out" to launch and attach.

(lldb) process attach --name a.out --waitfor
(lldb) pro at -n a.out -w 

Attach to a remote gdb protocol server running on system "eorgadd", port 8000.

(lldb) gdb-remote eorgadd:8000 

Attach to a remote gdb protocol server running on the local system, port 8000.

(lldb) gdb-remote 8000

Attach to a Darwin kernel in kdp mode on system "eorgadd".

(lldb) kdp-remote eorgadd 

Do a source level single step in the currently selected thread.

(lldb) thread step-in
(lldb) step
(lldb)

Do a source level single step over in the currently selected thread.

(lldb) thread step-over
(lldb) next
(lldb) n

Do an instruction level single step in the currently selected thread.

(lldb) thread step-inst
(lldb) si

Do an instruction level single step over in the currently selected thread.

(lldb) thread step-inst-over
(lldb) ni 

Step out of the currently selected frame.

(lldb) thread step-out
(lldb) finish


Command Structure of LLDB

Unlike gdb's command set, which is rather free-form, we tried to make the lldb command syntax fairly structured. The commands are all of the form:
<noun> <verb> [-options [option-value]] [argument [argument...]] The command line parsing is done before command execution, so it is uniform across all the commands. The command syntax for basic commands is very simple, arguments, options and option values are all white-space separated, and double-quotes are used to protect white-spaces in an argument. If you need to put a backslash or double-quote character in an argument you back-slash it in the argument. That makes the command syntax more regular, but it also means you may have to quote some arguments in lldb that you wouldn't in gdb.
Options can be placed anywhere on the command line, but if the arguments begin with a "-" then you have to tell lldb that you're done with options for the current command by adding an option termination: "--" So for instance if you want to launch a process and give the "process launch" command the "--stop-at-entry" option, yet you want the process you are about to launch to be launched with the arguments "-program_arg value", you would type:
(lldb) process launch --stop-at-entry -- -program_arg value We also tried to reduce the number of special purpose argument parsers, which sometimes forces the user to be a little more explicit about stating their intentions. The first instance you'll note of this is the breakpoint command. In gdb, to set a breakpoint, you might enter
(gdb) break foo.c:12 to break at line 12 of foo.c, and:
(gdb) break foo to break at the function foo. As time went on, the parser that tells foo.c:12 from foo from foo.c::foo (which means the function foo in the file foo.c) got more and more complex and bizarre, and especially in C++ there are times where there's really no way to specify the function you want to break on. The lldb commands are more verbose but also more precise and allow for intellegent auto completion.
To set the same file and line breakpoint in LLDB you can enter either of:
(lldb) breakpoint set --file foo.c --line 12
(lldb) breakpoint set -f foo.c -l 12
To set a breakpoint on a function named foo in LLDB you can enter either of:
(lldb) breakpoint set --name foo
(lldb) breakpoint set -n foo
You can use the --name option multiple times to make a breakpoint on a set of functions as well. This is convenient since it allows you to set commmon conditions or commands without having to specify them multiple times:
(lldb) breakpoint set --name foo --name bar Setting breakpoints by name is even more specialized in LLDB as you can specify that you want to set a breakpoint at a function by method name. To set a breakpoint on all C++ methods named foo you can enter either of:
(lldb) breakpoint set --method foo
(lldb) breakpoint set -M foo
To set a breakpoint Objective C selectors named alignLeftEdges: you can enter either of:
(lldb) breakpoint set --selector alignLeftEdges:
(lldb) breakpoint set -S alignLeftEdges:
You can limit any breakpoints to a specific executable image by using the "--shlib <path>" ("-s <path>" for short):
(lldb) breakpoint set --shlib foo.dylib --name foo
(lldb) breakpoint set -s foo.dylib -n foo
The --shlib option can also be repeated to specify several shared libraries.
Suggestions on more interesting primitives of this sort are also very welcome.
Just like gdb, the lldb command interpreter does a shortest unique string match on command names, so the following two commands will both execute the same command:
(lldb) breakpoint set -n "-[SKTGraphicView alignLeftEdges:]"
(lldb) br s -n "-[SKTGraphicView alignLeftEdges:]"
lldb also supports command completion for source file names, symbol names, file names, etc. Completion is initiated by a hitting a TAB. Individual options in a command can have different completers, so for instance the "--file <path>" option in "breakpoint" completes to source files, the "--shlib <path>" option to currently loaded shared libraries, etc. We can even do things like if you specify "--shlib <path>", and are completing on "--file <path>", we will only list source files in the shared library specified by "--shlib <path>".
The individual commands are pretty extensively documented. You can use the help command to get an overview of which commands are available or to obtain details about specific commands. There is also an apropos command that will search the help text for all commands for a particular word and dump a summary help string for each matching command.
Finally, there is a mechanism to construct aliases for commonly used commands. So for instance if you get annoyed typing:
(lldb) breakpoint set --file foo.c --line 12 you can do:
(lldb) command alias bfl breakpoint set -f %1 -l %2
(lldb) bfl foo.c 12
We have added a few aliases for commonly used commands (e.g. "step", "next" and "continue") but we haven't tried to be exhaustive because in our experience it is more convenient to make the basic commands unique down to a letter or two, and then learn these sequences than to fill the namespace with lots of aliases, and then have to type them all the way out.
However, users are free to customize lldb's command set however they like, and since lldb reads the file ~/.lldbinit at startup, you can store all your aliases there and they will be generally available to you. Your aliases are also documented in the help command so you can remind yourself of what you've set up.
One alias of note that we do include by popular demand is a weak emulator of gdb's "break" command. It doesn't try to do everything that gdb's break command does (for instance, it doesn't handle foo.c::bar. But it mostly works, and makes the transition easier. Also by popular demand, it is aliased to b. If you actually want to learn the lldb command set natively, that means it will get in the way of the rest of the breakpoint commands. Fortunately, if you don't like one of our aliases, you an easily get rid of it by running (for example):
(lldb) command unalias b I actually also do:
(lldb) command alias b breakpoint so I can run the native lldb breakpoint command with just b
The lldb command parser also supports "raw" commands, where, after command options are stripped off, the rest of the command string is passed uninterpreted to the command. This is convenient for commands whose arguments might be some complex expression that would be painful to backslash protect. For instance the "expression" command is a "raw" command for obvious reasons. The "help" output for a command will tell you if it is "raw" or not, so you know what to expect. The one thing you have to watch out for is that since raw commands still can have options, if your command string has dashes in it, you'll have to indicate these are not option markers by putting "--" after the command name, but before your command string.
lldb also has a built-in Python interpreter, which is accessible by the "script" command. All the functionality of the debugger is available as classes in the Python interpreter, so the more complex commands that in gdb you would introduce with the "define" command can be done by writing Python functions using the lldb-Python library, then loading the scripts into your running session and accessing them with the "script" command.
Having given an overview of lldb's command syntax, we proceed to lay out the stages of a standard debug session.

Features of LLDB

LLDB supports a broad variety of basic debugging features such as reading DWARF, supporting step, next, finish, backtraces, etc. Some more interested bits are:
  • Plug-in architecture for portability and extensibility:
    • Object file parsers for executable file formats. Support currently includes Mach-O (32 and 64-bit) & ELF (32-bit).
    • Object container parsers to extract object files contained within a file. Support currently includes universal Mach-O files & BSD Archives.
    • Debug symbol file parsers to incrementally extract debug information from object files. Support currently includes DWARF & Mach-O symbol tables.
    • Symbol vendor plug-ins collect data from a variety of different sources for an executable object.
    • Disassembly plug-ins for each architecture. Support currently includes an LLVM disassembler for i386, x86-64 , & ARM/Thumb.
    • Debugger plug-ins implement the host and target specific functions required to debug.
  • SWIG-generated script bridging allows Python to access and control the public API of the debugger library.
  • A remote protocol server, debugserver, implements Mac OS X debugging on i386 and x86-64.
  • A command line debugger - the lldb executable itself.
  • A framework API to the library.

Platform Support of LLDB

LLDB is known to work on the following platforms, but ports to new platforms are welcome:
  • Mac OS X desktop user space debugging for i386 and x86-64
  • iOS simulator debugging on i386
  • iOS device debugging on ARM
  • Linux local user-space debugging for i386 and x86-64
  • FreeBSD local user-space debugging for i386 and x86-64

Compiler Integration Benefits

LLDB currently converts debug information into clang types so that it can leverage the clang compiler infrastructure. This allows LLDB to support the latest C, C++, Objective C and Objective C++ language features and runtimes in expressions without having to reimplement any of this functionality. It also leverages the compiler to take care of all ABI details when making functions calls for expressions, when disassembling instructions and extracting instruciton details, and much more.
The major benefits include:
  • Up to date language support for C, C++, Objective C
  • Multi-line expressions that can declare local variables and types
  • Utilitize the JIT for expressions when supported
  • Evaluate expression Intermediate Representation (IR) when JIT can't be used

Overview on LLDB

From Wikipedia
The LLDB is a high-performance debugger. It is built as a set of reusable components which extensively use existing libraries from the larger LLVM Project, such as the Clang expression parser and LLVM disassembler.

LLDB aims to be modular and easy to use and maintains much of the familiarity of GDB. This allows GDB users to quickly ramp up to start using LLDB. LLDB is also able to disassemble machine code using LLVM libraries so as soon as new instructions are added to LLVM, they are automatically available in LLDB.

LLDB is the default debugger in Xcode on Mac OS X and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator.

All of the code in the LLDB project is available under the standard LLVM License, an open source "BSD-style" license.

The LLDB debugger APIs are exposed as a C++ object oriented interface in a shared library.

The lldb command line tool links to, and uses this public API. On Mac OS X the shared library is exposed as a framework named LLDB.framework, and unix systems expose it as lldb.so.

The entire API is also then exposed through Python script bindings which allow the API to be used within the LLDB embedded script interpreter, and also in any python script that loads the lldb.py module in standard python script files.

Sharing the LLDB API allows LLDB to not only be used for debugging, but also for symbolication, disassembly, object and symbol file introspection, and much more.