xref: /llvm-project/lldb/test/API/functionalities/thread/step_until/TestStepUntil.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test stepping over vrs. hitting breakpoints & subsequent stepping in various forms."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class StepUntilTestCase(TestBase):
11    def setUp(self):
12        # Call super's setUp().
13        TestBase.setUp(self)
14        # Find the line numbers that we will step to in main:
15        self.main_source = "main.c"
16        self.less_than_two = line_number("main.c", "Less than 2")
17        self.greater_than_two = line_number("main.c", "Greater than or equal to 2.")
18        self.back_out_in_main = line_number("main.c", "Back out in main")
19
20    def common_setup(self, args):
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23
24        target = self.dbg.CreateTarget(exe)
25        self.assertTrue(target, VALID_TARGET)
26
27        main_source_spec = lldb.SBFileSpec(self.main_source)
28        break_before = target.BreakpointCreateBySourceRegex(
29            "At the start", main_source_spec
30        )
31        self.assertTrue(break_before, VALID_BREAKPOINT)
32
33        # Now launch the process, and do not stop at entry point.
34        process = target.LaunchSimple(args, None, self.get_process_working_directory())
35
36        self.assertTrue(process, PROCESS_IS_VALID)
37
38        # The stop reason of the thread should be breakpoint.
39        threads = lldbutil.get_threads_stopped_at_breakpoint(process, break_before)
40
41        if len(threads) != 1:
42            self.fail("Failed to stop at first breakpoint in main.")
43
44        thread = threads[0]
45        return thread
46
47    def do_until(self, args, until_lines, expected_linenum):
48        thread = self.common_setup(args)
49
50        cmd_interp = self.dbg.GetCommandInterpreter()
51        ret_obj = lldb.SBCommandReturnObject()
52
53        cmd_line = "thread until"
54        for line_num in until_lines:
55            cmd_line += " %d" % (line_num)
56
57        cmd_interp.HandleCommand(cmd_line, ret_obj)
58        self.assertTrue(
59            ret_obj.Succeeded(), "'%s' failed: %s." % (cmd_line, ret_obj.GetError())
60        )
61
62        frame = thread.frames[0]
63        line = frame.GetLineEntry().GetLine()
64        self.assertEqual(
65            line, expected_linenum, "Did not get the expected stop line number"
66        )
67
68    def test_hitting_one(self):
69        """Test thread step until - targeting one line and hitting it."""
70        self.do_until(None, [self.less_than_two], self.less_than_two)
71
72    def test_targetting_two_hitting_first(self):
73        """Test thread step until - targeting two lines and hitting one."""
74        self.do_until(
75            ["foo", "bar", "baz"],
76            [self.less_than_two, self.greater_than_two],
77            self.greater_than_two,
78        )
79
80    def test_targetting_two_hitting_second(self):
81        """Test thread step until - targeting two lines and hitting the other one."""
82        self.do_until(
83            None, [self.less_than_two, self.greater_than_two], self.less_than_two
84        )
85
86    def test_missing_one(self):
87        """Test thread step until - targeting one line and missing it by stepping out to call site"""
88        self.do_until(
89            ["foo", "bar", "baz"], [self.less_than_two], self.back_out_in_main
90        )
91