1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# Work around awkwardness in gdb's python printers:
# 1. In src/main-gdb-head.py, define the printer mechanism.
# 2. In src/*/*.py, define all the printer classes.
# 3. In src/main-gdb-tail.py, reflect to actually add the printers.
# gdb sticks everything in one scope.
# This lets us enumerate what *we* added.
initial_globals = {id(v):v for v in globals().values()}
import re
# copied from gdb/types.py for compatibility with old gdb
def get_basic_type(type_):
"""Return the "basic" type of a type.
Arguments:
type_: The type to reduce to its basic type.
Returns:
type_ with const/volatile is stripped away,
and typedefs/references converted to the underlying type.
"""
while (type_.code == gdb.TYPE_CODE_REF or
type_.code == gdb.TYPE_CODE_TYPEDEF):
if type_.code == gdb.TYPE_CODE_REF:
type_ = type_.target()
else:
type_ = type_.strip_typedefs()
return type_.unqualified()
def info_symbol(addr):
''' returns (symbol, offset, section, lib or None) or None?
'''
info = gdb.execute('info symbol %d' % addr, to_string=True)
try:
sym_and_off, sec_and_lib = info.split(' in section ')
except ValueError:
return None
try:
sym, off = sym_and_off.split(' + ')
except ValueError:
sym = sym_and_off
off = 0
else:
off = int(off, 10)
try:
sec, lib = sec_and_lib.split(' of ')
except ValueError:
sec = sec_and_lib
lib = None
return (sym, off, sec, lib)
def finish():
global finish, initial_globals, FastPrinters, EnumPrinter, PointerPrinter
final_globals = {id(v):v for v in globals().values()}
diff = set(final_globals.keys()) - set(initial_globals.keys()) \
- {
'finish',
'initial_globals',
'FastPrinters',
'EnumPrinter',
'PointerPrinter',
}
fp = FastPrinters()
ep = EnumPrinter
ptrp = PointerPrinter
# After this, don't access any more globals in this function.
del finish, initial_globals, FastPrinters, EnumPrinter, PointerPrinter
for i in diff:
v = final_globals[i]
if hasattr(v, 'children') or hasattr(v, 'to_string'):
fp.add_printer(v)
# TODO see if there's a way to detect the top-level printers too
# the problem is that some of them collide and the order *is*
# important, but sets and dicts don't preserve order.
# Particularly, 'PointerPrinter' must come after 'FastPrinters'.
obj = gdb.current_objfile()
if obj is None:
obj = gdb
filename = '<unknown>'
else:
filename = obj.filename
obj.pretty_printers.append(fp)
n = len(obj.pretty_printers)
obj.pretty_printers.append(ep)
obj.pretty_printers.append(ptrp)
n = len(obj.pretty_printers) - n
print('Added %d+%d custom printers for %s'
% (len(fp.printers), n, filename))
class EnumPrinter(object):
__slots__ = ('_value')
name = 'enum-class'
enabled = True
def __new__(cls, v):
type = get_basic_type(v.type)
if type.code != gdb.TYPE_CODE_ENUM:
return None
return object.__new__(cls)
def __init__(self, v):
self._value = v
def to_string(self):
v = self._value
self.__class__.enabled = False
try:
name = str(v)
finally:
self.__class__.enabled = True
name = name.split('::')[-1]
scope = get_basic_type(v.type).tag
return '%s::%s' % (scope, name)
class PointerPrinter(object):
__slots__ = ('_value')
name = 'any-symbol-pointer'
enabled = True
def __new__(cls, v):
type = get_basic_type(v.type)
if type.code != gdb.TYPE_CODE_PTR:
return None
return object.__new__(cls)
def __init__(self, v):
self._value = v
def to_string(self):
v = self._value
addr = int(v.cast(gdb.lookup_type('uintptr_t')))
if not addr:
s = 'nullptr'
else:
try:
sym, off, sec, lib = info_symbol(addr)
except:
s = '<heap 0x%x>' % addr
else:
if off:
s = '<%s+%d>' % off
else:
s = '<%s>' % sym
# TODO should I add (type *) ?
return s
class FastPrinters(object):
''' printer dispatch the way gdb *should* have done it
'''
__slots__ = ('name', 'enabled', 'printers')
def __init__(self):
self.name = 'tmwa'
self.enabled = True
self.printers = {}
def add_printer(self, cls):
assert hasattr(cls, 'enabled')
# TODO: check if the class name exists
# this is really hard since templates are involved
self.printers[(cls.name, getattr(cls, 'depth', 0))] = cls
@property
def subprinters(self):
return list(self.printers.values())
def strip_templates(self, name, __pattern=re.compile('<[^<>]*>')):
# TODO what about '<' and '>' as non-type template parameters?
changed = 1
while changed:
name, changed = __pattern.subn('', name)
return name
def get_tag_and_depth(self, type):
depth = 0
while True:
type = get_basic_type(type)
if type.code != gdb.TYPE_CODE_PTR:
break
type = type.target()
depth += 1
return (str(type), depth)
def __call__(self, value):
(stype, depth) = self.get_tag_and_depth(value.type)
#(dtype, _) = self.get_tag_and_depth(value.dynamic_type)
if stype is None:
return
stype = self.strip_templates(stype)
p = self.printers.get((stype, depth))
if p is not None and p.enabled:
return p(value)
return None
class char(object):
__slots__ = ('_value')
name = 'char'
depth = 1
enabled = True
def __init__(self, value):
self._value = value
def to_string(self):
return self._value.lazy_string()
|