| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: 2024 Kienan Stewart <kstewart@efficios.com> |
| 4 | # SPDX-License-Identifer: GPL-2.0-only |
| 5 | # |
| 6 | |
| 7 | import copy |
| 8 | import pathlib |
| 9 | import sys |
| 10 | import os |
| 11 | import subprocess |
| 12 | from typing import Any, Callable, Type |
| 13 | |
| 14 | # Import in-tree test utils |
| 15 | test_utils_import_path = pathlib.Path(__file__).absolute().parents[3] / "utils" |
| 16 | sys.path.append(str(test_utils_import_path)) |
| 17 | |
| 18 | import lttngtest |
| 19 | import bt2 |
| 20 | |
| 21 | # Determine if LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP is set. This will |
| 22 | # affect if certain events may or may not be expected when compiling with |
| 23 | # C++. |
| 24 | # @see https://github.com/lttng/lttng-ust/blob/47fa3e4ed7ab43e034dc61fc1480f919f4ee51d0/include/lttng/ust-compiler.h#L51 |
| 25 | # |
| 26 | compound_literal_on_heap = False |
| 27 | process = subprocess.Popen( |
| 28 | [ |
| 29 | os.path.join( |
| 30 | str(test_utils_import_path), |
| 31 | "testapp", |
| 32 | "gen-ust-events-constructor", |
| 33 | "uses_heap", |
| 34 | ) |
| 35 | ] |
| 36 | ) |
| 37 | process.wait() |
| 38 | if process.returncode == 0: |
| 39 | compound_literal_on_heap = True |
| 40 | |
| 41 | expected_events_common_cpp = [ |
| 42 | { |
| 43 | "name": "tp:constructor_cplusplus", |
| 44 | "msg": "global - across units before define", |
| 45 | "count": 0, |
| 46 | "may_fail": compound_literal_on_heap, |
| 47 | }, |
| 48 | { |
| 49 | "name": "tp:constructor_cplusplus", |
| 50 | "msg": "global - same unit before define", |
| 51 | "count": 0, |
| 52 | "may_fail": compound_literal_on_heap, |
| 53 | }, |
| 54 | { |
| 55 | "name": "tp:constructor_cplusplus", |
| 56 | "msg": "global - same unit after define", |
| 57 | "count": 0, |
| 58 | "may_fail": compound_literal_on_heap, |
| 59 | }, |
| 60 | { |
| 61 | "name": "tp:constructor_cplusplus", |
| 62 | "msg": "global - across units after define", |
| 63 | "count": 0, |
| 64 | "may_fail": compound_literal_on_heap, |
| 65 | }, |
| 66 | { |
| 67 | "name": "tp:constructor_cplusplus", |
| 68 | "msg": "global - same unit before provider", |
| 69 | "count": 0, |
| 70 | "may_fail": compound_literal_on_heap, |
| 71 | }, |
| 72 | { |
| 73 | "name": "tp:constructor_cplusplus", |
| 74 | "msg": "global - same unit after provider", |
| 75 | "count": 0, |
| 76 | }, |
| 77 | { |
| 78 | "name": "tp:constructor_cplusplus", |
| 79 | "msg": "global - across units after provider", |
| 80 | "count": 0, |
| 81 | }, |
| 82 | {"name": "tp:constructor_cplusplus", "msg": "main() local", "count": 0}, |
| 83 | {"name": "tp:destructor_cplusplus", "msg": "main() local", "count": 0}, |
| 84 | { |
| 85 | "name": "tp:destructor_cplusplus", |
| 86 | "msg": "global - across units after provider", |
| 87 | "count": 0, |
| 88 | }, |
| 89 | { |
| 90 | "name": "tp:destructor_cplusplus", |
| 91 | "msg": "global - same unit after provider", |
| 92 | "count": 0, |
| 93 | }, |
| 94 | { |
| 95 | "name": "tp:destructor_cplusplus", |
| 96 | "msg": "global - same unit before provider", |
| 97 | "count": 0, |
| 98 | "may_fail": compound_literal_on_heap, |
| 99 | }, |
| 100 | { |
| 101 | "name": "tp:destructor_cplusplus", |
| 102 | "msg": "global - across units after define", |
| 103 | "count": 0, |
| 104 | "may_fail": compound_literal_on_heap, |
| 105 | }, |
| 106 | { |
| 107 | "name": "tp:destructor_cplusplus", |
| 108 | "msg": "global - same unit after define", |
| 109 | "count": 0, |
| 110 | "may_fail": compound_literal_on_heap, |
| 111 | }, |
| 112 | { |
| 113 | "name": "tp:destructor_cplusplus", |
| 114 | "msg": "global - same unit before define", |
| 115 | "count": 0, |
| 116 | "may_fail": compound_literal_on_heap, |
| 117 | }, |
| 118 | { |
| 119 | "name": "tp:destructor_cplusplus", |
| 120 | "msg": "global - across units before define", |
| 121 | "count": 0, |
| 122 | "may_fail": compound_literal_on_heap, |
| 123 | }, |
| 124 | ] |
| 125 | |
| 126 | expected_events_common = [ |
| 127 | { |
| 128 | "name": "tp:constructor_c_across_units_before_define", |
| 129 | "msg": None, |
| 130 | "count": 0, |
| 131 | "may_fail": compound_literal_on_heap, |
| 132 | }, |
| 133 | { |
| 134 | "name": "tp:constructor_c_same_unit_before_define", |
| 135 | "msg": None, |
| 136 | "count": 0, |
| 137 | "may_fail": compound_literal_on_heap, |
| 138 | }, |
| 139 | { |
| 140 | "name": "tp:constructor_c_same_unit_after_define", |
| 141 | "msg": None, |
| 142 | "count": 0, |
| 143 | "may_fail": compound_literal_on_heap, |
| 144 | }, |
| 145 | { |
| 146 | "name": "tp:constructor_c_across_units_after_define", |
| 147 | "msg": None, |
| 148 | "count": 0, |
| 149 | "may_fail": compound_literal_on_heap, |
| 150 | }, |
| 151 | { |
| 152 | "name": "tp:constructor_c_same_unit_before_provider", |
| 153 | "msg": None, |
| 154 | "count": 0, |
| 155 | "may_fail": compound_literal_on_heap, |
| 156 | }, |
| 157 | { |
| 158 | "name": "tp:constructor_c_same_unit_after_provider", |
| 159 | "msg": None, |
| 160 | "count": 0, |
| 161 | "may_fail": compound_literal_on_heap, |
| 162 | }, |
| 163 | {"name": "tp:constructor_c_across_units_after_provider", "msg": None, "count": 0}, |
| 164 | {"name": "tp:main", "msg": None, "count": 0}, |
| 165 | { |
| 166 | "name": "tp:destructor_c_across_units_after_provider", |
| 167 | "msg": None, |
| 168 | "count": 0, |
| 169 | "may_fail": compound_literal_on_heap, |
| 170 | }, |
| 171 | { |
| 172 | "name": "tp:destructor_c_same_unit_after_provider", |
| 173 | "msg": None, |
| 174 | "count": 0, |
| 175 | "may_fail": compound_literal_on_heap, |
| 176 | }, |
| 177 | { |
| 178 | "name": "tp:destructor_c_same_unit_before_provider", |
| 179 | "msg": None, |
| 180 | "count": 0, |
| 181 | "may_fail": compound_literal_on_heap, |
| 182 | }, |
| 183 | { |
| 184 | "name": "tp:destructor_c_across_units_after_define", |
| 185 | "msg": None, |
| 186 | "count": 0, |
| 187 | "may_fail": compound_literal_on_heap, |
| 188 | }, |
| 189 | { |
| 190 | "name": "tp:destructor_c_same_unit_after_define", |
| 191 | "msg": None, |
| 192 | "count": 0, |
| 193 | "may_fail": compound_literal_on_heap, |
| 194 | }, |
| 195 | { |
| 196 | "name": "tp:destructor_c_same_unit_before_define", |
| 197 | "msg": None, |
| 198 | "count": 0, |
| 199 | "may_fail": compound_literal_on_heap, |
| 200 | }, |
| 201 | { |
| 202 | "name": "tp:destructor_c_across_units_before_define", |
| 203 | "msg": None, |
| 204 | "count": 0, |
| 205 | "may_fail": compound_literal_on_heap, |
| 206 | }, |
| 207 | ] |
| 208 | |
| 209 | expected_events_tp_so_cpp = [ |
| 210 | { |
| 211 | "name": "tp_so:constructor_cplusplus_provider_shared_library", |
| 212 | "msg": "global - shared library define and provider", |
| 213 | "count": 0, |
| 214 | }, |
| 215 | { |
| 216 | "name": "tp_so:constructor_cplusplus_provider_shared_library", |
| 217 | "msg": "main() local - shared library define and provider", |
| 218 | "count": 0, |
| 219 | }, |
| 220 | { |
| 221 | "name": "tp_so:destructor_cplusplus_provider_shared_library", |
| 222 | "msg": "main() local - shared library define and provider", |
| 223 | "count": 0, |
| 224 | }, |
| 225 | { |
| 226 | "name": "tp_so:destructor_cplusplus_provider_shared_library", |
| 227 | "msg": "global - shared library define and provider", |
| 228 | "count": 0, |
| 229 | }, |
| 230 | ] |
| 231 | |
| 232 | expected_events_tp_so = [ |
| 233 | {"name": "tp_so_c:constructor_c_provider_shared_library", "msg": None, "count": 0}, |
| 234 | {"name": "tp_so_c:destructor_c_provider_shared_library", "msg": None, "count": 0}, |
| 235 | ] |
| 236 | |
| 237 | expected_events_tp_a_cpp = [ |
| 238 | { |
| 239 | "name": "tp_a:constructor_cplusplus_provider_static_archive", |
| 240 | "msg": "global - static archive define and provider", |
| 241 | "count": 0, |
| 242 | "may_fail": compound_literal_on_heap, |
| 243 | }, |
| 244 | { |
| 245 | "name": "tp_a:constructor_cplusplus_provider_static_archive", |
| 246 | "msg": "main() local - static archive define and provider", |
| 247 | "count": 0, |
| 248 | }, |
| 249 | { |
| 250 | "name": "tp_a:destructor_cplusplus_provider_static_archive", |
| 251 | "msg": "main() local - static archive define and provider", |
| 252 | "count": 0, |
| 253 | }, |
| 254 | { |
| 255 | "name": "tp_a:destructor_cplusplus_provider_static_archive", |
| 256 | "msg": "global - static archive define and provider", |
| 257 | "count": 0, |
| 258 | "may_fail": compound_literal_on_heap, |
| 259 | }, |
| 260 | ] |
| 261 | |
| 262 | expected_events_tp_a = [ |
| 263 | {"name": "tp_a_c:constructor_c_provider_static_archive", "msg": None, "count": 0}, |
| 264 | {"name": "tp_a_c:destructor_c_provider_static_archive", "msg": None, "count": 0}, |
| 265 | ] |
| 266 | |
| 267 | |
| 268 | def capture_trace(tap, test_env, application, description): |
| 269 | # type: (lttngtest.TapGenerator, lttngtest._Environment) -> lttngtest.LocalSessionOutputLocation |
| 270 | tap.diagnostic(description) |
| 271 | |
| 272 | session_output_location = lttngtest.LocalSessionOutputLocation( |
| 273 | test_env.create_temporary_directory("trace") |
| 274 | ) |
| 275 | |
| 276 | client = lttngtest.LTTngClient(test_env, log=tap.diagnostic) |
| 277 | |
| 278 | with tap.case("Create a session") as test_case: |
| 279 | session = client.create_session(output=session_output_location) |
| 280 | tap.diagnostic("Created session `{session_name}`".format(session_name=session.name)) |
| 281 | |
| 282 | with tap.case( |
| 283 | "Add a channel to session `{session_name}`".format(session_name=session.name) |
| 284 | ) as test_case: |
| 285 | channel = session.add_channel(lttngtest.TracingDomain.User) |
| 286 | tap.diagnostic("Created channel `{channel_name}`".format(channel_name=channel.name)) |
| 287 | |
| 288 | # Enable all user space events, the default for a user tracepoint event rule. |
| 289 | channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp*")) |
| 290 | |
| 291 | with tap.case( |
| 292 | "Start session `{session_name}`".format(session_name=session.name) |
| 293 | ) as test_case: |
| 294 | session.start() |
| 295 | |
| 296 | test_app = test_env.launch_test_application(application) |
| 297 | with tap.case( |
| 298 | "Run test app '{}'".format(application, session_name=session.name) |
| 299 | ) as test_case: |
| 300 | test_app.wait_for_exit() |
| 301 | |
| 302 | with tap.case( |
| 303 | "Stop session `{session_name}`".format(session_name=session.name) |
| 304 | ) as test_case: |
| 305 | session.stop() |
| 306 | |
| 307 | with tap.case( |
| 308 | "Destroy session `{session_name}`".format(session_name=session.name) |
| 309 | ) as test_case: |
| 310 | session.destroy() |
| 311 | |
| 312 | return session_output_location |
| 313 | |
| 314 | |
| 315 | def validate_trace(trace_location, tap, expected_events): |
| 316 | # type: (pathlib.Path, lttngtest.TapGenerator) |
| 317 | unknown_event_count = 0 |
| 318 | |
| 319 | for msg in bt2.TraceCollectionMessageIterator(str(trace_location)): |
| 320 | if type(msg) is not bt2._EventMessageConst: |
| 321 | continue |
| 322 | |
| 323 | found = False |
| 324 | for event in expected_events: |
| 325 | if event["name"] == msg.event.name and event["msg"] is None: |
| 326 | found = True |
| 327 | event["count"] = event["count"] + 1 |
| 328 | break |
| 329 | elif ( |
| 330 | event["name"] == msg.event.name |
| 331 | and event["msg"] is not None |
| 332 | and event["msg"] == msg.event["msg"] |
| 333 | ): |
| 334 | found = True |
| 335 | event["count"] = event["count"] + 1 |
| 336 | break |
| 337 | |
| 338 | if found == False: |
| 339 | unknown_event_count = unknown_event_count + 1 |
| 340 | printmsg = None |
| 341 | if "msg" in msg.event: |
| 342 | printmsg = msg.event["msg"] |
| 343 | tap.diagnostic( |
| 344 | 'Unexpected event name="{}" msg="{}" encountered'.format( |
| 345 | msg.event.name, str(printmsg) |
| 346 | ) |
| 347 | ) |
| 348 | |
| 349 | for event in expected_events: |
| 350 | may_fail = "may_fail" in event.keys() and event["may_fail"] |
| 351 | if not may_fail: |
| 352 | tap.test( |
| 353 | event["count"] == 1, |
| 354 | 'Found expected event name="{}" msg="{}"'.format( |
| 355 | event["name"], str(event["msg"]) |
| 356 | ), |
| 357 | ) |
| 358 | else: |
| 359 | tap.skip("Event '{}' may or may not be recorded".format(event["name"])) |
| 360 | |
| 361 | tap.test(unknown_event_count == 0, "Found no unexpected events") |