修复 bug 占用我们日常开发的大部分时间,熟练的使用调试工具可以给我们节约大部分的时间。

LLDB 的常用命令

expression

expresion 是一个非常常用的命令,我们可以通过这个命令来执行一些表达式,这样我们就不需要重写运行工程了,例如:

  (lldb) expression -- self.view.backgroundColor = [UIColor greenColor]
(lldb) expression -- (void)[CATransaction flush] // 用于刷新页面,

我们也可以使用 expression 来输出我们关注的信息

(lldb) expression -O -- self.view //这时候 就会输出对象 UIView,注意 -O 代表输出的是一个对象 ,这里有另外一种简写方式 po.
<UIView: 0x7d2974a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7d294813>>

有时候我们自定义的类并没有重写 description 方法,如果我们直接输出这个对象的话可能只会显示这个类名和地址,例如

(lldb) expression -O -- self
<JChatAboutMeViewController: 0x7d2974a0>

这地址并不是我们想要的,我们想要的是这个对象内部信息,这里我推荐一个插件 chisel ,我们可以使用chisel 提供的命令来打印这个对象, 例如

(lldb) pinternals 0x7d2974a0
(JChatAboutMeViewController) $4 = {
UIViewController = {
UIResponder = {
NSObject = {
isa = JChatAboutMeViewController
}
_hasOverrideClient = '\0'
_hasOverrideHost = '\0'
_hasInputAssistantItem = '\0'
}
_overrideTransitioningDelegate = nil
_view = nil
_tabBarItem = nil
_navigationItem = 0x7d4abf40
_toolbarItems = nil
_title = nil
_nibName = nil
_nibBundle = nil
_parentViewController = 0x7cb84c00
_childModalViewController = nil
_parentModalViewController = nil
_previousRootViewController = nil
_modalTransitionView = nil
_modalPreservedFirstResponder = nil
_dimmingView = nil
_dropShadowView = nil
_currentAction = nil
_storyboard = nil
_externalObjectsTableForViewLoading = nil
_topLevelObjectsToKeepAliveFromStoryboard = nil
_savedHeaderSuperview = nil
_savedFooterSuperview = nil
_editButtonItem = nil
_searchDisplayController = nil
_strongSearchDisplayController = nil
_modalTransitionStyle = 0
_modalPresentationStyle = 0
_lastKnownInterfaceOrientation = 0
_popoverController = nil
_containerViewInSheet = nil
_recordedContentScrollView = nil
_afterAppearance = nil
_explicitAppearanceTransitionLevel = 0
_interfaceBuilderKeyCommands = nil
_addedKeyCommands = nil
_overrideTraitCollections = nil
_previewSourceViews = nil
_retainCount = 0
_ignoreAppSupportedOrientations = '\0'
_viewHostsLayoutEngine = '\0'
_storyboardIdentifier = nil
_transitioningDelegate = nil
_frozenTraitCollection = nil
overrideUseCustomPresentation = '\0'
_modalPresentationCapturesStatusBarAppearance = '\0'
_disablesAutomaticKeyboardDismissal = '\0'
_ignoresParentMargins = '\0'
_childViewControllers = nil
_customNavigationInteractiveTransitionDuration = 0
_customNavigationInteractiveTransitionPercentComplete = 0
_customTransitioningView = nil
_lastNotifiedTraitCollection = nil
_presentationController = nil
_preferredFocusedItem = nil
_navigationControllerContentOffsetAdjustment = 0
_contentMargin = 16
_topLayoutGuide = nil
_bottomLayoutGuide = nil
_topBarInsetGuideConstraint = nil
_bottomBarInsetGuideConstraint = nil
_storyboardSegueTemplates = nil
_segueResponsibleForModalPresentation = nil
_sourceViewControllerIfPresentedViaPopoverSegue = nil
_modalSourceViewController = nil
_presentedStatusBarViewController = nil
_edgesForExtendedLayout = 15
__childControllerToIgnoreWhileLookingForTransitionCoordinator = nil
_presentingFocusedItem = nil
_storyboardPreviewSegueTemplates = nil
_storyboardCommitSegueTemplates = nil
_storyboardPreviewingRegistrants = nil
__embeddedView = 0xffffffff
__embeddingView = 0x78b909c0
__embeddedDelegate = 0x00007faa
_originalPresentationController = 0x78af3630
_temporaryPresentationController = 0x00007faa
}
}

这样我们就能看到这个对象的内部信息了。

