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

Advanced iOS Debugging

Advanced iOS Debugging

The debug process constitutes an important part in an app's development cycle. Knowing the (right) tools and techniques means you can optimizes time and therefore costs. In this session we will see a number of techniques to optimize debugging of iOS applications exploiting the power of Xcode, LLDB and other support tools.

Massimo Oliviero

March 23, 2013
Tweet

More Decks by Massimo Oliviero

Other Decks in Programming

Transcript

  1. [email protected]
    Massimo Oliviero
    Advanced iOS Debugging
    @maxoly #code12

    View Slide

  2. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Massimo Oliviero
    Freelance Software Developer
    Co-founder of #pragma mark http://pragmamark.org
    Online
    web http://www.massimooliviero.net
    email [email protected]
    twitter @maxoly
    slide http://www.slideshare.net/MassimoOliviero

    View Slide

  3. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Agenda
    • Code, some tips for standard functions
    • Xcode, the best tools for debugging
    • LLDB, your great friend
    • Tools, network debugging how to and more
    • Remote, over the air debugging

    View Slide

  4. Advanced iOS Debugging
    Code

    View Slide

  5. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    It all began...
    NSLog(@"Error :( %@", error);
    NSLog(@"User: %@", user);
    NSLog(@"Array: %@", array);
    NSLog(@"URL: %@", url);
    NSLog(@"Count: %i", count);
    NSLog(@"User %@",
    user);
    NSLog(@"Array %@",
    array);
    NSLog(@"Error :( %@", error);
    NSLog(@"Error :( %@", error
    NSLog(@"URL: %@", url);
    NSLog(@"Array: %@", array);
    NSLog(@"User:
    NSLog(@"User: %@", user);
    NSLog(@"User: %@", user);
    NSLog(@"User: %@", user);
    NSLog(@"User: %@", user);
    NSLog(@"User: %@", user);
    NSLog(@"User: %@", user);

    View Slide

  6. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    NSLog
    • It prints debugs output only to the console
    • It’s a simple native Foundation function
    • It’s not too bad, but It’s an ancient technique
    • It slows things down considerably (if not handled)

    View Slide

  7. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    NSLog optimization
    • Use convenient macro
    • Use string conversions
    • Try alternative frameworks

    View Slide

  8. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    NSLog macro
    #if defined DEBUG
    #define MYNSLog(s, ...) NSLog((@"%s [Line %d] " s), __PRETTY_FUNCTION__,
    __LINE__, ##__VA_ARGS__)
    #else
    #define MYNSLog(s, ...)
    #endif
    -[TestViewController viewDidLoad] [Line 33] message
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    MYNSLog(@"message");
    }
    MyGreatApp-prefix.pch
    TestViewController.m
    Console

    View Slide

  9. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    NSLog macro
    • Enables DEBUG mode output only
    • Outputs function name and line number
    • Place macro into .pch file or in a header file
    • You can use other macros like __ FILE__ (for example)

    View Slide

  10. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    String conversion functions
    CGPoint point = CGPointMake(10.5f, 12.3f);
    NSLog(@"point: %@", NSStringFromCGPoint(point));
    AdvanceDebuggingExample[3050:c07] point: {10.5, 12.3}
    AdvanceDebuggingExample.m
    Console

    View Slide

  11. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    String conversion functions
    Returns a string formatted to contain the data from a
    structure:
    • NSStringFromCGAffineTransform
    • NSStringFromCGPoint
    • NSStringFromCGRect
    • NSStringFromCGSize
    • NSStringFromUIEdgeInsets
    • NSStringFromUIOffset

    View Slide

  12. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Try alternative frameworks
    • CocoaLumberjack
    https://github.com/robbiehanson/CocoaLumberjack
    • NSLogger
    https://github.com/fpillet/NSLogger
    • DMLogger
    https://github.com/malcommac/DMLogger

    View Slide

  13. Advanced iOS Debugging
    Demo

    View Slide

  14. Advanced iOS Debugging
    Xcode

    View Slide

  15. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Xcode
    • Configure your Behaviors
    • Print more information with Arguments
    • Go beyond logging with Breakpoints

    View Slide

  16. Advanced iOS Debugging
    Behaviors

    View Slide

  17. Xcode default Behaviors
    Debugger bar

    View Slide

  18. Xcode default Behaviors
    Variables View Console
    Debugger
    Navigator

    View Slide

  19. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Xcode Behaviors
    • Match Xcode to your Workflow
    • Use Behaviors to control Xcode
    • Behaviors lets you specify what should happen when a
    variety of events occur (like Run)

    View Slide

  20. Behaviors
    When
    pauses
    Show Debug Navigator
    Show debugger views

    View Slide

  21. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Change default Behavior
    For example, when Running pauses:
    • show the Breakpoint Navigator instead of Debug Navigator
    • show only Variable Views
    • open another tab only with Console view

    View Slide

  22. Advanced iOS Debugging
    Demo

    View Slide

  23. Advanced iOS Debugging
    Arguments

    View Slide

  24. Arguments
    Product > Scheme > Edit Scheme > Arguments

    View Slide

  25. Core Data Logging
    -com.apple.CoreData.SQLDebug 1

    View Slide

  26. Core Data and iCloud
    -com.apple.coredata.ubiquity.logLevel 3

    View Slide

  27. Advanced iOS Debugging
    Breakpoints

    View Slide

  28. Creating and editing breakpoint

    View Slide

  29. Breakpoint Navigator

    View Slide

  30. Exception Breakpoint

    View Slide

  31. Symbolic Breakpoint

    View Slide

  32. Breakpoint Action

    View Slide

  33. Breakpoint action
    Condition to evaluate
    The num of time to ignore
    breakpoint before stoping
    Log Message Action
    Debugger Command Action
    Play sound
    Continue program execution

    View Slide

  34. Debugger Command Action
    po variable
    expr (void)NSLog(@”variable: %@”, variable)
    breakpoint set -f ADEMasterViewController.m -l 83

    View Slide

  35. Sharing Breakpoint
    Share breakpoints with the team, so that all can benefit from it
    This action will create a new directory to be committed in the repository
    AdvanceDebuggingExample.xcodeproj/xcshareddata/

    View Slide

  36. Advanced iOS Debugging
    Demo

    View Slide

  37. Advanced iOS Debugging
    LLDB

    View Slide

  38. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Why LLDB
    • Consistent Command Syntax
    • Scriptability with Python
    • Performance
    • ...

    View Slide

  39. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    LLDB Commands
    print object po [object]
    print variable print [variable]
    assign value expr [variable] = [value]
    (lldb)

    View Slide

  40. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    LLDB Commands
    set breakpoint breakpoint set -f [file] -l [line]
    load script command script import ~/test.py
    evaluate expression expr
    [-options [option-value]] [argument [argument...]]

    View Slide

  41. Custom object, the problem
    No summary for custom object
    @interface ADEEvent : NSObject
    @property (nonatomic, strong) NSString *title;
    @property (nonatomic, strong) NSDate *when;
    @end
    ADEEvent *event = [[ADEEvent alloc] init];
    event.title = @"Codemotion Conference";
    event.when = [NSDate date];
    Init object
    Custom object

    View Slide

  42. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Custom summary
    • Create a Python script that will instruct LLDB on how to
    display a summary of your custom object
    • Load your Python script via command line or ~/.lldbinit
    file

    View Slide

  43. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Custom summary
    import lldb
    def ade_summary(valobj, internal_dict):
    ! titleAsString = valobj.GetChildMemberWithName('_title').GetSummary()
    ! whenAsString = valobj.GetChildMemberWithName('_when').GetSummary()
    ! return 'Title: ' + titleAsString + ' - when: ' + whenAsString
    def __lldb_init_module(debugger, dict):
    debugger.HandleCommand('type summary add ADEEvent -F CustomSummaries1.ade_summary')
    • Create “ADEEvent_summary.py” in Xcode project
    • Then load script:
    (lldb) command script import /path/to/ADEEvent_summary.py

    View Slide

  44. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Console tips
    (lldb) po [self.view recursiveDescription]
    $7 = 0x082a2c60 gestureRecognizers = ; layer = ; contentOffset: {0, 0}>
    | layer = >
    | | 0x8282c30>; layer = >
    | | | = YES; userInteractionEnabled = NO; layer = >
    | | (lldb) po [[UIWindow keyWindow] recursiveDescription]
    $5 = 0x08273bc0 >
    | 0xd071460>>
    | | +H; layer = >
    | | | >
    ...

    View Slide

  45. Advanced iOS Debugging
    Demo

    View Slide

  46. Advanced iOS Debugging
    Tools

    View Slide

  47. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Tools
    • Network Link Conditioner
    • Charles
    • PonyDebugger
    • Deploymate

    View Slide

  48. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Network Link Conditioner
    • It’s a utility that enables you to simulate network
    conditions
    • To install just select Xcode > Open Developer Tool >
    More Developer Tools. You’ll be taken to Apple’s developer
    downloads site
    • Download “Hardware IO Tools for Xcode”

    View Slide

  49. Network Link Conditioner

    View Slide

  50. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Charles
    • It’s a web debugging proxy
    • You can inspect, modify and record requests & responses
    • SSL Proxing (http://www.charlesproxy.com/
    documentation/faqs/ssl-connections-from-within-iphone-
    applications/)
    http://www.charlesproxy.com/

    View Slide

  51. Charles

    View Slide

  52. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    PonyDebugger
    • Network Traffic Debugger
    • Core Data Browser
    • It is a client library and gateway server combination that
    uses Chrome Developer Tools on your browser to debug
    your application's network traffic and managed object
    contexts.
    https://github.com/square/PonyDebugger

    View Slide

  53. PonyDebugger

    View Slide

  54. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Deploymate
    • If using an API introduced later than your target OS but
    your app is targeting an older OS version, Xcode doesn't
    warn you about it
    • It helps identify unavailable, deprecated and obsolete API

    View Slide

  55. Deploymate

    View Slide

  56. Advanced iOS Debugging
    Demo

    View Slide

  57. Advanced iOS Debugging
    Remote

    View Slide

  58. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Remote debugging
    • Apple Crash Reports
    • PLCrashReporter
    • TestFlight

    View Slide

  59. Advanced iOS Debugging
    Apple Crash Reports

    View Slide

  60. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Apple Crash Reports
    • For app published on App Store, you can acquire crash log
    from iTunes Connect and import it into Organizer for
    symbolication
    • To symbolicate a crash log, Xcode needs to have access to
    the matching application binary that was uploaded to the
    App Store, and the .dSYM file that was generated when
    that binary was built. This must be an exact match
    https://itunesconnect.apple.com

    View Slide

  61. Apple Crash Reports

    View Slide

  62. Apple Crash Reports

    View Slide

  63. Xcode Symbolication

    View Slide

  64. Advanced iOS Debugging
    PLCrashReporter

    View Slide

  65. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    PLCrashReporter
    • In-process CrashReporter framework for the iPhone and
    Mac OS X
    • Handles both uncaught Objective-C exceptions and fatal
    signals
    • Backtraces for all active threads are provided
    https://code.google.com/p/plcrashreporter/

    View Slide

  66. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    - (void) handleCrashReport {
    PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
    NSData *crashData;
    NSError *error;
    // Try loading the crash report
    crashData = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
    if (crashData == nil) {
    NSLog(@"Could not load crash report: %@", error);
    goto finish;
    }
    PLCrashReport *report = [[[PLCrashReport alloc] initWithData: crashData error: &error] autorelease];
    if (report == nil) {
    NSLog(@"Could not parse crash report");
    goto finish;
    }
    ....
    return;
    }
    // from UIApplicationDelegate protocol
    - (void) applicationDidFinishLaunching: (UIApplication *) application {
    PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
    NSError *error;
    // Check if we previously crashed
    if ([crashReporter hasPendingCrashReport])
    [self handleCrashReport];
    // Enable the Crash Reporter
    if (![crashReporter enableCrashReporterAndReturnError: &error])
    NSLog(@"Warning: Could not enable crash reporter: %@", error);
    }

    View Slide

  67. Advanced iOS Debugging
    TestFlight

    View Slide

  68. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    TestFlight
    • Invite your testers, drop in the SDK and start uploading
    your builds.
    • Upload your builds and TestFlight takes care of the rest.
    Painless over-the-air distribution to your testers and
    distribution lists.
    • Complete tracking of your build, from distribution to
    sessions, checkpoints and crashes.
    https://testflightapp.com/

    View Slide

  69. TestFlight Apps Managment

    View Slide

  70. TestFlight Crashes

    View Slide

  71. Advanced iOS Debugging
    Final Thoughts

    View Slide

  72. Massimo Oliviero - [email protected] - www.massimooliviero.net - @maxoly
    Final Thoughts
    • Don’t use NSLog anymore ;)
    • Create your Xcode Behaviors
    • Breakpoints are your friends
    • LLDB is a promising youngster
    • Tools can save your life
    • The QA phase is essential

    View Slide

  73. Advanced iOS Debugging
    Thank you!
    Massimo Oliviero
    [email protected]
    http://www.massimooliviero.net
    follow me on twitter @maxoly
    http://www.slideshare.net/MassimoOliviero
    https://speakerdeck.com/massimooliviero

    View Slide