转自:http://www.albertopasca.it/whiletrue/2013/06/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:
1
2 |
*** Terminating app due to uncaught exception ’NSRangeException’, reason:
’-[__NSCFArray objectAtIndex:]: index (10) beyond bounds (3) |
Symbolic breakpoint
Used to break on every call to a defined method:
1
2 |
- [UIViewController viewDidLoad]
- [__NSCFArray objectAtIndex:] |
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.
Log objects with NSLog (po)
1
|
NSLog
(
@
"obj: %@", obj
);
|
Conditional log
Ex: print “str” content only if aNumber is greater than 10.
1
|
expr
(
void
)NSLog
(
@
"Ok, print a log: %@", str
)
"
|
Log something in a loop
Want to start printing value of “i” after 5 loop?
1
2 3 4 |
for
(
int i
=
0; i
<
10; i
++
)
{ [self self ]; // something } |
Use “ignore” value and print using
1
|
expr
(
void
)NSLog
(
@
"Ok, print a log: %@", str
)
|
Set breakpoints in runtime
Very useful if you want to set a breakpoint on a determinate condition. In runtime.
1
|
breakpoint set
-f APViewController.m
-l
33
|
Debug with sound :)
Useful LLDB commands
When Xcode stop on a breakpoint, you can interact with lldb from the console.
Help
1
|
(lldb
) help
|
Print backtrace (bt)
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
(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)
1
|
(lldb
) print anInt
|
Print objects (po)
1
2 |
(lldb
) po anObj
(lldb ) po 0x0715aa40 |
Print expressions (expr)
1
2 |
(lldb
) expr
5
+
2
(lldb ) expr aString = @ "aNewValue" |
Print something in thread
1
|
(lldb
) help frame
|