Jose Fonseca | 247e1fa | 2019-04-28 14:14:44 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 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 | |
| 28 | import subprocess |
| 29 | import sys |
| 30 | import os.path |
| 31 | import optparse |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 32 | import re |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 33 | |
| 34 | import unpickle |
| 35 | |
| 36 | |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 37 | class LeakDetector(unpickle.Unpickler): |
| 38 | |
| 39 | def __init__(self, apitrace, trace): |
| 40 | |
| 41 | cmd = [apitrace, 'pickle', '--symbolic', trace] |
Jose Fonseca | 247e1fa | 2019-04-28 14:14:44 +0100 | [diff] [blame] | 42 | p = subprocess.Popen(args = cmd, stdout=subprocess.PIPE) |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 43 | |
| 44 | unpickle.Unpickler.__init__(self, p.stdout) |
| 45 | |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 46 | self.numContexts = 0 |
| 47 | |
| 48 | # a map of maps |
| 49 | self.objectDicts = {} |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 50 | |
| 51 | def parse(self): |
| 52 | unpickle.Unpickler.parse(self) |
| 53 | |
| 54 | # Reached the end of the trace -- dump any live objects |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 55 | self.dumpLeaks("<EOF>") |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 56 | |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 57 | genDelRegExp = re.compile('^gl(Gen|Delete)(Buffers|Textures|FrameBuffers|RenderBuffers)[A-Z]*$') |
| 58 | |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 59 | 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 Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 65 | if 0: |
| 66 | sys.stderr.write('%s\n' % call) |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 67 | |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 68 | mo = self.genDelRegExp.match(call.functionName) |
| 69 | if mo: |
| 70 | verb = mo.group(1) |
| 71 | subject = mo.group(2) |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 72 | |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 73 | subject = subject.lower().rstrip('s') |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 74 | objectDict = self.objectDicts.setdefault(subject, {}) |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 75 | |
| 76 | if verb == 'Gen': |
| 77 | self.handleGenerate(call, objectDict) |
| 78 | elif verb == 'Delete': |
| 79 | self.handleDelete(call, objectDict) |
| 80 | else: |
| 81 | assert 0 |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 82 | |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 83 | # TODO: Track labels via glObjectLabel* calls |
| 84 | |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 85 | if call.functionName in [ |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 86 | 'CGLCreateContext', |
| 87 | 'eglCreateContext', |
| 88 | 'glXCreateContext', |
Bruno de Oliveira Abinader | 73dc602 | 2017-10-18 17:03:27 -0700 | [diff] [blame] | 89 | 'glXCreateNewContext', |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 90 | '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 Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 100 | 'glXDestroyContext', |
| 101 | 'eglDestroyContext', |
| 102 | 'wglDeleteContext', |
| 103 | ]: |
Jose Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 104 | assert self.numContexts > 0 |
| 105 | self.numContexts -= 1 |
| 106 | if self.numContexts == 0: |
| 107 | self.dumpLeaks(call.no) |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 108 | |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 109 | 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 Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 114 | # TODO: Keep track of call stack backtrace too |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 115 | |
| 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 Fonseca | d1caa42 | 2016-01-19 14:52:49 +0000 | [diff] [blame] | 126 | def dumpLeaks(self, currentCallNo): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 127 | for kind, objectDict in self.objectDicts.items(): |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 128 | self.dumpNamespaceLeaks(currentCallNo, objectDict, kind) |
| 129 | |
| 130 | def dumpNamespaceLeaks(self, currentCallNo, objectDict, kind): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 131 | for name, creationCallNo in (sorted(iter(objectDict.items()),key=lambda t: t[1])): |
Jose Fonseca | 771c34f | 2016-01-19 14:41:55 +0000 | [diff] [blame] | 132 | sys.stderr.write('%u: error: %s %u was not destroyed until %s\n' % (creationCallNo, kind, name, currentCallNo)) |
| 133 | objectDict.clear() |
Jose Fonseca | 731ccce | 2016-01-19 12:36:27 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def 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 | |
| 162 | if __name__ == '__main__': |
| 163 | main() |