blob: a76c149893de47b348e61f4ed14da50dab076c10 [file] [log] [blame]
Caroline Tice8607f032011-01-29 00:19:53 +00001"""
Jim Inghamc49d0d42014-03-19 23:55:54 +00002Test lldb logging. This test just makes sure logging doesn't crash, and produces some output.
Caroline Tice8607f032011-01-29 00:19:53 +00003"""
4
Pavel Labath8ac06992015-03-20 09:43:20 +00005import os, time, string
Caroline Tice8607f032011-01-29 00:19:53 +00006import unittest2
7import lldb
8from lldbtest import *
9
10class LogTestCase(TestBase):
11
Greg Clayton4570d3e2013-12-10 23:19:29 +000012 mydir = TestBase.compute_mydir(__file__)
Pavel Labath8ac06992015-03-20 09:43:20 +000013 append_log_file = "lldb-commands-log-append.txt"
14 truncate_log_file = "lldb-commands-log-truncate.txt"
15
Pavel Labath8ac06992015-03-20 09:43:20 +000016 @classmethod
17 def classCleanup(cls):
18 """Cleanup the test byproducts."""
19 cls.RemoveTempFile(cls.truncate_log_file)
20 cls.RemoveTempFile(cls.append_log_file)
Caroline Tice8607f032011-01-29 00:19:53 +000021
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000022 def test (self):
23 self.build()
24 if self.debug_info == "dsym":
25 self.command_log_tests ("dsym")
26 else:
27 self.command_log_tests ("dwarf")
Caroline Tice8607f032011-01-29 00:19:53 +000028
Johnny Chenec1ccca2011-01-29 00:52:54 +000029 def command_log_tests (self, type):
Caroline Tice8607f032011-01-29 00:19:53 +000030 exe = os.path.join (os.getcwd(), "a.out")
31 self.expect("file " + exe,
32 patterns = [ "Current executable set to .*a.out" ])
33
Johnny Chene0ec9ea2011-03-04 01:35:22 +000034 log_file = os.path.join (os.getcwd(), "lldb-commands-log-%s-%s-%s.txt" % (type,
Greg Claytonc1b2ccf2013-01-08 00:01:36 +000035 os.path.basename(self.getCompiler()),
Johnny Chene0ec9ea2011-03-04 01:35:22 +000036 self.getArchitecture()))
Caroline Tice8607f032011-01-29 00:19:53 +000037
38 if (os.path.exists (log_file)):
39 os.remove (log_file)
40
Michael Sartainc2052432013-08-01 18:51:08 +000041 # By default, Debugger::EnableLog() will set log options to
42 # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
43 # threadnames here, so we enable just threadsafe (-t).
44 self.runCmd ("log enable -t -f '%s' lldb commands" % (log_file))
Caroline Tice8607f032011-01-29 00:19:53 +000045
Johnny Chen72c40822011-04-21 20:55:57 +000046 self.runCmd ("command alias bp breakpoint")
Caroline Tice8607f032011-01-29 00:19:53 +000047
48 self.runCmd ("bp set -n main")
49
50 self.runCmd ("bp l")
51
Greg Claytonc7d88852014-02-12 23:46:08 +000052 self.runCmd("log disable lldb")
53
Caroline Tice8607f032011-01-29 00:19:53 +000054 self.assertTrue (os.path.isfile (log_file))
55
Caroline Tice8607f032011-01-29 00:19:53 +000056 f = open (log_file)
57 log_lines = f.readlines()
58 f.close ()
Caroline Tice8607f032011-01-29 00:19:53 +000059 os.remove (log_file)
60
Jim Inghamc49d0d42014-03-19 23:55:54 +000061 self.assertTrue(log_lines > 0, "Something was written to the log file.")
Caroline Tice8607f032011-01-29 00:19:53 +000062
Pavel Labath8ac06992015-03-20 09:43:20 +000063 # Check that lldb truncates its log files
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000064 @no_debug_info_test
Pavel Labath8ac06992015-03-20 09:43:20 +000065 def test_log_truncate (self):
66 if (os.path.exists (self.truncate_log_file)):
67 os.remove (self.truncate_log_file)
68
69 # put something in our log file
70 with open(self.truncate_log_file, "w") as f:
71 for i in range(1, 1000):
72 f.write("bacon\n")
73
74 self.runCmd ("log enable -t -f '%s' lldb commands" % (self.truncate_log_file))
75 self.runCmd ("help log")
76 self.runCmd ("log disable lldb")
77
78 self.assertTrue (os.path.isfile (self.truncate_log_file))
79 with open(self.truncate_log_file, "r") as f:
80 contents = f.read ()
81
82 # check that it got removed
83 self.assertTrue(string.find(contents, "bacon") == -1)
84
85 # Check that lldb can append to a log file
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000086 @no_debug_info_test
Pavel Labath8ac06992015-03-20 09:43:20 +000087 def test_log_append (self):
88 if (os.path.exists (self.append_log_file)):
89 os.remove (self.append_log_file)
90
91 # put something in our log file
92 with open(self.append_log_file, "w") as f:
93 f.write("bacon\n")
94
95 self.runCmd ("log enable -t -a -f '%s' lldb commands" % (self.append_log_file))
96 self.runCmd ("help log")
97 self.runCmd ("log disable lldb")
98
99 self.assertTrue (os.path.isfile (self.append_log_file))
100 with open(self.append_log_file, "r") as f:
101 contents = f.read ()
102
103 # check that it is still there
104 self.assertTrue(string.find(contents, "bacon") == 0)
105
Caroline Tice8607f032011-01-29 00:19:53 +0000106if __name__ == '__main__':
107 import atexit
108 lldb.SBDebugger.Initialize()
109 atexit.register(lambda: lldb.SBDebugger.Terminate())
110 unittest2.main()
111