问题 将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

并在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 命令。


12674
2017-10-04 08:29


起源



答案:


您可以使用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 ')

然后在调试控制台中写:

comma script import ~/.lldb/po.py
print_to_file

12
2017-10-12 08:24



这里的改进:为什么不让命令成为一个参数 - 然后你可以说“print_to_file po self”?此外,我还没有检查LLDB是否为您自动重置句柄,但重置输出文件句柄似乎是一种好习惯:)最后,您实际上可以只为“劣等命令”获取SBCommandReturnObject(po self in the示例)并将其写入文件而不是劫持lldb的标准输出。如果可以避免的话,我通常会小心翼翼地尝试使用stdin / stdout。 - Enrico Granata
有没有办法重定向 所有 调试器输出到文件?我没试过上面的脚本 HandleCommand 线,它没有用。 - blackwing
恩里科,你能考虑展示一个你自己的例子/答案吗?我喜欢你的方法的声音,但我没有足够的信息来测试它。 - insitusec


答案:


您可以使用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 ')

然后在调试控制台中写:

comma script import ~/.lldb/po.py
print_to_file

12
2017-10-12 08:24



这里的改进:为什么不让命令成为一个参数 - 然后你可以说“print_to_file po self”?此外,我还没有检查LLDB是否为您自动重置句柄,但重置输出文件句柄似乎是一种好习惯:)最后,您实际上可以只为“劣等命令”获取SBCommandReturnObject(po self in the示例)并将其写入文件而不是劫持lldb的标准输出。如果可以避免的话,我通常会小心翼翼地尝试使用stdin / stdout。 - Enrico Granata
有没有办法重定向 所有 调试器输出到文件?我没试过上面的脚本 HandleCommand 线,它没有用。 - blackwing
恩里科,你能考虑展示一个你自己的例子/答案吗?我喜欢你的方法的声音,但我没有足够的信息来测试它。 - insitusec


以下是对上述一些评论的略微修改:

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)

这允许命令作为参数提供,并在运行命令后将输出文件句柄恢复为stdout。


0
2018-06-10 15:57





我创建了一个使用的Python脚本 memory read 然后处理输出以将其转换为我需要的格式(在我的情况下,每行一个值): https://gist.github.com/aleph7/96ec9b3c5df60a07b216


0
2017-07-10 21:24