相信大部分人都有使用过lldb命令进行调试程序,如po
一个对象,打印出该对象的属性,lldb是一个有着REPL的特性的开源调试器,还可以支持C++,Python插件。本文要讲的就是利用Facebook开源的外部插件(Chisel)增强lldb的调试。
1.安装Chisel
奉上Chisel源码地址
首先Chisel的安装要使用homebrew,homebrew安装可以看这里,本文不多叙述
打开终端,输入一下命令
brew update
brew install chisel
安装完成后还要安装日志提示在``/.lldbinit`添加一行,如果没有该文件也可以新建一个,然后重启Xcode,令Chisel生效
2.使用Chisel
-
pvc
命令,该命令可以递归打印viewcontroller的层级,利用该命令我们可以清楚的看到viewcontroller的层级结构,还可以查看view的加载情况。
pvc
<IWTabBarController 0x7fa5f3db2580>, state: appeared, view: <UILayoutContainerView 0x7fa5f3d32c80> | <UINavigationController 0x7fa5f4869000>, state: appeared, view: <UILayoutContainerView 0x7fa5f3df9ec0> | | <IWHomeViewController 0x7fa5f3db4eb0>, state: appeared, view: <UITableView 0x7fa5f4052e00> | <UINavigationController 0x7fa5f409d000>, state: disappeared, view: <UILayoutContainerView 0x7fa5f3c70930> not in the window | | <IWMessageViewController 0x7fa5f3e72520>, state: disappeared, view: (view not loaded) | <UINavigationController 0x7fa5f4868a00>, state: disappeared, view: <UILayoutContainerView 0x7fa5f3ececb0> not in the window | | <IWDiscoverViewController 0x7fa5f3e94990>, state: disappeared, view: (view not loaded) | <UINavigationController 0x7fa5f488e000>, state: disappeared, view: <UILayoutContainerView 0x7fa5f3f144b0> not in the window | | <IWMeViewController 0x7fa5f3ed4650>, state: disappeared, view: (view not loaded)
3.pviews
命令递归查看view的层级结构,打印出每个view的frame,是否接受点击等,方便调试。
pviews self.view
<UIView: 0x7fbbdb4c8ac0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7fbbdb4c7b30>> | <_UILayoutGuide: 0x7fbbdb4c8e90; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fbbdb4c7730>> | <_UILayoutGuide: 0x7fbbdb4c99e0; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fbbdb4c7a20>> | <UIView: 0x7fbbdb4cbf90; frame = (100 100; 150 150); layer = <CALayer: 0x7fbbdb4cb600>> | | <UILabel: 0x7fbbdb4c80a0; frame = (0 0; 100 100); text = 'chisel'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fbbdb4c8490>>
2.fv
和 fvc
,这两个命令都是用来查看当前内存中存在的实例对象,其中fv是对view进行搜索,而fvc是对viewcontroller进行搜索,二者都支持正则搜索
(lldb) fv cus 0x7fd5aa63deb0 IWCustomerCell 0x7fd5aa537240 IWCustomerCell 0x7fd5aa7959e0 IWCustomerCell 0x7fd5aa793460 IWCustomerCell 0x7fd5aa7915c0 IWCustomerCell 0x7fd5aa78daf0 IWCustomerCell (lldb) fvc home 0x7fd5aa4aa340 IWHomeViewController
5.show
和hide
命令,这两个命令可以显示或者隐藏某个viewOrLayer
6.caflush这个命令会重新绘制界面,当你在调试界面颜色或者坐标之类的时候可以直接在控制台修改view的属性,然后caflush
就可以看到效果了
(lldb) p self.tview (UIView *) $2 = 0x00007f8651564a60 (lldb) e (void)[$1 setBackgroundColor:[UIColor greenColor]] (lldb) caflush
7,bmessage
该命令用于打断点。在日常调试中,我们可能在[MyViewController viewWillAppear:]
中添加断点调试,但是如果该方法没有实现,通常做法是子类中实现,然后打断点。现在使用bmessage
也可以试下调试功能(lldb) bmessage -[MyViewController viewWillAppear:]
,上面命令可以在其父类的viewWillAppear:
中打断点,
8,visualize
一个有意思的命令,它可以让你使用预览打开一个 UIImage, CGImageRef, UIView, 或 CALayer。定位到每个view的具体情况
(lldb) visualize imageView
3,Last but the most important
Reference article
Debug of Xcode: Fix Console po frame Printing
Dancing in the Debugger — A Waltz with LLDB
Chisel official description
网友评论