blob: 009e39084a74623cc55eae24ec74f14ad8ca175f [file] [log] [blame]
Enrico Granata217f91f2011-08-17 19:07:52 +00001import re
Enrico Granatad50f18b2012-03-29 19:29:45 +00002import Logger
Enrico Granata217f91f2011-08-17 19:07:52 +00003
4# C++ STL formatters for LLDB
Enrico Granata5a61fc0d2011-08-22 16:10:25 +00005# These formatters are based upon the version of the GNU libstdc++
Enrico Granata5a1bff02012-02-08 19:57:18 +00006# as it ships with Mac OS X 10.6.8 thru 10.7.3
Enrico Granata5a61fc0d2011-08-22 16:10:25 +00007# You are encouraged to look at the STL implementation for your platform
8# before relying on these formatters to do the right thing for your setup
Enrico Granata217f91f2011-08-17 19:07:52 +00009
10class StdListSynthProvider:
11
12 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +000013 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +000014 self.valobj = valobj
Enrico Granata217f91f2011-08-17 19:07:52 +000015
Enrico Granata9d60f602012-03-09 03:09:58 +000016 def next_node(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +000017 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +000018 return node.GetChildMemberWithName('_M_next')
19
20 def is_valid(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +000021 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +000022 return self.value(self.next_node(node)) != self.node_address
23
24 def value(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +000025 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +000026 return node.GetValueAsUnsigned()
27
28 # Floyd's cyle-finding algorithm
29 # try to detect if this list has a loop
30 def has_loop(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +000031 global _list_uses_loop_detector
32 logger = Logger.Logger()
33 if _list_uses_loop_detector == False:
34 logger >> "Asked not to use loop detection"
35 return False
Enrico Granata9d60f602012-03-09 03:09:58 +000036 slow = self.next
37 fast1 = self.next
38 fast2 = self.next
39 while self.is_valid(slow):
40 slow_value = self.value(slow)
41 fast1 = self.next_node(fast2)
42 fast2 = self.next_node(fast1)
43 if self.value(fast1) == slow_value or self.value(fast2) == slow_value:
44 return True
45 slow = self.next_node(slow)
46 return False
47
Enrico Granata217f91f2011-08-17 19:07:52 +000048 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +000049 global _list_capping_size
50 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +000051 if self.count == None:
52 self.count = self.num_children_impl()
Enrico Granatad50f18b2012-03-29 19:29:45 +000053 if self.count > _list_capping_size:
54 self.count = _list_capping_size
Enrico Granata9d60f602012-03-09 03:09:58 +000055 return self.count
56
57 def num_children_impl(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +000058 logger = Logger.Logger()
59 global _list_capping_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000060 try:
61 next_val = self.next.GetValueAsUnsigned(0)
62 prev_val = self.prev.GetValueAsUnsigned(0)
63 # After a std::list has been initialized, both next and prev will be non-NULL
64 if next_val == 0 or prev_val == 0:
65 return 0
66 if next_val == self.node_address:
67 return 0
68 if next_val == prev_val:
69 return 1
Enrico Granata9d60f602012-03-09 03:09:58 +000070 if self.has_loop():
71 return 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000072 size = 2
73 current = self.next
74 while current.GetChildMemberWithName('_M_next').GetValueAsUnsigned(0) != self.node_address:
75 size = size + 1
76 current = current.GetChildMemberWithName('_M_next')
Enrico Granatad50f18b2012-03-29 19:29:45 +000077 if size > _list_capping_size:
78 return _list_capping_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000079 return (size - 1)
80 except:
81 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +000082
83 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +000084 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000085 try:
86 return int(name.lstrip('[').rstrip(']'))
87 except:
88 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +000089
90 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +000091 logger = Logger.Logger()
Enrico Granata22fe3172012-03-30 16:07:08 +000092 logger >> "Fetching child " + str(index)
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000093 if index < 0:
94 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +000095 if index >= self.num_children():
96 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000097 try:
98 offset = index
99 current = self.next
100 while offset > 0:
101 current = current.GetChildMemberWithName('_M_next')
102 offset = offset - 1
103 return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type)
104 except:
Enrico Granata23e578c2011-08-22 16:38:44 +0000105 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000106
Enrico Granata4cee9e52012-02-03 18:11:52 +0000107 def extract_type(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000108 logger = Logger.Logger()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000109 list_type = self.valobj.GetType().GetUnqualifiedType()
Enrico Granatac5bc4122012-03-27 02:35:13 +0000110 if list_type.IsReferenceType():
111 list_type = list_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000112 if list_type.GetNumberOfTemplateArguments() > 0:
113 data_type = list_type.GetTemplateArgumentType(0)
114 else:
115 data_type = None
116 return data_type
Enrico Granata217f91f2011-08-17 19:07:52 +0000117
118 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000119 logger = Logger.Logger()
Enrico Granata79c1baf2012-03-31 00:15:24 +0000120 # preemptively setting this to None - we might end up changing our mind later
121 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000122 try:
123 impl = self.valobj.GetChildMemberWithName('_M_impl')
124 node = impl.GetChildMemberWithName('_M_node')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000125 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0)
126 self.next = node.GetChildMemberWithName('_M_next')
127 self.prev = node.GetChildMemberWithName('_M_prev')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000128 self.data_type = self.extract_type()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000129 self.data_size = self.data_type.GetByteSize()
130 except:
131 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000132
133class StdVectorSynthProvider:
134
135 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000136 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000137 self.valobj = valobj;
Enrico Granata217f91f2011-08-17 19:07:52 +0000138
139 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000140 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000141 if self.count == None:
142 self.count = self.num_children_impl()
143 return self.count
144
145 def is_valid_pointer(ptr,process):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000146 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000147 error = lldb.SBError()
148 process.ReadMemory(ptr,1,error)
149 return False if error.Fail() else True
150
151 def num_children_impl(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000152 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000153 try:
154 start_val = self.start.GetValueAsUnsigned(0)
155 finish_val = self.finish.GetValueAsUnsigned(0)
156 end_val = self.end.GetValueAsUnsigned(0)
157 # Before a vector has been constructed, it will contain bad values
158 # so we really need to be careful about the length we return since
159 # unitialized data can cause us to return a huge number. We need
160 # to also check for any of the start, finish or end of storage values
161 # being zero (NULL). If any are, then this vector has not been
162 # initialized yet and we should return zero
Enrico Granata217f91f2011-08-17 19:07:52 +0000163
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000164 # Make sure nothing is NULL
165 if start_val == 0 or finish_val == 0 or end_val == 0:
166 return 0
167 # Make sure start is less than finish
168 if start_val >= finish_val:
169 return 0
170 # Make sure finish is less than or equal to end of storage
171 if finish_val > end_val:
172 return 0
Enrico Granata217f91f2011-08-17 19:07:52 +0000173
Enrico Granata9d60f602012-03-09 03:09:58 +0000174 # if we have a struct (or other data type that the compiler pads to native word size)
175 # this check might fail, unless the sizeof() we get is itself incremented to take the
176 # padding bytes into account - on current clang it looks like this is the case
177 num_children = (finish_val-start_val)
178 if (num_children % self.data_size) != 0:
179 return 0
180 else:
181 num_children = num_children/self.data_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000182 return num_children
183 except:
184 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000185
186 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000187 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000188 try:
189 return int(name.lstrip('[').rstrip(']'))
190 except:
191 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000192
193 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000194 logger = Logger.Logger()
Enrico Granata22fe3172012-03-30 16:07:08 +0000195 logger >> "Retrieving child " + str(index)
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000196 if index < 0:
197 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +0000198 if index >= self.num_children():
199 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000200 try:
201 offset = index * self.data_size
202 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type)
203 except:
204 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000205
206 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000207 logger = Logger.Logger()
Enrico Granata79c1baf2012-03-31 00:15:24 +0000208 # preemptively setting this to None - we might end up changing our mind later
209 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000210 try:
211 impl = self.valobj.GetChildMemberWithName('_M_impl')
212 self.start = impl.GetChildMemberWithName('_M_start')
213 self.finish = impl.GetChildMemberWithName('_M_finish')
214 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
215 self.data_type = self.start.GetType().GetPointeeType()
216 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000217 # if any of these objects is invalid, it means there is no point in trying to fetch anything
218 if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid():
219 self.count = None
220 else:
221 self.count = 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000222 except:
223 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000224
225
226class StdMapSynthProvider:
227
228 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000229 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000230 self.valobj = valobj;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000231 logger >> "Providing synthetic children for a map named " + str(valobj.GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000232
233 # we need this function as a temporary workaround for rdar://problem/10801549
234 # which prevents us from extracting the std::pair<K,V> SBType out of the template
235 # arguments for _Rep_Type _M_t in the map itself - because we have to make up the
236 # typename and then find it, we may hit the situation were std::string has multiple
237 # names but only one is actually referenced in the debug information. hence, we need
238 # to replace the longer versions of std::string with the shorter one in order to be able
239 # to find the type name
240 def fixup_class_name(self, class_name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000241 logger = Logger.Logger()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000242 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000243 return 'std::basic_string<char>',True
Enrico Granata4cee9e52012-02-03 18:11:52 +0000244 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000245 return 'std::basic_string<char>',True
246 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
247 return 'std::basic_string<char>',True
248 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
249 return 'std::basic_string<char>',True
250 return class_name,False
Enrico Granata217f91f2011-08-17 19:07:52 +0000251
252 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000253 logger = Logger.Logger()
Enrico Granata79c1baf2012-03-31 00:15:24 +0000254 # preemptively setting this to None - we might end up changing our mind later
255 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000256 try:
Enrico Granatad50f18b2012-03-29 19:29:45 +0000257 # we will set this to True if we find out that discovering a node in the map takes more steps than the overall size of the RB tree
258 # if this gets set to True, then we will merrily return None for any child from that moment on
259 self.garbage = False
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000260 self.Mt = self.valobj.GetChildMemberWithName('_M_t')
261 self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl')
262 self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000263
Enrico Granatac5bc4122012-03-27 02:35:13 +0000264 map_type = self.valobj.GetType()
265 if map_type.IsReferenceType():
Enrico Granata22fe3172012-03-30 16:07:08 +0000266 logger >> "Dereferencing type"
Enrico Granatac5bc4122012-03-27 02:35:13 +0000267 map_type = map_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000268
Enrico Granatac5bc4122012-03-27 02:35:13 +0000269 map_arg_0 = str(map_type.GetTemplateArgumentType(0).GetName())
270 map_arg_1 = str(map_type.GetTemplateArgumentType(1).GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000271
Enrico Granata22fe3172012-03-30 16:07:08 +0000272 logger >> "map has args " + str(map_arg_0) + " and " + str(map_arg_1)
273
Enrico Granatac5bc4122012-03-27 02:35:13 +0000274 map_arg_0,fixed_0 = self.fixup_class_name(map_arg_0)
275 map_arg_1,fixed_1 = self.fixup_class_name(map_arg_1)
276
Enrico Granata22fe3172012-03-30 16:07:08 +0000277 logger >> "arg_0 has become: " + str(map_arg_0) + " (fixed: " + str(fixed_0) + ")"
278 logger >> "arg_1 has become: " + str(map_arg_1) + " (fixed: " + str(fixed_1) + ")"
279
Enrico Granatac5bc4122012-03-27 02:35:13 +0000280 # HACK: this is related to the above issue with the typename for std::string
281 # being shortened by clang - the changes to typename display and searching to honor
282 # namespaces make it so that we go looking for std::pair<const std::basic_string<char>, ...>
283 # but when we find a type for this, we then compare it against the fully-qualified
284 # std::pair<const std::basic_string<char, std::char_traits... and of course fail
285 # the way to bypass this problem is to avoid using the std:: prefix in this specific case
286 if fixed_0 or fixed_1:
287 map_arg_type = "pair<const " + map_arg_0 + ", " + map_arg_1
288 else:
289 map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1
290
Enrico Granata4cee9e52012-02-03 18:11:52 +0000291 if map_arg_1[-1] == '>':
292 map_arg_type = map_arg_type + " >"
293 else:
294 map_arg_type = map_arg_type + ">"
295
Enrico Granata22fe3172012-03-30 16:07:08 +0000296 logger >> "final contents datatype is: " + str(map_arg_type)
297
Enrico Granata4cee9e52012-02-03 18:11:52 +0000298 self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type)
299
Enrico Granata22fe3172012-03-30 16:07:08 +0000300 logger >> "and the SBType is: " + str(self.data_type)
301
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000302 # from libstdc++ implementation of _M_root for rbtree
303 self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000304 self.data_size = self.data_type.GetByteSize()
305 self.skip_size = self.Mheader.GetType().GetByteSize()
306 except:
307 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000308
Enrico Granata217f91f2011-08-17 19:07:52 +0000309 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000310 global _map_capping_size
311 logger = Logger.Logger()
312 if self.count == None:
313 self.count = self.num_children_impl()
314 if self.count > _map_capping_size:
315 self.count = _map_capping_size
316 return self.count
317
318 def num_children_impl(self):
319 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000320 try:
321 root_ptr_val = self.node_ptr_value(self.Mroot)
322 if root_ptr_val == 0:
323 return 0;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000324 count = self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0)
325 logger >> "I have " + str(count) + " children available"
326 return count
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000327 except:
Enrico Granata217f91f2011-08-17 19:07:52 +0000328 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000329
330 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000331 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000332 try:
333 return int(name.lstrip('[').rstrip(']'))
334 except:
335 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000336
337 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000338 logger = Logger.Logger()
339 logger >> "Being asked to fetch child[" + str(index) + "]"
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000340 if index < 0:
341 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000342 if index >= self.num_children():
343 return None;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000344 if self.garbage:
345 logger >> "Returning None since we are a garbage tree"
346 return None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000347 try:
348 offset = index
349 current = self.left(self.Mheader);
350 while offset > 0:
351 current = self.increment_node(current)
352 offset = offset - 1;
353 # skip all the base stuff and get at the data
354 return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type)
355 except:
356 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000357
358 # utility functions
359 def node_ptr_value(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000360 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000361 return node.GetValueAsUnsigned(0)
362
363 def right(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000364 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000365 return node.GetChildMemberWithName("_M_right");
366
367 def left(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000368 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000369 return node.GetChildMemberWithName("_M_left");
370
371 def parent(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000372 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000373 return node.GetChildMemberWithName("_M_parent");
374
375 # from libstdc++ implementation of iterator for rbtree
376 def increment_node(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000377 logger = Logger.Logger()
378 max_steps = self.num_children()
Enrico Granata217f91f2011-08-17 19:07:52 +0000379 if self.node_ptr_value(self.right(node)) != 0:
380 x = self.right(node);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000381 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000382 while self.node_ptr_value(self.left(x)) != 0:
383 x = self.left(x);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000384 max_steps -= 1
385 logger >> str(max_steps) + " more to go before giving up"
386 if max_steps <= 0:
387 self.garbage = True
388 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000389 return x;
390 else:
391 x = node;
392 y = self.parent(x)
Enrico Granatad50f18b2012-03-29 19:29:45 +0000393 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000394 while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))):
395 x = y;
396 y = self.parent(y);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000397 max_steps -= 1
398 logger >> str(max_steps) + " more to go before giving up"
399 if max_steps <= 0:
400 self.garbage = True
401 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000402 if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y):
403 x = y;
404 return x;
405
Enrico Granatad50f18b2012-03-29 19:29:45 +0000406
407_map_capping_size = 255
408_list_capping_size = 255
409_list_uses_loop_detector = True