event-rule: userspace probe: force location on create
[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/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>
34
35 /* For error.h. */
36 int lttng_opt_quiet = 1;
37 int lttng_opt_verbose;
38 int lttng_opt_mi;
39
40 #define NUM_TESTS 184
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
228 static void test_event_rule_userspace_probe(void)
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
264 uprobe = lttng_event_rule_userspace_probe_create(probe_location);
265 ok(uprobe, "uprobe event rule object creation.");
266
267 status = lttng_event_rule_userspace_probe_get_location(
268 uprobe, &probe_location_tmp);
269 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
270 "Getting uprobe event rule location.");
271 ok(lttng_userspace_probe_location_is_equal(
272 probe_location, probe_location_tmp),
273 "Location is equal.");
274
275 status = lttng_event_rule_userspace_probe_set_event_name(uprobe, probe_name);
276 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
277 "Setting uprobe event rule name: %s.", probe_name);
278 status = lttng_event_rule_userspace_probe_get_event_name(uprobe, &tmp);
279 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting uprobe name.");
280 ok(!strcmp(probe_name, tmp), "Uprobe name are equal.");
281
282 ok(lttng_event_rule_serialize(uprobe, &payload) == 0, "Serializing.");
283
284 {
285 struct lttng_payload_view view =
286 lttng_payload_view_from_payload(
287 &payload, 0, -1);
288
289 ok(lttng_event_rule_create_from_payload(
290 &view, &uprobe_from_buffer) > 0,
291 "Deserializing.");
292 }
293
294 ok(lttng_event_rule_is_equal(uprobe, uprobe_from_buffer),
295 "serialized and from buffer are equal.");
296
297 end:
298 lttng_payload_reset(&payload);
299 lttng_event_rule_destroy(uprobe);
300 lttng_event_rule_destroy(uprobe_from_buffer);
301 lttng_userspace_probe_location_destroy(probe_location);
302 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
303 }
304
305 static void test_event_rule_kernel_probe_by_location(
306 const struct lttng_kernel_probe_location *location)
307 {
308 struct lttng_event_rule *kprobe = NULL;
309 struct lttng_event_rule *kprobe_from_buffer = NULL;
310 enum lttng_event_rule_status status;
311 const struct lttng_kernel_probe_location *_location;
312
313 const char *probe_name = "my_probe";
314 const char *tmp;
315 struct lttng_payload payload;
316
317 diag("Event rule kprobe for location type %d.",
318 lttng_kernel_probe_location_get_type(location));
319
320 lttng_payload_init(&payload);
321
322 kprobe = lttng_event_rule_kernel_probe_create(location);
323 ok(kprobe, "kprobe event rule object creation.");
324
325 status = lttng_event_rule_kernel_probe_get_location(kprobe, &_location);
326 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
327 "Getting kprobe event rule location.");
328 ok(lttng_kernel_probe_location_is_equal(location, _location), "Locations are equal.");
329
330 status = lttng_event_rule_kernel_probe_set_event_name(kprobe, probe_name);
331 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
332 "Setting kprobe event rule name: %s.", probe_name);
333 status = lttng_event_rule_kernel_probe_get_event_name(kprobe, &tmp);
334 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting kprobe name.");
335 ok(!strcmp(probe_name, tmp), "kprobe name are equal.");
336
337 ok(lttng_event_rule_serialize(kprobe, &payload) == 0, "Serializing.");
338
339 {
340 struct lttng_payload_view view =
341 lttng_payload_view_from_payload(
342 &payload, 0, -1);
343
344 ok(lttng_event_rule_create_from_payload(
345 &view, &kprobe_from_buffer) > 0,
346 "Deserializing.");
347 }
348
349 ok(lttng_event_rule_is_equal(kprobe, kprobe_from_buffer),
350 "serialized and from buffer are equal.");
351
352 lttng_payload_reset(&payload);
353 lttng_event_rule_destroy(kprobe);
354 lttng_event_rule_destroy(kprobe_from_buffer);
355 }
356
357 static void test_event_rule_kernel_probe(void)
358 {
359 struct lttng_kernel_probe_location *address_location = NULL;
360 struct lttng_kernel_probe_location *symbol_location = NULL;
361
362 address_location = lttng_kernel_probe_location_address_create(50);
363 symbol_location = lttng_kernel_probe_location_symbol_create("une_bonne", 50);
364 assert(address_location);
365 assert(symbol_location);
366
367 test_event_rule_kernel_probe_by_location(address_location);
368 test_event_rule_kernel_probe_by_location(symbol_location);
369
370 lttng_kernel_probe_location_destroy(address_location);
371 lttng_kernel_probe_location_destroy(symbol_location);
372 }
373
374 int main(int argc, const char *argv[])
375 {
376 plan_tests(NUM_TESTS);
377 test_event_rule_tracepoint();
378 test_event_rule_syscall();
379 test_event_rule_userspace_probe();
380 test_event_rule_kernel_probe();
381 return exit_status();
382 }
This page took 0.036518 seconds and 4 git commands to generate.