[lldb-dev] Custom formatter request

Somorjai, Akos ASomorjai at graphisoft.com
Mon Dec 10 20:50:10 PST 2012


Hi Enrico & Daniel,
Thanks all, I'm browsing through the formatters page. I managed to throw together a skeleton, but it doesn't seem to work too well. Maybe I failed to grasp something:

import lldb
def UniString_SummaryProvider(valobj, dict):
  e = lldb.SBError()
  s = u'"'
  if valobj.GetValue() != 0:
    content = valobj.GetChildMemberWithName('content')
    length = content.GetPointeeData(0,1).GetChildMemberWithName('length')
    string = content.GetPointeeData(0,1).GetChildMemberWithName('string')
    i = 0
    newchar = -1
    while newchar != 0 and i < length:
      # read next wchar character out of memory
      data_val = string.GetPointeeData(i, 1)
      newchar = data_val.GetUnsignedInt16(e, 0)   # utf-16
      if e.fail:
        return '<error>'
      i = i + 1
      # add the character to our string 's'
      if newchar != 0:
        s = s + unichr(newchar)
  s = s + u'"'
  return s.encode('utf-8')

def __lldb_init_module(debugger,dict):
  debugger.HandleCommand("type summary add -F UniString.UniString_SummaryProvider GS::UniString")



Yes, I'd like to show the string member (it's in UTF-16) in the summary. I was looking at the CFString.py formatter, but that seems to be an overkill in our case.
The string[1] member is an 'old' C trick, here's the allocation routine. It preallocates a separate buffer of the right size, then uses placement new to put the SharedBuffer member there.

inline GS::UniString::SharedBuffer::SharedBuffer (USize initialLength, USize initialCapacity, Int32 initialRefCounter):
length     (initialLength),
capacity   (initialCapacity),
refCounter (initialRefCounter)
{
}

inline USize GS::UniString::CapacityToBufferSize (USize capacity)
{
return (sizeof (SharedBuffer) + (capacity - 1) * sizeof (unichar));
}

inline USize GS::UniString::BufferSizeToCapacity (USize bufferSize)
{
return ((bufferSize - sizeof (SharedBuffer)) / sizeof (unichar) + 1);
}

GS::UniString::SharedBuffer* GS::UniString::AllocateBuffer (USize capacity)
{
if (capacity == 0)
return ShareEmptyBuffer ();

capacity++; // to ensure capacity for the closing 0 (used in conversion)

USize bufferSize = CapacityToBufferSize (capacity);
if (bufferSize < MinBufferSize4) {
if (bufferSize < MinBufferSize1)
bufferSize = MinBufferSize1;
else if (bufferSize < MinBufferSize2)
bufferSize = MinBufferSize2;
else if (bufferSize < MinBufferSize3)
bufferSize = MinBufferSize3;
else
bufferSize = MinBufferSize4;
}

void* buffer = cachedAllocator.Allocate (bufferSize, &bufferSize);
capacity = BufferSizeToCapacity (bufferSize);

return new (buffer) SharedBuffer (0, capacity, 1);
}

Best, Akos

On Dec 10, 2012, at 11:32 PM, Enrico Granata <egranata at apple.com<mailto:egranata at apple.com>>
 wrote:

Hi.
It looks like your task should be relatively easy since you have the full definition of the UniString and the SharedBuffer.
I am assuming you will want to show the string content as your summary (as it happens for std::string and NSString).
If so, how are you going to use the string buffer? Is it going to contain a pointer to the real data?
I am probably slightly confused by your comment next to it.
Could you explain the logic for storing the string data or provide an example of code that handles e.g. the string allocation, or access to it?
Given that understanding, it should be relatively straightforward to accomplish your task.

Enrico Granata
✉ egranata@.com
✆ (408) 972-7683

On Dec 10, 2012, at 7:35 AM, "Somorjai, Akos" <ASomorjai at graphisoft.com<mailto:ASomorjai at graphisoft.com>> wrote:

Hello everyone,

I need a little help with a custom formatter for the class below; I'd appreciate if someone could throw together a skeleton I can start from.

Thanks,

Akos


Here's the class layout:

class UniString {

private:
struct SharedBuffer;

SharedBuffer* content; // shared buffer storing content of the string

// Implementation classes and structures

struct SharedBuffer {
USize length; // length of the string in UniChar::Layout units
USize capacity; // capacity of the allocated buffer in UniChar::Layout units
SInt32 refCounter; // stores number of references to this shared buffer
char reserved[2]; // padding reserved for the future use
unichar string[1]; // buffer storing content of the string (extends beyond of the SharedBuffer)

inline SharedBuffer ();
inline SharedBuffer (USize initialLength, USize initialCapacity, Int32 initialRefCounter);
};
…
};


_______________________________________________
lldb-dev mailing list
lldb-dev at cs.uiuc.edu<mailto:lldb-dev at cs.uiuc.edu>
http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev


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


More information about the lldb-dev mailing list