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++ |
| 5 | # as it ships with Mac OS X 10.6.8 and 10.7.0 |
| 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 | |
| 15 | def num_children(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 16 | try: |
| 17 | next_val = self.next.GetValueAsUnsigned(0) |
| 18 | prev_val = self.prev.GetValueAsUnsigned(0) |
| 19 | # After a std::list has been initialized, both next and prev will be non-NULL |
| 20 | if next_val == 0 or prev_val == 0: |
| 21 | return 0 |
| 22 | if next_val == self.node_address: |
| 23 | return 0 |
| 24 | if next_val == prev_val: |
| 25 | return 1 |
| 26 | size = 2 |
| 27 | current = self.next |
| 28 | while current.GetChildMemberWithName('_M_next').GetValueAsUnsigned(0) != self.node_address: |
| 29 | size = size + 1 |
| 30 | current = current.GetChildMemberWithName('_M_next') |
| 31 | return (size - 1) |
| 32 | except: |
| 33 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 34 | |
| 35 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 36 | try: |
| 37 | return int(name.lstrip('[').rstrip(']')) |
| 38 | except: |
| 39 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 40 | |
| 41 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 42 | if index < 0: |
| 43 | return None; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 44 | if index >= self.num_children(): |
| 45 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 46 | try: |
| 47 | offset = index |
| 48 | current = self.next |
| 49 | while offset > 0: |
| 50 | current = current.GetChildMemberWithName('_M_next') |
| 51 | offset = offset - 1 |
| 52 | return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type) |
| 53 | except: |
| 54 | return None: |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 55 | |
| 56 | def extract_type_name(self,name): |
| 57 | self.type_name = name[16:] |
| 58 | index = 2 |
| 59 | count_of_template = 1 |
| 60 | while index < len(self.type_name): |
| 61 | if self.type_name[index] == '<': |
| 62 | count_of_template = count_of_template + 1 |
| 63 | elif self.type_name[index] == '>': |
| 64 | count_of_template = count_of_template - 1 |
| 65 | elif self.type_name[index] == ',' and count_of_template == 1: |
| 66 | self.type_name = self.type_name[:index] |
| 67 | break |
| 68 | index = index + 1 |
| 69 | self.type_name_nospaces = self.type_name.replace(", ", ",") |
| 70 | |
| 71 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 72 | try: |
| 73 | impl = self.valobj.GetChildMemberWithName('_M_impl') |
| 74 | node = impl.GetChildMemberWithName('_M_node') |
| 75 | self.extract_type_name(impl.GetType().GetName()) |
| 76 | self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0) |
| 77 | self.next = node.GetChildMemberWithName('_M_next') |
| 78 | self.prev = node.GetChildMemberWithName('_M_prev') |
| 79 | self.data_type = node.GetTarget().FindFirstType(self.type_name) |
| 80 | # tries to fight against a difference in formatting type names between gcc and clang |
| 81 | if self.data_type.IsValid() == False: |
| 82 | self.data_type = node.GetTarget().FindFirstType(self.type_name_nospaces) |
| 83 | self.data_size = self.data_type.GetByteSize() |
| 84 | except: |
| 85 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 86 | |
| 87 | class StdVectorSynthProvider: |
| 88 | |
| 89 | def __init__(self, valobj, dict): |
| 90 | self.valobj = valobj; |
| 91 | self.update() |
| 92 | |
| 93 | def num_children(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 94 | try: |
| 95 | start_val = self.start.GetValueAsUnsigned(0) |
| 96 | finish_val = self.finish.GetValueAsUnsigned(0) |
| 97 | end_val = self.end.GetValueAsUnsigned(0) |
| 98 | # Before a vector has been constructed, it will contain bad values |
| 99 | # so we really need to be careful about the length we return since |
| 100 | # unitialized data can cause us to return a huge number. We need |
| 101 | # to also check for any of the start, finish or end of storage values |
| 102 | # being zero (NULL). If any are, then this vector has not been |
| 103 | # initialized yet and we should return zero |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 104 | |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 105 | # Make sure nothing is NULL |
| 106 | if start_val == 0 or finish_val == 0 or end_val == 0: |
| 107 | return 0 |
| 108 | # Make sure start is less than finish |
| 109 | if start_val >= finish_val: |
| 110 | return 0 |
| 111 | # Make sure finish is less than or equal to end of storage |
| 112 | if finish_val > end_val: |
| 113 | return 0 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 114 | |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 115 | num_children = (finish_val-start_val)/self.data_size |
| 116 | return num_children |
| 117 | except: |
| 118 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 119 | |
| 120 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 121 | try: |
| 122 | return int(name.lstrip('[').rstrip(']')) |
| 123 | except: |
| 124 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 125 | |
| 126 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 127 | if index < 0: |
| 128 | return None; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 129 | if index >= self.num_children(): |
| 130 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 131 | try: |
| 132 | offset = index * self.data_size |
| 133 | return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) |
| 134 | except: |
| 135 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 136 | |
| 137 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 138 | try: |
| 139 | impl = self.valobj.GetChildMemberWithName('_M_impl') |
| 140 | self.start = impl.GetChildMemberWithName('_M_start') |
| 141 | self.finish = impl.GetChildMemberWithName('_M_finish') |
| 142 | self.end = impl.GetChildMemberWithName('_M_end_of_storage') |
| 143 | self.data_type = self.start.GetType().GetPointeeType() |
| 144 | self.data_size = self.data_type.GetByteSize() |
| 145 | except: |
| 146 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | class StdMapSynthProvider: |
| 150 | |
| 151 | def __init__(self, valobj, dict): |
| 152 | self.valobj = valobj; |
| 153 | self.update() |
| 154 | |
| 155 | def update(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 156 | try: |
| 157 | self.Mt = self.valobj.GetChildMemberWithName('_M_t') |
| 158 | self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl') |
| 159 | self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header') |
| 160 | # from libstdc++ implementation of _M_root for rbtree |
| 161 | self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent') |
| 162 | # the stuff into the tree is actually a std::pair<const key, value> |
| 163 | # life would be much easier if gcc had a coherent way to print out |
| 164 | # template names in debug info |
| 165 | self.expand_clang_type_name() |
| 166 | self.expand_gcc_type_name() |
| 167 | self.data_type = self.Mt.GetTarget().FindFirstType(self.clang_type_name) |
| 168 | if self.data_type.IsValid() == False: |
| 169 | self.data_type = self.Mt.GetTarget().FindFirstType(self.gcc_type_name) |
| 170 | self.data_size = self.data_type.GetByteSize() |
| 171 | self.skip_size = self.Mheader.GetType().GetByteSize() |
| 172 | except: |
| 173 | pass |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 174 | |
| 175 | def expand_clang_type_name(self): |
| 176 | type_name = self.Mimpl.GetType().GetName() |
| 177 | index = type_name.find("std::pair<") |
| 178 | type_name = type_name[index+5:] |
| 179 | index = 6 |
| 180 | template_count = 1 |
| 181 | while index < len(type_name): |
| 182 | if type_name[index] == '<': |
| 183 | template_count = template_count + 1 |
| 184 | elif type_name[index] == '>' and template_count == 1: |
| 185 | type_name = type_name[:index+1] |
| 186 | break |
| 187 | elif type_name[index] == '>': |
| 188 | template_count = template_count - 1 |
| 189 | index = index + 1; |
| 190 | self.clang_type_name = type_name |
| 191 | |
| 192 | def expand_gcc_type_name(self): |
| 193 | type_name = self.Mt.GetType().GetName() |
| 194 | index = type_name.find("std::pair<") |
| 195 | type_name = type_name[index+5:] |
| 196 | index = 6 |
| 197 | template_count = 1 |
| 198 | while index < len(type_name): |
| 199 | if type_name[index] == '<': |
| 200 | template_count = template_count + 1 |
| 201 | elif type_name[index] == '>' and template_count == 1: |
| 202 | type_name = type_name[:index+1] |
| 203 | break |
| 204 | elif type_name[index] == '>': |
| 205 | template_count = template_count - 1 |
| 206 | elif type_name[index] == ' ' and template_count == 1 and type_name[index-1] == ',': |
| 207 | type_name = type_name[0:index] + type_name[index+1:] |
| 208 | index = index - 1 |
| 209 | index = index + 1; |
| 210 | self.gcc_type_name = type_name |
| 211 | |
| 212 | def num_children(self): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 213 | try: |
| 214 | root_ptr_val = self.node_ptr_value(self.Mroot) |
| 215 | if root_ptr_val == 0: |
| 216 | return 0; |
| 217 | return self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0) |
| 218 | except: |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 219 | return 0; |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 220 | |
| 221 | def get_child_index(self,name): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 222 | try: |
| 223 | return int(name.lstrip('[').rstrip(']')) |
| 224 | except: |
| 225 | return -1 |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 226 | |
| 227 | def get_child_at_index(self,index): |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 228 | if index < 0: |
| 229 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 230 | if index >= self.num_children(): |
| 231 | return None; |
Enrico Granata | 68506fb | 2011-08-22 16:10:25 +0000 | [diff] [blame^] | 232 | try: |
| 233 | offset = index |
| 234 | current = self.left(self.Mheader); |
| 235 | while offset > 0: |
| 236 | current = self.increment_node(current) |
| 237 | offset = offset - 1; |
| 238 | # skip all the base stuff and get at the data |
| 239 | return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type) |
| 240 | except: |
| 241 | return None |
Enrico Granata | 074e3b6 | 2011-08-17 19:07:52 +0000 | [diff] [blame] | 242 | |
| 243 | # utility functions |
| 244 | def node_ptr_value(self,node): |
| 245 | return node.GetValueAsUnsigned(0) |
| 246 | |
| 247 | def right(self,node): |
| 248 | return node.GetChildMemberWithName("_M_right"); |
| 249 | |
| 250 | def left(self,node): |
| 251 | return node.GetChildMemberWithName("_M_left"); |
| 252 | |
| 253 | def parent(self,node): |
| 254 | return node.GetChildMemberWithName("_M_parent"); |
| 255 | |
| 256 | # from libstdc++ implementation of iterator for rbtree |
| 257 | def increment_node(self,node): |
| 258 | if self.node_ptr_value(self.right(node)) != 0: |
| 259 | x = self.right(node); |
| 260 | while self.node_ptr_value(self.left(x)) != 0: |
| 261 | x = self.left(x); |
| 262 | return x; |
| 263 | else: |
| 264 | x = node; |
| 265 | y = self.parent(x) |
| 266 | while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))): |
| 267 | x = y; |
| 268 | y = self.parent(y); |
| 269 | if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y): |
| 270 | x = y; |
| 271 | return x; |
| 272 | |