c ++:LLDB + Python - 如何在python脚本中打印std :: string

时间:2016-09-08 23:59:24

标签: c++ lldb

我正在尝试使用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

有谁知道什么魔术酱允许我将std :: string帧变量转换成python字符串以传递给json.dump()?

2 个答案:

答案 0 :(得分:3)

Jim在上面的正确轨道上发送了我 - 对我有用的最终代码是:

e = lldb.SBError()
frame.GetThread().GetProcess().ReadCStringFromMemory(frame.E‌​valuateExpression("b‌​uffer.c_str()").GetV‌​alueAsUnsigned(), 0xffffff, e) 

答案 1 :(得分:1)

您需要来自SBValue的摘要。本页:

http://lldb.llvm.org/varformats.html

更详细地描述了摘要。 SBValue.GetSummary调用将执行您想要的操作。

任何时候lldb需要从实际但无益的值转换为用户友好的值,它通过摘要机制完成此操作。例如,对于char *,0x12345是实际值,但您确实希望看到" C字符串的内容从0x12345开始。" GetValue将显示0x12345,GetSummary为字符串。