1c8448dca15d526a8681954f5773981bb99923affCaroline Tice"""
2c8448dca15d526a8681954f5773981bb99923affCaroline TiceTest lldb logging.
3c8448dca15d526a8681954f5773981bb99923affCaroline Tice"""
4c8448dca15d526a8681954f5773981bb99923affCaroline Tice
5c8448dca15d526a8681954f5773981bb99923affCaroline Ticeimport os, time
6c8448dca15d526a8681954f5773981bb99923affCaroline Ticeimport unittest2
7c8448dca15d526a8681954f5773981bb99923affCaroline Ticeimport lldb
8c8448dca15d526a8681954f5773981bb99923affCaroline Ticefrom lldbtest import *
9c8448dca15d526a8681954f5773981bb99923affCaroline Tice
10c8448dca15d526a8681954f5773981bb99923affCaroline Ticeclass LogTestCase(TestBase):
11c8448dca15d526a8681954f5773981bb99923affCaroline Tice
12c8448dca15d526a8681954f5773981bb99923affCaroline Tice    mydir = "logging"
13c8448dca15d526a8681954f5773981bb99923affCaroline Tice
14c8448dca15d526a8681954f5773981bb99923affCaroline Tice    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
1521b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dsym_test
16c8448dca15d526a8681954f5773981bb99923affCaroline Tice    def test_with_dsym (self):
17c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.buildDsym ()
18989bc04d3e5aae5524cfa26ad0d9e20a0e35d056Johnny Chen        self.command_log_tests ("dsym")
19c8448dca15d526a8681954f5773981bb99923affCaroline Tice
2021b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dwarf_test
21c8448dca15d526a8681954f5773981bb99923affCaroline Tice    def test_with_dwarf (self):
22c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.buildDwarf ()
23989bc04d3e5aae5524cfa26ad0d9e20a0e35d056Johnny Chen        self.command_log_tests ("dwarf")
24c8448dca15d526a8681954f5773981bb99923affCaroline Tice
25989bc04d3e5aae5524cfa26ad0d9e20a0e35d056Johnny Chen    def command_log_tests (self, type):
26c8448dca15d526a8681954f5773981bb99923affCaroline Tice        exe = os.path.join (os.getcwd(), "a.out")
27c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.expect("file " + exe,
28c8448dca15d526a8681954f5773981bb99923affCaroline Tice                    patterns = [ "Current executable set to .*a.out" ])
29c8448dca15d526a8681954f5773981bb99923affCaroline Tice
301a4d5e7b6eb2283b4c7d8e308f4087f39cc05ed6Johnny Chen        log_file = os.path.join (os.getcwd(), "lldb-commands-log-%s-%s-%s.txt" % (type,
312fcbf6e3d86ac0e6a95e11e5e232a9f72bd612d2Greg Clayton                                                                                  os.path.basename(self.getCompiler()),
321a4d5e7b6eb2283b4c7d8e308f4087f39cc05ed6Johnny Chen                                                                                  self.getArchitecture()))
33c8448dca15d526a8681954f5773981bb99923affCaroline Tice
34c8448dca15d526a8681954f5773981bb99923affCaroline Tice        if (os.path.exists (log_file)):
35c8448dca15d526a8681954f5773981bb99923affCaroline Tice            os.remove (log_file)
36c8448dca15d526a8681954f5773981bb99923affCaroline Tice
377526625a76ca97a2637749b0cc4068bf0bd907fbMichael Sartain        # By default, Debugger::EnableLog() will set log options to
387526625a76ca97a2637749b0cc4068bf0bd907fbMichael Sartain        # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
397526625a76ca97a2637749b0cc4068bf0bd907fbMichael Sartain        # threadnames here, so we enable just threadsafe (-t).
407526625a76ca97a2637749b0cc4068bf0bd907fbMichael Sartain        self.runCmd ("log enable -t -f '%s' lldb commands" % (log_file))
41c8448dca15d526a8681954f5773981bb99923affCaroline Tice
4226d132f874bcc1b8311b24cd4c14c17c27de9ff1Johnny Chen        self.runCmd ("command alias bp breakpoint")
43c8448dca15d526a8681954f5773981bb99923affCaroline Tice
44c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.runCmd ("bp set -n main")
45c8448dca15d526a8681954f5773981bb99923affCaroline Tice
46c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.runCmd ("bp l")
47c8448dca15d526a8681954f5773981bb99923affCaroline Tice
48c8448dca15d526a8681954f5773981bb99923affCaroline Tice        expected_log_lines = [
49169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "Processing command: command alias bp breakpoint\n",
50169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, cmd_obj : 'command alias'\n",
51169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, revised_command_line: 'command alias bp breakpoint'\n",
52169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, wants_raw_input:'True'\n",
53169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command line after removing command name(s): 'bp breakpoint'\n",
54169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command succeeded\n",
55169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "Processing command: bp set -n main\n",
56169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, cmd_obj : 'breakpoint set'\n",
57169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, revised_command_line: 'breakpoint set -n main'\n",
58169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, wants_raw_input:'False'\n",
59169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command line after removing command name(s): '-n main'\n",
60169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command succeeded\n",
61169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "Processing command: bp l\n",
62169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, cmd_obj : 'breakpoint list'\n",
63169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, revised_command_line: 'breakpoint l'\n",
64169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, wants_raw_input:'False'\n",
65169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command line after removing command name(s): ''\n",
66169cd51c9e085a2140c2b4f54f87c68f1cc4d859Daniel Malea            "HandleCommand, command succeeded\n",
67c8448dca15d526a8681954f5773981bb99923affCaroline Tice            ]
68c8448dca15d526a8681954f5773981bb99923affCaroline Tice
69c8448dca15d526a8681954f5773981bb99923affCaroline Tice        self.assertTrue (os.path.isfile (log_file))
70c8448dca15d526a8681954f5773981bb99923affCaroline Tice
71c8448dca15d526a8681954f5773981bb99923affCaroline Tice        idx = 0
72c8448dca15d526a8681954f5773981bb99923affCaroline Tice        end = len (expected_log_lines)
73c8448dca15d526a8681954f5773981bb99923affCaroline Tice        f = open (log_file)
74c8448dca15d526a8681954f5773981bb99923affCaroline Tice        log_lines = f.readlines()
75c8448dca15d526a8681954f5773981bb99923affCaroline Tice        f.close ()
76989bc04d3e5aae5524cfa26ad0d9e20a0e35d056Johnny Chen        self.runCmd("log disable lldb")
77c8448dca15d526a8681954f5773981bb99923affCaroline Tice        os.remove (log_file)
78c8448dca15d526a8681954f5773981bb99923affCaroline Tice
79c8448dca15d526a8681954f5773981bb99923affCaroline Tice        err_msg = ""
80c8448dca15d526a8681954f5773981bb99923affCaroline Tice        success = True
81c8448dca15d526a8681954f5773981bb99923affCaroline Tice
82c8448dca15d526a8681954f5773981bb99923affCaroline Tice        if len (log_lines) != len (expected_log_lines):
83c8448dca15d526a8681954f5773981bb99923affCaroline Tice            success = False
84c8448dca15d526a8681954f5773981bb99923affCaroline Tice            err_msg = "Wrong number of lines in log file; expected: " + repr (len (expected_log_lines)) + " found: " + repr(len (log_lines))
85c8448dca15d526a8681954f5773981bb99923affCaroline Tice        else:
86c8448dca15d526a8681954f5773981bb99923affCaroline Tice            for line1, line2 in zip (log_lines, expected_log_lines):
87c8448dca15d526a8681954f5773981bb99923affCaroline Tice                if line1 != line2:
88c8448dca15d526a8681954f5773981bb99923affCaroline Tice                    success = False
89c8448dca15d526a8681954f5773981bb99923affCaroline Tice                    err_msg = "Expected '" + line2 + "'; Found '" + line1 + "'"
90c8448dca15d526a8681954f5773981bb99923affCaroline Tice                    break
91c8448dca15d526a8681954f5773981bb99923affCaroline Tice
92c8448dca15d526a8681954f5773981bb99923affCaroline Tice        if not success:
93c8448dca15d526a8681954f5773981bb99923affCaroline Tice            self.fail (err_msg)
94c8448dca15d526a8681954f5773981bb99923affCaroline Tice
95c8448dca15d526a8681954f5773981bb99923affCaroline Tice
96c8448dca15d526a8681954f5773981bb99923affCaroline Ticeif __name__ == '__main__':
97c8448dca15d526a8681954f5773981bb99923affCaroline Tice    import atexit
98c8448dca15d526a8681954f5773981bb99923affCaroline Tice    lldb.SBDebugger.Initialize()
99c8448dca15d526a8681954f5773981bb99923affCaroline Tice    atexit.register(lambda: lldb.SBDebugger.Terminate())
100c8448dca15d526a8681954f5773981bb99923affCaroline Tice    unittest2.main()
101c8448dca15d526a8681954f5773981bb99923affCaroline Tice
102