Knowing your way around debugging tools will make you a better, more productive and happier programmer. Learn some handy tricks to get you started.
In this article I will share a few tips on how to use some of the debugging tools that come integrated in Xcode. I’ll show you some LLDB commands and how to use them along with breakpoints. Hopefully I’ll provide you with new weapons to analyse your code and find bugs.
Xcode integrates a set of debugging tools throughout its main window. The image below shows the layout of the Xcode debugger when the app is paused at a breakpoint.
LLDB is an open-source debugger, which is part of the LLVM compiler development suite. It comes bundled with Xcode and you can find it in the console at the bottom right side of the window, as pictured in the image above.
Here are some fundamental LLDB commands:
The help command lists all the commands available. If you want to know more about a specific command you can type help <command> to get all the details on this specific command.
The print command is extremely useful. It allows you to print variable values on the console, making it unnecessary to resort to technics such as logging variable values or simplifying the behavior of the program.
The image above shows a very common practice, which consists of logging a variable in order to inspect its value.
By placing a breakpoint after the variable is assigned with its value (line 5), the program will pause its execution. Here we can use the print command to inspect the value of the variable as shown in the picture.
LLDB does prefix matching, so you will be fine if you type prin, pri or p. You can also use $0 to reference the result and type, for example, p $0 + 10 to get a result of 109.
The expression command is also very useful as it allows you to modify the value of a variable. In the example above we have changed the value of count to 80, by using the expression command. As you can see, not only it changed the value of the variable in the debugger but it actually changed it in the program. You can observe that from the result of the print command.
The picture below shows you Xcode’s breakpoint manager. Here you can manage the list of breakpoints in your program, and perform other actions such as deleting or enabling and disabling breakpoints.
A very common practice is using breakpoints to stop the execution of a program, inspect its state and search for bugs. However, there is more to it…
Breakpoints allow us other possibilities as we already demonstrated in this article.
By using breakpoints we have demonstrated that we can instruct the program when to stop, and run commands on it using LLDB, allowing us to inspect and modify variables.
Xcode allows us to create symbolic breakpoints, which can save you a lot of time finding out a bug.
To create a symbolic breakpoint click on the “+” button on the bottom left corner of XCode debug manager, and choose Symbolic Breakpoint.
A popover will appear allowing you to insert any symbol, and the breakpoint will cause the program to stop whenever this symbol is executed anywhere in your program, or in Apple’s code.
In the picture, you can see an example of this. I’m converting the variable count to an object of type NSNumber. I inserted the symbol [NSNumber numberWithUnsignedInteger:] in order have the program stop when the conversion is made.
As you can see in the picture, the program stopped when the symbol was executed even without having a breakpoint in that specific line of code.
Combining LLDB with Breakpoints
Using LLDB commands combined with breakpoints opens a lot of possibilities.
I’m going to show you two practical examples to demonstrate how to combine these two tools to help you get more information from your program and perform changes on it if you wish.
In the example above, I used a breakpoint to stop the program and by using the LLDB command po, which evaluates an expression on the current thread, managed to retrieve the view hierarchy of a view controller.
With the information I got on the view hierarchy I can do some manipulation using LLDB.
Using the memory reference from the UIView (0x7fec90406350) I was able to stash it and then change it’s background color, using the expression command.
As you can see in the picture, after continuing the execution of the program the background color of the view was modified to red.
Let’s try another example. I added a button to the view controller, using the storyboard and connected it has an IBOutlet. I called it myButton.
Using the po command I found out who is receiving the actions when the button is tapped.
First I got all the targets associated with myButton, and with the information I got from that evaluation, I managed to extract the actions associated with it.
Wrapping it up…
In this article, I tried to provide a small insight on how debugging tools might help you to understand your code, and save you a lot of time.
There is a lot more you can do with LLDB and Breakpoints, so go ahead and explore!
Written by João Gameiro | Mobile Developer at Cleverti