blob: 0459060c2c8f8c734fc2db6b7cb652d240b62faf [file] [log] [blame]
Jose Fonseca731ccce2016-01-19 12:36:27 +00001#!/usr/bin/env python
2##########################################################################
3#
4# Copyright 2014-2016 VMware, Inc.
5# All Rights Reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24#
25##########################################################################/
26
27
28import subprocess
29import sys
30import os.path
31import optparse
Jose Fonseca771c34f2016-01-19 14:41:55 +000032import re
Jose Fonseca731ccce2016-01-19 12:36:27 +000033
34import unpickle
35
36
Jose Fonseca731ccce2016-01-19 12:36:27 +000037class LeakDetector(unpickle.Unpickler):
38
39 def __init__(self, apitrace, trace):
40
41 cmd = [apitrace, 'pickle', '--symbolic', trace]
42 p = subprocess.Popen(args = cmd, stdout = subprocess.PIPE)
43
44 unpickle.Unpickler.__init__(self, p.stdout)
45
Jose Fonsecad1caa422016-01-19 14:52:49 +000046 self.numContexts = 0
47
48 # a map of maps
49 self.objectDicts = {}
Jose Fonseca731ccce2016-01-19 12:36:27 +000050
51 def parse(self):
52 unpickle.Unpickler.parse(self)
53
54 # Reached the end of the trace -- dump any live objects
Jose Fonsecad1caa422016-01-19 14:52:49 +000055 self.dumpLeaks("<EOF>")
Jose Fonseca731ccce2016-01-19 12:36:27 +000056
Jose Fonseca771c34f2016-01-19 14:41:55 +000057 genDelRegExp = re.compile('^gl(Gen|Delete)(Buffers|Textures|FrameBuffers|RenderBuffers)[A-Z]*$')
58
Jose Fonseca731ccce2016-01-19 12:36:27 +000059 def handleCall(self, call):
60 # Ignore calls without side effects
61 if call.flags & unpickle.CALL_FLAG_NO_SIDE_EFFECTS:
62 return
63
64 # Dump call for debugging:
Jose Fonseca771c34f2016-01-19 14:41:55 +000065 if 0:
66 sys.stderr.write('%s\n' % call)
Jose Fonseca731ccce2016-01-19 12:36:27 +000067
Jose Fonseca771c34f2016-01-19 14:41:55 +000068 mo = self.genDelRegExp.match(call.functionName)
69 if mo:
70 verb = mo.group(1)
71 subject = mo.group(2)
Jose Fonseca731ccce2016-01-19 12:36:27 +000072
Jose Fonseca771c34f2016-01-19 14:41:55 +000073 subject = subject.lower().rstrip('s')
Jose Fonsecad1caa422016-01-19 14:52:49 +000074 objectDict = self.objectDicts.setdefault(subject, {})
Jose Fonseca771c34f2016-01-19 14:41:55 +000075
76 if verb == 'Gen':
77 self.handleGenerate(call, objectDict)
78 elif verb == 'Delete':
79 self.handleDelete(call, objectDict)
80 else:
81 assert 0
Jose Fonseca731ccce2016-01-19 12:36:27 +000082
Jose Fonsecad1caa422016-01-19 14:52:49 +000083 # TODO: Track labels via glObjectLabel* calls
84
Jose Fonseca731ccce2016-01-19 12:36:27 +000085 if call.functionName in [
Jose Fonsecad1caa422016-01-19 14:52:49 +000086 'CGLCreateContext',
87 'eglCreateContext',
88 'glXCreateContext',
89 'glXCreateContextAttribsARB',
90 'glXCreateContextWithConfigSGIX',
91 'wglCreateContext',
92 'wglCreateContextAttribsARB',
93 ]:
94 # FIXME: Ignore failing context creation calls
95 self.numContexts += 1
96
97 if call.functionName in [
98 'CGLDestroyContext',
Jose Fonseca731ccce2016-01-19 12:36:27 +000099 'glXDestroyContext',
100 'eglDestroyContext',
101 'wglDeleteContext',
102 ]:
Jose Fonsecad1caa422016-01-19 14:52:49 +0000103 assert self.numContexts > 0
104 self.numContexts -= 1
105 if self.numContexts == 0:
106 self.dumpLeaks(call.no)
Jose Fonseca731ccce2016-01-19 12:36:27 +0000107
Jose Fonseca771c34f2016-01-19 14:41:55 +0000108 def handleGenerate(self, call, objectDict):
109 n, names = call.argValues()
110 for i in range(n):
111 name = names[i]
112 objectDict[name] = call.no
Jose Fonsecad1caa422016-01-19 14:52:49 +0000113 # TODO: Keep track of call stack backtrace too
Jose Fonseca771c34f2016-01-19 14:41:55 +0000114
115 def handleDelete(self, call, objectDict):
116 n, names = call.argValues()
117 for i in range(n):
118 name = names[i]
119 try:
120 del objectDict[name]
121 except KeyError:
122 # Ignore if texture name was never generated
123 pass
124
Jose Fonsecad1caa422016-01-19 14:52:49 +0000125 def dumpLeaks(self, currentCallNo):
126 for kind, objectDict in self.objectDicts.iteritems():
Jose Fonseca771c34f2016-01-19 14:41:55 +0000127 self.dumpNamespaceLeaks(currentCallNo, objectDict, kind)
128
129 def dumpNamespaceLeaks(self, currentCallNo, objectDict, kind):
130 for name, creationCallNo in objectDict.iteritems():
131 sys.stderr.write('%u: error: %s %u was not destroyed until %s\n' % (creationCallNo, kind, name, currentCallNo))
132 objectDict.clear()
Jose Fonseca731ccce2016-01-19 12:36:27 +0000133
134
135def main():
136 '''Main program.
137 '''
138
139 # Parse command line options
140 optparser = optparse.OptionParser(
141 usage='\n\t%prog [options] TRACE',
142 version='%%prog')
143 optparser.add_option(
144 '-a', '--apitrace', metavar='PROGRAM',
145 type='string', dest='apitrace', default='apitrace',
146 help='apitrace command [default: %default]')
147
148 options, args = optparser.parse_args(sys.argv[1:])
149 if len(args) != 1:
150 optparser.error("incorrect number of arguments")
151
152 inTrace = args[0]
153 if not os.path.isfile(inTrace):
154 sys.stderr.write("error: `%s` does not exist\n" % inTrace)
155 sys.exit(1)
156
157 detector = LeakDetector(options.apitrace, inTrace)
158 detector.parse()
159
160
161if __name__ == '__main__':
162 main()