-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake-gdb.py
175 lines (141 loc) · 4.91 KB
/
make-gdb.py
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
import re
import gdb #pylint: disable=import-error
import gdb.printing #pylint: disable=import-error
# Memoize types we commonly use
_TYPES = {}
def getType(tname):
global _TYPES
if tname not in _TYPES:
tn = tname.rstrip('*')
if tn not in _TYPES:
_TYPES[tn] = gdb.lookup_type(tn)
while tn != tname:
# Want a pointer type
t = tn
tn += '*'
_TYPES[tn] = _TYPES[t].pointer()
return _TYPES[tname]
class ShowArgv(gdb.Function):
"""Return the pretty-print of a null-terminated array of strings
Argument:
A char** where the last one is NULL (e.g., argv)
"""
def __init__(self):
gdb.Function.__init__(self, "showargv")
def invoke(self, argv):
str = '['
i = 0
while argv[i] != 0:
if i > 0:
str += ', '
str += argv[i].string()
i += 1
str += ']'
return str
ShowArgv()
class FileLocation(object):
"""Print a file location"""
def __init__(self, val):
self.val = val
def to_string(self):
if long(self.val['filenm']):
return "%s:%d" % (str(self.val['filenm']), self.val['lineno'])
return ''
class VariablePrinter(object):
"""Print a struct variable"""
def __init__(self, val):
self.val = val
def to_string(self):
if self.val['append']:
a = '+='
elif self.val['conditional']:
a = '?='
else:
a = '='
flags = []
s = str(self.val['flavor'])
if s != 'f_bogus':
flags.append(s)
s = str(self.val['origin'])
if s != 'o_default':
flags.append(s)
s = str(self.val['export'])
if s != 'v_default':
flags.append(s)
return '%s[%s]: "%s" %s "%s"' % (
self.val['fileinfo'], ','.join(flags),
self.val['name'].string(), a, self.val['value'].string())
class HashTablePrinter(object):
"""Manage a hash table."""
DELITEM = None
def __init__(self, val):
self.val = val
def to_string(self):
return "size=%d, capacity=%d, empty=%d, collisions=%d, rehashes=%d" % (
self.val['ht_size'], self.val['ht_capacity'],
self.val['ht_empty_slots'], self.val['ht_collisions'],
self.val['ht_rehashes'])
def children(self):
for (i, v) in self.iterator():
nm = '[%d] ' % i
yield (nm, i)
yield (nm, v)
def iterator(self):
if HashTablePrinter.DELITEM is None:
HashTablePrinter.DELITEM = gdb.lookup_global_symbol('hash_deleted_item').value()
lst = self.val['ht_vec']
for i in xrange(0, self.val['ht_size']):
v = lst[i]
if long(v) != 0 and v != HashTablePrinter.DELITEM:
yield (i, v)
def display_hint(self):
return 'map'
class VariableSetPrinter(object):
"""Print a variable_set"""
def __init__(self, val):
self.tbl = HashTablePrinter(val['table'])
def to_string(self):
return self.tbl.to_string()
def children(self):
for (i, v) in self.tbl.iterator():
ptr = v.cast(getType('struct variable*'))
nm = '[%d] ' % (i)
yield (nm, ptr)
yield (nm, str(ptr.dereference()))
def display_hint(self):
return 'map'
class VariableSetListPrinter(object):
"""Print a variable_set_list"""
GLOBALSET = None
def __init__(self, val):
self.val = val
def to_string(self):
return str(self.val.address)
def children(self):
if VariableSetListPrinter.GLOBALSET is None:
block = gdb.lookup_global_symbol('init_hash_global_variable_set').symtab.static_block()
VariableSetListPrinter.GLOBALSET = gdb.lookup_symbol('global_variable_set', block)[0].value().address
ptr = self.val.address
i = 0
while long(ptr) != 0:
nm = '[%d] ' % (i)
yield (nm, ptr['set'])
if long(ptr['set']) == long(VariableSetListPrinter.GLOBALSET):
yield (nm, "global_variable_set")
else:
yield (nm, str(ptr['set'].dereference()))
i += 1
ptr = ptr['next']
def display_hint(self):
return 'map'
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("gnumake")
pp.add_printer('floc', r'^floc$', FileLocation)
pp.add_printer('variable', r'^variable$', VariablePrinter)
pp.add_printer('hashtable', r'^hash_table$', HashTablePrinter)
pp.add_printer('variableset', r'^variable_set$', VariableSetPrinter)
pp.add_printer('variablesetlist', r'^variable_set_list$', VariableSetListPrinter)
return pp
# Use replace=True so we can re-source this file
gdb.printing.register_pretty_printer(gdb.current_objfile(),
build_pretty_printer(), replace=True)