c++: LLDB + Python -如何在Python脚本中打印std::字符串

c++: LLDB + Python - how to print a std::string in the python script

本文关键字:Python 脚本 打印 std 字符串 LLDB c++      更新时间:2023-10-16

我正在尝试LLDB + python,以便更好地将json字符串打印到文件中。对于给定的std::string变量(称其为buffer),我在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()吗?

您需要SBValue的摘要。这个页面:

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

更详细地描述了摘要。SBValue。GetSummary调用会做你想做的。

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

Jim把我送到了正确的轨道上——最终对我有效的代码是:

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