summaryrefslogtreecommitdiff
path: root/src/gallium/tools/trace/pytracediff.py
blob: 64f2722302d0ef4bea4c102ec9f73ae02d3ab83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
# coding=utf-8
##########################################################################
#
# pytracediff - Compare Gallium XML trace files
# (C) Copyright 2022 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################

from parse import *
import sys
import re
import signal
import functools
import argparse
import difflib

assert sys.version_info >= (3, 6), 'Python >= 3.6 required'


###
### ANSI color codes
###
PKK_ANSI_ESC       = '\33['
PKK_ANSI_NORMAL    = '0m'
PKK_ANSI_RED       = '31m'
PKK_ANSI_GREEN     = '32m'
PKK_ANSI_PURPLE    = '35m'
PKK_ANSI_BOLD      = '1m'


###
### Utility functions and classes
###
def pkk_fatal(msg):
    print(f"ERROR: {msg}", file=sys.stderr)
    sys.exit(1)


def pkk_info(msg):
    print(msg, file=sys.stderr)


def pkk_signal_handler(signal, frame):
    print("\nQuitting due to SIGINT / Ctrl+C!")
    sys.exit(1)


def pkk_arg_range(vstr, vmin, vmax):
    try:
        value = int(vstr)
    except Exception as e:
        raise argparse.ArgumentTypeError(f"value '{vstr}' is not an integer")

    value = int(vstr)
    if value < vmin or value > vmax:
        raise argparse.ArgumentTypeError(f"value {value} not in range {vmin}-{vmax}")
    else:
        return value


class PKKArgumentParser(argparse.ArgumentParser):
    def print_help(self):
        print("pytracediff - Compare Gallium XML trace files\n"
        "(C) Copyright 2022 Matti 'ccr' Hämäläinen <ccr@tnsp.org>\n")
        super().print_help()
        print("\nList of junk calls:")
        for klass, call in sorted(trace_ignore_calls):
            print(f"  {klass}::{call}")

    def error(self, msg):
        self.print_help()
        print(f"\nERROR: {msg}", file=sys.stderr)
        sys.exit(2)


class PKKTraceParser(TraceParser):
    def __init__(self, stream, options, state):
        TraceParser.__init__(self, stream, options, state)
        self.call_stack = []

    def handle_call(self, call):
        self.call_stack.append(call)


class PKKPrettyPrinter(PrettyPrinter):
    def __init__(self, options):
        self.options = options

    def entry_start(self):
        self.data = []
        self.line = ""

    def entry_get(self):
        if self.line != "":
            self.data.append(self.line)
        return self.data

    def text(self, text):
        self.line += text

    def literal(self, text):
        self.line += text

    def function(self, text):
        self.line += text

    def variable(self, text):
        self.line += text

    def address(self, text):
        self.line += text

    def newline(self):
        self.data.append(self.line)
        self.line = ""


    def visit_literal(self, node):
        if node.value is None:
            self.literal("NULL")
        elif isinstance(node.value, str):
            self.literal('"' + node.value + '"')
        else:
            self.literal(repr(node.value))

    def visit_blob(self, node):
        self.address("blob()")

    def visit_named_constant(self, node):
        self.literal(node.name)

    def visit_array(self, node):
        self.text("{")
        sep = ""
        for value in node.elements:
            self.text(sep)
            if sep != "":
                self.newline()
            value.visit(self)
            sep = ", "
        self.text("}")

    def visit_struct(self, node):
        self.text("{")
        sep = ""
        for name, value in node.members:
            self.text(sep)
            if sep != "":
                self.newline()
            self.variable(name)
            self.text(" = ")
            value.visit(self)
            sep = ", "
        self.text("}")

    def visit_pointer(self, node):
        if self.options.named_ptrs:
            self.address(node.named_address())
        else:
            self.address(node.address)

    def visit_call(self, node):
        if not self.options.suppress_variants:
            self.text(f"[{node.no:8d}] ")

        if node.klass is not None:
            self.function(node.klass +"::"+ node.method)
        else:
            self.function(node.method)

        if not self.options.method_only:
            self.text("(")
            if len(node.args):
                self.newline()
                sep = ""
                for name, value in node.args:
                    self.text(sep)
                    if sep != "":
                        self.newline()
                    self.variable(name)
                    self.text(" = ")
                    value.visit(self)
                    sep = ", "
                self.newline()

            self.text(")")

            if node.ret is not None:
                self.text(" = ")
                node.ret.visit(self)

        if not self.options.suppress_variants and node.time is not None:
            self.text(" // time ")
            node.time.visit(self)


def pkk_parse_trace(filename, options, state):
    pkk_info(f"Parsing {filename} ...")
    try:
        if filename.endswith(".gz"):
            from gzip import GzipFile
            stream = io.TextIOWrapper(GzipFile(filename, "rb"))
        elif filename.endswith(".bz2"):
            from bz2 import BZ2File
            stream = io.TextIOWrapper(BZ2File(filename, "rb"))
        else:
            stream = open(filename, "rt")

    except OSError as e:
        pkk_fatal(str(e))

    parser = PKKTraceParser(stream, options, state)
    parser.parse()

    return parser.call_stack


