1"""
2This tests that we do not lose control of the inferior, while doing an instruction-level step
3over a thread creation instruction.
4"""
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class CreateDuringInstructionStepTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @skipUnlessPlatform(['linux'])
17    @expectedFailureAndroid('llvm.org/pr24737', archs=['arm'])
18    @skipIf(oslist=["linux"], archs=["arm", "aarch64"], bugnumber="llvm.org/pr24737")
19    def test_step_inst(self):
20        self.build()
21        exe = self.getBuildArtifact("a.out")
22        target = self.dbg.CreateTarget(exe)
23        self.assertTrue(target and target.IsValid(), "Target is valid")
24
25        # This should create a breakpoint in the stepping thread.
26        breakpoint = target.BreakpointCreateByName("main")
27        self.assertTrue(
28            breakpoint and breakpoint.IsValid(),
29            "Breakpoint is valid")
30
31        # Run the program.
32        process = target.LaunchSimple(
33            None, None, self.get_process_working_directory())
34        self.assertTrue(process and process.IsValid(), PROCESS_IS_VALID)
35
36        # The stop reason of the thread should be breakpoint.
37        self.assertEqual(
38            process.GetState(),
39            lldb.eStateStopped,
40            PROCESS_STOPPED)
41
42        threads = lldbutil.get_threads_stopped_at_breakpoint(
43            process, breakpoint)
44        self.assertEqual(len(threads), 1, STOPPED_DUE_TO_BREAKPOINT)
45
46        thread = threads[0]
47        self.assertTrue(thread and thread.IsValid(), "Thread is valid")
48
49        # Make sure we see only one threads
50        self.assertEqual(
51            process.GetNumThreads(),
52            1,
53            'Number of expected threads and actual threads do not match.')
54
55        # Keep stepping until we see the thread creation
56        while process.GetNumThreads() < 2:
57            thread.StepInstruction(False)
58            self.assertEqual(
59                process.GetState(),
60                lldb.eStateStopped,
61                PROCESS_STOPPED)
62            self.assertEqual(
63                thread.GetStopReason(),
64                lldb.eStopReasonPlanComplete,
65                "Step operation succeeded")
66            if self.TraceOn():
67                self.runCmd("disassemble --pc")
68
69        if self.TraceOn():
70            self.runCmd("thread list")
71
72        # We have successfully caught thread creation. Now just run to
73        # completion
74        process.Continue()
75
76        # At this point, the inferior process should have exited.
77        self.assertState(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
78