David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 1 | import gdb.printing |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 2 | |
| 3 | class Iterator: |
| 4 | def __iter__(self): |
| 5 | return self |
| 6 | |
| 7 | # Python 2 compatibility |
| 8 | def next(self): |
| 9 | return self.__next__() |
| 10 | |
| 11 | def children(self): |
| 12 | return self |
| 13 | |
Fangrui Song | 6a90637 | 2018-06-26 00:41:49 +0000 | [diff] [blame] | 14 | def escape_bytes(val, l): |
| 15 | return '"' + val.string(encoding='Latin-1', length=l).encode('unicode_escape').decode() + '"' |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 16 | |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 17 | class SmallStringPrinter: |
| 18 | """Print an llvm::SmallString object.""" |
| 19 | |
| 20 | def __init__(self, val): |
| 21 | self.val = val |
| 22 | |
| 23 | def to_string(self): |
| 24 | begin = self.val['BeginX'] |
Fangrui Song | d9c2547 | 2018-07-23 21:33:51 +0000 | [diff] [blame] | 25 | return escape_bytes(begin.cast(gdb.lookup_type('char').pointer()), self.val['Size']) |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 26 | |
| 27 | class StringRefPrinter: |
| 28 | """Print an llvm::StringRef object.""" |
| 29 | |
| 30 | def __init__(self, val): |
| 31 | self.val = val |
| 32 | |
| 33 | def to_string(self): |
Fangrui Song | 6a90637 | 2018-06-26 00:41:49 +0000 | [diff] [blame] | 34 | return escape_bytes(self.val['Data'], self.val['Length']) |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 35 | |
Fangrui Song | d9c2547 | 2018-07-23 21:33:51 +0000 | [diff] [blame] | 36 | class SmallVectorPrinter(Iterator): |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 37 | """Print an llvm::SmallVector object.""" |
| 38 | |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 39 | def __init__(self, val): |
| 40 | self.val = val |
Fangrui Song | d9c2547 | 2018-07-23 21:33:51 +0000 | [diff] [blame] | 41 | t = val.type.template_argument(0).pointer() |
| 42 | self.begin = val['BeginX'].cast(t) |
| 43 | self.size = val['Size'] |
| 44 | self.i = 0 |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 45 | |
Fangrui Song | d9c2547 | 2018-07-23 21:33:51 +0000 | [diff] [blame] | 46 | def __next__(self): |
| 47 | if self.i == self.size: |
| 48 | raise StopIteration |
| 49 | ret = '[{}]'.format(self.i), (self.begin+self.i).dereference() |
| 50 | self.i += 1 |
| 51 | return ret |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 52 | |
| 53 | def to_string(self): |
Fangrui Song | d9c2547 | 2018-07-23 21:33:51 +0000 | [diff] [blame] | 54 | return 'llvm::SmallVector of Size {}, Capacity {}'.format(self.size, self.val['Capacity']) |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 55 | |
| 56 | def display_hint (self): |
| 57 | return 'array' |
| 58 | |
| 59 | class ArrayRefPrinter: |
| 60 | """Print an llvm::ArrayRef object.""" |
| 61 | |
| 62 | class _iterator: |
| 63 | def __init__(self, begin, end): |
| 64 | self.cur = begin |
| 65 | self.end = end |
| 66 | self.count = 0 |
| 67 | |
| 68 | def __iter__(self): |
| 69 | return self |
| 70 | |
| 71 | def next(self): |
| 72 | if self.cur == self.end: |
| 73 | raise StopIteration |
| 74 | count = self.count |
| 75 | self.count = self.count + 1 |
| 76 | cur = self.cur |
| 77 | self.cur = self.cur + 1 |
| 78 | return '[%d]' % count, cur.dereference() |
| 79 | |
Igor Kudrin | a097a36 | 2016-08-05 16:48:31 +0000 | [diff] [blame] | 80 | __next__ = next |
| 81 | |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 82 | def __init__(self, val): |
| 83 | self.val = val |
| 84 | |
Philip Pfaffe | 0566f23 | 2018-07-09 18:51:50 +0000 | [diff] [blame] | 85 | __next__ = next |
| 86 | |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 87 | def children(self): |
| 88 | data = self.val['Data'] |
| 89 | return self._iterator(data, data + self.val['Length']) |
| 90 | |
| 91 | def to_string(self): |
| 92 | return 'llvm::ArrayRef of length %d' % (self.val['Length']) |
| 93 | |
| 94 | def display_hint (self): |
| 95 | return 'array' |
| 96 | |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 97 | class ExpectedPrinter(Iterator): |
| 98 | """Print an llvm::Expected object.""" |
David Blaikie | c66e7e3 | 2016-12-16 19:16:22 +0000 | [diff] [blame] | 99 | |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 100 | def __init__(self, val): |
| 101 | self.val = val |
David Blaikie | c66e7e3 | 2016-12-16 19:16:22 +0000 | [diff] [blame] | 102 | |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 103 | def __next__(self): |
| 104 | val = self.val |
| 105 | if val is None: |
| 106 | raise StopIteration |
| 107 | self.val = None |
| 108 | if val['HasError']: |
| 109 | return ('error', val['ErrorStorage'].address.cast( |
| 110 | gdb.lookup_type('llvm::ErrorInfoBase').pointer()).dereference()) |
| 111 | return ('value', val['TStorage'].address.cast( |
| 112 | val.type.template_argument(0).pointer()).dereference()) |
David Blaikie | c66e7e3 | 2016-12-16 19:16:22 +0000 | [diff] [blame] | 113 | |
| 114 | def to_string(self): |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 115 | return 'llvm::Expected{}'.format(' is error' if self.val['HasError'] else '') |
| 116 | |
| 117 | class OptionalPrinter(Iterator): |
| 118 | """Print an llvm::Optional object.""" |
| 119 | |
| 120 | def __init__(self, val): |
| 121 | self.val = val |
| 122 | |
| 123 | def __next__(self): |
| 124 | val = self.val |
| 125 | if val is None: |
| 126 | raise StopIteration |
| 127 | self.val = None |
| 128 | if not val['Storage']['hasVal']: |
| 129 | raise StopIteration |
| 130 | return ('value', val['Storage']['storage']['buffer'].address.cast( |
| 131 | val.type.template_argument(0).pointer()).dereference()) |
| 132 | |
| 133 | def to_string(self): |
| 134 | return 'llvm::Optional{}'.format('' if self.val['Storage']['hasVal'] else ' is not initialized') |
David Blaikie | c66e7e3 | 2016-12-16 19:16:22 +0000 | [diff] [blame] | 135 | |
David Blaikie | 23cbb11 | 2016-12-16 23:53:14 +0000 | [diff] [blame] | 136 | class DenseMapPrinter: |
| 137 | "Print a DenseMap" |
| 138 | |
| 139 | class _iterator: |
| 140 | def __init__(self, key_info_t, begin, end): |
| 141 | self.key_info_t = key_info_t |
| 142 | self.cur = begin |
| 143 | self.end = end |
| 144 | self.advancePastEmptyBuckets() |
| 145 | self.first = True |
| 146 | |
| 147 | def __iter__(self): |
| 148 | return self |
| 149 | |
| 150 | def advancePastEmptyBuckets(self): |
| 151 | # disabled until the comments below can be addressed |
| 152 | # keeping as notes/posterity/hints for future contributors |
| 153 | return |
| 154 | n = self.key_info_t.name |
| 155 | is_equal = gdb.parse_and_eval(n + '::isEqual') |
| 156 | empty = gdb.parse_and_eval(n + '::getEmptyKey()') |
| 157 | tombstone = gdb.parse_and_eval(n + '::getTombstoneKey()') |
| 158 | # the following is invalid, GDB fails with: |
| 159 | # Python Exception <class 'gdb.error'> Attempt to take address of value |
| 160 | # not located in memory. |
| 161 | # because isEqual took parameter (for the unsigned long key I was testing) |
| 162 | # by const ref, and GDB |
| 163 | # It's also not entirely general - we should be accessing the "getFirst()" |
| 164 | # member function, not the 'first' member variable, but I've yet to figure |
| 165 | # out how to find/call member functions (especially (const) overloaded |
| 166 | # ones) on a gdb.Value. |
| 167 | while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)): |
| 168 | self.cur = self.cur + 1 |
| 169 | |
| 170 | def next(self): |
| 171 | if self.cur == self.end: |
| 172 | raise StopIteration |
| 173 | cur = self.cur |
| 174 | v = cur.dereference()['first' if self.first else 'second'] |
| 175 | if not self.first: |
| 176 | self.cur = self.cur + 1 |
| 177 | self.advancePastEmptyBuckets() |
| 178 | self.first = True |
| 179 | else: |
| 180 | self.first = False |
| 181 | return 'x', v |
| 182 | |
Philip Pfaffe | 0566f23 | 2018-07-09 18:51:50 +0000 | [diff] [blame] | 183 | __next__ = next |
| 184 | |
David Blaikie | 23cbb11 | 2016-12-16 23:53:14 +0000 | [diff] [blame] | 185 | def __init__(self, val): |
| 186 | self.val = val |
| 187 | |
| 188 | def children(self): |
| 189 | t = self.val.type.template_argument(3).pointer() |
| 190 | begin = self.val['Buckets'].cast(t) |
| 191 | end = (begin + self.val['NumBuckets']).cast(t) |
| 192 | return self._iterator(self.val.type.template_argument(2), begin, end) |
| 193 | |
| 194 | def to_string(self): |
| 195 | return 'llvm::DenseMap with %d elements' % (self.val['NumEntries']) |
| 196 | |
| 197 | def display_hint(self): |
| 198 | return 'map' |
| 199 | |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 200 | class TwinePrinter: |
| 201 | "Print a Twine" |
| 202 | |
| 203 | def __init__(self, val): |
| 204 | self._val = val |
| 205 | |
| 206 | def display_hint(self): |
| 207 | return 'string' |
| 208 | |
| 209 | def string_from_pretty_printer_lookup(self, val): |
| 210 | '''Lookup the default pretty-printer for val and use it. |
| 211 | |
| 212 | If no pretty-printer is defined for the type of val, print an error and |
| 213 | return a placeholder string.''' |
| 214 | |
| 215 | pp = gdb.default_visualizer(val) |
| 216 | if pp: |
| 217 | s = pp.to_string() |
| 218 | |
| 219 | # The pretty-printer may return a LazyString instead of an actual Python |
| 220 | # string. Convert it to a Python string. However, GDB doesn't seem to |
| 221 | # register the LazyString type, so we can't check |
| 222 | # "type(s) == gdb.LazyString". |
| 223 | if 'LazyString' in type(s).__name__: |
| 224 | s = s.value().address.string() |
| 225 | |
| 226 | else: |
| 227 | print(('No pretty printer for {} found. The resulting Twine ' + |
| 228 | 'representation will be incomplete.').format(val.type.name)) |
| 229 | s = '(missing {})'.format(val.type.name) |
| 230 | |
| 231 | return s |
| 232 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 233 | def is_twine_kind(self, kind, expected): |
| 234 | if not kind.endswith(expected): |
| 235 | return False |
| 236 | # apparently some GDB versions add the NodeKind:: namespace |
| 237 | # (happens for me on GDB 7.11) |
| 238 | return kind in ('llvm::Twine::' + expected, |
| 239 | 'llvm::Twine::NodeKind::' + expected) |
| 240 | |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 241 | def string_from_child(self, child, kind): |
| 242 | '''Return the string representation of the Twine::Child child.''' |
| 243 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 244 | if self.is_twine_kind(kind, 'EmptyKind') or self.is_twine_kind(kind, 'NullKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 245 | return '' |
| 246 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 247 | if self.is_twine_kind(kind, 'TwineKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 248 | return self.string_from_twine_object(child['twine'].dereference()) |
| 249 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 250 | if self.is_twine_kind(kind, 'CStringKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 251 | return child['cString'].string() |
| 252 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 253 | if self.is_twine_kind(kind, 'StdStringKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 254 | val = child['stdString'].dereference() |
| 255 | return self.string_from_pretty_printer_lookup(val) |
| 256 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 257 | if self.is_twine_kind(kind, 'StringRefKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 258 | val = child['stringRef'].dereference() |
| 259 | pp = StringRefPrinter(val) |
| 260 | return pp.to_string() |
| 261 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 262 | if self.is_twine_kind(kind, 'SmallStringKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 263 | val = child['smallString'].dereference() |
| 264 | pp = SmallStringPrinter(val) |
| 265 | return pp.to_string() |
| 266 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 267 | if self.is_twine_kind(kind, 'CharKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 268 | return chr(child['character']) |
| 269 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 270 | if self.is_twine_kind(kind, 'DecUIKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 271 | return str(child['decUI']) |
| 272 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 273 | if self.is_twine_kind(kind, 'DecIKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 274 | return str(child['decI']) |
| 275 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 276 | if self.is_twine_kind(kind, 'DecULKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 277 | return str(child['decUL'].dereference()) |
| 278 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 279 | if self.is_twine_kind(kind, 'DecLKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 280 | return str(child['decL'].dereference()) |
| 281 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 282 | if self.is_twine_kind(kind, 'DecULLKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 283 | return str(child['decULL'].dereference()) |
| 284 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 285 | if self.is_twine_kind(kind, 'DecLLKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 286 | return str(child['decLL'].dereference()) |
| 287 | |
David Blaikie | 719c94a | 2017-06-04 03:27:12 +0000 | [diff] [blame] | 288 | if self.is_twine_kind(kind, 'UHexKind'): |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 289 | val = child['uHex'].dereference() |
| 290 | return hex(int(val)) |
| 291 | |
| 292 | print(('Unhandled NodeKind {} in Twine pretty-printer. The result will be ' |
| 293 | 'incomplete.').format(kind)) |
| 294 | |
| 295 | return '(unhandled {})'.format(kind) |
| 296 | |
| 297 | def string_from_twine_object(self, twine): |
| 298 | '''Return the string representation of the Twine object twine.''' |
| 299 | |
| 300 | lhs_str = '' |
| 301 | rhs_str = '' |
| 302 | |
| 303 | lhs = twine['LHS'] |
| 304 | rhs = twine['RHS'] |
| 305 | lhs_kind = str(twine['LHSKind']) |
| 306 | rhs_kind = str(twine['RHSKind']) |
| 307 | |
| 308 | lhs_str = self.string_from_child(lhs, lhs_kind) |
| 309 | rhs_str = self.string_from_child(rhs, rhs_kind) |
| 310 | |
| 311 | return lhs_str + rhs_str |
| 312 | |
| 313 | def to_string(self): |
| 314 | return self.string_from_twine_object(self._val) |
David Blaikie | 23cbb11 | 2016-12-16 23:53:14 +0000 | [diff] [blame] | 315 | |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 316 | pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport") |
| 317 | pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter) |
| 318 | pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter) |
| 319 | pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter) |
| 320 | pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter) |
Fangrui Song | a157b8b | 2018-06-25 23:38:48 +0000 | [diff] [blame] | 321 | pp.add_printer('llvm::Expected', '^llvm::Expected<.*>$', ExpectedPrinter) |
David Blaikie | c66e7e3 | 2016-12-16 19:16:22 +0000 | [diff] [blame] | 322 | pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter) |
David Blaikie | 23cbb11 | 2016-12-16 23:53:14 +0000 | [diff] [blame] | 323 | pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter) |
David Blaikie | 718e8ad | 2017-03-15 20:51:44 +0000 | [diff] [blame] | 324 | pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter) |
David Blaikie | 09ec575 | 2016-06-01 01:02:32 +0000 | [diff] [blame] | 325 | gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) |