1"""Test that we are able to evaluate expressions when the inferior is blocked in a syscall"""
2
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8
9class ExprSyscallTestCase(TestBase):
10
11    @expectedFailureAll(
12        oslist=["windows"],
13        bugnumber="llvm.org/pr21765, getpid() does not exist on Windows")
14    @expectedFailureNetBSD
15    def test_setpgid(self):
16        self.build()
17        self.expr_syscall()
18
19    def expr_syscall(self):
20        # Create a target by the debugger.
21        target = self.createTestTarget()
22
23        listener = lldb.SBListener("my listener")
24
25        # launch the inferior and don't wait for it to stop
26        self.dbg.SetAsync(True)
27        error = lldb.SBError()
28        flags = target.GetLaunchInfo().GetLaunchFlags()
29        process = target.Launch(listener,
30                                None,      # argv
31                                None,      # envp
32                                None,      # stdin_path
33                                None,      # stdout_path
34                                None,      # stderr_path
35                                None,      # working directory
36                                flags,     # launch flags
37                                False,     # Stop at entry
38                                error)     # error
39
40        self.assertTrue(process and process.IsValid(), PROCESS_IS_VALID)
41
42        event = lldb.SBEvent()
43
44        # Give the child enough time to reach the syscall,
45        # while clearing out all the pending events.
46        # The last WaitForEvent call will time out after 2 seconds.
47        while listener.WaitForEvent(2, event):
48            pass
49
50        # now the process should be running (blocked in the syscall)
51        self.assertEqual(
52            process.GetState(),
53            lldb.eStateRunning,
54            "Process is running")
55
56        # send the process a signal
57        process.SendAsyncInterrupt()
58        while listener.WaitForEvent(2, event):
59            pass
60
61        # as a result the process should stop
62        # in all likelihood we have stopped in the middle of the sleep()
63        # syscall
64        self.assertEqual(
65            process.GetState(),
66            lldb.eStateStopped,
67            PROCESS_STOPPED)
68        thread = process.GetSelectedThread()
69
70        # try evaluating a couple of expressions in this state
71        self.expect_expr("release_flag = 1", result_value="1")
72        self.expect_expr("(int)getpid()", result_value=str(process.GetProcessID()))
73
74        # and run the process to completion
75        process.Continue()
76
77        # process all events
78        while listener.WaitForEvent(10, event):
79            new_state = lldb.SBProcess.GetStateFromEvent(event)
80            if new_state == lldb.eStateExited:
81                break
82
83        self.assertState(process.GetState(), lldb.eStateExited)
84        self.assertEqual(process.GetExitStatus(), 0)
85