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