blob: f8e995cd57050263cc503baf46d9f52e796d4350 [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
16
17 @classmethod
18 def classCleanup(cls):
19 """Cleanup the test byproducts."""
20 cls.RemoveTempFile(cls.truncate_log_file)
21 cls.RemoveTempFile(cls.append_log_file)
Caroline Tice8607f032011-01-29 00:19:53 +000022
Robert Flack13c7ad92015-03-30 14:12:17 +000023 @skipUnlessDarwin
Johnny Chen24086bc2012-04-06 19:54:10 +000024 @dsym_test
Caroline Tice8607f032011-01-29 00:19:53 +000025 def test_with_dsym (self):
26 self.buildDsym ()
Johnny Chenec1ccca2011-01-29 00:52:54 +000027 self.command_log_tests ("dsym")
Caroline Tice8607f032011-01-29 00:19:53 +000028
Johnny Chen24086bc2012-04-06 19:54:10 +000029 @dwarf_test
Caroline Tice8607f032011-01-29 00:19:53 +000030 def test_with_dwarf (self):
31 self.buildDwarf ()
Johnny Chenec1ccca2011-01-29 00:52:54 +000032 self.command_log_tests ("dwarf")
Caroline Tice8607f032011-01-29 00:19:53 +000033
Johnny Chenec1ccca2011-01-29 00:52:54 +000034 def command_log_tests (self, type):
Caroline Tice8607f032011-01-29 00:19:53 +000035 exe = os.path.join (os.getcwd(), "a.out")
36 self.expect("file " + exe,
37 patterns = [ "Current executable set to .*a.out" ])
38
Johnny Chene0ec9ea2011-03-04 01:35:22 +000039 log_file = os.path.join (os.getcwd(), "lldb-commands-log-%s-%s-%s.txt" % (type,
Greg Claytonc1b2ccf2013-01-08 00:01:36 +000040 os.path.basename(self.getCompiler()),
Johnny Chene0ec9ea2011-03-04 01:35:22 +000041 self.getArchitecture()))
Caroline Tice8607f032011-01-29 00:19:53 +000042
43 if (os.path.exists (log_file)):
44 os.remove (log_file)
45
Michael Sartainc2052432013-08-01 18:51:08 +000046 # By default, Debugger::EnableLog() will set log options to
47 # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
48 # threadnames here, so we enable just threadsafe (-t).
49 self.runCmd ("log enable -t -f '%s' lldb commands" % (log_file))
Caroline Tice8607f032011-01-29 00:19:53 +000050
Johnny Chen72c40822011-04-21 20:55:57 +000051 self.runCmd ("command alias bp breakpoint")
Caroline Tice8607f032011-01-29 00:19:53 +000052
53 self.runCmd ("bp set -n main")
54
55 self.runCmd ("bp l")
56
Greg Claytonc7d88852014-02-12 23:46:08 +000057 self.runCmd("log disable lldb")
58
Caroline Tice8607f032011-01-29 00:19:53 +000059 self.assertTrue (os.path.isfile (log_file))
60
Caroline Tice8607f032011-01-29 00:19:53 +000061 f = open (log_file)
62 log_lines = f.readlines()
63 f.close ()
Caroline Tice8607f032011-01-29 00:19:53 +000064 os.remove (log_file)
65
Jim Inghamc49d0d42014-03-19 23:55:54 +000066 self.assertTrue(log_lines > 0, "Something was written to the log file.")
Caroline Tice8607f032011-01-29 00:19:53 +000067
Pavel Labath8ac06992015-03-20 09:43:20 +000068 # Check that lldb truncates its log files
69 def test_log_truncate (self):
70 if (os.path.exists (self.truncate_log_file)):
71 os.remove (self.truncate_log_file)
72
73 # put something in our log file
74 with open(self.truncate_log_file, "w") as f:
75 for i in range(1, 1000):
76 f.write("bacon\n")
77
78 self.runCmd ("log enable -t -f '%s' lldb commands" % (self.truncate_log_file))
79 self.runCmd ("help log")
80 self.runCmd ("log disable lldb")
81
82 self.assertTrue (os.path.isfile (self.truncate_log_file))
83 with open(self.truncate_log_file, "r") as f:
84 contents = f.read ()
85
86 # check that it got removed
87 self.assertTrue(string.find(contents, "bacon") == -1)
88
89 # Check that lldb can append to a log file
90 def test_log_append (self):
91 if (os.path.exists (self.append_log_file)):
92 os.remove (self.append_log_file)
93
94 # put something in our log file
95 with open(self.append_log_file, "w") as f:
96 f.write("bacon\n")
97
98 self.runCmd ("log enable -t -a -f '%s' lldb commands" % (self.append_log_file))
99 self.runCmd ("help log")
100 self.runCmd ("log disable lldb")
101
102 self.assertTrue (os.path.isfile (self.append_log_file))
103 with open(self.append_log_file, "r") as f:
104 contents = f.read ()
105
106 # check that it is still there
107 self.assertTrue(string.find(contents, "bacon") == 0)
108
109
Caroline Tice8607f032011-01-29 00:19:53 +0000110if __name__ == '__main__':
111 import atexit
112 lldb.SBDebugger.Initialize()
113 atexit.register(lambda: lldb.SBDebugger.Terminate())
114 unittest2.main()
115