鉴于wchar_t* str;
哪些指向以空结尾的 utf32(或 utf16)字符串,我应该使用什么命令在 lldb 中打印它?
问问题
4960 次
2 回答
9
我假设您想将其打印为 utf8。这有点涉及 - 您需要为 python 中的类型创建一个摘要提供程序,该提供程序返回一个 utf8 字符串进行打印。不过也不是特别复杂。创建一个小python文件,如~/lldb/wcharsummary.py
内容
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')
将此加载到 lldb 并将此 python 函数设置为 wchar_t* 的摘要提供程序;最容易将其放入~/.lldbinit
文件中以供重用:
command script import ~/lldb/wcharsummary.py
type summary add -F wcharsummary.wchar_SummaryProvider "wchar_t *"
然后给定一些在 32 位 wchar_t 中有一些 utf32 编码字符的源,
NSString *str = @"こんにちは"; // 5 characters long
wchar_t *str_utf32_wchar = (wchar_t*) [[str dataUsingEncoding:NSUTF32StringEncoding] bytes];
lldb 将为我们以 utf8 格式打印它们:
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)
于 2012-10-17T01:51:42.060 回答
0
我对 Jason 的代码进行了一些修改以wxString
直接处理,而不必更改int*
指针的摘要。
通过在 Xcode 调试器控制台中键入命令来测试它script
,然后粘贴下面的代码并按 ctrl-D。然后再次在 lldb 提示符下,键入type summary add --python-function wxString_SummaryProvider "wxString"
. 适用于我的 wxWidgets 构建。
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')
于 2013-08-17T12:04:42.347 回答