Xcode and the Power of LLDB: Real-Time Application Management
By: Dmytro Pylypenko
Tags: Programming, Swift, iOS, Mobile, Xcode

Commands

Why should I use any commands via terminal and know LLDB API, if I already have a nice GUI in Xcode?

The main reason of knowing the API is next — having this knowledge you will know what features it provides and what you can build over it, you will be able to react faster, being not dependent from not implemented features in UI, and learn about inner parts of the application, memory, registers, etc.

Process

process status — shows the current status of debugging, of the application, in selected thread and current frame in Call Stack. For reports automation, it can be useful to send via Python-script.

Call Stack

During executing the code, call stacks are being created and contain stack frames: function arguments, local variables of function, and addresses to return when call finishes.

And navigating through Call Stack to examine the application flow you can use and find more detailed information in:

Thread

In XCode you can find GUI in the left panel, in Debug Navigator, CMD+7.

Xcode Debug Navigator Thread

thread backtrace — gives your list of stack frames of the current thread.

If you want to get list from all threads — run thread backtrace all , and respectively thread backtrace unique to see grouped by unique call stacks.

For navigating between threads use thread select command with the number of needed thread for reaching stack frames.

thread list — gets all threads list with information and see selected thread within astericks (*)

thread return — stubs the return value from the function in the frame. Write the command: thread return false , for simulation needed behavior. But you can return only with scalar types. Execution of the command takes some time.

Frame

The command gives the possibility to select and examine the current thread’s stack frames.

frame variable , in short frame var , or shorter is v , using it retrieves descriptive information of a value, if provided, from the current stack frames, using debug information and memory. Otherwise, it gives verbose information about the frame.

Command accepts useful flags, which gives useful information about the value. Thus, if you need to examine the value, be sure that you located it in the appropriate frame, where the variable for access exists. You can check information about the current frame using frame info .

So, if we want to examine the value thing , which has been initialized and exists in memory:

Using specific flags you get appropriate output information about the value:

As you’ve probably noticed, there’s no chance variable in the output. This is because chance is the computed variable and can be only evaluated, by expression — more about this in the next paragraph.

navigation

If you need to move between stack frames, lldb has commands up and down , which can accept arguments of the offset, thus if you at very top, and want to move to the actual level — write down 10000 (it will reach counting the offset or the edge of levels)

Based on these commands we have already graphic user interface (GUI) in Xcode, like switching between threads, frames, evaluating values, watching CPU/Memory/Disk/Network reports, etc.