我在 Xcode 中使用 lldb,我的一个变量包含大量 JSON 数据。使用
po myVar
对分析这些数据没有多大帮助,因为它会在微型 Xcode 调试控制台中输出。
有没有办法将 lldb 输出重定向到文件?
我看到 here 这样的功能似乎在 gdb 上可用:
(gdb) set logging on
(gdb) set logging file /tmp/mem.txt
(gdb) x/512bx 0xbffff3c0
(gdb) set logging off
并且在 lldb 中被“翻译”为:
(lldb) memory read --outfile /tmp/mem.txt --count 512 0xbffff3c0
(lldb) me r -o/tmp/mem.txt -c512 0xbffff3c0
(lldb) x/512bx -o/tmp/mem.txt 0xbffff3c0
但是,
memory read
命令对我的情况没有帮助,显然,--outfile
不适用于 print
命令。
您可以使用 Python 脚本来执行此操作(以及更多),如下所述:
在您选择的目录中创建一个名为 po.py 的文件(例如“~/.lldb”):
import lldb
def print_to_file(debugger, command, result, dict):
#Change the output file to a path/name of your choice
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
#Change command to the command you want the output of
command = "po self"
debugger.HandleCommand(command)
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f po.print_to_file print_to_file ')
然后在lldb中写:
command script import ~/.lldb/po.py
print_to_file
我发现
session save <filename>
比这里列出的选项更好、更容易。它与您不能使用它(据我所知有选择地使用)不太一样,但是对于生成日志,它非常方便。
这里结合了上面的一些评论,稍作修改:
def toFile(debugger, command, result, dict):
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
debugger.HandleCommand(command)
f.close()
debugger.SetOutputFileHandle(sys.stdout, True)
这允许命令作为参数提供,并在命令运行后将输出文件句柄恢复为标准输出。
假设您有一个名为 jsonData 的变量(具有数据类型),您可以使用以下命令将其保存到文件中:
expr jsonData.write(to: URL(fileURLWithPath: "/tmp/datadump.bin"))
或者,您可以将此变量使用的内存转储到文件中,而不是上面的命令,如下例所示:
(lldb) po jsonData
▿ Optional<Data>
▿ some : 32547 bytes
- count : 32547
▿ pointer : 0x00007fe8b69bb410
- pointerValue : 140637472797712
(lldb) memory read --force --binary --outfile /tmp/datadump.bin --count 32547 0x00007fe8b69bb410
32547 bytes written to '/tmp/datadump.bin'
两种方法:
write
)session save {outputFileFullPath}
session save /Users/crifan/dev/tmp/sub_1000A0460.txt
参考:4iar/lldb-write:将 lldb 命令的输出写入文件
步骤:
git clone https://github.com/4iar/lldb-write.git
vi ~/.lldbinit
command script import /{change_to_your_path}/lldb-write/write.py
write /some/path/outputFile.txt {yourOriginLldbCommand}
dis -f
是:write /Users/crifan/dev/tmp/lldb_akd_symbol2575_disassemble.txt dis -f