1"""
2Test breakpoint hit count is reset when target runs.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8
9
10class HitcountResetUponRun(TestBase):
11    BREAKPOINT_TEXT = "Set a breakpoint here"
12
13    def check_stopped_at_breakpoint_and_hit_once(self, thread, breakpoint):
14        frame0 = thread.GetFrameAtIndex(0)
15        location1 = breakpoint.FindLocationByAddress(frame0.GetPC())
16        self.assertTrue(location1)
17        self.assertEqual(location1.GetHitCount(), 1)
18        self.assertEqual(breakpoint.GetHitCount(), 1)
19
20    def test_hitcount_reset_upon_run(self):
21        self.build()
22
23        exe = self.getBuildArtifact("a.out")
24
25        target = self.dbg.CreateTarget(exe)
26        self.assertTrue(target, VALID_TARGET)
27
28        breakpoint = target.BreakpointCreateBySourceRegex(
29            self.BREAKPOINT_TEXT, lldb.SBFileSpec("main.cpp")
30        )
31        self.assertTrue(
32            breakpoint.IsValid() and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
33        )
34
35        process = target.LaunchSimple(None, None, self.get_process_working_directory())
36        self.assertTrue(process, PROCESS_IS_VALID)
37
38        from lldbsuite.test.lldbutil import get_stopped_thread
39
40        # Verify 1st breakpoint location is hit.
41        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
42        self.assertTrue(
43            thread.IsValid(), "There should be a thread stopped due to breakpoint"
44        )
45        self.check_stopped_at_breakpoint_and_hit_once(thread, breakpoint)
46
47        # Relaunch
48        process.Kill()
49        process = target.LaunchSimple(None, None, self.get_process_working_directory())
50        self.assertTrue(process, PROCESS_IS_VALID)
51
52        # Verify the hit counts are still one.
53        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
54        self.assertTrue(
55            thread.IsValid(), "There should be a thread stopped due to breakpoint"
56        )
57        self.check_stopped_at_breakpoint_and_hit_once(thread, breakpoint)
58