Presented by Yahya
Saddiq
17 March 2019
Debugging
With Xcode and LLDB
Vodafone Proprietary classified as C2 - Internal
The
Agenda
3 6 November 2019
1. The 5
stages of
debugging
3. The 5
execution
controls
2. Basic
debugging
4. Advanced
debugging
Vodafone Proprietary classified as C2 - Internal
The 5 Stages of
Debugging
Vodafone Proprietary classified as C2 - Internal
Denial
5 6 November 2019
“What do you mean it’s broken!?
That code has worked forever!”
Vodafone Proprietary classified as C2 - Internal
Anger
6 6 November 2019
[Expletive], now I have to fix this
broken piece of crap…
Vodafone Proprietary classified as C2 - Internal
Bargaining
7 6 November 2019
“Hmm, maybe if I hack this bit up
here that will fix it?”
Vodafone Proprietary classified as C2 - Internal
Depression
8 6 November 2019
“It’s all broken, it can’t be fixed.
Maybe I should just go live on a farm.
Is the janitorial staff hiring?”
Vodafone Proprietary classified as C2 - Internal
Acceptance
9 6 November 2019
“Whew! That was a rough bug but I
finally got it fixed!
How did that code ever even work in
the first place? (shrug) oh well on to
the next task…”
Vodafone Proprietary classified as C2 - Internal
Making errors is part of the learning process. In
programming, these annoying things are known as 🐞’s
(bugs).
Being a thorough and efficient debugger will make you a
better programmer and eventually allow you to write
excellent code in the long run.
Vodafone Proprietary classified as C2 - Internal
Avoid re-run code after every
fix, and continue debugging.
Vodafone Proprietary classified as C2 - Internal
What is a breakpoint?
6 November 201912
• A breakpoint is a debugging tool that allows you to pause the execution of your
program up to a certain moment.
Vodafone Proprietary classified as C2 - Internal
Why should I use a breakpoint?
6 November 201913
• Creating “pause” points in your code can help you investigate your code to see
where bugs occur.
Vodafone Proprietary classified as C2 - Internal
How do I create a breakpoint?
6 November 201914
• Decide on where you want to pause the execution of your code and click in the left
gutter to create a blue breakpoint.
Vodafone Proprietary classified as C2 - Internal
The 5 Execution
Controls
Vodafone Proprietary classified as C2 - Internal
Activate/deactivate
Breakpoints
16 6 November 2019
keyboard shortcut is ⌃ + ⌘ + Y
Vodafone Proprietary classified as C2 - Internal
Continue/Pause
Execution
17 6 November 2019
keyboard shortcut is ⌘ + Y
Vodafone Proprietary classified as C2 - Internal
Step Over
18 6 November 2019
keyboard shortcut is F6
Vodafone Proprietary classified as C2 - Internal
Step Into
19 6 November 2019
keyboard shortcut is F7
Vodafone Proprietary classified as C2 - Internal
Step Out
20 6 November 2019
keyboard shortcut is F8
Vodafone Proprietary classified as C2 - Internal
Advanced Debugging
Vodafone Proprietary classified as C2 - Internal
Breakpoint editor
6 November 201922
• Breakpoints play an important role in debugging, and Xcode offers powerful
functions with them.
• Breakpoints tips to boost your efficiency while debugging.
– Sum value when an iterator equals to 60
– Sum value only if an iterator is greater than or equal to 90
– As previous plus without manually continue program execution and see all sum values at once.
Vodafone Proprietary classified as C2 - Internal
Change / Inject code
6 November 201923
• We use it to edit a value at a breakpoint
• Inject code: Lets say you forgot to set delegate, now instead of rerun we can use
the same expression to inject and continue debugging.
expression variable_name = value
Vodafone Proprietary classified as C2 - Internal
6 November 201924
Print Assembly Code Values
• Printing values of a Symbolic Breakpoint for UIlabel setText method
• Let the debugger show you the code setting a certain label text
– Symbol
– Condition
– Action: Debugger Command
-[UILabel setText:]
(BOOL)[(NSString*)$arg3 isEqualToString:@"Standard double room"]
po $arg3
Vodafone Proprietary classified as C2 - Internal
6 November 201925
Adding symbolic breakpoint through LLDB
command
• We use it to activate a symbolic breakpoint after another breakpoint has been hit.
breakpoint set --one-shot true --name "-[UILabel setText:]"
Vodafone Proprietary classified as C2 - Internal
6 November 201926
Skip a code line/specific line
• Put a breakpoint for the line to be skipped or to go to specific line.
thread jump --by 1
thread jump --line 102
Note: this move the program counter, and that could put the program into a broken state and lead to crashes.
Vodafone Proprietary classified as C2 - Internal
6 November 201927
Watchpoint
• Watchpoint is like a breakpoint which pauses each time that property is changed.
Vodafone Proprietary classified as C2 - Internal
6 November 201928
Inspecting and updating UIView on the fly
• Another magic we can do with LLDB commands is to inspect view frame with
position and size in the debugger and changing their position and sizes while the
code is paused waiting for further actions.
– Inspect UILabel
– Modify its width
– Move it
– Change its background color
– Change its text
po textLabel?.description
po unsafeBitCast(0x0, to: UILabel.self).backgroundColor = .green
po unsafeBitCast(0x0, to: UILabel.self).frame.origin.y = 75
po unsafeBitCast(0x0, to: UILabel.self).frame.size = CGSize(width: 200, height: 44)
po unsafeBitCast(0x0, to: UILabel.self).text = debugging is awesome
Vodafone Proprietary classified as C2 - Internal
Be awesome,
Keep debugging

