是否有一个LLDB命令可以将原始地址转换为可用的Swift类?
例如:
(lldb) po 0x7df67c50 as MKPinAnnotationView
我知道这个地址指向MKPinAnnotationView,但它不在我可以选择的框架中。但是,我想将原始地址转换为MKPinAnnotationView,以便我可以检查其属性。这可能吗?
在Xcode 8.2.1和Swift 3下,lldb命令po或p不适用于类型变量。您将需要使用swift命令print来检查键入的对象实例的属性。 (感谢cbowns's answer!)E.g。:
(lldb) expr -l Swift -- import UIKit
(lldb) expr -l Swift -- let $pin = unsafeBitCast(0x7df67c50, to: MKPinAnnotationView.self)
(lldb) expr -l Swift -- print($pin.alpha)
po
是一个别名,这意味着它可以被覆盖。您可以通过使用objc处理十六进制地址来覆盖po
:
command regex po
s/(0x[[:xdigit:]]+)/expression -l objc -O -- %1/
s/(.+)/expression -O -- %1/
要查看它有什么影响,您可以告诉lldb扩展这些别名:
(lldb) settings set interpreter.expand-regex-aliases true
我也创造了https://github.com/kastiglione/swift_po,它是Swift的替代po
。它处理对象地址,并且还有一些其他改进。
您可以使用Swift的unsafeBitCast
函数将地址强制转换为对象实例:
(lldb) e let $pin = unsafeBitCast(0x7df67c50, MKPinAnnotationView.self)
(lldb) po $pin
然后你可以像往常一样使用$pin
- 访问属性,调用方法等。
查看此文章以获取更多信息:Swift Memory Dumping。
expression
的lldb格式似乎在Xcode 7.3中已经改变。以下让我开始:
(lldb) expr -l Swift -- import UIKit
(lldb) expr -l Swift -- let $view = unsafeBitCast(0x7fb75d8349c0, UIView.self)
从Xcode 8 / Swift 3开始,这对我有用。 (这是基于@sfaxon's answer。)
(lldb) expr -l Swift -- import UIKit
(lldb) expr -l Swift -- let $nav = unsafeBitCast(0x1030ff000, to: UINavigationController.self)
对于自定义类,您需要导入项目
expr -l Swift -- import MyTestProject
expr -l Swift -- let $vc = unsafeBitCast(0x7fad22c066d0, ViewController.self)
expr -l Swift -- print($vc.view)
感谢上面的所有答案,unsafeBitCast也适用于Xcode 8.3.2 / Swift 3 / macOS / Cocoa应用程序。
记住当前实例的地址
(lldb) p tabView.controlTint
(NSControlTint) $R10 = defaultControlTint
(lldb) p self
(LearningStoryboard.NSTabViewController) $R11 = 0x00006080000e2280 {
.....
后来,检查一下
(lldb) p unsafeBitCast(0x00006080000e2280, to: NSTabViewController.self).tabView.controlTint
(NSControlTint) $R20 = graphiteControlTint
(lldb) p $R11.tabView.controlTint
(NSControlTint) $R21 = graphiteControlTint
如果发生这样的事情
(lldb) p unsafeBitCast(0x00006080000e2280, to: NSTabViewController.self).tabView.controlTint
error: use of undeclared identifier 'to'
(lldb) p $R11.tabView.controlTint
error: use of undeclared identifier '$R11'
确保选择一个Swift源代码的堆栈帧而不是汇编器。
当通过单击“暂停”按钮暂停应用程序或因异常而停止应用程序时,可能会发生这种情况。通过相应地选择堆栈帧,让lldb推断出正确的编程语言。
Objective-C版本
po ((MKPinAnnotationView *)0x7df67c50).alpha
当您的LLDB会话在Swift环境中启动时,@ Xi Chen的答案非常有效。但是,在某些情况下,您可能已在Swift上下文之外的断点处停止;例如,当它是Objective-C API的符号断点时。
error: unknown type name 'let'
error: use of undeclared identifier 'unsafeBitCast'
在这种情况下,您需要使用Objective-C以旧方式执行此操作:
e MKPinAnnotationView *$pin = (MKPinAnnotationView *)0x7df67c50
现在你可以像你一样使用$pin
。
最简单的方法,快捷4
expr unsafeBitCast(0x7df67c50, to: MKPinAnnotationView.self)