blob: cb42877c11efc4e1de2c0da647fd447da5146b8f [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
Zachary Turner35d017f2015-10-23 17:04:29 +00005from __future__ import print_function
6
Zachary Turner19474e12015-11-03 19:20:39 +00007
Zachary Turner77db4a82015-10-22 20:06:20 +00008
Pavel Labath8ac06992015-03-20 09:43:20 +00009import os, time, string
Caroline Tice8607f032011-01-29 00:19:53 +000010import lldb
Zachary Turner95c453a2015-11-03 02:06:18 +000011from lldbsuite.test.lldbtest import *
Caroline Tice8607f032011-01-29 00:19:53 +000012
13class LogTestCase(TestBase):
14
Greg Clayton4570d3e2013-12-10 23:19:29 +000015 mydir = TestBase.compute_mydir(__file__)
Pavel Labath8ac06992015-03-20 09:43:20 +000016 append_log_file = "lldb-commands-log-append.txt"
17 truncate_log_file = "lldb-commands-log-truncate.txt"
18
Pavel Labath8ac06992015-03-20 09:43:20 +000019 @classmethod
20 def classCleanup(cls):
21 """Cleanup the test byproducts."""
22 cls.RemoveTempFile(cls.truncate_log_file)
23 cls.RemoveTempFile(cls.append_log_file)
Caroline Tice8607f032011-01-29 00:19:53 +000024
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000025 def test (self):
26 self.build()
27 if self.debug_info == "dsym":
28 self.command_log_tests ("dsym")
29 else:
30 self.command_log_tests ("dwarf")
Caroline Tice8607f032011-01-29 00:19:53 +000031
Johnny Chenec1ccca2011-01-29 00:52:54 +000032 def command_log_tests (self, type):
Caroline Tice8607f032011-01-29 00:19:53 +000033 exe = os.path.join (os.getcwd(), "a.out")
34 self.expect("file " + exe,
35 patterns = [ "Current executable set to .*a.out" ])
36
Johnny Chene0ec9ea2011-03-04 01:35:22 +000037 log_file = os.path.join (os.getcwd(), "lldb-commands-log-%s-%s-%s.txt" % (type,
Greg Claytonc1b2ccf2013-01-08 00:01:36 +000038 os.path.basename(self.getCompiler()),
Johnny Chene0ec9ea2011-03-04 01:35:22 +000039 self.getArchitecture()))
Caroline Tice8607f032011-01-29 00:19:53 +000040
41 if (os.path.exists (log_file)):
42 os.remove (log_file)
43
Michael Sartainc2052432013-08-01 18:51:08 +000044 # By default, Debugger::EnableLog() will set log options to
45 # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
46 # threadnames here, so we enable just threadsafe (-t).
47 self.runCmd ("log enable -t -f '%s' lldb commands" % (log_file))
Caroline Tice8607f032011-01-29 00:19:53 +000048
Johnny Chen72c40822011-04-21 20:55:57 +000049 self.runCmd ("command alias bp breakpoint")
Caroline Tice8607f032011-01-29 00:19:53 +000050
51 self.runCmd ("bp set -n main")
52
53 self.runCmd ("bp l")
54
Greg Claytonc7d88852014-02-12 23:46:08 +000055 self.runCmd("log disable lldb")
56
Caroline Tice8607f032011-01-29 00:19:53 +000057 self.assertTrue (os.path.isfile (log_file))
58
Caroline Tice8607f032011-01-29 00:19:53 +000059 f = open (log_file)
60 log_lines = f.readlines()
61 f.close ()
Caroline Tice8607f032011-01-29 00:19:53 +000062 os.remove (log_file)
63
Adrian McCarthy88011452016-01-15 20:45:06 +000064 self.assertGreater(len(log_lines), 0, "Something was written to the log file.")
Caroline Tice8607f032011-01-29 00:19:53 +000065
Pavel Labath8ac06992015-03-20 09:43:20 +000066 # Check that lldb truncates its log files
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000067 @no_debug_info_test
Pavel Labath8ac06992015-03-20 09:43:20 +000068 def test_log_truncate (self):
69 if (os.path.exists (self.truncate_log_file)):
70 os.remove (self.truncate_log_file)
71
72 # put something in our log file
73 with open(self.truncate_log_file, "w") as f:
74 for i in range(1, 1000):
75 f.write("bacon\n")
76
77 self.runCmd ("log enable -t -f '%s' lldb commands" % (self.truncate_log_file))
78 self.runCmd ("help log")
79 self.runCmd ("log disable lldb")
80
81 self.assertTrue (os.path.isfile (self.truncate_log_file))
82 with open(self.truncate_log_file, "r") as f:
83 contents = f.read ()
84
85 # check that it got removed
Adrian McCarthy88011452016-01-15 20:45:06 +000086 self.assertEquals(contents.find("bacon"), -1)
Pavel Labath8ac06992015-03-20 09:43:20 +000087
88 # Check that lldb can append to a log file
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000089 @no_debug_info_test
Pavel Labath8ac06992015-03-20 09:43:20 +000090 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
Adrian McCarthy88011452016-01-15 20:45:06 +0000107 self.assertEquals(contents.find("bacon"), 0)