Xcode中断点的威力

这里先推荐两篇Xcode相关的文章:
iOS调试 — 基本技巧
 
本文目录:
1、添加一个特殊的断点
    异常断点(Exception breakpoint)
    符号断点(Symbolic breakpoint)
2、打印到控制台
    使用NSLog打印字符串
    使用NSLog打印对象(po)
    带条件的打印
    在循环里面打印一些东西
3、运行时设置断点
4、调试中播放声音
5、LLDB中有用的一些命令
    打印帮助
    打印调用栈
    打印最基本的内容 (p)
    打印对象(po)
    打印表达式(expr)
    打印线程中的一些东西
 
正文
下面是非常有用的一些Xcode调试技术(使用断点和LLDB调试器)
 
1、添加一个特殊的断点
异常断点(Exception breakpoint)
如果添加了异常断点,当程序每次发生了异常,都会被中断。一般用来捕获未知异常。如下示例:
 
 
 
  1. *** Terminating app due to uncaught exception ’NSRangeException’, reason: 
  2. ’-[__NSCFArray objectAtIndex:]: index (10) beyond bounds (3) 
 
 
符号断点(Symbolic breakpoint)
符号断点可以中断某个函数的调用。
 
 
  1. - [UIViewController viewDidLoad] 
  2. - [__NSCFArray objectAtIndex:] 
 
2、打印到控制台
使用NSLog打印字符串
使用断点来替换NSLog代码(或者在运行时添加一个NSLog)——与代码写NSLog的效果相同。
使用NSLog打印对象(po)
 
 
  1. NSLog(@"obj: %@", obj); 
带条件的打印
例如:当aNumber大于10才打印出“str”的内容。
 
 
  1. expr (void)NSLog(@"Ok, print a log: %@", str)" 
在循环里面打印一些东西
例如,在循环中希望i大于5才开始打印。
 
 
  1. for ( int i=0; i<10; i++ ) 
  2. [self self]; // something 
 
使用“ignore”值,并利用下面的代码进行打印:
 
 
  1. expr (void)NSLog(@"Ok, print a log: %@", str) 
 
3、运行时设置断点
在运行的时候,根据条件设置断点有时候非常有用。
 
 
  1. breakpoint set -f APViewController.m -l 33 
4、调试中播放声音
 
 
5、LLDB中有用的一些命令
当Xcode停留在某个断点时,我们可以通过控制台(console)与 lldb进行交互。
 
打印帮助
 
 
  1. (lldb) help 
 
打印调用栈(bt)
 
 
  1. (lldb) bt 
  2. * 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 
  3. frame #0: 0x00003146 Debug`-[APViewController callMe:andANumber:](self=0x07187e50, _cmd=0x000038b9, str=0x0715aa40, aNum=38) + 230 at APViewController.m:33 
  4. frame #1: 0x0000304a Debug`-[APViewController viewDidLoad](self=0x07187e50, _cmd=0x005c5a77) + 122 at APViewController.m:16 
  5. frame #2: 0x000f41c7 UIKit`-[UIViewController loadViewIfRequired] + 536 
  6. frame #3: 0x000f4232 UIKit`-[UIViewController view] + 33 
  7. frame #4: 0x000433d5 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 66 
  8. frame #5: 0x0004376f UIKit`-[UIWindow _setHidden:forced:] + 368 
  9. frame #6: 0x00043905 UIKit`-[UIWindow _orderFrontWithoutMakingKey] + 49 
  10. frame #7: 0x0004c917 UIKit`-[UIWindow makeKeyAndVisible] + 65 
  11. frame #8: 0x00002e1b Debug`-[APAppDelegate application:didFinishLaunchingWithOptions:](self=0x07560750, _cmd=0x005a9c21, application=0x0716a640, launchOptions=0x00000000) + 571 at APAppDelegate.m:28 
  12. frame #9: 0x00010157 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266 
  13. frame #10: 0x00010747 UIKit`-[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248 
  14. frame #11: 0x0001194b UIKit`-[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805 
  15. frame #12: 0x00022cb5 UIKit`-[UIApplication handleEvent:withNewEvent:] + 1022 
  16. frame #13: 0x00023beb UIKit`-[UIApplication sendEvent:] + 85 
  17. frame #14: 0x00015698 UIKit`_UIApplicationHandleEvent + 9874 
  18. frame #15: 0x01becdf9 GraphicsServices`_PurpleEventCallback + 339 
  19. frame #16: 0x01becad0 GraphicsServices`PurpleEventCallback + 46 
  20. frame #17: 0x01c06bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 
  21. frame #18: 0x01c06962 CoreFoundation`__CFRunLoopDoSource1 + 146 
  22. frame #19: 0x01c37bb6 CoreFoundation`__CFRunLoopRun + 2118 
  23. frame #20: 0x01c36f44 CoreFoundation`CFRunLoopRunSpecific + 276 
  24. frame #21: 0x01c36e1b CoreFoundation`CFRunLoopRunInMode + 123 
  25. frame #22: 0x0001117a UIKit`-[UIApplication _run] + 774 
  26. frame #23: 0x00012ffc UIKit`UIApplicationMain + 1211 
  27. frame #24: 0x00002b22 Debug`main(argc=1, argv=0xbffff3a4) + 130 at main.m:16 
  28. frame #25: 0x00002a55 Debug`start + 53 
  29. (lldb) 
 
打印最基本的内容 (p)
 
 
  1. (lldb) print anInt 
 
打印对象(po)
 
 
  1. (lldb) po anObj 
  2. (lldb) po 0x0715aa40 
 
打印表达式(expr)
 
 
  1. (lldb) expr 5+2 
  2. (lldb) expr aString = @"aNewValue" 
 
打印线程中的一些东西
 
 
  1. (lldb) help frame 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值