1"""Test the 'step target' feature."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestStepTarget(TestBase):
11
12    def setUp(self):
13        # Call super's setUp().
14        TestBase.setUp(self)
15        # Find the line numbers that we will step to in main:
16        self.main_source = "main.c"
17        self.end_line = line_number(self.main_source, "All done")
18
19    @add_test_categories(['pyapi'])
20    def get_to_start(self):
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23
24        target = self.dbg.CreateTarget(exe)
25        self.assertTrue(target, VALID_TARGET)
26
27        self.main_source_spec = lldb.SBFileSpec(self.main_source)
28
29        break_in_main = target.BreakpointCreateBySourceRegex(
30            'Break here to try targetted stepping', self.main_source_spec)
31        self.assertTrue(break_in_main, VALID_BREAKPOINT)
32        self.assertGreater(break_in_main.GetNumLocations(), 0, "Has locations.")
33
34        # Now launch the process, and do not stop at entry point.
35        process = target.LaunchSimple(
36            None, None, self.get_process_working_directory())
37
38        self.assertTrue(process, PROCESS_IS_VALID)
39
40        # The stop reason of the thread should be breakpoint.
41        threads = lldbutil.get_threads_stopped_at_breakpoint(
42            process, break_in_main)
43
44        if len(threads) != 1:
45            self.fail("Failed to stop at first breakpoint in main.")
46
47        thread = threads[0]
48        return thread
49
50    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr32343")
51    def test_with_end_line(self):
52        """Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
53
54        thread = self.get_to_start()
55
56        error = lldb.SBError()
57        thread.StepInto("lotsOfArgs", self.end_line, error)
58        frame = thread.frames[0]
59
60        self.assertEqual(frame.name, "lotsOfArgs", "Stepped to lotsOfArgs.")
61
62    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr32343")
63    def test_with_end_line_bad_name(self):
64        """Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
65
66        thread = self.get_to_start()
67
68        error = lldb.SBError()
69        thread.StepInto("lotsOfArgssss", self.end_line, error)
70        frame = thread.frames[0]
71        self.assertEqual(frame.line_entry.line, self.end_line,
72            "Stepped to the block end.")
73
74    def test_with_end_line_deeper(self):
75        """Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
76
77        thread = self.get_to_start()
78
79        error = lldb.SBError()
80        thread.StepInto("modifyInt", self.end_line, error)
81        frame = thread.frames[0]
82        self.assertEqual(frame.name, "modifyInt", "Stepped to modifyInt.")
83
84    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr32343")
85    def test_with_command_and_block(self):
86        """Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
87
88        thread = self.get_to_start()
89
90        result = lldb.SBCommandReturnObject()
91        self.dbg.GetCommandInterpreter().HandleCommand(
92            'thread step-in -t "lotsOfArgs" -e block', result)
93        self.assertTrue(
94            result.Succeeded(),
95            "thread step-in command succeeded.")
96
97        frame = thread.frames[0]
98        self.assertEqual(frame.name, "lotsOfArgs", "Stepped to lotsOfArgs.")
99
100    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr32343")
101    def test_with_command_and_block_and_bad_name(self):
102        """Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
103
104        thread = self.get_to_start()
105
106        result = lldb.SBCommandReturnObject()
107        self.dbg.GetCommandInterpreter().HandleCommand(
108            'thread step-in -t "lotsOfArgsssss" -e block', result)
109        self.assertTrue(
110            result.Succeeded(),
111            "thread step-in command succeeded.")
112
113        frame = thread.frames[0]
114
115        self.assertEqual(frame.name, "main", "Stepped back out to main.")
116        # end_line is set to the line after the containing block.  Check that
117        # we got there:
118        self.assertEqual(frame.line_entry.line, self.end_line,
119            "Got out of the block")
120