blob: fb970e8bc43efd8d14fe049cfc388f7faa72aa3a [file] [log] [blame]
Enrico Granata217f91f2011-08-17 19:07:52 +00001import re
2
3# C++ STL formatters for LLDB
Enrico Granata5a61fc0d2011-08-22 16:10:25 +00004# These formatters are based upon the version of the GNU libstdc++
Enrico Granata5a1bff02012-02-08 19:57:18 +00005# as it ships with Mac OS X 10.6.8 thru 10.7.3
Enrico Granata5a61fc0d2011-08-22 16:10:25 +00006# You are encouraged to look at the STL implementation for your platform
7# before relying on these formatters to do the right thing for your setup
Enrico Granata217f91f2011-08-17 19:07:52 +00008
9class StdListSynthProvider:
10
11 def __init__(self, valobj, dict):
12 self.valobj = valobj
Enrico Granata217f91f2011-08-17 19:07:52 +000013
Enrico Granata9d60f602012-03-09 03:09:58 +000014 def next_node(self,node):
15 return node.GetChildMemberWithName('_M_next')
16
17 def is_valid(self,node):
18 return self.value(self.next_node(node)) != self.node_address
19
20 def value(self,node):
21 return node.GetValueAsUnsigned()
22
23 # Floyd's cyle-finding algorithm
24 # try to detect if this list has a loop
25 def has_loop(self):
26 slow = self.next
27 fast1 = self.next
28 fast2 = self.next
29 while self.is_valid(slow):
30 slow_value = self.value(slow)
31 fast1 = self.next_node(fast2)
32 fast2 = self.next_node(fast1)
33 if self.value(fast1) == slow_value or self.value(fast2) == slow_value:
34 return True
35 slow = self.next_node(slow)
36 return False
37
Enrico Granata217f91f2011-08-17 19:07:52 +000038 def num_children(self):
Enrico Granata9d60f602012-03-09 03:09:58 +000039 if self.count == None:
40 self.count = self.num_children_impl()
41 return self.count
42
43 def num_children_impl(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000044 try:
45 next_val = self.next.GetValueAsUnsigned(0)
46 prev_val = self.prev.GetValueAsUnsigned(0)
47 # After a std::list has been initialized, both next and prev will be non-NULL
48 if next_val == 0 or prev_val == 0:
49 return 0
50 if next_val == self.node_address:
51 return 0
52 if next_val == prev_val:
53 return 1
Enrico Granata9d60f602012-03-09 03:09:58 +000054 if self.has_loop():
55 return 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000056 size = 2
57 current = self.next
58 while current.GetChildMemberWithName('_M_next').GetValueAsUnsigned(0) != self.node_address:
59 size = size + 1
60 current = current.GetChildMemberWithName('_M_next')
61 return (size - 1)
62 except:
63 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +000064
65 def get_child_index(self,name):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000066 try:
67 return int(name.lstrip('[').rstrip(']'))
68 except:
69 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +000070
71 def get_child_at_index(self,index):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000072 if index < 0:
73 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +000074 if index >= self.num_children():
75 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000076 try:
77 offset = index
78 current = self.next
79 while offset > 0:
80 current = current.GetChildMemberWithName('_M_next')
81 offset = offset - 1
82 return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type)
83 except:
Enrico Granata23e578c2011-08-22 16:38:44 +000084 return None
Enrico Granata217f91f2011-08-17 19:07:52 +000085
Enrico Granata4cee9e52012-02-03 18:11:52 +000086 def extract_type(self):
87 list_type = self.valobj.GetType().GetUnqualifiedType()
Enrico Granatac5bc4122012-03-27 02:35:13 +000088 if list_type.IsReferenceType():
89 list_type = list_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +000090 if list_type.GetNumberOfTemplateArguments() > 0:
91 data_type = list_type.GetTemplateArgumentType(0)
92 else:
93 data_type = None
94 return data_type
Enrico Granata217f91f2011-08-17 19:07:52 +000095
96 def update(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +000097 try:
98 impl = self.valobj.GetChildMemberWithName('_M_impl')
99 node = impl.GetChildMemberWithName('_M_node')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000100 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0)
101 self.next = node.GetChildMemberWithName('_M_next')
102 self.prev = node.GetChildMemberWithName('_M_prev')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000103 self.data_type = self.extract_type()
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000104 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000105 self.count = None
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000106 except:
107 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000108
109class StdVectorSynthProvider:
110
111 def __init__(self, valobj, dict):
112 self.valobj = valobj;
Enrico Granata217f91f2011-08-17 19:07:52 +0000113
114 def num_children(self):
Enrico Granata9d60f602012-03-09 03:09:58 +0000115 if self.count == None:
116 self.count = self.num_children_impl()
117 return self.count
118
119 def is_valid_pointer(ptr,process):
120 error = lldb.SBError()
121 process.ReadMemory(ptr,1,error)
122 return False if error.Fail() else True
123
124 def num_children_impl(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000125 try:
126 start_val = self.start.GetValueAsUnsigned(0)
127 finish_val = self.finish.GetValueAsUnsigned(0)
128 end_val = self.end.GetValueAsUnsigned(0)
129 # Before a vector has been constructed, it will contain bad values
130 # so we really need to be careful about the length we return since
131 # unitialized data can cause us to return a huge number. We need
132 # to also check for any of the start, finish or end of storage values
133 # being zero (NULL). If any are, then this vector has not been
134 # initialized yet and we should return zero
Enrico Granata217f91f2011-08-17 19:07:52 +0000135
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000136 # Make sure nothing is NULL
137 if start_val == 0 or finish_val == 0 or end_val == 0:
138 return 0
139 # Make sure start is less than finish
140 if start_val >= finish_val:
141 return 0
142 # Make sure finish is less than or equal to end of storage
143 if finish_val > end_val:
144 return 0
Enrico Granata217f91f2011-08-17 19:07:52 +0000145
Enrico Granata9d60f602012-03-09 03:09:58 +0000146 # if we have a struct (or other data type that the compiler pads to native word size)
147 # this check might fail, unless the sizeof() we get is itself incremented to take the
148 # padding bytes into account - on current clang it looks like this is the case
149 num_children = (finish_val-start_val)
150 if (num_children % self.data_size) != 0:
151 return 0
152 else:
153 num_children = num_children/self.data_size
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000154 return num_children
155 except:
156 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000157
158 def get_child_index(self,name):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000159 try:
160 return int(name.lstrip('[').rstrip(']'))
161 except:
162 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000163
164 def get_child_at_index(self,index):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000165 if index < 0:
166 return None;
Enrico Granata217f91f2011-08-17 19:07:52 +0000167 if index >= self.num_children():
168 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000169 try:
170 offset = index * self.data_size
171 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type)
172 except:
173 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000174
175 def update(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000176 try:
177 impl = self.valobj.GetChildMemberWithName('_M_impl')
178 self.start = impl.GetChildMemberWithName('_M_start')
179 self.finish = impl.GetChildMemberWithName('_M_finish')
180 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
181 self.data_type = self.start.GetType().GetPointeeType()
182 self.data_size = self.data_type.GetByteSize()
Enrico Granata9d60f602012-03-09 03:09:58 +0000183 # if any of these objects is invalid, it means there is no point in trying to fetch anything
184 if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid():
185 self.count = None
186 else:
187 self.count = 0
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000188 except:
189 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000190
191
192class StdMapSynthProvider:
193
194 def __init__(self, valobj, dict):
195 self.valobj = valobj;
Enrico Granata4cee9e52012-02-03 18:11:52 +0000196
197 # we need this function as a temporary workaround for rdar://problem/10801549
198 # which prevents us from extracting the std::pair<K,V> SBType out of the template
199 # arguments for _Rep_Type _M_t in the map itself - because we have to make up the
200 # typename and then find it, we may hit the situation were std::string has multiple
201 # names but only one is actually referenced in the debug information. hence, we need
202 # to replace the longer versions of std::string with the shorter one in order to be able
203 # to find the type name
204 def fixup_class_name(self, class_name):
Enrico Granata4cee9e52012-02-03 18:11:52 +0000205 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000206 return 'std::basic_string<char>',True
Enrico Granata4cee9e52012-02-03 18:11:52 +0000207 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
Enrico Granatac5bc4122012-03-27 02:35:13 +0000208 return 'std::basic_string<char>',True
209 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
210 return 'std::basic_string<char>',True
211 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
212 return 'std::basic_string<char>',True
213 return class_name,False
Enrico Granata217f91f2011-08-17 19:07:52 +0000214
215 def update(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000216 try:
217 self.Mt = self.valobj.GetChildMemberWithName('_M_t')
218 self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl')
219 self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header')
Enrico Granata4cee9e52012-02-03 18:11:52 +0000220
Enrico Granatac5bc4122012-03-27 02:35:13 +0000221 map_type = self.valobj.GetType()
222 if map_type.IsReferenceType():
223 map_type = map_type.GetDereferencedType()
Enrico Granata4cee9e52012-02-03 18:11:52 +0000224
Enrico Granatac5bc4122012-03-27 02:35:13 +0000225 map_arg_0 = str(map_type.GetTemplateArgumentType(0).GetName())
226 map_arg_1 = str(map_type.GetTemplateArgumentType(1).GetName())
Enrico Granata4cee9e52012-02-03 18:11:52 +0000227
Enrico Granatac5bc4122012-03-27 02:35:13 +0000228 map_arg_0,fixed_0 = self.fixup_class_name(map_arg_0)
229 map_arg_1,fixed_1 = self.fixup_class_name(map_arg_1)
230
231 # HACK: this is related to the above issue with the typename for std::string
232 # being shortened by clang - the changes to typename display and searching to honor
233 # namespaces make it so that we go looking for std::pair<const std::basic_string<char>, ...>
234 # but when we find a type for this, we then compare it against the fully-qualified
235 # std::pair<const std::basic_string<char, std::char_traits... and of course fail
236 # the way to bypass this problem is to avoid using the std:: prefix in this specific case
237 if fixed_0 or fixed_1:
238 map_arg_type = "pair<const " + map_arg_0 + ", " + map_arg_1
239 else:
240 map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1
241
Enrico Granata4cee9e52012-02-03 18:11:52 +0000242 if map_arg_1[-1] == '>':
243 map_arg_type = map_arg_type + " >"
244 else:
245 map_arg_type = map_arg_type + ">"
246
247 self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type)
248
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000249 # from libstdc++ implementation of _M_root for rbtree
250 self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent')
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000251 self.data_size = self.data_type.GetByteSize()
252 self.skip_size = self.Mheader.GetType().GetByteSize()
253 except:
254 pass
Enrico Granata217f91f2011-08-17 19:07:52 +0000255
Enrico Granata217f91f2011-08-17 19:07:52 +0000256 def num_children(self):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000257 try:
258 root_ptr_val = self.node_ptr_value(self.Mroot)
259 if root_ptr_val == 0:
260 return 0;
261 return self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0)
262 except:
Enrico Granata217f91f2011-08-17 19:07:52 +0000263 return 0;
Enrico Granata217f91f2011-08-17 19:07:52 +0000264
265 def get_child_index(self,name):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000266 try:
267 return int(name.lstrip('[').rstrip(']'))
268 except:
269 return -1
Enrico Granata217f91f2011-08-17 19:07:52 +0000270
271 def get_child_at_index(self,index):
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000272 if index < 0:
273 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000274 if index >= self.num_children():
275 return None;
Enrico Granata5a61fc0d2011-08-22 16:10:25 +0000276 try:
277 offset = index
278 current = self.left(self.Mheader);
279 while offset > 0:
280 current = self.increment_node(current)
281 offset = offset - 1;
282 # skip all the base stuff and get at the data
283 return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type)
284 except:
285 return None
Enrico Granata217f91f2011-08-17 19:07:52 +0000286
287 # utility functions
288 def node_ptr_value(self,node):
289 return node.GetValueAsUnsigned(0)
290
291 def right(self,node):
292 return node.GetChildMemberWithName("_M_right");
293
294 def left(self,node):
295 return node.GetChildMemberWithName("_M_left");
296
297 def parent(self,node):
298 return node.GetChildMemberWithName("_M_parent");
299
300 # from libstdc++ implementation of iterator for rbtree
301 def increment_node(self,node):
302 if self.node_ptr_value(self.right(node)) != 0:
303 x = self.right(node);
304 while self.node_ptr_value(self.left(x)) != 0:
305 x = self.left(x);
306 return x;
307 else:
308 x = node;
309 y = self.parent(x)
310 while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))):
311 x = y;
312 y = self.parent(y);
313 if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y):
314 x = y;
315 return x;
316