event-rule: tracepoint: set default pattern to '*'
[lttng-tools.git] / src / common / event-rule / syscall.c
CommitLineData
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
8#include <assert.h>
58daac01 9#include <common/credentials.h>
e6a39346
JR
10#include <common/error.h>
11#include <common/macros.h>
12#include <common/payload.h>
13#include <common/payload-view.h>
14#include <common/runas.h>
959e3c66
JR
15#include <common/hashtable/hashtable.h>
16#include <common/hashtable/utils.h>
e6a39346
JR
17#include <lttng/event-rule/event-rule-internal.h>
18#include <lttng/event-rule/syscall-internal.h>
19
20#define IS_SYSCALL_EVENT_RULE(rule) \
21 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_SYSCALL)
22
23static void lttng_event_rule_syscall_destroy(struct lttng_event_rule *rule)
24{
25 struct lttng_event_rule_syscall *syscall;
26
27 if (rule == NULL) {
28 return;
29 }
30
31 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
32
33 free(syscall->pattern);
34 free(syscall->filter_expression);
35 free(syscall->internal_filter.filter);
36 free(syscall->internal_filter.bytecode);
37 free(syscall);
38}
39
40static bool lttng_event_rule_syscall_validate(
41 const struct lttng_event_rule *rule)
42{
43 bool valid = false;
44 struct lttng_event_rule_syscall *syscall;
45
46 if (!rule) {
47 goto end;
48 }
49
50 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
51
52 /* Required field. */
53 if (!syscall->pattern) {
54 ERR("Invalid syscall event rule: a pattern must be set.");
55 goto end;
56 }
57
58 valid = true;
59end:
60 return valid;
61}
62
63static int lttng_event_rule_syscall_serialize(
64 const struct lttng_event_rule *rule,
65 struct lttng_payload *payload)
66{
67 int ret;
68 size_t pattern_len, filter_expression_len;
69 struct lttng_event_rule_syscall *syscall;
70 struct lttng_event_rule_syscall_comm syscall_comm;
71
72 if (!rule || !IS_SYSCALL_EVENT_RULE(rule)) {
73 ret = -1;
74 goto end;
75 }
76
77 DBG("Serializing syscall event rule");
78 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
79
80 pattern_len = strlen(syscall->pattern) + 1;
81
82 if (syscall->filter_expression != NULL) {
83 filter_expression_len = strlen(syscall->filter_expression) + 1;
84 } else {
85 filter_expression_len = 0;
86 }
87
88 syscall_comm.pattern_len = pattern_len;
89 syscall_comm.filter_expression_len = filter_expression_len;
90
91 ret = lttng_dynamic_buffer_append(
92 &payload->buffer, &syscall_comm, sizeof(syscall_comm));
93 if (ret) {
94 goto end;
95 }
96
97 ret = lttng_dynamic_buffer_append(
98 &payload->buffer, syscall->pattern, pattern_len);
99 if (ret) {
100 goto end;
101 }
102
103 ret = lttng_dynamic_buffer_append(&payload->buffer,
104 syscall->filter_expression, filter_expression_len);
105end:
106 return ret;
107}
108
109static bool lttng_event_rule_syscall_is_equal(const struct lttng_event_rule *_a,
110 const struct lttng_event_rule *_b)
111{
112 bool is_equal = false;
113 struct lttng_event_rule_syscall *a, *b;
114
115 a = container_of(_a, struct lttng_event_rule_syscall, parent);
116 b = container_of(_b, struct lttng_event_rule_syscall, parent);
117
118 if (!!a->filter_expression != !!b->filter_expression) {
119 goto end;
120 }
121
122 assert(a->pattern);
123 assert(b->pattern);
124 if (strcmp(a->pattern, b->pattern)) {
125 goto end;
126 }
127
128 if (a->filter_expression && b->filter_expression) {
129 if (strcmp(a->filter_expression, b->filter_expression)) {
130 goto end;
131 }
132 } else if (!!a->filter_expression != !!b->filter_expression) {
133 /* One is set and not the other. */
134 goto end;
135 }
136
137 is_equal = true;
138end:
139 return is_equal;
140}
141
142static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode(
58daac01
JR
143 struct lttng_event_rule *rule,
144 const struct lttng_credentials *creds)
e6a39346
JR
145{
146 int ret;
147 enum lttng_error_code ret_code = LTTNG_OK;
148 struct lttng_event_rule_syscall *syscall;
149 enum lttng_event_rule_status status;
150 const char *filter;
2b00d462 151 struct lttng_bytecode *bytecode = NULL;
e6a39346
JR
152
153 assert(rule);
154
155 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
156
157 /* Generate the filter bytecode. */
158 status = lttng_event_rule_syscall_get_filter(rule, &filter);
159 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
160 filter = NULL;
161 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
162 ret_code = LTTNG_ERR_FILTER_INVAL;
163 goto end;
164 }
165
166 if (filter && filter[0] == '\0') {
167 ret_code = LTTNG_ERR_FILTER_INVAL;
168 goto end;
169 }
170
171 if (filter == NULL) {
172 /* Nothing to do. */
173 ret = LTTNG_OK;
174 goto end;
175 }
176
177 syscall->internal_filter.filter = strdup(filter);
178 if (syscall->internal_filter.filter == NULL) {
179 ret_code = LTTNG_ERR_NOMEM;
180 goto end;
181 }
182
183 ret = run_as_generate_filter_bytecode(
58daac01 184 syscall->internal_filter.filter, creds, &bytecode);
e6a39346
JR
185 if (ret) {
186 ret_code = LTTNG_ERR_FILTER_INVAL;
187 }
188
189 syscall->internal_filter.bytecode = bytecode;
190 bytecode = NULL;
191
192end:
193 free(bytecode);
194 return ret_code;
195}
196
197static const char *lttng_event_rule_syscall_get_internal_filter(
198 const struct lttng_event_rule *rule)
199{
200 struct lttng_event_rule_syscall *syscall;
201
202 assert(rule);
203 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
204
205 return syscall->internal_filter.filter;
206}
207
2b00d462 208static const struct lttng_bytecode *
e6a39346
JR
209lttng_event_rule_syscall_get_internal_filter_bytecode(
210 const struct lttng_event_rule *rule)
211{
212 struct lttng_event_rule_syscall *syscall;
213
214 assert(rule);
215 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
216
217 return syscall->internal_filter.bytecode;
218}
219
993578ff
JR
220static enum lttng_event_rule_generate_exclusions_status
221lttng_event_rule_syscall_generate_exclusions(const struct lttng_event_rule *rule,
222 struct lttng_event_exclusion **exclusions)
e6a39346 223{
993578ff
JR
224 /* Unsupported. */
225 *exclusions = NULL;
226 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
e6a39346
JR
227}
228
959e3c66
JR
229static unsigned long
230lttng_event_rule_syscall_hash(
231 const struct lttng_event_rule *rule)
232{
233 unsigned long hash;
234 struct lttng_event_rule_syscall *syscall_rule =
235 container_of(rule, typeof(*syscall_rule), parent);
236
237 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_SYSCALL,
238 lttng_ht_seed);
239 hash ^= hash_key_str(syscall_rule->pattern, lttng_ht_seed);
240 if (syscall_rule->filter_expression) {
241 hash ^= hash_key_str(syscall_rule->filter_expression,
242 lttng_ht_seed);
243 }
244
245 return hash;
246}
247
a3ecaea8 248struct lttng_event_rule *lttng_event_rule_syscall_create(void)
e6a39346
JR
249{
250 struct lttng_event_rule *rule = NULL;
251 struct lttng_event_rule_syscall *syscall_rule;
252
253 syscall_rule = zmalloc(sizeof(struct lttng_event_rule_syscall));
254 if (!syscall_rule) {
255 goto end;
256 }
257
258 rule = &syscall_rule->parent;
259 lttng_event_rule_init(
260 &syscall_rule->parent, LTTNG_EVENT_RULE_TYPE_SYSCALL);
261 syscall_rule->parent.validate = lttng_event_rule_syscall_validate;
262 syscall_rule->parent.serialize = lttng_event_rule_syscall_serialize;
263 syscall_rule->parent.equal = lttng_event_rule_syscall_is_equal;
264 syscall_rule->parent.destroy = lttng_event_rule_syscall_destroy;
265 syscall_rule->parent.generate_filter_bytecode =
266 lttng_event_rule_syscall_generate_filter_bytecode;
267 syscall_rule->parent.get_filter =
268 lttng_event_rule_syscall_get_internal_filter;
269 syscall_rule->parent.get_filter_bytecode =
270 lttng_event_rule_syscall_get_internal_filter_bytecode;
271 syscall_rule->parent.generate_exclusions =
272 lttng_event_rule_syscall_generate_exclusions;
959e3c66 273 syscall_rule->parent.hash = lttng_event_rule_syscall_hash;
e6a39346
JR
274end:
275 return rule;
276}
277
278LTTNG_HIDDEN
279ssize_t lttng_event_rule_syscall_create_from_payload(
280 struct lttng_payload_view *view,
281 struct lttng_event_rule **_event_rule)
282{
283 ssize_t ret, offset = 0;
284 enum lttng_event_rule_status status;
285 const struct lttng_event_rule_syscall_comm *syscall_comm;
286 const char *pattern;
287 const char *filter_expression = NULL;
288 struct lttng_buffer_view current_buffer_view;
289 struct lttng_event_rule *rule = NULL;
290
291 if (!_event_rule) {
292 ret = -1;
293 goto end;
294 }
295
296 if (view->buffer.size < sizeof(*syscall_comm)) {
297 ERR("Failed to initialize from malformed event rule syscall: buffer too short to contain header");
298 ret = -1;
299 goto end;
300 }
301
302 current_buffer_view = lttng_buffer_view_from_view(
303 &view->buffer, offset, sizeof(*syscall_comm));
3e6e0df2 304 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
e6a39346
JR
305 ret = -1;
306 goto end;
307 }
308
3e6e0df2 309 syscall_comm = (typeof(syscall_comm)) current_buffer_view.data;
e6a39346
JR
310 rule = lttng_event_rule_syscall_create();
311 if (!rule) {
312 ERR("Failed to create event rule syscall");
313 ret = -1;
314 goto end;
315 }
316
317 /* Skip to payload. */
318 offset += current_buffer_view.size;
319
320 /* Map the pattern. */
321 current_buffer_view = lttng_buffer_view_from_view(
322 &view->buffer, offset, syscall_comm->pattern_len);
3e6e0df2 323 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
e6a39346
JR
324 ret = -1;
325 goto end;
326 }
327
3e6e0df2 328 pattern = current_buffer_view.data;
e6a39346
JR
329 if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
330 syscall_comm->pattern_len)) {
331 ret = -1;
332 goto end;
333 }
334
335 /* Skip after the pattern. */
336 offset += syscall_comm->pattern_len;
337
338 if (!syscall_comm->filter_expression_len) {
339 goto skip_filter_expression;
340 }
341
342 /* Map the filter_expression. */
343 current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
344 syscall_comm->filter_expression_len);
3e6e0df2 345 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
e6a39346
JR
346 ret = -1;
347 goto end;
348 }
349
3e6e0df2 350 filter_expression = current_buffer_view.data;
e6a39346
JR
351 if (!lttng_buffer_view_contains_string(&current_buffer_view,
352 filter_expression,
353 syscall_comm->filter_expression_len)) {
354 ret = -1;
355 goto end;
356 }
357
358 /* Skip after the pattern. */
359 offset += syscall_comm->filter_expression_len;
360
361skip_filter_expression:
362
363 status = lttng_event_rule_syscall_set_pattern(rule, pattern);
364 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
365 ERR("Failed to set event rule syscall pattern");
366 ret = -1;
367 goto end;
368 }
369
370 if (filter_expression) {
371 status = lttng_event_rule_syscall_set_filter(
372 rule, filter_expression);
373 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
374 ERR("Failed to set event rule syscall pattern");
375 ret = -1;
376 goto end;
377 }
378 }
379
380 *_event_rule = rule;
381 rule = NULL;
382 ret = offset;
383end:
384 lttng_event_rule_destroy(rule);
385 return ret;
386}
387
388enum lttng_event_rule_status lttng_event_rule_syscall_set_pattern(
389 struct lttng_event_rule *rule, const char *pattern)
390{
391 char *pattern_copy = NULL;
392 struct lttng_event_rule_syscall *syscall;
393 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
394
395 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern ||
396 strlen(pattern) == 0) {
397 status = LTTNG_EVENT_RULE_STATUS_INVALID;
398 goto end;
399 }
400
401 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
402 pattern_copy = strdup(pattern);
403 if (!pattern_copy) {
404 status = LTTNG_EVENT_RULE_STATUS_ERROR;
405 goto end;
406 }
407
408 if (syscall->pattern) {
409 free(syscall->pattern);
410 }
411
412 syscall->pattern = pattern_copy;
413 pattern_copy = NULL;
414end:
415 return status;
416}
417
418enum lttng_event_rule_status lttng_event_rule_syscall_get_pattern(
419 const struct lttng_event_rule *rule, const char **pattern)
420{
421 struct lttng_event_rule_syscall *syscall;
422 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
423
424 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern) {
425 status = LTTNG_EVENT_RULE_STATUS_INVALID;
426 goto end;
427 }
428
429 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
430 if (!syscall->pattern) {
431 status = LTTNG_EVENT_RULE_STATUS_UNSET;
432 goto end;
433 }
434
435 *pattern = syscall->pattern;
436end:
437 return status;
438}
439
440enum lttng_event_rule_status lttng_event_rule_syscall_set_filter(
441 struct lttng_event_rule *rule, const char *expression)
442{
443 char *expression_copy = NULL;
444 struct lttng_event_rule_syscall *syscall;
445 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
446
447 /* TODO: validate that the passed expression is valid. */
448
449 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression ||
450 strlen(expression) == 0) {
451 status = LTTNG_EVENT_RULE_STATUS_INVALID;
452 goto end;
453 }
454
455 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
456 expression_copy = strdup(expression);
457 if (!expression_copy) {
458 status = LTTNG_EVENT_RULE_STATUS_ERROR;
459 goto end;
460 }
461
462 if (syscall->filter_expression) {
463 free(syscall->filter_expression);
464 }
465
466 syscall->filter_expression = expression_copy;
467 expression_copy = NULL;
468end:
469 return status;
470}
471
472enum lttng_event_rule_status lttng_event_rule_syscall_get_filter(
473 const struct lttng_event_rule *rule, const char **expression)
474{
475 struct lttng_event_rule_syscall *syscall;
476 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
477
478 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression) {
479 status = LTTNG_EVENT_RULE_STATUS_INVALID;
480 goto end;
481 }
482
483 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
484 if (!syscall->filter_expression) {
485 status = LTTNG_EVENT_RULE_STATUS_UNSET;
486 goto end;
487 }
488
489 *expression = syscall->filter_expression;
490end:
491 return status;
492}
This page took 0.065056 seconds and 4 git commands to generate.