1"""
2Test lldb target stop-hook command.
3"""
4
5import os
6import unittest2
7import StringIO
8import lldb
9from lldbtest import *
10import lldbutil
11
12class StopHookCmdTestCase(TestBase):
13
14    mydir = os.path.join("functionalities", "stop-hook")
15
16    # Regression test.
17    def test_not_crashing_if_no_target(self):
18        """target stop-hook list should not crash if no target has been set."""
19        self.runCmd("target stop-hook list", check=False)
20
21    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
22    @dsym_test
23    def test_with_dsym(self):
24        """Test a sequence of target stop-hook commands."""
25        self.buildDsym()
26        self.stop_hook_cmd_sequence()
27
28    @dwarf_test
29    def test_with_dwarf(self):
30        """Test a sequence of target stop-hook commands."""
31        self.buildDwarf()
32        self.stop_hook_cmd_sequence()
33
34    def setUp(self):
35        # Call super's setUp().
36        TestBase.setUp(self)
37        # Find the line numbers inside main.cpp.
38        self.begl = line_number('main.cpp', '// Set breakpoint here to test target stop-hook.')
39        self.endl = line_number('main.cpp', '// End of the line range for which stop-hook is to be run.')
40        self.line = line_number('main.cpp', '// Another breakpoint which is outside of the stop-hook range.')
41
42    def stop_hook_cmd_sequence(self):
43        """Test a sequence of target stop-hook commands."""
44        exe = os.path.join(os.getcwd(), "a.out")
45        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
46
47        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.begl, num_expected_locations=1, loc_exact=True)
48
49        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
50
51        self.runCmd("target stop-hook add -f main.cpp -l %d -e %d -o 'expr ptr'" % (self.begl, self.endl))
52
53        self.expect('target stop-hook list', 'Stop Hook added successfully',
54            substrs = ['State: enabled',
55                       'expr ptr'])
56
57        self.runCmd('target stop-hook disable')
58
59        self.expect('target stop-hook list', 'Stop Hook disabled successfully',
60            substrs = ['State: disabled',
61                       'expr ptr'])
62
63        self.runCmd('target stop-hook enable')
64
65        self.expect('target stop-hook list', 'Stop Hook enabled successfully',
66            substrs = ['State: enabled',
67                       'expr ptr'])
68
69        self.runCmd("settings set auto-confirm true")
70        self.addTearDownHook(lambda: self.runCmd("settings clear auto-confirm"))
71
72        self.runCmd('target stop-hook delete')
73
74        self.expect('target stop-hook list', 'Stop Hook deleted successfully',
75            substrs = ['No stop hooks.'])
76
77
78if __name__ == '__main__':
79    import atexit
80    lldb.SBDebugger.Initialize()
81    atexit.register(lambda: lldb.SBDebugger.Terminate())
82    unittest2.main()
83