5

Given wchar_t* str; which points to a null-terminated utf32 (or utf16) string, what command should I use to print it in lldb?

2 Answers 2

10

I assume you want to print it as utf8. It's a little involved - you need to create a summary provider for the type in python that returns a utf8 string for printing. It's not especially complicated though. Create a little python file like ~/lldb/wcharsummary.py with contents like

import lldb
def wchar_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
      if size == 2:
        newchar = data_val.GetUnsignedInt16(e, 0)   # utf-16
      if size == 4:
        newchar = data_val.GetUnsignedInt32(e, 0)   # utf-32
      else:
        return '<error>'
      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')

Load this in to lldb and set this python function as the summary provider for wchar_t*; easiest to put this in your ~/.lldbinit file for re-use:

command script import ~/lldb/wcharsummary.py
type summary add -F wcharsummary.wchar_SummaryProvider "wchar_t *"

then given some source that has some utf32 encoded characters in 32-bit wchar_t's,

NSString *str = @"こんにちは";  // 5 characters long
wchar_t *str_utf32_wchar = (wchar_t*) [[str dataUsingEncoding:NSUTF32StringEncoding] bytes];

lldb will print them in utf8 for us:

Process 22278 stopped
* thread #1: tid = 0x1c03, 0x0000000100000e92 a.out`main + 146 at a.m:11, stop reason = step over
    #0: 0x0000000100000e92 a.out`main + 146 at a.m:11
   8    
   9        NSString *str = @"こんにちは";  // 5 characters long
   10       wchar_t *str_utf32_wchar = (wchar_t*) [[str dataUsingEncoding:NSUTF32StringEncoding] bytes];
-> 11       printf ("0x%llx 0x%llx 0x%llx 0x%llx\n", (uint64_t) str_utf32_wchar[0], (uint64_t) str_utf32_wchar[1], 
   12                                                (uint64_t) str_utf32_wchar[2], (uint64_t) str_utf32_wchar[3]);
   13   
   14       [pool release];

(lldb) fr va
(NSAutoreleasePool *) pool = 0x0000000100108190
(NSString *) str = 0x0000000100001068 @"こんにちは"
(wchar_t *) str_utf32_wchar = 0x0000000100107f80 "こんにちは"

(lldb) p str_utf32_wchar
(wchar_t *) $0 = 0x0000000100107f80 "こんにちは"

(lldb) x/16b `str_utf32_wchar`
0x100107f80: 0xff 0xfe 0x00 0x00 0x53 0x30 0x00 0x00
0x100107f88: 0x93 0x30 0x00 0x00 0x6b 0x30 0x00 0x00
(lldb) 
10
  • Is there anything specific I need to do to enable this as of Xcode 4.6.2? I copy/pasted your sample str and str_utf32_wchar into my project and I'm still unable to see anything more than a memory address when printing the latter in lldb. Is there any way to verify that wcharsummary.wchar_SummaryProvider was found?
    – rich.e
    Apr 21, 2013 at 18:16
  • I just tried it with Xcode 4.6.2's lldb. When you do type summary add -F wcharsummary.wchar_SummaryProvider "wchar_t *" note that he wcharsummary part refers to the filename of the python file. In my case, it was saved as ~/lldb/wcharsummary.py and so the functions defined in that file are added under the wcharsummary. namespace in python. If you named your python file something different, you'll need to register it differently with type summary add. Apr 22, 2013 at 8:53
  • Hum, that is exactly how I have it set up, ala this pastebin. Still no luck. Thanks for the help, anyhow.
    – rich.e
    Apr 22, 2013 at 19:00
  • Are you trying to print UTF32 data? I think the original script worked correctly for that encoding of unicode characters but I don't think it actually worked correctly for the other encodings it claims to support -- I only tested UTF32. Apr 22, 2013 at 19:08
  • Yes, I tried both he example str / tr_utf32_wchar you used above and what I've really been after which is debugging std::wstring's on Mac. Methinks the script is not being loaded when Xcode starts, is there any way to check that?
    – rich.e
    Apr 22, 2013 at 19:51
0

I've modified Jason's code a little bit to handle wxString directly instead of having to change the summary for int* pointers.

Test it by typing the script command in Xcode debugger console, then pasting the code below and pressing ctrl-D. Then at the lldb prompt again, type type summary add --python-function wxString_SummaryProvider "wxString". Works well with my wxWidgets build.

def wxString_SummaryProvider(valobj, dict):
  e = lldb.SBError()
  charPointer = valobj.GetChildMemberWithName('m_impl').GetChildMemberWithName('_M_dataplus').GetChildMemberWithName('_M_p')
  valobj = charPointer
  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
      if size == 2:
        newchar = data_val.GetUnsignedInt16(e, 0)   # utf-16
      if size == 4:
        newchar = data_val.GetUnsignedInt32(e, 0)   # utf-32
      else:
        return '<error>'
      if e.fail:
        return '<error>'
      i = i + 1
      # add the character to our string 's'
      # print "char2 = %s" % newchar
      if newchar != 0:
        s = s + unichr(newchar)
  s = s + u'"'
  return s.encode('utf-8')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.