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