Advanced iOS Debbuging (Reloaded)

2,877 views
2,756 views

Published on

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.

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,877
On SlideShare
0
From Embeds
0
Number of Embeds
247
Actions
Shares
0
Downloads
105
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Advanced iOS Debbuging (Reloaded)

  1. 1. Advanced iOS DebuggingMassimo Oliviero
  2. 2. Massimo OlivieroFreelance Software Developerweb http://www.massimooliviero.netemail massimo.oliviero@gmail.comslide http://www.slideshare.net/MassimoOlivierotwitter @maxoly
  3. 3. 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‣ Resources, links and videos
  4. 4. Source codeKimeraA simple iOS application for educational purposehttps://github.com/maxoly/Kimera
  5. 5. Code
  6. 6. 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 :( %@", errorNSLog(@"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);
  7. 7. 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)
  8. 8. NSLog optimization‣ Use convenient macro‣ Use string conversion functions‣ Try alternative frameworks
  9. 9. #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.pchTestViewController.mConsoleNSLog macro
  10. 10. 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)
  11. 11. CGPoint point = CGPointMake(10.5f, 12.3f);NSLog(@"point: %@", NSStringFromCGPoint(point));AdvanceDebuggingExample[3050:c07] point: {10.5, 12.3}AdvanceDebuggingExample.mConsoleString conversion functions
  12. 12. String conversion functions‣ NSStringFromCGAffineTransform‣ NSStringFromCGPoint‣ NSStringFromCGRect‣ NSStringFromCGSize‣ NSStringFromUIEdgeInsets‣ NSStringFromUIOffset
  13. 13. Try alternative framworks‣ CocoaLumberjackhttps://github.com/robbiehanson/CocoaLumberjack‣ NSLoggerhttps://github.com/fpillet/NSLogger‣ DMLoggerhttps://github.com/malcommac/DMLogger
  14. 14. Demo
  15. 15. Xcode
  16. 16. Xcode‣ Configure your Behaviors‣ Print more information with Arguments‣ Push the limit with Build Settings‣ Go beyond logging with Breakpoints
  17. 17. Behaviors
  18. 18. Xcode defaultDebugger bar
  19. 19. Xcode default BehaviorsVariablesView ConsoleDebuggerNavigator
  20. 20. Xcode Behaviors‣ Match Xcode to your Workflow‣ Use Behaviors to control Xcode‣ Behaviors lets you specify what should happen when avariety of events occur (like Run)
  21. 21. BehaviorsWhenpausesShow Debug NavigatorShow debugger views
  22. 22. Change default BehaviorFor example, when Running pauses:‣ show the Breakpoint Navigator instead of DebugNavigator‣ show only Variable Views‣ open another tab only with Console view
  23. 23. Demo
  24. 24. Arguments
  25. 25. ArgumentsProduct > Scheme > Edit Scheme > Arguments
  26. 26. Core Data Logging-com.apple.CoreData.SQLDebug 1
  27. 27. Core Data and iCloud logging-com.apple.coredata.ubiquity.logLevel 3
  28. 28. Build Settings
  29. 29. Enable Static Analyzer
  30. 30. Treat Warning as Error
  31. 31. Warnings
  32. 32. Warnings - Objective-C
  33. 33. Breakpoints
  34. 34. Creating and editing breakpoint
  35. 35. Breakpoint Navigator
  36. 36. Exception Breakpoint
  37. 37. Symbolic Breakpoint
  38. 38. Breakpoint Action
  39. 39. Breakpoint ActionCondition to evaluateThe num of time to ignorebreakpoint before stopingLog Message ActionDebugger Command ActionPlay soundContinue program execution
  40. 40. Debugger Command Actionpo variableexpr (void)NSLog(@”variable: %@”, variable)breakpoint set -f ADEMasterViewController.m -l 83
  41. 41. Sharing BreakpointShare breakpoints with the team, so that all can benefit from itThis action will create a new directory to be committed in the repositoryAdvanceDebuggingExample.xcodeproj/xcshareddata/
  42. 42. Demo
  43. 43. LLDB
  44. 44. Why LLDB‣ Consistent Command Syntax‣ Scriptability with Python‣ Performance‣ ...
  45. 45. LLDB Commandsprint object po [object]print variable print [variable]assign value expr [variable] = [value](lldb) <command>
  46. 46. LLDB Commandsset breakpoint breakpoint set -f [file] -l [line]load script command script import ~/test.pyevaluate expression expr <expression><noun> <verb> [-options [option-value]] [argument [argument...]]
  47. 47. Custom Object, the problemNo summary for custom object@interface KMRMArgument : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSDate *detail;@endKMRMArgument *argument = [[KMRMArgument alloc] init];argument.title = @"Debugging";argument.when = @"Some advance techniques";Init objectCustom object
  48. 48. Custom Summary, the solution‣ Create a Python script that will instruct LLDB on how todisplay a summary of your custom object‣ Load your Python script via command line or ~/.lldbinitfile
  49. 49. Custom Summary‣ Create“KMRMArgument_summary.py”in Xcode project‣ Then load script:(lldb) command script import /path/to/KMRMArgument_summary.pyimport lldbdef arg_summary(valobj, internal_dict):! nameAsString = valobj.GetChildMemberWithName(_name).GetSummary()! detailAsString = valobj.GetChildMemberWithName(_detail).GetSummary()! return Title: + nameAsString + - detail: + detailAsStringdef __lldb_init_module(debugger, dict):debugger.HandleCommand(type summary add KRMRArgument -F KRMRArgument_summary.arg_summary)
  50. 50. Console tips(lldb) po [self.view recursiveDescription]$7 = 0x082a2c60 <UITableView: 0x8971000; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = W+H;gestureRecognizers = <NSArray: 0x8184be0>; layer = <CALayer: 0x8184570>; contentOffset: {0, 0}>| <UITableViewCell: 0x8282900; frame = (0 176; 320 44); text = 2013-03-12 21:22:34 +0000; autoresize = W;layer = <CALayer: 0x8282a30>>| | <UITableViewCellContentView: 0x8282a60; frame = (0 0; 300 43); gestureRecognizers = <NSArray:0x8282c30>; layer = <CALayer: 0x8282ac0>>| | | <UILabel: 0x8282e50; frame = (10 0; 280 43); text = 2013-03-12 21:22:34 +0000; clipsToBounds= YES; userInteractionEnabled = NO; layer = <CALayer: 0x8282ee0>>| | <UIButton: 0x8282c80; frame = (290 0; 30 43); opaque = NO; userInteractionEnabled = NO; layer = ...(lldb) po [[UIWindow keyWindow] recursiveDescription]$5 = 0x08273bc0 <UIWindow: 0x8181010; frame = (0 0; 320 568); layer = <UIWindowLayer: 0x81810e0>>| <UILayoutContainerView: 0xd06eed0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer:0xd071460>>| | <UINavigationTransitionView: 0xd09e850; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xd09e920>>| | | <UIViewControllerWrapperView: 0x846e160; frame = (0 64; 320 504); autoresize = W+H; layer =<CALayer: 0x846e210>>...
  51. 51. Demo
  52. 52. Tools
  53. 53. Tools‣ Network Link Conditioner‣ Charles‣ PonyDebugger‣ Deploymate‣ Spark Inspector
  54. 54. Network LinkConditioner
  55. 55. Network Link Conditioner‣ It’s a utility that enables you to simulate networkconditions‣ To install just select Xcode > Open Developer Tool >More Developer Tools. You’ll be taken to Apple’sdeveloper downloads site‣ Download“Hardware IO Tools for Xcode”
  56. 56. Network Link Conditioner
  57. 57. Charles
  58. 58. Charles Proxy‣ It’s a web debugging proxy‣ You can inspect, modify and record requests &responses‣ SSL Proxinghttp://www.charlesproxy.com/documentation/faqs/ssl-connections-from-within-iphone-applications/‣ http://www.charlesproxy.com‣ Price start from US$50
  59. 59. Charles Proxy
  60. 60. PonyDebugger
  61. 61. PonyDebugger‣ Network Traffic Debugger‣ Core Data Browser‣ It is a client library and gateway server combinationthat uses Chrome Developer Tools on your browser todebug your applications network traffic and managedobject contexts.‣ https://github.com/square/PonyDebugger
  62. 62. PonyDebugger
  63. 63. Deploymate
  64. 64. Deploymate‣ If using an API introduced later than your target OS butyour app is targeting an older OS version, Xcodedoesnt warn you about it‣ It helps identify unavailable, deprecated and obsoleteAPI‣ http://www.deploymateapp.com/‣ Price US$19.99
  65. 65. Deploymate
  66. 66. Spark Inspector
  67. 67. Spark Inspector‣ Runtime Debugger for iOS‣ Monitor & Experiment in Real-time‣ See Notifications in Real-time‣ http://sparkinspector.com/‣ Price € 31,79
  68. 68. Spark Inspector
  69. 69. Demo
  70. 70. Remote
  71. 71. Remote debugging‣ Apple Crash Reports‣ PLCrashReporter‣ TestFlight
  72. 72. Apple Crash Reports
  73. 73. Apple Crash Reports‣ For app published on App Store, you can acquire crashlog from iTunes Connect and import it into Organizerfor symbolication‣ To symbolicate a crash log, Xcode needs to have accessto the matching application binary that was uploadedto the App Store, and the .dSYM file that wasgenerated when that binary was built. This must be anexact match‣ https://itunesconnect.apple.com
  74. 74. Apple Crash Reports
  75. 75. Apple Crash Reports
  76. 76. Xcode Symbolication
  77. 77. PLCrashReporter
  78. 78. PLCrashReporter‣ In-process CrashReporter framework for the iPhoneand Mac OS X‣ Handles both uncaught Objective-C exceptions andfatal signals‣ Backtraces for all active threads are provided‣ https://code.google.com/p/plcrashreporter/
  79. 79. PLCrashReporter- (void) handleCrashReport {PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];NSData *crashData;NSError *error;// Try loading the crash reportcrashData = [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 crashedif ([crashReporter hasPendingCrashReport])[self handleCrashReport];// Enable the Crash Reporterif (![crashReporter enableCrashReporterAndReturnError: &error])NSLog(@"Warning: Could not enable crash reporter: %@", error);}
  80. 80. TestFlight
  81. 81. TestFlight‣ Invite your testers, drop in the SDK and start uploadingyour builds.‣ Upload your builds and TestFlight takes care of therest. Painless over-the-air distribution to your testersand distribution lists.‣ Complete tracking of your build, from distribution tosessions, checkpoints and crashes.‣ https://testflightapp.com
  82. 82. TestFlight Apps Management
  83. 83. TestFlight Crashes
  84. 84. Resources
  85. 85. Resources‣ Links‣ Videos
  86. 86. Links
  87. 87. Links‣ Overview of iOS Crash Reporting Toolshttp://www.raywenderlich.com/33669/overview-of-ios-crash-reporting-tools-part-1http://www.raywenderlich.com/33669/overview-of-ios-crash-reporting-tools-part-2‣ How to use Instruments in Xcodehttp://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode‣ Demystifying iOS Application Crash Loghttp://www.raywenderlich.com/23704/demystifying-ios-application-crash-logs
  88. 88. Links‣ Intermediate Debugging with Xcode 4.5http://www.raywenderlich.com/28289/debugging-ios-apps-in-xcode-4-5‣ Xcode LLDB Tutorialhttp://www.cimgf.com/2012/12/13/xcode-lldb-tutorial/‣ iOS Simulator Tips & Trickshttp://mobile.tutsplus.com/tutorials/iphone/ios-simulator/
  89. 89. Links‣ Compiler Warnings for Objective-C Developershttp://oleb.net/blog/2013/04/compiler-warnings-for-objective-c-developers/‣ Defensive Programming in Cocoahttp://www.mikeash.com/pyblog/friday-qa-2010-08-27-defensive-programming-in-cocoa.html‣ Testing and Debugging in iOS Simulatorhttps://developer.apple.com/library/ios/#documentation/IDEs/Conceptual/iOS_Simulator_Guide/TestingontheiOSSimulator/TestingontheiOSSimulator.html
  90. 90. Links‣ iOS Debugging (slide by Dave Kozol)http://arbormoon.com/sites/default/files/ASIDebuggingCocoaConfChicago.pdf‣ Strong, Better, Faster with Instruments and Debugging(slide by Kyle Richter)http://dragonforged.com/CocoaConf.pdf‣ LLDB Script Exampleshttp://llvm.org/svn/llvm-project/lldb/trunk/examples/
  91. 91. Videos
  92. 92. Videos‣ WWDC 2012 - 415 - Debugging with LLDB‣ WWDC 2011 - 321 - Migrating from GDB to LLDB‣ WWDC 2010 - 316 - Debugging with Xcode 4 and LLDB‣ NSScreenCast - 002 - Memory Problems
  93. 93. Final Thoughts
  94. 94. 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
  95. 95. Thank youMassimo Olivieromassimo.oliviero@gmail.comhttp://www.massimooliviero.netfollow me on twitter @maxolyhttp://www.slideshare.net/MassimoOlivierohttps://speakerdeck.com/massimooliviero

×