2 * Unit tests for the notification API.
4 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
10 #include "bin/lttng/loglevel.hpp"
12 #include <common/payload-view.hpp>
13 #include <common/payload.hpp>
15 #include <lttng/domain.h>
16 #include <lttng/event-rule/jul-logging-internal.hpp>
17 #include <lttng/event-rule/jul-logging.h>
18 #include <lttng/event-rule/kernel-kprobe-internal.hpp>
19 #include <lttng/event-rule/kernel-kprobe.h>
20 #include <lttng/event-rule/kernel-syscall-internal.hpp>
21 #include <lttng/event-rule/kernel-syscall.h>
22 #include <lttng/event-rule/kernel-tracepoint-internal.hpp>
23 #include <lttng/event-rule/kernel-tracepoint.h>
24 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
25 #include <lttng/event-rule/kernel-uprobe.h>
26 #include <lttng/event-rule/python-logging-internal.hpp>
27 #include <lttng/event-rule/python-logging.h>
28 #include <lttng/event-rule/user-tracepoint-internal.hpp>
29 #include <lttng/event-rule/user-tracepoint.h>
30 #include <lttng/event.h>
31 #include <lttng/kernel-probe-internal.hpp>
32 #include <lttng/kernel-probe.h>
33 #include <lttng/userspace-probe-internal.hpp>
34 #include <lttng/userspace-probe.h>
43 int lttng_opt_quiet
= 1;
44 int lttng_opt_verbose
;
48 #define UPROBE_NUM_TESTS 9
50 #define UPROBE_NUM_TESTS 0
51 #endif /* __linux__ */
53 #define NUM_TESTS (244 + UPROBE_NUM_TESTS)
56 struct tracepoint_test
{
57 enum lttng_domain_type type
;
58 bool support_name_pattern_exclusion
;
62 using log_level_name_getter
= const char *(*) (int);
64 using event_rule_create
= struct lttng_event_rule
*(*) ();
65 using event_rule_set_log_level
= enum lttng_event_rule_status (*)(
66 struct lttng_event_rule
*, const struct lttng_log_level_rule
*);
68 static void test_event_rule_kernel_tracepoint()
70 struct lttng_event_rule
*tracepoint
= nullptr;
71 struct lttng_event_rule
*tracepoint_from_buffer
= nullptr;
72 enum lttng_event_rule_status status
;
73 const char *pattern
= "my_event_*";
74 const char *filter
= "msg_id == 23 && size >= 2048";
76 struct lttng_payload payload
;
78 diag("Testing lttng_event_rule_kernel_tracepoint.");
80 lttng_payload_init(&payload
);
82 tracepoint
= lttng_event_rule_kernel_tracepoint_create();
83 ok(tracepoint
, "tracepoint object.");
85 status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(tracepoint
, pattern
);
86 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
87 status
= lttng_event_rule_kernel_tracepoint_get_name_pattern(tracepoint
, &tmp
);
88 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
89 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
91 status
= lttng_event_rule_kernel_tracepoint_set_filter(tracepoint
, filter
);
92 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
93 status
= lttng_event_rule_kernel_tracepoint_get_filter(tracepoint
, &tmp
);
94 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
95 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
97 ok(lttng_event_rule_serialize(tracepoint
, &payload
) == 0, "Serializing.");
100 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
102 ok(lttng_event_rule_create_from_payload(&view
, &tracepoint_from_buffer
) > 0,
106 ok(lttng_event_rule_is_equal(tracepoint
, tracepoint_from_buffer
),
107 "serialized and from buffer are equal.");
109 lttng_payload_reset(&payload
);
110 lttng_event_rule_destroy(tracepoint
);
111 lttng_event_rule_destroy(tracepoint_from_buffer
);
114 static void test_event_rule_user_tracepoint()
118 struct lttng_event_rule
*tracepoint
= nullptr;
119 struct lttng_event_rule
*tracepoint_from_buffer
= nullptr;
120 enum lttng_event_rule_status status
;
121 const char *pattern
= "my_event_*";
122 const char *filter
= "msg_id == 23 && size >= 2048";
124 const char *name_pattern_exclusions
[] = { "my_event_test1",
127 struct lttng_log_level_rule
*log_level_rule
= nullptr;
128 const struct lttng_log_level_rule
*log_level_rule_return
= nullptr;
129 struct lttng_payload payload
;
131 diag("Testing lttng_event_rule_user_tracepoint.");
133 lttng_payload_init(&payload
);
135 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
136 LTTNG_ASSERT(log_level_rule
);
138 tracepoint
= lttng_event_rule_user_tracepoint_create();
139 ok(tracepoint
, "user tracepoint object.");
141 status
= lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint
, pattern
);
142 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
143 status
= lttng_event_rule_user_tracepoint_get_name_pattern(tracepoint
, &tmp
);
144 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
145 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
147 status
= lttng_event_rule_user_tracepoint_set_filter(tracepoint
, filter
);
148 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
149 status
= lttng_event_rule_user_tracepoint_get_filter(tracepoint
, &tmp
);
150 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
151 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
153 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(tracepoint
,
154 &log_level_rule_return
);
155 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
157 status
= lttng_event_rule_user_tracepoint_set_log_level_rule(tracepoint
, log_level_rule
);
158 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
159 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(tracepoint
,
160 &log_level_rule_return
);
161 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
163 /* Name pattern exclusions */
164 for (i
= 0; i
< 3; i
++) {
165 status
= lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
166 tracepoint
, name_pattern_exclusions
[i
]);
167 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
168 "setting name pattern exclusions \"%s\"",
169 name_pattern_exclusions
[i
]);
172 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(tracepoint
,
174 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting name pattern exclusion count.");
175 ok(count
== 3, "count is %d/3", count
);
177 for (i
= 0; i
< count
; i
++) {
178 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
179 tracepoint
, i
, &tmp
);
180 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
181 "getting name pattern exclusion at index %d.",
183 ok(!strncmp(name_pattern_exclusions
[i
], tmp
, strlen(name_pattern_exclusions
[i
])),
186 name_pattern_exclusions
[i
]);
189 ok(lttng_event_rule_serialize(tracepoint
, &payload
) == 0, "Serializing.");
192 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
194 ok(lttng_event_rule_create_from_payload(&view
, &tracepoint_from_buffer
) > 0,
198 ok(lttng_event_rule_is_equal(tracepoint
, tracepoint_from_buffer
),
199 "serialized and from buffer are equal.");
201 lttng_payload_reset(&payload
);
202 lttng_event_rule_destroy(tracepoint
);
203 lttng_event_rule_destroy(tracepoint_from_buffer
);
204 lttng_log_level_rule_destroy(log_level_rule
);
207 static void test_event_rule_syscall()
209 struct lttng_event_rule
*syscall
= nullptr;
210 struct lttng_event_rule
*syscall_from_buffer
= nullptr;
211 enum lttng_event_rule_status status
;
212 const char *pattern
= "my_event_*";
213 const char *filter
= "msg_id == 23 && size >= 2048";
215 struct lttng_payload payload
;
217 diag("Event rule syscall.");
219 lttng_payload_init(&payload
);
221 syscall
= lttng_event_rule_kernel_syscall_create(
222 LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
);
223 ok(syscall
, "syscall object.");
225 status
= lttng_event_rule_kernel_syscall_set_name_pattern(syscall
, pattern
);
226 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
227 status
= lttng_event_rule_kernel_syscall_get_name_pattern(syscall
, &tmp
);
228 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
229 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
231 status
= lttng_event_rule_kernel_syscall_set_filter(syscall
, filter
);
232 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
233 status
= lttng_event_rule_kernel_syscall_get_filter(syscall
, &tmp
);
234 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
235 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
237 ok(lttng_event_rule_serialize(syscall
, &payload
) == 0, "Serializing.");
240 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
242 ok(lttng_event_rule_create_from_payload(&view
, &syscall_from_buffer
) > 0,
246 ok(lttng_event_rule_is_equal(syscall
, syscall_from_buffer
),
247 "serialized and from buffer are equal.");
249 lttng_payload_reset(&payload
);
250 lttng_event_rule_destroy(syscall
);
251 lttng_event_rule_destroy(syscall_from_buffer
);
254 static void test_event_rule_jul_logging()
256 struct lttng_event_rule
*jul_logging
= nullptr;
257 struct lttng_event_rule
*jul_logging_from_buffer
= nullptr;
258 enum lttng_event_rule_status status
;
259 const char *pattern
= "my_event_*";
260 const char *filter
= "msg_id == 23 && size >= 2048";
262 struct lttng_log_level_rule
*log_level_rule
= nullptr;
263 const struct lttng_log_level_rule
*log_level_rule_return
= nullptr;
264 struct lttng_payload payload
;
266 diag("Testing lttng_event_rule_user_jul_logging.");
268 lttng_payload_init(&payload
);
270 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
271 LTTNG_ASSERT(log_level_rule
);
273 jul_logging
= lttng_event_rule_jul_logging_create();
274 ok(jul_logging
, "jul_logging object.");
276 status
= lttng_event_rule_jul_logging_set_name_pattern(jul_logging
, pattern
);
277 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
278 status
= lttng_event_rule_jul_logging_get_name_pattern(jul_logging
, &tmp
);
279 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
280 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
282 status
= lttng_event_rule_jul_logging_set_filter(jul_logging
, filter
);
283 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
284 status
= lttng_event_rule_jul_logging_get_filter(jul_logging
, &tmp
);
285 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
286 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
288 status
= lttng_event_rule_jul_logging_get_log_level_rule(jul_logging
,
289 &log_level_rule_return
);
290 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
292 status
= lttng_event_rule_jul_logging_set_log_level_rule(jul_logging
, log_level_rule
);
293 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
294 status
= lttng_event_rule_jul_logging_get_log_level_rule(jul_logging
,
295 &log_level_rule_return
);
296 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
298 ok(lttng_event_rule_serialize(jul_logging
, &payload
) == 0, "Serializing.");
301 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
303 ok(lttng_event_rule_create_from_payload(&view
, &jul_logging_from_buffer
) > 0,
307 ok(lttng_event_rule_is_equal(jul_logging
, jul_logging_from_buffer
),
308 "serialized and from buffer are equal.");
310 lttng_payload_reset(&payload
);
311 lttng_event_rule_destroy(jul_logging
);
312 lttng_event_rule_destroy(jul_logging_from_buffer
);
313 lttng_log_level_rule_destroy(log_level_rule
);
316 static void test_event_rule_log4j_logging()
318 struct lttng_event_rule
*log4j_logging
= nullptr;
319 struct lttng_event_rule
*log4j_logging_from_buffer
= nullptr;
320 enum lttng_event_rule_status status
;
321 const char *pattern
= "my_event_*";
322 const char *filter
= "msg_id == 23 && size >= 2048";
324 struct lttng_log_level_rule
*log_level_rule
= nullptr;
325 const struct lttng_log_level_rule
*log_level_rule_return
= nullptr;
326 struct lttng_payload payload
;
328 diag("Testing lttng_event_rule_user_log4j_logging.");
330 lttng_payload_init(&payload
);
332 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
333 LTTNG_ASSERT(log_level_rule
);
335 log4j_logging
= lttng_event_rule_log4j_logging_create();
336 ok(log4j_logging
, "log4j_logging object.");
338 status
= lttng_event_rule_log4j_logging_set_name_pattern(log4j_logging
, pattern
);
339 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
340 status
= lttng_event_rule_log4j_logging_get_name_pattern(log4j_logging
, &tmp
);
341 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
342 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
344 status
= lttng_event_rule_log4j_logging_set_filter(log4j_logging
, filter
);
345 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
346 status
= lttng_event_rule_log4j_logging_get_filter(log4j_logging
, &tmp
);
347 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
348 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
350 status
= lttng_event_rule_log4j_logging_get_log_level_rule(log4j_logging
,
351 &log_level_rule_return
);
352 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
354 status
= lttng_event_rule_log4j_logging_set_log_level_rule(log4j_logging
, log_level_rule
);
355 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
356 status
= lttng_event_rule_log4j_logging_get_log_level_rule(log4j_logging
,
357 &log_level_rule_return
);
358 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
360 ok(lttng_event_rule_serialize(log4j_logging
, &payload
) == 0, "Serializing.");
363 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
365 ok(lttng_event_rule_create_from_payload(&view
, &log4j_logging_from_buffer
) > 0,
369 ok(lttng_event_rule_is_equal(log4j_logging
, log4j_logging_from_buffer
),
370 "serialized and from buffer are equal.");
372 lttng_payload_reset(&payload
);
373 lttng_event_rule_destroy(log4j_logging
);
374 lttng_event_rule_destroy(log4j_logging_from_buffer
);
375 lttng_log_level_rule_destroy(log_level_rule
);
378 static void test_event_rule_log4j2_logging()
380 struct lttng_event_rule
*log4j2_logging
= nullptr;
381 struct lttng_event_rule
*log4j2_logging_from_buffer
= nullptr;
382 enum lttng_event_rule_status status
;
383 const char *pattern
= "my_event_*";
384 const char *filter
= "msg_id == 23 && size >= 2048";
386 struct lttng_log_level_rule
*log_level_rule
= nullptr;
387 const struct lttng_log_level_rule
*log_level_rule_return
= nullptr;
388 struct lttng_payload payload
;
390 diag("Testing lttng_event_rule_user_log4j2_logging.");
392 lttng_payload_init(&payload
);
394 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
395 LTTNG_ASSERT(log_level_rule
);
397 log4j2_logging
= lttng_event_rule_log4j2_logging_create();
398 ok(log4j2_logging
, "log4j2_logging object.");
400 status
= lttng_event_rule_log4j2_logging_set_name_pattern(log4j2_logging
, pattern
);
401 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
402 status
= lttng_event_rule_log4j2_logging_get_name_pattern(log4j2_logging
, &tmp
);
403 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
404 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
406 status
= lttng_event_rule_log4j2_logging_set_filter(log4j2_logging
, filter
);
407 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
408 status
= lttng_event_rule_log4j2_logging_get_filter(log4j2_logging
, &tmp
);
409 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
410 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
412 status
= lttng_event_rule_log4j2_logging_get_log_level_rule(log4j2_logging
,
413 &log_level_rule_return
);
414 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
416 status
= lttng_event_rule_log4j2_logging_set_log_level_rule(log4j2_logging
, log_level_rule
);
417 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
418 status
= lttng_event_rule_log4j2_logging_get_log_level_rule(log4j2_logging
,
419 &log_level_rule_return
);
420 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
422 ok(lttng_event_rule_serialize(log4j2_logging
, &payload
) == 0, "Serializing.");
425 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
427 ok(lttng_event_rule_create_from_payload(&view
, &log4j2_logging_from_buffer
) > 0,
431 ok(lttng_event_rule_is_equal(log4j2_logging
, log4j2_logging_from_buffer
),
432 "serialized and from buffer are equal.");
434 lttng_payload_reset(&payload
);
435 lttng_event_rule_destroy(log4j2_logging
);
436 lttng_event_rule_destroy(log4j2_logging_from_buffer
);
437 lttng_log_level_rule_destroy(log_level_rule
);
440 static void test_event_rule_python_logging()
442 struct lttng_event_rule
*python_logging
= nullptr;
443 struct lttng_event_rule
*python_logging_from_buffer
= nullptr;
444 enum lttng_event_rule_status status
;
445 const char *pattern
= "my_event_*";
446 const char *filter
= "msg_id == 23 && size >= 2048";
448 struct lttng_log_level_rule
*log_level_rule
= nullptr;
449 const struct lttng_log_level_rule
*log_level_rule_return
= nullptr;
450 struct lttng_payload payload
;
452 diag("Testing lttng_event_rule_user_python_logging.");
454 lttng_payload_init(&payload
);
456 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
457 LTTNG_ASSERT(log_level_rule
);
459 python_logging
= lttng_event_rule_python_logging_create();
460 ok(python_logging
, "python_logging object.");
462 status
= lttng_event_rule_python_logging_set_name_pattern(python_logging
, pattern
);
463 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
464 status
= lttng_event_rule_python_logging_get_name_pattern(python_logging
, &tmp
);
465 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
466 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
468 status
= lttng_event_rule_python_logging_set_filter(python_logging
, filter
);
469 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
470 status
= lttng_event_rule_python_logging_get_filter(python_logging
, &tmp
);
471 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
472 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
474 status
= lttng_event_rule_python_logging_get_log_level_rule(python_logging
,
475 &log_level_rule_return
);
476 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
478 status
= lttng_event_rule_python_logging_set_log_level_rule(python_logging
, log_level_rule
);
479 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
480 status
= lttng_event_rule_python_logging_get_log_level_rule(python_logging
,
481 &log_level_rule_return
);
482 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
484 ok(lttng_event_rule_serialize(python_logging
, &payload
) == 0, "Serializing.");
487 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
489 ok(lttng_event_rule_create_from_payload(&view
, &python_logging_from_buffer
) > 0,
493 ok(lttng_event_rule_is_equal(python_logging
, python_logging_from_buffer
),
494 "serialized and from buffer are equal.");
496 lttng_payload_reset(&payload
);
497 lttng_event_rule_destroy(python_logging
);
498 lttng_event_rule_destroy(python_logging_from_buffer
);
499 lttng_log_level_rule_destroy(log_level_rule
);
503 static void test_event_rule_userspace_probe()
505 struct lttng_event_rule
*uprobe
= nullptr;
506 struct lttng_event_rule
*uprobe_from_buffer
= nullptr;
507 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= nullptr;
508 struct lttng_userspace_probe_location
*probe_location
= nullptr;
509 const struct lttng_userspace_probe_location
*probe_location_tmp
= nullptr;
510 enum lttng_event_rule_status status
;
512 const char *probe_name
= "my_probe.";
514 struct lttng_payload payload
;
516 diag("Event rule uprobe.");
517 lttng_payload_init(&payload
);
519 lookup_method
= lttng_userspace_probe_location_lookup_method_function_elf_create();
520 if (!lookup_method
) {
521 fail("Setup error on userspace probe lookup method creation.");
525 probe_location
= lttng_userspace_probe_location_function_create(
527 "lttng_userspace_probe_location_tracepoint_create",
529 if (!probe_location
) {
530 fail("Setup error on userspace probe location creation.");
534 /* Ownership transferred to the probe location function object. */
535 lookup_method
= nullptr;
537 lttng_payload_init(&payload
);
539 uprobe
= lttng_event_rule_kernel_uprobe_create(probe_location
);
540 ok(uprobe
, "uprobe event rule object creation.");
542 status
= lttng_event_rule_kernel_uprobe_get_location(uprobe
, &probe_location_tmp
);
543 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting uprobe event rule location.");
544 ok(lttng_userspace_probe_location_is_equal(probe_location
, probe_location_tmp
),
545 "Location is equal.");
547 status
= lttng_event_rule_kernel_uprobe_set_event_name(uprobe
, probe_name
);
548 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting uprobe event rule name: %s.", probe_name
);
549 status
= lttng_event_rule_kernel_uprobe_get_event_name(uprobe
, &tmp
);
550 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting uprobe name.");
551 ok(!strcmp(probe_name
, tmp
), "Uprobe name are equal.");
553 ok(lttng_event_rule_serialize(uprobe
, &payload
) == 0, "Serializing.");
556 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
558 ok(lttng_event_rule_create_from_payload(&view
, &uprobe_from_buffer
) > 0,
562 ok(lttng_event_rule_is_equal(uprobe
, uprobe_from_buffer
),
563 "serialized and from buffer are equal.");
566 lttng_payload_reset(&payload
);
567 lttng_event_rule_destroy(uprobe
);
568 lttng_event_rule_destroy(uprobe_from_buffer
);
569 lttng_userspace_probe_location_destroy(probe_location
);
570 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
573 static void test_event_rule_userspace_probe(void)
579 test_event_rule_kernel_probe_by_location(const struct lttng_kernel_probe_location
*location
)
581 struct lttng_event_rule
*kprobe
= nullptr;
582 struct lttng_event_rule
*kprobe_from_buffer
= nullptr;
583 enum lttng_event_rule_status status
;
584 const struct lttng_kernel_probe_location
*_location
;
586 const char *probe_name
= "my_probe";
588 struct lttng_payload payload
;
590 diag("Event rule kprobe for location type %d.",
591 lttng_kernel_probe_location_get_type(location
));
593 lttng_payload_init(&payload
);
595 kprobe
= lttng_event_rule_kernel_kprobe_create(location
);
596 ok(kprobe
, "kprobe event rule object creation.");
598 status
= lttng_event_rule_kernel_kprobe_get_location(kprobe
, &_location
);
599 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting kprobe event rule location.");
600 ok(lttng_kernel_probe_location_is_equal(location
, _location
), "Locations are equal.");
602 status
= lttng_event_rule_kernel_kprobe_set_event_name(kprobe
, probe_name
);
603 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting kprobe event rule name: %s.", probe_name
);
604 status
= lttng_event_rule_kernel_kprobe_get_event_name(kprobe
, &tmp
);
605 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting kprobe name.");
606 ok(!strcmp(probe_name
, tmp
), "kprobe name are equal.");
608 ok(lttng_event_rule_serialize(kprobe
, &payload
) == 0, "Serializing.");
611 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
613 ok(lttng_event_rule_create_from_payload(&view
, &kprobe_from_buffer
) > 0,
617 ok(lttng_event_rule_is_equal(kprobe
, kprobe_from_buffer
),
618 "serialized and from buffer are equal.");
620 lttng_payload_reset(&payload
);
621 lttng_event_rule_destroy(kprobe
);
622 lttng_event_rule_destroy(kprobe_from_buffer
);
625 static void test_event_rule_kernel_probe()
627 struct lttng_kernel_probe_location
*address_location
= nullptr;
628 struct lttng_kernel_probe_location
*symbol_location
= nullptr;
630 address_location
= lttng_kernel_probe_location_address_create(50);
631 symbol_location
= lttng_kernel_probe_location_symbol_create("une_bonne", 50);
632 LTTNG_ASSERT(address_location
);
633 LTTNG_ASSERT(symbol_location
);
635 test_event_rule_kernel_probe_by_location(address_location
);
636 test_event_rule_kernel_probe_by_location(symbol_location
);
638 lttng_kernel_probe_location_destroy(address_location
);
639 lttng_kernel_probe_location_destroy(symbol_location
);
642 static void test_set_event_rule_log_level_rules(struct lttng_event_rule
*event_rule
,
643 event_rule_set_log_level set_log_level
,
645 enum lttng_event_rule_status
*exactly_status
,
646 enum lttng_event_rule_status
*as_severe_status
)
648 struct lttng_log_level_rule
*log_level_rule
;
650 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
651 LTTNG_ASSERT(log_level_rule
);
653 *as_severe_status
= set_log_level(event_rule
, log_level_rule
);
654 lttng_log_level_rule_destroy(log_level_rule
);
656 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
657 LTTNG_ASSERT(log_level_rule
);
659 *exactly_status
= set_log_level(event_rule
, log_level_rule
);
660 lttng_log_level_rule_destroy(log_level_rule
);
663 static void test_event_rule_log_level_generic(enum lttng_event_rule_type event_rule_type
,
664 log_level_name_getter get_log_level_name
,
665 event_rule_create create_event_rule
,
666 event_rule_set_log_level set_log_level
,
667 const int tagged_log_level_values
[],
668 size_t tagged_log_level_values_count
,
669 const int valid_log_level_values
[],
670 size_t valid_log_level_values_count
,
671 const int invalid_log_level_values
[],
672 size_t invalid_log_level_values_count
)
675 struct lttng_event_rule
*rule
;
676 enum lttng_event_rule_status er_exactly_status
, er_as_severe_status
;
677 const char *event_rule_type_str
= lttng_event_rule_type_str(event_rule_type
);
679 diag("Test %s event rule + log level rule", event_rule_type_str
);
681 rule
= create_event_rule();
684 for (i
= 0; i
< tagged_log_level_values_count
; i
++) {
685 const int tagged_log_level_value
= tagged_log_level_values
[i
];
687 test_set_event_rule_log_level_rules(rule
,
689 tagged_log_level_value
,
691 &er_as_severe_status
);
692 ok(er_exactly_status
== LTTNG_EVENT_RULE_STATUS_OK
,
693 "Log level rule \"exactly\" accepted by %s event rule: level = %s",
695 get_log_level_name(tagged_log_level_value
));
696 ok(er_as_severe_status
== LTTNG_EVENT_RULE_STATUS_OK
,
697 "Log level rule \"as least as severe as\" accepted by %s event rule: level = %s",
699 get_log_level_name(tagged_log_level_value
));
702 /* Test valid custom loglevels */
703 for (i
= 0; i
< valid_log_level_values_count
; i
++) {
704 const int valid_log_level_value
= valid_log_level_values
[i
];
706 test_set_event_rule_log_level_rules(rule
,
708 valid_log_level_value
,
710 &er_as_severe_status
);
711 ok(er_exactly_status
== LTTNG_EVENT_RULE_STATUS_OK
,
712 "Log level rule \"exactly\" accepted by %s event rule: level = %d",
714 valid_log_level_value
);
715 ok(er_as_severe_status
== LTTNG_EVENT_RULE_STATUS_OK
,
716 "Log level rule \"as least as severe as\" accepted by %s event rule: level = %d",
718 valid_log_level_value
);
721 /* Test invalid custom loglevels */
722 for (i
= 0; i
< invalid_log_level_values_count
; i
++) {
723 const int invalid_log_level_value
= invalid_log_level_values
[i
];
725 test_set_event_rule_log_level_rules(rule
,
727 invalid_log_level_value
,
729 &er_as_severe_status
);
730 ok(er_exactly_status
== LTTNG_EVENT_RULE_STATUS_INVALID
,
731 "Log level rule \"exactly\" rejected by %s event rule: level = %d",
733 invalid_log_level_value
);
734 ok(er_as_severe_status
== LTTNG_EVENT_RULE_STATUS_INVALID
,
735 "Log level rule \"as least as severe as\" rejected by %s event rule: level = %d",
737 invalid_log_level_value
);
740 lttng_event_rule_destroy(rule
);
743 static void test_event_rule_log_level_ust()
745 const int tagged_log_level_values
[] = {
746 LTTNG_LOGLEVEL_EMERG
, LTTNG_LOGLEVEL_ALERT
,
747 LTTNG_LOGLEVEL_CRIT
, LTTNG_LOGLEVEL_ERR
,
748 LTTNG_LOGLEVEL_WARNING
, LTTNG_LOGLEVEL_NOTICE
,
749 LTTNG_LOGLEVEL_INFO
, LTTNG_LOGLEVEL_DEBUG_SYSTEM
,
750 LTTNG_LOGLEVEL_DEBUG_PROGRAM
, LTTNG_LOGLEVEL_DEBUG_PROCESS
,
751 LTTNG_LOGLEVEL_DEBUG_MODULE
, LTTNG_LOGLEVEL_DEBUG_UNIT
,
752 LTTNG_LOGLEVEL_DEBUG_FUNCTION
, LTTNG_LOGLEVEL_DEBUG_LINE
,
753 LTTNG_LOGLEVEL_DEBUG
,
755 const int invalid_log_level_values
[] = {
758 LTTNG_LOGLEVEL_DEBUG
+ 1,
759 LTTNG_LOGLEVEL_EMERG
- 1,
762 test_event_rule_log_level_generic(LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
,
763 loglevel_value_to_name
,
764 lttng_event_rule_user_tracepoint_create
,
765 lttng_event_rule_user_tracepoint_set_log_level_rule
,
766 tagged_log_level_values
,
767 ARRAY_SIZE(tagged_log_level_values
),
770 invalid_log_level_values
,
771 ARRAY_SIZE(invalid_log_level_values
));
774 static void test_event_rule_log_level_jul()
776 const int tagged_log_level_values
[] = {
777 LTTNG_LOGLEVEL_JUL_OFF
, LTTNG_LOGLEVEL_JUL_SEVERE
, LTTNG_LOGLEVEL_JUL_WARNING
,
778 LTTNG_LOGLEVEL_JUL_INFO
, LTTNG_LOGLEVEL_JUL_CONFIG
, LTTNG_LOGLEVEL_JUL_FINE
,
779 LTTNG_LOGLEVEL_JUL_FINER
, LTTNG_LOGLEVEL_JUL_FINEST
, LTTNG_LOGLEVEL_JUL_ALL
,
781 const int valid_log_level_values
[] = { 0, -1980, 1995 };
782 /* JUL loglevels between int32_min and int32_max are all valid. */
784 test_event_rule_log_level_generic(LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
,
785 loglevel_jul_value_to_name
,
786 lttng_event_rule_jul_logging_create
,
787 lttng_event_rule_jul_logging_set_log_level_rule
,
788 tagged_log_level_values
,
789 ARRAY_SIZE(tagged_log_level_values
),
790 valid_log_level_values
,
791 ARRAY_SIZE(valid_log_level_values
),
796 static void test_event_rule_log_level_log4j()
798 const int tagged_log_level_values
[] = {
799 LTTNG_LOGLEVEL_LOG4J_OFF
, LTTNG_LOGLEVEL_LOG4J_FATAL
, LTTNG_LOGLEVEL_LOG4J_ERROR
,
800 LTTNG_LOGLEVEL_LOG4J_WARN
, LTTNG_LOGLEVEL_LOG4J_INFO
, LTTNG_LOGLEVEL_LOG4J_DEBUG
,
801 LTTNG_LOGLEVEL_LOG4J_TRACE
, LTTNG_LOGLEVEL_LOG4J_ALL
,
803 const int valid_log_level_values
[] = { 0 - 1980, 1995 };
804 /* Log4j loglevels between int32_min and int32_max are all valid. */
806 test_event_rule_log_level_generic(LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
,
807 loglevel_log4j_value_to_name
,
808 lttng_event_rule_log4j_logging_create
,
809 lttng_event_rule_log4j_logging_set_log_level_rule
,
810 tagged_log_level_values
,
811 ARRAY_SIZE(tagged_log_level_values
),
812 valid_log_level_values
,
813 ARRAY_SIZE(valid_log_level_values
),
818 static void test_event_rule_log_level_log4j2()
820 const int tagged_log_level_values
[] = {
821 LTTNG_LOGLEVEL_LOG4J2_OFF
, LTTNG_LOGLEVEL_LOG4J2_FATAL
,
822 LTTNG_LOGLEVEL_LOG4J2_ERROR
, LTTNG_LOGLEVEL_LOG4J2_WARN
,
823 LTTNG_LOGLEVEL_LOG4J2_INFO
, LTTNG_LOGLEVEL_LOG4J2_DEBUG
,
824 LTTNG_LOGLEVEL_LOG4J2_TRACE
, LTTNG_LOGLEVEL_LOG4J2_ALL
,
826 const int valid_log_level_values
[] = { 0, 101, 1995 };
827 const int invalid_log_level_values
[] = { -1, -100, -65537 };
829 test_event_rule_log_level_generic(LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
,
830 loglevel_log4j2_value_to_name
,
831 lttng_event_rule_log4j2_logging_create
,
832 lttng_event_rule_log4j2_logging_set_log_level_rule
,
833 tagged_log_level_values
,
834 ARRAY_SIZE(tagged_log_level_values
),
835 valid_log_level_values
,
836 ARRAY_SIZE(valid_log_level_values
),
837 invalid_log_level_values
,
838 ARRAY_SIZE(invalid_log_level_values
));
841 static void test_event_rule_log_level_python()
843 const int tagged_log_level_values
[] = {
844 LTTNG_LOGLEVEL_PYTHON_CRITICAL
, LTTNG_LOGLEVEL_PYTHON_ERROR
,
845 LTTNG_LOGLEVEL_PYTHON_WARNING
, LTTNG_LOGLEVEL_PYTHON_INFO
,
846 LTTNG_LOGLEVEL_PYTHON_DEBUG
, LTTNG_LOGLEVEL_PYTHON_NOTSET
,
848 const int valid_log_level_values
[] = {
855 test_event_rule_log_level_generic(LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
,
856 loglevel_python_value_to_name
,
857 lttng_event_rule_python_logging_create
,
858 lttng_event_rule_python_logging_set_log_level_rule
,
859 tagged_log_level_values
,
860 ARRAY_SIZE(tagged_log_level_values
),
861 valid_log_level_values
,
862 ARRAY_SIZE(valid_log_level_values
),
869 plan_tests(NUM_TESTS
);
870 test_event_rule_kernel_tracepoint();
871 test_event_rule_user_tracepoint();
872 test_event_rule_syscall();
873 test_event_rule_userspace_probe();
874 test_event_rule_kernel_probe();
875 test_event_rule_log4j_logging();
876 test_event_rule_log4j2_logging();
877 test_event_rule_jul_logging();
878 test_event_rule_python_logging();
879 test_event_rule_log_level_ust();
880 test_event_rule_log_level_jul();
881 test_event_rule_log_level_log4j();
882 test_event_rule_log_level_log4j2();
883 test_event_rule_log_level_python();
884 return exit_status();