687340e5fd3dfa18a5ea7ab531a951c1d4b2c199
[openbsd] /
1 """
2 Test DarwinLog log message formatting options provided by the
3 StructuredDataDarwinLog plugin.
4
5 These tests are currently only supported when running against Darwin
6 targets.
7 """
8
9
10 import lldb
11 import re
12
13 from lldbsuite.test import decorators
14 from lldbsuite.test import lldbtest
15 from lldbsuite.test import darwin_log
16
17
18 class TestDarwinLogMessageFormat(darwin_log.DarwinLogTestBase):
19
20     mydir = lldbtest.TestBase.compute_mydir(__file__)
21
22     def setUp(self):
23         # Call super's setUp().
24         super(TestDarwinLogMessageFormat, self).setUp()
25
26         # Source filename.
27         self.source = 'main.c'
28
29         # Output filename.
30         self.exe_name = self.getBuildArtifact("a.out")
31         self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
32
33         # Locate breakpoint.
34         self.line = lldbtest.line_number(self.source, '// break here')
35
36     def tearDown(self):
37         # Shut down the process if it's still running.
38         if self.child:
39             self.runCmd('process kill')
40             self.expect_prompt()
41             self.runCmd('quit')
42
43         # Let parent clean up
44         super(TestDarwinLogMessageFormat, self).tearDown()
45
46     # ==========================================================================
47     # Test settings around log message formatting
48     # ==========================================================================
49
50     REGEXES = [
51         re.compile(r"\[([^]]+)\] This is the log message."),  # Match log
52                                                               # with header.
53         re.compile(r"This is the log message."),  # Match no-header content.
54         re.compile(r"exited with status")         # Fallback if no log emitted.
55     ]
56
57     @decorators.skipUnlessDarwin
58     def test_display_without_header_works(self):
59         """Test that turning off log message headers works as advertised."""
60         self.do_test([], expect_regexes=self.REGEXES)
61
62         # We should not match the first pattern as we shouldn't have header
63         # content.
64         self.assertIsNotNone(self.child.match)
65         self.assertFalse((len(self.child.match.groups()) > 0) and
66                          (self.child.match.group(1) != ""),
67                          "we should not have seen a header")
68
69     @decorators.skipUnlessDarwin
70     def test_display_with_header_works(self):
71         """Test that displaying any header works."""
72         self.do_test(
73             ["--timestamp-relative", "--subsystem", "--category",
74              "--activity-chain"],
75             expect_regexes=self.REGEXES,
76             settings_commands=[
77                 "display-header true"
78             ])
79
80         # We should match the first pattern as we should have header
81         # content.
82         self.assertIsNotNone(self.child.match)
83         self.assertTrue((len(self.child.match.groups()) > 0) and
84                         (self.child.match.group(1) != ""),
85                         "we should have printed a header")
86
87     def assert_header_contains_timestamp(self, header):
88         fields = header.split(',')
89         self.assertGreater(len(fields), 0,
90                            "there should have been header content present")
91         self.assertRegexpMatches(fields[0],
92                                  r"^\d+:\d{2}:\d{2}.\d{9}$",
93                                  "time field should match expected format")
94
95     @decorators.skipUnlessDarwin
96     def test_header_timefield_only_works(self):
97         """Test that displaying a header with only the timestamp works."""
98         self.do_test(["--timestamp-relative"], expect_regexes=self.REGEXES)
99
100         # We should match the first pattern as we should have header
101         # content.
102         self.assertIsNotNone(self.child.match)
103         self.assertTrue((len(self.child.match.groups()) > 0) and
104                         (self.child.match.group(1) != ""),
105                         "we should have printed a header")
106         header = self.child.match.group(1)
107         self.assertEqual(len(header.split(',')), 1,
108                          "there should only be one header field")
109         self.assert_header_contains_timestamp(header)
110
111     @decorators.skipUnlessDarwin
112     def test_header_subsystem_only_works(self):
113         """Test that displaying a header with only the subsystem works."""
114         self.do_test(["--subsystem"], expect_regexes=self.REGEXES)
115
116         # We should match the first pattern as we should have header
117         # content.
118         self.assertIsNotNone(self.child.match)
119         self.assertTrue((len(self.child.match.groups()) > 0) and
120                         (self.child.match.group(1) != ""),
121                         "we should have printed a header")
122         header = self.child.match.group(1)
123         self.assertEqual(len(header.split(',')), 1,
124                          "there should only be one header field")
125         self.assertEquals(header,
126                           "subsystem=org.llvm.lldb.test.sub1")
127
128     @decorators.skipUnlessDarwin
129     def test_header_category_only_works(self):
130         """Test that displaying a header with only the category works."""
131         self.do_test(["--category"], expect_regexes=self.REGEXES)
132
133         # We should match the first pattern as we should have header
134         # content.
135         self.assertIsNotNone(self.child.match)
136         self.assertTrue((len(self.child.match.groups()) > 0) and
137                         (self.child.match.group(1) != ""),
138                         "we should have printed a header")
139         header = self.child.match.group(1)
140         self.assertEqual(len(header.split(',')), 1,
141                          "there should only be one header field")
142         self.assertEquals(header,
143                           "category=cat1")
144
145     @decorators.skipUnlessDarwin
146     def test_header_activity_chain_only_works(self):
147         """Test that displaying a header with only the activity chain works."""
148         self.do_test(["--activity-chain"], expect_regexes=self.REGEXES)
149
150         # We should match the first pattern as we should have header
151         # content.
152         self.assertIsNotNone(self.child.match)
153         self.assertTrue((len(self.child.match.groups()) > 0) and
154                         (self.child.match.group(1) != ""),
155                         "we should have printed a header")
156         header = self.child.match.group(1)
157         self.assertEqual(len(header.split(',')), 1,
158                          "there should only be one header field")
159         self.assertEquals(header,
160                           "activity-chain=parent-activity:child-activity")
161
162     # @decorators.skipUnlessDarwin
163     # def test_header_activity_no_chain_only_works(self):
164     #     """Test that displaying a header with only the activity works."""
165     #     self.do_test(
166     #         [],
167     #         expect_regexes=self.REGEXES,
168     #         settings_commands=[
169     #             "display-header true",
170     #             "format-include-timestamp false",
171     #             "format-include-activity true",
172     #             "format-include-category false",
173     #             "format-include-subsystem false",
174     #             "display-activity-chain false"
175     #         ])
176
177     #     # We should match the first pattern as we should have header
178     #     # content.
179     #     self.assertIsNotNone(self.child.match)
180     #     self.assertTrue((len(self.child.match.groups()) > 0) and
181     #                     (self.child.match.group(1) != ""),
182     #                     "we should have printed a header")
183     #     header = self.child.match.group(1)
184     #     self.assertEqual(len(header.split(',')), 1,
185     #                      "there should only be one header field")
186     #     self.assertEquals(header,
187     #                       "activity=child-activity")