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