Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tech Talk: Advanced Debugging with Xcode

Tech Talk: Advanced Debugging with Xcode

An internal presentation on advanced debugging, tips, and techniques for getting the most out of Xcode.

Avatar for Craig Pearlman

Craig Pearlman

March 29, 2018

Other Decks in Programming

Transcript

  1. Tech Talk: Advanced Debugging with Xcode Craig Pearlman, iOS Developer

    Fjord ATX © 2018 Fjord/Accenture Interactive
  2. Agenda » Why? » Gettin' Around » What's This Do?

    » Fun with Breakpoints » LLDB Is Your Friend » When Good Layouts Go Bad » Don't Forget Instruments! » Question Time Advanced Debugging with Xcode 2/53
  3. Why? » iOS 7 Tech Talks: Hidden iOS 7 Development

    Gems » https://developer.apple.com/videos/play/techtalks-ios-7/8/ » CocoaConf Boston 2014: Effective Xcode: Tips & Techniques by Laura Savino » Notes from Xcode Walkabout: https://gist.github.com/ lsavino/860a5bfa47c6834e890b » Leveling Up: Walk away having learned at least one thing Advanced Debugging with Xcode 3/53
  4. Open Quickly… » File ▸ Open Quickly… (⌘⇧O) » Append

    ":##" to jump to a specific line (ViewCon:12) » All symbols—classes, structs, enums, functions, etc. » Matches across symbol names in sequence: UINC will find UINavigationController Advanced Debugging with Xcode 5/53
  5. Useful Shortcuts Command Shortcut Navigate ▸ Reveal in Project Navigator

    ⌘⇧J Navigate ▸ Jump to Definition ⌃⌘J Navigate ▸ Jump to Next/Previous Issue ⌘'/⌘" Navigate ▸ Go Back/Forward ⌃⌘⇠/⌃⌘⇢ View ▸ Debug Area ▸ Activate Console ⌘⇧C View ▸ Debug Area ▸ Hide/Show Debug Area ⌘⇧Y View ▸ Standard Editor ▸ Show Document Items ⌃6 Debug ▸ Activate/Deactivate Breakpoints ⌘Y Advanced Debugging with Xcode 6/53
  6. Filters » Need to find a specific file? Use filters!

    » Great way to find all matching files in a complex hierarchy Advanced Debugging with Xcode 8/53
  7. Searching » Search can do much more than look for

    text » Search by type, limit match scope, and limit where to search » Limiting project scope useful to ignore Pods Advanced Debugging with Xcode 9/53
  8. Behaviors » Xcode ▸ Preferences… (⌘,) » Xcode ▸ Behaviors

    ▸ Edit Behaviors… Advanced Debugging with Xcode 10/53
  9. Arguments » Change app runtime behavior: » Launch arguments »

    Environment variables » NSHipster: Launch Arguments & Environment Variables » http://nshipster.com/launch- arguments-and- environment-variables/ Advanced Debugging with Xcode 12/53
  10. Diagnostic Options » Many arguments are available in the Edit

    Scheme sheet » Product ▸ Scheme ▸ Edit Scheme… (⌘<) » Set and Run: Option (⌥) + Product ▸ Run… (⌥⌘R) » Memory Allocator: Enabling the Malloc Debugging Features » https://developer.apple.com/library/ content/documentation/Performance/ Conceptual/ManagingMemory/ Articles/MallocDebug.html Advanced Debugging with Xcode 13/53
  11.  Code Diagnostics » Thread Sanitizer » https://developer.apple.com/documentation/code_diagnostics/thread_sanitizer » Address

    Sanitizer » https://developer.apple.com/documentation/code_diagnostics/address_sanitizer » Main Thread Checker » https://developer.apple.com/documentation/code_diagnostics/ main_thread_checker » Undefined Behavior Sanitizer » https://developer.apple.com/documentation/code_diagnostics/ undefined_behavior_sanitizer Advanced Debugging with Xcode 14/53
  12. Visual Memory Debugger » Shows a visual graph of allocated

    memory » Use Your Loaf: Xcode Visual Memory Debugger (source of the screen shots) » https://useyourloaf.com/blog/xcode-visual-memory-debugger/ » Pete the Developer: iOS — Identifying Memory Leaks using the Xcode Memory Graph Debugger » http://petethedeveloper.com/post/identityingmemoryleaksgraphdebugger/ Advanced Debugging with Xcode 15/53
  13. Localized Strings » -NSDoubleLocalizedStrings » Doubles the text to simulate

    languages with long words without breakable space » "cut" in German is ausschneiden (for example) » -NSShowNonLocalizedStrings YES » Shows text from non-localized strings IN ALL CAPS » Both are also available from the Options tab in Edit Scheme… Advanced Debugging with Xcode 17/53
  14. Set Language/Locale at Runtime » Run your app in Canadian

    French: » -AppleLanguages (fr) » -AppleLocale fr_CA » Use Your Loaf: Using Launch Arguments To Test Localizations » https://useyourloaf.com/blog/using-launch-arguments- to-test-localizations/ » Both are also available from the Options tab in Edit Scheme… Advanced Debugging with Xcode 18/53
  15. ⚠ WARNING Turn off these features when you no longer

    need them! Many have a significant performance impact! Advanced Debugging with Xcode 20/53
  16. Not These Breakpoints Try it anyway; it's music by geeks,

    for geeks (available on iTunes) Advanced Debugging with Xcode 22/53
  17. Conditional Breakpoints » No more print(), NSLog(), or os_log() just

    to debug » Set a conditional breakpoint with an action of "Log Message" » Check "Automatically continue after evaluating actions" » No need to recompile and re-run Advanced Debugging with Xcode 23/53
  18. Breakpoints in User Space » User breakpoints live across all

    your projects » Drag and drop a breakpoint to move it between groups Advanced Debugging with Xcode 24/53
  19. Breakpoint Types » There are also different types of breakpoints

    » Save an All Exceptions breakpoint in user space » In LLDB, po $arg1 to print the exception » Symbolic breakpoints can break inside Apple's frameworks Advanced Debugging with Xcode 25/53
  20. Watchpoints » Pause execution whenever a value is changed »

    On pause, use the call stack to find where the change occurred » Watchpoints can be managed from LLDB Watchpoint 1 hit: old value: true new value: false Advanced Debugging with Xcode 26/53
  21. The Debugger » LLDB lives in the same pane as

    the console log » Look for the (lldb) prompt » Enter help for a full list of commands » Awesome way to test a specific line of code in context » Examine contents of complex data structures » Test and tweak an expression to get the correct syntax » See if an expression is returning what you expect » http://lldb.llvm.org Advanced Debugging with Xcode 28/53
  22. p <expr> » Evaluate an expression on the current thread

    » Displays any returned value with LLDB's default formatting (lldb) p anInt (Int) $R0 = 10 (lldb) p anObject (UIView) $R1 = 0x00007fa331606770 { UIKit.UIResponder = { baseNSObject@0 = { isa = UIView } } } Advanced Debugging with Xcode 29/53
  23. po <expr> » Evaluate an expression on the current thread

    » Displays any returned value with formatting controlled by the type's author (debugDescription) (lldb) po anInt 10 (lldb) po anObject <UIView: 0x7fa331606770; frame = (0 0; 0 0); layer = <CALayer: 0x604000030b00>> Advanced Debugging with Xcode 30/53
  24. fr v <expr> » Show variables for the current stack

    frame » Defaults to all arguments and local variables in scope » Names of argument, local, file static and file global variables can be specified » Children of aggregate variables can be specified such as var->child.x (lldb) fr v anInt (Int) anInt = 10 (lldb) fr v anObject (UIView) anObject = 0x00007fa331606770 { UIKit.UIResponder = { baseNSObject@0 = { isa = UIView } } } Advanced Debugging with Xcode 31/53
  25. expr <expr> » Evaluate an expression on the current thread

    » Displays any returned value with LLDB's default formatting » Can affect the current context and change variable values (lldb) p varInt (Int) $R0 = 12 (lldb) expr varInt = 15 (lldb) p varInt (Int) $R2 = 15 Advanced Debugging with Xcode 32/53
  26. Quick Look » Useful to view data not easily expressible

    as (short) text » Much more reliable from the variables pane than hovering over a symbol in code » You can Quick Look a view and see a preview of it, too! Advanced Debugging with Xcode 33/53
  27. Visual Debugger » View your view hierarchy visually at runtime

    » Bottom toolbar: » View 2D/3D (can rotate in 3D) » Zoom » Show constraints » Show hidden/obscured views » Right-hand slider to modify shown scope Advanced Debugging with Xcode 35/53
  28. Visual Debugger, continued » Filter the outline view using the

    filter bar » Use the "Focus on" option to show a specific view and its hierarchy Advanced Debugging with Xcode 36/53
  29. Recursive Descriptions » po view.value(forKey: "recursiveDescription")! (lldb) po view.value(forKey: "recursiveDescription")!

    <UIView: 0x7fcf646166f0; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer: 0x60c00022e1a0>> | <UILabel: 0x7fcf64615210; frame = (166 323; 42 21); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60c000286770>> Advanced Debugging with Xcode 37/53
  30. Constraint Identifiers let height = label.heightAnchor.constraint(equalToConstant: 20.0) height.identifier = "LabelHeight"

    height.active = true 2018-03-27 11:44:50.061399-0400 Example[44373:13943052] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x604000280aa0 UILabel:0x7f9098405820'Label'.centerY == UIView:0x7f9098405520.centerY (active)>", "<NSLayoutConstraint:0x604000280b40 UILabel:0x7f9098405820'Label'.top == UILayoutGuide:0x6040007ac5c0'UIViewSafeAreaLayoutGuide'.top + 200 (active)>", "<NSLayoutConstraint:0x604000280050 'LabelHeight' UILabel:0x7f9098405820'Label'.height == 20 (active)>", "<NSLayoutConstraint:0x608000286680 'UIView-Encapsulated-Layout-Height' UIView:0x7f9098405520.height == 812 (active)>", "<NSLayoutConstraint:0x604000280b90 'UIViewSafeAreaLayoutGuide-top' V:|-(44)-[UILayoutGuide:0x6040007ac5c0'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7f9098405520 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x604000280050 'LabelHeight' UILabel:0x7f9098405820'Label'.height == 20 (active)> Advanced Debugging with Xcode 38/53
  31. Et tu, Auto Layout? » http://www.wtfautolayout.com » Paste in the

    constraint issue(s) from the console » Returns a human-readable interpretation Advanced Debugging with Xcode 39/53
  32. Feedback Loop Debugger » Disabled by default » When enabled,

    runs layout x times before throwing an error » -UIViewLayoutFeedbackLoopDebuggingThreshold x » Replace UI with NS for macOS » Provides layout detail for each pass // don't actually do this override func viewDidLayoutSubviews() { view.setNeedsLayout() } Advanced Debugging with Xcode 40/53
  33. Allocations » Samples an app's memory usage and shows class-level

    retain/release for objects » Once opened, click the red record icon on the top left to start capturing results » Switch to your app and use it to capture activity » Pause when you no longer need to capture Advanced Debugging with Xcode 44/53
  34. Allocations, continued » To get the most out of the

    outline: » Switch the display to Call Trees » Select "Invert Call Tree" and "Hide System Libraries" Advanced Debugging with Xcode 45/53
  35. Thanks Gracias Danke  Merci Mahalo ת ו ד ה

    Спасибо Asante Tack Diolch  Kiitos ευχαριστώ Grazie Gratias ago  Obrigado א ד א נ ק Advanced Debugging with Xcode 48/53
  36. Resources (1/5) » iOS 7 Tech Talks: Hidden iOS 7

    Development Gems » https://developer.apple.com/videos/play/techtalks-ios-7/8/ » CocoaConf Boston 2014: Effective Xcode: Tips & Techniques by Laura Savino » Notes from Xcode Walkabout: https://gist.github.com/lsavino/860a5bfa47c6834e890b » Appdevcon 2018: Learn to Debug Like a Pro by Paul Hudson (@twostraws) » https://speakerdeck.com/twostraws/learn-to-debug-like-a-pro » NSHipster: Launch Arguments & Environment Variables » http://nshipster.com/launch-arguments-and-environment-variables/ » Use Your Loaf: Using Launch Arguments To Test Localizations » https://useyourloaf.com/blog/using-launch-arguments-to-test-localizations/ Advanced Debugging with Xcode 49/53
  37. Resources (2/5) » Memory Allocator: Enabling the Malloc Debugging Features

    » https://developer.apple.com/library/content/documentation/Performance/Conceptual/ ManagingMemory/Articles/MallocDebug.html » @johnsundell: https://twitter.com/johnsundell/status/973273054699773953 » James Dempsey and the Breakpoints: Backtrace » https://itunes.apple.com/us/album/backtrace/id926558924?uo=4&at=11lpkp&ct=09 » The LLDB Debugger » http://lldb.llvm.org » Why The Failure, Auto Layout » http://www.wtfautolayout.com Advanced Debugging with Xcode 50/53
  38. Resources (3/5) » Documentation > Code Diagnostics > Thread Sanitizer:

    » https://developer.apple.com/documentation/code_diagnostics/thread_sanitizer » Documentation > Code Diagnostics > Address Sanitizer » https://developer.apple.com/documentation/code_diagnostics/address_sanitizer » Documentation > Code Diagnostics > Main Thread Checker » https://developer.apple.com/documentation/code_diagnostics/ main_thread_checker » Documentation > Code Diagnostics > Undefined Behavior Sanitizer » https://developer.apple.com/documentation/code_diagnostics/ undefined_behavior_sanitizer Advanced Debugging with Xcode 51/53
  39. Resources (4/5) » Technical Note TN2239: iOS Debugging Magic »

    https://developer.apple.com/library/content/technotes/tn2239/_index.html » Technical Note TN2124: Mac OS X Debugging Magic » https://developer.apple.com/library/content/technotes/tn2124/_index.html » Debugging with Xcode: Specialized Debugging Workflows » https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/ debugging_with_xcode/chapters/special_debugging_workflows.html » Use Your Loaf: Xcode Visual Memory Debugger » https://useyourloaf.com/blog/xcode-visual-memory-debugger/ » Pete the Developer: iOS — Identifying Memory Leaks using the Xcode Memory Graph Debugger » http://petethedeveloper.com/post/identityingmemoryleaksgraphdebugger/ Advanced Debugging with Xcode 52/53
  40. Resources (5/5) » WWDC 2016: Visual Debugging with Xcode [Session

    410] » https://developer.apple.com/videos/play/wwdc2016/410/ » WWDC 2017: App Startup Time: Past, Present, and Future [Session 413] » https://developer.apple.com/videos/play/wwdc2017/413/ » WWDC 2017: Debugging with Xcode 9 [Session 404] » https://developer.apple.com/videos/play/wwdc2017/404/ » Also recommend similar WWDC sessions for Xcode 6, 7, and 8 » WWDC 2017: Finding Bugs Using Xcode Runtime Tools [Session 406] » https://developer.apple.com/videos/play/wwdc2017/406/ Advanced Debugging with Xcode 53/53