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
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.assertEqual(
44            self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
45            lldb.eStopReasonInstrumentation)
46
47        report_thread_id = self.dbg.GetSelectedTarget(
48        ).process.GetSelectedThread().GetIndexID()
49
50        self.expect(
51            "thread info -s",
52            "The extended stop info should contain the TSan provided fields",
53            substrs=[
54                "instrumentation_class",
55                "description",
56                "mops"])
57
58        output_lines = self.res.GetOutput().split('\n')
59        json_line = '\n'.join(output_lines[2:])
60        data = json.loads(json_line)
61        self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
62        self.assertEqual(data["issue_type"], "data-race")
63        self.assertEqual(len(data["mops"]), 2)
64
65        self.assertEqual(data["mops"][0]["thread_id"], report_thread_id)
66
67        other_thread_id = data["mops"][1]["thread_id"]
68        self.assertNotEqual(other_thread_id, report_thread_id)
69        other_thread = self.dbg.GetSelectedTarget(
70        ).process.GetThreadByIndexID(other_thread_id)
71        self.assertTrue(other_thread.IsValid())
72
73        self.runCmd("thread select %d" % other_thread_id)
74
75        self.expect(
76            "thread backtrace",
77            "The other thread should be stopped in f1 or f2",
78            substrs=[
79                "a.out",
80                "main.c"])
81