How to print Chromium string16 values from lldb using python scripts

I found this here: http://stackoverflow.com/questions/12923873/how-to-print-wchar-t-string-in-lldb with a minor tweak of if -> elif

Thanks stack overflow!

Python file:

$ cat ~/work/lldbscripts/unsignedshortptrsummary.py
import lldb
def unsignedshortptr_SummaryProvider(valobj, dict):
  e = lldb.SBError()
  s = u'"'
  if valobj.GetValue() != 0:
    i = 0
    newchar = -1
    while newchar != 0:
          # read next wchar character out of memory
      data_val = valobj.GetPointeeData(i, 1)
      size = data_val.GetByteSize()
      if size == 1:
        newchar = data_val.GetUnsignedInt8(e, 0)    # utf-8
      elif size == 2:
        newchar = data_val.GetUnsignedInt16(e, 0)   # utf-16
      elif size == 4:
        newchar = data_val.GetUnsignedInt32(e, 0)   # utf-32
      else:
        return "<errorsize: %i>" % size
      if e.fail:
        return '<errorfail>'
      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')

init file:

$ cat .lldbinit
command script import /Users/justincohen/work/lldbscripts/unsignedshortptrsummary.py
type summary add -F unsignedshortptrsummary.unsignedshortptr_SummaryProvider "unsigned short *"

Gives you:

(lldb) p new_text
(string16) $5 = {
  (std::basic_string<unsigned short, base::string16_char_traits, std::allocator<unsigned short> >::_Alloc_hider) _M_dataplus = {
    (unsigned short *) _M_p = 0x110625cc "This is a string16!!"
  }
}

Leave a Reply