Xcode – power of breakpoints

A collection of useful Xcode debugging technique, using Breakpoints and LLDBdebugger.

Adding a special Breakpoint
Exception breakpoint

used to break each time a new exception is raised.
Common use, to catch uncaught exception:

*** Terminating app due to uncaught exception ’NSRangeException’, reason:
’-[__NSCFArray objectAtIndex:]: index (10) beyond bounds (3)

xcode-debug-01

Symbolic breakpoint

Used to break on every call to a defined method:

-[UIViewController viewDidLoad]
-[__NSCFArray objectAtIndex:]

xcode-debug-02

Logging to console
Log string with NSLog

Replace your NSLog line (or add an NSLog in runtime) with a breakpoint.
Same effect, 0 line of code.
xcode-debug-04

Log objects with NSLog (po)
NSLog(@"obj: %@", obj);

xcode-debug-05

Conditional log

Ex: print “str” content only if aNumber is greater than 10.

expr (void)NSLog(@"Ok, print a log: %@", str)"

xcode-debug-07

Log something in a loop

Want to start printing value of “i” after 5 loop?

for ( int i=0; i<10; i++ ) {
[self self]; // something
}

Use “ignore” value and print using

expr (void)NSLog(@"Ok, print a log: %@", str)

xcode-debug-08

Set breakpoints at runtime

Very useful if you want to set a breakpoint on a determinate condition. In runtime.

breakpoint set -f APViewController.m -l 33

xcode-debug-09

Debug with sound

Schermata 06-2456470 alle 15.43.13

Useful LLDB commands

When Xcode stop on a breakpoint, you can interact with lldb from the console.

Help
(lldb) help
Print backtrace (bt)
(lldb) bt
* thread #1: tid = 0x1c03, 0x00003146 Debug`-[APViewController callMe:andANumber:](self=0x07187e50, _cmd=0x000038b9, str=0x0715aa40, aNum=38) + 230 at APViewController.m:33, stop reason = breakpoint 3.1
frame #0: 0x00003146 Debug`-[APViewController callMe:andANumber:](self=0x07187e50, _cmd=0x000038b9, str=0x0715aa40, aNum=38) + 230 at APViewController.m:33
frame #1: 0x0000304a Debug`-[APViewController viewDidLoad](self=0x07187e50, _cmd=0x005c5a77) + 122 at APViewController.m:16
frame #2: 0x000f41c7 UIKit`-[UIViewController loadViewIfRequired] + 536
frame #3: 0x000f4232 UIKit`-[UIViewController view] + 33
frame #4: 0x000433d5 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 66
frame #5: 0x0004376f UIKit`-[UIWindow _setHidden:forced:] + 368
frame #6: 0x00043905 UIKit`-[UIWindow _orderFrontWithoutMakingKey] + 49
frame #7: 0x0004c917 UIKit`-[UIWindow makeKeyAndVisible] + 65
frame #8: 0x00002e1b Debug`-[APAppDelegate application:didFinishLaunchingWithOptions:](self=0x07560750, _cmd=0x005a9c21, application=0x0716a640, launchOptions=0x00000000) + 571 at APAppDelegate.m:28
frame #9: 0x00010157 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266
frame #10: 0x00010747 UIKit`-[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248
frame #11: 0x0001194b UIKit`-[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805
frame #12: 0x00022cb5 UIKit`-[UIApplication handleEvent:withNewEvent:] + 1022
frame #13: 0x00023beb UIKit`-[UIApplication sendEvent:] + 85
frame #14: 0x00015698 UIKit`_UIApplicationHandleEvent + 9874
frame #15: 0x01becdf9 GraphicsServices`_PurpleEventCallback + 339
frame #16: 0x01becad0 GraphicsServices`PurpleEventCallback + 46
frame #17: 0x01c06bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #18: 0x01c06962 CoreFoundation`__CFRunLoopDoSource1 + 146
frame #19: 0x01c37bb6 CoreFoundation`__CFRunLoopRun + 2118
frame #20: 0x01c36f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #21: 0x01c36e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #22: 0x0001117a UIKit`-[UIApplication _run] + 774
frame #23: 0x00012ffc UIKit`UIApplicationMain + 1211
frame #24: 0x00002b22 Debug`main(argc=1, argv=0xbffff3a4) + 130 at main.m:16
frame #25: 0x00002a55 Debug`start + 53
(lldb)
Print primitives (p)
(lldb) print anInt
Print objects (po)
(lldb) po anObj
(lldb) po 0x0715aa40
Print expressions (expr)
(lldb) expr 5+2
(lldb) expr aString = @"aNewValue"
Print something in thread
(lldb) help frame

enjoy debug.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.