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 be used for improving the formatter
8 def read_memory(process,location,size):
10 error = lldb.SBError()
11 for x in range(0,size-1):
12 byte = process.ReadUnsignedFromMemory(x+location,1,error)
14 data = data + "err%s" % "" if x == size-2 else ":"
17 data = data + "0x%x" % byte
29 data = data + "(%s)" % chr(byte)
32 except Exception as e:
36 def diagnose_nsstring_Command_Impl(debugger,command,result,internal_dict):
38 A command to diagnose the LLDB NSString data formatter
40 (lldb) diagnose-nsstring <expr returning NSString>
42 (lldb) diagnose-nsstring @"Hello world"
44 target = debugger.GetSelectedTarget()
45 process = target.GetProcess()
46 thread = process.GetSelectedThread()
47 frame = thread.GetSelectedFrame()
48 if not target.IsValid() or not process.IsValid():
49 return "unable to get target/process - cannot proceed"
50 options = lldb.SBExpressionOptions()
51 options.SetFetchDynamicValue()
52 error = lldb.SBError()
54 nsstring = frame.EvaluateExpression(command,options)
56 nsstring = target.EvaluateExpression(command,options)
57 print >>result,str(nsstring)
58 nsstring_address = nsstring.GetValueAsUnsigned(0)
59 if nsstring_address == 0:
60 return "unable to obtain the string - cannot proceed"
62 struct $__lldb__notInlineMutable {\
65 signed long capacity;\
66 unsigned int hasGap:1;\
67 unsigned int isFixedCapacity:1;\
68 unsigned int isExternalMutable:1;\
69 unsigned int capacityProvidedExternally:1;\n\
71 unsigned long desiredCapacity:60;\n\
73 unsigned long desiredCapacity:28;\n\
75 void* contentsAllocator;\
78 struct $__lldb__CFString {\
86 struct __notInlineImmutable1 {\
89 void* contentsDeallocator;\
90 } notInlineImmutable1;\
91 struct __notInlineImmutable2 {\
93 void* contentsDeallocator;\
94 } notInlineImmutable2;\
95 struct $__lldb__notInlineMutable notInlineMutable;\
100 expression = expression + "*(($__lldb__CFString*) %d)" % nsstring_address
102 dumped = target.EvaluateExpression(expression,options)
103 print >>result, str(dumped)
105 little_endian = (target.byte_order == lldb.eByteOrderLittle)
106 ptr_size = target.addr_size
108 info_bits = dumped.GetChildMemberWithName("_cfinfo").GetChildAtIndex(0 if little_endian else 3).GetValueAsUnsigned(0)
109 is_mutable = (info_bits & 1) == 1
110 is_inline = (info_bits & 0x60) == 0
111 has_explicit_length = (info_bits & (1 | 4)) != 4
112 is_unicode = (info_bits & 0x10) == 0x10
113 is_special = (nsstring.GetDynamicValue(lldb.eDynamicCanRunTarget).GetTypeName() == "NSPathStore2")
114 has_null = (info_bits & 8) == 8
116 print >>result,"\nInfo=%d\nMutable=%s\nInline=%s\nExplicit=%s\nUnicode=%s\nSpecial=%s\nNull=%s\n" % \
117 (info_bits, "yes" if is_mutable else "no","yes" if is_inline else "no","yes" if has_explicit_length else "no","yes" if is_unicode else "no","yes" if is_special else "no","yes" if has_null else "no")
120 explicit_length_offset = 0
121 if not has_null and has_explicit_length and not is_special:
122 explicit_length_offset = 2*ptr_size
123 if is_mutable and not is_inline:
124 explicit_length_offset = explicit_length_offset + ptr_size
127 elif not is_inline and not is_mutable:
128 explicit_length_offset = explicit_length_offset + ptr_size
130 explicit_length_offset = 0
132 if explicit_length_offset == 0:
133 print >>result,"There is no explicit length marker - skipping this step\n"
135 explicit_length_offset = nsstring_address + explicit_length_offset
136 explicit_length = process.ReadUnsignedFromMemory(explicit_length_offset, 4, error)
137 print >>result,"Explicit length location is at 0x%x - read value is %d\n" % (explicit_length_offset,explicit_length)
140 location = 2 * ptr_size + nsstring_address
141 location = process.ReadPointerFromMemory(location,error)
142 elif is_inline and has_explicit_length and not is_unicode and not is_special and not is_mutable:
143 location = 3 * ptr_size + nsstring_address
145 location = 2 * ptr_size + nsstring_address
147 if not has_explicit_length:
148 print >>result,"Unicode & Inline & !Explicit is a new combo - no formula for it"
152 location = process.ReadPointerFromMemory(location,error)
154 location = nsstring_address + ptr_size + 4
156 location = 2 * ptr_size + nsstring_address
157 if not has_explicit_length:
160 location = 2 * ptr_size + nsstring_address
161 location = process.ReadPointerFromMemory(location,error)
162 print >>result,"Expected data location: 0x%x\n" % (location)
163 print >>result,"1K of data around location: %s\n" % read_memory(process,location,1024)
164 print >>result,"5K of data around string pointer: %s\n" % read_memory(process,nsstring_address,1024*5)
166 def __lldb_init_module(debugger, internal_dict):
167 debugger.HandleCommand("command script add -f %s.diagnose_nsstring_Command_Impl diagnose-nsstring" % __name__)
168 print 'The "diagnose-nsstring" command has been installed, type "help diagnose-nsstring" for detailed help.'
170 __lldb_init_module(lldb.debugger,None)
171 __lldb_init_module = None