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