chisel 还提供一些便捷的功能,比如打印 pviews 递归打印层级,不过我更喜欢使用 xcode 自带的debug view hierarchy,这样更加直观。

我们可以使用 pvc 递归输出试图控制器的层级关系,例如

(lldb) pvc
<JChatSwift.JChatMainTabViewController 0x7d49d560>, state: appeared, view: <UILayoutContainerView 0x7d479860>
| <UINavigationController 0x7c3afc00>, state: appeared, view: <UILayoutContainerView 0x7be8e9a0>
| | <JChatConversationListViewController 0x7d49d2b0>, state: disappeared, view: <UIView 0x7d375320> not in the window
| | <JChatChattingViewController 0x7bfbdde0>, state: appeared, view: <UIView 0x7d373b30>
| <UINavigationController 0x7eb7a000>, state: disappeared, view: <UILayoutContainerView 0x7d4a2c20> not in the window
| | <JChatContactsViewController 0x7bea3d70>, state: disappeared, view: <UILayoutContainerView 0x7d4a1d40> not in the window
| <UINavigationController 0x7cb84c00>, state: disappeared, view: <UILayoutContainerView 0x7bf87590> not in the window
| | <JChatAboutMeViewController 0x7d2974a0>, state: disappeared, view: (view not loaded)

通过 pvc 和 pinternals 这样我们就可以在任何地方了解我们所有界面状态了。

在 xcode8 以后,我们也可以通过debug memory graph 来查看程序运行的内存状态。

thread

我们可以使用 thread backtrace 来输出线程的堆栈信息,例如

(lldb) thread backtrace  // 这个命令可以简写为bt
* thread #1: tid = 0x14169a, 0x001273e5 JChatSwift`JChatChattingViewController.viewDidLayoutSubviews(self=0x7b6bc560) -> () + 21 at JChatChattingViewController.swift:48, queue = 'com.apple.main-thread', stop reason = breakpoint 7.1
* frame #0: 0x001273e5 JChatSwift`JChatChattingViewController.viewDidLayoutSubviews(self=0x7b6bc560) -> () + 21 at JChatChattingViewController.swift:48
frame #1: 0x00127502 JChatSwift`@objc JChatChattingViewController.viewDidLayoutSubviews() -> () + 34 at JChatChattingViewController.swift:0
frame #2: 0x039dc811 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1598
frame #3: 0x0305c1b9 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 59
frame #4: 0x03791769 QuartzCore`-[CALayer layoutSublayers] + 141
frame #5: 0x03784a47 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 401
frame #6: 0x0378489d QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 21
frame #7: 0x0370e49f QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 339
frame #8: 0x0373d290 QuartzCore`CA::Transaction::commit() + 498
frame #9: 0x0373eda0 QuartzCore`CA::Transaction::flush_transaction() + 38
frame #10: 0x0393685c UIKit`_afterCACommitHandler + 375
frame #11: 0x022f676e CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
frame #12: 0x022f66c7 CoreFoundation`__CFRunLoopDoObservers + 391
frame #13: 0x022da3a6 CoreFoundation`__CFRunLoopRun + 1414
frame #14: 0x022d9bab CoreFoundation`CFRunLoopRunSpecific + 395
frame #15: 0x022d9a0b CoreFoundation`CFRunLoopRunInMode + 123
frame #16: 0x06a1ab4c GraphicsServices`GSEventRunModal + 177
frame #17: 0x06a1a9c7 GraphicsServices`GSEventRun + 80
frame #18: 0x039077fb UIKit`UIApplicationMain + 148
frame #19: 0x001a7ce1 JChatSwift`main + 145 at AppDelegate.swift:15
frame #20: 0x06037799 libdyld.dylib`start + 1

我们可以看到程序停在JChatChattingViewController.swift:48 这一行

watchpoint

监视某个变量的改变,有时候我们想知道一个对象在什么时候被修改了,我们可以使用 watchpoint set var,当var 改变的时候程序就就会停在改变的地方, 如果用 chisel

的话,我们可以使用 wivar 命令来监听值的变化,例如

(lldb) wivar self name

作者:HuminiOS - 极光

原文:iOS 调试篇

知乎专栏:极光日报