Basic + Advanced Debugging

  • 2.
    Presented by Yahya Saddiq 17March 2019 Debugging With Xcode and LLDB
  • 3.
    Vodafone Proprietary classifiedas C2 - Internal The Agenda 3 6 November 2019 1. The 5 stages of debugging 3. The 5 execution controls 2. Basic debugging 4. Advanced debugging
  • 4.
    Vodafone Proprietary classifiedas C2 - Internal The 5 Stages of Debugging
  • 5.
    Vodafone Proprietary classifiedas C2 - Internal Denial 5 6 November 2019 “What do you mean it’s broken!? That code has worked forever!”
  • 6.
    Vodafone Proprietary classifiedas C2 - Internal Anger 6 6 November 2019 [Expletive], now I have to fix this broken piece of crap…
  • 7.
    Vodafone Proprietary classifiedas C2 - Internal Bargaining 7 6 November 2019 “Hmm, maybe if I hack this bit up here that will fix it?”
  • 8.
    Vodafone Proprietary classifiedas C2 - Internal Depression 8 6 November 2019 “It’s all broken, it can’t be fixed. Maybe I should just go live on a farm. Is the janitorial staff hiring?”
  • 9.
    Vodafone Proprietary classifiedas C2 - Internal Acceptance 9 6 November 2019 “Whew! That was a rough bug but I finally got it fixed! How did that code ever even work in the first place? (shrug) oh well on to the next task…”
  • 10.
    Vodafone Proprietary classifiedas C2 - Internal Making errors is part of the learning process. In programming, these annoying things are known as 🐞’s (bugs). Being a thorough and efficient debugger will make you a better programmer and eventually allow you to write excellent code in the long run.
  • 11.
    Vodafone Proprietary classifiedas C2 - Internal Avoid re-run code after every fix, and continue debugging.
  • 12.
    Vodafone Proprietary classifiedas C2 - Internal What is a breakpoint? 6 November 201912 • A breakpoint is a debugging tool that allows you to pause the execution of your program up to a certain moment.
  • 13.
    Vodafone Proprietary classifiedas C2 - Internal Why should I use a breakpoint? 6 November 201913 • Creating “pause” points in your code can help you investigate your code to see where bugs occur.
  • 14.
    Vodafone Proprietary classifiedas C2 - Internal How do I create a breakpoint? 6 November 201914 • Decide on where you want to pause the execution of your code and click in the left gutter to create a blue breakpoint.
  • 15.
    Vodafone Proprietary classifiedas C2 - Internal The 5 Execution Controls
  • 16.
    Vodafone Proprietary classifiedas C2 - Internal Activate/deactivate Breakpoints 16 6 November 2019 keyboard shortcut is ⌃ + ⌘ + Y
  • 17.
    Vodafone Proprietary classifiedas C2 - Internal Continue/Pause Execution 17 6 November 2019 keyboard shortcut is ⌘ + Y
  • 18.
    Vodafone Proprietary classifiedas C2 - Internal Step Over 18 6 November 2019 keyboard shortcut is F6
  • 19.
    Vodafone Proprietary classifiedas C2 - Internal Step Into 19 6 November 2019 keyboard shortcut is F7
  • 20.
    Vodafone Proprietary classifiedas C2 - Internal Step Out 20 6 November 2019 keyboard shortcut is F8
  • 21.
    Vodafone Proprietary classifiedas C2 - Internal Advanced Debugging
  • 22.
    Vodafone Proprietary classifiedas C2 - Internal Breakpoint editor 6 November 201922 • Breakpoints play an important role in debugging, and Xcode offers powerful functions with them. • Breakpoints tips to boost your efficiency while debugging. – Sum value when an iterator equals to 60 – Sum value only if an iterator is greater than or equal to 90 – As previous plus without manually continue program execution and see all sum values at once.
  • 23.
    Vodafone Proprietary classifiedas C2 - Internal Change / Inject code 6 November 201923 • We use it to edit a value at a breakpoint • Inject code: Lets say you forgot to set delegate, now instead of rerun we can use the same expression to inject and continue debugging. expression variable_name = value
  • 24.
    Vodafone Proprietary classifiedas C2 - Internal 6 November 201924 Print Assembly Code Values • Printing values of a Symbolic Breakpoint for UIlabel setText method • Let the debugger show you the code setting a certain label text – Symbol – Condition – Action: Debugger Command -[UILabel setText:] (BOOL)[(NSString*)$arg3 isEqualToString:@"Standard double room"] po $arg3
  • 25.
    Vodafone Proprietary classifiedas C2 - Internal 6 November 201925 Adding symbolic breakpoint through LLDB command • We use it to activate a symbolic breakpoint after another breakpoint has been hit. breakpoint set --one-shot true --name "-[UILabel setText:]"
  • 26.
    Vodafone Proprietary classifiedas C2 - Internal 6 November 201926 Skip a code line/specific line • Put a breakpoint for the line to be skipped or to go to specific line. thread jump --by 1 thread jump --line 102 Note: this move the program counter, and that could put the program into a broken state and lead to crashes.
  • 27.
    Vodafone Proprietary classifiedas C2 - Internal 6 November 201927 Watchpoint • Watchpoint is like a breakpoint which pauses each time that property is changed.
  • 28.
    Vodafone Proprietary classifiedas C2 - Internal 6 November 201928 Inspecting and updating UIView on the fly • Another magic we can do with LLDB commands is to inspect view frame with position and size in the debugger and changing their position and sizes while the code is paused waiting for further actions. – Inspect UILabel – Modify its width – Move it – Change its background color – Change its text po textLabel?.description po unsafeBitCast(0x0, to: UILabel.self).backgroundColor = .green po unsafeBitCast(0x0, to: UILabel.self).frame.origin.y = 75 po unsafeBitCast(0x0, to: UILabel.self).frame.size = CGSize(width: 200, height: 44) po unsafeBitCast(0x0, to: UILabel.self).text = debugging is awesome
  • 29.
    Vodafone Proprietary classifiedas C2 - Internal Be awesome, Keep debugging