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
35 from highlight import ColorHighlighter, LessHighlighter
38 ignoredFunctionNames = set([
41 'glXGetCurrentDisplay',
43 'glXGetProcAddressARB',
48 class Loader(Unpickler):
50 def __init__(self, stream):
51 Unpickler.__init__(self, stream)
54 def handleCall(self, call):
55 if call.functionName not in ignoredFunctionNames:
56 self.calls.append(call)
60 def readtrace(trace, calls):
69 stdout = subprocess.PIPE,
73 parser = Loader(p.stdout)
80 def __init__(self, a, b, highlighter, callNos = False):
83 self.highlighter = highlighter
84 self.delete_color = highlighter.red
85 self.insert_color = highlighter.green
86 self.callNos = callNos
89 matcher = difflib.SequenceMatcher(self.isjunk, self.a, self.b)
90 for tag, alo, ahi, blo, bhi in matcher.get_opcodes():
92 self.replace(alo, ahi, blo, bhi)
98 self.equal(alo, ahi, blo, bhi)
100 raise ValueError, 'unknown tag %s' % (tag,)
102 def isjunk(self, call):
103 return call.functionName == 'glGetError' and call.ret in ('GL_NO_ERROR', 0)
105 def replace(self, alo, ahi, blo, bhi):
106 assert alo < ahi and blo < bhi
108 a_names = [call.functionName for call in self.a[alo:ahi]]
109 b_names = [call.functionName for call in self.b[blo:bhi]]
111 matcher = difflib.SequenceMatcher(None, a_names, b_names)
112 for tag, _alo, _ahi, _blo, _bhi in matcher.get_opcodes():
118 self.replace_dissimilar(_alo, _ahi, _blo, _bhi)
119 elif tag == 'delete':
120 self.delete(_alo, _ahi)
121 elif tag == 'insert':
122 self.insert(_blo, _bhi)
124 self.replace_similar(_alo, _ahi, _blo, _bhi)
126 raise ValueError, 'unknown tag %s' % (tag,)
128 def replace_similar(self, alo, ahi, blo, bhi):
129 assert alo < ahi and blo < bhi
130 assert ahi - alo == bhi - blo
131 for i in xrange(0, bhi - blo):
132 a_call = self.a[alo + i]
133 b_call = self.b[blo + i]
134 assert a_call.functionName == b_call.functionName
135 assert len(a_call.args) == len(b_call.args)
136 self.replace_prefix()
138 self.replace_value(a_call.no, b_call.no)
139 self.highlighter.write(' ')
140 self.highlighter.bold(True)
141 self.highlighter.write(b_call.functionName)
142 self.highlighter.bold(False)
143 self.highlighter.write('(')
145 for j in xrange(len(b_call.args)):
146 self.highlighter.write(sep)
147 self.replace_value(a_call.args[j], b_call.args[j])
149 self.highlighter.write(')')
150 if a_call.ret is not None or b_call.ret is not None:
151 self.highlighter.write(' = ')
152 self.replace_value(a_call.ret, b_call.ret)
153 self.highlighter.write('\n')
155 def replace_dissimilar(self, alo, ahi, blo, bhi):
156 assert alo < ahi and blo < bhi
157 if bhi - blo < ahi - alo:
158 first = self.insert(blo, bhi)
159 second = self.delete(alo, ahi)
161 first = self.delete(alo, ahi)
162 second = self.insert(blo, bhi)
164 for g in first, second:
168 def replace_value(self, a, b):
170 self.highlighter.write(str(b))
172 self.highlighter.strike()
173 self.highlighter.color(self.delete_color)
174 self.highlighter.write(str(a))
175 self.highlighter.normal()
176 #self.highlighter.write(" -> ")
177 self.highlighter.write(" ")
178 self.highlighter.color(self.insert_color)
179 self.highlighter.write(str(b))
180 self.highlighter.normal()
184 def delete(self, alo, ahi):
185 self.dump(self.delete_prefix, self.a, alo, ahi, self.normal_suffix)
187 def insert(self, blo, bhi):
188 self.dump(self.insert_prefix, self.b, blo, bhi, self.normal_suffix)
190 def equal(self, alo, ahi, blo, bhi):
192 self.replace_similar(alo, ahi, blo, bhi)
194 self.dump(self.equal_prefix, self.b, blo, bhi, self.normal_suffix)
196 def dump(self, prefix, x, lo, hi, suffix):
197 for i in xrange(lo, hi):
201 self.highlighter.write(str(call.no) + ' ')
202 self.highlighter.bold(True)
203 self.highlighter.write(call.functionName)
204 self.highlighter.bold(False)
205 self.highlighter.write('(' + ', '.join(map(repr, call.args)) + ')')
206 if call.ret is not None:
207 self.highlighter.write(' = ' + repr(call.ret))
209 self.highlighter.write('\n')
211 def delete_prefix(self):
212 self.highlighter.write('- ')
213 self.highlighter.strike()
214 self.highlighter.color(self.delete_color)
216 def insert_prefix(self):
217 self.highlighter.write('+ ')
218 self.highlighter.color(self.insert_color)
220 def equal_prefix(self):
221 self.highlighter.write(' ')
223 def normal_suffix(self):
224 self.highlighter.normal()
226 def replace_prefix(self):
227 self.highlighter.write('| ')
234 # Parse command line options
235 optparser = optparse.OptionParser(
236 usage='\n\t%prog <trace> <trace>',
238 optparser.add_option(
239 '-a', '--apitrace', metavar='PROGRAM',
240 type='string', dest='apitrace', default='apitrace',
241 help='apitrace command [default: %default]')
242 optparser.add_option(
243 '-c', '--calls', metavar='CALLSET',
244 type="string", dest="calls", default='1-10000',
245 help="calls to compare [default: %default]")
246 optparser.add_option(
247 '--ref-calls', metavar='CALLSET',
248 type="string", dest="ref_calls", default=None,
249 help="calls to compare from reference trace")
250 optparser.add_option(
251 '--src-calls', metavar='CALLSET',
252 type="string", dest="src_calls", default=None,
253 help="calls to compare from source trace")
254 optparser.add_option(
257 dest="call_nos", default=False,
258 help="dump call numbers")
260 (options, args) = optparser.parse_args(sys.argv[1:])
262 optparser.error("incorrect number of arguments")
264 if options.ref_calls is None:
265 options.ref_calls = options.calls
266 if options.src_calls is None:
267 options.src_calls = options.calls
269 ref_calls = readtrace(args[0], options.ref_calls)
270 src_calls = readtrace(args[1], options.src_calls)
272 if sys.stdout.isatty():
273 highlighter = LessHighlighter()
275 highlighter = ColorHighlighter()
277 differ = SDiffer(ref_calls, src_calls, highlighter, options.call_nos)
281 if __name__ == '__main__':