将lldb输出重定向到文件

Gui*_*gis 16 io-redirection lldb

我在Xcode中使用lldb,我的一个变量包含大量的JSON数据.使用po myVar对分析这些数据没有多大帮助,因为它将在微小的Xcode调试控制台中输出.

有没有办法将lldb输出重定向到文件?

我在这里看到这样的功能似乎在gdb上可用:

(gdb) set logging on
(gdb) set logging file /tmp/mem.txt
(gdb) x/512bx 0xbffff3c0
(gdb) set logging off
Run Code Online (Sandbox Code Playgroud)

并在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
Run Code Online (Sandbox Code Playgroud)

但是,该memory read命令对我的情况没有帮助,并且--outfile似乎没有可用于该print命令.

vin*_*aut 14

您可以使用Python脚本(以及更多),如下所述:

Xcode中的LLDB 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 ')
Run Code Online (Sandbox Code Playgroud)

然后在调试控制台中写:

comma script import ~/.lldb/po.py
print_to_file
Run Code Online (Sandbox Code Playgroud)

  • 这里的改进:为什么不让命令成为一个参数 - 那么你可以说“print_to_file po self”?另外,我还没有检查 LLDB 是否会自动为您重置句柄,但重置输出文件句柄似乎是一个很好的做法:)最后,您实际上可以只获取“劣等命令”的 SBCommandReturnObject(po self 在示例)并将其写入文件而不是劫持 lldb 的 stdout。如果可以避免的话,我通常会对尝试使用 stdin/stdout 持谨慎态度。 (2认同)
  • 有没有办法将*all*debugger输出重定向到文件?我在没有`HandleCommand`行的情况下尝试了上面的脚本,但它没有用. (2认同)