blob: c954ae4d0de33c07001367a9652af4650919fd4b [file] [log] [blame]
Enrico Granataeb4a4792012-02-23 23:10:27 +00001# synthetic children and summary provider for CFString
2# (and related NSString class)
3import lldb
Enrico Granata896cd1d2012-03-01 19:32:33 +00004import objc_runtime
Enrico Granataeb4a4792012-02-23 23:10:27 +00005
6def CFString_SummaryProvider (valobj,dict):
7 provider = CFStringSynthProvider(valobj,dict);
8 if provider.invalid == False:
9 try:
10 summary = provider.get_child_at_index(provider.get_child_index("content")).GetSummary();
11 except:
12 summary = None
13 if summary == None:
14 summary = 'no valid string here'
15 return '@'+summary
16 return ''
17
18def CFAttributedString_SummaryProvider (valobj,dict):
19 offset = valobj.GetTarget().GetProcess().GetAddressByteSize()
20 pointee = valobj.GetValueAsUnsigned(0)
21 summary = 'no valid string here'
22 if pointee != None and pointee != 0:
23 pointee = pointee + offset
24 child_ptr = valobj.CreateValueFromAddress("string_ptr",pointee,valobj.GetType())
25 child = child_ptr.CreateValueFromAddress("string_data",child_ptr.GetValueAsUnsigned(),valobj.GetType()).AddressOf()
26 provider = CFStringSynthProvider(child,dict);
27 if provider.invalid == False:
28 try:
29 summary = provider.get_child_at_index(provider.get_child_index("content")).GetSummary();
30 except:
31 summary = 'no valid string here'
32 if summary == None:
33 summary = 'no valid string here'
34 return '@'+summary
35
36
37def __lldb_init_module(debugger,dict):
38 debugger.HandleCommand("type summary add -F CFString.CFString_SummaryProvider NSString CFStringRef CFMutableStringRef")
39 debugger.HandleCommand("type summary add -F CFString.CFAttributedString_SummaryProvider NSAttributedString")
40
41class CFStringSynthProvider:
42 def __init__(self,valobj,dict):
43 self.valobj = valobj;
44 self.update()
45
46 # children other than "content" are for debugging only and must not be used in production code
47 def num_children(self):
48 if self.invalid:
49 return 0;
50 return 6;
51
52 def read_unicode(self, pointer):
53 process = self.valobj.GetTarget().GetProcess()
54 error = lldb.SBError()
55 pystr = u''
56 # cannot do the read at once because the length value has
57 # a weird encoding. better play it safe here
58 while True:
59 content = process.ReadMemory(pointer, 2, error)
60 new_bytes = bytearray(content)
61 b0 = new_bytes[0]
62 b1 = new_bytes[1]
63 pointer = pointer + 2
64 if b0 == 0 and b1 == 0:
65 break
66 # rearrange bytes depending on endianness
67 # (do we really need this or is Cocoa going to
68 # use Windows-compatible little-endian even
69 # if the target is big endian?)
70 if self.is_little:
71 value = b1 * 256 + b0
72 else:
73 value = b0 * 256 + b1
74 pystr = pystr + unichr(value)
75 return pystr
76
77 # handle the special case strings
78 # only use the custom code for the tested LP64 case
79 def handle_special(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +000080 if self.is_64_bit == False:
Enrico Granataeb4a4792012-02-23 23:10:27 +000081 # for 32bit targets, use safe ObjC code
82 return self.handle_unicode_string_safe()
83 offset = 12
84 pointer = self.valobj.GetValueAsUnsigned(0) + offset
85 pystr = self.read_unicode(pointer)
86 return self.valobj.CreateValueFromExpression("content",
87 "(char*)\"" + pystr.encode('utf-8') + "\"")
88
89 # last resort call, use ObjC code to read; the final aim is to
90 # be able to strip this call away entirely and only do the read
91 # ourselves
92 def handle_unicode_string_safe(self):
93 return self.valobj.CreateValueFromExpression("content",
94 "(char*)\"" + self.valobj.GetObjectDescription() + "\"");
95
96 def handle_unicode_string(self):
97 # step 1: find offset
98 if self.inline:
99 pointer = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base();
100 if self.explicit == False:
101 # untested, use the safe code path
102 return self.handle_unicode_string_safe();
103 else:
Enrico Granata74ec8f92012-03-01 19:48:54 +0000104 # a full pointer is skipped here before getting to the live data
105 if self.is_64_bit == False:
Enrico Granata896cd1d2012-03-01 19:32:33 +0000106 pointer = pointer + 4
107 else:
108 pointer = pointer + 8;
Enrico Granataeb4a4792012-02-23 23:10:27 +0000109 else:
110 pointer = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base();
111 # read 8 bytes here and make an address out of them
112 try:
113 vopointer = self.valobj.CreateChildAtOffset("dummy",
114 pointer,self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType());
115 pointer = vopointer.GetValueAsUnsigned(0)
116 except:
117 return self.valobj.CreateValueFromExpression("content",
118 '(char*)"@\"invalid NSString\""')
119 # step 2: read Unicode data at pointer
120 pystr = self.read_unicode(pointer)
121 # step 3: return it
122 return self.valobj.CreateValueFromExpression("content",
123 "(char*)\"" + pystr.encode('utf-8') + "\"")
124
125 def handle_inline_explicit(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000126 if self.is_64_bit:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000127 offset = 24
128 else:
129 offset = 12
130 offset = offset + self.valobj.GetValueAsUnsigned(0)
131 return self.valobj.CreateValueFromExpression("content",
132 "(char*)(" + str(offset) + ")")
133
134 def handle_mutable_string(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000135 if self.is_64_bit:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000136 offset = 16
137 else:
138 offset = 8
139 data = self.valobj.CreateChildAtOffset("content",
140 offset, self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType());
141 data_value = data.GetValueAsUnsigned(0)
142 data_value = data_value + 1
143 return self.valobj.CreateValueFromExpression("content", "(char*)(" + str(data_value) + ")")
144
145 def handle_UTF8_inline(self):
146 offset = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base();
147 if self.explicit == False:
148 offset = offset + 1;
149 return self.valobj.CreateValueFromAddress("content",
150 offset, self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar)).AddressOf();
151
152 def handle_UTF8_not_inline(self):
153 offset = self.size_of_cfruntime_base();
154 return self.valobj.CreateChildAtOffset("content",
155 offset,self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType());
156
157 def get_child_at_index(self,index):
158 if index == 0:
159 return self.valobj.CreateValueFromExpression("mutable",
160 str(int(self.mutable)));
161 if index == 1:
162 return self.valobj.CreateValueFromExpression("inline",
163 str(int(self.inline)));
164 if index == 2:
165 return self.valobj.CreateValueFromExpression("explicit",
166 str(int(self.explicit)));
167 if index == 3:
168 return self.valobj.CreateValueFromExpression("unicode",
169 str(int(self.unicode)));
170 if index == 4:
171 return self.valobj.CreateValueFromExpression("special",
172 str(int(self.special)));
173 if index == 5:
174 # we are handling the several possible combinations of flags.
175 # for each known combination we have a function that knows how to
176 # go fetch the data from memory instead of running code. if a string is not
177 # correctly displayed, one should start by finding a combination of flags that
178 # makes it different from these known cases, and provide a new reader function
179 # if this is not possible, a new flag might have to be made up (like the "special" flag
180 # below, which is not a real flag in CFString), or alternatively one might need to use
181 # the ObjC runtime helper to detect the new class and deal with it accordingly
182 if self.mutable == True:
183 return self.handle_mutable_string()
184 elif self.inline == True and self.explicit == True and \
185 self.unicode == False and self.special == False and \
186 self.mutable == False:
187 return self.handle_inline_explicit()
188 elif self.unicode == True:
189 return self.handle_unicode_string();
190 elif self.special == True:
191 return self.handle_special();
192 elif self.inline == True:
193 return self.handle_UTF8_inline();
194 else:
195 return self.handle_UTF8_not_inline();
196
197 def get_child_index(self,name):
198 if name == "content":
199 return self.num_children() - 1;
200 if name == "mutable":
201 return 0;
202 if name == "inline":
203 return 1;
204 if name == "explicit":
205 return 2;
206 if name == "unicode":
207 return 3;
208 if name == "special":
209 return 4;
210
211 def is_64bit(self):
212 return self.valobj.GetTarget().GetProcess().GetAddressByteSize() == 8
213
214 def is_little_endian(self):
215 return self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle
216
217 # CFRuntimeBase is defined as having an additional
218 # 4 bytes (padding?) on LP64 architectures
219 # to get its size we add up sizeof(pointer)+4
220 # and then add 4 more bytes if we are on a 64bit system
221 def size_of_cfruntime_base(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000222 if self.is_64_bit == True:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000223 return 8+4+4;
224 else:
225 return 4+4;
226
227 # the info bits are part of the CFRuntimeBase structure
228 # to get at them we have to skip a uintptr_t and then get
229 # at the least-significant byte of a 4 byte array. If we are
230 # on big-endian this means going to byte 3, if we are on
231 # little endian (OSX & iOS), this means reading byte 0
232 def offset_of_info_bits(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000233 if self.is_64_bit == True:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000234 offset = 8;
235 else:
236 offset = 4;
237 if self.is_little == False:
238 offset = offset + 3;
239 return offset;
240
241 def read_info_bits(self):
242 cfinfo = self.valobj.CreateChildAtOffset("cfinfo",
243 self.offset_of_info_bits(),
244 self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar));
245 cfinfo.SetFormat(11)
246 info = cfinfo.GetValue();
247 if info != None:
248 self.invalid = False;
249 return int(info,0);
250 else:
251 self.invalid = True;
252 return None;
253
254 # calculating internal flag bits of the CFString object
255 # this stuff is defined and discussed in CFString.c
256 def is_mutable(self):
257 return (self.info_bits & 1) == 1;
258
259 def is_inline(self):
260 return (self.info_bits & 0x60) == 0;
261
262 # this flag's name is ambiguous, it turns out
263 # we must skip a length byte to get at the data
264 # when this flag is False
265 def has_explicit_length(self):
266 return (self.info_bits & (1 | 4)) != 4;
267
268 # probably a subclass of NSString. obtained this from [str pathExtension]
269 # here info_bits = 0 and Unicode data at the start of the padding word
270 # in the long run using the isa value might be safer as a way to identify this
271 # instead of reading the info_bits
272 def is_special_case(self):
273 return self.info_bits == 0;
274
275 def is_unicode(self):
276 return (self.info_bits & 0x10) == 0x10;
277
278 # preparing ourselves to read into memory
279 # by adjusting architecture-specific info
280 def adjust_for_architecture(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000281 self.is_64_bit = self.is_64bit();
Enrico Granataeb4a4792012-02-23 23:10:27 +0000282 self.is_little = self.is_little_endian();
283
284 # reading info bits out of the CFString and computing
285 # useful values to get at the real data
286 def compute_flags(self):
287 self.info_bits = self.read_info_bits();
288 if self.info_bits == None:
289 return;
290 self.mutable = self.is_mutable();
291 self.inline = self.is_inline();
292 self.explicit = self.has_explicit_length();
293 self.unicode = self.is_unicode();
294 self.special = self.is_special_case();
295
296 def update(self):
297 self.adjust_for_architecture();
298 self.compute_flags();