xref: /llvm-project/lldb/test/API/python_api/process/cancel_attach/TestCancelAttach.py (revision 1277bea4311692d3bd3d1a6566ec1011d3e72f65)
1d1bf1947Sjimingham"""
2d1bf1947SjiminghamTest using SendAsyncInterrupt to interrupt an "attach wait"
3d1bf1947Sjimingham"""
4d1bf1947Sjimingham
5d1bf1947Sjiminghamimport lldb
6d1bf1947Sjiminghamimport sys
7d1bf1947Sjiminghamimport time
8d1bf1947Sjiminghamimport threading
9d1bf1947Sjiminghamfrom lldbsuite.test.decorators import *
10d1bf1947Sjiminghamfrom lldbsuite.test.lldbtest import *
11d1bf1947Sjiminghamimport lldbsuite.test.lldbutil
12d1bf1947Sjimingham
13d1bf1947Sjimingham
14d1bf1947Sjiminghamclass AttachCancelTestCase(TestBase):
15d1bf1947Sjimingham    NO_DEBUG_INFO_TESTCASE = True
16d1bf1947Sjimingham
17*1277bea4SDmitry Vasilyev    @skipIf(
18*1277bea4SDmitry Vasilyev        remote=True,
19*1277bea4SDmitry Vasilyev        hostoslist=["windows"],
20*1277bea4SDmitry Vasilyev        bugnumber="https://github.com/llvm/llvm-project/issues/115618",
21*1277bea4SDmitry Vasilyev    )
22d1bf1947Sjimingham    def test_scripted_implementation(self):
23d1bf1947Sjimingham        """Test that cancelling a stuck "attach waitfor" works."""
24d1bf1947Sjimingham        # First make an empty target for the attach:
25d1bf1947Sjimingham        target = self.dbg.CreateTarget(None)
26d1bf1947Sjimingham
27d1bf1947Sjimingham        # We need to cancel this, so we need to do the attach
28d1bf1947Sjimingham        # on a separate thread:
29d1bf1947Sjimingham        class AttachThread(threading.Thread):
30d1bf1947Sjimingham            def __init__(self, target, error):
31d1bf1947Sjimingham                # Make this a daemon thread so if we don't manage to interrupt,
32d1bf1947Sjimingham                # Python will keep this thread from hanging the test.
33d1bf1947Sjimingham                threading.Thread.__init__(self, daemon=True)
34d1bf1947Sjimingham                self.target = target
35d1bf1947Sjimingham                self.error = error
36d1bf1947Sjimingham
37d1bf1947Sjimingham            def run(self):
38096c530aSJonas Devlieghere                self.target.AttachToProcessWithName(
39096c530aSJonas Devlieghere                    lldb.SBListener(), "LLDB-No-Such-Process", True, self.error
40096c530aSJonas Devlieghere                )
41d1bf1947Sjimingham
42d1bf1947Sjimingham        error = lldb.SBError()
43d1bf1947Sjimingham        thread = AttachThread(target, error)
44d1bf1947Sjimingham        thread.start()
45d1bf1947Sjimingham
46d1bf1947Sjimingham        # Now wait till the attach on the child thread has made a process
47d1bf1947Sjimingham        # for the attach attempt:
48d1bf1947Sjimingham        while not target.process.IsValid():
49d1bf1947Sjimingham            time.sleep(1)
50d1bf1947Sjimingham        # I don't have a positive signal for "we started the attach attempt"
51d1bf1947Sjimingham        # so the best I can do is sleep a bit more here to give that a chance
52d1bf1947Sjimingham        # to start:
53d1bf1947Sjimingham        time.sleep(1)
54d1bf1947Sjimingham
55d1bf1947Sjimingham        # Now send the attach interrupt:
56d1bf1947Sjimingham        target.process.SendAsyncInterrupt()
57d1bf1947Sjimingham        # We don't want to stall if we can't interrupt, so join with a timeout:
58d1bf1947Sjimingham        thread.join(60)
59d1bf1947Sjimingham        if thread.is_alive():
60d1bf1947Sjimingham            self.fail("The attach thread is alive after timeout interval")
61d1bf1947Sjimingham
62d1bf1947Sjimingham        # Now check the error, should say the attach was interrupted:
63d1bf1947Sjimingham        self.assertTrue(error.Fail(), "We succeeded in not attaching")
64