iOS 调试心得的更多相关文章

  1. LPC2478_调试心得(转)

    1.在调试“E:\htwang\smart2200v201\ARM嵌入式系统实验教程(二)\开发板出厂编程程序\液晶显示程序\LCM_Disp”的程序时,想使用外部RAM进行仿真调试,在将ADS1.2 ...

  2. iOS调试

    iOS高效调试 写代码难免出现bug.储备些调试技能绝对能够提高你的工作效率,让bug无所遁形.下面就和大家分享一些我在工作中常用的iOS调试小技能. 1. 打印 最简单,基础的调试方法就是打印日志了 ...

  3. Xamarin.iOS调试提示需要iOS SDK

    Xamarin.iOS调试提示需要iOS SDK   错误信息:The version of Xamarin.iOS requires th iOS 9.3 SDK (shipped with Xco ...

  4. iOS 调试 crash breakpoint EXC_BAD_ACCESS SIGABRT

    原文地址:iOS 调试 crash breakpoint EXC_BAD_ACCESS SIGABRT作者:流年若离殇 在调试程序的时候,总是碰到crash的bug,而且一追踪就是一些汇编的代码,让人 ...

  5. MIPI接口LCD屏调试心得(转)

    源: MIPI接口LCD屏调试心得

  6. 提高Android和iOS调试编译速度

    http://www.cnblogs.com/findumars/p/7841252.html 提高Android和iOS调试编译速度   如果您使用Delphi开发App,就会遇到:Android和 ...

  7. uniapp - 关于ios调试

    [ios调试] 1.一台windows电脑.一根apple数据线(一旦连接以后,apple设备就会自动识别itunes软件,如果没有安装会提示) 2.安装itunes (爱思助手) 3.官方教程:ht ...

  8. VGA调试心得

    以前自己调试过视频信号,无非就时钟加行场同步加数据线,如果视频信号出问题,第一看现象,第二测频率,反正出问题不是消隐信号出问题,就是时钟频率出问题.通过这种方式也调试成功过几个显示屏,然后就以为自己对 ...

  9. React Native调试心得

    在做React Native开发时,少不了的需要对React Native程序进行调试.调试程序是每一位开发者的基本功,高效的调试不仅能提高开发效率,也能降低Bug率.本文将向大家分享React Na ...

随机推荐

  1. --@angularjs--理解Angular中的$apply()以及$digest()

    $apply() 和 $digest() 在 AngularJS 中是两个核心概念,但是有时候它们又让人困惑.而为了了解 AngularJS 的工作方式,首先需要了解 $apply() 和 $dige ...

  2. Mac 电脑系统的重装

    首先来说一下我为什么会想到重装Mac的系统呢??? 其实呢  很简单的一个理由,在我写上一个项目的时候,在功能code编写完成后,在模拟器上运行是完全没有问题的,但是在真机上就不行,大家也都知道,在X ...

  3. jQuery事件绑定、解绑、命名空间

    jQuery事件绑定.解绑.命名空间 <%@ page language="java" import="java.util.*" pageEncoding ...

  4. 如何设置secureCRT的鼠标右键为弹出文本操作菜单功能

    secureCRT的鼠标右键功能默认是粘贴的功能,用起来和windows系统的风格不一致, 如果要改为右键为弹出文本操作菜单功能,方便对选择的内容做拷贝编辑操作,可以在 options菜单----&g ...

  5. nodejs 中使用 mocha + should + jscoverage 生成 单元测试覆盖率报告

    最近一直在做nodejs,而关于js的单元测试覆盖率网上资料比较少而且吧比较零散,我从网上找来一些资料整理一下分析给大家,希望大家可以少走弯路. 首先我是从windows环境下测试的,用到的工具有 m ...

  6. puppet来管理文件和软件包

    puppet来管理文件和软件包 1 exec的使用,可以运行shell命令 为配置文件添加配置,指示写了关键部分,其他配置省略没写 代码示例如下: [root@pup manifests]# cat ...

  7. python中关于发邮件的示例

    发送邮件示例代码如下: from WebUtils import ProperitiesLoad from email.mime.text import MIMEText from email.mim ...

  8. js 计算月/周的第一天和最后一天

    因为项目开发中遇到需要向后台传本周的开始和结束时间,以及上一周的起止时间,就琢磨了半天,总算写出来一套,写篇文章是为了方便自己记忆,也是分享给需要的人,水平有限,写的不好请见谅: getDateStr ...

  9. 无限“递归”的python程序

    如果一个函数直接或者间接调用了自己,那么就形成了递归(recursion),比如斐波那契数列的一个实现 def fib(n): if n <= 2: return 1 else: return ...

  10. 持久层框架之MyBatis

    1.mybatis框架介绍: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...