xref: /llvm-project/lldb/test/API/functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that the breakpoint auto-continue flag works correctly.
3"""
4
5
6import lldb
7import lldbsuite.test.lldbutil as lldbutil
8from lldbsuite.test.lldbtest import *
9
10
11class BreakpointAutoContinue(TestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def test_breakpoint_auto_continue(self):
15        """Make sure the auto continue continues with no other complications"""
16        self.build()
17        self.simple_auto_continue()
18
19    def test_auto_continue_with_command(self):
20        """Add a command, make sure the command gets run"""
21        self.build()
22        self.auto_continue_with_command()
23
24    def test_auto_continue_on_location(self):
25        """Set auto-continue on a location and make sure only that location continues"""
26        self.build()
27        self.auto_continue_location()
28
29    def make_target_and_bkpt(
30        self,
31        additional_options=None,
32        num_expected_loc=1,
33        pattern="Set a breakpoint here",
34    ):
35        self.target = self.createTestTarget()
36
37        extra_options_txt = "--auto-continue 1 "
38        if additional_options:
39            extra_options_txt += additional_options
40        bpno = lldbutil.run_break_set_by_source_regexp(
41            self,
42            pattern,
43            extra_options=extra_options_txt,
44            num_expected_locations=num_expected_loc,
45        )
46        return bpno
47
48    def launch_it(self, expected_state):
49        error = lldb.SBError()
50        launch_info = self.target.GetLaunchInfo()
51        launch_info.SetWorkingDirectory(self.get_process_working_directory())
52
53        process = self.target.Launch(launch_info, error)
54        self.assertSuccess(error, "Launch failed.")
55
56        state = process.GetState()
57        self.assertEqual(state, expected_state, "Didn't get expected state")
58
59        return process
60
61    def simple_auto_continue(self):
62        bpno = self.make_target_and_bkpt()
63        process = self.launch_it(lldb.eStateExited)
64
65        bkpt = self.target.FindBreakpointByID(bpno)
66        self.assertEqual(
67            bkpt.GetHitCount(), 2, "Should have run through the breakpoint twice"
68        )
69
70    def auto_continue_with_command(self):
71        bpno = self.make_target_and_bkpt(
72            "-N BKPT -C 'break modify --auto-continue 0 BKPT'"
73        )
74        process = self.launch_it(lldb.eStateStopped)
75        state = process.GetState()
76        self.assertState(state, lldb.eStateStopped, "Process should be stopped")
77        bkpt = self.target.FindBreakpointByID(bpno)
78        threads = lldbutil.get_threads_stopped_at_breakpoint(process, bkpt)
79        self.assertEqual(
80            len(threads), 1, "There was a thread stopped at our breakpoint"
81        )
82        self.assertEqual(bkpt.GetHitCount(), 2, "Should have hit the breakpoint twice")
83
84    def auto_continue_location(self):
85        bpno = self.make_target_and_bkpt(
86            pattern="Set a[^ ]* breakpoint here", num_expected_loc=2
87        )
88        bkpt = self.target.FindBreakpointByID(bpno)
89        bkpt.SetAutoContinue(False)
90
91        loc = lldb.SBBreakpointLocation()
92        for i in range(0, 2):
93            func_name = bkpt.location[i].GetAddress().function.name
94            if func_name == "main":
95                loc = bkpt.location[i]
96
97        self.assertTrue(loc.IsValid(), "Didn't find a location in main")
98        loc.SetAutoContinue(True)
99
100        process = self.launch_it(lldb.eStateStopped)
101
102        threads = lldbutil.get_threads_stopped_at_breakpoint(process, bkpt)
103        self.assertEqual(
104            len(threads), 1, "Didn't get one thread stopped at our breakpoint"
105        )
106        func_name = threads[0].frame[0].function.name
107        self.assertEqual(func_name, "call_me")
108