| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
try: |
| 23 |
from collections.abc import Mapping |
| 24 |
except Exception: |
| 25 |
from collections import Mapping |
| 26 |
import gdb |
| 27 |
import re |
| 28 |
import six |
| 29 |
|
| 30 |
from boost.util.compatibility import use_gdb_printing |
| 31 |
|
| 32 |
class SimplePrinter(object): |
| 33 |
|
| 34 |
def __init__(self, name, function): |
| 35 |
self.name = name |
| 36 |
self.function = function |
| 37 |
self.enabled = True |
| 38 |
|
| 39 |
def invoke(self, val): |
| 40 |
if not self.enabled: |
| 41 |
return None |
| 42 |
return self.function(self.name, val) |
| 43 |
|
| 44 |
class NameLookup(Mapping): |
| 45 |
|
| 46 |
def __init__(self): |
| 47 |
self.map = {} |
| 48 |
self.name_regex = re.compile(r'^([\w:]+)(<.*>)?') |
| 49 |
|
| 50 |
def add(self, name, printer): |
| 51 |
self.map[name] = printer |
| 52 |
|
| 53 |
def __len__(self): |
| 54 |
return len(self.map) |
| 55 |
|
| 56 |
def __getitem__(self, type): |
| 57 |
typename = self._basic_type(type) |
| 58 |
if typename and typename in self.map: |
| 59 |
return self.map[typename] |
| 60 |
return None |
| 61 |
|
| 62 |
def __iter__(self): |
| 63 |
return self.map |
| 64 |
|
| 65 |
def _basic_type(self, type): |
| 66 |
basic_type = self.basic_type(type) |
| 67 |
if basic_type: |
| 68 |
match = self.name_regex.match(basic_type) |
| 69 |
if match: |
| 70 |
return match.group(1) |
| 71 |
return None |
| 72 |
|
| 73 |
@staticmethod |
| 74 |
def basic_type(type): |
| 75 |
if type.code == gdb.TYPE_CODE_REF: |
| 76 |
type = type.target() |
| 77 |
type = type.unqualified().strip_typedefs() |
| 78 |
return type.tag |
| 79 |
|
| 80 |
class FunctionLookup(Mapping): |
| 81 |
|
| 82 |
def __init__(self): |
| 83 |
self.map = {} |
| 84 |
|
| 85 |
def add(self, test, printer): |
| 86 |
self.map[test] = printer |
| 87 |
|
| 88 |
def __len__(self): |
| 89 |
return len(self.map) |
| 90 |
|
| 91 |
def __getitem__(self, type): |
| 92 |
for (test, printer) in six.iteritems(self.map): |
| 93 |
if test(type): |
| 94 |
return printer |
| 95 |
return None |
| 96 |
|
| 97 |
def __iter__(self): |
| 98 |
return self.map |
| 99 |
|
| 100 |
class Printer(object): |
| 101 |
|
| 102 |
def __init__(self, name): |
| 103 |
self.name = name |
| 104 |
self.subprinters = [] |
| 105 |
self.name_lookup = NameLookup() |
| 106 |
self.func_lookup = FunctionLookup() |
| 107 |
self.enabled = True |
| 108 |
|
| 109 |
def add(self, name, function, lookup = None): |
| 110 |
printer = SimplePrinter(name, function) |
| 111 |
self.subprinters.append(printer) |
| 112 |
if not lookup: |
| 113 |
self.name_lookup.add(name, printer) |
| 114 |
else: |
| 115 |
self.func_lookup.add(lookup, printer) |
| 116 |
|
| 117 |
|
| 118 |
def __call__(self, val): |
| 119 |
printer = self.name_lookup[val.type] |
| 120 |
if not printer: |
| 121 |
printer = self.func_lookup[val.type] |
| 122 |
|
| 123 |
if printer: |
| 124 |
return printer.invoke(val) |
| 125 |
return None |
| 126 |
|
| 127 |
def register_pretty_printer(printer, obj): |
| 128 |
'''Registers printer with objfile''' |
| 129 |
|
| 130 |
if use_gdb_printing: |
| 131 |
gdb.printing.register_pretty_printer(obj, printer) |
| 132 |
else: |
| 133 |
if obj is None: |
| 134 |
obj = gdb |
| 135 |
obj.pretty_printers.append(printer) |
| 136 |
|
| 137 |
|