blob: 07e25c321a9f35366cbf17ffaad6cc95357c971f [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 Granata5a61fc0d2011-08-22 16:10:25 +000092 if index < 0:
93 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +000094 if index >= self.num_children():
95 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000096 try:
97 offset = index
98 current = self.next
99 while offset > 0:
100 current = current.GetChildMemberWithName('_M_next')
101 offset = offset - 1
102 return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type)
103 except:
Enrico Granata23e578c2011-08-22 16:38:44 +0000104 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000105
Enrico Granata4cee9e52012-02-03 18:11:52 +0000106 def extract_type(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000107 logger = Logger.Logger()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000108 list_type = self.valobj.GetType().GetUnqualifiedType()
Enrico Granatac5bc4122012-03-27 02:35:13 +0000109 if list_type.IsReferenceType():
110 list_type = list_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000111 if list_type.GetNumberOfTemplateArguments() > 0:
112 data_type = list_type.GetTemplateArgumentType(0)
113 else:
114 data_type = None
115 return data_type
Enrico Granata217f91f2011-08-17 19:07:52 +0000116
117 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000118 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000119 try:
120 impl = self.valobj.GetChildMemberWithName('_M_impl')
121 node = impl.GetChildMemberWithName('_M_node')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000122 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0)
123 self.next = node.GetChildMemberWithName('_M_next')
124 self.prev = node.GetChildMemberWithName('_M_prev')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000125 self.data_type = self.extract_type()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000126 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000127 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000128 except:
129 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000130
131class StdVectorSynthProvider:
132
133 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000134 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000135 self.valobj = valobj;
Enrico Granata217f91f2011-08-17 19:07:52 +0000136
137 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000138 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000139 if self.count == None:
140 self.count = self.num_children_impl()
141 return self.count
142
143 def is_valid_pointer(ptr,process):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000144 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000145 error = lldb.SBError()
146 process.ReadMemory(ptr,1,error)
147 return False if error.Fail() else True
148
149 def num_children_impl(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000150 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000151 try:
152 start_val = self.start.GetValueAsUnsigned(0)
153 finish_val = self.finish.GetValueAsUnsigned(0)
154 end_val = self.end.GetValueAsUnsigned(0)
155 # Before a vector has been constructed, it will contain bad values
156 # so we really need to be careful about the length we return since
157 # unitialized data can cause us to return a huge number. We need
158 # to also check for any of the start, finish or end of storage values
159 # being zero (NULL). If any are, then this vector has not been
160 # initialized yet and we should return zero
Enrico Granata217f91f2011-08-17 19:07:52 +0000161
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000162 # Make sure nothing is NULL
163 if start_val == 0 or finish_val == 0 or end_val == 0:
164 return 0
165 # Make sure start is less than finish
166 if start_val >= finish_val:
167 return 0
168 # Make sure finish is less than or equal to end of storage
169 if finish_val > end_val:
170 return 0
Enrico Granata217f91f2011-08-17 19:07:52 +0000171
Enrico Granata9d60f602012-03-09 03:09:58 +0000172 # if we have a struct (or other data type that the compiler pads to native word size)
173 # this check might fail, unless the sizeof() we get is itself incremented to take the
174 # padding bytes into account - on current clang it looks like this is the case
175 num_children = (finish_val-start_val)
176 if (num_children % self.data_size) != 0:
177 return 0
178 else:
179 num_children = num_children/self.data_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000180 return num_children
181 except:
182 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000183
184 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000185 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000186 try:
187 return int(name.lstrip('[').rstrip(']'))
188 except:
189 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000190
191 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000192 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000193 if index < 0:
194 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +0000195 if index >= self.num_children():
196 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000197 try:
198 offset = index * self.data_size
199 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type)
200 except:
201 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000202
203 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000204 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000205 try:
206 impl = self.valobj.GetChildMemberWithName('_M_impl')
207 self.start = impl.GetChildMemberWithName('_M_start')
208 self.finish = impl.GetChildMemberWithName('_M_finish')
209 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
210 self.data_type = self.start.GetType().GetPointeeType()
211 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000212 # if any of these objects is invalid, it means there is no point in trying to fetch anything
213 if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid():
214 self.count = None
215 else:
216 self.count = 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000217 except:
218 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000219
220
221class StdMapSynthProvider:
222
223 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000224 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000225 self.valobj = valobj;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000226 logger >> "Providing synthetic children for a map named " + str(valobj.GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000227
228 # we need this function as a temporary workaround for rdar://problem/10801549
229 # which prevents us from extracting the std::pair<K,V> SBType out of the template
230 # arguments for _Rep_Type _M_t in the map itself - because we have to make up the
231 # typename and then find it, we may hit the situation were std::string has multiple
232 # names but only one is actually referenced in the debug information. hence, we need
233 # to replace the longer versions of std::string with the shorter one in order to be able
234 # to find the type name
235 def fixup_class_name(self, class_name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000236 logger = Logger.Logger()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000237 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000238 return 'std::basic_string<char>',True
Enrico Granata4cee9e52012-02-03 18:11:52 +0000239 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000240 return 'std::basic_string<char>',True
241 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
242 return 'std::basic_string<char>',True
243 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
244 return 'std::basic_string<char>',True
245 return class_name,False
Enrico Granata217f91f2011-08-17 19:07:52 +0000246
247 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000248 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000249 try:
Enrico Granatad50f18b2012-03-29 19:29:45 +0000250 self.count = None
251 # 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
252 # if this gets set to True, then we will merrily return None for any child from that moment on
253 self.garbage = False
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000254 self.Mt = self.valobj.GetChildMemberWithName('_M_t')
255 self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl')
256 self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000257
Enrico Granatac5bc4122012-03-27 02:35:13 +0000258 map_type = self.valobj.GetType()
259 if map_type.IsReferenceType():
260 map_type = map_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000261
Enrico Granatac5bc4122012-03-27 02:35:13 +0000262 map_arg_0 = str(map_type.GetTemplateArgumentType(0).GetName())
263 map_arg_1 = str(map_type.GetTemplateArgumentType(1).GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000264
Enrico Granatac5bc4122012-03-27 02:35:13 +0000265 map_arg_0,fixed_0 = self.fixup_class_name(map_arg_0)
266 map_arg_1,fixed_1 = self.fixup_class_name(map_arg_1)
267
268 # HACK: this is related to the above issue with the typename for std::string
269 # being shortened by clang - the changes to typename display and searching to honor
270 # namespaces make it so that we go looking for std::pair<const std::basic_string<char>, ...>
271 # but when we find a type for this, we then compare it against the fully-qualified
272 # std::pair<const std::basic_string<char, std::char_traits... and of course fail
273 # the way to bypass this problem is to avoid using the std:: prefix in this specific case
274 if fixed_0 or fixed_1:
275 map_arg_type = "pair<const " + map_arg_0 + ", " + map_arg_1
276 else:
277 map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1
278
Enrico Granata4cee9e52012-02-03 18:11:52 +0000279 if map_arg_1[-1] == '>':
280 map_arg_type = map_arg_type + " >"
281 else:
282 map_arg_type = map_arg_type + ">"
283
284 self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type)
285
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000286 # from libstdc++ implementation of _M_root for rbtree
287 self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000288 self.data_size = self.data_type.GetByteSize()
289 self.skip_size = self.Mheader.GetType().GetByteSize()
290 except:
291 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000292
Enrico Granata217f91f2011-08-17 19:07:52 +0000293 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000294 global _map_capping_size
295 logger = Logger.Logger()
296 if self.count == None:
297 self.count = self.num_children_impl()
298 if self.count > _map_capping_size:
299 self.count = _map_capping_size
300 return self.count
301
302 def num_children_impl(self):
303 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000304 try:
305 root_ptr_val = self.node_ptr_value(self.Mroot)
306 if root_ptr_val == 0:
307 return 0;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000308 count = self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0)
309 logger >> "I have " + str(count) + " children available"
310 return count
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000311 except:
Enrico Granata217f91f2011-08-17 19:07:52 +0000312 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000313
314 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000315 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000316 try:
317 return int(name.lstrip('[').rstrip(']'))
318 except:
319 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000320
321 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000322 logger = Logger.Logger()
323 logger >> "Being asked to fetch child[" + str(index) + "]"
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000324 if index < 0:
325 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000326 if index >= self.num_children():
327 return None;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000328 if self.garbage:
329 logger >> "Returning None since we are a garbage tree"
330 return None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000331 try:
332 offset = index
333 current = self.left(self.Mheader);
334 while offset > 0:
335 current = self.increment_node(current)
336 offset = offset - 1;
337 # skip all the base stuff and get at the data
338 return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type)
339 except:
340 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000341
342 # utility functions
343 def node_ptr_value(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000344 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000345 return node.GetValueAsUnsigned(0)
346
347 def right(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000348 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000349 return node.GetChildMemberWithName("_M_right");
350
351 def left(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000352 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000353 return node.GetChildMemberWithName("_M_left");
354
355 def parent(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000356 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000357 return node.GetChildMemberWithName("_M_parent");
358
359 # from libstdc++ implementation of iterator for rbtree
360 def increment_node(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000361 logger = Logger.Logger()
362 max_steps = self.num_children()
Enrico Granata217f91f2011-08-17 19:07:52 +0000363 if self.node_ptr_value(self.right(node)) != 0:
364 x = self.right(node);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000365 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000366 while self.node_ptr_value(self.left(x)) != 0:
367 x = self.left(x);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000368 max_steps -= 1
369 logger >> str(max_steps) + " more to go before giving up"
370 if max_steps <= 0:
371 self.garbage = True
372 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000373 return x;
374 else:
375 x = node;
376 y = self.parent(x)
Enrico Granatad50f18b2012-03-29 19:29:45 +0000377 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000378 while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))):
379 x = y;
380 y = self.parent(y);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000381 max_steps -= 1
382 logger >> str(max_steps) + " more to go before giving up"
383 if max_steps <= 0:
384 self.garbage = True
385 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000386 if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y):
387 x = y;
388 return x;
389
Enrico Granatad50f18b2012-03-29 19:29:45 +0000390
391_map_capping_size = 255
392_list_capping_size = 255
393_list_uses_loop_detector = True