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