1"""
2Test the "process continue -b" option.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class TestContinueToBkpts(TestBase):
13
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @add_test_categories(['pyapi'])
17    def test_continue_to_breakpoints(self):
18        """Test that the continue to breakpoints feature works correctly."""
19        self.build()
20        self.do_test_continue_to_breakpoint()
21
22    def setUp(self):
23        # Call super's setUp().
24        TestBase.setUp(self)
25        self.main_source_spec = lldb.SBFileSpec("main.c")
26
27    def continue_and_check(self, stop_list, bkpt_to_hit, loc_to_hit = 0):
28        """Build up a command that will run a continue -b commands using the breakpoints on stop_list, and
29           ensure that we hit bkpt_to_hit.
30           If loc_to_hit is not 0, also verify that we hit that location."""
31        command = "process continue"
32        for elem in stop_list:
33            command += " -b {0}".format(elem)
34        self.expect(command)
35        self.assertEqual(self.thread.stop_reason, lldb.eStopReasonBreakpoint, "Hit a breakpoint")
36        self.assertEqual(self.thread.GetStopReasonDataAtIndex(0), bkpt_to_hit, "Hit the right breakpoint")
37        if loc_to_hit != 0:
38            self.assertEqual(self.thread.GetStopReasonDataAtIndex(1), loc_to_hit, "Hit the right location")
39        for bkpt_id in self.bkpt_list:
40            bkpt = self.target.FindBreakpointByID(bkpt_id)
41            self.assertTrue(bkpt.IsValid(), "Breakpoint id's round trip")
42            if bkpt.MatchesName("disabled"):
43                self.assertFalse(bkpt.IsEnabled(), "Disabled breakpoints stay disabled: {0}".format(bkpt.GetID()))
44            else:
45                self.assertTrue(bkpt.IsEnabled(), "Enabled breakpoints stay enabled: {0}".format(bkpt.GetID()))
46        # Also do our multiple location one:
47        bkpt = self.target.FindBreakpointByID(self.multiple_loc_id)
48        self.assertTrue(bkpt.IsValid(), "Breakpoint with locations round trip")
49        for i in range(1,3):
50            loc = bkpt.FindLocationByID(i)
51            self.assertTrue(loc.IsValid(), "Locations round trip")
52            if i == 2:
53                self.assertTrue(loc.IsEnabled(), "Locations that were enabled stay enabled")
54            else:
55                self.assertFalse(loc.IsEnabled(), "Locations that were disabled stay disabled")
56
57    def do_test_continue_to_breakpoint(self):
58        """Test the continue to breakpoint feature."""
59        (self.target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
60                                   "Stop here to get started", self.main_source_spec)
61
62        # Now set up all our breakpoints:
63        bkpt_pattern = "This is the {0} stop"
64        bkpt_elements = ["zeroth", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth"]
65        disabled_bkpts = ["first", "eigth"]
66        bkpts_for_MyBKPT = ["first", "sixth", "nineth"]
67        self.bkpt_list = []
68        for elem in bkpt_elements:
69            bkpt = self.target.BreakpointCreateBySourceRegex(bkpt_pattern.format(elem), self.main_source_spec)
70            self.assertGreater(bkpt.GetNumLocations(), 0, "Found a bkpt match")
71            self.bkpt_list.append(bkpt.GetID())
72            bkpt.AddName(elem)
73            if elem in disabled_bkpts:
74                bkpt.AddName("disabled")
75                bkpt.SetEnabled(False)
76            if elem in bkpts_for_MyBKPT:
77                bkpt.AddName("MyBKPT")
78        # Also make one that has several locations, so we can test locations:
79        mult_bkpt = self.target.BreakpointCreateBySourceRegex(bkpt_pattern.format("(seventh|eighth|nineth)"), self.main_source_spec)
80        self.assertEqual(mult_bkpt.GetNumLocations(), 3, "Got three matches")
81        mult_bkpt.AddName("Locations")
82        # Disable all of these:
83        for i in range(1,4):
84            loc = mult_bkpt.FindLocationByID(i)
85            self.assertTrue(loc.IsValid(), "Location {0} is valid".format(i))
86            loc.SetEnabled(False)
87            self.assertFalse(loc.IsEnabled(), "Loc {0} wasn't disabled".format(i))
88        self.multiple_loc_id = mult_bkpt.GetID()
89
90        # First test out various error conditions
91
92        # All locations of the multiple_loc_id are disabled, so running to this should be an error:
93        self.expect("process continue -b {0}".format(self.multiple_loc_id), error=True, msg="Running to a disabled breakpoint by number")
94
95        # Now re-enable the middle one so we can run to it:
96        loc = mult_bkpt.FindLocationByID(2)
97        loc.SetEnabled(True)
98
99        self.expect("process continue -b {0}".format(self.bkpt_list[1]), error=True, msg="Running to a disabled breakpoint by number")
100        self.expect("process continue -b {0}.1".format(self.bkpt_list[1]), error=True, msg="Running to a location of a disabled breakpoint")
101        self.expect("process continue -b disabled", error=True, msg="Running to a disabled set of breakpoints")
102        self.expect("process continue -b {0}.{1}".format(self.multiple_loc_id, 1), error=True, msg="Running to a disabled breakpoint location")
103        self.expect("process continue -b {0}".format("THERE_ARE_NO_BREAKPOINTS_BY_THIS_NAME"), error=True, msg="Running to no such name")
104        self.expect("process continue -b {0}".format(1000), error=True, msg="Running to no such breakpoint")
105        self.expect("process continue -b {0}.{1}".format(self.multiple_loc_id, 1000), error=True, msg="Running to no such location")
106
107        # Now move forward, this time with breakpoint numbers.  First time we don't skip other bkpts.
108        bkpt = self.bkpt_list[0]
109        self.continue_and_check([str(bkpt)], bkpt)
110
111        # Now skip to the third stop, do it by name and supply one of the later breakpoints as well:
112        # This continue has to muck with the sync mode of the debugger, so let's make sure we
113        # put it back.  First try if it was in sync mode:
114        orig_async = self.dbg.GetAsync()
115        self.dbg.SetAsync(True)
116        self.continue_and_check([bkpt_elements[2], bkpt_elements[7]], self.bkpt_list[2])
117        after_value = self.dbg.GetAsync()
118        self.dbg.SetAsync(orig_async)
119        self.assertTrue(after_value, "Preserve async as True if it started that way")
120
121        # Now try a name that has several breakpoints.
122        # This time I'm also going to check that we put the debugger async mode back if
123        # if was False to begin with:
124        self.dbg.SetAsync(False)
125        self.continue_and_check(["MyBKPT"], self.bkpt_list[6])
126        after_value = self.dbg.GetAsync()
127        self.dbg.SetAsync(orig_async)
128        self.assertFalse(after_value, "Preserve async as False if it started that way")
129
130        # Now let's run to a particular location.  Also specify a breakpoint we've already hit:
131        self.continue_and_check([self.bkpt_list[0], self.multiple_loc_id], self.multiple_loc_id, 2)
132