1 # This implements the "diagnose-nsstring" command, usually installed in the debug session like
2 # command script import lldb.diagnose
3 # it is used when NSString summary formatter fails to replicate the logic that went into LLDB making the
4 # decisions it did and providing some useful context information that can
5 # be used for improving the formatter
10 def read_memory(process
, location
, size
):
12 error
= lldb
.SBError()
13 for x
in range(0, size
- 1):
14 byte
= process
.ReadUnsignedFromMemory(x
+ location
, 1, error
)
16 data
= data
+ "err%s" % "" if x
== size
- 2 else ":"
19 data
= data
+ "0x%x" % byte
31 data
= data
+ "(%s)" % chr(byte
)
34 except Exception as e
:
39 def diagnose_nsstring_Command_Impl(debugger
, command
, result
, internal_dict
):
41 A command to diagnose the LLDB NSString data formatter
43 (lldb) diagnose-nsstring <expr returning NSString>
45 (lldb) diagnose-nsstring @"Hello world"
47 target
= debugger
.GetSelectedTarget()
48 process
= target
.GetProcess()
49 thread
= process
.GetSelectedThread()
50 frame
= thread
.GetSelectedFrame()
51 if not target
.IsValid() or not process
.IsValid():
52 return "unable to get target/process - cannot proceed"
53 options
= lldb
.SBExpressionOptions()
54 options
.SetFetchDynamicValue()
55 error
= lldb
.SBError()
57 nsstring
= frame
.EvaluateExpression(command
, options
)
59 nsstring
= target
.EvaluateExpression(command
, options
)
60 print(str(nsstring
), file=result
)
61 nsstring_address
= nsstring
.GetValueAsUnsigned(0)
62 if nsstring_address
== 0:
63 return "unable to obtain the string - cannot proceed"
65 struct $__lldb__notInlineMutable {\
68 signed long capacity;\
69 unsigned int hasGap:1;\
70 unsigned int isFixedCapacity:1;\
71 unsigned int isExternalMutable:1;\
72 unsigned int capacityProvidedExternally:1;\n\
74 unsigned long desiredCapacity:60;\n\
76 unsigned long desiredCapacity:28;\n\
78 void* contentsAllocator;\
81 struct $__lldb__CFString {\
89 struct __notInlineImmutable1 {\
92 void* contentsDeallocator;\
93 } notInlineImmutable1;\
94 struct __notInlineImmutable2 {\
96 void* contentsDeallocator;\
97 } notInlineImmutable2;\
98 struct $__lldb__notInlineMutable notInlineMutable;\
103 expression
= expression
+ "*(($__lldb__CFString*) %d)" % nsstring_address
105 dumped
= target
.EvaluateExpression(expression
, options
)
106 print(str(dumped
), file=result
)
108 little_endian
= target
.byte_order
== lldb
.eByteOrderLittle
109 ptr_size
= target
.addr_size
112 dumped
.GetChildMemberWithName("_cfinfo")
113 .GetChildAtIndex(0 if little_endian
else 3)
114 .GetValueAsUnsigned(0)
116 is_mutable
= (info_bits
& 1) == 1
117 is_inline
= (info_bits
& 0x60) == 0
118 has_explicit_length
= (info_bits
& (1 |
4)) != 4
119 is_unicode
= (info_bits
& 0x10) == 0x10
121 nsstring
.GetDynamicValue(lldb
.eDynamicCanRunTarget
).GetTypeName()
124 has_null
= (info_bits
& 8) == 8
127 "\nInfo=%d\nMutable=%s\nInline=%s\nExplicit=%s\nUnicode=%s\nSpecial=%s\nNull=%s\n"
130 "yes" if is_mutable
else "no",
131 "yes" if is_inline
else "no",
132 "yes" if has_explicit_length
else "no",
133 "yes" if is_unicode
else "no",
134 "yes" if is_special
else "no",
135 "yes" if has_null
else "no",
140 explicit_length_offset
= 0
141 if not has_null
and has_explicit_length
and not is_special
:
142 explicit_length_offset
= 2 * ptr_size
143 if is_mutable
and not is_inline
:
144 explicit_length_offset
= explicit_length_offset
+ ptr_size
147 elif not is_inline
and not is_mutable
:
148 explicit_length_offset
= explicit_length_offset
+ ptr_size
150 explicit_length_offset
= 0
152 if explicit_length_offset
== 0:
153 print("There is no explicit length marker - skipping this step\n", file=result
)
155 explicit_length_offset
= nsstring_address
+ explicit_length_offset
156 explicit_length
= process
.ReadUnsignedFromMemory(
157 explicit_length_offset
, 4, error
160 "Explicit length location is at 0x%x - read value is %d\n"
161 % (explicit_length_offset
, explicit_length
),
166 location
= 2 * ptr_size
+ nsstring_address
167 location
= process
.ReadPointerFromMemory(location
, error
)
170 and has_explicit_length
175 location
= 3 * ptr_size
+ nsstring_address
177 location
= 2 * ptr_size
+ nsstring_address
179 if not has_explicit_length
:
181 "Unicode & Inline & !Explicit is a new combo - no formula for it",
187 location
= process
.ReadPointerFromMemory(location
, error
)
189 location
= nsstring_address
+ ptr_size
+ 4
191 location
= 2 * ptr_size
+ nsstring_address
192 if not has_explicit_length
:
195 location
= 2 * ptr_size
+ nsstring_address
196 location
= process
.ReadPointerFromMemory(location
, error
)
197 print("Expected data location: 0x%x\n" % (location
), file=result
)
199 "1K of data around location: %s\n" % read_memory(process
, location
, 1024),
203 "5K of data around string pointer: %s\n"
204 % read_memory(process
, nsstring_address
, 1024 * 5),
209 def __lldb_init_module(debugger
, internal_dict
):
210 debugger
.HandleCommand(
211 "command script add -o -f %s.diagnose_nsstring_Command_Impl diagnose-nsstring"
215 'The "diagnose-nsstring" command has been installed, type "help diagnose-nsstring" for detailed help.'
219 __lldb_init_module(lldb
.debugger
, None)
220 __lldb_init_module
= None