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    @skipIftvOS # fork not available on tvOS.
28    @skipIfwatchOS # fork not available on watchOS.
29    def test_setpgid(self):
30        self.build()
31        exe = self.getBuildArtifact("a.out")
32
33        # Use a file as a synchronization point between test and inferior.
34        pid_file_path = lldbutil.append_to_process_working_directory(self,
35            "pid_file_%d" % (int(time.time())))
36        self.addTearDownHook(
37            lambda: self.run_platform_command(
38                "rm %s" %
39                (pid_file_path)))
40
41        popen = self.spawnSubprocess(exe, [pid_file_path])
42
43        pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
44
45        # make sure we cleanup the forked child also
46        def cleanupChild():
47            if lldb.remote_platform:
48                lldb.remote_platform.Kill(int(pid))
49            else:
50                if os.path.exists("/proc/" + pid):
51                    os.kill(int(pid), signal.SIGKILL)
52        self.addTearDownHook(cleanupChild)
53
54        # Create a target by the debugger.
55        target = self.dbg.CreateTarget(exe)
56        self.assertTrue(target, VALID_TARGET)
57
58        listener = lldb.SBListener("my.attach.listener")
59        error = lldb.SBError()
60        process = target.AttachToProcessWithID(listener, int(pid), error)
61        self.assertTrue(error.Success() and process, PROCESS_IS_VALID)
62
63        # set a breakpoint just before the setpgid() call
64        lldbutil.run_break_set_by_file_and_line(
65            self, 'main.c', self.line, num_expected_locations=-1)
66
67        thread = process.GetSelectedThread()
68
69        # release the child from its loop
70        value = thread.GetSelectedFrame().EvaluateExpression("release_child_flag = 1")
71        self.assertTrue(value.IsValid())
72        self.assertEquals(value.GetValueAsUnsigned(0), 1)
73        process.Continue()
74
75        # make sure the child's process group id is different from its pid
76        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
77        self.assertTrue(value.IsValid())
78        self.assertNotEqual(value.GetValueAsUnsigned(0), int(pid))
79
80        # step over the setpgid() call
81        thread.StepOver()
82        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
83
84        # verify that the process group has been set correctly
85        # this also checks that we are still in full control of the child
86        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
87        self.assertTrue(value.IsValid())
88        self.assertEqual(value.GetValueAsUnsigned(0), int(pid))
89
90        # run to completion
91        process.Continue()
92        self.assertEqual(process.GetState(), lldb.eStateExited)
93