1"""
2Test that breakpoint works correctly in the presence of dead-code stripping.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class DeadStripTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "dead-strip")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @dsym_test
17    def test_with_dsym(self):
18        """Test breakpoint works correctly with dead-code stripping."""
19        self.buildDsym()
20        self.dead_strip()
21
22    @skipIfFreeBSD # The -dead_strip linker option isn't supported on FreeBSD versions of ld.
23    @skipIfLinux # The -dead_strip linker option isn't supported on Linux versions of ld.
24    @dwarf_test
25    def test_with_dwarf(self):
26        """Test breakpoint works correctly with dead-code stripping."""
27        self.buildDwarf()
28        self.dead_strip()
29
30    def dead_strip(self):
31        """Test breakpoint works correctly with dead-code stripping."""
32        exe = os.path.join(os.getcwd(), "a.out")
33        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
34
35        # Break by function name f1 (live code).
36        lldbutil.run_break_set_by_symbol (self, "f1", num_expected_locations=1, module_name="a.out")
37
38        # Break by function name f2 (dead code).
39        lldbutil.run_break_set_by_symbol (self, "f2", num_expected_locations=0, module_name="a.out")
40
41        # Break by function name f3 (live code).
42        lldbutil.run_break_set_by_symbol (self, "f3", num_expected_locations=1, module_name="a.out")
43
44        self.runCmd("run", RUN_SUCCEEDED)
45
46        # The stop reason of the thread should be breakpoint (breakpoint #1).
47        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
48            substrs = ['stopped',
49                       'a.out`f1',
50                       'stop reason = breakpoint'])
51
52        # The breakpoint should have a hit count of 1.
53        self.expect("breakpoint list -f 1", BREAKPOINT_HIT_ONCE,
54            substrs = [' resolved, hit count = 1'])
55
56        self.runCmd("continue")
57
58        # The stop reason of the thread should be breakpoint (breakpoint #3).
59        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
60            substrs = ['stopped',
61                       'a.out`f3',
62                       'stop reason = breakpoint'])
63
64        # The breakpoint should have a hit count of 1.
65        self.expect("breakpoint list -f 3", BREAKPOINT_HIT_ONCE,
66            substrs = [' resolved, hit count = 1'])
67
68
69if __name__ == '__main__':
70    import atexit
71    lldb.SBDebugger.Initialize()
72    atexit.register(lambda: lldb.SBDebugger.Terminate())
73    unittest2.main()
74