c++: LLDB + Python - 如何在 python 脚本中打印 std::string
c++: LLDB + Python - how to print a std::string in the python script
我正在试验 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() ?
您需要来自 SBValue 的摘要。本页:
http://lldb.llvm.org/varformats.html
更详细地描述了摘要。 SBValue.GetSummary 调用将执行您想要的操作。
任何时候 lldb 需要将实际但无用的值转换为用户友好的值时,它都会通过摘要机制执行此操作。例如,对于一个 char *,0x12345 是实际值,但您确实希望看到 "the contents of the C-string starting at 0x12345." GetValue 将显示 0x12345,GetSummary 将显示字符串。
Jim 在上面的正确轨道上发送给我 - 对我有用的最终代码是:
e = lldb.SBError()
frame.GetThread().GetProcess().ReadCStringFromMemory(frame.EvaluateExpression("buffer.c_str()").GetValueAsUnsigned(), 0xffffff, e)
我正在试验 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() ?
您需要来自 SBValue 的摘要。本页:
http://lldb.llvm.org/varformats.html
更详细地描述了摘要。 SBValue.GetSummary 调用将执行您想要的操作。
任何时候 lldb 需要将实际但无用的值转换为用户友好的值时,它都会通过摘要机制执行此操作。例如,对于一个 char *,0x12345 是实际值,但您确实希望看到 "the contents of the C-string starting at 0x12345." GetValue 将显示 0x12345,GetSummary 将显示字符串。
Jim 在上面的正确轨道上发送给我 - 对我有用的最终代码是:
e = lldb.SBError()
frame.GetThread().GetProcess().ReadCStringFromMemory(frame.EvaluateExpression("buffer.c_str()").GetValueAsUnsigned(), 0xffffff, e)