使用 Swift 和 lldb 设置符号断点

Setting a symbolic breakpoint with Swift and lldb

使用Swift时如何在lldb中设置符号断点?例如,有时我使用:

(lldb) b set -F '-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]'

但这不再有效:

Breakpoint 2: no locations (pending). WARNING: Unable to resolve breakpoint to any actual locations.

我也试过了

(lldb) b set -F 'UIView.updateConstraintsIfNeeded()'
(lldb) b set -F 'UIView.updateConstraintsIfNeeded'

但是没有爱。我想问题归结为 lldb 在使用 Swift 时认为 "fully qualified function name" 是什么。文档说:

-F ( --fullname )

Set the breakpoint by fully qualified function names. For C++ this means namespaces and all arguments, and for Objective C this means a full function prototype with class and selector. Can be repeated multiple times to make one breakpoint for multiple names.

Swift呢?

当 lldb 设置断点时,任何更高级的匹配断点(包括 -F),它需要知道目标语言,因为它所做的匹配类型取决于此。默认情况下,lldb 选择当前框架的语言,但您可以覆盖它。因此,例如,当您停在 Swift 帧中时,要中断 ObjC 符号,请执行:

(lldb) break set -F '-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]' -L objc
Breakpoint 3: where = UIKit`-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded], address = 0x0000000100963acc

或者您可以使用 -n 中断选项,它不会尝试理解它匹配的符号,而只是在符号名称中广泛搜索该字符串。

请注意,在这种情况下,您需要打破 ObjC 符号,而不是它在 Swift 中出现的方式,因为 swift 端实际上只是 objc 的垫片,并且没有真的没有 swift 可以挂钩的辅助符号。

在 lldb 控制台中,您可以提供部分名称并使用正则表达式进行自动查找:

br set -r updateConstraintsIfNeeded\]

结果是:

Breakpoint 4: where = UIKit`-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]

在 Xcode 的当前版本中,您还可以在 "add a symbolic breakpoint" UI 中使用部分名称,因为现在有代码完成功能可以帮助您。