A Beginners Guide to LLDB in Xcode


Learn how to debug your Xcode projects
Explore advanced LLDB Breakpoints
Keep your code free of NSLogs

Often times, programmers will add NSLogging to their projects to capture the current outputs of variables or formulas used throughout their app.

NSLog(@"%@",self.titleString);
NSLog(@"%f",(self.weatherSlider.value-self.weatherHourOffset)/24);

Pretty soon your code and the subsequent LLDB output are a mess, littered with NSLog statements buried across multiple classes. Fear not, Xcode gives us powerful tools for monitoring and debugging our projects without crappin’ up our code or the console output.

Breakpoints


Breakpoints are created by single left-clicking on the row count in the left margin of your Xcode window. They appear as blue flags and will pause your application at that exact line in the code.

Breakpoint on row 181

When you run your application in the iOS Simulator or through your phone, the breakpoint with only trigger and pause your application if the code at the breakpoint is executed. In the above example, if the self.hourSlider.value is less than 2400, the breakpoint will not trigger.

Basic Commands

When your breakpoint does trigger, Xcode comes into focus with the breakpoint highlighted in green. You can then enter commands into the debug console in the bottom right of the screen.

Here is a quick guide of basic commands:

p = print  // This will take the arguments given and print the output using the compiler language and store it in its own variable for use in the debugger (designated by a $n)                                
The output from row #2 is stored in variable $1. The variable is then used on row #3 to represent 8 + 3

You can even use the p command to reassign variables and properties in your project! Just use the = operator. You can then see the updated value immediately, but the app will only update when you hit the play button |> in the debug console.

po = print object  // This will print the object's description method. It does not store a variable. 
Note that po shows all the properties and memory address of the object. p simply shows the memory address and stores the variable

Note: The lldb debugger obeys scope, so you will only have access to local variables in the method or class properties. You do have the ability to call other methods in the class like the below.

p [self updateGradient]


Breakpoints Advanced

Now that we have the basics down, let’s look at the really useful advanced features of breakpoints.

Exception Breakpoint

The most useful of which in my opinion is the “Exception Breakpoint”. Navigate to the breakpoint section on the left hand side and click the + button on the bottom and select

This breakpoint once added will insert a breakpoint extremely close to the location in code that causes an exception such as “Index out of bounds”. See the before and after screenshots below:

No exception breakpoint
Exception breakpoint

Breakpoint Conditions

Start by right-clicking and editing any breakpoint.

In the prompt that opens you can enter conditions for variables in the scope of the breakpoint. For example, you could design a breakpoint to only fire in a For loop when i == 16.

Breakpoint Actions

If you select the Add Action button there are a wide variety of additional configurations. In the example below, I add an NSLog for the current military time when i == 16 in my for loop. I then hit the + sign and also added a sound to play when that is hit so I can keep my eyes on the app instead of the debug console. Finally, I check the box at the bottom for “Automatically continue after evaluating actions”. This will prevent the breakpoint from pausing the application, but still log the messages configured for that breakpoint.

Breakpoint Collaboration

Now that you can make super awesome breakpoints, you want the ability to share these breakpoints with your project team members. Yet, when you Git Add/Commit/Push, your teammates don’t see your breakpoints. Say what?!?

To share your breakpoints with teammates, simply right-click on the breakpoint and hit share.

Shared breakpoints will be displayed as follows: