45b7eda257faae2807ac1c3489c2fb8ceef69703
[lttng-tools.git] / src / common / event-rule / user-tracepoint.c
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>
9 #include <common/credentials.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/optional.h>
13 #include <common/payload.h>
14 #include <common/payload-view.h>
15 #include <common/runas.h>
16 #include <common/hashtable/hashtable.h>
17 #include <common/hashtable/utils.h>
18 #include <common/string-utils/string-utils.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/event-rule/user-tracepoint-internal.h>
21 #include <lttng/log-level-rule.h>
22 #include <lttng/event.h>
23
24 #define IS_USER_TRACEPOINT_EVENT_RULE(rule) \
25 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT)
26
27 static void lttng_event_rule_user_tracepoint_destroy(struct lttng_event_rule *rule)
28 {
29 struct lttng_event_rule_user_tracepoint *tracepoint;
30
31 if (rule == NULL) {
32 return;
33 }
34
35 tracepoint = container_of(
36 rule, struct lttng_event_rule_user_tracepoint, parent);
37
38 lttng_log_level_rule_destroy(tracepoint->log_level_rule);
39 lttng_dynamic_pointer_array_reset(&tracepoint->exclusions);
40 free(tracepoint->pattern);
41 free(tracepoint->filter_expression);
42 free(tracepoint->internal_filter.filter);
43 free(tracepoint->internal_filter.bytecode);
44 free(tracepoint);
45 }
46
47 static bool lttng_event_rule_user_tracepoint_validate(
48 const struct lttng_event_rule *rule)
49 {
50 bool valid = false;
51 struct lttng_event_rule_user_tracepoint *tracepoint;
52
53 if (!rule) {
54 goto end;
55 }
56
57 tracepoint = container_of(
58 rule, struct lttng_event_rule_user_tracepoint, parent);
59
60 /* Required field. */
61 if (!tracepoint->pattern) {
62 ERR("Invalid user tracepoint event rule: a pattern must be set.");
63 goto end;
64 }
65
66 valid = true;
67 end:
68 return valid;
69 }
70
71 static int lttng_event_rule_user_tracepoint_serialize(
72 const struct lttng_event_rule *rule,
73 struct lttng_payload *payload)
74 {
75 int ret, i;
76 size_t pattern_len, filter_expression_len, exclusions_len, header_offset;
77 size_t size_before_log_level_rule;
78 struct lttng_event_rule_user_tracepoint *tracepoint;
79 struct lttng_event_rule_user_tracepoint_comm tracepoint_comm;
80 enum lttng_event_rule_status status;
81 unsigned int exclusion_count;
82 size_t exclusions_appended_len = 0;
83 struct lttng_event_rule_user_tracepoint_comm *header;
84
85 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule)) {
86 ret = -1;
87 goto end;
88 }
89
90 header_offset = payload->buffer.size;
91
92 DBG("Serializing user tracepoint event rule.");
93 tracepoint = container_of(
94 rule, struct lttng_event_rule_user_tracepoint, parent);
95
96 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule, &exclusion_count);
97 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
98
99 pattern_len = strlen(tracepoint->pattern) + 1;
100
101 if (tracepoint->filter_expression != NULL) {
102 filter_expression_len =
103 strlen(tracepoint->filter_expression) + 1;
104 } else {
105 filter_expression_len = 0;
106 }
107
108 exclusions_len = 0;
109 for (i = 0; i < exclusion_count; i++) {
110 const char *exclusion;
111
112 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
113 rule, i, &exclusion);
114 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
115
116 /* Length field. */
117 exclusions_len += sizeof(uint32_t);
118 /* Payload (null terminated). */
119 exclusions_len += strlen(exclusion) + 1;
120 }
121
122 tracepoint_comm.pattern_len = pattern_len;
123 tracepoint_comm.filter_expression_len = filter_expression_len;
124 tracepoint_comm.exclusions_count = exclusion_count;
125 tracepoint_comm.exclusions_len = exclusions_len;
126
127 ret = lttng_dynamic_buffer_append(&payload->buffer, &tracepoint_comm,
128 sizeof(tracepoint_comm));
129 if (ret) {
130 goto end;
131 }
132
133 ret = lttng_dynamic_buffer_append(
134 &payload->buffer, tracepoint->pattern, pattern_len);
135 if (ret) {
136 goto end;
137 }
138
139 ret = lttng_dynamic_buffer_append(&payload->buffer, tracepoint->filter_expression,
140 filter_expression_len);
141 if (ret) {
142 goto end;
143 }
144
145 size_before_log_level_rule = payload->buffer.size;
146
147 ret = lttng_log_level_rule_serialize(tracepoint->log_level_rule, payload);
148 if (ret < 0) {
149 goto end;
150 }
151
152 header = (typeof(header)) ((char *) payload->buffer.data + header_offset);
153 header->log_level_rule_len =
154 payload->buffer.size - size_before_log_level_rule;
155
156 for (i = 0; i < exclusion_count; i++) {
157 size_t len;
158 const char *exclusion;
159
160 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
161 rule, i, &exclusion);
162 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
163
164 len = strlen(exclusion) + 1;
165 /* Append exclusion length, includes the null terminator. */
166 ret = lttng_dynamic_buffer_append(
167 &payload->buffer, &len, sizeof(uint32_t));
168 if (ret) {
169 goto end;
170 }
171
172 exclusions_appended_len += sizeof(uint32_t);
173
174 /* Include the '\0' in the payload. */
175 ret = lttng_dynamic_buffer_append(
176 &payload->buffer, exclusion, len);
177 if (ret) {
178 goto end;
179 }
180
181 exclusions_appended_len += len;
182 }
183
184 assert(exclusions_len == exclusions_appended_len);
185
186 end:
187 return ret;
188 }
189
190 static bool lttng_event_rule_user_tracepoint_is_equal(
191 const struct lttng_event_rule *_a,
192 const struct lttng_event_rule *_b)
193 {
194 int i;
195 bool is_equal = false;
196 struct lttng_event_rule_user_tracepoint *a, *b;
197 unsigned int count_a, count_b;
198 enum lttng_event_rule_status status;
199
200 a = container_of(_a, struct lttng_event_rule_user_tracepoint, parent);
201 b = container_of(_b, struct lttng_event_rule_user_tracepoint, parent);
202
203 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_a, &count_a);
204 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
205 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_b, &count_b);
206 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
207
208 /* Quick checks. */
209 if (count_a != count_b) {
210 goto end;
211 }
212
213 if (!!a->filter_expression != !!b->filter_expression) {
214 goto end;
215 }
216
217 /* Long check. */
218 assert(a->pattern);
219 assert(b->pattern);
220 if (strcmp(a->pattern, b->pattern)) {
221 goto end;
222 }
223
224 if (a->filter_expression && b->filter_expression) {
225 if (strcmp(a->filter_expression, b->filter_expression)) {
226 goto end;
227 }
228 } else if (!!a->filter_expression != !!b->filter_expression) {
229 /* One is set; not the other. */
230 goto end;
231 }
232
233 if (!lttng_log_level_rule_is_equal(
234 a->log_level_rule, b->log_level_rule)) {
235 goto end;
236 }
237
238 for (i = 0; i < count_a; i++) {
239 const char *exclusion_a, *exclusion_b;
240
241 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
242 _a, i, &exclusion_a);
243 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
244 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
245 _b, i, &exclusion_b);
246 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
247 if (strcmp(exclusion_a, exclusion_b)) {
248 goto end;
249 }
250 }
251
252 is_equal = true;
253 end:
254 return is_equal;
255 }
256
257 static enum lttng_error_code
258 lttng_event_rule_user_tracepoint_generate_filter_bytecode(
259 struct lttng_event_rule *rule,
260 const struct lttng_credentials *creds)
261 {
262 int ret;
263 enum lttng_error_code ret_code;
264 struct lttng_event_rule_user_tracepoint *tracepoint;
265 enum lttng_event_rule_status status;
266 const char *filter;
267 struct lttng_bytecode *bytecode = NULL;
268
269 assert(rule);
270
271 tracepoint = container_of(
272 rule, struct lttng_event_rule_user_tracepoint, parent);
273
274 status = lttng_event_rule_user_tracepoint_get_filter(rule, &filter);
275 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
276 filter = NULL;
277 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
278 ret_code = LTTNG_ERR_FILTER_INVAL;
279 goto end;
280 }
281
282 if (filter && filter[0] == '\0') {
283 ret_code = LTTNG_ERR_FILTER_INVAL;
284 goto error;
285 }
286
287 if (filter) {
288 tracepoint->internal_filter.filter = strdup(filter);
289 if (tracepoint->internal_filter.filter == NULL) {
290 ret_code = LTTNG_ERR_NOMEM;
291 goto error;
292 }
293 } else {
294 tracepoint->internal_filter.filter = NULL;
295 }
296
297 if (tracepoint->internal_filter.filter == NULL) {
298 ret_code = LTTNG_OK;
299 goto end;
300 }
301
302 ret = run_as_generate_filter_bytecode(
303 tracepoint->internal_filter.filter, creds,
304 &bytecode);
305 if (ret) {
306 ret_code = LTTNG_ERR_FILTER_INVAL;
307 goto end;
308 }
309
310 tracepoint->internal_filter.bytecode = bytecode;
311 bytecode = NULL;
312 ret_code = LTTNG_OK;
313
314 error:
315 end:
316 free(bytecode);
317 return ret_code;
318 }
319
320 static const char *lttng_event_rule_user_tracepoint_get_internal_filter(
321 const struct lttng_event_rule *rule)
322 {
323 struct lttng_event_rule_user_tracepoint *tracepoint;
324
325 assert(rule);
326 tracepoint = container_of(
327 rule, struct lttng_event_rule_user_tracepoint, parent);
328 return tracepoint->internal_filter.filter;
329 }
330
331 static const struct lttng_bytecode *
332 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode(
333 const struct lttng_event_rule *rule)
334 {
335 struct lttng_event_rule_user_tracepoint *tracepoint;
336
337 assert(rule);
338 tracepoint = container_of(
339 rule, struct lttng_event_rule_user_tracepoint, parent);
340 return tracepoint->internal_filter.bytecode;
341 }
342
343 static enum lttng_event_rule_generate_exclusions_status
344 lttng_event_rule_user_tracepoint_generate_exclusions(
345 const struct lttng_event_rule *rule,
346 struct lttng_event_exclusion **_exclusions)
347 {
348 unsigned int nb_exclusions = 0, i;
349 struct lttng_event_exclusion *exclusions;
350 enum lttng_event_rule_status event_rule_status;
351 enum lttng_event_rule_generate_exclusions_status ret_status;
352
353 assert(_exclusions);
354
355 event_rule_status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
356 rule, &nb_exclusions);
357 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
358 if (nb_exclusions == 0) {
359 /* Nothing to do. */
360 exclusions = NULL;
361 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
362 goto end;
363 }
364
365 exclusions = zmalloc(sizeof(struct lttng_event_exclusion) +
366 (LTTNG_SYMBOL_NAME_LEN * nb_exclusions));
367 if (!exclusions) {
368 PERROR("Failed to allocate exclusions buffer");
369 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OUT_OF_MEMORY;
370 goto end;
371 }
372
373 exclusions->count = nb_exclusions;
374 for (i = 0; i < nb_exclusions; i++) {
375 int copy_ret;
376 const char *exclusion_str;
377
378 event_rule_status =
379 lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
380 rule, i, &exclusion_str);
381 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
382
383 copy_ret = lttng_strncpy(exclusions->names[i], exclusion_str,
384 LTTNG_SYMBOL_NAME_LEN);
385 if (copy_ret) {
386 free(exclusions);
387 exclusions = NULL;
388 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_ERROR;
389 goto end;
390 }
391 }
392
393 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK;
394
395 end:
396 *_exclusions = exclusions;
397 return ret_status;
398 }
399
400 static void destroy_lttng_exclusions_element(void *ptr)
401 {
402 free(ptr);
403 }
404
405 static unsigned long lttng_event_rule_user_tracepoint_hash(
406 const struct lttng_event_rule *rule)
407 {
408 unsigned long hash;
409 unsigned int i, exclusion_count;
410 enum lttng_event_rule_status status;
411 struct lttng_event_rule_user_tracepoint *tp_rule =
412 container_of(rule, typeof(*tp_rule), parent);
413
414 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT,
415 lttng_ht_seed);
416 hash ^= hash_key_str(tp_rule->pattern, lttng_ht_seed);
417
418 if (tp_rule->filter_expression) {
419 hash ^= hash_key_str(tp_rule->filter_expression, lttng_ht_seed);
420 }
421
422 if (tp_rule->log_level_rule) {
423 hash ^= lttng_log_level_rule_hash(tp_rule->log_level_rule);
424 }
425
426 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule,
427 &exclusion_count);
428 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
429
430 for (i = 0; i < exclusion_count; i++) {
431 const char *exclusion;
432
433 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
434 rule, i, &exclusion);
435 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
436 hash ^= hash_key_str(exclusion, lttng_ht_seed);
437 }
438
439 return hash;
440 }
441
442 struct lttng_event_rule *lttng_event_rule_user_tracepoint_create(void)
443 {
444 struct lttng_event_rule *rule = NULL;
445 struct lttng_event_rule_user_tracepoint *tp_rule;
446 enum lttng_event_rule_status status;
447
448 tp_rule = zmalloc(sizeof(struct lttng_event_rule_user_tracepoint));
449 if (!tp_rule) {
450 goto end;
451 }
452
453 rule = &tp_rule->parent;
454 lttng_event_rule_init(&tp_rule->parent, LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT);
455 tp_rule->parent.validate = lttng_event_rule_user_tracepoint_validate;
456 tp_rule->parent.serialize = lttng_event_rule_user_tracepoint_serialize;
457 tp_rule->parent.equal = lttng_event_rule_user_tracepoint_is_equal;
458 tp_rule->parent.destroy = lttng_event_rule_user_tracepoint_destroy;
459 tp_rule->parent.generate_filter_bytecode =
460 lttng_event_rule_user_tracepoint_generate_filter_bytecode;
461 tp_rule->parent.get_filter =
462 lttng_event_rule_user_tracepoint_get_internal_filter;
463 tp_rule->parent.get_filter_bytecode =
464 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode;
465 tp_rule->parent.generate_exclusions =
466 lttng_event_rule_user_tracepoint_generate_exclusions;
467 tp_rule->parent.hash = lttng_event_rule_user_tracepoint_hash;
468
469 /* Not necessary for now. */
470 tp_rule->parent.generate_lttng_event = NULL;
471
472 tp_rule->log_level_rule = NULL;
473
474 lttng_dynamic_pointer_array_init(&tp_rule->exclusions,
475 destroy_lttng_exclusions_element);
476
477 /* Default pattern is '*'. */
478 status = lttng_event_rule_user_tracepoint_set_name_pattern(rule, "*");
479 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
480 lttng_event_rule_destroy(rule);
481 rule = NULL;
482 }
483
484 end:
485 return rule;
486 }
487
488 LTTNG_HIDDEN
489 ssize_t lttng_event_rule_user_tracepoint_create_from_payload(
490 struct lttng_payload_view *view,
491 struct lttng_event_rule **_event_rule)
492 {
493 ssize_t ret, offset = 0;
494 int i;
495 enum lttng_event_rule_status status;
496 const struct lttng_event_rule_user_tracepoint_comm *tracepoint_comm;
497 const char *pattern;
498 const char *filter_expression = NULL;
499 const char **exclusions = NULL;
500 const uint32_t *exclusion_len;
501 const char *exclusion;
502 struct lttng_buffer_view current_buffer_view;
503 struct lttng_event_rule *rule = NULL;
504 struct lttng_log_level_rule *log_level_rule = NULL;
505
506 if (!_event_rule) {
507 ret = -1;
508 goto end;
509 }
510
511 current_buffer_view = lttng_buffer_view_from_view(
512 &view->buffer, offset, sizeof(*tracepoint_comm));
513 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
514 ERR("Failed to initialize from malformed event rule tracepoint: buffer too short to contain header.");
515 ret = -1;
516 goto end;
517 }
518
519 tracepoint_comm = (typeof(tracepoint_comm)) current_buffer_view.data;
520
521 rule = lttng_event_rule_user_tracepoint_create();
522 if (!rule) {
523 ERR("Failed to create event rule user tracepoint.");
524 ret = -1;
525 goto end;
526 }
527
528 /* Skip to payload. */
529 offset += current_buffer_view.size;
530
531 /* Map the pattern. */
532 current_buffer_view = lttng_buffer_view_from_view(
533 &view->buffer, offset, tracepoint_comm->pattern_len);
534
535 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
536 ret = -1;
537 goto end;
538 }
539
540 pattern = current_buffer_view.data;
541 if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
542 tracepoint_comm->pattern_len)) {
543 ret = -1;
544 goto end;
545 }
546
547 /* Skip after the pattern. */
548 offset += tracepoint_comm->pattern_len;
549
550 if (!tracepoint_comm->filter_expression_len) {
551 goto skip_filter_expression;
552 }
553
554 /* Map the filter_expression. */
555 current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
556 tracepoint_comm->filter_expression_len);
557 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
558 ret = -1;
559 goto end;
560 }
561
562 filter_expression = current_buffer_view.data;
563 if (!lttng_buffer_view_contains_string(&current_buffer_view,
564 filter_expression,
565 tracepoint_comm->filter_expression_len)) {
566 ret = -1;
567 goto end;
568 }
569
570 /* Skip after the pattern. */
571 offset += tracepoint_comm->filter_expression_len;
572
573 skip_filter_expression:
574 if (!tracepoint_comm->log_level_rule_len) {
575 goto skip_log_level_rule;
576 }
577
578 {
579 /* Map the log level rule. */
580 struct lttng_payload_view current_payload_view =
581 lttng_payload_view_from_view(view, offset,
582 tracepoint_comm->log_level_rule_len);
583
584 ret = lttng_log_level_rule_create_from_payload(
585 &current_payload_view, &log_level_rule);
586 if (ret < 0) {
587 ret = -1;
588 goto end;
589 }
590
591 assert(ret == tracepoint_comm->log_level_rule_len);
592 }
593
594 /* Skip after the log level rule. */
595 offset += tracepoint_comm->log_level_rule_len;
596
597 skip_log_level_rule:
598 for (i = 0; i < tracepoint_comm->exclusions_count; i++) {
599 current_buffer_view = lttng_buffer_view_from_view(
600 &view->buffer, offset, sizeof(*exclusion_len));
601 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
602 ret = -1;
603 goto end;
604 }
605
606 exclusion_len = (typeof(exclusion_len)) current_buffer_view.data;
607 offset += sizeof(*exclusion_len);
608
609 current_buffer_view = lttng_buffer_view_from_view(
610 &view->buffer, offset, *exclusion_len);
611 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
612 ret = -1;
613 goto end;
614 }
615
616 exclusion = current_buffer_view.data;
617 if (!lttng_buffer_view_contains_string(&current_buffer_view,
618 exclusion, *exclusion_len)) {
619 ret = -1;
620 goto end;
621 }
622
623 status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(rule, exclusion);
624 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
625 ERR("Failed to add event rule user tracepoint exclusion \"%s\".",
626 exclusion);
627 ret = -1;
628 goto end;
629 }
630
631 /* Skip to next exclusion. */
632 offset += *exclusion_len;
633 }
634
635 status = lttng_event_rule_user_tracepoint_set_name_pattern(rule, pattern);
636 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
637 ERR("Failed to set event rule user tracepoint pattern.");
638 ret = -1;
639 goto end;
640 }
641
642 if (filter_expression) {
643 status = lttng_event_rule_user_tracepoint_set_filter(
644 rule, filter_expression);
645 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
646 ERR("Failed to set event rule user tracepoint pattern.");
647 ret = -1;
648 goto end;
649 }
650 }
651
652 if (log_level_rule) {
653 status = lttng_event_rule_user_tracepoint_set_log_level_rule(
654 rule, log_level_rule);
655 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
656 ERR("Failed to set event rule user tracepoint log level rule.");
657 ret = -1;
658 goto end;
659 }
660 }
661
662 *_event_rule = rule;
663 rule = NULL;
664 ret = offset;
665 end:
666 free(exclusions);
667 lttng_log_level_rule_destroy(log_level_rule);
668 lttng_event_rule_destroy(rule);
669 return ret;
670 }
671
672 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_name_pattern(
673 struct lttng_event_rule *rule, const char *pattern)
674 {
675 char *pattern_copy = NULL;
676 struct lttng_event_rule_user_tracepoint *tracepoint;
677 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
678
679 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !pattern ||
680 strlen(pattern) == 0) {
681 status = LTTNG_EVENT_RULE_STATUS_INVALID;
682 goto end;
683 }
684
685 tracepoint = container_of(
686 rule, struct lttng_event_rule_user_tracepoint, parent);
687 pattern_copy = strdup(pattern);
688 if (!pattern_copy) {
689 status = LTTNG_EVENT_RULE_STATUS_ERROR;
690 goto end;
691 }
692
693 /* Normalize the pattern. */
694 strutils_normalize_star_glob_pattern(pattern_copy);
695
696 free(tracepoint->pattern);
697
698 tracepoint->pattern = pattern_copy;
699 pattern_copy = NULL;
700 end:
701 return status;
702 }
703
704 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern(
705 const struct lttng_event_rule *rule, const char **pattern)
706 {
707 struct lttng_event_rule_user_tracepoint *tracepoint;
708 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
709
710 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !pattern) {
711 status = LTTNG_EVENT_RULE_STATUS_INVALID;
712 goto end;
713 }
714
715 tracepoint = container_of(
716 rule, struct lttng_event_rule_user_tracepoint, parent);
717 if (!tracepoint->pattern) {
718 status = LTTNG_EVENT_RULE_STATUS_UNSET;
719 goto end;
720 }
721
722 *pattern = tracepoint->pattern;
723 end:
724 return status;
725 }
726
727 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_filter(
728 struct lttng_event_rule *rule, const char *expression)
729 {
730 char *expression_copy = NULL;
731 struct lttng_event_rule_user_tracepoint *tracepoint;
732 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
733
734 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !expression ||
735 strlen(expression) == 0) {
736 status = LTTNG_EVENT_RULE_STATUS_INVALID;
737 goto end;
738 }
739
740 tracepoint = container_of(
741 rule, struct lttng_event_rule_user_tracepoint, parent);
742 expression_copy = strdup(expression);
743 if (!expression_copy) {
744 PERROR("Failed to copy filter expression");
745 status = LTTNG_EVENT_RULE_STATUS_ERROR;
746 goto end;
747 }
748
749 if (tracepoint->filter_expression) {
750 free(tracepoint->filter_expression);
751 }
752
753 tracepoint->filter_expression = expression_copy;
754 expression_copy = NULL;
755 end:
756 return status;
757 }
758
759 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_filter(
760 const struct lttng_event_rule *rule, const char **expression)
761 {
762 struct lttng_event_rule_user_tracepoint *tracepoint;
763 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
764
765 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !expression) {
766 status = LTTNG_EVENT_RULE_STATUS_INVALID;
767 goto end;
768 }
769
770 tracepoint = container_of(
771 rule, struct lttng_event_rule_user_tracepoint, parent);
772 if (!tracepoint->filter_expression) {
773 status = LTTNG_EVENT_RULE_STATUS_UNSET;
774 goto end;
775 }
776
777 *expression = tracepoint->filter_expression;
778 end:
779 return status;
780 }
781
782 static bool log_level_rule_valid(const struct lttng_log_level_rule *rule)
783 {
784 bool valid = false;
785 enum lttng_log_level_rule_status status;
786 int level;
787
788 switch (lttng_log_level_rule_get_type(rule)) {
789 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
790 status = lttng_log_level_rule_exactly_get_level(rule, &level);
791 break;
792 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
793 status = lttng_log_level_rule_at_least_as_severe_as_get_level(
794 rule, &level);
795 break;
796 default:
797 abort();
798 }
799
800 assert(status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
801
802 if (level < LTTNG_LOGLEVEL_EMERG) {
803 /* Invalid. */
804 goto end;
805 }
806 if (level > LTTNG_LOGLEVEL_DEBUG) {
807 /* Invalid. */
808 goto end;
809 }
810
811 valid = true;
812
813 end:
814 return valid;
815 }
816
817 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_log_level_rule(
818 struct lttng_event_rule *rule,
819 const struct lttng_log_level_rule *log_level_rule)
820 {
821 struct lttng_event_rule_user_tracepoint *tracepoint;
822 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
823 struct lttng_log_level_rule *copy = NULL;
824
825 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule)) {
826 status = LTTNG_EVENT_RULE_STATUS_INVALID;
827 goto end;
828 }
829
830 tracepoint = container_of(
831 rule, struct lttng_event_rule_user_tracepoint, parent);
832
833 if (!log_level_rule_valid(log_level_rule)) {
834 status = LTTNG_EVENT_RULE_STATUS_INVALID;
835 goto end;
836 }
837
838 copy = lttng_log_level_rule_copy(log_level_rule);
839 if (copy == NULL) {
840 status = LTTNG_EVENT_RULE_STATUS_ERROR;
841 goto end;
842 }
843
844 if (tracepoint->log_level_rule) {
845 lttng_log_level_rule_destroy(tracepoint->log_level_rule);
846 }
847
848 tracepoint->log_level_rule = copy;
849
850 end:
851 return status;
852 }
853
854 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_log_level_rule(
855 const struct lttng_event_rule *rule,
856 const struct lttng_log_level_rule **log_level_rule
857 )
858 {
859 struct lttng_event_rule_user_tracepoint *tracepoint;
860 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
861
862 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !log_level_rule) {
863 status = LTTNG_EVENT_RULE_STATUS_INVALID;
864 goto end;
865 }
866
867 tracepoint = container_of(
868 rule, struct lttng_event_rule_user_tracepoint, parent);
869 if (tracepoint->log_level_rule == NULL) {
870 status = LTTNG_EVENT_RULE_STATUS_UNSET;
871 goto end;
872 }
873
874 *log_level_rule = tracepoint->log_level_rule;
875 end:
876 return status;
877 }
878
879 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
880 struct lttng_event_rule *rule,
881 const char *exclusion)
882 {
883 int ret;
884 char *exclusion_copy = NULL;
885 struct lttng_event_rule_user_tracepoint *tracepoint;
886 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
887
888 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) ||
889 !exclusion) {
890 status = LTTNG_EVENT_RULE_STATUS_INVALID;
891 goto end;
892 }
893
894 tracepoint = container_of(
895 rule, struct lttng_event_rule_user_tracepoint, parent);
896
897 if (strlen(exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
898 status = LTTNG_EVENT_RULE_STATUS_INVALID;
899 goto end;
900 }
901
902 exclusion_copy = strdup(exclusion);
903 if (!exclusion_copy) {
904 status = LTTNG_EVENT_RULE_STATUS_ERROR;
905 goto end;
906 }
907
908 ret = lttng_dynamic_pointer_array_add_pointer(&tracepoint->exclusions,
909 exclusion_copy);
910 if (ret < 0) {
911 status = LTTNG_EVENT_RULE_STATUS_ERROR;
912 goto end;
913 }
914
915 exclusion_copy = NULL;
916 end:
917 free(exclusion_copy);
918 return status;
919 }
920
921 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
922 const struct lttng_event_rule *rule, unsigned int *count)
923 {
924 struct lttng_event_rule_user_tracepoint *tracepoint;
925 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
926
927 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !count) {
928 status = LTTNG_EVENT_RULE_STATUS_INVALID;
929 goto end;
930 }
931
932 tracepoint = container_of(
933 rule, struct lttng_event_rule_user_tracepoint, parent);
934 *count = lttng_dynamic_pointer_array_get_count(&tracepoint->exclusions);
935 end:
936 return status;
937 }
938
939 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
940 const struct lttng_event_rule *rule,
941 unsigned int index,
942 const char **exclusion)
943 {
944 unsigned int count;
945 struct lttng_event_rule_user_tracepoint *tracepoint;
946 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
947
948 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !exclusion) {
949 status = LTTNG_EVENT_RULE_STATUS_INVALID;
950 goto end;
951 }
952
953 tracepoint = container_of(
954 rule, struct lttng_event_rule_user_tracepoint, parent);
955 if (lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule, &count) !=
956 LTTNG_EVENT_RULE_STATUS_OK) {
957 goto end;
958 }
959
960 if (index >= count) {
961 goto end;
962 }
963
964 *exclusion = lttng_dynamic_pointer_array_get_pointer(
965 &tracepoint->exclusions, index);
966 end:
967 return status;
968 }
This page took 0.050271 seconds and 4 git commands to generate.