1from __future__ import print_function
2import lldb
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test.decorators import *
5from gdbclientutils import *
6
7
8class TestRestartBug(GDBRemoteTestBase):
9
10    @expectedFailureAll(bugnumber="llvm.org/pr24530")
11    def test(self):
12        """
13        Test auto-continue behavior when a process is interrupted to deliver
14        an "asynchronous" packet. This simulates the situation when a process
15        stops on its own just as lldb client is about to interrupt it. The
16        client should not auto-continue in this case, unless the user has
17        explicitly requested that we ignore signals of this type.
18        """
19        class MyResponder(MockGDBServerResponder):
20            continueCount = 0
21
22            def setBreakpoint(self, packet):
23                return "OK"
24
25            def interrupt(self):
26                # Simulate process stopping due to a raise(SIGINT) just as lldb
27                # is about to interrupt it.
28                return "T02reason:signal"
29
30            def cont(self):
31                self.continueCount += 1
32                if self.continueCount == 1:
33                    # No response, wait for the client to interrupt us.
34                    return None
35                return "W00" # Exit
36
37        self.server.responder = MyResponder()
38        target = self.createTarget("a.yaml")
39        process = self.connect(target)
40        self.dbg.SetAsync(True)
41        process.Continue()
42
43        # resume the process and immediately try to set another breakpoint. When using the remote
44        # stub, this will trigger a request to stop the process.  Make sure we
45        # do not lose this signal.
46        bkpt = target.BreakpointCreateByAddress(0x1234)
47        self.assertTrue(bkpt.IsValid())
48        self.assertEqual(bkpt.GetNumLocations(), 1)
49
50        event = lldb.SBEvent()
51        while self.dbg.GetListener().WaitForEvent(2, event):
52            if self.TraceOn():
53                print("Process changing state to:",
54                    self.dbg.StateAsCString(process.GetStateFromEvent(event)))
55            if process.GetStateFromEvent(event) == lldb.eStateExited:
56                break
57
58        # We should get only one continue packet as the client should not
59        # auto-continue after setting the breakpoint.
60        self.assertEqual(self.server.responder.continueCount, 1)
61        # And the process should end up in the stopped state.
62        self.assertEqual(process.GetState(), lldb.eStateStopped)
63