xref: /llvm-project/lldb/test/API/python_api/was_interrupted/interruptible.py (revision 586114510c5fa71d1377c7f53e68a3b12c472aa2)
1import lldb
2import threading
3
4local_data = None
5
6
7class BarrierContainer(threading.local):
8    def __init__(self, before_interrupt_barrier, after_interrupt_barrier, event):
9        self.event = event
10        self.before_interrupt_barrier = before_interrupt_barrier
11        self.after_interrupt_barrier = after_interrupt_barrier
12
13
14class WelcomeCommand(object):
15    def __init__(self, debugger, session_dict):
16        return
17
18    def get_short_help(self):
19        return "A command that waits for an interrupt before returning."
20
21    def check_was_interrupted(self, debugger, use_interpreter):
22        if use_interpreter:
23            self.was_interrupted = debugger.GetCommandInterpreter().WasInterrupted()
24        else:
25            self.was_interrupted = debugger.InterruptRequested()
26        if local_data.event:
27            self.was_canceled = local_data.event.is_set()
28
29    def __call__(self, debugger, args, exe_ctx, result):
30        """Command arguments:
31        {interp/debugger} - Whether to use SBCommandInterpreter::WasInterrupted
32                            of SBDebugger::InterruptRequested().
33        check - Don't do the rendevous, just check if an interrupt was requested.
34                If check is not provided, we'll do the lock and then check.
35        poll  - Should we poll once after the rendevous or spin waiting for the
36                interruption to happen.
37
38        For the interrupt cases, the command waits serially on the barriers
39        passed to it in local data, giving the test runner a chance to set the
40        interrupt.  Once the barriers are passed, it waits for the interrupt
41        or the event.
42        If it finds an interrupt, it returns "Command was interrupted". If it gets an
43        event before seeing the interrupt it returns "Command was not interrupted."
44        For the "poll" case, it waits on the rendevous, then checks once.
45        For the "check" case, it doesn't wait, but just returns whether there was
46        an interrupt in force or not."""
47
48        if local_data is None:
49            result.SetError("local data was not set.")
50            result.SetStatus(lldb.eReturnStatusFailed)
51            return
52
53        use_interpreter = "interp" in args
54        if not use_interpreter:
55            if not "debugger" in args:
56                result.SetError("Must pass either 'interp' or 'debugger'")
57                result.SetStatus(lldb.eReturnStatusFailed)
58                return
59
60        self.was_interrupted = False
61        self.was_canceled = False
62
63        if "check" in args:
64            self.check_was_interrupted(debugger, use_interpreter)
65            if self.was_interrupted:
66                result.Print("Command was interrupted")
67            else:
68                result.Print("Command was not interrupted")
69        else:
70            # Wait here to rendevous in the test before it sets the interrupt.
71            local_data.before_interrupt_barrier.wait()
72            # Now the test will set the interrupt, and we can continue:
73            local_data.after_interrupt_barrier.wait()
74
75            if "poll" in args:
76                self.check_was_interrupted(debugger, use_interpreter)
77            else:
78                while not self.was_interrupted and not self.was_canceled:
79                    self.check_was_interrupted(debugger, use_interpreter)
80
81            if self.was_interrupted:
82                result.Print("Command was interrupted")
83            else:
84                result.Print("Command was not interrupted")
85
86            if self.was_canceled:
87                result.Print("Command was canceled")
88        result.SetStatus(lldb.eReturnStatusSuccessFinishResult)
89        return True
90