12/08/2018, 14:52

LLDB Debugger

If you don't know what means LLDB abbreviation, then this post is for you! I am quite serious. This tool makes life much easier when you publish your iOS applications. Honestly, how many times you had to write NSLog or print to debug some variable? How many times you had to recompile ...

If you don't know what means LLDB abbreviation, then this post is for you! I am quite serious. This tool makes life much easier when you publish your iOS applications. Honestly, how many times you had to write NSLog or print to debug some variable? How many times you had to recompile application to check new parameters? Or have you changed color of UIView just for debugging?

That with this tool without writing additional lines in the code you can do this:

  • Print the parameters
  • Change parameters value
  • Change UI parameters value
  • Create new parameters
  • Execute different pieces of code for Debug and Release

So, are you interested? Then let's begin!!!

What is LLDB?

LLDB is a next generation, high-performance debugger. It is built as a set of reusable components which highly leverage existing libraries in the larger LLVM Project, such as the Clang expression parser and LLVM disassembler. 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.

Create a simple application, in order to put a breakpoint inside viewDidLoad() method, click on the beginning of the line.

Now run the application, and you will see the following:

The application stopped on the breakpoint line. There is LLDB console on the lower right corner of the window. There are visualized parameters of the controller to the left of the LLDB console. The leftmost column is the navigator, where you can see all threads and working processes of application. However all attention of toda's post is be given exclusively to LLDB console. LLDB is a today's star!

Commands

There are three most important commands, which you need to know to work with LLDB successfully: print, expression and print object These commands are just variants of expression command with different flags, but it doesn't matter for now.

Print

print is used to display parameter's values and it creates a new parameter with printed value. Example:

(lldb) print count
(Int) $R0 = 4

Now we have initialized a new parameter called $R0 In the console print is aliased as p, which is much more convenient and faster to type

(lldb) p count
(Int) $R0 = 4

Expression

expression is used to modify a parameter without restarting compiled application.

(lldb) expression count = 32
(lldb) p count 
(Int) $R1 = 32

+In the console expression is aliased as e

(lldb) e count = 32
(lldb) p count 
(Int) $R1 = 32

Also by using expression you can create new parameters. Name of parameter must start with $.

(lldb) e var $myVar = 1234
(lldb) p $myVar
(Int) $R2 = 1234

Print Object

Print Object is the most suitable command to watch parameter value, because it works without creating new parameter. Command is e -O -- , but luckily it is aliased as po

(lldb) po count
4

Flow Control

When you have stopped the application and you need to proceed to the next line or continue process, you can use following commands: n - Step over. Executes the following line of code, but does not enter inside functions s - Step into. Executes the following line of code, and enters inside functions c - Continue process You can work without using this commands. There are buttons which do same actions as commands above in XCode.

Now try to change the value of count and see what happens.

(lldb) p count
(Int) $R0 = 4
(lldb) e count = 34
(lldb) s
(lldb) n
count ===> 10
(lldb) 

Why print("count ===> (count)") didn't print 40? Because the compiler analyzed code and detected that value of count is not changed. I don't know how how exactly compiler works, but I think that is some optimization algorithm. So make compiler think that count might change.

0