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