1"""
2Test breakpoint commands for a breakpoint ID with multiple locations.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class BreakpointLocationsTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "breakpoint", "breakpoint_locations")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @dsym_test
17    def test_with_dsym(self):
18        """Test breakpoint enable/disable for a breakpoint ID with multiple locations."""
19        self.buildDsym()
20        self.breakpoint_locations_test()
21
22    @dwarf_test
23    def test_with_dwarf(self):
24        """Test breakpoint enable/disable for a breakpoint ID with multiple locations."""
25        self.buildDwarf()
26        self.breakpoint_locations_test()
27
28    def setUp(self):
29        # Call super's setUp().
30        TestBase.setUp(self)
31        # Find the line number to break inside main().
32        self.line = line_number('main.c', '// Set break point at this line.')
33
34    def breakpoint_locations_test(self):
35        """Test breakpoint enable/disable for a breakpoint ID with multiple locations."""
36        exe = os.path.join(os.getcwd(), "a.out")
37        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
38
39        # This should create a breakpoint with 3 locations.
40        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=3)
41
42        # The breakpoint list should show 3 locations.
43        self.expect("breakpoint list -f", "Breakpoint locations shown correctly",
44            substrs = ["1: file = 'main.c', line = %d, locations = 3" % self.line],
45            patterns = ["where = a.out`func_inlined .+unresolved, hit count = 0",
46                        "where = a.out`main .+\[inlined\].+unresolved, hit count = 0"])
47
48        # The 'breakpoint disable 3.*' command should fail gracefully.
49        self.expect("breakpoint disable 3.*",
50                    "Disabling an invalid breakpoint should fail gracefully",
51                    error=True,
52            startstr = "error: '3' is not a valid breakpoint ID.")
53
54        # The 'breakpoint disable 1.*' command should disable all 3 locations.
55        self.expect("breakpoint disable 1.*", "All 3 breakpoint locatons disabled correctly",
56            startstr = "3 breakpoints disabled.")
57
58        # Run the program.
59        self.runCmd("run", RUN_SUCCEEDED)
60
61        # We should not stopped on any breakpoint at all.
62        self.expect("process status", "No stopping on any disabled breakpoint",
63            patterns = ["^Process [0-9]+ exited with status = 0"])
64
65        # The 'breakpoint enable 1.*' command should enable all 3 breakpoints.
66        self.expect("breakpoint enable 1.*", "All 3 breakpoint locatons enabled correctly",
67            startstr = "3 breakpoints enabled.")
68
69        # The 'breakpoint disable 1.1' command should disable 1 location.
70        self.expect("breakpoint disable 1.1", "1 breakpoint locatons disabled correctly",
71            startstr = "1 breakpoints disabled.")
72
73        # Run the program againt.  We should stop on the two breakpoint locations.
74        self.runCmd("run", RUN_SUCCEEDED)
75
76        # Stopped once.
77        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
78            substrs = ["stop reason = breakpoint 1."])
79
80        # Continue the program, there should be another stop.
81        self.runCmd("process continue")
82
83        # Stopped again.
84        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
85            substrs = ["stop reason = breakpoint 1."])
86
87        # At this point, 1.1 has a hit count of 0 and the other a hit count of 1".
88        self.expect("breakpoint list -f", "The breakpoints should report correct hit counts",
89            patterns = ["1\.1: .+ unresolved, hit count = 0 +Options: disabled",
90                        "1\.2: .+ resolved, hit count = 1",
91                        "1\.3: .+ resolved, hit count = 1"])
92
93
94if __name__ == '__main__':
95    import atexit
96    lldb.SBDebugger.Initialize()
97    atexit.register(lambda: lldb.SBDebugger.Terminate())
98    unittest2.main()
99