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