KEMBAR78
Advanced debugging | PDF
Advanced Debugging
Xcode
By



Ali Akhtar
LLDB
• Debugger in Xcode

• LLDB replaced GDB

• Xcode 5

• Clang is an "LLVM native" C/C++/Objective-C compiler
Optional Types
Raw Display
Static/Dynamic Types
Dynamic Dispatch
Dynamic Dispatch
Dynamic Dispatch
• Use final keyword
• Declaration can’t be overridden
• Allows the compiler to safely elide dynamic dispatc
indirection.
• final keyword enables the sealing of classes and
methods.
Dynamic Type in LLDB
Dynamic Type in LLDB
Protocol
Protocol
Protocol
Debugging Optimized Swift
Code
Objective C and Swift
Variable View
Objective C and Swift
Expression
Objective C and Swift
Expression
Objective C and Swift
Expression
Can’t access utcDate
Expression modify
variable of program
Data Formatters for Swift
Objects
Data Formatters for Swift
Objects
Uniqueness
Uniqueness
Uniqueness
Uniqueness
Commands lldb
• bugreport (full report of current app’s state)

• frame info 

• refcount (shows you reference count for the specific
object)
Commands lldb
Control app’s execution
flow
• Automate debugging. Stuffs
• Speedup Debugging Process
Add Actions
Command Alias
Exploring Memory Address
Defining Reusable
Functions
User Defined Predicate
BreakPoint
Xcode BreakPoint
Multiple Locations
Multiple Locations
View Hierarchy Debugging
• Ability to inspect UI hierarchy
• app’s hierarchy, views, constraints, and view controllers, displayed as a hierarchical list
• Object inspector and size inspector
View Hierarchy Debugging
View Debugging
View Debugging
Address Sanitizer
• Track down memory violations 

• Finds memory corruptions

•  Finds Memory errors at runtime

• LLVM based tool for C languages and Swift 

• These crashes are not deterministic
Limitation
• Detect memory errors at runtime

• Cannot detect memory leaks

•  Cannot detect access uninitialised memory
Address Sanitizer
Advanced_Apple_Debugging_&_Reverse_Engineering_v2.0.pdf
Address Sanitizer
• strlen(s) gives you the length of the string held in
the s variable, up to the first NULL character
Address Sanitizer
use-after-free bug
use-after-free bug
Out-of-Scope
Solution
1)Variable aren’t accessed outside of their
scope
2)Allocate memory using malloc function
Use of Stack Memory After Function
Return
Use of Stack Memory After Function Return
• Function returns a pointer to its local variable

• Once the function returns, that pointer is no longer valid

•  If we use we are again accessing garbage memory

• Either crash or unexpected behaviour
Solution
• Use pointer arguments
 Memory Graph Debugger
• Find  retain cycle

• Find  memory leaks

• Pause app execution when activated

• Display objects currently in a heap

• Live Allocations uncheck when not using

• Clean build when using
1) How to Use
2) How to Use
Allocations
Allocations
Allocations
Memory Leaks
Controller TableView
Visible Cell
Visible Cell
Visible Cell
Visible Cell
Closure
Memory Leaks
Thinking
• Pop Controller

• Why it still in heap

• dealloc also not called 

• Not a simple leak so Memory Graph Debugger don’t tell

• Using heap content we identify
Memory Leaks
Memory Leaks
Memory Leaks
No controller in heap content also
Thread Sanitizer (TSan)
Threading Issues

• Hard to consistently reproduce

• Difficult to debug (Timing matter)

• Lead to unpredictable results
Thread Sanitizer (TSan)
• Use of uninitialised mutexes

• Thread leak (missing pthread_join)

• Unsafe calls in signal handlers

• Unlock from wrong thread

• Data races / Race condition (access same memory
location without proper sync )

• Xcode 8
Thread Sanitizer (TSan)
Data Race
Reasons
Data Race example
Data Race example
Data Race example
Lazy Initialization in Objective C
Lazy Initialization in Swift
Race Condition
Solution
Swift Access Races
• Multiple threads call a mutating method on Structs

• Pass a shared variable as inout  without systematic
manner

• Access races can result in unpredictable behavior
Swift Access Races
Races on Collections and
Other APIs
Races on Collections and
Other APIs
Uninitialised Mutexes
• Detects when a mutex is used before it’s initialized.

• pthread_mutex_lock(_:) or pthread_mutex_unlock(_:) is
called with a pthread_mutex_t variable that wasn’t
initialized

• Attempting to use an uninitialized mutex results in an error
Uninitialised Mutexes
Solution
Main Thread Checker
• Detect invalid use of AppKit, UIKit, and other APIs from a
background thread.

• Missed UI updates

• Visual defects
Main Thread Checker
Main Thread Checker
Static Analyzer
•  Find bugs in your code before you even run your app

• Possible code paths in a few seconds, reporting potential
bugs 

•  Identifies areas in your code that don’t follow
recommended API
Static Analyzer
Problems
• Logic flaws, such as accessing uninitialized variables and
dereferencing null pointers

• Dead store (unused variable) flaws

• API usage flaws
Examples

Advanced debugging