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
14    @expectedFailureAll(
15        oslist=["linux"],
16        bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
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(
28            "file " + exe,
29            patterns=["Current executable set to .*a.out"])
30
31        self.runCmd("run")
32
33        stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
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("thread list", "A data race should be detected",
41                    substrs=['stopped', 'stop reason = Data race detected'])
42
43        self.expect(
44            "thread info -s",
45            "The extended stop info should contain the TSan provided fields",
46            substrs=[
47                "instrumentation_class",
48                "description",
49                "mops"])
50
51        output_lines = self.res.GetOutput().split('\n')
52        json_line = '\n'.join(output_lines[2:])
53        data = json.loads(json_line)
54        self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
55        self.assertEqual(data["issue_type"], "data-race")
56
57        self.assertTrue(data["location_filename"].endswith("/main.cpp"))
58        self.assertEqual(
59            data["location_line"],
60            line_number(
61                'main.cpp',
62                '// global variable'))
63