Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print wchar_t string in lldb?

Tags:

lldb

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

like image 872
David Lin Avatar asked Jan 15 '23 07:01

David Lin


1 Answers

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) 
like image 180
Jason Molenda Avatar answered Jan 27 '23 01:01

Jason Molenda