WWDC 2018 — Advanced Debugging with Xcode and LLDB — 412
Summary: We should avoid re-run of our code after every fix we make, and continue debugging.
We will cover following points today:
- Change / Inject code
- Print Assembly Code Values
- Adding symbolic breakpoint through lldb command
- Skip a Code Line
- Custom Debug Description
- Watchpoint
- Running a command in objective C mode
- Creating a command alias
- Using python script in LLDB
- Updating animation from debugger
- Constraint Debugging
Change / Inject code
To edit a value at breakpoint use the below lldb command
expression variable_name = value
You can also configure breakpoint to have this done automatically for you. Click on breakpoint > Edit Breakpoint
Inject code: Lets say you forgot to set delegate, now instead of rerun use the same expression to inject and continue debugging.
Print Assembly Code Values
Lets say you have setup a Symbolic Breakpoint for UIlabel setText method
Now when the breakpoint hits how do you print the value when you see a debug point in assembly code
at this point try these commands and observe the values
po $arg1
po (SEL)$arg2
po $arg3
Adding symbolic breakpoint through lldb command
Lets say we want to activate a symbolic breakpoint after another breakpoint has been hit. Then we can create a breakpoint with following debugger command.
breakpoint set --one-shot true --name "-[UILabel setText:]"
Skip a Code Line
Put a breakpoint for the line to be skipped, then simply drag button with two lines to next line.
We want to skip this line every-time it is hit, lets configure breakpoint for it
thread jump --by 1
Custom Debug Description
for custom classes we can implement this protocol to get description in LLDB
Watchpoint
Watchpoint is like a breakpoint which pauses each time that property is changed. This can be set using the context menu in the variables section of debugger.
Running a command in objective C mode
There are objective C libraries, for which you want to send command in ObjC manner or send a command without swift’s strict type check. To do that you can use below expression command
expression -l objc -o --[`self.view` recursiveDescription]
Back ticks are optional, these are like a preprocessor step to evaluate first and then insert the result.
Creating a command alias
Imagine if you have to type this code again and again expression -l objc -o —
command alias poc expression -l objc -o --
Using python script in LLDB
import python file: command script import ~/nudge.py
Download nudge.py from here Use Scripts to Add Custom Commands to LLDB
Updating animation from LLDB
change the required values using unsafeBitCast for memory address or expression command then use CATransaction.flush() to perform animation while debugging
Constraint debugging
Open Xcode’s Visual Debugging mode > select the constraint > copy the constraint from view debugger.
Now when you paste in LLDB with po you get the address of it and you can continue updating it with expression command and then CATransaction.flush()
Enable “Malloc Stack” for stack trace in visual inspector.
If you have implemented Custom Debug Description then you will see that too in this inspector.
xcode’s debug bar has new light and dark mode option for the target app.