| 1 |
import dis |
| 2 |
import os.path |
| 3 |
import re |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import sysconfig |
| 7 |
import types |
| 8 |
import unittest |
| 9 |
|
| 10 |
from test import support |
| 11 |
from test.support import findfile, MS_WINDOWS |
| 12 |
|
| 13 |
|
| 14 |
if not support.has_subprocess_support: |
| 15 |
raise unittest.SkipTest("test module requires subprocess") |
| 16 |
|
| 17 |
|
| 18 |
def abspath(filename): |
| 19 |
return os.path.abspath(findfile(filename, subdir="dtracedata")) |
| 20 |
|
| 21 |
|
| 22 |
def normalize_trace_output(output): |
| 23 |
"""Normalize DTrace output for comparison. |
| 24 |
|
| 25 |
DTrace keeps a per-CPU buffer, and when showing the fired probes, buffers |
| 26 |
are concatenated. So if the operating system moves our thread around, the |
| 27 |
straight result can be "non-causal". So we add timestamps to the probe |
| 28 |
firing, sort by that field, then strip it from the output""" |
| 29 |
|
| 30 |
# When compiling with '--with-pydebug', strip '[# refs]' debug output. |
| 31 |
output = re.sub(r"\[[0-9]+ refs\]", "", output) |
| 32 |
try: |
| 33 |
result = [ |
| 34 |
row.split("\t") |
| 35 |
for row in output.splitlines() |
| 36 |
if row and not row.startswith('#') |
| 37 |
] |
| 38 |
result.sort(key=lambda row: int(row[0])) |
| 39 |
result = [row[1] for row in result] |
| 40 |
return "\n".join(result) |
| 41 |
except (IndexError, ValueError): |
| 42 |
raise AssertionError( |
| 43 |
"tracer produced unparsable output:\n{}".format(output) |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
class TraceBackend: |
| 48 |
EXTENSION = None |
| 49 |
COMMAND = None |
| 50 |
COMMAND_ARGS = [] |
| 51 |
|
| 52 |
def run_case(self, name, optimize_python=None): |
| 53 |
actual_output = normalize_trace_output(self.trace_python( |
| 54 |
script_file=abspath(name + self.EXTENSION), |
| 55 |
python_file=abspath(name + ".py"), |
| 56 |
optimize_python=optimize_python)) |
| 57 |
|
| 58 |
with open(abspath(name + self.EXTENSION + ".expected")) as f: |
| 59 |
expected_output = f.read().rstrip() |
| 60 |
|
| 61 |
return (expected_output, actual_output) |
| 62 |
|
| 63 |
def generate_trace_command(self, script_file, subcommand=None): |
| 64 |
command = self.COMMAND + [script_file] |
| 65 |
if subcommand: |
| 66 |
command += ["-c", subcommand] |
| 67 |
return command |
| 68 |
|
| 69 |
def trace(self, script_file, subcommand=None): |
| 70 |
command = self.generate_trace_command(script_file, subcommand) |
| 71 |
stdout, _ = subprocess.Popen(command, |
| 72 |
stdout=subprocess.PIPE, |
| 73 |
stderr=subprocess.STDOUT, |
| 74 |
universal_newlines=True).communicate() |
| 75 |
return stdout |
| 76 |
|
| 77 |
def trace_python(self, script_file, python_file, optimize_python=None): |
| 78 |
python_flags = [] |
| 79 |
if optimize_python: |
| 80 |
python_flags.extend(["-O"] * optimize_python) |
| 81 |
subcommand = " ".join([sys.executable] + python_flags + [python_file]) |
| 82 |
return self.trace(script_file, subcommand) |
| 83 |
|
| 84 |
def assert_usable(self): |
| 85 |
try: |
| 86 |
output = self.trace(abspath("assert_usable" + self.EXTENSION)) |
| 87 |
output = output.strip() |
| 88 |
except (FileNotFoundError, NotADirectoryError, PermissionError) as fnfe: |
| 89 |
output = str(fnfe) |
| 90 |
if output != "probe: success": |
| 91 |
raise unittest.SkipTest( |
| 92 |
"{}(1) failed: {}".format(self.COMMAND[0], output) |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
class DTraceBackend(TraceBackend): |
| 97 |
EXTENSION = ".d" |
| 98 |
COMMAND = ["dtrace", "-q", "-s"] |
| 99 |
|
| 100 |
|
| 101 |
class SystemTapBackend(TraceBackend): |
| 102 |
EXTENSION = ".stp" |
| 103 |
COMMAND = ["stap", "-g"] |
| 104 |
|
| 105 |
|
| 106 |
@unittest.skipIf(MS_WINDOWS, "Tests not compliant with trace on Windows.") |
| 107 |
class TraceTests: |
| 108 |
# unittest.TestCase options |
| 109 |
maxDiff = None |
| 110 |
|
| 111 |
# TraceTests options |
| 112 |
backend = None |
| 113 |
optimize_python = 0 |
| 114 |
|
| 115 |
@classmethod |
| 116 |
def setUpClass(self): |
| 117 |
self.backend.assert_usable() |
| 118 |
|
| 119 |
def run_case(self, name): |
| 120 |
actual_output, expected_output = self.backend.run_case( |
| 121 |
name, optimize_python=self.optimize_python) |
| 122 |
self.assertEqual(actual_output, expected_output) |
| 123 |
|
| 124 |
def test_function_entry_return(self): |
| 125 |
self.run_case("call_stack") |
| 126 |
|
| 127 |
def test_verify_call_opcodes(self): |
| 128 |
"""Ensure our call stack test hits all function call opcodes""" |
| 129 |
|
| 130 |
opcodes = set(["CALL_FUNCTION", "CALL_FUNCTION_EX", "CALL_FUNCTION_KW"]) |
| 131 |
|
| 132 |
with open(abspath("call_stack.py")) as f: |
| 133 |
code_string = f.read() |
| 134 |
|
| 135 |
def get_function_instructions(funcname): |
| 136 |
# Recompile with appropriate optimization setting |
| 137 |
code = compile(source=code_string, |
| 138 |
filename="<string>", |
| 139 |
mode="exec", |
| 140 |
optimize=self.optimize_python) |
| 141 |
|
| 142 |
for c in code.co_consts: |
| 143 |
if isinstance(c, types.CodeType) and c.co_name == funcname: |
| 144 |
return dis.get_instructions(c) |
| 145 |
return [] |
| 146 |
|
| 147 |
for instruction in get_function_instructions('start'): |
| 148 |
opcodes.discard(instruction.opname) |
| 149 |
|
| 150 |
self.assertEqual(set(), opcodes) |
| 151 |
|
| 152 |
def test_gc(self): |
| 153 |
self.run_case("gc") |
| 154 |
|
| 155 |
def test_line(self): |
| 156 |
self.run_case("line") |
| 157 |
|
| 158 |
|
| 159 |
class DTraceNormalTests(TraceTests, unittest.TestCase): |
| 160 |
backend = DTraceBackend() |
| 161 |
optimize_python = 0 |
| 162 |
|
| 163 |
|
| 164 |
class DTraceOptimizedTests(TraceTests, unittest.TestCase): |
| 165 |
backend = DTraceBackend() |
| 166 |
optimize_python = 2 |
| 167 |
|
| 168 |
|
| 169 |
class SystemTapNormalTests(TraceTests, unittest.TestCase): |
| 170 |
backend = SystemTapBackend() |
| 171 |
optimize_python = 0 |
| 172 |
|
| 173 |
|
| 174 |
class SystemTapOptimizedTests(TraceTests, unittest.TestCase): |
| 175 |
backend = SystemTapBackend() |
| 176 |
optimize_python = 2 |
| 177 |
|
| 178 |
class CheckDtraceProbes(unittest.TestCase): |
| 179 |
@classmethod |
| 180 |
def setUpClass(cls): |
| 181 |
if sysconfig.get_config_var('WITH_DTRACE'): |
| 182 |
readelf_major_version, readelf_minor_version = cls.get_readelf_version() |
| 183 |
if support.verbose: |
| 184 |
print(f"readelf version: {readelf_major_version}.{readelf_minor_version}") |
| 185 |
else: |
| 186 |
raise unittest.SkipTest("CPython must be configured with the --with-dtrace option.") |
| 187 |
|
| 188 |
|
| 189 |
@staticmethod |
| 190 |
def get_readelf_version(): |
| 191 |
try: |
| 192 |
cmd = ["readelf", "--version"] |
| 193 |
proc = subprocess.Popen( |
| 194 |
cmd, |
| 195 |
stdout=subprocess.PIPE, |
| 196 |
stderr=subprocess.PIPE, |
| 197 |
universal_newlines=True, |
| 198 |
) |
| 199 |
with proc: |
| 200 |
version, stderr = proc.communicate() |
| 201 |
|
| 202 |
if proc.returncode: |
| 203 |
raise Exception( |
| 204 |
f"Command {' '.join(cmd)!r} failed " |
| 205 |
f"with exit code {proc.returncode}: " |
| 206 |
f"stdout={version!r} stderr={stderr!r}" |
| 207 |
) |
| 208 |
except OSError: |
| 209 |
raise unittest.SkipTest("Couldn't find readelf on the path") |
| 210 |
|
| 211 |
# Regex to parse: |
| 212 |
# 'GNU readelf (GNU Binutils) 2.40.0\n' -> 2.40 |
| 213 |
match = re.search(r"^(?:GNU) readelf.*?\b(\d+)\.(\d+)", version) |
| 214 |
if match is None: |
| 215 |
raise unittest.SkipTest(f"Unable to parse readelf version: {version}") |
| 216 |
|
| 217 |
return int(match.group(1)), int(match.group(2)) |
| 218 |
|
| 219 |
def get_readelf_output(self): |
| 220 |
command = ["readelf", "-n", sys.executable] |
| 221 |
stdout, _ = subprocess.Popen( |
| 222 |
command, |
| 223 |
stdout=subprocess.PIPE, |
| 224 |
stderr=subprocess.STDOUT, |
| 225 |
universal_newlines=True, |
| 226 |
).communicate() |
| 227 |
return stdout |
| 228 |
|
| 229 |
def test_check_probes(self): |
| 230 |
readelf_output = self.get_readelf_output() |
| 231 |
|
| 232 |
available_probe_names = [ |
| 233 |
"Name: import__find__load__done", |
| 234 |
"Name: import__find__load__start", |
| 235 |
"Name: audit", |
| 236 |
"Name: gc__start", |
| 237 |
"Name: gc__done", |
| 238 |
] |
| 239 |
|
| 240 |
for probe_name in available_probe_names: |
| 241 |
with self.subTest(probe_name=probe_name): |
| 242 |
self.assertIn(probe_name, readelf_output) |
| 243 |
|
| 244 |
@unittest.expectedFailure |
| 245 |
def test_missing_probes(self): |
| 246 |
readelf_output = self.get_readelf_output() |
| 247 |
|
| 248 |
# Missing probes will be added in the future. |
| 249 |
missing_probe_names = [ |
| 250 |
"Name: function__entry", |
| 251 |
"Name: function__return", |
| 252 |
"Name: line", |
| 253 |
] |
| 254 |
|
| 255 |
for probe_name in missing_probe_names: |
| 256 |
with self.subTest(probe_name=probe_name): |
| 257 |
self.assertIn(probe_name, readelf_output) |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
unittest.main() |