xref: /llvm-project/lldb/test/API/commands/expression/expr-in-syscall/TestExpressionInSyscall.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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    @expectedFailureAll(
11        oslist=["windows"],
12        bugnumber="llvm.org/pr21765, getpid() does not exist on Windows",
13    )
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(
30            listener,
31            None,  # argv
32            None,  # envp
33            None,  # stdin_path
34            None,  # stdout_path
35            None,  # stderr_path
36            None,  # working directory
37            flags,  # launch flags
38            False,  # Stop at entry
39            error,
40        )  # 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(process.GetState(), lldb.eStateRunning, "Process is running")
54
55        # send the process a signal
56        process.SendAsyncInterrupt()
57        while listener.WaitForEvent(2, event):
58            pass
59
60        # as a result the process should stop
61        # in all likelihood we have stopped in the middle of the sleep()
62        # syscall
63        self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
64        thread = process.GetSelectedThread()
65
66        # try evaluating a couple of expressions in this state
67        self.expect_expr("release_flag = 1", result_value="1")
68        self.expect_expr("(int)getpid()", result_value=str(process.GetProcessID()))
69
70        # and run the process to completion
71        process.Continue()
72
73        # process all events
74        while listener.WaitForEvent(10, event):
75            new_state = lldb.SBProcess.GetStateFromEvent(event)
76            if new_state == lldb.eStateExited:
77                break
78
79        self.assertState(process.GetState(), lldb.eStateExited)
80        self.assertEqual(process.GetExitStatus(), 0)
81