LLDB

2018-07-12  本文已影响17人  William_

不用断点调试
如果你正在运行 iOS app,你可以试试这个: (因为全局变量是可访问的)

(lldb) po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
<UIWindow: 0x7fbb61726ac0; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x60c0002440b0>; layer = <UIWindowLayer: 0x60c000038c60>>
   | <UILayoutContainerView: 0x7fbb61606620; frame = (0 0; 375 812); autoresize = W+H; gestureRecognizers = <NSArray: 0x608000058ea0>; layer = <CALayer: 0x600000421aa0>>
   |    | <UINavigationTransitionView: 0x7fbb6140a9f0; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x608000033d20>>
   |    |    | <UIViewControllerWrapperView: 0x7fbb61403b60; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer: 0x60800002fe80>>
   |    |    |    | <UIView: 0x7fbb6160b280; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer: 0x600000421ba0>>
   |    |    |    |    | <UIView: 0x7fbb616044b0; frame = (100 100; 100 100); layer = <CALayer: 0x600000421ae0>>
   |    |    |    |    | <UIButton: 0x7fbb61605cb0; frame = (200 200; 100 30); opaque = NO; layer = <CALayer: 0x600000421980>>
   |    | <UINavigationBar: 0x7fbb6171b9c0; frame = (0 44; 375 44); opaque = NO; autoresize = W; layer = <CALayer: 0x60c0000388e0>>
   |    |    | <_UIBarBackground: 0x7fbb6171c1b0; frame = (0 -44; 375 88); userInteractionEnabled = NO; layer = <CALayer: 0x60c000038480>>
   |    |    |    | <UIImageView: 0x7fbb6171cea0; frame = (0 88; 375 0.333333); userInteractionEnabled = NO; layer = <CALayer: 0x60c000038940>>
   |    |    |    | <UIVisualEffectView: 0x7fbb6171d0d0; frame = (0 0; 375 88); layer = <CALayer: 0x60c000038960>>
   |    |    |    |    | <_UIVisualEffectBackdropView: 0x7fbb61607b50; frame = (0 0; 375 88); autoresize = W+H; userInteractionEnabled = NO; layer = <UICABackdropLayer: 0x600000422560>>
   |    |    |    |    | <_UIVisualEffectSubview: 0x7fbb61608dc0; frame = (0 0; 375 88); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x600000422700>>
   |    |    | <_UINavigationBarLargeTitleView: 0x7fbb6171e7e0; frame = (0 0; 0 44); clipsToBounds = YES; hidden = YES; layer = <CALayer: 0x60c000038cc0>>
   |    |    |    | <UILabel: 0x7fbb6171f580; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60c000282440>>
   |    |    | <_UINavigationBarContentView: 0x7fbb6171db60; frame = (0 0; 375 44); layer = <CALayer: 0x60c0000389c0>>
   |    |    | <_UINavigationBarModernPromptView: 0x7fbb61723c30; frame = (0 0; 0 44); alpha = 0; hidden = YES; layer = <CALayer: 0x60c000039b60>>
   |    |    |    | <UILabel: 0x7fbb61724050; frame = (0 25.3333; 0 0); text = ''; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60c000282760>>

有了上面的输出,我们可以获取这个 view:

(lldb) e id $myView = (id)0x7fbb616044b0

然后在调试器中改变它的背景色:
(lldb) e (void)[$myView setBackgroundColor:[UIColor blueColor]]

但是只有程序继续运行之后才会看到界面的变化。因为改变的内容必须被发送到渲染服务中,然后显示才会被更新。

渲染服务实际上是一个另外的进程 (被称作 backboardd)。这就是说即使我们正在调试的内容所在的进程被打断了,backboardd 也还是继续运行着的。

这意味着你可以运行下面的命令,而不用继续运行程序:

(lldb) e (void)[CATransaction flush]

Push 一个 View Controller
想象一个以 UINavigationController 为 root ViewController 的应用。你可以通过下面的命令,轻松地获取它:

(lldb) e id $nvc = [[[UIApplication sharedApplication] keyWindow] rootViewController]

然后 push 一个 child view controller:

(lldb) e id $vc = [UIViewController new]
(lldb) e (void)[[$vc view] setBackgroundColor:[UIColor yellowColor]]
(lldb) e (void)[$vc setTitle:@"Yay!"]
(lldb) e (void)[$nvc pushViewContoller:$vc animated:YES]

最后运行下面的命令:

e (void)[CATransaction flush]

navigation Controller 就会立刻就被 push 到你眼前。
查找按钮的 target
想象你在调试器中有一个 $myButton 的变量,可以是创建出来的,也可以是从 UI 上抓取出来的,或者是你停止在断点时的一个局部变量。你想知道,按钮按下的时候谁会接收到按钮发出的 action。非常简单:

(lldb) po [$myButton allTargets]
{(
    <MagicEventListener: 0x7fb58bd2e240>
)}
(lldb) po [$myButton actionsForTarget:(id)0x7fb58bd2e240 forControlEvent:0]
<__NSArrayM 0x7fb58bd2aa40>(
_handleTap:
)

现在你或许想在它发生的时候加一个断点。在 -[MagicEventListener _handleTap:] 设置一个符号断点就可以了,在 Xcode 和 LLDB 中都可以,然后你就可以点击按钮并停在你所希望的地方了。

上一篇下一篇

猜你喜欢

热点阅读