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