Introduce lttng_event_rule_log4j_logging
[lttng-tools.git] / tests / unit / test_event_rule.c
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 <assert.h>
11 #include <inttypes.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include <tap/tap.h>
17
18 #include <common/payload-view.h>
19 #include <common/payload.h>
20 #include <lttng/domain.h>
21 #include <lttng/event-rule/jul-logging-internal.h>
22 #include <lttng/event-rule/jul-logging.h>
23 #include <lttng/event-rule/kernel-kprobe-internal.h>
24 #include <lttng/event-rule/kernel-kprobe.h>
25 #include <lttng/event-rule/kernel-syscall-internal.h>
26 #include <lttng/event-rule/kernel-syscall.h>
27 #include <lttng/event-rule/tracepoint-internal.h>
28 #include <lttng/event-rule/tracepoint.h>
29 #include <lttng/event-rule/kernel-tracepoint-internal.h>
30 #include <lttng/event-rule/kernel-tracepoint.h>
31 #include <lttng/event-rule/kernel-uprobe-internal.h>
32 #include <lttng/event-rule/kernel-uprobe.h>
33 #include <lttng/event-rule/user-tracepoint-internal.h>
34 #include <lttng/event-rule/user-tracepoint.h>
35 #include <lttng/event.h>
36 #include <lttng/kernel-probe-internal.h>
37 #include <lttng/kernel-probe.h>
38 #include <lttng/userspace-probe-internal.h>
39 #include <lttng/userspace-probe.h>
40 #include "bin/lttng/loglevel.h"
41
42 /* For error.h. */
43 int lttng_opt_quiet = 1;
44 int lttng_opt_verbose;
45 int lttng_opt_mi;
46
47 #define NUM_TESTS 304
48
49 struct tracepoint_test {
50 enum lttng_domain_type type;
51 bool support_name_pattern_exclusion;
52 };
53
54 typedef const char *(*log_level_name_getter)(int log_level);
55
56 static
57 void test_event_rule_tracepoint_by_domain(const struct tracepoint_test *test)
58 {
59 unsigned int count;
60 struct lttng_event_rule *tracepoint = NULL;
61 struct lttng_event_rule *tracepoint_from_buffer = NULL;
62 enum lttng_event_rule_status status;
63 enum lttng_domain_type domain_type, type;
64 const char *pattern="my_event_*";
65 const char *filter="msg_id == 23 && size >= 2048";
66 const char *tmp;
67 const char *name_pattern_exclusions[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"};
68 struct lttng_log_level_rule *log_level_rule = NULL;
69 const struct lttng_log_level_rule *log_level_rule_return = NULL;
70 struct lttng_payload payload;
71
72 type = test->type;
73 diag("Testing domain %d.", type);
74
75 lttng_payload_init(&payload);
76
77 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
78 assert(log_level_rule);
79
80 tracepoint = lttng_event_rule_tracepoint_create(type);
81 ok(tracepoint, "tracepoint object.");
82
83 status = lttng_event_rule_tracepoint_get_domain_type(tracepoint, &domain_type);
84 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get tracepoint domain.");
85 ok(domain_type == type, "domain type got %d expected %d.", domain_type, type);
86
87 status = lttng_event_rule_tracepoint_set_name_pattern(tracepoint, pattern);
88 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
89 status = lttng_event_rule_tracepoint_get_name_pattern(tracepoint, &tmp);
90 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
91 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
92
93 status = lttng_event_rule_tracepoint_set_filter(tracepoint, filter);
94 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
95 status = lttng_event_rule_tracepoint_get_filter(tracepoint, &tmp);
96 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
97 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
98
99 status = lttng_event_rule_tracepoint_get_log_level_rule(tracepoint, &log_level_rule_return);
100 ok(status == LTTNG_EVENT_RULE_STATUS_UNSET, "get unset log level rule.");
101
102 if (type != LTTNG_DOMAIN_KERNEL) {
103 status = lttng_event_rule_tracepoint_set_log_level_rule(tracepoint, log_level_rule);
104 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting log level rule.");
105 status = lttng_event_rule_tracepoint_get_log_level_rule(tracepoint, &log_level_rule_return);
106 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get log level rule.");
107 }
108
109 if (test->support_name_pattern_exclusion) {
110 int i;
111
112 for (i = 0; i < 3; i++) {
113 status = lttng_event_rule_tracepoint_add_name_pattern_exclusion(tracepoint, name_pattern_exclusions[i]);
114 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting name pattern exclusions \"%s\"", name_pattern_exclusions[i]);
115 }
116
117 status = lttng_event_rule_tracepoint_get_name_pattern_exclusion_count(tracepoint, &count);
118 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting name pattern exclusion count.");
119 ok(count == 3, "count is %d/3", count);
120
121 for (i = 0; i < count; i++) {
122 status = lttng_event_rule_tracepoint_get_name_pattern_exclusion_at_index(tracepoint, i, &tmp);
123 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting name pattern exclusion at index %d.", i);
124 ok(!strncmp(name_pattern_exclusions[i], tmp, strlen(name_pattern_exclusions[i])), "%s == %s.", tmp, name_pattern_exclusions[i]);
125 }
126 } else {
127 int i;
128
129 for (i = 0; i < 3; i++) {
130 status = lttng_event_rule_tracepoint_add_name_pattern_exclusion(tracepoint, name_pattern_exclusions[i]);
131 ok(status == LTTNG_EVENT_RULE_STATUS_UNSUPPORTED, "setting name pattern exclusions unsupported \"%s\".", name_pattern_exclusions[i]);
132 }
133
134 status = lttng_event_rule_tracepoint_get_name_pattern_exclusion_count(tracepoint, &count);
135 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting name pattern exclusion count.");
136 ok(count == 0, "count is %d/0", count);
137 }
138
139 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
140
141 {
142 struct lttng_payload_view view =
143 lttng_payload_view_from_payload(
144 &payload, 0, -1);
145
146 ok(lttng_event_rule_create_from_payload(
147 &view, &tracepoint_from_buffer) > 0,
148 "Deserializing.");
149 }
150
151 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer), "serialized and from buffer are equal.");
152
153 lttng_payload_reset(&payload);
154 lttng_event_rule_destroy(tracepoint);
155 lttng_event_rule_destroy(tracepoint_from_buffer);
156 lttng_log_level_rule_destroy(log_level_rule);
157 }
158
159 static
160 void test_event_rule_tracepoint(void)
161 {
162 int i;
163 struct lttng_event_rule *tracepoint = NULL;
164 struct tracepoint_test tests[] = {{LTTNG_DOMAIN_JUL, false},
165 {LTTNG_DOMAIN_KERNEL, false},
166 {LTTNG_DOMAIN_LOG4J, false},
167 {LTTNG_DOMAIN_PYTHON, false},
168 {LTTNG_DOMAIN_UST, true}};
169
170 diag("Testing lttng_event_rule_tracepoint.");
171 tracepoint = lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_NONE);
172 ok(!tracepoint, "Domain type restriction on create.");
173
174 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
175 test_event_rule_tracepoint_by_domain(&tests[i]);
176 }
177 }
178
179 static
180 void test_event_rule_kernel_tracepoint(void)
181 {
182 struct lttng_event_rule *tracepoint = NULL;
183 struct lttng_event_rule *tracepoint_from_buffer = NULL;
184 enum lttng_event_rule_status status;
185 const char *pattern="my_event_*";
186 const char *filter="msg_id == 23 && size >= 2048";
187 const char *tmp;
188 struct lttng_payload payload;
189
190 diag("Testing lttng_event_rule_kernel_tracepoint.");
191
192 lttng_payload_init(&payload);
193
194 tracepoint = lttng_event_rule_kernel_tracepoint_create();
195 ok(tracepoint, "tracepoint object.");
196
197 status = lttng_event_rule_kernel_tracepoint_set_name_pattern(tracepoint, pattern);
198 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
199 status = lttng_event_rule_kernel_tracepoint_get_name_pattern(tracepoint, &tmp);
200 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
201 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
202
203 status = lttng_event_rule_kernel_tracepoint_set_filter(tracepoint, filter);
204 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
205 status = lttng_event_rule_kernel_tracepoint_get_filter(tracepoint, &tmp);
206 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
207 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
208
209 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
210
211 {
212 struct lttng_payload_view view =
213 lttng_payload_view_from_payload(
214 &payload, 0, -1);
215
216 ok(lttng_event_rule_create_from_payload(
217 &view, &tracepoint_from_buffer) > 0,
218 "Deserializing.");
219 }
220
221 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer), "serialized and from buffer are equal.");
222
223 lttng_payload_reset(&payload);
224 lttng_event_rule_destroy(tracepoint);
225 lttng_event_rule_destroy(tracepoint_from_buffer);
226 }
227
228 static
229 void test_event_rule_user_tracepoint(void)
230 {
231 int i;
232 unsigned int count;
233 struct lttng_event_rule *tracepoint = NULL;
234 struct lttng_event_rule *tracepoint_from_buffer = NULL;
235 enum lttng_event_rule_status status;
236 const char *pattern="my_event_*";
237 const char *filter="msg_id == 23 && size >= 2048";
238 const char *tmp;
239 const char *name_pattern_exclusions[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"};
240 struct lttng_log_level_rule *log_level_rule = NULL;
241 const struct lttng_log_level_rule *log_level_rule_return = NULL;
242 struct lttng_payload payload;
243
244 diag("Testing lttng_event_rule_user_tracepoint.");
245
246 lttng_payload_init(&payload);
247
248 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
249 assert(log_level_rule);
250
251 tracepoint = lttng_event_rule_user_tracepoint_create();
252 ok(tracepoint, "user tracepoint object.");
253
254 status = lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint, pattern);
255 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
256 status = lttng_event_rule_user_tracepoint_get_name_pattern(tracepoint, &tmp);
257 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
258 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
259
260 status = lttng_event_rule_user_tracepoint_set_filter(tracepoint, filter);
261 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
262 status = lttng_event_rule_user_tracepoint_get_filter(tracepoint, &tmp);
263 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
264 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
265
266 status = lttng_event_rule_user_tracepoint_get_log_level_rule(tracepoint, &log_level_rule_return);
267 ok(status == LTTNG_EVENT_RULE_STATUS_UNSET, "get unset log level rule.");
268
269 status = lttng_event_rule_user_tracepoint_set_log_level_rule(
270 tracepoint, log_level_rule);
271 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting log level rule.");
272 status = lttng_event_rule_user_tracepoint_get_log_level_rule(
273 tracepoint, &log_level_rule_return);
274 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get log level rule.");
275
276 /* Name pattern exclusions */
277 for (i = 0; i < 3; i++) {
278 status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
279 tracepoint, name_pattern_exclusions[i]);
280 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
281 "setting name pattern exclusions \"%s\"",
282 name_pattern_exclusions[i]);
283 }
284
285 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
286 tracepoint, &count);
287 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
288 "getting name pattern exclusion count.");
289 ok(count == 3, "count is %d/3", count);
290
291 for (i = 0; i < count; i++) {
292 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
293 tracepoint, i, &tmp);
294 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
295 "getting name pattern exclusion at index %d.",
296 i);
297 ok(!strncmp(name_pattern_exclusions[i], tmp,
298 strlen(name_pattern_exclusions[i])),
299 "%s == %s.", tmp, name_pattern_exclusions[i]);
300 }
301
302 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
303
304 {
305 struct lttng_payload_view view =
306 lttng_payload_view_from_payload(
307 &payload, 0, -1);
308
309 ok(lttng_event_rule_create_from_payload(
310 &view, &tracepoint_from_buffer) > 0,
311 "Deserializing.");
312 }
313
314 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer), "serialized and from buffer are equal.");
315
316 lttng_payload_reset(&payload);
317 lttng_event_rule_destroy(tracepoint);
318 lttng_event_rule_destroy(tracepoint_from_buffer);
319 lttng_log_level_rule_destroy(log_level_rule);
320 }
321
322 static void test_event_rule_syscall(void)
323 {
324 struct lttng_event_rule *syscall = NULL;
325 struct lttng_event_rule *syscall_from_buffer = NULL;
326 enum lttng_event_rule_status status;
327 const char *pattern = "my_event_*";
328 const char *filter = "msg_id == 23 && size >= 2048";
329 const char *tmp;
330 struct lttng_payload payload;
331
332 diag("Event rule syscall.");
333
334 lttng_payload_init(&payload);
335
336 syscall = lttng_event_rule_kernel_syscall_create(LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY);
337 ok(syscall, "syscall object.");
338
339 status = lttng_event_rule_kernel_syscall_set_name_pattern(syscall, pattern);
340 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
341 status = lttng_event_rule_kernel_syscall_get_name_pattern(syscall, &tmp);
342 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
343 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
344
345 status = lttng_event_rule_kernel_syscall_set_filter(syscall, filter);
346 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
347 status = lttng_event_rule_kernel_syscall_get_filter(syscall, &tmp);
348 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
349 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
350
351 ok(lttng_event_rule_serialize(syscall, &payload) == 0, "Serializing.");
352
353 {
354 struct lttng_payload_view view =
355 lttng_payload_view_from_payload(
356 &payload, 0, -1);
357
358 ok(lttng_event_rule_create_from_payload(
359 &view, &syscall_from_buffer) > 0,
360 "Deserializing.");
361 }
362
363 ok(lttng_event_rule_is_equal(syscall, syscall_from_buffer),
364 "serialized and from buffer are equal.");
365
366 lttng_payload_reset(&payload);
367 lttng_event_rule_destroy(syscall);
368 lttng_event_rule_destroy(syscall_from_buffer);
369 }
370
371 static
372 void test_event_rule_jul_logging(void)
373 {
374 struct lttng_event_rule *jul_logging = NULL;
375 struct lttng_event_rule *jul_logging_from_buffer = NULL;
376 enum lttng_event_rule_status status;
377 const char *pattern="my_event_*";
378 const char *filter="msg_id == 23 && size >= 2048";
379 const char *tmp;
380 struct lttng_log_level_rule *log_level_rule = NULL;
381 const struct lttng_log_level_rule *log_level_rule_return = NULL;
382 struct lttng_payload payload;
383
384 diag("Testing lttng_event_rule_user_jul_logging.");
385
386 lttng_payload_init(&payload);
387
388 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
389 assert(log_level_rule);
390
391 jul_logging = lttng_event_rule_jul_logging_create();
392 ok(jul_logging, "jul_logging object.");
393
394 status = lttng_event_rule_jul_logging_set_name_pattern(jul_logging, pattern);
395 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
396 status = lttng_event_rule_jul_logging_get_name_pattern(jul_logging, &tmp);
397 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
398 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
399
400 status = lttng_event_rule_jul_logging_set_filter(jul_logging, filter);
401 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
402 status = lttng_event_rule_jul_logging_get_filter(jul_logging, &tmp);
403 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
404 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
405
406 status = lttng_event_rule_jul_logging_get_log_level_rule(jul_logging, &log_level_rule_return);
407 ok(status == LTTNG_EVENT_RULE_STATUS_UNSET, "get unset log level rule.");
408
409 status = lttng_event_rule_jul_logging_set_log_level_rule(
410 jul_logging, log_level_rule);
411 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting log level rule.");
412 status = lttng_event_rule_jul_logging_get_log_level_rule(
413 jul_logging, &log_level_rule_return);
414 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get log level rule.");
415
416 ok(lttng_event_rule_serialize(jul_logging, &payload) == 0, "Serializing.");
417
418 {
419 struct lttng_payload_view view =
420 lttng_payload_view_from_payload(
421 &payload, 0, -1);
422
423 ok(lttng_event_rule_create_from_payload(
424 &view, &jul_logging_from_buffer) > 0,
425 "Deserializing.");
426 }
427
428 ok(lttng_event_rule_is_equal(jul_logging, jul_logging_from_buffer), "serialized and from buffer are equal.");
429
430 lttng_payload_reset(&payload);
431 lttng_event_rule_destroy(jul_logging);
432 lttng_event_rule_destroy(jul_logging_from_buffer);
433 lttng_log_level_rule_destroy(log_level_rule);
434 }
435
436 static
437 void test_event_rule_log4j_logging(void)
438 {
439 struct lttng_event_rule *log4j_logging = NULL;
440 struct lttng_event_rule *log4j_logging_from_buffer = NULL;
441 enum lttng_event_rule_status status;
442 const char *pattern="my_event_*";
443 const char *filter="msg_id == 23 && size >= 2048";
444 const char *tmp;
445 struct lttng_log_level_rule *log_level_rule = NULL;
446 const struct lttng_log_level_rule *log_level_rule_return = NULL;
447 struct lttng_payload payload;
448
449 diag("Testing lttng_event_rule_user_log4j_logging.");
450
451 lttng_payload_init(&payload);
452
453 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
454 assert(log_level_rule);
455
456 log4j_logging = lttng_event_rule_log4j_logging_create();
457 ok(log4j_logging, "log4j_logging object.");
458
459 status = lttng_event_rule_log4j_logging_set_name_pattern(log4j_logging, pattern);
460 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
461 status = lttng_event_rule_log4j_logging_get_name_pattern(log4j_logging, &tmp);
462 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
463 ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
464
465 status = lttng_event_rule_log4j_logging_set_filter(log4j_logging, filter);
466 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
467 status = lttng_event_rule_log4j_logging_get_filter(log4j_logging, &tmp);
468 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
469 ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
470
471 status = lttng_event_rule_log4j_logging_get_log_level_rule(log4j_logging, &log_level_rule_return);
472 ok(status == LTTNG_EVENT_RULE_STATUS_UNSET, "get unset log level rule.");
473
474 status = lttng_event_rule_log4j_logging_set_log_level_rule(
475 log4j_logging, log_level_rule);
476 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting log level rule.");
477 status = lttng_event_rule_log4j_logging_get_log_level_rule(
478 log4j_logging, &log_level_rule_return);
479 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get log level rule.");
480
481 ok(lttng_event_rule_serialize(log4j_logging, &payload) == 0, "Serializing.");
482
483 {
484 struct lttng_payload_view view =
485 lttng_payload_view_from_payload(
486 &payload, 0, -1);
487
488 ok(lttng_event_rule_create_from_payload(
489 &view, &log4j_logging_from_buffer) > 0,
490 "Deserializing.");
491 }
492
493 ok(lttng_event_rule_is_equal(log4j_logging, log4j_logging_from_buffer), "serialized and from buffer are equal.");
494
495 lttng_payload_reset(&payload);
496 lttng_event_rule_destroy(log4j_logging);
497 lttng_event_rule_destroy(log4j_logging_from_buffer);
498 lttng_log_level_rule_destroy(log_level_rule);
499 }
500
501 static void test_event_rule_userspace_probe(void)
502 {
503 struct lttng_event_rule *uprobe = NULL;
504 struct lttng_event_rule *uprobe_from_buffer = NULL;
505 struct lttng_userspace_probe_location_lookup_method *lookup_method =
506 NULL;
507 struct lttng_userspace_probe_location *probe_location = NULL;
508 const struct lttng_userspace_probe_location *probe_location_tmp = NULL;
509 enum lttng_event_rule_status status;
510
511 const char *probe_name = "my_probe.";
512 const char *tmp;
513 struct lttng_payload payload;
514
515 diag("Event rule uprobe.");
516
517 lookup_method = lttng_userspace_probe_location_lookup_method_function_elf_create();
518 if (!lookup_method) {
519 fail("Setup error on userspace probe lookup method creation.");
520 goto end;
521 }
522
523 probe_location = lttng_userspace_probe_location_function_create(
524 "/proc/self/exe",
525 "lttng_userspace_probe_location_tracepoint_create",
526 lookup_method);
527 if (!probe_location) {
528 fail("Setup error on userspace probe location creation.");
529 goto end;
530 }
531
532 /* Ownership transferred to the probe location function object. */
533 lookup_method = NULL;
534
535 lttng_payload_init(&payload);
536
537 uprobe = lttng_event_rule_kernel_uprobe_create(probe_location);
538 ok(uprobe, "uprobe event rule object creation.");
539
540 status = lttng_event_rule_kernel_uprobe_get_location(
541 uprobe, &probe_location_tmp);
542 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
543 "Getting uprobe event rule location.");
544 ok(lttng_userspace_probe_location_is_equal(
545 probe_location, probe_location_tmp),
546 "Location is equal.");
547
548 status = lttng_event_rule_kernel_uprobe_set_event_name(uprobe, probe_name);
549 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
550 "Setting uprobe event rule name: %s.", probe_name);
551 status = lttng_event_rule_kernel_uprobe_get_event_name(uprobe, &tmp);
552 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting uprobe name.");
553 ok(!strcmp(probe_name, tmp), "Uprobe name are equal.");
554
555 ok(lttng_event_rule_serialize(uprobe, &payload) == 0, "Serializing.");
556
557 {
558 struct lttng_payload_view view =
559 lttng_payload_view_from_payload(
560 &payload, 0, -1);
561
562 ok(lttng_event_rule_create_from_payload(
563 &view, &uprobe_from_buffer) > 0,
564 "Deserializing.");
565 }
566
567 ok(lttng_event_rule_is_equal(uprobe, uprobe_from_buffer),
568 "serialized and from buffer are equal.");
569
570 end:
571 lttng_payload_reset(&payload);
572 lttng_event_rule_destroy(uprobe);
573 lttng_event_rule_destroy(uprobe_from_buffer);
574 lttng_userspace_probe_location_destroy(probe_location);
575 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
576 }
577
578 static void test_event_rule_kernel_probe_by_location(
579 const struct lttng_kernel_probe_location *location)
580 {
581 struct lttng_event_rule *kprobe = NULL;
582 struct lttng_event_rule *kprobe_from_buffer = NULL;
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,
600 "Getting kprobe event rule location.");
601 ok(lttng_kernel_probe_location_is_equal(location, _location), "Locations are equal.");
602
603 status = lttng_event_rule_kernel_kprobe_set_event_name(kprobe, probe_name);
604 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
605 "Setting kprobe event rule name: %s.", probe_name);
606 status = lttng_event_rule_kernel_kprobe_get_event_name(kprobe, &tmp);
607 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting kprobe name.");
608 ok(!strcmp(probe_name, tmp), "kprobe name are equal.");
609
610 ok(lttng_event_rule_serialize(kprobe, &payload) == 0, "Serializing.");
611
612 {
613 struct lttng_payload_view view =
614 lttng_payload_view_from_payload(
615 &payload, 0, -1);
616
617 ok(lttng_event_rule_create_from_payload(
618 &view, &kprobe_from_buffer) > 0,
619 "Deserializing.");
620 }
621
622 ok(lttng_event_rule_is_equal(kprobe, kprobe_from_buffer),
623 "serialized and from buffer are equal.");
624
625 lttng_payload_reset(&payload);
626 lttng_event_rule_destroy(kprobe);
627 lttng_event_rule_destroy(kprobe_from_buffer);
628 }
629
630 static void test_event_rule_kernel_probe(void)
631 {
632 struct lttng_kernel_probe_location *address_location = NULL;
633 struct lttng_kernel_probe_location *symbol_location = NULL;
634
635 address_location = lttng_kernel_probe_location_address_create(50);
636 symbol_location = lttng_kernel_probe_location_symbol_create("une_bonne", 50);
637 assert(address_location);
638 assert(symbol_location);
639
640 test_event_rule_kernel_probe_by_location(address_location);
641 test_event_rule_kernel_probe_by_location(symbol_location);
642
643 lttng_kernel_probe_location_destroy(address_location);
644 lttng_kernel_probe_location_destroy(symbol_location);
645 }
646
647 static void test_set_event_rule_log_level_rules(
648 struct lttng_event_rule *event_rule,
649 int log_level,
650 enum lttng_event_rule_status *exactly_status,
651 enum lttng_event_rule_status *as_severe_status)
652 {
653 struct lttng_log_level_rule *log_level_rule;
654
655 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(
656 log_level);
657 assert(log_level_rule);
658
659 *as_severe_status = lttng_event_rule_tracepoint_set_log_level_rule(
660 event_rule, log_level_rule);
661 lttng_log_level_rule_destroy(log_level_rule);
662
663 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
664 assert(log_level_rule);
665
666 *exactly_status = lttng_event_rule_tracepoint_set_log_level_rule(
667 event_rule, log_level_rule);
668 lttng_log_level_rule_destroy(log_level_rule);
669 }
670
671 static void test_event_rule_log_level_generic(const char *domain_name,
672 enum lttng_domain_type domain,
673 log_level_name_getter get_log_level_name,
674 const int tagged_log_level_values[],
675 size_t tagged_log_level_values_count,
676 const int valid_log_level_values[],
677 size_t valid_log_level_values_count,
678 const int invalid_log_level_values[],
679 size_t invalid_log_level_values_count)
680 {
681 size_t i;
682 struct lttng_event_rule *tracepoint_rule;
683 enum lttng_event_rule_status er_exactly_status, er_as_severe_status;
684
685 diag("Test %s event rule + log level rule", domain_name);
686 tracepoint_rule = lttng_event_rule_tracepoint_create(domain);
687 assert(tracepoint_rule);
688
689 for (i = 0; i < tagged_log_level_values_count; i++) {
690 const int tagged_log_level_value = tagged_log_level_values[i];
691
692 test_set_event_rule_log_level_rules(tracepoint_rule,
693 tagged_log_level_value,
694 &er_exactly_status, &er_as_severe_status);
695 ok(er_exactly_status == LTTNG_EVENT_RULE_STATUS_OK,
696 "Log level rule \"exactly\" accepted by %s tracepoint event rule: level = %s",
697 domain_name,
698 get_log_level_name(
699 tagged_log_level_value));
700 ok(er_as_severe_status == LTTNG_EVENT_RULE_STATUS_OK,
701 "Log level rule \"as least as severe as\" accepted by %s tracepoint event rule: level = %s",
702 domain_name,
703 get_log_level_name(
704 tagged_log_level_value));
705 }
706
707 for (i = 0; i < valid_log_level_values_count; i++) {
708 const int valid_log_level_value = valid_log_level_values[i];
709
710 test_set_event_rule_log_level_rules(tracepoint_rule,
711 valid_log_level_value,
712 &er_exactly_status, &er_as_severe_status);
713 ok(er_exactly_status == LTTNG_EVENT_RULE_STATUS_OK,
714 "Log level rule \"exactly\" accepted by %s tracepoint event rule: level = %d",
715 domain_name,
716 valid_log_level_value);
717 ok(er_as_severe_status == LTTNG_EVENT_RULE_STATUS_OK,
718 "Log level rule \"as least as severe as\" accepted by %s tracepoint event rule: level = %d",
719 domain_name,
720 valid_log_level_value);
721 }
722
723 for (i = 0; i < invalid_log_level_values_count; i++) {
724 const int invalid_log_level_value = invalid_log_level_values[i];
725
726 test_set_event_rule_log_level_rules(tracepoint_rule,
727 invalid_log_level_value,
728 &er_exactly_status, &er_as_severe_status);
729 ok(er_exactly_status == LTTNG_EVENT_RULE_STATUS_INVALID,
730 "Log level rule \"exactly\" rejected by %s tracepoint event rule: level = %d",
731 domain_name,
732 invalid_log_level_value);
733 ok(er_as_severe_status == LTTNG_EVENT_RULE_STATUS_INVALID,
734 "Log level rule \"as least as severe as\" rejected by %s tracepoint event rule: level = %d",
735 domain_name,
736 invalid_log_level_value);
737 }
738
739 lttng_event_rule_destroy(tracepoint_rule);
740 }
741
742 static void test_event_rule_log_level_ust(void)
743 {
744 const int tagged_log_level_values[] = {
745 LTTNG_LOGLEVEL_EMERG,
746 LTTNG_LOGLEVEL_ALERT,
747 LTTNG_LOGLEVEL_CRIT,
748 LTTNG_LOGLEVEL_ERR,
749 LTTNG_LOGLEVEL_WARNING,
750 LTTNG_LOGLEVEL_NOTICE,
751 LTTNG_LOGLEVEL_INFO,
752 LTTNG_LOGLEVEL_DEBUG_SYSTEM,
753 LTTNG_LOGLEVEL_DEBUG_PROGRAM,
754 LTTNG_LOGLEVEL_DEBUG_PROCESS,
755 LTTNG_LOGLEVEL_DEBUG_MODULE,
756 LTTNG_LOGLEVEL_DEBUG_UNIT,
757 LTTNG_LOGLEVEL_DEBUG_FUNCTION,
758 LTTNG_LOGLEVEL_DEBUG_LINE,
759 LTTNG_LOGLEVEL_DEBUG,
760 };
761 const int invalid_log_level_values[] = {
762 -1980,
763 1995,
764 LTTNG_LOGLEVEL_DEBUG + 1,
765 LTTNG_LOGLEVEL_EMERG - 1,
766 };
767
768 test_event_rule_log_level_generic("user space", LTTNG_DOMAIN_UST,
769 loglevel_value_to_name, tagged_log_level_values,
770 ARRAY_SIZE(tagged_log_level_values),
771 NULL, 0,
772 invalid_log_level_values,
773 ARRAY_SIZE(invalid_log_level_values));
774 }
775
776 static void test_event_rule_log_level_jul(void)
777 {
778 const int tagged_log_level_values[] = {
779 LTTNG_LOGLEVEL_JUL_OFF,
780 LTTNG_LOGLEVEL_JUL_SEVERE,
781 LTTNG_LOGLEVEL_JUL_WARNING,
782 LTTNG_LOGLEVEL_JUL_INFO,
783 LTTNG_LOGLEVEL_JUL_CONFIG,
784 LTTNG_LOGLEVEL_JUL_FINE,
785 LTTNG_LOGLEVEL_JUL_FINER,
786 LTTNG_LOGLEVEL_JUL_FINEST,
787 LTTNG_LOGLEVEL_JUL_ALL,
788 };
789 const int valid_log_level_values[] = {
790 0,
791 -1980,
792 1995
793 };
794
795 test_event_rule_log_level_generic("Java Util Logging", LTTNG_DOMAIN_JUL,
796 loglevel_jul_value_to_name, tagged_log_level_values,
797 ARRAY_SIZE(tagged_log_level_values),
798 valid_log_level_values,
799 ARRAY_SIZE(valid_log_level_values), NULL, 0);
800 }
801
802 static void test_event_rule_log_level_log4j(void)
803 {
804 const int tagged_log_level_values[] = {
805 LTTNG_LOGLEVEL_LOG4J_OFF,
806 LTTNG_LOGLEVEL_LOG4J_FATAL,
807 LTTNG_LOGLEVEL_LOG4J_ERROR,
808 LTTNG_LOGLEVEL_LOG4J_WARN,
809 LTTNG_LOGLEVEL_LOG4J_INFO,
810 LTTNG_LOGLEVEL_LOG4J_DEBUG,
811 LTTNG_LOGLEVEL_LOG4J_TRACE,
812 LTTNG_LOGLEVEL_LOG4J_ALL,
813 };
814 const int valid_log_level_values[] = {
815 0
816 -1980,
817 1995
818 };
819
820 test_event_rule_log_level_generic("Log4j", LTTNG_DOMAIN_LOG4J,
821 loglevel_log4j_value_to_name, tagged_log_level_values,
822 ARRAY_SIZE(tagged_log_level_values),
823 valid_log_level_values,
824 ARRAY_SIZE(valid_log_level_values), NULL, 0);
825 }
826
827 static void test_event_rule_log_level_python(void)
828 {
829 const int tagged_log_level_values[] = {
830 LTTNG_LOGLEVEL_PYTHON_CRITICAL,
831 LTTNG_LOGLEVEL_PYTHON_ERROR,
832 LTTNG_LOGLEVEL_PYTHON_WARNING,
833 LTTNG_LOGLEVEL_PYTHON_INFO,
834 LTTNG_LOGLEVEL_PYTHON_DEBUG,
835 LTTNG_LOGLEVEL_PYTHON_NOTSET,
836 };
837 const int valid_log_level_values[] = {
838 45,
839 35,
840 0,
841 -657,
842 };
843
844 test_event_rule_log_level_generic("Python", LTTNG_DOMAIN_PYTHON,
845 loglevel_python_value_to_name, tagged_log_level_values,
846 ARRAY_SIZE(tagged_log_level_values),
847 valid_log_level_values,
848 ARRAY_SIZE(valid_log_level_values),
849 NULL, 0);
850 }
851
852 int main(int argc, const char *argv[])
853 {
854 plan_tests(NUM_TESTS);
855 test_event_rule_tracepoint();
856 test_event_rule_kernel_tracepoint();
857 test_event_rule_user_tracepoint();
858 test_event_rule_syscall();
859 test_event_rule_userspace_probe();
860 test_event_rule_kernel_probe();
861 test_event_rule_log4j_logging();
862 test_event_rule_jul_logging();
863 test_event_rule_log_level_ust();
864 test_event_rule_log_level_jul();
865 test_event_rule_log_level_log4j();
866 test_event_rule_log_level_python();
867 return exit_status();
868 }
This page took 0.065514 seconds and 5 git commands to generate.