blob: dc3946df5be8a7c0ce07576757c5c599916831de [file] [log] [blame]
Enrico Granata074e3b62011-08-17 19:07:52 +00001import re
2
3# C++ STL formatters for LLDB
Enrico Granata68506fb2011-08-22 16:10:25 +00004# These formatters are based upon the version of the GNU libstdc++
Enrico Granata7e7f6b92012-02-08 19:57:18 +00005# as it ships with Mac OS X 10.6.8 thru 10.7.3
Enrico Granata68506fb2011-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 Granata074e3b62011-08-17 19:07:52 +00008
9class StdListSynthProvider:
10
11 def __init__(self, valobj, dict):
12 self.valobj = valobj
Enrico Granata074e3b62011-08-17 19:07:52 +000013
Enrico Granata06cdb8d2012-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 Granata074e3b62011-08-17 19:07:52 +000038 def num_children(self):
Enrico Granata06cdb8d2012-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 Granata68506fb2011-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 Granata06cdb8d2012-03-09 03:09:58 +000054 if self.has_loop():
55 return 0
Enrico Granata68506fb2011-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 Granata074e3b62011-08-17 19:07:52 +000064
65 def get_child_index(self,name):
Enrico Granata68506fb2011-08-22 16:10:25 +000066 try:
67 return int(name.lstrip('[').rstrip(']'))
68 except:
69 return -1
Enrico Granata074e3b62011-08-17 19:07:52 +000070
71 def get_child_at_index(self,index):
Enrico Granata68506fb2011-08-22 16:10:25 +000072 if index < 0:
73 return None;
Enrico Granata074e3b62011-08-17 19:07:52 +000074 if index >= self.num_children():
75 return None;
Enrico Granata68506fb2011-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 Granata278bc672011-08-22 16:38:44 +000084 return None
Enrico Granata074e3b62011-08-17 19:07:52 +000085
Enrico Granata6bbfa6c2012-02-03 18:11:52 +000086 def extract_type(self):
87 list_type = self.valobj.GetType().GetUnqualifiedType()
88 if list_type.GetNumberOfTemplateArguments() > 0:
89 data_type = list_type.GetTemplateArgumentType(0)
90 else:
91 data_type = None
92 return data_type
Enrico Granata074e3b62011-08-17 19:07:52 +000093
94 def update(self):
Enrico Granata68506fb2011-08-22 16:10:25 +000095 try:
96 impl = self.valobj.GetChildMemberWithName('_M_impl')
97 node = impl.GetChildMemberWithName('_M_node')
Enrico Granata68506fb2011-08-22 16:10:25 +000098 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0)
99 self.next = node.GetChildMemberWithName('_M_next')
100 self.prev = node.GetChildMemberWithName('_M_prev')
Enrico Granata6bbfa6c2012-02-03 18:11:52 +0000101 self.data_type = self.extract_type()
Enrico Granata68506fb2011-08-22 16:10:25 +0000102 self.data_size = self.data_type.GetByteSize()
Enrico Granata06cdb8d2012-03-09 03:09:58 +0000103 self.count = None
Enrico Granata68506fb2011-08-22 16:10:25 +0000104 except:
105 pass
Enrico Granata074e3b62011-08-17 19:07:52 +0000106
107class StdVectorSynthProvider:
108
109 def __init__(self, valobj, dict):
110 self.valobj = valobj;
Enrico Granata074e3b62011-08-17 19:07:52 +0000111
112 def num_children(self):
Enrico Granata06cdb8d2012-03-09 03:09:58 +0000113 if self.count == None:
114 self.count = self.num_children_impl()
115 return self.count
116
117 def is_valid_pointer(ptr,process):
118 error = lldb.SBError()
119 process.ReadMemory(ptr,1,error)
120 return False if error.Fail() else True
121
122 def num_children_impl(self):
Enrico Granata68506fb2011-08-22 16:10:25 +0000123 try:
124 start_val = self.start.GetValueAsUnsigned(0)
125 finish_val = self.finish.GetValueAsUnsigned(0)
126 end_val = self.end.GetValueAsUnsigned(0)
127 # Before a vector has been constructed, it will contain bad values
128 # so we really need to be careful about the length we return since
129 # unitialized data can cause us to return a huge number. We need
130 # to also check for any of the start, finish or end of storage values
131 # being zero (NULL). If any are, then this vector has not been
132 # initialized yet and we should return zero
Enrico Granata074e3b62011-08-17 19:07:52 +0000133
Enrico Granata68506fb2011-08-22 16:10:25 +0000134 # Make sure nothing is NULL
135 if start_val == 0 or finish_val == 0 or end_val == 0:
136 return 0
137 # Make sure start is less than finish
138 if start_val >= finish_val:
139 return 0
140 # Make sure finish is less than or equal to end of storage
141 if finish_val > end_val:
142 return 0
Enrico Granata074e3b62011-08-17 19:07:52 +0000143
Enrico Granata06cdb8d2012-03-09 03:09:58 +0000144 # if we have a struct (or other data type that the compiler pads to native word size)
145 # this check might fail, unless the sizeof() we get is itself incremented to take the
146 # padding bytes into account - on current clang it looks like this is the case
147 num_children = (finish_val-start_val)
148 if (num_children % self.data_size) != 0:
149 return 0
150 else:
151 num_children = num_children/self.data_size
Enrico Granata68506fb2011-08-22 16:10:25 +0000152 return num_children
153 except:
154 return 0;
Enrico Granata074e3b62011-08-17 19:07:52 +0000155
156 def get_child_index(self,name):
Enrico Granata68506fb2011-08-22 16:10:25 +0000157 try:
158 return int(name.lstrip('[').rstrip(']'))
159 except:
160 return -1
Enrico Granata074e3b62011-08-17 19:07:52 +0000161
162 def get_child_at_index(self,index):
Enrico Granata68506fb2011-08-22 16:10:25 +0000163 if index < 0:
164 return None;
Enrico Granata074e3b62011-08-17 19:07:52 +0000165 if index >= self.num_children():
166 return None;
Enrico Granata68506fb2011-08-22 16:10:25 +0000167 try:
168 offset = index * self.data_size
169 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type)
170 except:
171 return None
Enrico Granata074e3b62011-08-17 19:07:52 +0000172
173 def update(self):
Enrico Granata68506fb2011-08-22 16:10:25 +0000174 try:
175 impl = self.valobj.GetChildMemberWithName('_M_impl')
176 self.start = impl.GetChildMemberWithName('_M_start')
177 self.finish = impl.GetChildMemberWithName('_M_finish')
178 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
179 self.data_type = self.start.GetType().GetPointeeType()
180 self.data_size = self.data_type.GetByteSize()
Enrico Granata06cdb8d2012-03-09 03:09:58 +0000181 # if any of these objects is invalid, it means there is no point in trying to fetch anything
182 if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid():
183 self.count = None
184 else:
185 self.count = 0
Enrico Granata68506fb2011-08-22 16:10:25 +0000186 except:
187 pass
Enrico Granata074e3b62011-08-17 19:07:52 +0000188
189
190class StdMapSynthProvider:
191
192 def __init__(self, valobj, dict):
193 self.valobj = valobj;
Enrico Granata6bbfa6c2012-02-03 18:11:52 +0000194
195 # we need this function as a temporary workaround for rdar://problem/10801549
196 # which prevents us from extracting the std::pair<K,V> SBType out of the template
197 # arguments for _Rep_Type _M_t in the map itself - because we have to make up the
198 # typename and then find it, we may hit the situation were std::string has multiple
199 # names but only one is actually referenced in the debug information. hence, we need
200 # to replace the longer versions of std::string with the shorter one in order to be able
201 # to find the type name
202 def fixup_class_name(self, class_name):
203 if class_name == 'std::basic_string<char, class std::char_traits<char>, class std::allocator<char> >':
204 return 'std::basic_string<char>'
205 if class_name == 'basic_string<char, class std::char_traits<char>, class std::allocator<char> >':
206 return 'std::basic_string<char>'
207 if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
208 return 'std::basic_string<char>'
209 if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >':
210 return 'std::basic_string<char>'
211 return class_name
Enrico Granata074e3b62011-08-17 19:07:52 +0000212
213 def update(self):
Enrico Granata68506fb2011-08-22 16:10:25 +0000214 try:
215 self.Mt = self.valobj.GetChildMemberWithName('_M_t')
216 self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl')
217 self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header')
Enrico Granata6bbfa6c2012-02-03 18:11:52 +0000218
219 map_arg_0 = str(self.valobj.GetType().GetTemplateArgumentType(0).GetName())
220 map_arg_1 = str(self.valobj.GetType().GetTemplateArgumentType(1).GetName())
221
222 map_arg_0 = self.fixup_class_name(map_arg_0)
223 map_arg_1 = self.fixup_class_name(map_arg_1)
224
225 map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1
226 if map_arg_1[-1] == '>':
227 map_arg_type = map_arg_type + " >"
228 else:
229 map_arg_type = map_arg_type + ">"
230
231 self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type)
232
Enrico Granata68506fb2011-08-22 16:10:25 +0000233 # from libstdc++ implementation of _M_root for rbtree
234 self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent')
Enrico Granata68506fb2011-08-22 16:10:25 +0000235 self.data_size = self.data_type.GetByteSize()
236 self.skip_size = self.Mheader.GetType().GetByteSize()
237 except:
238 pass
Enrico Granata074e3b62011-08-17 19:07:52 +0000239
Enrico Granata074e3b62011-08-17 19:07:52 +0000240 def num_children(self):
Enrico Granata68506fb2011-08-22 16:10:25 +0000241 try:
242 root_ptr_val = self.node_ptr_value(self.Mroot)
243 if root_ptr_val == 0:
244 return 0;
245 return self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0)
246 except:
Enrico Granata074e3b62011-08-17 19:07:52 +0000247 return 0;
Enrico Granata074e3b62011-08-17 19:07:52 +0000248
249 def get_child_index(self,name):
Enrico Granata68506fb2011-08-22 16:10:25 +0000250 try:
251 return int(name.lstrip('[').rstrip(']'))
252 except:
253 return -1
Enrico Granata074e3b62011-08-17 19:07:52 +0000254
255 def get_child_at_index(self,index):
Enrico Granata68506fb2011-08-22 16:10:25 +0000256 if index < 0:
257 return None
Enrico Granata074e3b62011-08-17 19:07:52 +0000258 if index >= self.num_children():
259 return None;
Enrico Granata68506fb2011-08-22 16:10:25 +0000260 try:
261 offset = index
262 current = self.left(self.Mheader);
263 while offset > 0:
264 current = self.increment_node(current)
265 offset = offset - 1;
266 # skip all the base stuff and get at the data
267 return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type)
268 except:
269 return None
Enrico Granata074e3b62011-08-17 19:07:52 +0000270
271 # utility functions
272 def node_ptr_value(self,node):
273 return node.GetValueAsUnsigned(0)
274
275 def right(self,node):
276 return node.GetChildMemberWithName("_M_right");
277
278 def left(self,node):
279 return node.GetChildMemberWithName("_M_left");
280
281 def parent(self,node):
282 return node.GetChildMemberWithName("_M_parent");
283
284 # from libstdc++ implementation of iterator for rbtree
285 def increment_node(self,node):
286 if self.node_ptr_value(self.right(node)) != 0:
287 x = self.right(node);
288 while self.node_ptr_value(self.left(x)) != 0:
289 x = self.left(x);
290 return x;
291 else:
292 x = node;
293 y = self.parent(x)
294 while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))):
295 x = y;
296 y = self.parent(y);
297 if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y):
298 x = y;
299 return x;
300