xref: /llvm-project/lldb/test/API/macosx/safe-to-func-call/TestSafeFuncCalls.py (revision 1eeeab82c6eb185f5139e633a59c2dbcb15616e4)
1"""Test function call thread safety."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestSafeFuncCalls(TestBase):
11    @skipUnlessDarwin
12    @add_test_categories(["pyapi"])
13    def test_with_python_api(self):
14        """Test function call thread safety."""
15        self.build()
16        exe = self.getBuildArtifact("a.out")
17
18        target = self.dbg.CreateTarget(exe)
19        self.assertTrue(target, VALID_TARGET)
20        self.main_source_spec = lldb.SBFileSpec("main.c")
21        break1 = target.BreakpointCreateByName("stopper", "a.out")
22        self.assertTrue(break1, VALID_BREAKPOINT)
23        process = target.LaunchSimple(None, None, self.get_process_working_directory())
24        self.assertTrue(process, PROCESS_IS_VALID)
25        threads = lldbutil.get_threads_stopped_at_breakpoint(process, break1)
26        self.assertEqual(len(threads), 1, "Failed to stop at breakpoint 1.")
27
28        self.assertEqual(
29            process.GetNumThreads(),
30            2,
31            "Check that the process has two threads when sitting at the stopper() breakpoint",
32        )
33
34        main_thread = lldb.SBThread()
35        select_thread = lldb.SBThread()
36        for idx in range(0, process.GetNumThreads()):
37            t = process.GetThreadAtIndex(idx)
38            if t.GetName() == "main thread":
39                main_thread = t
40            if t.GetName() == "select thread":
41                select_thread = t
42
43        self.assertTrue(
44            main_thread.IsValid() and select_thread.IsValid(),
45            "Got both expected threads",
46        )
47
48        self.assertTrue(
49            main_thread.SafeToCallFunctions(),
50            "It is safe to call functions on the main thread",
51        )
52        self.assertFalse(
53            select_thread.SafeToCallFunctions(),
54            "It is not safe to call functions on the select thread",
55        )
56