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    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        self.addTearDownHook(self.cleanupSubprocesses)
41
42        pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
43
44        # make sure we cleanup the forked child also
45        def cleanupChild():
46            if lldb.remote_platform:
47                lldb.remote_platform.Kill(int(pid))
48            else:
49                if os.path.exists("/proc/" + pid):
50                    os.kill(int(pid), signal.SIGKILL)
51        self.addTearDownHook(cleanupChild)
52
53        # Create a target by the debugger.
54        target = self.dbg.CreateTarget(exe)
55        self.assertTrue(target, VALID_TARGET)
56
57        listener = lldb.SBListener("my.attach.listener")
58        error = lldb.SBError()
59        process = target.AttachToProcessWithID(listener, int(pid), error)
60        self.assertTrue(error.Success() and process, PROCESS_IS_VALID)
61
62        # set a breakpoint just before the setpgid() call
63        lldbutil.run_break_set_by_file_and_line(
64            self, 'main.c', self.line, num_expected_locations=-1)
65
66        thread = process.GetSelectedThread()
67
68        # release the child from its loop
69        value = thread.GetSelectedFrame().EvaluateExpression("release_child_flag = 1")
70        self.assertTrue(value.IsValid())
71        self.assertEquals(value.GetValueAsUnsigned(0), 1)
72        process.Continue()
73
74        # make sure the child's process group id is different from its pid
75        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
76        self.assertTrue(value.IsValid())
77        self.assertNotEqual(value.GetValueAsUnsigned(0), int(pid))
78
79        # step over the setpgid() call
80        thread.StepOver()
81        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
82
83        # verify that the process group has been set correctly
84        # this also checks that we are still in full control of the child
85        value = thread.GetSelectedFrame().EvaluateExpression("(int)getpgid(0)")
86        self.assertTrue(value.IsValid())
87        self.assertEqual(value.GetValueAsUnsigned(0), int(pid))
88
89        # run to completion
90        process.Continue()
91        self.assertEqual(process.GetState(), lldb.eStateExited)
92