[lldb-dev] Converting a pointer to python string in a formatter

Florin Trofin via lldb-dev lldb-dev at lists.llvm.org
Wed Mar 14 20:22:52 PDT 2018


Hi,

I came across the formatter example in unicode_strings.py where in
utf16_summary() I see this code:

string_data = value.process.ReadMemory(pointer, length, error)
# utf8 is safe to emit as-is on OSX
return '"%s"' % (string_data.decode('utf-16').encode('utf-8'))

I am trying to replicate that in my own formatter and I'm having difficulty
converting the pointer to an addr_t which is what ReadMemory wants. In my
case pointer comes from:

self.rep.GetChildMemberWithName("__l").GetChildMemberWithName("__data_")

(this is a basic_string which has a union:
struct __rep
{
   union {
   __long  __l; // Used for long strings
   __short __s; // Used for short strings  - stores in place
   __raw   __r; // ??
   };
};

and __long is defined as:

struct __long {
   pointer __data_;
   size_type __size_;
   size_type __cap_;
};

So __data_ is a basic_string::pointer (plain C pointer).

How do I convert this pointer to the addr_t that I need? I believe that the
GetChildMemberWithName returns an SBValue. I tried pointer.GetPointeeData(0,
1).GetLoadAddress() but I'm not getting the corect results.

Thanks!

Florin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20180314/fcd0ff75/attachment.html>


More information about the lldb-dev mailing list