def pkk_get_line(data, nline, indent, width):
    if nline < len(data):
        tmp = indent + data[nline]
        if len(tmp) > width:
            return tmp[0:(width - 3)] + "..."
        else:
            return tmp
    else:
        return ""


###
### Main program starts
###
if __name__ == "__main__":
    signal.signal(signal.SIGINT, pkk_signal_handler)

    ### Parse arguments
    optparser = PKKArgumentParser(
        usage="%(prog)s [options] <tracefile #1> <tracefile #2>\n")

    optparser.add_argument("filename1",
        type=str, action="store",
        metavar="<tracefile #1>",
        help="Gallium trace XML filename (plain or .gz, .bz2)")

    optparser.add_argument("filename2",
        type=str, action="store",
        metavar="<tracefile #2>",
        help="Gallium trace XML filename (plain or .gz, .bz2)")

    optparser.add_argument("-p", "--plain",
        dest="plain",
        action="store_true",
        help="disable ANSI color etc. formatting")

    optparser.add_argument("-S", "--sup-variants",
        dest="suppress_variants",
        action="store_true",
        help="suppress some variants in output")

    optparser.add_argument("-C", "--sup-common",
        dest="suppress_common",
        action="store_true",
        help="suppress common sections completely")

    optparser.add_argument("-N", "--named",
        dest="named_ptrs",
        action="store_true",
        help="generate symbolic names for raw pointer values")

    optparser.add_argument("-M", "--method-only",
        dest="method_only",
        action="store_true",
        help="output only call names without arguments")

    optparser.add_argument("-I", "--ignore-junk",
        dest="ignore_junk",
        action="store_true",
        help="filter out/ignore junk calls (see below)")

    optparser.add_argument("-w", "--width",
        dest="output_width",
        type=functools.partial(pkk_arg_range, vmin=16, vmax=512), default=80,
        metavar="N",
        help="output width (default: %(default)s)")

    options = optparser.parse_args()

    ### Parse input files
    stack1 = pkk_parse_trace(options.filename1, options, TraceStateData())
    stack2 = pkk_parse_trace(options.filename2, options, TraceStateData())

    ### Perform diffing
    pkk_info("Matching trace sequences ...")
    sequence = difflib.SequenceMatcher(None, stack1, stack2, autojunk=False)

    pkk_info("Sequencing diff ...")
    opcodes = sequence.get_opcodes()
    if len(opcodes) == 1 and opcodes[0][0] == "equal":
        print("The files are identical.")
        sys.exit(0)

    ### Output results
    pkk_info("Outputting diff ...")
    colwidth = int((options.output_width - 3) / 2)
    colfmt   = "{}{:"+ str(colwidth) +"s}{} {}{}{} {}{:"+ str(colwidth) +"s}{}"

    printer = PKKPrettyPrinter(options)

    for tag, start1, end1, start2, end2 in opcodes:
        if tag == "equal":
            if options.suppress_common:
                print("[...]")
                continue

            sep = "|"
            ansi1 = ansi2 = ansiend = ""
        elif tag == "insert":
            sep = "+"
            ansi1 = ""
            ansi2 = PKK_ANSI_ESC + PKK_ANSI_GREEN

        elif tag == "delete":
            sep = "-"
            ansi1 = PKK_ANSI_ESC + PKK_ANSI_RED
            ansi2 = ""
        elif tag == "replace":
            sep = ">"
            ansi1 = PKK_ANSI_ESC + PKK_ANSI_BOLD
            ansi2 = PKK_ANSI_ESC + PKK_ANSI_BOLD
        else:
            pkk_fatal(f"Internal error, unsupported difflib.SequenceMatcher operation '{tag}'.")

        # No ANSI, please
        if options.plain:
            ansi1 = ansisep = ansi2 = ansiend = ""
        else:
            ansisep = PKK_ANSI_ESC + PKK_ANSI_PURPLE
            ansiend = PKK_ANSI_ESC + PKK_ANSI_NORMAL

        # Print out the block
        ncall1 = start1
        ncall2 = start2
        last1 = last2 = False
        while True:
            # Get line data
            if ncall1 < end1:
                printer.entry_start()
                stack1[ncall1].visit(printer)
                data1 = printer.entry_get()
                ncall1 += 1
            else:
                data1 = []
                last1 = True

            if ncall2 < end2:
                printer.entry_start()
                stack2[ncall2].visit(printer)
                data2 = printer.entry_get()
                ncall2 += 1
            else:
                data2 = []
                last2 = True

            # Check if we are at last call of both
            if last1 and last2:
                break

            nline = 0
            while nline < len(data1) or nline < len(data2):
                # Determine line start indentation
                if nline > 0:
                    if options.suppress_variants:
                        indent = " "*8
                    else:
                        indent = " "*12
                else:
                    indent = ""

                # Output line
                print(colfmt.format(
                    ansi1, pkk_get_line(data1, nline, indent, colwidth), ansiend,
                    ansisep, sep, ansiend,
                    ansi2, pkk_get_line(data2, nline, indent, colwidth), ansiend).
                    rstrip())

                nline += 1