Developers spend a lot of time fixing bugs or, in other words, debugging. In fact, sometimes we spend more time fixing bugs than actually developing new features. As a result, debugging skills are crucial for your success as a developer, and in the following articles, we will try to leverage our debugging skills by learning about the tools xCode provides us.
What is LLDB?
A low-level debugger (LLDB) is the default debugger used in Xcode and is part of the LLVM project. LLDB replaced GDB in XCode 5 and has several advantages, such as performance and scriptability.
Evaluate Code in Your Console With Expression
One of the most useful and powerful commands in LLDB is
expression lets you evaluate code in the console and saves you precious time with debugging and testing your code. Not only can you run code, but you can also modify existing variables without recompiling your project.
Take a look at the following example:
In the above example, we have a struct name
person, with two fields called
age. We stopped the program with a breakpoint and changed the
Now, when we print it again, we see that the actual value is modified during runtime. This is very useful when working on certain tasks in your app, such as animations, colors, or just values to see how your app behaves.
Make Your Life Easier in the Console By Creating an Alias
Do your fingers hurt from typing all day in the console? You can easily define an alias to make your life easier!
Let’s say we want to create an alias that increases the age property of
person by 1:
(lldb) command alias a1 expression person.age += 1
So the lldb command format for creating an alias is:
“command alias” + <name of alias> + <actual command>
Print Variables Nicely With PO and P
PO is the most common command developers use when they debug in LLDB. So what actually is
Well, we already know what an
expression is and what an
alias is. So
PO is an
alias that print variables nicely, that does:
expression -O -l swift — <name of variable>
What do we mean by “nicely”? Every object can have a textual representation, and you can override this by implementing a protocol named
This protocol has one variable you need to override called
debugDescription, and its returned value appears in the console and the debugger.
PO command is not alone — we also have the
P is used to print variables — and these are mostly not objects with some internal formatter.
If you try to
PO something which is not an actual object (like
Int), you can get the results of an unrelated objective-c object.
Print Variables Faster With the V Command
Before we understand what the
V command does, we need to understand how
When you call a
PO command in LLDB, it generates a small piece of code that should go and evaluate the expression you wrote. Afterward, it compiles the code, executes it, and moves it to some data formatter (in the case of
P) or runs the
debugDescription method (in case of
PO) to print in in the console.
V command skips the evaluation of the code and just accesses the variable memory and passes the data it finds there to the data formatter to print it in the console. This means that using
V is much faster than
P since it doesn’t require and code evolution during debugging.
V command comes with a drawback — since
V doesn’t compile the code, it can only access variables that are on the current stack frame. Also, it cannot print computed properties since it needs to compile code for that.
V command is only available in XCode 10.2 and newer, and it’s an alias for frame expression.
Using the console is an important skill when debugging because it can save you precious time by injecting code in breakpoints, and it can reflect your app state by printing variables values easily.
But debugging doesn’t end here. In the next chapters, I will try to give more tips and tools to make your debugging sessions smoother and shorter.