blob: 88ef55a1d92bc9009e43c1252806c50e3a7148d5 [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 Granata5a61fc0d2011-08-22 16:10:25 +0000120 try:
121 impl = self.valobj.GetChildMemberWithName('_M_impl')
122 node = impl.GetChildMemberWithName('_M_node')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000123 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0)
124 self.next = node.GetChildMemberWithName('_M_next')
125 self.prev = node.GetChildMemberWithName('_M_prev')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000126 self.data_type = self.extract_type()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000127 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000128 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000129 except:
130 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000131
132class StdVectorSynthProvider:
133
134 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000135 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000136 self.valobj = valobj;
Enrico Granata217f91f2011-08-17 19:07:52 +0000137
138 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000139 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000140 if self.count == None:
141 self.count = self.num_children_impl()
142 return self.count
143
144 def is_valid_pointer(ptr,process):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000145 logger = Logger.Logger()
Enrico Granata9d60f602012-03-09 03:09:58 +0000146 error = lldb.SBError()
147 process.ReadMemory(ptr,1,error)
148 return False if error.Fail() else True
149
150 def num_children_impl(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000151 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000152 try:
153 start_val = self.start.GetValueAsUnsigned(0)
154 finish_val = self.finish.GetValueAsUnsigned(0)
155 end_val = self.end.GetValueAsUnsigned(0)
156 # Before a vector has been constructed, it will contain bad values
157 # so we really need to be careful about the length we return since
158 # unitialized data can cause us to return a huge number. We need
159 # to also check for any of the start, finish or end of storage values
160 # being zero (NULL). If any are, then this vector has not been
161 # initialized yet and we should return zero
Enrico Granata217f91f2011-08-17 19:07:52 +0000162
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000163 # Make sure nothing is NULL
164 if start_val == 0 or finish_val == 0 or end_val == 0:
165 return 0
166 # Make sure start is less than finish
167 if start_val >= finish_val:
168 return 0
169 # Make sure finish is less than or equal to end of storage
170 if finish_val > end_val:
171 return 0
Enrico Granata217f91f2011-08-17 19:07:52 +0000172
Enrico Granata9d60f602012-03-09 03:09:58 +0000173 # if we have a struct (or other data type that the compiler pads to native word size)
174 # this check might fail, unless the sizeof() we get is itself incremented to take the
175 # padding bytes into account - on current clang it looks like this is the case
176 num_children = (finish_val-start_val)
177 if (num_children % self.data_size) != 0:
178 return 0
179 else:
180 num_children = num_children/self.data_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000181 return num_children
182 except:
183 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000184
185 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000186 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000187 try:
188 return int(name.lstrip('[').rstrip(']'))
189 except:
190 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000191
192 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000193 logger = Logger.Logger()
Enrico Granata22fe3172012-03-30 16:07:08 +0000194 logger >> "Retrieving child " + str(index)
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000195 if index < 0:
196 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +0000197 if index >= self.num_children():
198 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000199 try:
200 offset = index * self.data_size
201 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type)
202 except:
203 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000204
205 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000206 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000207 try:
208 impl = self.valobj.GetChildMemberWithName('_M_impl')
209 self.start = impl.GetChildMemberWithName('_M_start')
210 self.finish = impl.GetChildMemberWithName('_M_finish')
211 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
212 self.data_type = self.start.GetType().GetPointeeType()
213 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000214 # if any of these objects is invalid, it means there is no point in trying to fetch anything
215 if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid():
216 self.count = None
217 else:
218 self.count = 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000219 except:
220 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000221
222
223class StdMapSynthProvider:
224
225 def __init__(self, valobj, dict):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000226 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000227 self.valobj = valobj;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000228 logger >> "Providing synthetic children for a map named " + str(valobj.GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000229
230 # we need this function as a temporary workaround for rdar://problem/10801549
231 # which prevents us from extracting the std::pair<K,V> SBType out of the template
232 # arguments for _Rep_Type _M_t in the map itself - because we have to make up the
233 # typename and then find it, we may hit the situation were std::string has multiple
234 # names but only one is actually referenced in the debug information. hence, we need
235 # to replace the longer versions of std::string with the shorter one in order to be able
236 # to find the type name
237 def fixup_class_name(self, class_name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000238 logger = Logger.Logger()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000239 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000240 return 'std::basic_string<char>',True
Enrico Granata4cee9e52012-02-03 18:11:52 +0000241 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000242 return 'std::basic_string<char>',True
243 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
244 return 'std::basic_string<char>',True
245 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
246 return 'std::basic_string<char>',True
247 return class_name,False
Enrico Granata217f91f2011-08-17 19:07:52 +0000248
249 def update(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000250 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000251 try:
Enrico Granatad50f18b2012-03-29 19:29:45 +0000252 self.count = None
253 # 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
254 # if this gets set to True, then we will merrily return None for any child from that moment on
255 self.garbage = False
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000256 self.Mt = self.valobj.GetChildMemberWithName('_M_t')
257 self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl')
258 self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000259
Enrico Granatac5bc4122012-03-27 02:35:13 +0000260 map_type = self.valobj.GetType()
261 if map_type.IsReferenceType():
Enrico Granata22fe3172012-03-30 16:07:08 +0000262 logger >> "Dereferencing type"
Enrico Granatac5bc4122012-03-27 02:35:13 +0000263 map_type = map_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000264
Enrico Granatac5bc4122012-03-27 02:35:13 +0000265 map_arg_0 = str(map_type.GetTemplateArgumentType(0).GetName())
266 map_arg_1 = str(map_type.GetTemplateArgumentType(1).GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000267
Enrico Granata22fe3172012-03-30 16:07:08 +0000268 logger >> "map has args " + str(map_arg_0) + " and " + str(map_arg_1)
269
Enrico Granatac5bc4122012-03-27 02:35:13 +0000270 map_arg_0,fixed_0 = self.fixup_class_name(map_arg_0)
271 map_arg_1,fixed_1 = self.fixup_class_name(map_arg_1)
272
Enrico Granata22fe3172012-03-30 16:07:08 +0000273 logger >> "arg_0 has become: " + str(map_arg_0) + " (fixed: " + str(fixed_0) + ")"
274 logger >> "arg_1 has become: " + str(map_arg_1) + " (fixed: " + str(fixed_1) + ")"
275
Enrico Granatac5bc4122012-03-27 02:35:13 +0000276 # HACK: this is related to the above issue with the typename for std::string
277 # being shortened by clang - the changes to typename display and searching to honor
278 # namespaces make it so that we go looking for std::pair<const std::basic_string<char>, ...>
279 # but when we find a type for this, we then compare it against the fully-qualified
280 # std::pair<const std::basic_string<char, std::char_traits... and of course fail
281 # the way to bypass this problem is to avoid using the std:: prefix in this specific case
282 if fixed_0 or fixed_1:
283 map_arg_type = "pair<const " + map_arg_0 + ", " + map_arg_1
284 else:
285 map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1
286
Enrico Granata4cee9e52012-02-03 18:11:52 +0000287 if map_arg_1[-1] == '>':
288 map_arg_type = map_arg_type + " >"
289 else:
290 map_arg_type = map_arg_type + ">"
291
Enrico Granata22fe3172012-03-30 16:07:08 +0000292 logger >> "final contents datatype is: " + str(map_arg_type)
293
Enrico Granata4cee9e52012-02-03 18:11:52 +0000294 self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type)
295
Enrico Granata22fe3172012-03-30 16:07:08 +0000296 logger >> "and the SBType is: " + str(self.data_type)
297
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000298 # from libstdc++ implementation of _M_root for rbtree
299 self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000300 self.data_size = self.data_type.GetByteSize()
301 self.skip_size = self.Mheader.GetType().GetByteSize()
302 except:
303 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000304
Enrico Granata217f91f2011-08-17 19:07:52 +0000305 def num_children(self):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000306 global _map_capping_size
307 logger = Logger.Logger()
308 if self.count == None:
309 self.count = self.num_children_impl()
310 if self.count > _map_capping_size:
311 self.count = _map_capping_size
312 return self.count
313
314 def num_children_impl(self):
315 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000316 try:
317 root_ptr_val = self.node_ptr_value(self.Mroot)
318 if root_ptr_val == 0:
319 return 0;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000320 count = self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0)
321 logger >> "I have " + str(count) + " children available"
322 return count
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000323 except:
Enrico Granata217f91f2011-08-17 19:07:52 +0000324 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000325
326 def get_child_index(self,name):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000327 logger = Logger.Logger()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000328 try:
329 return int(name.lstrip('[').rstrip(']'))
330 except:
331 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000332
333 def get_child_at_index(self,index):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000334 logger = Logger.Logger()
335 logger >> "Being asked to fetch child[" + str(index) + "]"
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000336 if index < 0:
337 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000338 if index >= self.num_children():
339 return None;
Enrico Granatad50f18b2012-03-29 19:29:45 +0000340 if self.garbage:
341 logger >> "Returning None since we are a garbage tree"
342 return None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000343 try:
344 offset = index
345 current = self.left(self.Mheader);
346 while offset > 0:
347 current = self.increment_node(current)
348 offset = offset - 1;
349 # skip all the base stuff and get at the data
350 return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type)
351 except:
352 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000353
354 # utility functions
355 def node_ptr_value(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000356 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000357 return node.GetValueAsUnsigned(0)
358
359 def right(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000360 logger = Logger.Logger()
Enrico Granata217f91f2011-08-17 19:07:52 +0000361 return node.GetChildMemberWithName("_M_right");
362
363 def left(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_left");
366
367 def parent(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_parent");
370
371 # from libstdc++ implementation of iterator for rbtree
372 def increment_node(self,node):
Enrico Granatad50f18b2012-03-29 19:29:45 +0000373 logger = Logger.Logger()
374 max_steps = self.num_children()
Enrico Granata217f91f2011-08-17 19:07:52 +0000375 if self.node_ptr_value(self.right(node)) != 0:
376 x = self.right(node);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000377 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000378 while self.node_ptr_value(self.left(x)) != 0:
379 x = self.left(x);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000380 max_steps -= 1
381 logger >> str(max_steps) + " more to go before giving up"
382 if max_steps <= 0:
383 self.garbage = True
384 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000385 return x;
386 else:
387 x = node;
388 y = self.parent(x)
Enrico Granatad50f18b2012-03-29 19:29:45 +0000389 max_steps -= 1
Enrico Granata217f91f2011-08-17 19:07:52 +0000390 while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))):
391 x = y;
392 y = self.parent(y);
Enrico Granatad50f18b2012-03-29 19:29:45 +0000393 max_steps -= 1
394 logger >> str(max_steps) + " more to go before giving up"
395 if max_steps <= 0:
396 self.garbage = True
397 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000398 if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y):
399 x = y;
400 return x;
401
Enrico Granatad50f18b2012-03-29 19:29:45 +0000402
403_map_capping_size = 255
404_list_capping_size = 255
405_list_uses_loop_detector = True