-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheckdebuginfo.py
388 lines (323 loc) · 12.4 KB
/
checkdebuginfo.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
######################################################################
# Analyse debug output
######################################################################
# Create the debug output by setting the debug_flag to True (see __init__.py)
# and calling "viewer.py <inputfile> 2>err.log".
#
# checkdebuginfo alloc - Checks allocations/deallocations
#
# checkdebuginfo deps - Check dependency cleanup
#
# checkdebuginfo dot >dot.txt - Create a dot file (dot -Tpng dot.txt -odot.png)
#
######################################################################
import sys, os, optparse, random, colorsys, fnmatch
# InfoReader
class InfoReader:
def __init__(self, filename):
self.filename = filename
self.fhandle = file(filename)
# A dictionary with class "names". Key is the address as string.
self.objectname = {}
self.slotname = {}
def read(self):
self.onBegin()
for s in self.fhandle:
n = s.find(":")
if n!=-1:
addr = s[:n]
info = s[n+1:].strip()
n = info.find("::")
classname = info[:n]
info = info[n+2:]
n = info.find("(")
methodname = info[:n]
args = self.getArgs(info[n:])
self.findInstanceName(addr, classname, methodname, args)
self.line = s.strip()
self.onMethodCall(addr, classname, methodname, args)
self.onEnd()
def getArgs(self, s):
res = []
bracket = 0
arg = ""
for c in s:
if c=='(':
bracket+=1
arg=""
elif c==')':
bracket-=1
if bracket==0:
res.append(arg.strip())
break
elif c==',' and bracket==1:
res.append(arg.strip())
arg=""
else:
arg+=c
return res
# instanceName
def instanceName(self, addr):
"""Return the name of an instance.
addr is the address of the object.
"""
if addr in self.slotname:
return self.slotname[addr]
elif addr in self.objectname:
return self.objectname[addr]
else:
return "?_"+addr
def findInstanceName(self, addr, classname, methodname, args):
rawclassname = classname.split("<")[0]
# Constructor?
if rawclassname==methodname:
if addr not in self.objectname:
name = "%s(%s)"%(methodname, ", ".join(args))
# Check if the component name already exists and add the
# address if it does
if classname=="Component":
for k in self.objectname:
if self.objectname[k]==name:
name = name+addr
break
self.objectname[addr] = name
# addSlot()?
elif methodname=="addSlot":
slotname, slotaddr = args
slotname = slotname[1:-1]
compname = self.objectname[addr]
self.slotname[slotaddr] = compname+"."+slotname
def onMethodCall(self, addr, classname, methodname, args):
"""Method callback.
This callback is called for every method call.
addr is the address of the object whose method was called.
classname is the name of the class and methodname the name
of the method that was called. args is a list of arguments.
The current line is in self.line
"""
pass
def onBegin(self):
"""Initial callback.
This callback is called before reading starts.
"""
pass
def onEnd(self):
"""Final callback.
This callback is called after the file was read.
"""
pass
######################################################################
# AllocDeallocChecker
class AllocDeallocChecker(InfoReader):
"""Checks if allocs/deallocs balances.
This class checks constructor/destructor calls to see if every
object created is also deleted in the end.
"""
def __init__(self, filename):
InfoReader.__init__(self, filename)
self.numobjects = 0
self.instances = {}
def onMethodCall(self, addr, classname, methodname, args):
classname = classname.split("<")[0]
if methodname==classname:
if addr not in self.instances:
self.instances[addr] = 0
self.numobjects += 1
self.instances[addr]+=1
elif methodname=="~"+classname:
if addr not in self.instances:
print "%s: %s: Destructor without constructor!"%(addr, classname)
else:
self.instances[addr]-=1
if self.instances[addr]==0:
del self.instances[addr]
def onEnd(self):
for k in self.instances:
print "Object %s at %s not deleted"%(self.instanceName(k), k)
print self.numobjects, "objects created,",len(self.instances), "objects not deleted!"
######################################################################
class CheckDependencies(InfoReader):
"""Checks dependencies.
This class checks if established dependencies are removed in the end.
"""
def __init__(self, filename):
InfoReader.__init__(self, filename)
# Key: Addr Value:List of dependents
self.deps = {}
def onMethodCall(self, addr, classname, methodname, args):
if classname=="Slot<T>":
if methodname=="addDependent":
if addr not in self.deps:
self.deps[addr] = []
if args[0] in self.deps[addr]:
print "Object %s is added to %s twice!"%(args[0], addr)
self.deps[addr].append(args[0])
elif methodname=="removeDependent":
if addr not in self.deps:
print addr, "removeDependent() on empty list!"
try:
self.deps[addr].remove(args[0])
except ValueError:
print "%s: removeDependent(%s) without addDependent()!"%(addr, args[0])
print " %s: %s"%(addr, self.instanceName(addr))
print " %s: %s"%(args[0], self.instanceName(args[0]))
def onEnd(self):
for k in self.deps:
if self.deps[k]!=[]:
print "%s: %d dependencies not removed:"%(k, len(self.deps[k])),
print self.deps[k]
print " %s: %s"%(k, self.instanceName(k))
for a in self.deps[k]:
print " %s: %s"%(a, self.instanceName(a))
######################################################################
class MakeDot(InfoReader):
"""Creates a dot description file of the dependency graph.
checkdebuginfo.py dot >dot.txt
dot -Tpng dot.txt -odot.png
"""
def __init__(self, filename):
InfoReader.__init__(self, filename)
# Key: Addr Value:List of dependents
self.deps = {}
def onMethodCall(self, addr, classname, methodname, args):
if methodname=="addDependent":
if addr not in self.deps:
self.deps[addr] = []
if args[0] in self.deps[addr]:
print "Object %s is added to %s twice!"%(args[0], addr)
self.deps[addr].append(args[0])
def onEnd(self):
# The colors for the components
colors = ["oldlace","lightblue", "lightpink", "palegreen",
"lightgray", "plum", "lightgoldenrod", "tan", "bisque"]
# ignored = ["WorldRoot_inertiatensor", "WorldRoot_cog",
# "WorldRoot_totalmass", "WorldRoot_worldtransform"]
ignored = ["WorldRoot_*", "TargetCamera_*", "_lookat_*"]
# ignored = []
# Remove dependencies from/to ignored slots
remove = []
for addr in self.deps:
# Is addr an ignored slot?
name = self.instanceName(addr)
name = self.instanceNameToNodeName(name)
removed = False
for pattern in ignored:
if fnmatch.fnmatch(name, pattern):
remove.append(addr)
removed = True
break
if removed:
continue
# Does the dependencies go to ignored slots?
newdeps = []
for da in self.deps[addr]:
name = self.instanceName(da)
name = self.instanceNameToNodeName(name)
ok = True
for pattern in ignored:
if fnmatch.fnmatch(name, pattern):
ok = False
break
if ok:
newdeps.append(da)
self.deps[addr] = newdeps
if newdeps==[]:
remove.append(addr)
for a in remove:
del self.deps[a]
# Make a dictionary that contains all objects present in this graph...
# -> nodes
nodes = {}
for addr in self.deps:
nodes[addr] = 1
for da in self.deps[addr]:
nodes[da] = 1
# Output the graph...
print "digraph G {"
# "Declare" all nodes and set their attributes...
cols = {}
for a in nodes:
name = self.instanceName(a)
name = self.instanceNameToNodeName(name)
comp = "_".join(name.split("_")[:-1])
if comp not in cols:
if len(colors)>0:
col = colors[0]
del colors[0]
else:
col = "white"
cols[comp] = col
else:
col = cols[comp]
n = name.rfind("_")
if n!=-1:
label = name[:n]+"."+name[n+1:]
else:
label = name
print ' %s [style=filled, fillcolor=%s, label="%s\\n(%s)"]'%(name, col, label, a)
print ""
# Output the edges...
for addr in self.deps:
for da in self.deps[addr]:
name1 = self.instanceName(da)
name2 = self.instanceName(addr)
name1 = self.instanceNameToNodeName(name1)
name2 = self.instanceNameToNodeName(name2)
print " %s -> %s;"%(name2, name1)
print "}"
def instanceNameToNodeName(self, name):
"""Convert an instance name to a valid dot node name."""
name = name.replace(" ", "")
name = name.replace("Component", "")
name = name.replace("(", "").replace(")", "")
name = name.replace("\"", "")
name = name.replace(".", "_")
name = name.replace("?", "_")
name = name.replace(",", "_")
name = name.replace("=", "_")
return name
######################################################################
# AddrTranslator
class AddrTranslator(InfoReader):
"""
"""
def __init__(self, filename):
InfoReader.__init__(self, filename)
self.prev_instance = ""
self.addrs = {}
def onMethodCall(self, addr, classname, methodname, args):
inst = self.instanceName(addr)
self.addrs[addr]=inst
if inst!=self.prev_instance:
print 70*"-"
print "INSTANCE:",self.instanceName(addr)
print ""
self.prev_instance = inst
print self.line
def onEnd(self):
print "\nWriting file err_names.log..."
f = file("err_names.log", "w")
for a in self.addrs:
print >>f, a, "->", self.addrs[a]
f.close()
######################################################################
cmd = ""
usage = """usage: %prog [options] command
commands: alloc, deps, dot"""
parser = optparse.OptionParser(usage)
parser.add_option("-l", "--logfile", metavar="NAME", help="The name of the debug info file (default: err.log)", default="err.log")
opts, args = parser.parse_args()
if len(args)>0:
cmd = args[0]
if cmd=="alloc":
reader = AllocDeallocChecker(opts.logfile)
elif cmd=="deps":
reader = CheckDependencies(opts.logfile)
elif cmd=="dot":
reader = MakeDot(opts.logfile)
elif cmd=="translate":
reader = AddrTranslator(opts.logfile)
else:
parser.print_help()
sys.exit(1)
reader.read()