xref: /llvm-project/lldb/test/API/functionalities/tsan/cpp_global_location/TestTsanCPPGlobalLocation.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Tests that TSan correctly reports the filename and line number of a racy global C++ variable.
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 TsanCPPGlobalLocationTestCase(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.expect(
47            "thread info -s",
48            "The extended stop info should contain the TSan provided fields",
49            substrs=["instrumentation_class", "description", "mops"],
50        )
51
52        output_lines = self.res.GetOutput().split("\n")
53        json_line = "\n".join(output_lines[2:])
54        data = json.loads(json_line)
55        self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
56        self.assertEqual(data["issue_type"], "data-race")
57
58        self.assertTrue(data["location_filename"].endswith("/main.cpp"))
59        self.assertEqual(
60            data["location_line"], line_number("main.cpp", "// global variable")
61        )
62