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