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
18 #include <common/payload-view.h>
19 #include <common/payload.h>
20 #include <lttng/domain.h>
21 #include <lttng/event-rule/kernel-probe-internal.h>
22 #include <lttng/event-rule/kernel-probe.h>
23 #include <lttng/event-rule/syscall-internal.h>
24 #include <lttng/event-rule/syscall.h>
25 #include <lttng/event-rule/tracepoint-internal.h>
26 #include <lttng/event-rule/tracepoint.h>
27 #include <lttng/event-rule/userspace-probe-internal.h>
28 #include <lttng/event-rule/userspace-probe.h>
29 #include <lttng/event.h>
30 #include <lttng/kernel-probe-internal.h>
31 #include <lttng/kernel-probe.h>
32 #include <lttng/userspace-probe-internal.h>
33 #include <lttng/userspace-probe.h>
36 int lttng_opt_quiet
= 1;
37 int lttng_opt_verbose
;
42 struct tracepoint_test
{
43 enum lttng_domain_type type
;
44 bool support_exclusion
;
48 void test_event_rule_tracepoint_by_domain(const struct tracepoint_test
*test
)
51 struct lttng_event_rule
*tracepoint
= NULL
;
52 struct lttng_event_rule
*tracepoint_from_buffer
= NULL
;
53 enum lttng_event_rule_status status
;
54 enum lttng_domain_type domain_type
, type
;
55 const char *pattern
="my_event_*";
56 const char *filter
="msg_id == 23 && size >= 2048";
58 const char *exclusions
[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"};
59 struct lttng_log_level_rule
*log_level_rule
= NULL
;
60 const struct lttng_log_level_rule
*log_level_rule_return
= NULL
;
61 struct lttng_payload payload
;
64 diag("Testing domain %d.", type
);
66 lttng_payload_init(&payload
);
68 log_level_rule
= lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO
);
69 assert(log_level_rule
);
71 tracepoint
= lttng_event_rule_tracepoint_create(type
);
72 ok(tracepoint
, "tracepoint object.");
74 status
= lttng_event_rule_tracepoint_get_domain_type(tracepoint
, &domain_type
);
75 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get tracepoint domain.");
76 ok(domain_type
== type
, "domain type got %d expected %d.", domain_type
, type
);
78 status
= lttng_event_rule_tracepoint_set_pattern(tracepoint
, pattern
);
79 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
80 status
= lttng_event_rule_tracepoint_get_pattern(tracepoint
, &tmp
);
81 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
82 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
84 status
= lttng_event_rule_tracepoint_set_filter(tracepoint
, filter
);
85 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
86 status
= lttng_event_rule_tracepoint_get_filter(tracepoint
, &tmp
);
87 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
88 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
90 status
= lttng_event_rule_tracepoint_get_log_level_rule(tracepoint
, &log_level_rule_return
);
91 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset log level rule.");
93 status
= lttng_event_rule_tracepoint_set_log_level_rule(tracepoint
, log_level_rule
);
94 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting log level rule.");
95 status
= lttng_event_rule_tracepoint_get_log_level_rule(tracepoint
, &log_level_rule_return
);
96 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get log level rule.");
98 if (test
->support_exclusion
) {
101 for (i
= 0; i
< 3; i
++) {
102 status
= lttng_event_rule_tracepoint_add_exclusion(tracepoint
, exclusions
[i
]);
103 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting exclusions \"%s\"", exclusions
[i
]);
106 status
= lttng_event_rule_tracepoint_get_exclusions_count(tracepoint
, &count
);
107 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion count.");
108 ok(count
== 3, "count is %d/3", count
);
110 for (i
= 0; i
< count
; i
++) {
111 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(tracepoint
, i
, &tmp
);
112 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion at index %d.", i
);
113 ok(!strncmp(exclusions
[i
], tmp
, strlen(exclusions
[i
])), "%s == %s.", tmp
, exclusions
[i
]);
118 for (i
= 0; i
< 3; i
++) {
119 status
= lttng_event_rule_tracepoint_add_exclusion(tracepoint
, exclusions
[i
]);
120 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSUPPORTED
, "setting exclusions unsupported \"%s\".", exclusions
[i
]);
123 status
= lttng_event_rule_tracepoint_get_exclusions_count(tracepoint
, &count
);
124 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion count.");
125 ok(count
== 0, "count is %d/0", count
);
128 ok(lttng_event_rule_serialize(tracepoint
, &payload
) == 0, "Serializing.");
131 struct lttng_payload_view view
=
132 lttng_payload_view_from_payload(
135 ok(lttng_event_rule_create_from_payload(
136 &view
, &tracepoint_from_buffer
) > 0,
140 ok(lttng_event_rule_is_equal(tracepoint
, tracepoint_from_buffer
), "serialized and from buffer are equal.");
142 lttng_payload_reset(&payload
);
143 lttng_event_rule_destroy(tracepoint
);
144 lttng_event_rule_destroy(tracepoint_from_buffer
);
145 lttng_log_level_rule_destroy(log_level_rule
);
149 void test_event_rule_tracepoint(void)
152 struct lttng_event_rule
*tracepoint
= NULL
;
153 struct tracepoint_test tests
[] = {{LTTNG_DOMAIN_JUL
, false},
154 {LTTNG_DOMAIN_KERNEL
, false},
155 {LTTNG_DOMAIN_LOG4J
, false},
156 {LTTNG_DOMAIN_PYTHON
, false},
157 {LTTNG_DOMAIN_UST
, true}};
159 diag("Testing lttng_event_rule_tracepoint.");
160 tracepoint
= lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_NONE
);
161 ok(!tracepoint
, "Domain type restriction on create.");
163 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++) {
164 test_event_rule_tracepoint_by_domain(&tests
[i
]);
168 static void test_event_rule_syscall(void)
170 struct lttng_event_rule
*syscall
= NULL
;
171 struct lttng_event_rule
*syscall_from_buffer
= NULL
;
172 enum lttng_event_rule_status status
;
173 const char *pattern
= "my_event_*";
174 const char *filter
= "msg_id == 23 && size >= 2048";
176 struct lttng_payload payload
;
178 diag("Event rule syscall.");
180 lttng_payload_init(&payload
);
182 syscall
= lttng_event_rule_syscall_create();
183 ok(syscall
, "syscall object.");
185 status
= lttng_event_rule_syscall_set_pattern(syscall
, pattern
);
186 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
187 status
= lttng_event_rule_syscall_get_pattern(syscall
, &tmp
);
188 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
189 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
191 status
= lttng_event_rule_syscall_set_filter(syscall
, filter
);
192 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
193 status
= lttng_event_rule_syscall_get_filter(syscall
, &tmp
);
194 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
195 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
197 ok(lttng_event_rule_serialize(syscall
, &payload
) == 0, "Serializing.");
200 struct lttng_payload_view view
=
201 lttng_payload_view_from_payload(
204 ok(lttng_event_rule_create_from_payload(
205 &view
, &syscall_from_buffer
) > 0,
209 ok(lttng_event_rule_is_equal(syscall
, syscall_from_buffer
),
210 "serialized and from buffer are equal.");
212 lttng_payload_reset(&payload
);
213 lttng_event_rule_destroy(syscall
);
214 lttng_event_rule_destroy(syscall_from_buffer
);
217 static void test_event_rule_userspace_probe(void)
219 struct lttng_event_rule
*uprobe
= NULL
;
220 struct lttng_event_rule
*uprobe_from_buffer
= NULL
;
221 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
223 struct lttng_userspace_probe_location
*probe_location
= NULL
;
224 const struct lttng_userspace_probe_location
*probe_location_tmp
= NULL
;
225 enum lttng_event_rule_status status
;
227 const char *probe_name
= "my_probe.";
229 struct lttng_payload payload
;
231 diag("Event rule uprobe.");
233 lookup_method
= lttng_userspace_probe_location_lookup_method_function_elf_create();
234 if (!lookup_method
) {
235 fail("Setup error on userspace probe lookup method creation.");
239 probe_location
= lttng_userspace_probe_location_function_create(
241 "lttng_userspace_probe_location_tracepoint_create",
243 if (!probe_location
) {
244 fail("Setup error on userspace probe location creation.");
248 /* Ownership transferred to the probe location function object. */
249 lookup_method
= NULL
;
251 lttng_payload_init(&payload
);
253 uprobe
= lttng_event_rule_userspace_probe_create(probe_location
);
254 ok(uprobe
, "uprobe event rule object creation.");
256 status
= lttng_event_rule_userspace_probe_get_location(
257 uprobe
, &probe_location_tmp
);
258 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
259 "Getting uprobe event rule location.");
260 ok(lttng_userspace_probe_location_is_equal(
261 probe_location
, probe_location_tmp
),
262 "Location is equal.");
264 status
= lttng_event_rule_userspace_probe_set_event_name(uprobe
, probe_name
);
265 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
266 "Setting uprobe event rule name: %s.", probe_name
);
267 status
= lttng_event_rule_userspace_probe_get_event_name(uprobe
, &tmp
);
268 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting uprobe name.");
269 ok(!strcmp(probe_name
, tmp
), "Uprobe name are equal.");
271 ok(lttng_event_rule_serialize(uprobe
, &payload
) == 0, "Serializing.");
274 struct lttng_payload_view view
=
275 lttng_payload_view_from_payload(
278 ok(lttng_event_rule_create_from_payload(
279 &view
, &uprobe_from_buffer
) > 0,
283 ok(lttng_event_rule_is_equal(uprobe
, uprobe_from_buffer
),
284 "serialized and from buffer are equal.");
287 lttng_payload_reset(&payload
);
288 lttng_event_rule_destroy(uprobe
);
289 lttng_event_rule_destroy(uprobe_from_buffer
);
290 lttng_userspace_probe_location_destroy(probe_location
);
291 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
294 static void test_event_rule_kernel_probe_by_location(
295 const struct lttng_kernel_probe_location
*location
)
297 struct lttng_event_rule
*kprobe
= NULL
;
298 struct lttng_event_rule
*kprobe_from_buffer
= NULL
;
299 enum lttng_event_rule_status status
;
300 const struct lttng_kernel_probe_location
*_location
;
302 const char *probe_name
= "my_probe";
304 struct lttng_payload payload
;
306 diag("Event rule kprobe for location type %d.",
307 lttng_kernel_probe_location_get_type(location
));
309 lttng_payload_init(&payload
);
311 kprobe
= lttng_event_rule_kernel_probe_create(location
);
312 ok(kprobe
, "kprobe event rule object creation.");
314 status
= lttng_event_rule_kernel_probe_get_location(kprobe
, &_location
);
315 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
316 "Getting kprobe event rule location.");
317 ok(lttng_kernel_probe_location_is_equal(location
, _location
), "Locations are equal.");
319 status
= lttng_event_rule_kernel_probe_set_event_name(kprobe
, probe_name
);
320 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
321 "Setting kprobe event rule name: %s.", probe_name
);
322 status
= lttng_event_rule_kernel_probe_get_event_name(kprobe
, &tmp
);
323 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting kprobe name.");
324 ok(!strcmp(probe_name
, tmp
), "kprobe name are equal.");
326 ok(lttng_event_rule_serialize(kprobe
, &payload
) == 0, "Serializing.");
329 struct lttng_payload_view view
=
330 lttng_payload_view_from_payload(
333 ok(lttng_event_rule_create_from_payload(
334 &view
, &kprobe_from_buffer
) > 0,
338 ok(lttng_event_rule_is_equal(kprobe
, kprobe_from_buffer
),
339 "serialized and from buffer are equal.");
341 lttng_payload_reset(&payload
);
342 lttng_event_rule_destroy(kprobe
);
343 lttng_event_rule_destroy(kprobe_from_buffer
);
346 static void test_event_rule_kernel_probe(void)
348 struct lttng_kernel_probe_location
*address_location
= NULL
;
349 struct lttng_kernel_probe_location
*symbol_location
= NULL
;
351 address_location
= lttng_kernel_probe_location_address_create(50);
352 symbol_location
= lttng_kernel_probe_location_symbol_create("une_bonne", 50);
353 assert(address_location
);
354 assert(symbol_location
);
356 test_event_rule_kernel_probe_by_location(address_location
);
357 test_event_rule_kernel_probe_by_location(symbol_location
);
359 lttng_kernel_probe_location_destroy(address_location
);
360 lttng_kernel_probe_location_destroy(symbol_location
);
363 int main(int argc
, const char *argv
[])
365 plan_tests(NUM_TESTS
);
366 test_event_rule_tracepoint();
367 test_event_rule_syscall();
368 test_event_rule_userspace_probe();
369 test_event_rule_kernel_probe();
370 return exit_status();