xref: /llvm-project/lldb/test/API/functionalities/thread/create_after_attach/TestCreateAfterAttach.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest thread creation after process attach.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass CreateAfterAttachTestCase(TestBase):
1399451b44SJordan Rupprecht    def setUp(self):
1499451b44SJordan Rupprecht        # Call super's setUp().
1599451b44SJordan Rupprecht        TestBase.setUp(self)
1699451b44SJordan Rupprecht        # Find the line numbers for our breakpoints.
17*2238dcc3SJonas Devlieghere        self.break_1 = line_number("main.cpp", "// Set first breakpoint here")
18*2238dcc3SJonas Devlieghere        self.break_2 = line_number("main.cpp", "// Set second breakpoint here")
19*2238dcc3SJonas Devlieghere        self.break_3 = line_number("main.cpp", "// Set third breakpoint here")
2099451b44SJordan Rupprecht
21700dd173SJonas Devlieghere    # Occasionally hangs on Windows, may be same as other issues.
22700dd173SJonas Devlieghere    @skipIfWindows
23700dd173SJonas Devlieghere    @skipIfiOSSimulator
24266c90feSMichał Górny    @expectedFailureNetBSD
25700dd173SJonas Devlieghere    def test_create_after_attach(self):
2699451b44SJordan Rupprecht        """Test thread creation after process attach."""
27d7dbe2c4SPavel Labath        self.build()
2899451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2999451b44SJordan Rupprecht
3099451b44SJordan Rupprecht        # Spawn a new process
3115f067f1SMichał Górny        # use realpath to workaround llvm.org/pr48376
3215f067f1SMichał Górny        popen = self.spawnSubprocess(os.path.realpath(exe))
3399451b44SJordan Rupprecht        pid = popen.pid
3499451b44SJordan Rupprecht
3599451b44SJordan Rupprecht        # Attach to the spawned process
3699451b44SJordan Rupprecht        self.runCmd("process attach -p " + str(pid))
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        target = self.dbg.GetSelectedTarget()
3999451b44SJordan Rupprecht
4099451b44SJordan Rupprecht        process = target.GetProcess()
4199451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht        # This should create a breakpoint in the main thread.
4499451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
45*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_1, num_expected_locations=1
46*2238dcc3SJonas Devlieghere        )
4799451b44SJordan Rupprecht
4899451b44SJordan Rupprecht        # This should create a breakpoint in the second child thread.
4999451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
50*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_2, num_expected_locations=1
51*2238dcc3SJonas Devlieghere        )
5299451b44SJordan Rupprecht
5399451b44SJordan Rupprecht        # This should create a breakpoint in the first child thread.
5499451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
55*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_3, num_expected_locations=1
56*2238dcc3SJonas Devlieghere        )
5799451b44SJordan Rupprecht
5899451b44SJordan Rupprecht        # Note:  With std::thread, we cannot rely on particular thread numbers.  Using
5999451b44SJordan Rupprecht        # std::thread may cause the program to spin up a thread pool (and it does on
6099451b44SJordan Rupprecht        # Windows), so the thread numbers are non-deterministic.
6199451b44SJordan Rupprecht
6299451b44SJordan Rupprecht        # Run to the first breakpoint
6399451b44SJordan Rupprecht        self.runCmd("continue")
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
66*2238dcc3SJonas Devlieghere        self.expect(
67*2238dcc3SJonas Devlieghere            "thread list",
68*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
69*2238dcc3SJonas Devlieghere            substrs=["stopped", "* thread #", "main", "stop reason = breakpoint"],
70*2238dcc3SJonas Devlieghere        )
7199451b44SJordan Rupprecht
7299451b44SJordan Rupprecht        # Change a variable to escape the loop
7399451b44SJordan Rupprecht        self.runCmd("expression main_thread_continue = 1")
7499451b44SJordan Rupprecht
7599451b44SJordan Rupprecht        # Run to the second breakpoint
7699451b44SJordan Rupprecht        self.runCmd("continue")
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
79*2238dcc3SJonas Devlieghere        self.expect(
80*2238dcc3SJonas Devlieghere            "thread list",
81*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
82*2238dcc3SJonas Devlieghere            substrs=[
83*2238dcc3SJonas Devlieghere                "stopped",
84*2238dcc3SJonas Devlieghere                "* thread #",
85*2238dcc3SJonas Devlieghere                "thread_2_func",
86*2238dcc3SJonas Devlieghere                "stop reason = breakpoint",
87*2238dcc3SJonas Devlieghere            ],
88*2238dcc3SJonas Devlieghere        )
8999451b44SJordan Rupprecht
9099451b44SJordan Rupprecht        # Change a variable to escape the loop
9199451b44SJordan Rupprecht        self.runCmd("expression child_thread_continue = 1")
9299451b44SJordan Rupprecht
9399451b44SJordan Rupprecht        # Run to the third breakpoint
9499451b44SJordan Rupprecht        self.runCmd("continue")
9599451b44SJordan Rupprecht
9699451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
9799451b44SJordan Rupprecht        # Thread 3 may or may not have already exited.
98*2238dcc3SJonas Devlieghere        self.expect(
99*2238dcc3SJonas Devlieghere            "thread list",
100*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
101*2238dcc3SJonas Devlieghere            substrs=[
102*2238dcc3SJonas Devlieghere                "stopped",
103*2238dcc3SJonas Devlieghere                "* thread #",
104*2238dcc3SJonas Devlieghere                "thread_1_func",
105*2238dcc3SJonas Devlieghere                "stop reason = breakpoint",
106*2238dcc3SJonas Devlieghere            ],
107*2238dcc3SJonas Devlieghere        )
10899451b44SJordan Rupprecht
10999451b44SJordan Rupprecht        # Run to completion
11099451b44SJordan Rupprecht        self.runCmd("continue")
11199451b44SJordan Rupprecht
11299451b44SJordan Rupprecht        # At this point, the inferior process should have exited.
113*2238dcc3SJonas Devlieghere        self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
114