1"""Test that we get thread names when interrupting a process."""
2
3
4import time
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestInterruptThreadNames(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    @skipUnlessDarwin
16    @add_test_categories(['pyapi'])
17    def test_with_python_api(self):
18        """Test that we get thread names when interrupting a process."""
19        self.build()
20        exe = self.getBuildArtifact("a.out")
21
22        target = self.dbg.CreateTarget(exe)
23        self.assertTrue(target, VALID_TARGET)
24
25        launch_info = lldb.SBLaunchInfo(None)
26        error = lldb.SBError()
27        self.dbg.SetAsync(True)
28        process = target.Launch(launch_info, error)
29        self.assertTrue(process, PROCESS_IS_VALID)
30
31        listener = self.dbg.GetListener()
32        broadcaster = process.GetBroadcaster()
33        rc = broadcaster.AddListener(listener, lldb.SBProcess.eBroadcastBitStateChanged)
34        self.assertTrue(rc != 0, "Unable to add listener to process")
35        self.assertTrue(self.wait_for_running(process, listener), "Check that process is up and running")
36
37        inferior_set_up = self.wait_until_program_setup_complete(process, listener)
38
39        self.assertTrue(inferior_set_up.IsValid() and inferior_set_up.GetValueAsSigned() == 1, "Check that the program was able to create its threads within the allotted time")
40
41        self.check_number_of_threads(process)
42
43        main_thread = lldb.SBThread()
44        second_thread = lldb.SBThread()
45        third_thread = lldb.SBThread()
46        for idx in range(0, process.GetNumThreads()):
47            t = process.GetThreadAtIndex(idx)
48            if t.GetName() == "main thread":
49                main_thread = t
50            if t.GetName() == "second thread":
51                second_thread = t
52            if t.GetName() == "third thread":
53                third_thread = t
54
55        self.check_expected_threads_present(main_thread, second_thread, third_thread)
56
57        process.Kill()
58
59
60    # The process will set a global variable 'threads_up_and_running' to 1 when
61    # it has has completed its setup.  Sleep for one second, pause the program,
62    # check to see if the global has that value, and continue if it does not.
63    def wait_until_program_setup_complete(self, process, listener):
64        inferior_set_up = lldb.SBValue()
65        retry = 5
66        while retry > 0:
67            arch = self.getArchitecture()
68            # when running the testsuite against a remote arm device, it may take
69            # a little longer for the process to start up.  Use a "can't possibly take
70            # longer than this" value.
71            if arch == 'arm64' or arch == 'armv7':
72                time.sleep(10)
73            else:
74                time.sleep(1)
75            process.SendAsyncInterrupt()
76            self.assertTrue(self.wait_for_stop(process, listener), "Check that process is paused")
77            inferior_set_up = process.GetTarget().CreateValueFromExpression("threads_up_and_running", "threads_up_and_running")
78            if inferior_set_up.IsValid() and inferior_set_up.GetValueAsSigned() == 1:
79                retry = 0
80            else:
81                process.Continue()
82            retry = retry - 1
83        return inferior_set_up
84
85    # Listen to the process events until we get an event saying that the process is
86    # running.  Retry up to five times in case we get other events that are not
87    # what we're looking for.
88    def wait_for_running(self, process, listener):
89        retry_count = 5
90        if process.GetState() == lldb.eStateRunning:
91            return True
92
93        while retry_count > 0:
94            event = lldb.SBEvent()
95            listener.WaitForEvent(2, event)
96            if event.GetType() == lldb.SBProcess.eBroadcastBitStateChanged:
97                if process.GetState() == lldb.eStateRunning:
98                    return True
99            retry_count = retry_count - 1
100
101        return False
102
103    # Listen to the process events until we get an event saying the process is
104    # stopped.  Retry up to five times in case we get other events that we are
105    # not looking for.
106    def wait_for_stop(self, process, listener):
107        retry_count = 5
108        if process.GetState() == lldb.eStateStopped or process.GetState() == lldb.eStateCrashed or process.GetState() == lldb.eStateDetached or process.GetState() == lldb.eStateExited:
109            return True
110
111        while retry_count > 0:
112            event = lldb.SBEvent()
113            listener.WaitForEvent(2, event)
114            if event.GetType() == lldb.SBProcess.eBroadcastBitStateChanged:
115                if process.GetState() == lldb.eStateStopped or process.GetState() == lldb.eStateCrashed or process.GetState() == lldb.eStateDetached or process.GetState() == lldb.eStateExited:
116                    return True
117                if process.GetState() == lldb.eStateCrashed or process.GetState() == lldb.eStateDetached or process.GetState() == lldb.eStateExited:
118                    return False
119            retry_count = retry_count - 1
120
121        return False
122
123
124
125    def check_number_of_threads(self, process):
126        self.assertTrue(
127            process.GetNumThreads() == 3,
128            "Check that the process has three threads when sitting at the stopper() breakpoint")
129
130    def check_expected_threads_present(self, main_thread, second_thread, third_thread):
131        self.assertTrue(
132            main_thread.IsValid() and second_thread.IsValid() and third_thread.IsValid(),
133            "Got all three expected threads")
134