xref: /openbsd-src/gnu/llvm/lldb/examples/summaries/unicode_strings.py (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1*061da546Spatrick"""
2*061da546SpatrickExample data formatters for strings represented as (pointer,length) pairs
3*061da546Spatrickencoded in UTF8/16/32 for use with the LLDB debugger
4*061da546Spatrick
5*061da546SpatrickTo use in your projects, tweak the children names as appropriate for your data structures
6*061da546Spatrickand use as summaries for your data types
7*061da546Spatrick
8*061da546SpatrickPart of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9*061da546SpatrickSee https://llvm.org/LICENSE.txt for license information.
10*061da546SpatrickSPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11*061da546Spatrick"""
12*061da546Spatrick
13*061da546Spatrickimport lldb
14*061da546Spatrick
15*061da546Spatrick
16*061da546Spatrickdef utf8_summary(value, unused):
17*061da546Spatrick    pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
18*061da546Spatrick    length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
19*061da546Spatrick    if pointer == 0:
20*061da546Spatrick        return False
21*061da546Spatrick    if length == 0:
22*061da546Spatrick        return '""'
23*061da546Spatrick    error = lldb.SBError()
24*061da546Spatrick    string_data = value.process.ReadMemory(pointer, length, error)
25*061da546Spatrick    return '"%s"' % (string_data)  # utf8 is safe to emit as-is on OSX
26*061da546Spatrick
27*061da546Spatrick
28*061da546Spatrickdef utf16_summary(value, unused):
29*061da546Spatrick    pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
30*061da546Spatrick    length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
31*061da546Spatrick    # assume length is in bytes - if in UTF16 chars, just multiply by 2
32*061da546Spatrick    if pointer == 0:
33*061da546Spatrick        return False
34*061da546Spatrick    if length == 0:
35*061da546Spatrick        return '""'
36*061da546Spatrick    error = lldb.SBError()
37*061da546Spatrick    string_data = value.process.ReadMemory(pointer, length, error)
38*061da546Spatrick    # utf8 is safe to emit as-is on OSX
39*061da546Spatrick    return '"%s"' % (string_data.decode('utf-16').encode('utf-8'))
40*061da546Spatrick
41*061da546Spatrick
42*061da546Spatrickdef utf32_summary(value, unused):
43*061da546Spatrick    pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
44*061da546Spatrick    length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
45*061da546Spatrick    # assume length is in bytes - if in UTF32 chars, just multiply by 4
46*061da546Spatrick    if pointer == 0:
47*061da546Spatrick        return False
48*061da546Spatrick    if length == 0:
49*061da546Spatrick        return '""'
50*061da546Spatrick    error = lldb.SBError()
51*061da546Spatrick    string_data = value.process.ReadMemory(pointer, length, error)
52*061da546Spatrick    # utf8 is safe to emit as-is on OSX
53*061da546Spatrick    return '"%s"' % (string_data.decode('utf-32').encode('utf-8'))
54