使用 LLDB 将变量的内容写入文件

Write content of variable to file with LLDB

我正在调试 Swift 中的一些 JSON 并且想知道是否有办法将变量的内容写入文件。考虑一个简单的例子:

let jsonString = "{\"name\": \"John\", \"age\": 21}"

如何在使用 LLDB 暂停调试时将 jsonString 写入本地文件以进行更详细的检查?

你可以打电话给write(toFile:atomically:encoding:)。例如

(lldb) call jsonString.write(toFile: "path/to/file", atomically: true, encoding: .utf8)

您可以使用 lldb 的正则表达式别名使这更方便。例如,如果你想要一个 dump 命令,像这样 运行:

(lldb) dump jsonString path/to/file

将以下内容放入您的 ~/.lldbinit 文件以使用此 dump 别名:

command regex dump 's/(.+) (.+)/call %1.write(toFile: "%2", atomically: true, encoding: .utf8)/'