xref: /llvm-project/lldb/test/API/functionalities/tsan/thread_numbers/TestTsanThreadNumbers.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Tests that TSan and LLDB have correct thread numbers.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9import json
10
11
12class TsanThreadNumbersTestCase(TestBase):
13    @expectedFailureAll(
14        oslist=["linux"],
15        bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)",
16    )
17    @expectedFailureNetBSD
18    @skipIfFreeBSD  # llvm.org/pr21136 runtimes not yet available by default
19    @skipIfRemote
20    @skipUnlessThreadSanitizer
21    def test(self):
22        self.build()
23        self.tsan_tests()
24
25    def tsan_tests(self):
26        exe = self.getBuildArtifact("a.out")
27        self.expect("file " + exe, patterns=["Current executable set to .*a.out"])
28
29        self.runCmd("run")
30
31        stop_reason = (
32            self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
33        )
34        if stop_reason == lldb.eStopReasonExec:
35            # On OS X 10.10 and older, we need to re-exec to enable
36            # interceptors.
37            self.runCmd("continue")
38
39        # the stop reason of the thread should be breakpoint.
40        self.expect(
41            "thread list",
42            "A data race should be detected",
43            substrs=["stopped", "stop reason = Data race detected"],
44        )
45
46        self.assertEqual(
47            self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
48            lldb.eStopReasonInstrumentation,
49        )
50
51        report_thread_id = (
52            self.dbg.GetSelectedTarget().process.GetSelectedThread().GetIndexID()
53        )
54
55        self.expect(
56            "thread info -s",
57            "The extended stop info should contain the TSan provided fields",
58            substrs=["instrumentation_class", "description", "mops"],
59        )
60
61        output_lines = self.res.GetOutput().split("\n")
62        json_line = "\n".join(output_lines[2:])
63        data = json.loads(json_line)
64        self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
65        self.assertEqual(data["issue_type"], "data-race")
66        self.assertEqual(len(data["mops"]), 2)
67
68        self.assertEqual(data["mops"][0]["thread_id"], report_thread_id)
69
70        other_thread_id = data["mops"][1]["thread_id"]
71        self.assertNotEqual(other_thread_id, report_thread_id)
72        other_thread = self.dbg.GetSelectedTarget().process.GetThreadByIndexID(
73            other_thread_id
74        )
75        self.assertTrue(other_thread.IsValid())
76
77        self.runCmd("thread select %d" % other_thread_id)
78
79        self.expect(
80            "thread backtrace",
81            "The other thread should be stopped in f1 or f2",
82            substrs=["a.out", "main.c"],
83        )
84