1"""
2Tests ThreadSanitizer's support to detect a leaked thread.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class TsanThreadLeakTestCase(TestBase):
12
13    @expectedFailureAll(
14        oslist=["linux"],
15        bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
16    @expectedFailureNetBSD
17    @skipIfFreeBSD  # llvm.org/pr21136 runtimes not yet available by default
18    @skipIfRemote
19    @skipUnlessThreadSanitizer
20    def test(self):
21        self.build()
22        self.tsan_tests()
23
24    def tsan_tests(self):
25        exe = self.getBuildArtifact("a.out")
26        self.expect(
27            "file " + exe,
28            patterns=["Current executable set to .*a.out"])
29
30        self.runCmd("run")
31
32        stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
33        if stop_reason == lldb.eStopReasonExec:
34            # On OS X 10.10 and older, we need to re-exec to enable
35            # interceptors.
36            self.runCmd("continue")
37
38        # the stop reason of the thread should be breakpoint.
39        self.expect("thread list", "A thread leak should be detected",
40                    substrs=['stopped', 'stop reason = Thread leak detected'])
41
42        self.assertEqual(
43            self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
44            lldb.eStopReasonInstrumentation)
45