master
py 110 lines 2.96 KB
Raw
1 #!/usr/bin/python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 import os
5 from pathlib import Path
6 from shutil import copyfile
7 from subprocess import check_call
8 import sys
9 import tempfile
10
11
12 def get_formats(backend):
13 formats = [
14 "c",
15 "h",
16 ]
17 if backend in {"dtrace", "ftrace", "log", "simple", "syslog"}:
18 formats += ["rs"]
19 if backend == "dtrace":
20 formats += [
21 "d",
22 "log-stap",
23 "simpletrace-stap",
24 "stap",
25 ]
26 if backend == "ust":
27 formats += [
28 "ust-events-c",
29 "ust-events-h",
30 ]
31 return formats
32
33
34 def test_tracetool_one(tracetool, backend, fmt, src_dir, build_dir):
35 rel_filename = backend + "." + fmt
36 actual_file = Path(build_dir, rel_filename)
37 expect_file = Path(src_dir, rel_filename)
38
39 args = [sys.executable, tracetool,
40 f"--format={fmt}", f"--backends={backend}", "--group=testsuite"]
41
42 if fmt.find("stap") != -1:
43 args += ["--binary=qemu", "--probe-prefix=qemu"]
44
45 # Use relative files for both, as these filenames end
46 # up in '#line' statements in the output
47 args += ["trace-events", rel_filename]
48
49 try:
50 check_call(args, cwd=build_dir)
51 actual = actual_file.read_text()
52 finally:
53 actual_file.unlink()
54
55 if os.getenv("QEMU_TEST_REGENERATE", False):
56 print(f"# regenerate {expect_file}")
57 expect_file.write_text(actual)
58
59 expect = expect_file.read_text()
60
61 assert expect == actual
62
63
64 def test_tracetool(tracetool, backend, source_dir, build_dir):
65 fail = False
66 scenarios = len(get_formats(backend))
67
68 print(f"1..{scenarios}")
69
70 src_events = Path(source_dir, "trace-events")
71 build_events = Path(build_dir, "trace-events")
72
73 try:
74 # We need a stable relative filename under build dir
75 # for the '#line' statements, so copy over the input
76 copyfile(src_events, build_events)
77
78 num = 1
79 for fmt in get_formats(backend):
80 status = "not ok"
81 hint = ""
82 try:
83 test_tracetool_one(tracetool, backend, fmt, source_dir, build_dir)
84 status = "ok"
85 except Exception as e:
86 print(f"# {e}")
87 fail = True
88 hint = (
89 " (set QEMU_TEST_REGENERATE=1 to recreate reference "
90 + "output if tracetool generator was intentionally changed)"
91 )
92 finally:
93 print(f"{status} {num} - {backend}.{fmt}{hint}")
94 finally:
95 build_events.unlink()
96
97 return fail
98
99
100 if __name__ == "__main__":
101 if len(sys.argv) != 5:
102 argv0 = sys.argv[0]
103 print("syntax: {argv0} TRACE-TOOL BACKEND SRC-DIR BUILD-DIR", file=sys.stderr)
104 sys.exit(1)
105
106 with tempfile.TemporaryDirectory(prefix=sys.argv[4]) as tmpdir:
107 fail = test_tracetool(sys.argv[1], sys.argv[2], sys.argv[3], tmpdir)
108 if fail:
109 sys.exit(1)
110 sys.exit(0)