2 ##########################################################################
4 # Copyright 2011 Jose Fonseca
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
34 from unpickle import Unpickler
37 ignoredFunctionNames = set([
40 'glXGetCurrentDisplay',
42 'glXGetProcAddressARB',
47 class Loader(Unpickler):
49 def __init__(self, stream):
50 Unpickler.__init__(self, stream)
53 def handleCall(self, call):
56 if call.functionName not in ignoredFunctionNames:
57 self.calls.append(call)
65 '--calls=' + options.calls,
68 stdout = subprocess.PIPE,
72 parser = Loader(p.stdout)
79 def __init__(self, a, b):
84 matcher = difflib.SequenceMatcher(None, self.a, self.b)
85 for tag, alo, ahi, blo, bhi in matcher.get_opcodes():
87 self.replace(alo, ahi, blo, bhi)
95 raise ValueError, 'unknown tag %s' % (tag,)
97 def replace(self, alo, ahi, blo, bhi):
98 assert alo < ahi and blo < bhi
100 a_names = [call.functionName for call in self.a[alo:ahi]]
101 b_names = [call.functionName for call in self.b[blo:bhi]]
103 matcher = difflib.SequenceMatcher(None, a_names, b_names)
104 for tag, _alo, _ahi, _blo, _bhi in matcher.get_opcodes():
110 self.replace_dissimilar(_alo, _ahi, _blo, _bhi)
111 elif tag == 'delete':
112 self.delete(_alo, _ahi)
113 elif tag == 'insert':
114 self.insert(_blo, _bhi)
116 self.replace_similar(_alo, _ahi, _blo, _bhi)
118 raise ValueError, 'unknown tag %s' % (tag,)
120 def replace_similar(self, alo, ahi, blo, bhi):
121 assert alo < ahi and blo < bhi
122 assert ahi - alo == bhi - blo
123 for i in xrange(0, bhi - blo):
124 a_call = self.a[alo + i]
125 b_call = self.b[blo + i]
126 assert a_call.functionName == b_call.functionName
127 assert len(a_call.args) == len(b_call.args)
128 sys.stdout.write(' ' + b_call.functionName + '(')
130 for j in xrange(len(b_call.args)):
131 sys.stdout.write(sep)
132 self.replace_value(a_call.args[j], b_call.args[j])
134 sys.stdout.write(')')
135 if a_call.ret is not None or b_call.ret is not None:
136 sys.stdout.write(' = ')
137 self.replace_value(a_call.ret, b_call.ret)
138 sys.stdout.write('\n')
140 def replace_dissimilar(self, alo, ahi, blo, bhi):
141 assert alo < ahi and blo < bhi
142 if bhi - blo < ahi - alo:
143 first = self.insert(blo, bhi)
144 second = self.delete(alo, ahi)
146 first = self.delete(alo, ahi)
147 second = self.insert(blo, bhi)
149 for g in first, second:
153 def replace_value(self, a, b):
155 sys.stdout.write(str(b))
157 sys.stdout.write('%s -> %s' % (a, b))
161 def delete(self, alo, ahi):
162 self.dump('- ' + self.escape + '9m', self.a, alo, ahi, self.escape + '0m')
164 def insert(self, blo, bhi):
165 self.dump('+ ', self.b, blo, bhi)
167 def equal(self, alo, ahi):
168 self.dump(' ' + self.escape + '2m', self.a, alo, ahi, self.escape + '0m')
170 def dump(self, prefix, x, lo, hi, suffix=""):
171 for i in xrange(lo, hi):
172 sys.stdout.write(prefix + str(x[i]) + suffix + '\n')
179 # Parse command line options
180 optparser = optparse.OptionParser(
181 usage='\n\t%prog <trace> <trace>',
183 optparser.add_option(
184 '-a', '--apitrace', metavar='PROGRAM',
185 type='string', dest='apitrace', default='apitrace',
186 help='apitrace command [default: %default]')
187 optparser.add_option(
188 '-c', '--calls', metavar='CALLSET',
189 type="string", dest="calls", default='1-10000',
190 help="calls to compare [default: %default]")
193 (options, args) = optparser.parse_args(sys.argv[1:])
195 optparser.error("incorrect number of arguments")
197 ref_calls = readtrace(args[0])
198 src_calls = readtrace(args[1])
200 differ = SDiffer(ref_calls, src_calls)
204 if __name__ == '__main__':