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
)
52 struct lttng_event_rule
*tracepoint
= NULL
;
53 struct lttng_event_rule
*tracepoint_from_buffer
= NULL
;
54 enum lttng_event_rule_status status
;
55 enum lttng_domain_type domain_type
, type
;
56 enum lttng_loglevel_type log_level_type
;
57 const char *pattern
="my_event_*";
58 const char *filter
="msg_id == 23 && size >= 2048";
60 const char *exclusions
[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"};
61 struct lttng_payload payload
;
64 diag("Testing domain %d.", type
);
66 lttng_payload_init(&payload
);
68 tracepoint
= lttng_event_rule_tracepoint_create(type
);
69 ok(tracepoint
, "tracepoint object.");
71 status
= lttng_event_rule_tracepoint_get_domain_type(tracepoint
, &domain_type
);
72 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get tracepoint domain.");
73 ok(domain_type
== type
, "domain type got %d expected %d.", domain_type
, type
);
75 status
= lttng_event_rule_tracepoint_set_pattern(tracepoint
, pattern
);
76 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
77 status
= lttng_event_rule_tracepoint_get_pattern(tracepoint
, &tmp
);
78 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
79 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
81 status
= lttng_event_rule_tracepoint_set_filter(tracepoint
, filter
);
82 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
83 status
= lttng_event_rule_tracepoint_get_filter(tracepoint
, &tmp
);
84 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
85 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
87 status
= lttng_event_rule_tracepoint_set_log_level_all(tracepoint
);
88 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting all log level.");
89 status
= lttng_event_rule_tracepoint_get_log_level_type(tracepoint
, &log_level_type
);
90 ok(log_level_type
== LTTNG_EVENT_LOGLEVEL_ALL
, "getting loglevel type all.");
91 status
= lttng_event_rule_tracepoint_get_log_level(tracepoint
, &ret
);
92 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSET
, "get unset loglevel value.");
94 status
= lttng_event_rule_tracepoint_set_log_level(tracepoint
, LTTNG_LOGLEVEL_INFO
);
95 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting single loglevel.");
96 status
= lttng_event_rule_tracepoint_get_log_level_type(tracepoint
, &log_level_type
);
97 ok(log_level_type
== LTTNG_EVENT_LOGLEVEL_SINGLE
, "getting loglevel type single.");
98 status
= lttng_event_rule_tracepoint_get_log_level(tracepoint
, &ret
);
99 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get loglevel value.");
100 ok(ret
== LTTNG_LOGLEVEL_INFO
, "loglevel value is equal.");
102 status
= lttng_event_rule_tracepoint_set_log_level_range_lower_bound(tracepoint
, LTTNG_LOGLEVEL_WARNING
);
103 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting range loglevel.");
104 status
= lttng_event_rule_tracepoint_get_log_level_type(tracepoint
, &log_level_type
);
105 ok(log_level_type
== LTTNG_EVENT_LOGLEVEL_RANGE
, "getting loglevel type range.");
106 status
= lttng_event_rule_tracepoint_get_log_level(tracepoint
, &ret
);
107 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "get loglevel value.");
108 ok(ret
== LTTNG_LOGLEVEL_WARNING
, "loglevel valuei is equal.");
110 if (test
->support_exclusion
) {
113 for (i
= 0; i
< 3; i
++) {
114 status
= lttng_event_rule_tracepoint_add_exclusion(tracepoint
, exclusions
[i
]);
115 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting exclusions \"%s\"", exclusions
[i
]);
118 status
= lttng_event_rule_tracepoint_get_exclusions_count(tracepoint
, &count
);
119 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion count.");
120 ok(count
== 3, "count is %d/3", count
);
122 for (i
= 0; i
< count
; i
++) {
123 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(tracepoint
, i
, &tmp
);
124 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion at index %d.", i
);
125 ok(!strncmp(exclusions
[i
], tmp
, strlen(exclusions
[i
])), "%s == %s.", tmp
, exclusions
[i
]);
130 for (i
= 0; i
< 3; i
++) {
131 status
= lttng_event_rule_tracepoint_add_exclusion(tracepoint
, exclusions
[i
]);
132 ok(status
== LTTNG_EVENT_RULE_STATUS_UNSUPPORTED
, "setting exclusions unsupported \"%s\".", exclusions
[i
]);
135 status
= lttng_event_rule_tracepoint_get_exclusions_count(tracepoint
, &count
);
136 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting exclusion count.");
137 ok(count
== 0, "count is %d/0", count
);
140 ok(lttng_event_rule_serialize(tracepoint
, &payload
) == 0, "Serializing.");
143 struct lttng_payload_view view
=
144 lttng_payload_view_from_payload(
147 ok(lttng_event_rule_create_from_payload(
148 &view
, &tracepoint_from_buffer
) > 0,
152 ok(lttng_event_rule_is_equal(tracepoint
, tracepoint_from_buffer
), "serialized and from buffer are equal.");
154 lttng_payload_reset(&payload
);
155 lttng_event_rule_destroy(tracepoint
);
156 lttng_event_rule_destroy(tracepoint_from_buffer
);
160 void test_event_rule_tracepoint(void)
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}};
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.");
174 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++) {
175 test_event_rule_tracepoint_by_domain(&tests
[i
]);
179 static void test_event_rule_syscall(void)
181 struct lttng_event_rule
*syscall
= NULL
;
182 struct lttng_event_rule
*syscall_from_buffer
= NULL
;
183 enum lttng_event_rule_status status
;
184 const char *pattern
= "my_event_*";
185 const char *filter
= "msg_id == 23 && size >= 2048";
187 struct lttng_payload payload
;
189 diag("Event rule syscall.");
191 lttng_payload_init(&payload
);
193 syscall
= lttng_event_rule_syscall_create();
194 ok(syscall
, "syscall object.");
196 status
= lttng_event_rule_syscall_set_pattern(syscall
, pattern
);
197 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting pattern.");
198 status
= lttng_event_rule_syscall_get_pattern(syscall
, &tmp
);
199 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting pattern.");
200 ok(!strncmp(pattern
, tmp
, strlen(pattern
)), "pattern is equal.");
202 status
= lttng_event_rule_syscall_set_filter(syscall
, filter
);
203 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "setting filter.");
204 status
= lttng_event_rule_syscall_get_filter(syscall
, &tmp
);
205 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "getting filter.");
206 ok(!strncmp(filter
, tmp
, strlen(filter
)), "filter is equal.");
208 ok(lttng_event_rule_serialize(syscall
, &payload
) == 0, "Serializing.");
211 struct lttng_payload_view view
=
212 lttng_payload_view_from_payload(
215 ok(lttng_event_rule_create_from_payload(
216 &view
, &syscall_from_buffer
) > 0,
220 ok(lttng_event_rule_is_equal(syscall
, syscall_from_buffer
),
221 "serialized and from buffer are equal.");
223 lttng_payload_reset(&payload
);
224 lttng_event_rule_destroy(syscall
);
225 lttng_event_rule_destroy(syscall_from_buffer
);
228 static void test_event_rule_userspace_probe(void)
230 struct lttng_event_rule
*uprobe
= NULL
;
231 struct lttng_event_rule
*uprobe_from_buffer
= NULL
;
232 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
234 struct lttng_userspace_probe_location
*probe_location
= NULL
;
235 const struct lttng_userspace_probe_location
*probe_location_tmp
= NULL
;
236 enum lttng_event_rule_status status
;
238 const char *probe_name
= "my_probe.";
240 struct lttng_payload payload
;
242 diag("Event rule uprobe.");
244 lookup_method
= lttng_userspace_probe_location_lookup_method_function_elf_create();
245 if (!lookup_method
) {
246 fail("Setup error on userspace probe lookup method creation.");
250 probe_location
= lttng_userspace_probe_location_function_create(
252 "lttng_userspace_probe_location_tracepoint_create",
254 if (!probe_location
) {
255 fail("Setup error on userspace probe location creation.");
259 /* Ownership transferred to the probe location function object. */
260 lookup_method
= NULL
;
262 lttng_payload_init(&payload
);
264 uprobe
= lttng_event_rule_userspace_probe_create();
265 ok(uprobe
, "uprobe event rule object creation.");
267 status
= lttng_event_rule_userspace_probe_set_location(uprobe
, probe_location
);
268 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
269 "Setting uprobe event rule location.");
271 status
= lttng_event_rule_userspace_probe_get_location(
272 uprobe
, &probe_location_tmp
);
273 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
274 "Getting uprobe event rule location.");
275 ok(lttng_userspace_probe_location_is_equal(
276 probe_location
, probe_location_tmp
),
277 "Location is equal.");
279 status
= lttng_event_rule_userspace_probe_set_name(uprobe
, probe_name
);
280 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
281 "Setting uprobe event rule name: %s.", probe_name
);
282 status
= lttng_event_rule_userspace_probe_get_name(uprobe
, &tmp
);
283 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting uprobe name.");
284 ok(!strcmp(probe_name
, tmp
), "Uprobe name are equal.");
286 ok(lttng_event_rule_serialize(uprobe
, &payload
) == 0, "Serializing.");
289 struct lttng_payload_view view
=
290 lttng_payload_view_from_payload(
293 ok(lttng_event_rule_create_from_payload(
294 &view
, &uprobe_from_buffer
) > 0,
298 ok(lttng_event_rule_is_equal(uprobe
, uprobe_from_buffer
),
299 "serialized and from buffer are equal.");
302 lttng_payload_reset(&payload
);
303 lttng_event_rule_destroy(uprobe
);
304 lttng_event_rule_destroy(uprobe_from_buffer
);
305 lttng_userspace_probe_location_destroy(probe_location
);
306 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
309 static void test_event_rule_kernel_probe_by_location(
310 const struct lttng_kernel_probe_location
*location
)
312 struct lttng_event_rule
*kprobe
= NULL
;
313 struct lttng_event_rule
*kprobe_from_buffer
= NULL
;
314 enum lttng_event_rule_status status
;
315 const struct lttng_kernel_probe_location
*_location
;
317 const char *probe_name
= "my_probe";
319 struct lttng_payload payload
;
321 diag("Event rule kprobe for location type %d.",
322 lttng_kernel_probe_location_get_type(location
));
324 lttng_payload_init(&payload
);
326 kprobe
= lttng_event_rule_kernel_probe_create();
327 ok(kprobe
, "kprobe event rule object creation.");
329 status
= lttng_event_rule_kernel_probe_set_location(kprobe
, location
);
330 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
331 "Setting kprobe event rule location.");
332 status
= lttng_event_rule_kernel_probe_get_location(kprobe
, &_location
);
333 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
334 "Getting kprobe event rule location.");
335 ok(lttng_kernel_probe_location_is_equal(location
, _location
), "Locations are equal.");
337 status
= lttng_event_rule_kernel_probe_set_name(kprobe
, probe_name
);
338 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
339 "Setting kprobe event rule name: %s.", probe_name
);
340 status
= lttng_event_rule_kernel_probe_get_name(kprobe
, &tmp
);
341 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Getting kprobe name.");
342 ok(!strcmp(probe_name
, tmp
), "kprobe name are equal.");
344 ok(lttng_event_rule_serialize(kprobe
, &payload
) == 0, "Serializing.");
347 struct lttng_payload_view view
=
348 lttng_payload_view_from_payload(
351 ok(lttng_event_rule_create_from_payload(
352 &view
, &kprobe_from_buffer
) > 0,
356 ok(lttng_event_rule_is_equal(kprobe
, kprobe_from_buffer
),
357 "serialized and from buffer are equal.");
359 lttng_payload_reset(&payload
);
360 lttng_event_rule_destroy(kprobe
);
361 lttng_event_rule_destroy(kprobe_from_buffer
);
364 static void test_event_rule_kernel_probe(void)
366 struct lttng_kernel_probe_location
*address_location
= NULL
;
367 struct lttng_kernel_probe_location
*symbol_location
= NULL
;
369 address_location
= lttng_kernel_probe_location_address_create(50);
370 symbol_location
= lttng_kernel_probe_location_symbol_create("une_bonne", 50);
371 assert(address_location
);
372 assert(symbol_location
);
374 test_event_rule_kernel_probe_by_location(address_location
);
375 test_event_rule_kernel_probe_by_location(symbol_location
);
377 lttng_kernel_probe_location_destroy(address_location
);
378 lttng_kernel_probe_location_destroy(symbol_location
);
381 int main(int argc
, const char *argv
[])
383 plan_tests(NUM_TESTS
);
384 test_event_rule_tracepoint();
385 test_event_rule_syscall();
386 test_event_rule_userspace_probe();
387 test_event_rule_kernel_probe();
388 return exit_status();