xref: /llvm-project/lldb/test/API/functionalities/thread/step_until/TestStepUntil.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass StepUntilTestCase(TestBase):
1199451b44SJordan Rupprecht    def setUp(self):
1299451b44SJordan Rupprecht        # Call super's setUp().
1399451b44SJordan Rupprecht        TestBase.setUp(self)
1499451b44SJordan Rupprecht        # Find the line numbers that we will step to in main:
1599451b44SJordan Rupprecht        self.main_source = "main.c"
16*2238dcc3SJonas Devlieghere        self.less_than_two = line_number("main.c", "Less than 2")
17*2238dcc3SJonas Devlieghere        self.greater_than_two = line_number("main.c", "Greater than or equal to 2.")
18*2238dcc3SJonas Devlieghere        self.back_out_in_main = line_number("main.c", "Back out in main")
1999451b44SJordan Rupprecht
20419cc0a0SVenkata Ramanaiah Nalamothu    def common_setup(self, args):
2199451b44SJordan Rupprecht        self.build()
2299451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2399451b44SJordan Rupprecht
2499451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
2599451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
2699451b44SJordan Rupprecht
2799451b44SJordan Rupprecht        main_source_spec = lldb.SBFileSpec(self.main_source)
2899451b44SJordan Rupprecht        break_before = target.BreakpointCreateBySourceRegex(
29*2238dcc3SJonas Devlieghere            "At the start", main_source_spec
30*2238dcc3SJonas Devlieghere        )
3199451b44SJordan Rupprecht        self.assertTrue(break_before, VALID_BREAKPOINT)
3299451b44SJordan Rupprecht
3399451b44SJordan Rupprecht        # Now launch the process, and do not stop at entry point.
34*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(args, None, self.get_process_working_directory())
3599451b44SJordan Rupprecht
3699451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
39*2238dcc3SJonas Devlieghere        threads = lldbutil.get_threads_stopped_at_breakpoint(process, break_before)
4099451b44SJordan Rupprecht
4199451b44SJordan Rupprecht        if len(threads) != 1:
4299451b44SJordan Rupprecht            self.fail("Failed to stop at first breakpoint in main.")
4399451b44SJordan Rupprecht
4499451b44SJordan Rupprecht        thread = threads[0]
4599451b44SJordan Rupprecht        return thread
4699451b44SJordan Rupprecht
47419cc0a0SVenkata Ramanaiah Nalamothu    def do_until(self, args, until_lines, expected_linenum):
48419cc0a0SVenkata Ramanaiah Nalamothu        thread = self.common_setup(args)
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht        cmd_interp = self.dbg.GetCommandInterpreter()
5199451b44SJordan Rupprecht        ret_obj = lldb.SBCommandReturnObject()
5299451b44SJordan Rupprecht
5399451b44SJordan Rupprecht        cmd_line = "thread until"
5499451b44SJordan Rupprecht        for line_num in until_lines:
5599451b44SJordan Rupprecht            cmd_line += " %d" % (line_num)
5699451b44SJordan Rupprecht
5799451b44SJordan Rupprecht        cmd_interp.HandleCommand(cmd_line, ret_obj)
58*2238dcc3SJonas Devlieghere        self.assertTrue(
59*2238dcc3SJonas Devlieghere            ret_obj.Succeeded(), "'%s' failed: %s." % (cmd_line, ret_obj.GetError())
60*2238dcc3SJonas Devlieghere        )
6199451b44SJordan Rupprecht
6299451b44SJordan Rupprecht        frame = thread.frames[0]
6399451b44SJordan Rupprecht        line = frame.GetLineEntry().GetLine()
64*2238dcc3SJonas Devlieghere        self.assertEqual(
65*2238dcc3SJonas Devlieghere            line, expected_linenum, "Did not get the expected stop line number"
66*2238dcc3SJonas Devlieghere        )
6799451b44SJordan Rupprecht
6899451b44SJordan Rupprecht    def test_hitting_one(self):
6999451b44SJordan Rupprecht        """Test thread step until - targeting one line and hitting it."""
7099451b44SJordan Rupprecht        self.do_until(None, [self.less_than_two], self.less_than_two)
7199451b44SJordan Rupprecht
7299451b44SJordan Rupprecht    def test_targetting_two_hitting_first(self):
7399451b44SJordan Rupprecht        """Test thread step until - targeting two lines and hitting one."""
74*2238dcc3SJonas Devlieghere        self.do_until(
75*2238dcc3SJonas Devlieghere            ["foo", "bar", "baz"],
76*2238dcc3SJonas Devlieghere            [self.less_than_two, self.greater_than_two],
77*2238dcc3SJonas Devlieghere            self.greater_than_two,
78*2238dcc3SJonas Devlieghere        )
7999451b44SJordan Rupprecht
8099451b44SJordan Rupprecht    def test_targetting_two_hitting_second(self):
8199451b44SJordan Rupprecht        """Test thread step until - targeting two lines and hitting the other one."""
82*2238dcc3SJonas Devlieghere        self.do_until(
83*2238dcc3SJonas Devlieghere            None, [self.less_than_two, self.greater_than_two], self.less_than_two
84*2238dcc3SJonas Devlieghere        )
8599451b44SJordan Rupprecht
8699451b44SJordan Rupprecht    def test_missing_one(self):
87419cc0a0SVenkata Ramanaiah Nalamothu        """Test thread step until - targeting one line and missing it by stepping out to call site"""
88*2238dcc3SJonas Devlieghere        self.do_until(
89*2238dcc3SJonas Devlieghere            ["foo", "bar", "baz"], [self.less_than_two], self.back_out_in_main
90*2238dcc3SJonas Devlieghere        )
91