1"""
2Test breakpoint hit count features.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class BreakpointHitCountTestCase(TestBase):
13
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @add_test_categories(['pyapi'])
17    def test_breakpoint_location_hit_count(self):
18        """Use Python APIs to check breakpoint hit count."""
19        self.build()
20        self.do_test_breakpoint_location_hit_count()
21
22    def test_breakpoint_one_shot(self):
23        """Check that one-shot breakpoints trigger only once."""
24        self.build()
25        target = self.createTestTarget()
26
27        self.runCmd("tb a")
28        process = target.LaunchSimple(
29            None, None, self.get_process_working_directory())
30        self.assertTrue(process, PROCESS_IS_VALID)
31
32        from lldbsuite.test.lldbutil import get_stopped_thread
33        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
34        self.assertTrue(
35            thread.IsValid(),
36            "There should be a thread stopped due to breakpoint")
37
38        frame0 = thread.GetFrameAtIndex(0)
39        self.assertTrue(frame0.GetFunctionName() == "a(int)" or frame0.GetFunctionName() == "int a(int)");
40
41        process.Continue()
42        self.assertState(process.GetState(), lldb.eStateExited)
43
44    def setUp(self):
45        # Call super's setUp().
46        TestBase.setUp(self)
47        self.a_int_body_line_no = line_number(
48            'main.cpp', '// Breakpoint Location 1')
49        self.a_float_body_line_no = line_number(
50            'main.cpp', '// Breakpoint Location 2')
51
52    def do_test_breakpoint_location_hit_count(self):
53        """Use Python APIs to check breakpoint hit count."""
54        target = self.createTestTarget()
55
56        # Create a breakpoint in main.cpp by name 'a',
57        # there should be two locations.
58        breakpoint = target.BreakpointCreateByName('a', 'a.out')
59        self.assertTrue(breakpoint and
60                        breakpoint.GetNumLocations() == 2,
61                        VALID_BREAKPOINT)
62
63        # Verify all breakpoint locations are enabled.
64        location1 = breakpoint.GetLocationAtIndex(0)
65        self.assertTrue(location1 and
66                        location1.IsEnabled(),
67                        VALID_BREAKPOINT_LOCATION)
68
69        location2 = breakpoint.GetLocationAtIndex(1)
70        self.assertTrue(location2 and
71                        location2.IsEnabled(),
72                        VALID_BREAKPOINT_LOCATION)
73
74        # Launch the process, and do not stop at entry point.
75        process = target.LaunchSimple(
76            None, None, self.get_process_working_directory())
77        self.assertTrue(process, PROCESS_IS_VALID)
78
79        # Verify 1st breakpoint location is hit.
80        from lldbsuite.test.lldbutil import get_stopped_thread
81        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
82        self.assertTrue(
83            thread.IsValid(),
84            "There should be a thread stopped due to breakpoint")
85
86        frame0 = thread.GetFrameAtIndex(0)
87        location1 = breakpoint.FindLocationByAddress(frame0.GetPC())
88        self.assertEqual(
89            frame0.GetLineEntry().GetLine(), self.a_int_body_line_no,
90            "Stopped in int a(int)")
91        self.assertTrue(location1)
92        self.assertEqual(location1.GetHitCount(), 1)
93        self.assertEqual(breakpoint.GetHitCount(), 1)
94
95        process.Continue()
96
97        # Verify 2nd breakpoint location is hit.
98        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
99        self.assertTrue(
100            thread.IsValid(),
101            "There should be a thread stopped due to breakpoint")
102
103        frame0 = thread.GetFrameAtIndex(0)
104        location2 = breakpoint.FindLocationByAddress(frame0.GetPC())
105        self.assertEqual(
106            frame0.GetLineEntry().GetLine(), self.a_float_body_line_no,
107            "Stopped in float a(float)")
108        self.assertTrue(location2)
109        self.assertEqual(location2.GetHitCount(), 1)
110        self.assertEqual(location1.GetHitCount(), 1)
111        self.assertEqual(breakpoint.GetHitCount(), 2)
112
113        process.Continue()
114
115        # Verify 2nd breakpoint location is hit again.
116        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
117        self.assertTrue(
118            thread.IsValid(),
119            "There should be a thread stopped due to breakpoint")
120
121        self.assertEqual(location2.GetHitCount(), 2)
122        self.assertEqual(location1.GetHitCount(), 1)
123        self.assertEqual(breakpoint.GetHitCount(), 3)
124
125        process.Continue()
126