我正在尝试 LLDB + python,以便更好地将 json 字符串打印到文件中。对于给定的 std::string 变量(称为缓冲区),我在 python 断点脚本中尝试了以下操作,以便漂亮地打印到文件中 - 全部都不成功:
json.dump(frame.FindVariable("buffer"), handle, indent=4)
# ^^^^ error that SBValue* is not serializable
json.dump(frame.FindVariable("buffer").GetValue(), handle, indent=4)
# ^^^^ emits null
json.dump(frame.EvaluateExpression("buffer.c_str()"), handle, indent=4)
# ^^^^ error that SBValue* is not serializable
json.dump(frame.EvaluateExpression("buffer.c_str()").GetValue(), handle, indent=4)
# ^^^^ prints an address...not useful
json.dump(frame.EvaluateExpression("buffer.c_str()").GetData(), handle, indent=4)
# ^^^^ error that SBValue* is not serializable
Run Code Online (Sandbox Code Playgroud)
有谁知道什么魔法可以让我将 std::string 框架变量转换为 python 字符串以传递给 json.dump() ?
您需要 SBValue 的摘要。这一页:
http://lldb.llvm.org/varformats.html
更详细地描述了摘要。SBValue.GetSummary 调用将执行您想要的操作。
每当 lldb 需要从实际但无用的值转换为用户友好的值时,它都会通过摘要机制来完成此操作。例如,对于 char *,0x12345 是实际值,但您确实想查看“从 0x12345 开始的 C 字符串的内容”。GetValue 将显示 0x12345,GetSummary 将显示字符串。