将 lldb 输出重定向到文件

问题描述 投票:0回答:5

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

lldb io-redirection
5个回答
18
投票

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

然后在lldb中写:

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

4
投票

我发现

session save <filename>
比这里列出的选项更好、更容易。它与您不能使用它(据我所知有选择地使用)不太一样,但是对于生成日志,它非常方便。


1
投票

这里结合了上面的一些评论,稍作修改:

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)

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


1
投票

假设您有一个名为 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'

0
投票

将 lldb 命令输出重定向到文件

两种方法:

  • 会话保存
  • lldb python 脚本添加新命令(例如:
    write

会话保存

  • 语法:
    session save {outputFileFullPath}
    • 优点:不需要额外的命令,将所有会话输出保存到文件
    • 缺点:无法将特定命令输出保存到单个文件
  • 例子:
    • session save /Users/crifan/dev/tmp/sub_1000A0460.txt
      • lldb sub_1000A0460.txt

lldb 脚本添加新命令:write

参考:4iar/lldb-write:将 lldb 命令的输出写入文件

步骤:

  • 下载代码
    • git clone https://github.com/4iar/lldb-write.git
  • 添加到 lldb 初始化脚本
    • vi ~/.lldbinit
    • 添加
      command script import /{change_to_your_path}/lldb-write/write.py
  • 重启 lldb
  • 用法
    • write /some/path/outputFile.txt {yourOriginLldbCommand}
  • 例子
    • 对于
      dis -f
      是:
      write /Users/crifan/dev/tmp/lldb_akd_symbol2575_disassemble.txt dis -f
      • lldb_akd_symbol2575_disassemble
© www.soinside.com 2019 - 2024. All rights reserved.