1"""Test that we handle inferiors which change their process group"""
2
3
4
5import os
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class ChangeProcessGroupTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break for main.c.
21        self.line = line_number('main.c', '// Set breakpoint here')
22
23    @skipIfFreeBSD  # Times out on FreeBSD llvm.org/pr23731
24    @skipIfWindows  # setpgid call does not exist on Windows
25    @expectedFailureAndroid("http://llvm.org/pr23762", api_levels=[16])
26    @expectedFailureNetBSD
27    @skipIfReproducer # File synchronization is not supported during replay.
28    @skipIftvOS # fork not available on tvOS.
29    @skipIfwatchOS # fork not available on watchOS.
30    def test_setpgid(self):
31        self.build()
32        exe = self.getBuildArtifact("a.out")
33
34        # Use a file as a synchronization point between test and inferior.
35        pid_file_path = lldbutil.append_to_process_working_directory(self,
36            "pid_file_%d" % (int(time.time())))
37        self.addTearDownHook(
38            lambda: self.run_platform_command(
39                "rm %s" %
40                (pid_file_path)))
41
42        popen = self.spawnSubprocess(exe, [pid_file_path])
43
44        pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
45
46        # make sure we cleanup the forked child also
47        def cleanupChild():
48            if lldb.remote_platform:
49                lldb.remote_platform.Kill(int(pid))
50            else:
51                if os.path.exists("/proc/" + pid):
52                    os.kill(int(pid), signal.SIGKILL)
53        self.addTearDownHook(cleanupChild)
54
55        # Create a target by the debugger.
56        target = self.dbg.CreateTarget(exe)
57        self.assertTrue(target, VALID_TARGET)
58
59        listener = lldb.SBListener("my.attach.listener")
60        error = lldb.SBError()
61        process = target.AttachToProcessWithID(listener, int(pid), error)
62        self.assertTrue(error.Success() and process, PROCESS_IS_VALID)
63
64        # set a breakpoint just before the setpgid() call
65        lldbutil.run_break_set_by_file_and_line(
66            self, 'main.c', self.line, num_expected_locations=-1)
67
68        thread = process.GetSelectedThread()
69
70        # release the child from its loop
71        value = thread.GetSelectedFrame().EvaluateExpression("release_child_flag = 1")
72        self.assertTrue(value.IsValid())
73        self.assertEquals(value.GetValueAsUnsigned(0), 1)
74        process.Continue()
75
76        # make sure the child's process group id is different from its pid
77        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
78        self.assertTrue(value.IsValid())
79        self.assertNotEqual(value.GetValueAsUnsigned(0), int(pid))
80
81        # step over the setpgid() call
82        thread.StepOver()
83        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
84
85        # verify that the process group has been set correctly
86        # this also checks that we are still in full control of the child
87        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
88        self.assertTrue(value.IsValid())
89        self.assertEqual(value.GetValueAsUnsigned(0), int(pid))
90
91        # run to completion
92        process.Continue()
93        self.assertEqual(process.GetState(), lldb.eStateExited)
94