lttng-ctl: Introduce lttng_log_level_rule
[lttng-tools.git] / tests / unit / test_event_rule.c
CommitLineData
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. */
36int lttng_opt_quiet = 1;
37int lttng_opt_verbose;
38int lttng_opt_mi;
39
85b05318 40#define NUM_TESTS 144
dbc20d3c
JR
41
42struct tracepoint_test {
43 enum lttng_domain_type type;
44 bool support_exclusion;
45};
46
47static
48void test_event_rule_tracepoint_by_domain(const struct tracepoint_test *test)
49{
dbc20d3c
JR
50 unsigned int count;
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;
dbc20d3c
JR
55 const char *pattern="my_event_*";
56 const char *filter="msg_id == 23 && size >= 2048";
57 const char *tmp;
58 const char *exclusions[] = {"my_event_test1", "my_event_test2" ,"my_event_test3"};
85b05318
JR
59 struct lttng_log_level_rule *log_level_rule = NULL;
60 const struct lttng_log_level_rule *log_level_rule_return = NULL;
dbc20d3c
JR
61 struct lttng_payload payload;
62
63 type = test->type;
64 diag("Testing domain %d.", type);
65
66 lttng_payload_init(&payload);
67
85b05318
JR
68 log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
69 assert(log_level_rule);
70
dbc20d3c
JR
71 tracepoint = lttng_event_rule_tracepoint_create(type);
72 ok(tracepoint, "tracepoint object.");
73
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);
77
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.");
83
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.");
89
85b05318
JR
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.");
92
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.");
dbc20d3c
JR
97
98 if (test->support_exclusion) {
99 int i;
100
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]);
104 }
105
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);
109
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]);
114 }
115 } else {
116 int i;
117
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]);
121 }
122
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);
126 }
127
128 ok(lttng_event_rule_serialize(tracepoint, &payload) == 0, "Serializing.");
129
130 {
131 struct lttng_payload_view view =
132 lttng_payload_view_from_payload(
133 &payload, 0, -1);
134
135 ok(lttng_event_rule_create_from_payload(
136 &view, &tracepoint_from_buffer) > 0,
137 "Deserializing.");
138 }
139
140 ok(lttng_event_rule_is_equal(tracepoint, tracepoint_from_buffer), "serialized and from buffer are equal.");
141
142 lttng_payload_reset(&payload);
143 lttng_event_rule_destroy(tracepoint);
144 lttng_event_rule_destroy(tracepoint_from_buffer);
85b05318 145 lttng_log_level_rule_destroy(log_level_rule);
dbc20d3c
JR
146}
147
148static
149void test_event_rule_tracepoint(void)
150{
151 int i;
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}};
158
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.");
162
163 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
164 test_event_rule_tracepoint_by_domain(&tests[i]);
165 }
166}
167
168static void test_event_rule_syscall(void)
169{
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";
175 const char *tmp;
176 struct lttng_payload payload;
177
178 diag("Event rule syscall.");
179
180 lttng_payload_init(&payload);
181
182 syscall = lttng_event_rule_syscall_create();
183 ok(syscall, "syscall object.");
184
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.");
190
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.");
196
197 ok(lttng_event_rule_serialize(syscall, &payload) == 0, "Serializing.");
198
199 {
200 struct lttng_payload_view view =
201 lttng_payload_view_from_payload(
202 &payload, 0, -1);
203
204 ok(lttng_event_rule_create_from_payload(
205 &view, &syscall_from_buffer) > 0,
206 "Deserializing.");
207 }
208
209 ok(lttng_event_rule_is_equal(syscall, syscall_from_buffer),
210 "serialized and from buffer are equal.");
211
212 lttng_payload_reset(&payload);
213 lttng_event_rule_destroy(syscall);
214 lttng_event_rule_destroy(syscall_from_buffer);
215}
216
1f1567a5 217static void test_event_rule_userspace_probe(void)
dbc20d3c
JR
218{
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 =
222 NULL;
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;
226
227 const char *probe_name = "my_probe.";
228 const char *tmp;
229 struct lttng_payload payload;
230
231 diag("Event rule uprobe.");
232
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.");
236 goto end;
237 }
238
239 probe_location = lttng_userspace_probe_location_function_create(
240 "/proc/self/exe",
241 "lttng_userspace_probe_location_tracepoint_create",
242 lookup_method);
243 if (!probe_location) {
244 fail("Setup error on userspace probe location creation.");
245 goto end;
246 }
247
248 /* Ownership transferred to the probe location function object. */
249 lookup_method = NULL;
250
251 lttng_payload_init(&payload);
252
10685de6 253 uprobe = lttng_event_rule_userspace_probe_create(probe_location);
dbc20d3c
JR
254 ok(uprobe, "uprobe event rule object creation.");
255
1f1567a5 256 status = lttng_event_rule_userspace_probe_get_location(
dbc20d3c
JR
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.");
263
405f9e7d 264 status = lttng_event_rule_userspace_probe_set_event_name(uprobe, probe_name);
dbc20d3c
JR
265 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
266 "Setting uprobe event rule name: %s.", probe_name);
405f9e7d 267 status = lttng_event_rule_userspace_probe_get_event_name(uprobe, &tmp);
dbc20d3c
JR
268 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting uprobe name.");
269 ok(!strcmp(probe_name, tmp), "Uprobe name are equal.");
270
271 ok(lttng_event_rule_serialize(uprobe, &payload) == 0, "Serializing.");
272
273 {
274 struct lttng_payload_view view =
275 lttng_payload_view_from_payload(
276 &payload, 0, -1);
277
278 ok(lttng_event_rule_create_from_payload(
279 &view, &uprobe_from_buffer) > 0,
280 "Deserializing.");
281 }
282
283 ok(lttng_event_rule_is_equal(uprobe, uprobe_from_buffer),
284 "serialized and from buffer are equal.");
285
286end:
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);
292}
293
f2791161 294static void test_event_rule_kernel_probe_by_location(
dbc20d3c
JR
295 const struct lttng_kernel_probe_location *location)
296{
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;
301
302 const char *probe_name = "my_probe";
303 const char *tmp;
304 struct lttng_payload payload;
305
306 diag("Event rule kprobe for location type %d.",
307 lttng_kernel_probe_location_get_type(location));
308
309 lttng_payload_init(&payload);
310
602a6d40 311 kprobe = lttng_event_rule_kernel_probe_create(location);
dbc20d3c
JR
312 ok(kprobe, "kprobe event rule object creation.");
313
f2791161 314 status = lttng_event_rule_kernel_probe_get_location(kprobe, &_location);
dbc20d3c
JR
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.");
318
d891bb52 319 status = lttng_event_rule_kernel_probe_set_event_name(kprobe, probe_name);
dbc20d3c
JR
320 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
321 "Setting kprobe event rule name: %s.", probe_name);
d891bb52 322 status = lttng_event_rule_kernel_probe_get_event_name(kprobe, &tmp);
dbc20d3c
JR
323 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Getting kprobe name.");
324 ok(!strcmp(probe_name, tmp), "kprobe name are equal.");
325
326 ok(lttng_event_rule_serialize(kprobe, &payload) == 0, "Serializing.");
327
328 {
329 struct lttng_payload_view view =
330 lttng_payload_view_from_payload(
331 &payload, 0, -1);
332
333 ok(lttng_event_rule_create_from_payload(
334 &view, &kprobe_from_buffer) > 0,
335 "Deserializing.");
336 }
337
338 ok(lttng_event_rule_is_equal(kprobe, kprobe_from_buffer),
339 "serialized and from buffer are equal.");
340
341 lttng_payload_reset(&payload);
342 lttng_event_rule_destroy(kprobe);
343 lttng_event_rule_destroy(kprobe_from_buffer);
344}
345
f2791161 346static void test_event_rule_kernel_probe(void)
dbc20d3c
JR
347{
348 struct lttng_kernel_probe_location *address_location = NULL;
349 struct lttng_kernel_probe_location *symbol_location = NULL;
350
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);
355
f2791161
JR
356 test_event_rule_kernel_probe_by_location(address_location);
357 test_event_rule_kernel_probe_by_location(symbol_location);
dbc20d3c
JR
358
359 lttng_kernel_probe_location_destroy(address_location);
360 lttng_kernel_probe_location_destroy(symbol_location);
361}
362
363int main(int argc, const char *argv[])
364{
365 plan_tests(NUM_TESTS);
366 test_event_rule_tracepoint();
367 test_event_rule_syscall();
1f1567a5 368 test_event_rule_userspace_probe();
f2791161 369 test_event_rule_kernel_probe();
dbc20d3c
JR
370 return exit_status();
371}
This page took 0.038737 seconds and 4 git commands to generate.