Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / tests / unit / test_event_rule.cpp
1 /*
2 * Unit tests for the notification API.
3 *
4 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only
7 *
8 */
9
10 #include "bin/lttng/loglevel.hpp"
11
12 #include <common/payload-view.hpp>
13 #include <common/payload.hpp>
14
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>
35
36 #include <inttypes.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <tap/tap.h>
40 #include <unistd.h>
41
42 /* For error.h. */
43 int lttng_opt_quiet = 1;
44 int lttng_opt_verbose;
45 int lttng_opt_mi;
46
47 #ifdef __linux__
48 #define UPROBE_NUM_TESTS 9
49 #else /* __linux__ */
50 #define UPROBE_NUM_TESTS 0
51 #endif /* __linux__ */
52
53 #define NUM_TESTS (244 + UPROBE_NUM_TESTS)
54
55 namespace {
56 struct tracepoint_test {
57 enum lttng_domain_type type;
58 bool support_name_pattern_exclusion;
59 };
60 } /* namespace */
61
62 using log_level_name_getter = const char *(*) (int);
63
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 *);
67
68 static void test_event_rule_kernel_tracepoint()
69 {
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";
75 const char *tmp;
76 struct lttng_payload payload;
77
78 diag("Testing lttng_event_rule_kernel_tracepoint.");
79
80 lttng_payload_init(&payload);
81
82 tracepoint = lttng_event_rule_kernel_tracepoint_create();
83 ok(tracepoint, "tracepoint object.");
84
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.");
90
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.");
96
97 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
98
99 {
100 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
101
102 ok(lttng_event_rule_create_from_payload(&view, &tracepoint_from_buffer) > 0,
103 "Deserializing.");
104 }
105
106 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer),
107 "serialized and from buffer are equal.");
108
109 lttng_payload_reset(&payload);
110 lttng_event_rule_destroy(tracepoint);
111 lttng_event_rule_destroy(tracepoint_from_buffer);
112 }
113
114 static void test_event_rule_user_tracepoint()
115 {
116 int i;
117 unsigned int count;
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";
123 const char *tmp;
124 const char *name_pattern_exclusions[] = { "my_event_test1",
125 "my_event_test2",
126 "my_event_test3" };
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;
130
131 diag("Testing lttng_event_rule_user_tracepoint.");
132
133 lttng_payload_init(&payload);
134
135 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
136 LTTNG_ASSERT(log_level_rule);
137
138 tracepoint = lttng_event_rule_user_tracepoint_create();
139 ok(tracepoint, "user tracepoint object.");
140
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.");
146
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.");
152
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.");
156
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.");
162
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]);
170 }
171
172 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(tracepoint,
173 &count);
174 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting name pattern exclusion count.");
175 ok(count == 3, "count is %d/3", count);
176
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.",
182 i);
183 ok(!strncmp(name_pattern_exclusions[i], tmp, strlen(name_pattern_exclusions[i])),
184 "%s == %s.",
185 tmp,
186 name_pattern_exclusions[i]);
187 }
188
189 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
190
191 {
192 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
193
194 ok(lttng_event_rule_create_from_payload(&view, &tracepoint_from_buffer) > 0,
195 "Deserializing.");
196 }
197
198 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer),
199 "serialized and from buffer are equal.");
200
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);
205 }
206
207 static void test_event_rule_syscall()
208 {
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";
214 const char *tmp;
215 struct lttng_payload payload;
216
217 diag("Event rule syscall.");
218
219 lttng_payload_init(&payload);
220
221 syscall = lttng_event_rule_kernel_syscall_create(
222 LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY);
223 ok(syscall, "syscall object.");
224
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.");
230
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.");
236
237 ok(lttng_event_rule_serialize(syscall, &payload) == 0, "Serializing.");
238
239 {
240 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
241
242 ok(lttng_event_rule_create_from_payload(&view, &syscall_from_buffer) > 0,
243 "Deserializing.");
244 }
245
246 ok(lttng_event_rule_is_equal(syscall, syscall_from_buffer),
247 "serialized and from buffer are equal.");
248
249 lttng_payload_reset(&payload);
250 lttng_event_rule_destroy(syscall);
251 lttng_event_rule_destroy(syscall_from_buffer);
252 }
253
254 static void test_event_rule_jul_logging()
255 {
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";
261 const char *tmp;
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;
265
266 diag("Testing lttng_event_rule_user_jul_logging.");
267
268 lttng_payload_init(&payload);
269
270 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
271 LTTNG_ASSERT(log_level_rule);
272
273 jul_logging = lttng_event_rule_jul_logging_create();
274 ok(jul_logging, "jul_logging object.");
275
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.");
281
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.");
287
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.");
291
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.");
297
298 ok(lttng_event_rule_serialize(jul_logging, &payload) == 0, "Serializing.");
299
300 {
301 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
302
303 ok(lttng_event_rule_create_from_payload(&view, &jul_logging_from_buffer) > 0,
304 "Deserializing.");
305 }
306
307 ok(lttng_event_rule_is_equal(jul_logging, jul_logging_from_buffer),
308 "serialized and from buffer are equal.");
309
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);
314 }
315
316 static void test_event_rule_log4j_logging()
317 {
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";
323 const char *tmp;
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;
327
328 diag("Testing lttng_event_rule_user_log4j_logging.");
329
330 lttng_payload_init(&payload);
331
332 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
333 LTTNG_ASSERT(log_level_rule);
334
335 log4j_logging = lttng_event_rule_log4j_logging_create();
336 ok(log4j_logging, "log4j_logging object.");
337
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.");
343
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.");
349
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.");
353
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.");
359
360 ok(lttng_event_rule_serialize(log4j_logging, &payload) == 0, "Serializing.");
361
362 {
363 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
364
365 ok(lttng_event_rule_create_from_payload(&view, &log4j_logging_from_buffer) > 0,
366 "Deserializing.");
367 }
368
369 ok(lttng_event_rule_is_equal(log4j_logging, log4j_logging_from_buffer),
370 "serialized and from buffer are equal.");
371
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);
376 }
377
378 static void test_event_rule_log4j2_logging()
379 {
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";
385 const char *tmp;
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;
389
390 diag("Testing lttng_event_rule_user_log4j2_logging.");
391
392 lttng_payload_init(&payload);
393
394 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
395 LTTNG_ASSERT(log_level_rule);
396
397 log4j2_logging = lttng_event_rule_log4j2_logging_create();
398 ok(log4j2_logging, "log4j2_logging object.");
399
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.");
405
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.");
411
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.");
415
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.");
421
422 ok(lttng_event_rule_serialize(log4j2_logging, &payload) == 0, "Serializing.");
423
424 {
425 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
426
427 ok(lttng_event_rule_create_from_payload(&view, &log4j2_logging_from_buffer) > 0,
428 "Deserializing.");
429 }
430
431 ok(lttng_event_rule_is_equal(log4j2_logging, log4j2_logging_from_buffer),
432 "serialized and from buffer are equal.");
433
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);
438 }
439
440 static void test_event_rule_python_logging()
441 {
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";
447 const char *tmp;
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;
451
452 diag("Testing lttng_event_rule_user_python_logging.");
453
454 lttng_payload_init(&payload);
455
456 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
457 LTTNG_ASSERT(log_level_rule);
458
459 python_logging = lttng_event_rule_python_logging_create();
460 ok(python_logging, "python_logging object.");
461
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.");
467
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.");
473
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.");
477
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.");
483
484 ok(lttng_event_rule_serialize(python_logging, &payload) == 0, "Serializing.");
485
486 {
487 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
488
489 ok(lttng_event_rule_create_from_payload(&view, &python_logging_from_buffer) > 0,
490 "Deserializing.");
491 }
492
493 ok(lttng_event_rule_is_equal(python_logging, python_logging_from_buffer),
494 "serialized and from buffer are equal.");
495
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);
500 }
501
502 #ifdef __linux__
503 static void test_event_rule_userspace_probe()
504 {
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;
511
512 const char *probe_name = "my_probe.";
513 const char *tmp;
514 struct lttng_payload payload;
515
516 diag("Event rule uprobe.");
517 lttng_payload_init(&payload);
518
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.");
522 goto end;
523 }
524
525 probe_location = lttng_userspace_probe_location_function_create(
526 "/proc/self/exe",
527 "lttng_userspace_probe_location_tracepoint_create",
528 lookup_method);
529 if (!probe_location) {
530 fail("Setup error on userspace probe location creation.");
531 goto end;
532 }
533
534 /* Ownership transferred to the probe location function object. */
535 lookup_method = nullptr;
536
537 lttng_payload_init(&payload);
538
539 uprobe = lttng_event_rule_kernel_uprobe_create(probe_location);
540 ok(uprobe, "uprobe event rule object creation.");
541
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.");
546
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.");
552
553 ok(lttng_event_rule_serialize(uprobe, &payload) == 0, "Serializing.");
554
555 {
556 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
557
558 ok(lttng_event_rule_create_from_payload(&view, &uprobe_from_buffer) > 0,
559 "Deserializing.");
560 }
561
562 ok(lttng_event_rule_is_equal(uprobe, uprobe_from_buffer),
563 "serialized and from buffer are equal.");
564
565 end:
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);
571 }
572 #else
573 static void test_event_rule_userspace_probe(void)
574 {
575 }
576 #endif
577
578 static void
579 test_event_rule_kernel_probe_by_location(const struct lttng_kernel_probe_location *location)
580 {
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;
585
586 const char *probe_name = "my_probe";
587 const char *tmp;
588 struct lttng_payload payload;
589
590 diag("Event rule kprobe for location type %d.",
591 lttng_kernel_probe_location_get_type(location));
592
593 lttng_payload_init(&payload);
594
595 kprobe = lttng_event_rule_kernel_kprobe_create(location);
596 ok(kprobe, "kprobe event rule object creation.");
597
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.");
601
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.");
607
608 ok(lttng_event_rule_serialize(kprobe, &payload) == 0, "Serializing.");
609
610 {
611 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
612
613 ok(lttng_event_rule_create_from_payload(&view, &kprobe_from_buffer) > 0,
614 "Deserializing.");
615 }
616
617 ok(lttng_event_rule_is_equal(kprobe, kprobe_from_buffer),
618 "serialized and from buffer are equal.");
619
620 lttng_payload_reset(&payload);
621 lttng_event_rule_destroy(kprobe);
622 lttng_event_rule_destroy(kprobe_from_buffer);
623 }
624
625 static void test_event_rule_kernel_probe()
626 {
627 struct lttng_kernel_probe_location *address_location = nullptr;
628 struct lttng_kernel_probe_location *symbol_location = nullptr;
629
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);
634
635 test_event_rule_kernel_probe_by_location(address_location);
636 test_event_rule_kernel_probe_by_location(symbol_location);
637
638 lttng_kernel_probe_location_destroy(address_location);
639 lttng_kernel_probe_location_destroy(symbol_location);
640 }
641
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,
644 int log_level,
645 enum lttng_event_rule_status *exactly_status,
646 enum lttng_event_rule_status *as_severe_status)
647 {
648 struct lttng_log_level_rule *log_level_rule;
649
650 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level);
651 LTTNG_ASSERT(log_level_rule);
652
653 *as_severe_status = set_log_level(event_rule, log_level_rule);
654 lttng_log_level_rule_destroy(log_level_rule);
655
656 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
657 LTTNG_ASSERT(log_level_rule);
658
659 *exactly_status = set_log_level(event_rule, log_level_rule);
660 lttng_log_level_rule_destroy(log_level_rule);
661 }
662
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)
673 {
674 size_t i;
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);
678
679 diag("Test %s event rule + log level rule", event_rule_type_str);
680
681 rule = create_event_rule();
682 LTTNG_ASSERT(rule);
683
684 for (i = 0; i < tagged_log_level_values_count; i++) {
685 const int tagged_log_level_value = tagged_log_level_values[i];
686
687 test_set_event_rule_log_level_rules(rule,
688 set_log_level,
689 tagged_log_level_value,
690 &er_exactly_status,
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",
694 event_rule_type_str,
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",
698 event_rule_type_str,
699 get_log_level_name(tagged_log_level_value));
700 }
701
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];
705
706 test_set_event_rule_log_level_rules(rule,
707 set_log_level,
708 valid_log_level_value,
709 &er_exactly_status,
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",
713 event_rule_type_str,
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",
717 event_rule_type_str,
718 valid_log_level_value);
719 }
720
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];
724
725 test_set_event_rule_log_level_rules(rule,
726 set_log_level,
727 invalid_log_level_value,
728 &er_exactly_status,
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",
732 event_rule_type_str,
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",
736 event_rule_type_str,
737 invalid_log_level_value);
738 }
739
740 lttng_event_rule_destroy(rule);
741 }
742
743 static void test_event_rule_log_level_ust()
744 {
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,
754 };
755 const int invalid_log_level_values[] = {
756 -1980,
757 1995,
758 LTTNG_LOGLEVEL_DEBUG + 1,
759 LTTNG_LOGLEVEL_EMERG - 1,
760 };
761
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),
768 nullptr,
769 0,
770 invalid_log_level_values,
771 ARRAY_SIZE(invalid_log_level_values));
772 }
773
774 static void test_event_rule_log_level_jul()
775 {
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,
780 };
781 const int valid_log_level_values[] = { 0, -1980, 1995 };
782 /* JUL loglevels between int32_min and int32_max are all valid. */
783
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),
792 nullptr,
793 0);
794 }
795
796 static void test_event_rule_log_level_log4j()
797 {
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,
802 };
803 const int valid_log_level_values[] = { 0 - 1980, 1995 };
804 /* Log4j loglevels between int32_min and int32_max are all valid. */
805
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),
814 nullptr,
815 0);
816 }
817
818 static void test_event_rule_log_level_log4j2()
819 {
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,
825 };
826 const int valid_log_level_values[] = { 0, 101, 1995 };
827 const int invalid_log_level_values[] = { -1, -100, -65537 };
828
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));
839 }
840
841 static void test_event_rule_log_level_python()
842 {
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,
847 };
848 const int valid_log_level_values[] = {
849 45,
850 35,
851 0,
852 -657,
853 };
854
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),
863 nullptr,
864 0);
865 }
866
867 int main()
868 {
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();
885 }
This page took 0.051741 seconds and 5 git commands to generate.