Skip to content

Commit 4dcbf65

Browse files
committed
fix 'CheckDtraceProbes' tests by reading available probes from the test process itself
1 parent c7b9a13 commit 4dcbf65

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

Lib/test/test_dtrace.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def kill_process_group(proc):
7474
proc.communicate() # Clean up
7575

7676

77-
def run_readelf(cmd):
77+
def run_command(cmd):
7878
# Force the C locale to disable localization.
7979
env = dict(os.environ, LC_ALL="C")
8080
try:
@@ -86,7 +86,7 @@ def run_readelf(cmd):
8686
env=env,
8787
)
8888
except OSError:
89-
raise unittest.SkipTest("Couldn't find readelf on the path")
89+
raise unittest.SkipTest(f"Couldn't find {cmd[0]!r} on the path")
9090

9191
with proc:
9292
stdout, stderr = proc.communicate()
@@ -437,14 +437,20 @@ class BPFTraceOptimizedTests(TraceTests, unittest.TestCase):
437437
class CheckDtraceProbes(unittest.TestCase):
438438
@classmethod
439439
def setUpClass(cls):
440+
if sys.platform == "sunos5":
441+
# Solaris does not expose available probes through readable ELF
442+
# section; it uses DTrace itself instead.
443+
DTraceBackend().assert_usable()
444+
return
445+
440446
readelf_major_version, readelf_minor_version = cls.get_readelf_version()
441447
if support.verbose:
442448
print(f"readelf version: {readelf_major_version}.{readelf_minor_version}")
443449

444450

445451
@staticmethod
446452
def get_readelf_version():
447-
version = run_readelf(["readelf", "--version"])
453+
version = run_command(["readelf", "--version"])
448454

449455
# Regex to parse:
450456
# 'GNU readelf (GNU Binutils) 2.40.0\n' -> 2.40
@@ -454,7 +460,13 @@ def get_readelf_version():
454460

455461
return int(match.group(1)), int(match.group(2))
456462

457-
def get_readelf_output(self):
463+
def get_probe_output(self):
464+
if sys.platform == "sunos5":
465+
# Get probes available in this process.
466+
return run_command(
467+
["dtrace", "-l", "-P", f"python{os.getpid()}"]
468+
)
469+
458470
binary = sys.executable
459471
if sysconfig.get_config_var("Py_ENABLE_SHARED"):
460472
lib_dir = sysconfig.get_config_var("LIBDIR")
@@ -476,37 +488,43 @@ def get_readelf_output(self):
476488
binary = libpython_path
477489
break
478490

479-
return run_readelf(["readelf", "-n", binary])
491+
return run_command(["readelf", "-n", binary])
492+
493+
def assert_probe_present(self, probe_name, output):
494+
if sys.platform == "sunos5":
495+
self.assertIn(probe_name.replace("__", "-"), output)
496+
else:
497+
self.assertIn(f"Name: {probe_name}", output)
480498

481499
def test_check_probes(self):
482-
readelf_output = self.get_readelf_output()
500+
probe_output = self.get_probe_output()
483501

484502
available_probe_names = [
485-
"Name: import__find__load__done",
486-
"Name: import__find__load__start",
487-
"Name: audit",
488-
"Name: gc__start",
489-
"Name: gc__done",
490-
"Name: function__entry",
491-
"Name: function__return",
503+
"import__find__load__done",
504+
"import__find__load__start",
505+
"audit",
506+
"gc__start",
507+
"gc__done",
508+
"function__entry",
509+
"function__return",
492510
]
493511

494512
for probe_name in available_probe_names:
495513
with self.subTest(probe_name=probe_name):
496-
self.assertIn(probe_name, readelf_output)
514+
self.assert_probe_present(probe_name, probe_output)
497515

498516
@unittest.expectedFailure
499517
def test_missing_probes(self):
500-
readelf_output = self.get_readelf_output()
518+
probe_output = self.get_probe_output()
501519

502520
# Missing probes will be added in the future.
503521
missing_probe_names = [
504-
"Name: line",
522+
"line",
505523
]
506524

507525
for probe_name in missing_probe_names:
508526
with self.subTest(probe_name=probe_name):
509-
self.assertIn(probe_name, readelf_output)
527+
self.assert_probe_present(probe_name, probe_output)
510528

511529

512530
if __name__ == '__main__':

0 commit comments

Comments
 (0)