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

Greg Clayton via lldb-dev lldb-dev at lists.llvm.org
Mon Mar 19 10:32:01 PDT 2018



> On Mar 14, 2018, at 8:22 PM, Florin Trofin via lldb-dev <lldb-dev at lists.llvm.org> wrote:
> 
> 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.

__data_ will contain the pointer value. Just do:

pointer.GetValueAsUnsigned()


> 
> Thanks!
> 
> Florin
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20180319/88eb4394/attachment.html>


More information about the lldb-dev mailing list