简体   繁体   English

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

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

I'm experimenting with LLDB + python in order to get better printing of json strings to a file. 我正在尝试LLDB + python,以便更好地将json字符串打印到文件中。 For a given std::string variable (call it buffer), I've tried the following within a python breakpoint script in order to pretty print to a file - all have been unsuccessful: 对于给定的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

Does anyone know what magic sauce will allow me to turn a std::string frame variable into a python string for passing to json.dump() ? 有谁知道有什么神奇的调味料可以将std :: string框架变量转换为python字符串以传递给json.dump()?

Jim sent me in the right track above - the final code that worked for me is: Jim在上面的正确位置发送了我-对我有用的最终代码是:

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

You want the summary from the SBValue. 您需要SBValue中的摘要。 This page: 这一页:

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

describes the summaries in more detail. 详细描述摘要。 The SBValue.GetSummary call will do what you want. SBValue.GetSummary调用将执行您想要的操作。

Any time that lldb needs to convert from the actual but unhelpful value to a user friendly one it does this through the summary mechanism. 每当lldb需要从实际但无用的值转换为用户友好的值时,它都会通过汇总机制来完成此操作。 For instance, for a char *, 0x12345 is the actual value, but you really want to see "the contents of the C-string starting at 0x12345." 例如,对于一个char *,0x12345是实际值,但您确实想看到“ C字符串的内容从0x12345开始”。 GetValue will show 0x12345, GetSummary the string. GetValue将显示0x12345,GetSummary字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM