简体   繁体   中英

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. 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:

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() ?

Jim sent me in the right track above - the final code that worked for me is:

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. This page:

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

describes the summaries in more detail. The SBValue.GetSummary call will do what you want.

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. 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." GetValue will show 0x12345, GetSummary the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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