blob: 706e60a912e7bd486cba5ba154139b928d8d2d94 [file] [log] [blame]
Jose Fonseca247e1fa2019-04-28 14:14:44 +01001#!/usr/bin/env python3
Jose Fonseca731ccce2016-01-19 12:36:27 +00002##########################################################################
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]
Jose Fonseca247e1fa2019-04-28 14:14:44 +010042 p = subprocess.Popen(args = cmd, stdout=subprocess.PIPE)
Jose Fonseca731ccce2016-01-19 12:36:27 +000043
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',
Bruno de Oliveira Abinader73dc6022017-10-18 17:03:27 -070089 'glXCreateNewContext',
Jose Fonsecad1caa422016-01-19 14:52:49 +000090 'glXCreateContextAttribsARB',
91 'glXCreateContextWithConfigSGIX',
92 'wglCreateContext',
93 'wglCreateContextAttribsARB',
94 ]:
95 # FIXME: Ignore failing context creation calls
96 self.numContexts += 1
97
98 if call.functionName in [
99 'CGLDestroyContext',
Jose Fonseca731ccce2016-01-19 12:36:27 +0000100 'glXDestroyContext',
101 'eglDestroyContext',
102 'wglDeleteContext',
103 ]:
Jose Fonsecad1caa422016-01-19 14:52:49 +0000104 assert self.numContexts > 0
105 self.numContexts -= 1
106 if self.numContexts == 0:
107 self.dumpLeaks(call.no)
Jose Fonseca731ccce2016-01-19 12:36:27 +0000108
Jose Fonseca771c34f2016-01-19 14:41:55 +0000109 def handleGenerate(self, call, objectDict):
110 n, names = call.argValues()
111 for i in range(n):
112 name = names[i]
113 objectDict[name] = call.no
Jose Fonsecad1caa422016-01-19 14:52:49 +0000114 # TODO: Keep track of call stack backtrace too
Jose Fonseca771c34f2016-01-19 14:41:55 +0000115
116 def handleDelete(self, call, objectDict):
117 n, names = call.argValues()
118 for i in range(n):
119 name = names[i]
120 try:
121 del objectDict[name]
122 except KeyError:
123 # Ignore if texture name was never generated
124 pass
125
Jose Fonsecad1caa422016-01-19 14:52:49 +0000126 def dumpLeaks(self, currentCallNo):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100127 for kind, objectDict in self.objectDicts.items():
Jose Fonseca771c34f2016-01-19 14:41:55 +0000128 self.dumpNamespaceLeaks(currentCallNo, objectDict, kind)
129
130 def dumpNamespaceLeaks(self, currentCallNo, objectDict, kind):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100131 for name, creationCallNo in (sorted(iter(objectDict.items()),key=lambda t: t[1])):
Jose Fonseca771c34f2016-01-19 14:41:55 +0000132 sys.stderr.write('%u: error: %s %u was not destroyed until %s\n' % (creationCallNo, kind, name, currentCallNo))
133 objectDict.clear()
Jose Fonseca731ccce2016-01-19 12:36:27 +0000134
135
136def main():
137 '''Main program.
138 '''
139
140 # Parse command line options
141 optparser = optparse.OptionParser(
142 usage='\n\t%prog [options] TRACE',
143 version='%%prog')
144 optparser.add_option(
145 '-a', '--apitrace', metavar='PROGRAM',
146 type='string', dest='apitrace', default='apitrace',
147 help='apitrace command [default: %default]')
148
149 options, args = optparser.parse_args(sys.argv[1:])
150 if len(args) != 1:
151 optparser.error("incorrect number of arguments")
152
153 inTrace = args[0]
154 if not os.path.isfile(inTrace):
155 sys.stderr.write("error: `%s` does not exist\n" % inTrace)
156 sys.exit(1)
157
158 detector = LeakDetector(options.apitrace, inTrace)
159 detector.parse()
160
161
162if __name__ == '__main__':
163 main()