1"""
2Test lldb logging.  This test just makes sure logging doesn't crash, and produces some output.
3"""
4
5
6
7import os
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class LogTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17    NO_DEBUG_INFO_TESTCASE = True
18
19    def setUp(self):
20        super(LogTestCase, self).setUp()
21        self.log_file = self.getBuildArtifact("log-file.txt")
22
23        if configuration.is_reproducer_replay():
24            self.log_file = self.getReproducerRemappedPath(self.log_file)
25
26    def test_file_writing(self):
27        self.build()
28        exe = self.getBuildArtifact("a.out")
29        self.expect("file " + exe,
30                    patterns=["Current executable set to .*a.out"])
31
32        if (os.path.exists(self.log_file)):
33            os.remove(self.log_file)
34
35        # By default, Debugger::EnableLog() will set log options to
36        # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
37        # threadnames here, so we enable just threadsafe (-t).
38        self.runCmd("log enable -t -f '%s' lldb commands" % (self.log_file))
39
40        self.runCmd("command alias bp breakpoint")
41
42        self.runCmd("bp set -n main")
43
44        self.runCmd("bp l")
45
46        self.runCmd("log disable lldb")
47
48        self.assertTrue(os.path.isfile(self.log_file))
49
50        with open(self.log_file, 'r') as f:
51            log_lines = f.read()
52        os.remove(self.log_file)
53
54        self.assertGreater(
55            len(log_lines),
56            0,
57            "Something was written to the log file.")
58
59    # Check that lldb truncates its log files
60    def test_log_truncate(self):
61        # put something in our log file
62        with open(self.log_file, "w") as f:
63            for i in range(1, 1000):
64                f.write("bacon\n")
65
66        self.runCmd("log enable -t -f '%s' lldb commands" % self.log_file)
67        self.runCmd("help log")
68        self.runCmd("log disable lldb")
69
70        self.assertTrue(os.path.isfile(self.log_file))
71        with open(self.log_file, "r") as f:
72            contents = f.read()
73
74        # check that it got removed
75        self.assertEquals(contents.find("bacon"), -1)
76
77    # Check that lldb can append to a log file
78    def test_log_append(self):
79        # put something in our log file
80        with open(self.log_file, "w") as f:
81            f.write("bacon\n")
82
83        self.runCmd( "log enable -t -a -f '%s' lldb commands" % self.log_file)
84        self.runCmd("help log")
85        self.runCmd("log disable lldb")
86
87        self.assertTrue(os.path.isfile(self.log_file))
88        with open(self.log_file, 'r') as f:
89            contents = f.read()
90
91        # check that it is still there
92        self.assertEquals(contents.find("bacon"), 0)
93
94    # Enable all log options and check that nothing crashes.
95    @skipIfWindows
96    def test_all_log_options(self):
97        if (os.path.exists(self.log_file)):
98            os.remove(self.log_file)
99
100        self.runCmd("log enable -v -t -s -T -p -n -S -F -f '%s' lldb commands" % self.log_file)
101        self.runCmd("help log")
102        self.runCmd("log disable lldb")
103
104        self.assertTrue(os.path.isfile(self.log_file))
105