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 | ||
da1e97c9 MD |
29 | expected_events = [ |
30 | {"name": "tp_so:constructor_c_provider_shared_library", "msg": None, "count": 0}, | |
31 | {"name": "tp_a:constructor_c_provider_static_archive", "msg": None, "count": 0}, | |
32 | { | |
33 | "name": "tp_so:constructor_cplusplus_provider_shared_library", | |
34 | "msg": "global - shared library define and provider", | |
35 | "count": 0, | |
36 | }, | |
37 | { | |
38 | "name": "tp_a:constructor_cplusplus_provider_static_archive", | |
39 | "msg": "global - static archive define and provider", | |
40 | "count": 0, | |
41 | }, | |
42 | {"name": "tp:constructor_c_across_units_before_define", "msg": None, "count": 0}, | |
43 | { | |
44 | "name": "tp:constructor_cplusplus", | |
45 | "msg": "global - across units before define", | |
46 | "count": 0, | |
47 | }, | |
48 | {"name": "tp:constructor_c_same_unit_before_define", "msg": None, "count": 0}, | |
49 | {"name": "tp:constructor_c_same_unit_after_define", "msg": None, "count": 0}, | |
50 | { | |
51 | "name": "tp:constructor_cplusplus", | |
52 | "msg": "global - same unit before define", | |
53 | "count": 0, | |
54 | }, | |
55 | { | |
56 | "name": "tp:constructor_cplusplus", | |
57 | "msg": "global - same unit after define", | |
58 | "count": 0, | |
59 | }, | |
60 | {"name": "tp:constructor_c_across_units_after_define", "msg": None, "count": 0}, | |
61 | { | |
62 | "name": "tp:constructor_cplusplus", | |
63 | "msg": "global - across units after define", | |
64 | "count": 0, | |
65 | }, | |
66 | {"name": "tp:constructor_c_same_unit_before_provider", "msg": None, "count": 0}, | |
67 | {"name": "tp:constructor_c_same_unit_after_provider", "msg": None, "count": 0}, | |
68 | { | |
69 | "name": "tp:constructor_cplusplus", | |
70 | "msg": "global - same unit before provider", | |
71 | "count": 0, | |
72 | }, | |
73 | { | |
74 | "name": "tp:constructor_cplusplus", | |
75 | "msg": "global - same unit after provider", | |
76 | "count": 0, | |
77 | }, | |
78 | {"name": "tp:constructor_c_across_units_after_provider", "msg": None, "count": 0}, | |
79 | { | |
80 | "name": "tp:constructor_cplusplus", | |
81 | "msg": "global - across units after provider", | |
82 | "count": 0, | |
83 | }, | |
84 | {"name": "tp:constructor_cplusplus", "msg": "main() local", "count": 0}, | |
85 | { | |
86 | "name": "tp_so:constructor_cplusplus_provider_shared_library", | |
87 | "msg": "main() local - shared library define and provider", | |
88 | "count": 0, | |
89 | }, | |
90 | { | |
91 | "name": "tp_a:constructor_cplusplus_provider_static_archive", | |
92 | "msg": "main() local - static archive define and provider", | |
93 | "count": 0, | |
94 | }, | |
95 | {"name": "tp:main", "msg": None, "count": 0}, | |
96 | { | |
97 | "name": "tp_a:destructor_cplusplus_provider_static_archive", | |
98 | "msg": "main() local - static archive define and provider", | |
99 | "count": 0, | |
100 | }, | |
101 | { | |
102 | "name": "tp_so:destructor_cplusplus_provider_shared_library", | |
103 | "msg": "main() local - shared library define and provider", | |
104 | "count": 0, | |
105 | }, | |
106 | {"name": "tp:destructor_cplusplus", "msg": "main() local", "count": 0}, | |
107 | { | |
108 | "name": "tp:destructor_cplusplus", | |
109 | "msg": "global - across units after provider", | |
110 | "count": 0, | |
111 | }, | |
112 | { | |
113 | "name": "tp:destructor_cplusplus", | |
114 | "msg": "global - same unit after provider", | |
115 | "count": 0, | |
116 | }, | |
117 | { | |
118 | "name": "tp:destructor_cplusplus", | |
119 | "msg": "global - same unit before provider", | |
120 | "count": 0, | |
121 | }, | |
122 | { | |
123 | "name": "tp:destructor_cplusplus", | |
124 | "msg": "global - across units after define", | |
125 | "count": 0, | |
126 | }, | |
127 | { | |
128 | "name": "tp:destructor_cplusplus", | |
129 | "msg": "global - same unit after define", | |
130 | "count": 0, | |
131 | }, | |
132 | { | |
133 | "name": "tp:destructor_cplusplus", | |
134 | "msg": "global - same unit before define", | |
135 | "count": 0, | |
136 | }, | |
137 | { | |
138 | "name": "tp:destructor_cplusplus", | |
139 | "msg": "global - across units before define", | |
140 | "count": 0, | |
141 | }, | |
142 | { | |
143 | "name": "tp_a:destructor_cplusplus_provider_static_archive", | |
144 | "msg": "global - static archive define and provider", | |
145 | "count": 0, | |
146 | }, | |
147 | { | |
148 | "name": "tp_so:destructor_cplusplus_provider_shared_library", | |
149 | "msg": "global - shared library define and provider", | |
150 | "count": 0, | |
151 | }, | |
152 | {"name": "tp:destructor_c_across_units_after_provider", "msg": None, "count": 0}, | |
153 | {"name": "tp:destructor_c_same_unit_after_provider", "msg": None, "count": 0}, | |
154 | {"name": "tp:destructor_c_same_unit_before_provider", "msg": None, "count": 0}, | |
155 | {"name": "tp:destructor_c_across_units_after_define", "msg": None, "count": 0}, | |
156 | {"name": "tp:destructor_c_same_unit_after_define", "msg": None, "count": 0}, | |
157 | {"name": "tp:destructor_c_same_unit_before_define", "msg": None, "count": 0}, | |
158 | {"name": "tp:destructor_c_across_units_before_define", "msg": None, "count": 0}, | |
159 | {"name": "tp_a:destructor_c_provider_static_archive", "msg": None, "count": 0}, | |
160 | {"name": "tp_so:destructor_c_provider_shared_library", "msg": None, "count": 0}, | |
161 | ] | |
162 | ||
d096be91 MJ |
163 | num_tests = 7 + len(expected_events) |
164 | ||
da1e97c9 | 165 | |
d2455527 JG |
166 | def capture_trace(tap, test_env): |
167 | # type: (lttngtest.TapGenerator, lttngtest._Environment) -> lttngtest.LocalSessionOutputLocation | |
da1e97c9 MD |
168 | tap.diagnostic( |
169 | "Capture trace from application with instrumented C/C++ constructors/destructors" | |
170 | ) | |
171 | ||
172 | session_output_location = lttngtest.LocalSessionOutputLocation( | |
173 | test_env.create_temporary_directory("trace") | |
174 | ) | |
175 | ||
aae4cdd1 | 176 | client = lttngtest.LTTngClient(test_env, log=tap.diagnostic) |
da1e97c9 MD |
177 | |
178 | with tap.case("Create a session") as test_case: | |
179 | session = client.create_session(output=session_output_location) | |
180 | tap.diagnostic("Created session `{session_name}`".format(session_name=session.name)) | |
181 | ||
182 | with tap.case( | |
183 | "Add a channel to session `{session_name}`".format(session_name=session.name) | |
184 | ) as test_case: | |
185 | channel = session.add_channel(lttngtest.TracingDomain.User) | |
186 | tap.diagnostic("Created channel `{channel_name}`".format(channel_name=channel.name)) | |
187 | ||
188 | # Enable all user space events, the default for a user tracepoint event rule. | |
189 | channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp*")) | |
190 | ||
d096be91 MJ |
191 | with tap.case( |
192 | "Start session `{session_name}`".format(session_name=session.name) | |
193 | ) as test_case: | |
194 | session.start() | |
195 | ||
da1e97c9 | 196 | test_app = test_env.launch_trace_test_constructor_application() |
d096be91 MJ |
197 | with tap.case("Run test app".format(session_name=session.name)) as test_case: |
198 | test_app.wait_for_exit() | |
199 | ||
200 | with tap.case( | |
201 | "Stop session `{session_name}`".format(session_name=session.name) | |
202 | ) as test_case: | |
203 | session.stop() | |
204 | ||
205 | with tap.case( | |
206 | "Destroy session `{session_name}`".format(session_name=session.name) | |
207 | ) as test_case: | |
208 | session.destroy() | |
209 | ||
da1e97c9 MD |
210 | return session_output_location |
211 | ||
212 | ||
873d3601 | 213 | def validate_trace(trace_location, tap): |
d096be91 | 214 | # type: (pathlib.Path, lttngtest.TapGenerator) |
da1e97c9 MD |
215 | unknown_event_count = 0 |
216 | ||
217 | for msg in bt2.TraceCollectionMessageIterator(str(trace_location)): | |
218 | if type(msg) is not bt2._EventMessageConst: | |
219 | continue | |
220 | ||
221 | found = False | |
222 | for event in expected_events: | |
223 | if event["name"] == msg.event.name and event["msg"] is None: | |
224 | found = True | |
225 | event["count"] = event["count"] + 1 | |
226 | break | |
227 | elif ( | |
228 | event["name"] == msg.event.name | |
229 | and event["msg"] is not None | |
230 | and event["msg"] == msg.event["msg"] | |
231 | ): | |
232 | found = True | |
233 | event["count"] = event["count"] + 1 | |
234 | break | |
d096be91 | 235 | |
da1e97c9 MD |
236 | if found == False: |
237 | unknown_event_count = unknown_event_count + 1 | |
238 | printmsg = None | |
239 | if "msg" in msg.event: | |
240 | printmsg = msg.event["msg"] | |
241 | tap.diagnostic( | |
242 | 'Unexpected event name="{}" msg="{}" encountered'.format( | |
243 | msg.event.name, str(printmsg) | |
244 | ) | |
245 | ) | |
246 | ||
247 | for event in expected_events: | |
d096be91 MJ |
248 | tap.test( |
249 | event["count"] == 1, | |
250 | 'Found expected event name="{}" msg="{}"'.format( | |
251 | event["name"], str(event["msg"]) | |
252 | ), | |
253 | ) | |
254 | ||
255 | tap.test(unknown_event_count == 0, "Found no unexpected events") | |
da1e97c9 MD |
256 | |
257 | ||
258 | tap = lttngtest.TapGenerator(num_tests) | |
259 | tap.diagnostic("Test user space constructor/destructor instrumentation coverage") | |
260 | ||
261 | with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env: | |
262 | outputlocation = capture_trace(tap, test_env) | |
d096be91 | 263 | validate_trace(outputlocation.path, tap) |
da1e97c9 MD |
264 | |
265 | sys.exit(0 if tap.is_successful else 1) |