Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

lldb

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

like image 952
Mike Ellery Avatar asked Jun 05 '26 01:06

Mike Ellery


2 Answers

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.

like image 88
Jim Ingham Avatar answered Jun 06 '26 17:06

Jim Ingham


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) 
like image 44
Mike Ellery Avatar answered Jun 06 '26 18:06

Mike Ellery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!