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