Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 1 | """ |
Jim Ingham | c49d0d4 | 2014-03-19 23:55:54 +0000 | [diff] [blame] | 2 | Test lldb logging. This test just makes sure logging doesn't crash, and produces some output. |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
Pavel Labath | 8ac0699 | 2015-03-20 09:43:20 +0000 | [diff] [blame] | 5 | import os, time, string |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 6 | import unittest2 |
| 7 | import lldb |
| 8 | from lldbtest import * |
| 9 | |
| 10 | class LogTestCase(TestBase): |
| 11 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 12 | mydir = TestBase.compute_mydir(__file__) |
Pavel Labath | 8ac0699 | 2015-03-20 09:43:20 +0000 | [diff] [blame] | 13 | 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 Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 22 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 23 | @skipUnlessDarwin |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 24 | @dsym_test |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 25 | def test_with_dsym (self): |
| 26 | self.buildDsym () |
Johnny Chen | ec1ccca | 2011-01-29 00:52:54 +0000 | [diff] [blame] | 27 | self.command_log_tests ("dsym") |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 28 | |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 29 | @dwarf_test |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 30 | def test_with_dwarf (self): |
| 31 | self.buildDwarf () |
Johnny Chen | ec1ccca | 2011-01-29 00:52:54 +0000 | [diff] [blame] | 32 | self.command_log_tests ("dwarf") |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 33 | |
Johnny Chen | ec1ccca | 2011-01-29 00:52:54 +0000 | [diff] [blame] | 34 | def command_log_tests (self, type): |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 35 | exe = os.path.join (os.getcwd(), "a.out") |
| 36 | self.expect("file " + exe, |
| 37 | patterns = [ "Current executable set to .*a.out" ]) |
| 38 | |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 39 | log_file = os.path.join (os.getcwd(), "lldb-commands-log-%s-%s-%s.txt" % (type, |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 40 | os.path.basename(self.getCompiler()), |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 41 | self.getArchitecture())) |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 42 | |
| 43 | if (os.path.exists (log_file)): |
| 44 | os.remove (log_file) |
| 45 | |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 46 | # 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 Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 50 | |
Johnny Chen | 72c4082 | 2011-04-21 20:55:57 +0000 | [diff] [blame] | 51 | self.runCmd ("command alias bp breakpoint") |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 52 | |
| 53 | self.runCmd ("bp set -n main") |
| 54 | |
| 55 | self.runCmd ("bp l") |
| 56 | |
Greg Clayton | c7d8885 | 2014-02-12 23:46:08 +0000 | [diff] [blame] | 57 | self.runCmd("log disable lldb") |
| 58 | |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 59 | self.assertTrue (os.path.isfile (log_file)) |
| 60 | |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 61 | f = open (log_file) |
| 62 | log_lines = f.readlines() |
| 63 | f.close () |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 64 | os.remove (log_file) |
| 65 | |
Jim Ingham | c49d0d4 | 2014-03-19 23:55:54 +0000 | [diff] [blame] | 66 | self.assertTrue(log_lines > 0, "Something was written to the log file.") |
Caroline Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 67 | |
Pavel Labath | 8ac0699 | 2015-03-20 09:43:20 +0000 | [diff] [blame] | 68 | # 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 Tice | 8607f03 | 2011-01-29 00:19:53 +0000 | [diff] [blame] | 110 | if __name__ == '__main__': |
| 111 | import atexit |
| 112 | lldb.SBDebugger.Initialize() |
| 113 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 114 | unittest2.main() |
| 115 | |