Commit | Line | Data |
---|---|---|
e6a39346 JR |
1 | /* |
2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
58daac01 | 8 | #include <common/credentials.h> |
e6a39346 | 9 | #include <common/error.h> |
6a751b95 JR |
10 | #include <common/hashtable/hashtable.h> |
11 | #include <common/hashtable/utils.h> | |
e6a39346 | 12 | #include <common/macros.h> |
6a751b95 | 13 | #include <common/mi-lttng.h> |
e6a39346 | 14 | #include <common/payload-view.h> |
6a751b95 | 15 | #include <common/payload.h> |
e6a39346 | 16 | #include <common/runas.h> |
40fd2ccf | 17 | #include <common/string-utils/string-utils.h> |
e6a39346 | 18 | #include <lttng/event-rule/event-rule-internal.h> |
4f7da553 | 19 | #include <lttng/event-rule/kernel-syscall-internal.h> |
e6a39346 JR |
20 | |
21 | #define IS_SYSCALL_EVENT_RULE(rule) \ | |
4f7da553 | 22 | (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL) |
e6a39346 | 23 | |
4f7da553 | 24 | static void lttng_event_rule_kernel_syscall_destroy(struct lttng_event_rule *rule) |
e6a39346 | 25 | { |
4f7da553 | 26 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
27 | |
28 | if (rule == NULL) { | |
29 | return; | |
30 | } | |
31 | ||
4f7da553 | 32 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
33 | |
34 | free(syscall->pattern); | |
35 | free(syscall->filter_expression); | |
36 | free(syscall->internal_filter.filter); | |
37 | free(syscall->internal_filter.bytecode); | |
38 | free(syscall); | |
39 | } | |
40 | ||
4f7da553 | 41 | static bool lttng_event_rule_kernel_syscall_validate( |
e6a39346 JR |
42 | const struct lttng_event_rule *rule) |
43 | { | |
44 | bool valid = false; | |
4f7da553 | 45 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
46 | |
47 | if (!rule) { | |
48 | goto end; | |
49 | } | |
50 | ||
4f7da553 | 51 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
52 | |
53 | /* Required field. */ | |
54 | if (!syscall->pattern) { | |
55 | ERR("Invalid syscall event rule: a pattern must be set."); | |
56 | goto end; | |
57 | } | |
58 | ||
59 | valid = true; | |
60 | end: | |
61 | return valid; | |
62 | } | |
63 | ||
4f7da553 | 64 | static int lttng_event_rule_kernel_syscall_serialize( |
e6a39346 JR |
65 | const struct lttng_event_rule *rule, |
66 | struct lttng_payload *payload) | |
67 | { | |
68 | int ret; | |
69 | size_t pattern_len, filter_expression_len; | |
4f7da553 JR |
70 | struct lttng_event_rule_kernel_syscall *syscall; |
71 | struct lttng_event_rule_kernel_syscall_comm syscall_comm; | |
e6a39346 JR |
72 | |
73 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule)) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | ||
78 | DBG("Serializing syscall event rule"); | |
4f7da553 | 79 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
80 | |
81 | pattern_len = strlen(syscall->pattern) + 1; | |
82 | ||
83 | if (syscall->filter_expression != NULL) { | |
84 | filter_expression_len = strlen(syscall->filter_expression) + 1; | |
85 | } else { | |
86 | filter_expression_len = 0; | |
87 | } | |
88 | ||
89 | syscall_comm.pattern_len = pattern_len; | |
90 | syscall_comm.filter_expression_len = filter_expression_len; | |
f6a5af19 | 91 | syscall_comm.emission_site = syscall->emission_site; |
e6a39346 JR |
92 | |
93 | ret = lttng_dynamic_buffer_append( | |
94 | &payload->buffer, &syscall_comm, sizeof(syscall_comm)); | |
95 | if (ret) { | |
96 | goto end; | |
97 | } | |
98 | ||
99 | ret = lttng_dynamic_buffer_append( | |
100 | &payload->buffer, syscall->pattern, pattern_len); | |
101 | if (ret) { | |
102 | goto end; | |
103 | } | |
104 | ||
105 | ret = lttng_dynamic_buffer_append(&payload->buffer, | |
106 | syscall->filter_expression, filter_expression_len); | |
107 | end: | |
108 | return ret; | |
109 | } | |
110 | ||
4f7da553 | 111 | static bool lttng_event_rule_kernel_syscall_is_equal(const struct lttng_event_rule *_a, |
e6a39346 JR |
112 | const struct lttng_event_rule *_b) |
113 | { | |
114 | bool is_equal = false; | |
4f7da553 | 115 | struct lttng_event_rule_kernel_syscall *a, *b; |
e6a39346 | 116 | |
4f7da553 JR |
117 | a = container_of(_a, struct lttng_event_rule_kernel_syscall, parent); |
118 | b = container_of(_b, struct lttng_event_rule_kernel_syscall, parent); | |
e6a39346 JR |
119 | |
120 | if (!!a->filter_expression != !!b->filter_expression) { | |
121 | goto end; | |
122 | } | |
123 | ||
a0377dfe FD |
124 | LTTNG_ASSERT(a->pattern); |
125 | LTTNG_ASSERT(b->pattern); | |
e6a39346 JR |
126 | if (strcmp(a->pattern, b->pattern)) { |
127 | goto end; | |
128 | } | |
129 | ||
130 | if (a->filter_expression && b->filter_expression) { | |
131 | if (strcmp(a->filter_expression, b->filter_expression)) { | |
132 | goto end; | |
133 | } | |
134 | } else if (!!a->filter_expression != !!b->filter_expression) { | |
135 | /* One is set and not the other. */ | |
136 | goto end; | |
137 | } | |
138 | ||
139 | is_equal = true; | |
140 | end: | |
141 | return is_equal; | |
142 | } | |
143 | ||
4f7da553 | 144 | static enum lttng_error_code lttng_event_rule_kernel_syscall_generate_filter_bytecode( |
58daac01 JR |
145 | struct lttng_event_rule *rule, |
146 | const struct lttng_credentials *creds) | |
e6a39346 JR |
147 | { |
148 | int ret; | |
149 | enum lttng_error_code ret_code = LTTNG_OK; | |
4f7da553 | 150 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
151 | enum lttng_event_rule_status status; |
152 | const char *filter; | |
2b00d462 | 153 | struct lttng_bytecode *bytecode = NULL; |
e6a39346 | 154 | |
a0377dfe | 155 | LTTNG_ASSERT(rule); |
e6a39346 | 156 | |
4f7da553 | 157 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
158 | |
159 | /* Generate the filter bytecode. */ | |
4f7da553 | 160 | status = lttng_event_rule_kernel_syscall_get_filter(rule, &filter); |
e6a39346 JR |
161 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { |
162 | filter = NULL; | |
163 | } else if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
164 | ret_code = LTTNG_ERR_FILTER_INVAL; | |
165 | goto end; | |
166 | } | |
167 | ||
168 | if (filter && filter[0] == '\0') { | |
169 | ret_code = LTTNG_ERR_FILTER_INVAL; | |
170 | goto end; | |
171 | } | |
172 | ||
173 | if (filter == NULL) { | |
174 | /* Nothing to do. */ | |
175 | ret = LTTNG_OK; | |
176 | goto end; | |
177 | } | |
178 | ||
179 | syscall->internal_filter.filter = strdup(filter); | |
180 | if (syscall->internal_filter.filter == NULL) { | |
181 | ret_code = LTTNG_ERR_NOMEM; | |
182 | goto end; | |
183 | } | |
184 | ||
185 | ret = run_as_generate_filter_bytecode( | |
58daac01 | 186 | syscall->internal_filter.filter, creds, &bytecode); |
e6a39346 JR |
187 | if (ret) { |
188 | ret_code = LTTNG_ERR_FILTER_INVAL; | |
189 | } | |
190 | ||
191 | syscall->internal_filter.bytecode = bytecode; | |
192 | bytecode = NULL; | |
193 | ||
194 | end: | |
195 | free(bytecode); | |
196 | return ret_code; | |
197 | } | |
198 | ||
4f7da553 | 199 | static const char *lttng_event_rule_kernel_syscall_get_internal_filter( |
e6a39346 JR |
200 | const struct lttng_event_rule *rule) |
201 | { | |
4f7da553 | 202 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 | 203 | |
a0377dfe | 204 | LTTNG_ASSERT(rule); |
4f7da553 | 205 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
206 | |
207 | return syscall->internal_filter.filter; | |
208 | } | |
209 | ||
2b00d462 | 210 | static const struct lttng_bytecode * |
4f7da553 | 211 | lttng_event_rule_kernel_syscall_get_internal_filter_bytecode( |
e6a39346 JR |
212 | const struct lttng_event_rule *rule) |
213 | { | |
4f7da553 | 214 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 | 215 | |
a0377dfe | 216 | LTTNG_ASSERT(rule); |
4f7da553 | 217 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
218 | |
219 | return syscall->internal_filter.bytecode; | |
220 | } | |
221 | ||
993578ff | 222 | static enum lttng_event_rule_generate_exclusions_status |
f46376a1 MJ |
223 | lttng_event_rule_kernel_syscall_generate_exclusions( |
224 | const struct lttng_event_rule *rule __attribute__((unused)), | |
993578ff | 225 | struct lttng_event_exclusion **exclusions) |
e6a39346 | 226 | { |
993578ff JR |
227 | /* Unsupported. */ |
228 | *exclusions = NULL; | |
229 | return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; | |
e6a39346 JR |
230 | } |
231 | ||
959e3c66 | 232 | static unsigned long |
4f7da553 | 233 | lttng_event_rule_kernel_syscall_hash( |
959e3c66 JR |
234 | const struct lttng_event_rule *rule) |
235 | { | |
236 | unsigned long hash; | |
4f7da553 | 237 | struct lttng_event_rule_kernel_syscall *syscall_rule = |
959e3c66 JR |
238 | container_of(rule, typeof(*syscall_rule), parent); |
239 | ||
4f7da553 | 240 | hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL, |
959e3c66 JR |
241 | lttng_ht_seed); |
242 | hash ^= hash_key_str(syscall_rule->pattern, lttng_ht_seed); | |
243 | if (syscall_rule->filter_expression) { | |
244 | hash ^= hash_key_str(syscall_rule->filter_expression, | |
245 | lttng_ht_seed); | |
246 | } | |
247 | ||
248 | return hash; | |
249 | } | |
250 | ||
6a751b95 JR |
251 | static enum lttng_error_code lttng_event_rule_kernel_syscall_mi_serialize( |
252 | const struct lttng_event_rule *rule, struct mi_writer *writer) | |
253 | { | |
254 | int ret; | |
255 | enum lttng_error_code ret_code; | |
256 | enum lttng_event_rule_status status; | |
257 | ||
258 | enum lttng_event_rule_kernel_syscall_emission_site site_type; | |
259 | const char *filter = NULL; | |
260 | const char *name_pattern = NULL; | |
261 | const char *site_type_str = NULL; | |
262 | ||
a0377dfe FD |
263 | LTTNG_ASSERT(rule); |
264 | LTTNG_ASSERT(writer); | |
265 | LTTNG_ASSERT(IS_SYSCALL_EVENT_RULE(rule)); | |
6a751b95 JR |
266 | |
267 | status = lttng_event_rule_kernel_syscall_get_name_pattern( | |
268 | rule, &name_pattern); | |
a0377dfe FD |
269 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
270 | LTTNG_ASSERT(name_pattern); | |
6a751b95 JR |
271 | |
272 | status = lttng_event_rule_kernel_syscall_get_filter(rule, &filter); | |
a0377dfe | 273 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK || |
6a751b95 JR |
274 | status == LTTNG_EVENT_RULE_STATUS_UNSET); |
275 | ||
276 | site_type = lttng_event_rule_kernel_syscall_get_emission_site(rule); | |
277 | ||
278 | switch (site_type) { | |
279 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT: | |
280 | site_type_str = mi_lttng_event_rule_kernel_syscall_emission_site_entry_exit; | |
281 | break; | |
282 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY: | |
283 | site_type_str = mi_lttng_event_rule_kernel_syscall_emission_site_entry; | |
284 | break; | |
285 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT: | |
286 | site_type_str = mi_lttng_event_rule_kernel_syscall_emission_site_exit; | |
287 | break; | |
288 | default: | |
289 | abort(); | |
290 | break; | |
291 | } | |
292 | ||
293 | /* Open event rule kernel syscall element. */ | |
294 | ret = mi_lttng_writer_open_element( | |
295 | writer, mi_lttng_element_event_rule_kernel_syscall); | |
296 | if (ret) { | |
297 | goto mi_error; | |
298 | } | |
299 | ||
300 | /* Emission site. */ | |
301 | ret = mi_lttng_writer_write_element_string(writer, | |
302 | mi_lttng_element_event_rule_kernel_syscall_emission_site, | |
303 | site_type_str); | |
304 | if (ret) { | |
305 | goto mi_error; | |
306 | } | |
307 | ||
308 | /* Name pattern. */ | |
309 | ret = mi_lttng_writer_write_element_string(writer, | |
310 | mi_lttng_element_event_rule_name_pattern, name_pattern); | |
311 | if (ret) { | |
312 | goto mi_error; | |
313 | } | |
314 | ||
315 | /* Filter. */ | |
316 | if (filter != NULL) { | |
317 | ret = mi_lttng_writer_write_element_string(writer, | |
318 | mi_lttng_element_event_rule_filter_expression, | |
319 | filter); | |
320 | if (ret) { | |
321 | goto mi_error; | |
322 | } | |
323 | } | |
324 | ||
325 | /* Close event rule kernel syscall. */ | |
326 | ret = mi_lttng_writer_close_element(writer); | |
327 | if (ret) { | |
328 | goto mi_error; | |
329 | } | |
330 | ||
331 | ret_code = LTTNG_OK; | |
332 | goto end; | |
333 | ||
334 | mi_error: | |
335 | ret_code = LTTNG_ERR_MI_IO_FAIL; | |
336 | end: | |
337 | return ret_code; | |
338 | } | |
339 | ||
4f7da553 JR |
340 | struct lttng_event_rule *lttng_event_rule_kernel_syscall_create( |
341 | enum lttng_event_rule_kernel_syscall_emission_site | |
f6a5af19 | 342 | emission_site) |
e6a39346 JR |
343 | { |
344 | struct lttng_event_rule *rule = NULL; | |
4f7da553 | 345 | struct lttng_event_rule_kernel_syscall *syscall_rule; |
67d38b93 | 346 | enum lttng_event_rule_status status; |
e6a39346 | 347 | |
57739a6b | 348 | /* Validate the emission site type */ |
f6a5af19 | 349 | switch (emission_site) { |
4f7da553 JR |
350 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT: |
351 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY: | |
352 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT: | |
57739a6b JR |
353 | break; |
354 | default: | |
355 | /* Invalid emission type */ | |
356 | goto end; | |
357 | } | |
358 | ||
a6bc4ca9 | 359 | syscall_rule = (lttng_event_rule_kernel_syscall *) zmalloc(sizeof(struct lttng_event_rule_kernel_syscall)); |
e6a39346 JR |
360 | if (!syscall_rule) { |
361 | goto end; | |
362 | } | |
363 | ||
364 | rule = &syscall_rule->parent; | |
365 | lttng_event_rule_init( | |
4f7da553 JR |
366 | &syscall_rule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL); |
367 | syscall_rule->parent.validate = lttng_event_rule_kernel_syscall_validate; | |
368 | syscall_rule->parent.serialize = lttng_event_rule_kernel_syscall_serialize; | |
369 | syscall_rule->parent.equal = lttng_event_rule_kernel_syscall_is_equal; | |
370 | syscall_rule->parent.destroy = lttng_event_rule_kernel_syscall_destroy; | |
e6a39346 | 371 | syscall_rule->parent.generate_filter_bytecode = |
4f7da553 | 372 | lttng_event_rule_kernel_syscall_generate_filter_bytecode; |
e6a39346 | 373 | syscall_rule->parent.get_filter = |
4f7da553 | 374 | lttng_event_rule_kernel_syscall_get_internal_filter; |
e6a39346 | 375 | syscall_rule->parent.get_filter_bytecode = |
4f7da553 | 376 | lttng_event_rule_kernel_syscall_get_internal_filter_bytecode; |
e6a39346 | 377 | syscall_rule->parent.generate_exclusions = |
4f7da553 JR |
378 | lttng_event_rule_kernel_syscall_generate_exclusions; |
379 | syscall_rule->parent.hash = lttng_event_rule_kernel_syscall_hash; | |
6a751b95 | 380 | syscall_rule->parent.mi_serialize = lttng_event_rule_kernel_syscall_mi_serialize; |
67d38b93 JR |
381 | |
382 | /* Default pattern is '*'. */ | |
4f7da553 | 383 | status = lttng_event_rule_kernel_syscall_set_name_pattern(rule, "*"); |
67d38b93 JR |
384 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
385 | lttng_event_rule_destroy(rule); | |
386 | rule = NULL; | |
387 | } | |
388 | ||
57739a6b | 389 | /* Emission site type */ |
f6a5af19 | 390 | syscall_rule->emission_site = emission_site; |
57739a6b | 391 | |
e6a39346 JR |
392 | end: |
393 | return rule; | |
394 | } | |
395 | ||
4f7da553 | 396 | ssize_t lttng_event_rule_kernel_syscall_create_from_payload( |
e6a39346 JR |
397 | struct lttng_payload_view *view, |
398 | struct lttng_event_rule **_event_rule) | |
399 | { | |
400 | ssize_t ret, offset = 0; | |
401 | enum lttng_event_rule_status status; | |
4f7da553 | 402 | const struct lttng_event_rule_kernel_syscall_comm *syscall_comm; |
e6a39346 JR |
403 | const char *pattern; |
404 | const char *filter_expression = NULL; | |
405 | struct lttng_buffer_view current_buffer_view; | |
406 | struct lttng_event_rule *rule = NULL; | |
407 | ||
408 | if (!_event_rule) { | |
409 | ret = -1; | |
410 | goto end; | |
411 | } | |
412 | ||
413 | if (view->buffer.size < sizeof(*syscall_comm)) { | |
414 | ERR("Failed to initialize from malformed event rule syscall: buffer too short to contain header"); | |
415 | ret = -1; | |
416 | goto end; | |
417 | } | |
418 | ||
419 | current_buffer_view = lttng_buffer_view_from_view( | |
420 | &view->buffer, offset, sizeof(*syscall_comm)); | |
3e6e0df2 | 421 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { |
e6a39346 JR |
422 | ret = -1; |
423 | goto end; | |
424 | } | |
425 | ||
3e6e0df2 | 426 | syscall_comm = (typeof(syscall_comm)) current_buffer_view.data; |
a6bc4ca9 | 427 | rule = lttng_event_rule_kernel_syscall_create((lttng_event_rule_kernel_syscall_emission_site) syscall_comm->emission_site); |
e6a39346 JR |
428 | if (!rule) { |
429 | ERR("Failed to create event rule syscall"); | |
430 | ret = -1; | |
431 | goto end; | |
432 | } | |
433 | ||
434 | /* Skip to payload. */ | |
435 | offset += current_buffer_view.size; | |
436 | ||
437 | /* Map the pattern. */ | |
438 | current_buffer_view = lttng_buffer_view_from_view( | |
439 | &view->buffer, offset, syscall_comm->pattern_len); | |
3e6e0df2 | 440 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { |
e6a39346 JR |
441 | ret = -1; |
442 | goto end; | |
443 | } | |
444 | ||
3e6e0df2 | 445 | pattern = current_buffer_view.data; |
e6a39346 JR |
446 | if (!lttng_buffer_view_contains_string(¤t_buffer_view, pattern, |
447 | syscall_comm->pattern_len)) { | |
448 | ret = -1; | |
449 | goto end; | |
450 | } | |
451 | ||
452 | /* Skip after the pattern. */ | |
453 | offset += syscall_comm->pattern_len; | |
454 | ||
455 | if (!syscall_comm->filter_expression_len) { | |
456 | goto skip_filter_expression; | |
457 | } | |
458 | ||
459 | /* Map the filter_expression. */ | |
460 | current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset, | |
461 | syscall_comm->filter_expression_len); | |
3e6e0df2 | 462 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { |
e6a39346 JR |
463 | ret = -1; |
464 | goto end; | |
465 | } | |
466 | ||
3e6e0df2 | 467 | filter_expression = current_buffer_view.data; |
e6a39346 JR |
468 | if (!lttng_buffer_view_contains_string(¤t_buffer_view, |
469 | filter_expression, | |
470 | syscall_comm->filter_expression_len)) { | |
471 | ret = -1; | |
472 | goto end; | |
473 | } | |
474 | ||
475 | /* Skip after the pattern. */ | |
476 | offset += syscall_comm->filter_expression_len; | |
477 | ||
478 | skip_filter_expression: | |
479 | ||
4f7da553 | 480 | status = lttng_event_rule_kernel_syscall_set_name_pattern(rule, pattern); |
e6a39346 JR |
481 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
482 | ERR("Failed to set event rule syscall pattern"); | |
483 | ret = -1; | |
484 | goto end; | |
485 | } | |
486 | ||
487 | if (filter_expression) { | |
4f7da553 | 488 | status = lttng_event_rule_kernel_syscall_set_filter( |
e6a39346 JR |
489 | rule, filter_expression); |
490 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
491 | ERR("Failed to set event rule syscall pattern"); | |
492 | ret = -1; | |
493 | goto end; | |
494 | } | |
495 | } | |
496 | ||
497 | *_event_rule = rule; | |
498 | rule = NULL; | |
499 | ret = offset; | |
500 | end: | |
501 | lttng_event_rule_destroy(rule); | |
502 | return ret; | |
503 | } | |
504 | ||
4f7da553 | 505 | enum lttng_event_rule_status lttng_event_rule_kernel_syscall_set_name_pattern( |
e6a39346 JR |
506 | struct lttng_event_rule *rule, const char *pattern) |
507 | { | |
508 | char *pattern_copy = NULL; | |
4f7da553 | 509 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
510 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
511 | ||
512 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern || | |
513 | strlen(pattern) == 0) { | |
514 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
515 | goto end; | |
516 | } | |
517 | ||
4f7da553 | 518 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
519 | pattern_copy = strdup(pattern); |
520 | if (!pattern_copy) { | |
521 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
522 | goto end; | |
523 | } | |
524 | ||
40fd2ccf JR |
525 | strutils_normalize_star_glob_pattern(pattern_copy); |
526 | ||
527 | free(syscall->pattern); | |
e6a39346 JR |
528 | |
529 | syscall->pattern = pattern_copy; | |
530 | pattern_copy = NULL; | |
531 | end: | |
532 | return status; | |
533 | } | |
534 | ||
4f7da553 | 535 | enum lttng_event_rule_status lttng_event_rule_kernel_syscall_get_name_pattern( |
e6a39346 JR |
536 | const struct lttng_event_rule *rule, const char **pattern) |
537 | { | |
4f7da553 | 538 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
539 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
540 | ||
541 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern) { | |
542 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
543 | goto end; | |
544 | } | |
545 | ||
4f7da553 | 546 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
547 | if (!syscall->pattern) { |
548 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
549 | goto end; | |
550 | } | |
551 | ||
552 | *pattern = syscall->pattern; | |
553 | end: | |
554 | return status; | |
555 | } | |
556 | ||
4f7da553 | 557 | enum lttng_event_rule_status lttng_event_rule_kernel_syscall_set_filter( |
e6a39346 JR |
558 | struct lttng_event_rule *rule, const char *expression) |
559 | { | |
560 | char *expression_copy = NULL; | |
4f7da553 | 561 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
562 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
563 | ||
564 | /* TODO: validate that the passed expression is valid. */ | |
565 | ||
566 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression || | |
567 | strlen(expression) == 0) { | |
568 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
569 | goto end; | |
570 | } | |
571 | ||
4f7da553 | 572 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
573 | expression_copy = strdup(expression); |
574 | if (!expression_copy) { | |
575 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
576 | goto end; | |
577 | } | |
578 | ||
579 | if (syscall->filter_expression) { | |
580 | free(syscall->filter_expression); | |
581 | } | |
582 | ||
583 | syscall->filter_expression = expression_copy; | |
584 | expression_copy = NULL; | |
585 | end: | |
586 | return status; | |
587 | } | |
588 | ||
4f7da553 | 589 | enum lttng_event_rule_status lttng_event_rule_kernel_syscall_get_filter( |
e6a39346 JR |
590 | const struct lttng_event_rule *rule, const char **expression) |
591 | { | |
4f7da553 | 592 | struct lttng_event_rule_kernel_syscall *syscall; |
e6a39346 JR |
593 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
594 | ||
595 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression) { | |
596 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
597 | goto end; | |
598 | } | |
599 | ||
4f7da553 | 600 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
e6a39346 JR |
601 | if (!syscall->filter_expression) { |
602 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
603 | goto end; | |
604 | } | |
605 | ||
606 | *expression = syscall->filter_expression; | |
607 | end: | |
608 | return status; | |
609 | } | |
4f7da553 JR |
610 | extern enum lttng_event_rule_kernel_syscall_emission_site |
611 | lttng_event_rule_kernel_syscall_get_emission_site( | |
57739a6b JR |
612 | const struct lttng_event_rule *rule) |
613 | { | |
4f7da553 JR |
614 | enum lttng_event_rule_kernel_syscall_emission_site emission_site = |
615 | LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN; | |
616 | struct lttng_event_rule_kernel_syscall *syscall; | |
57739a6b JR |
617 | |
618 | if (!rule || !IS_SYSCALL_EVENT_RULE(rule)) { | |
619 | goto end; | |
620 | } | |
621 | ||
4f7da553 | 622 | syscall = container_of(rule, struct lttng_event_rule_kernel_syscall, parent); |
f6a5af19 | 623 | emission_site = syscall->emission_site; |
57739a6b JR |
624 | |
625 | end: | |
f6a5af19 | 626 | return emission_site; |
57739a6b JR |
627 | } |
628 | ||
4f7da553 JR |
629 | const char *lttng_event_rule_kernel_syscall_emission_site_str( |
630 | enum lttng_event_rule_kernel_syscall_emission_site emission_site) | |
57739a6b | 631 | { |
f6a5af19 | 632 | switch (emission_site) { |
4f7da553 | 633 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY: |
57739a6b | 634 | return "entry"; |
4f7da553 | 635 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT: |
57739a6b | 636 | return "entry+exit"; |
4f7da553 | 637 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT: |
57739a6b JR |
638 | return "exit"; |
639 | default: | |
640 | return "???"; | |
641 | } | |
642 | } |