Commit | Line | Data |
---|---|---|
dbc20d3c JR |
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> | |
a3c51653 JR |
21 | #include <lttng/event-rule/kernel-probe-internal.h> |
22 | #include <lttng/event-rule/kernel-probe.h> | |
dbc20d3c JR |
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> | |
e957bf98 JR |
27 | #include <lttng/event-rule/userspace-probe-internal.h> |
28 | #include <lttng/event-rule/userspace-probe.h> | |
dbc20d3c JR |
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> | |
34 | ||
35 | /* For error.h. */ | |
36 | int lttng_opt_quiet = 1; | |
37 | int lttng_opt_verbose; | |
38 | int lttng_opt_mi; | |
39 | ||
602a6d40 | 40 | #define NUM_TESTS 185 |
dbc20d3c JR |
41 | |
42 | struct tracepoint_test { | |
43 | enum lttng_domain_type type; | |
44 | bool support_exclusion; | |
45 | }; | |
46 | ||
47 | static | |
48 | void test_event_rule_tracepoint_by_domain(const struct tracepoint_test *test) | |
49 | { | |
50 | int ret; | |
51 | unsigned int count; | |
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"; | |
59 | const char *tmp; | |
60 | const char *exclusions[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"}; | |
61 | struct lttng_payload payload; | |
62 | ||
63 | type = test->type; | |
64 | diag("Testing domain %d.", type); | |
65 | ||
66 | lttng_payload_init(&payload); | |
67 | ||
68 | tracepoint = lttng_event_rule_tracepoint_create(type); | |
69 | ok(tracepoint, "tracepoint object."); | |
70 | ||
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); | |
74 | ||
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."); | |
80 | ||
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."); | |
86 | ||
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."); | |
93 | ||
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."); | |
101 | ||
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."); | |
109 | ||
110 | if (test->support_exclusion) { | |
111 | int i; | |
112 | ||
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]); | |
116 | } | |
117 | ||
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); | |
121 | ||
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]); | |
126 | } | |
127 | } else { | |
128 | int i; | |
129 | ||
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]); | |
133 | } | |
134 | ||
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); | |
138 | } | |
139 | ||
140 | ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing."); | |
141 | ||
142 | { | |
143 | struct lttng_payload_view view = | |
144 | lttng_payload_view_from_payload( | |
145 | &payload, 0, -1); | |
146 | ||
147 | ok(lttng_event_rule_create_from_payload( | |
148 | &view, &tracepoint_from_buffer) > 0, | |
149 | "Deserializing."); | |
150 | } | |
151 | ||
152 | ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer), "serialized and from buffer are equal."); | |
153 | ||
154 | lttng_payload_reset(&payload); | |
155 | lttng_event_rule_destroy(tracepoint); | |
156 | lttng_event_rule_destroy(tracepoint_from_buffer); | |
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 void test_event_rule_syscall(void) | |
180 | { | |
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"; | |
186 | const char *tmp; | |
187 | struct lttng_payload payload; | |
188 | ||
189 | diag("Event rule syscall."); | |
190 | ||
191 | lttng_payload_init(&payload); | |
192 | ||
193 | syscall = lttng_event_rule_syscall_create(); | |
194 | ok(syscall, "syscall object."); | |
195 | ||
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."); | |
201 | ||
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."); | |
207 | ||
208 | ok(lttng_event_rule_serialize(syscall, &payload) == 0, "Serializing."); | |
209 | ||
210 | { | |
211 | struct lttng_payload_view view = | |
212 | lttng_payload_view_from_payload( | |
213 | &payload, 0, -1); | |
214 | ||
215 | ok(lttng_event_rule_create_from_payload( | |
216 | &view, &syscall_from_buffer) > 0, | |
217 | "Deserializing."); | |
218 | } | |
219 | ||
220 | ok(lttng_event_rule_is_equal(syscall, syscall_from_buffer), | |
221 | "serialized and from buffer are equal."); | |
222 | ||
223 | lttng_payload_reset(&payload); | |
224 | lttng_event_rule_destroy(syscall); | |
225 | lttng_event_rule_destroy(syscall_from_buffer); | |
226 | } | |
227 | ||
1f1567a5 | 228 | static void test_event_rule_userspace_probe(void) |
dbc20d3c JR |
229 | { |
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 = | |
233 | NULL; | |
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; | |
237 | ||
238 | const char *probe_name = "my_probe."; | |
239 | const char *tmp; | |
240 | struct lttng_payload payload; | |
241 | ||
242 | diag("Event rule uprobe."); | |
243 | ||
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."); | |
247 | goto end; | |
248 | } | |
249 | ||
250 | probe_location = lttng_userspace_probe_location_function_create( | |
251 | "/proc/self/exe", | |
252 | "lttng_userspace_probe_location_tracepoint_create", | |
253 | lookup_method); | |
254 | if (!probe_location) { | |
255 | fail("Setup error on userspace probe location creation."); | |
256 | goto end; | |
257 | } | |
258 | ||
259 | /* Ownership transferred to the probe location function object. */ | |
260 | lookup_method = NULL; | |
261 | ||
262 | lttng_payload_init(&payload); | |
263 | ||
1f1567a5 | 264 | uprobe = lttng_event_rule_userspace_probe_create(); |
dbc20d3c JR |
265 | ok(uprobe, "uprobe event rule object creation."); |
266 | ||
1f1567a5 | 267 | status = lttng_event_rule_userspace_probe_set_location(uprobe, probe_location); |
dbc20d3c JR |
268 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, |
269 | "Setting uprobe event rule location."); | |
270 | ||
1f1567a5 | 271 | status = lttng_event_rule_userspace_probe_get_location( |
dbc20d3c JR |
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."); | |
278 | ||
405f9e7d | 279 | status = lttng_event_rule_userspace_probe_set_event_name(uprobe, probe_name); |
dbc20d3c JR |
280 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, |
281 | "Setting uprobe event rule name: %s.", probe_name); | |
405f9e7d | 282 | status = lttng_event_rule_userspace_probe_get_event_name(uprobe, &tmp); |
dbc20d3c JR |
283 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting uprobe name."); |
284 | ok(!strcmp(probe_name, tmp), "Uprobe name are equal."); | |
285 | ||
286 | ok(lttng_event_rule_serialize(uprobe, &payload) == 0, "Serializing."); | |
287 | ||
288 | { | |
289 | struct lttng_payload_view view = | |
290 | lttng_payload_view_from_payload( | |
291 | &payload, 0, -1); | |
292 | ||
293 | ok(lttng_event_rule_create_from_payload( | |
294 | &view, &uprobe_from_buffer) > 0, | |
295 | "Deserializing."); | |
296 | } | |
297 | ||
298 | ok(lttng_event_rule_is_equal(uprobe, uprobe_from_buffer), | |
299 | "serialized and from buffer are equal."); | |
300 | ||
301 | end: | |
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); | |
307 | } | |
308 | ||
f2791161 | 309 | static void test_event_rule_kernel_probe_by_location( |
dbc20d3c JR |
310 | const struct lttng_kernel_probe_location *location) |
311 | { | |
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; | |
316 | ||
317 | const char *probe_name = "my_probe"; | |
318 | const char *tmp; | |
319 | struct lttng_payload payload; | |
320 | ||
321 | diag("Event rule kprobe for location type %d.", | |
322 | lttng_kernel_probe_location_get_type(location)); | |
323 | ||
324 | lttng_payload_init(&payload); | |
325 | ||
602a6d40 | 326 | kprobe = lttng_event_rule_kernel_probe_create(location); |
dbc20d3c JR |
327 | ok(kprobe, "kprobe event rule object creation."); |
328 | ||
f2791161 | 329 | status = lttng_event_rule_kernel_probe_get_location(kprobe, &_location); |
dbc20d3c JR |
330 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, |
331 | "Getting kprobe event rule location."); | |
332 | ok(lttng_kernel_probe_location_is_equal(location, _location), "Locations are equal."); | |
333 | ||
d891bb52 | 334 | status = lttng_event_rule_kernel_probe_set_event_name(kprobe, probe_name); |
dbc20d3c JR |
335 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, |
336 | "Setting kprobe event rule name: %s.", probe_name); | |
d891bb52 | 337 | status = lttng_event_rule_kernel_probe_get_event_name(kprobe, &tmp); |
dbc20d3c JR |
338 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting kprobe name."); |
339 | ok(!strcmp(probe_name, tmp), "kprobe name are equal."); | |
340 | ||
341 | ok(lttng_event_rule_serialize(kprobe, &payload) == 0, "Serializing."); | |
342 | ||
343 | { | |
344 | struct lttng_payload_view view = | |
345 | lttng_payload_view_from_payload( | |
346 | &payload, 0, -1); | |
347 | ||
348 | ok(lttng_event_rule_create_from_payload( | |
349 | &view, &kprobe_from_buffer) > 0, | |
350 | "Deserializing."); | |
351 | } | |
352 | ||
353 | ok(lttng_event_rule_is_equal(kprobe, kprobe_from_buffer), | |
354 | "serialized and from buffer are equal."); | |
355 | ||
356 | lttng_payload_reset(&payload); | |
357 | lttng_event_rule_destroy(kprobe); | |
358 | lttng_event_rule_destroy(kprobe_from_buffer); | |
359 | } | |
360 | ||
f2791161 | 361 | static void test_event_rule_kernel_probe(void) |
dbc20d3c JR |
362 | { |
363 | struct lttng_kernel_probe_location *address_location = NULL; | |
364 | struct lttng_kernel_probe_location *symbol_location = NULL; | |
365 | ||
366 | address_location = lttng_kernel_probe_location_address_create(50); | |
367 | symbol_location = lttng_kernel_probe_location_symbol_create("une_bonne", 50); | |
368 | assert(address_location); | |
369 | assert(symbol_location); | |
370 | ||
f2791161 JR |
371 | test_event_rule_kernel_probe_by_location(address_location); |
372 | test_event_rule_kernel_probe_by_location(symbol_location); | |
dbc20d3c JR |
373 | |
374 | lttng_kernel_probe_location_destroy(address_location); | |
375 | lttng_kernel_probe_location_destroy(symbol_location); | |
376 | } | |
377 | ||
378 | int main(int argc, const char *argv[]) | |
379 | { | |
380 | plan_tests(NUM_TESTS); | |
381 | test_event_rule_tracepoint(); | |
382 | test_event_rule_syscall(); | |
1f1567a5 | 383 | test_event_rule_userspace_probe(); |
f2791161 | 384 | test_event_rule_kernel_probe(); |
dbc20d3c JR |
385 | return exit_status(); |
386 | } |