Commit | Line | Data |
---|---|---|
da1e97c9 MD |
1 | #!/usr/bin/env python3 |
2 | # | |
3 | # Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
4 | # Copyright (C) 2023 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | # | |
6 | # SPDX-License-Identifier: GPL-2.0-only | |
7 | ||
8 | from cgi import test | |
9 | import pathlib | |
10 | import sys | |
11 | import os | |
12 | from typing import Any, Callable, Type | |
13 | ||
14 | """ | |
15 | Test instrumentation coverage of C/C++ constructors and destructors by LTTng-UST | |
16 | tracepoints. | |
17 | ||
18 | This test successively sets up a session, traces a test application, and then | |
19 | reads the resulting trace to determine if all the expected events are present. | |
20 | """ | |
21 | ||
22 | # Import in-tree test utils | |
23 | test_utils_import_path = pathlib.Path(__file__).absolute().parents[3] / "utils" | |
24 | sys.path.append(str(test_utils_import_path)) | |
25 | ||
26 | import lttngtest | |
27 | import bt2 | |
28 | ||
29 | num_tests = 3 | |
30 | ||
31 | expected_events = [ | |
32 | {"name": "tp_so:constructor_c_provider_shared_library", "msg": None, "count": 0}, | |
33 | {"name": "tp_a:constructor_c_provider_static_archive", "msg": None, "count": 0}, | |
34 | { | |
35 | "name": "tp_so:constructor_cplusplus_provider_shared_library", | |
36 | "msg": "global - shared library define and provider", | |
37 | "count": 0, | |
38 | }, | |
39 | { | |
40 | "name": "tp_a:constructor_cplusplus_provider_static_archive", | |
41 | "msg": "global - static archive define and provider", | |
42 | "count": 0, | |
43 | }, | |
44 | {"name": "tp:constructor_c_across_units_before_define", "msg": None, "count": 0}, | |
45 | { | |
46 | "name": "tp:constructor_cplusplus", | |
47 | "msg": "global - across units before define", | |
48 | "count": 0, | |
49 | }, | |
50 | {"name": "tp:constructor_c_same_unit_before_define", "msg": None, "count": 0}, | |
51 | {"name": "tp:constructor_c_same_unit_after_define", "msg": None, "count": 0}, | |
52 | { | |
53 | "name": "tp:constructor_cplusplus", | |
54 | "msg": "global - same unit before define", | |
55 | "count": 0, | |
56 | }, | |
57 | { | |
58 | "name": "tp:constructor_cplusplus", | |
59 | "msg": "global - same unit after define", | |
60 | "count": 0, | |
61 | }, | |
62 | {"name": "tp:constructor_c_across_units_after_define", "msg": None, "count": 0}, | |
63 | { | |
64 | "name": "tp:constructor_cplusplus", | |
65 | "msg": "global - across units after define", | |
66 | "count": 0, | |
67 | }, | |
68 | {"name": "tp:constructor_c_same_unit_before_provider", "msg": None, "count": 0}, | |
69 | {"name": "tp:constructor_c_same_unit_after_provider", "msg": None, "count": 0}, | |
70 | { | |
71 | "name": "tp:constructor_cplusplus", | |
72 | "msg": "global - same unit before provider", | |
73 | "count": 0, | |
74 | }, | |
75 | { | |
76 | "name": "tp:constructor_cplusplus", | |
77 | "msg": "global - same unit after provider", | |
78 | "count": 0, | |
79 | }, | |
80 | {"name": "tp:constructor_c_across_units_after_provider", "msg": None, "count": 0}, | |
81 | { | |
82 | "name": "tp:constructor_cplusplus", | |
83 | "msg": "global - across units after provider", | |
84 | "count": 0, | |
85 | }, | |
86 | {"name": "tp:constructor_cplusplus", "msg": "main() local", "count": 0}, | |
87 | { | |
88 | "name": "tp_so:constructor_cplusplus_provider_shared_library", | |
89 | "msg": "main() local - shared library define and provider", | |
90 | "count": 0, | |
91 | }, | |
92 | { | |
93 | "name": "tp_a:constructor_cplusplus_provider_static_archive", | |
94 | "msg": "main() local - static archive define and provider", | |
95 | "count": 0, | |
96 | }, | |
97 | {"name": "tp:main", "msg": None, "count": 0}, | |
98 | { | |
99 | "name": "tp_a:destructor_cplusplus_provider_static_archive", | |
100 | "msg": "main() local - static archive define and provider", | |
101 | "count": 0, | |
102 | }, | |
103 | { | |
104 | "name": "tp_so:destructor_cplusplus_provider_shared_library", | |
105 | "msg": "main() local - shared library define and provider", | |
106 | "count": 0, | |
107 | }, | |
108 | {"name": "tp:destructor_cplusplus", "msg": "main() local", "count": 0}, | |
109 | { | |
110 | "name": "tp:destructor_cplusplus", | |
111 | "msg": "global - across units after provider", | |
112 | "count": 0, | |
113 | }, | |
114 | { | |
115 | "name": "tp:destructor_cplusplus", | |
116 | "msg": "global - same unit after provider", | |
117 | "count": 0, | |
118 | }, | |
119 | { | |
120 | "name": "tp:destructor_cplusplus", | |
121 | "msg": "global - same unit before provider", | |
122 | "count": 0, | |
123 | }, | |
124 | { | |
125 | "name": "tp:destructor_cplusplus", | |
126 | "msg": "global - across units after define", | |
127 | "count": 0, | |
128 | }, | |
129 | { | |
130 | "name": "tp:destructor_cplusplus", | |
131 | "msg": "global - same unit after define", | |
132 | "count": 0, | |
133 | }, | |
134 | { | |
135 | "name": "tp:destructor_cplusplus", | |
136 | "msg": "global - same unit before define", | |
137 | "count": 0, | |
138 | }, | |
139 | { | |
140 | "name": "tp:destructor_cplusplus", | |
141 | "msg": "global - across units before define", | |
142 | "count": 0, | |
143 | }, | |
144 | { | |
145 | "name": "tp_a:destructor_cplusplus_provider_static_archive", | |
146 | "msg": "global - static archive define and provider", | |
147 | "count": 0, | |
148 | }, | |
149 | { | |
150 | "name": "tp_so:destructor_cplusplus_provider_shared_library", | |
151 | "msg": "global - shared library define and provider", | |
152 | "count": 0, | |
153 | }, | |
154 | {"name": "tp:destructor_c_across_units_after_provider", "msg": None, "count": 0}, | |
155 | {"name": "tp:destructor_c_same_unit_after_provider", "msg": None, "count": 0}, | |
156 | {"name": "tp:destructor_c_same_unit_before_provider", "msg": None, "count": 0}, | |
157 | {"name": "tp:destructor_c_across_units_after_define", "msg": None, "count": 0}, | |
158 | {"name": "tp:destructor_c_same_unit_after_define", "msg": None, "count": 0}, | |
159 | {"name": "tp:destructor_c_same_unit_before_define", "msg": None, "count": 0}, | |
160 | {"name": "tp:destructor_c_across_units_before_define", "msg": None, "count": 0}, | |
161 | {"name": "tp_a:destructor_c_provider_static_archive", "msg": None, "count": 0}, | |
162 | {"name": "tp_so:destructor_c_provider_shared_library", "msg": None, "count": 0}, | |
163 | ] | |
164 | ||
165 | ||
166 | def capture_trace( | |
167 | tap: lttngtest.TapGenerator, test_env: lttngtest._Environment | |
168 | ) -> lttngtest.LocalSessionOutputLocation: | |
169 | tap.diagnostic( | |
170 | "Capture trace from application with instrumented C/C++ constructors/destructors" | |
171 | ) | |
172 | ||
173 | session_output_location = lttngtest.LocalSessionOutputLocation( | |
174 | test_env.create_temporary_directory("trace") | |
175 | ) | |
176 | ||
177 | client: lttngtest.Controller = lttngtest.LTTngClient(test_env, log=tap.diagnostic) | |
178 | ||
179 | with tap.case("Create a session") as test_case: | |
180 | session = client.create_session(output=session_output_location) | |
181 | tap.diagnostic("Created session `{session_name}`".format(session_name=session.name)) | |
182 | ||
183 | with tap.case( | |
184 | "Add a channel to session `{session_name}`".format(session_name=session.name) | |
185 | ) as test_case: | |
186 | channel = session.add_channel(lttngtest.TracingDomain.User) | |
187 | tap.diagnostic("Created channel `{channel_name}`".format(channel_name=channel.name)) | |
188 | ||
189 | # Enable all user space events, the default for a user tracepoint event rule. | |
190 | channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp*")) | |
191 | ||
192 | session.start() | |
193 | test_app = test_env.launch_trace_test_constructor_application() | |
194 | test_app.wait_for_exit() | |
195 | session.stop() | |
196 | session.destroy() | |
197 | return session_output_location | |
198 | ||
199 | ||
200 | def validate_trace(trace_location: pathlib.Path, tap: lttngtest.TapGenerator) -> bool: | |
201 | success = True | |
202 | unknown_event_count = 0 | |
203 | ||
204 | for msg in bt2.TraceCollectionMessageIterator(str(trace_location)): | |
205 | if type(msg) is not bt2._EventMessageConst: | |
206 | continue | |
207 | ||
208 | found = False | |
209 | for event in expected_events: | |
210 | if event["name"] == msg.event.name and event["msg"] is None: | |
211 | found = True | |
212 | event["count"] = event["count"] + 1 | |
213 | break | |
214 | elif ( | |
215 | event["name"] == msg.event.name | |
216 | and event["msg"] is not None | |
217 | and event["msg"] == msg.event["msg"] | |
218 | ): | |
219 | found = True | |
220 | event["count"] = event["count"] + 1 | |
221 | break | |
222 | if found == False: | |
223 | unknown_event_count = unknown_event_count + 1 | |
224 | printmsg = None | |
225 | if "msg" in msg.event: | |
226 | printmsg = msg.event["msg"] | |
227 | tap.diagnostic( | |
228 | 'Unexpected event name="{}" msg="{}" encountered'.format( | |
229 | msg.event.name, str(printmsg) | |
230 | ) | |
231 | ) | |
232 | ||
233 | for event in expected_events: | |
234 | if event["count"] != 1: | |
235 | success = False | |
236 | tap.diagnostic("Expected event {} not found".format(event["name"])) | |
237 | if unknown_event_count != 0: | |
238 | success = False | |
239 | return success | |
240 | ||
241 | ||
242 | tap = lttngtest.TapGenerator(num_tests) | |
243 | tap.diagnostic("Test user space constructor/destructor instrumentation coverage") | |
244 | ||
245 | with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env: | |
246 | outputlocation = capture_trace(tap, test_env) | |
247 | tap.test( | |
248 | validate_trace(outputlocation.path, tap), | |
249 | "Validate that trace constains expected events", | |
250 | ) | |
251 | ||
252 | sys.exit(0 if tap.is_successful else 1) |