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