Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 1 | import re |
| 2 | |
| 3 | # C++ STL formatters for LLDB |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 4 | # These formatters are based upon the version of the GNU libstdc++ |
Enrico Granata | 7e7f6b9 | 2012-02-08 19:57:18 +0000 | [diff] [blame] | 5 | # as it ships with Mac OS X 10.6.8 thru 10.7.3 |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 6 | # 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 Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 8 | |
| 9 | class StdListSynthProvider: |
| 10 | |
| 11 | def __init__(self, valobj, dict): |
| 12 | self.valobj = valobj |
| 13 | self.update() |
| 14 | |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 15 | def next_node(self,node): |
| 16 | return node.GetChildMemberWithName('_M_next') |
| 17 | |
| 18 | def is_valid(self,node): |
| 19 | return self.value(self.next_node(node)) != self.node_address |
| 20 | |
| 21 | def value(self,node): |
| 22 | return node.GetValueAsUnsigned() |
| 23 | |
| 24 | # Floyd's cyle-finding algorithm |
| 25 | # try to detect if this list has a loop |
| 26 | def has_loop(self): |
| 27 | slow = self.next |
| 28 | fast1 = self.next |
| 29 | fast2 = self.next |
| 30 | while self.is_valid(slow): |
| 31 | slow_value = self.value(slow) |
| 32 | fast1 = self.next_node(fast2) |
| 33 | fast2 = self.next_node(fast1) |
| 34 | if self.value(fast1) == slow_value or self.value(fast2) == slow_value: |
| 35 | return True |
| 36 | slow = self.next_node(slow) |
| 37 | return False |
| 38 | |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 39 | def num_children(self): |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 40 | if self.count == None: |
| 41 | self.count = self.num_children_impl() |
| 42 | return self.count |
| 43 | |
| 44 | def num_children_impl(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 45 | try: |
| 46 | next_val = self.next.GetValueAsUnsigned(0) |
| 47 | prev_val = self.prev.GetValueAsUnsigned(0) |
| 48 | # After a std::list has been initialized, both next and prev will be non-NULL |
| 49 | if next_val == 0 or prev_val == 0: |
| 50 | return 0 |
| 51 | if next_val == self.node_address: |
| 52 | return 0 |
| 53 | if next_val == prev_val: |
| 54 | return 1 |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 55 | if self.has_loop(): |
| 56 | return 0 |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 57 | size = 2 |
| 58 | current = self.next |
| 59 | while current.GetChildMemberWithName('_M_next').GetValueAsUnsigned(0) != self.node_address: |
| 60 | size = size + 1 |
| 61 | current = current.GetChildMemberWithName('_M_next') |
| 62 | return (size - 1) |
| 63 | except: |
| 64 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 65 | |
| 66 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 67 | try: |
| 68 | return int(name.lstrip('[').rstrip(']')) |
| 69 | except: |
| 70 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 71 | |
| 72 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 73 | if index < 0: |
| 74 | return None; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 75 | if index >= self.num_children(): |
| 76 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 77 | try: |
| 78 | offset = index |
| 79 | current = self.next |
| 80 | while offset > 0: |
| 81 | current = current.GetChildMemberWithName('_M_next') |
| 82 | offset = offset - 1 |
| 83 | return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type) |
| 84 | except: |
Enrico Granata | 278bc67 | 2011-08-22 16:38:44 +0000 | [diff] [blame] | 85 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 86 | |
Enrico Granata | 6bbfa6c | 2012-02-03 18:11:52 +0000 | [diff] [blame] | 87 | def extract_type(self): |
| 88 | list_type = self.valobj.GetType().GetUnqualifiedType() |
| 89 | if list_type.GetNumberOfTemplateArguments() > 0: |
| 90 | data_type = list_type.GetTemplateArgumentType(0) |
| 91 | else: |
| 92 | data_type = None |
| 93 | return data_type |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 94 | |
| 95 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 96 | try: |
| 97 | impl = self.valobj.GetChildMemberWithName('_M_impl') |
| 98 | node = impl.GetChildMemberWithName('_M_node') |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 99 | self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0) |
| 100 | self.next = node.GetChildMemberWithName('_M_next') |
| 101 | self.prev = node.GetChildMemberWithName('_M_prev') |
Enrico Granata | 6bbfa6c | 2012-02-03 18:11:52 +0000 | [diff] [blame] | 102 | self.data_type = self.extract_type() |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 103 | self.data_size = self.data_type.GetByteSize() |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 104 | self.count = None |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 105 | except: |
| 106 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 107 | |
| 108 | class StdVectorSynthProvider: |
| 109 | |
| 110 | def __init__(self, valobj, dict): |
| 111 | self.valobj = valobj; |
| 112 | self.update() |
| 113 | |
| 114 | def num_children(self): |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 115 | 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 Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 125 | 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 Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 135 | |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 136 | # 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 Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 145 | |
Enrico Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 146 | # 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 Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 154 | return num_children |
| 155 | except: |
| 156 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 157 | |
| 158 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 159 | try: |
| 160 | return int(name.lstrip('[').rstrip(']')) |
| 161 | except: |
| 162 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 163 | |
| 164 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 165 | if index < 0: |
| 166 | return None; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 167 | if index >= self.num_children(): |
| 168 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 169 | try: |
| 170 | offset = index * self.data_size |
| 171 | return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) |
| 172 | except: |
| 173 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 174 | |
| 175 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 176 | 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 Granata | 06cdb8d | 2012-03-09 03:09:58 +0000 | [diff] [blame^] | 183 | # 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 Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 188 | except: |
| 189 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | class StdMapSynthProvider: |
| 193 | |
| 194 | def __init__(self, valobj, dict): |
| 195 | self.valobj = valobj; |
| 196 | self.update() |
Enrico Granata | 6bbfa6c | 2012-02-03 18:11:52 +0000 | [diff] [blame] | 197 | |
| 198 | # we need this function as a temporary workaround for rdar://problem/10801549 |
| 199 | # which prevents us from extracting the std::pair<K,V> SBType out of the template |
| 200 | # arguments for _Rep_Type _M_t in the map itself - because we have to make up the |
| 201 | # typename and then find it, we may hit the situation were std::string has multiple |
| 202 | # names but only one is actually referenced in the debug information. hence, we need |
| 203 | # to replace the longer versions of std::string with the shorter one in order to be able |
| 204 | # to find the type name |
| 205 | def fixup_class_name(self, class_name): |
| 206 | if class_name == 'std::basic_string<char, class std::char_traits<char>, class std::allocator<char> >': |
| 207 | return 'std::basic_string<char>' |
| 208 | if class_name == 'basic_string<char, class std::char_traits<char>, class std::allocator<char> >': |
| 209 | return 'std::basic_string<char>' |
| 210 | if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': |
| 211 | return 'std::basic_string<char>' |
| 212 | if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >': |
| 213 | return 'std::basic_string<char>' |
| 214 | return class_name |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 215 | |
| 216 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 217 | try: |
| 218 | self.Mt = self.valobj.GetChildMemberWithName('_M_t') |
| 219 | self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl') |
| 220 | self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header') |
Enrico Granata | 6bbfa6c | 2012-02-03 18:11:52 +0000 | [diff] [blame] | 221 | |
| 222 | map_arg_0 = str(self.valobj.GetType().GetTemplateArgumentType(0).GetName()) |
| 223 | map_arg_1 = str(self.valobj.GetType().GetTemplateArgumentType(1).GetName()) |
| 224 | |
| 225 | map_arg_0 = self.fixup_class_name(map_arg_0) |
| 226 | map_arg_1 = self.fixup_class_name(map_arg_1) |
| 227 | |
| 228 | map_arg_type = "std::pair<const " + map_arg_0 + ", " + map_arg_1 |
| 229 | if map_arg_1[-1] == '>': |
| 230 | map_arg_type = map_arg_type + " >" |
| 231 | else: |
| 232 | map_arg_type = map_arg_type + ">" |
| 233 | |
| 234 | self.data_type = self.valobj.GetTarget().FindFirstType(map_arg_type) |
| 235 | |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 236 | # from libstdc++ implementation of _M_root for rbtree |
| 237 | self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent') |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 238 | self.data_size = self.data_type.GetByteSize() |
| 239 | self.skip_size = self.Mheader.GetType().GetByteSize() |
| 240 | except: |
| 241 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 242 | |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 243 | def num_children(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 244 | try: |
| 245 | root_ptr_val = self.node_ptr_value(self.Mroot) |
| 246 | if root_ptr_val == 0: |
| 247 | return 0; |
| 248 | return self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0) |
| 249 | except: |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 250 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 251 | |
| 252 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 253 | try: |
| 254 | return int(name.lstrip('[').rstrip(']')) |
| 255 | except: |
| 256 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 257 | |
| 258 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 259 | if index < 0: |
| 260 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 261 | if index >= self.num_children(): |
| 262 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame] | 263 | try: |
| 264 | offset = index |
| 265 | current = self.left(self.Mheader); |
| 266 | while offset > 0: |
| 267 | current = self.increment_node(current) |
| 268 | offset = offset - 1; |
| 269 | # skip all the base stuff and get at the data |
| 270 | return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type) |
| 271 | except: |
| 272 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 273 | |
| 274 | # utility functions |
| 275 | def node_ptr_value(self,node): |
| 276 | return node.GetValueAsUnsigned(0) |
| 277 | |
| 278 | def right(self,node): |
| 279 | return node.GetChildMemberWithName("_M_right"); |
| 280 | |
| 281 | def left(self,node): |
| 282 | return node.GetChildMemberWithName("_M_left"); |
| 283 | |
| 284 | def parent(self,node): |
| 285 | return node.GetChildMemberWithName("_M_parent"); |
| 286 | |
| 287 | # from libstdc++ implementation of iterator for rbtree |
| 288 | def increment_node(self,node): |
| 289 | if self.node_ptr_value(self.right(node)) != 0: |
| 290 | x = self.right(node); |
| 291 | while self.node_ptr_value(self.left(x)) != 0: |
| 292 | x = self.left(x); |
| 293 | return x; |
| 294 | else: |
| 295 | x = node; |
| 296 | y = self.parent(x) |
| 297 | while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))): |
| 298 | x = y; |
| 299 | y = self.parent(y); |
| 300 | if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y): |
| 301 | x = y; |
| 302 | return x; |
| 303 | |