2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/credentials.h>
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>
15 #include <common/hashtable/hashtable.h>
16 #include <common/hashtable/utils.h>
17 #include <lttng/event-rule/event-rule-internal.h>
18 #include <lttng/event-rule/tracepoint-internal.h>
20 #define IS_TRACEPOINT_EVENT_RULE(rule) \
21 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT)
23 static void lttng_event_rule_tracepoint_destroy(struct lttng_event_rule
*rule
)
25 struct lttng_event_rule_tracepoint
*tracepoint
;
31 tracepoint
= container_of(
32 rule
, struct lttng_event_rule_tracepoint
, parent
);
34 lttng_dynamic_pointer_array_reset(&tracepoint
->exclusions
);
35 free(tracepoint
->pattern
);
36 free(tracepoint
->filter_expression
);
37 free(tracepoint
->internal_filter
.filter
);
38 free(tracepoint
->internal_filter
.bytecode
);
42 static bool lttng_event_rule_tracepoint_validate(
43 const struct lttng_event_rule
*rule
)
46 struct lttng_event_rule_tracepoint
*tracepoint
;
52 tracepoint
= container_of(
53 rule
, struct lttng_event_rule_tracepoint
, parent
);
56 if (!tracepoint
->pattern
) {
57 ERR("Invalid tracepoint event rule: a pattern must be set.");
62 if (tracepoint
->domain
== LTTNG_DOMAIN_NONE
) {
63 ERR("Invalid tracepoint event rule: a domain must be set.");
72 static int lttng_event_rule_tracepoint_serialize(
73 const struct lttng_event_rule
*rule
,
74 struct lttng_payload
*payload
)
77 size_t pattern_len
, filter_expression_len
, exclusions_len
;
78 struct lttng_event_rule_tracepoint
*tracepoint
;
79 struct lttng_event_rule_tracepoint_comm tracepoint_comm
;
80 enum lttng_event_rule_status status
;
81 unsigned int exclusion_count
;
82 size_t exclusions_appended_len
= 0;
84 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
)) {
89 DBG("Serializing tracepoint event rule.");
90 tracepoint
= container_of(
91 rule
, struct lttng_event_rule_tracepoint
, parent
);
93 status
= lttng_event_rule_tracepoint_get_exclusions_count(rule
, &exclusion_count
);
94 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
96 pattern_len
= strlen(tracepoint
->pattern
) + 1;
98 if (tracepoint
->filter_expression
!= NULL
) {
99 filter_expression_len
=
100 strlen(tracepoint
->filter_expression
) + 1;
102 filter_expression_len
= 0;
106 for (i
= 0; i
< exclusion_count
; i
++) {
107 const char *exclusion
;
109 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
110 rule
, i
, &exclusion
);
111 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
114 exclusions_len
+= sizeof(uint32_t);
115 /* Payload (null terminated). */
116 exclusions_len
+= strlen(exclusion
) + 1;
119 tracepoint_comm
.domain_type
= (int8_t) tracepoint
->domain
;
120 tracepoint_comm
.loglevel_type
= (int8_t) tracepoint
->loglevel
.type
;
121 tracepoint_comm
.loglevel_value
= tracepoint
->loglevel
.value
;
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
;
127 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &tracepoint_comm
,
128 sizeof(tracepoint_comm
));
133 ret
= lttng_dynamic_buffer_append(
134 &payload
->buffer
, tracepoint
->pattern
, pattern_len
);
139 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, tracepoint
->filter_expression
,
140 filter_expression_len
);
145 for (i
= 0; i
< exclusion_count
; i
++) {
147 const char *exclusion
;
149 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
150 rule
, i
, &exclusion
);
151 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
153 len
= strlen(exclusion
) + 1;
154 /* Append exclusion length, includes the null terminator. */
155 ret
= lttng_dynamic_buffer_append(
156 &payload
->buffer
, &len
, sizeof(uint32_t));
161 exclusions_appended_len
+= sizeof(uint32_t);
163 /* Include the '\0' in the payload. */
164 ret
= lttng_dynamic_buffer_append(
165 &payload
->buffer
, exclusion
, len
);
170 exclusions_appended_len
+= len
;
173 assert(exclusions_len
== exclusions_appended_len
);
179 static bool lttng_event_rule_tracepoint_is_equal(
180 const struct lttng_event_rule
*_a
,
181 const struct lttng_event_rule
*_b
)
184 bool is_equal
= false;
185 struct lttng_event_rule_tracepoint
*a
, *b
;
186 unsigned int count_a
, count_b
;
187 enum lttng_event_rule_status status
;
189 a
= container_of(_a
, struct lttng_event_rule_tracepoint
, parent
);
190 b
= container_of(_b
, struct lttng_event_rule_tracepoint
, parent
);
192 status
= lttng_event_rule_tracepoint_get_exclusions_count(_a
, &count_a
);
193 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
194 status
= lttng_event_rule_tracepoint_get_exclusions_count(_b
, &count_b
);
195 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
198 if (a
->domain
!= b
->domain
) {
202 if (count_a
!= count_b
) {
206 if (!!a
->filter_expression
!= !!b
->filter_expression
) {
213 if (strcmp(a
->pattern
, b
->pattern
)) {
217 if (a
->filter_expression
&& b
->filter_expression
) {
218 if (strcmp(a
->filter_expression
, b
->filter_expression
)) {
221 } else if (!!a
->filter_expression
!= !!b
->filter_expression
) {
222 /* One is set; not the other. */
226 if (a
->loglevel
.type
!= b
->loglevel
.type
) {
230 if (a
->loglevel
.value
!= b
->loglevel
.value
) {
234 for (i
= 0; i
< count_a
; i
++) {
235 const char *exclusion_a
, *exclusion_b
;
237 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
238 _a
, i
, &exclusion_a
);
239 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
240 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
241 _b
, i
, &exclusion_b
);
242 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
243 if (strcmp(exclusion_a
, exclusion_b
)) {
254 * On success ret is 0;
256 * On error ret is negative.
258 * An event with NO loglevel and the name is * will return NULL.
260 static int generate_agent_filter(
261 const struct lttng_event_rule
*rule
, char **_agent_filter
)
265 char *agent_filter
= NULL
;
268 enum lttng_loglevel_type loglevel_type
;
269 enum lttng_event_rule_status status
;
272 assert(_agent_filter
);
274 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
275 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
280 status
= lttng_event_rule_tracepoint_get_filter(rule
, &filter
);
281 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
283 } else if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
288 status
= lttng_event_rule_tracepoint_get_log_level_type(
289 rule
, &loglevel_type
);
290 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
295 /* Don't add filter for the '*' event. */
296 if (strcmp(pattern
, "*") != 0) {
298 err
= asprintf(&agent_filter
,
299 "(%s) && (logger_name == \"%s\")",
302 err
= asprintf(&agent_filter
, "logger_name == \"%s\"",
307 PERROR("Failed to format agent filter string");
313 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
317 status
= lttng_event_rule_tracepoint_get_log_level(
318 rule
, &loglevel_value
);
319 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
324 if (loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
330 if (filter
|| agent_filter
) {
333 err
= asprintf(&new_filter
,
334 "(%s) && (int_loglevel %s %d)",
335 agent_filter
? agent_filter
: filter
,
340 agent_filter
= new_filter
;
342 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
347 PERROR("Failed to format agent filter string");
353 *_agent_filter
= agent_filter
;
361 static enum lttng_error_code
362 lttng_event_rule_tracepoint_generate_filter_bytecode(
363 struct lttng_event_rule
*rule
,
364 const struct lttng_credentials
*creds
)
367 enum lttng_error_code ret_code
;
368 struct lttng_event_rule_tracepoint
*tracepoint
;
369 enum lttng_domain_type domain_type
;
370 enum lttng_event_rule_status status
;
372 struct lttng_filter_bytecode
*bytecode
= NULL
;
376 tracepoint
= container_of(
377 rule
, struct lttng_event_rule_tracepoint
, parent
);
379 status
= lttng_event_rule_tracepoint_get_filter(rule
, &filter
);
380 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
382 } else if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
383 ret_code
= LTTNG_ERR_FILTER_INVAL
;
387 if (filter
&& filter
[0] == '\0') {
388 ret_code
= LTTNG_ERR_FILTER_INVAL
;
392 status
= lttng_event_rule_tracepoint_get_domain_type(
394 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
395 ret_code
= LTTNG_ERR_UNK
;
399 switch (domain_type
) {
400 case LTTNG_DOMAIN_LOG4J
:
401 case LTTNG_DOMAIN_JUL
:
402 case LTTNG_DOMAIN_PYTHON
:
406 ret
= generate_agent_filter(rule
, &agent_filter
);
408 ret_code
= LTTNG_ERR_FILTER_INVAL
;
412 tracepoint
->internal_filter
.filter
= agent_filter
;
418 tracepoint
->internal_filter
.filter
= strdup(filter
);
419 if (tracepoint
->internal_filter
.filter
== NULL
) {
420 ret_code
= LTTNG_ERR_NOMEM
;
424 tracepoint
->internal_filter
.filter
= NULL
;
430 if (tracepoint
->internal_filter
.filter
== NULL
) {
435 ret
= run_as_generate_filter_bytecode(
436 tracepoint
->internal_filter
.filter
, creds
,
439 ret_code
= LTTNG_ERR_FILTER_INVAL
;
443 tracepoint
->internal_filter
.bytecode
= bytecode
;
453 static const char *lttng_event_rule_tracepoint_get_internal_filter(
454 const struct lttng_event_rule
*rule
)
456 struct lttng_event_rule_tracepoint
*tracepoint
;
459 tracepoint
= container_of(
460 rule
, struct lttng_event_rule_tracepoint
, parent
);
461 return tracepoint
->internal_filter
.filter
;
464 static const struct lttng_filter_bytecode
*
465 lttng_event_rule_tracepoint_get_internal_filter_bytecode(
466 const struct lttng_event_rule
*rule
)
468 struct lttng_event_rule_tracepoint
*tracepoint
;
471 tracepoint
= container_of(
472 rule
, struct lttng_event_rule_tracepoint
, parent
);
473 return tracepoint
->internal_filter
.bytecode
;
476 static struct lttng_event_exclusion
*
477 lttng_event_rule_tracepoint_generate_exclusions(
478 const struct lttng_event_rule
*rule
)
480 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
481 enum lttng_event_rule_status status
;
482 struct lttng_event_exclusion
*local_exclusions
= NULL
;
483 struct lttng_event_exclusion
*ret_exclusions
= NULL
;
484 unsigned int nb_exclusions
= 0;
487 status
= lttng_event_rule_tracepoint_get_domain_type(rule
, &domain_type
);
488 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
490 switch (domain_type
) {
491 case LTTNG_DOMAIN_KERNEL
:
492 case LTTNG_DOMAIN_JUL
:
493 case LTTNG_DOMAIN_LOG4J
:
494 case LTTNG_DOMAIN_PYTHON
:
496 ret_exclusions
= NULL
;
498 case LTTNG_DOMAIN_UST
:
499 /* Exclusions supported. */
505 status
= lttng_event_rule_tracepoint_get_exclusions_count(
506 rule
, &nb_exclusions
);
507 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
508 if (nb_exclusions
== 0) {
510 ret_exclusions
= NULL
;
514 local_exclusions
= zmalloc(sizeof(struct lttng_event_exclusion
) +
515 (LTTNG_SYMBOL_NAME_LEN
* nb_exclusions
));
516 if (!local_exclusions
) {
517 PERROR("Failed to allocate exclusions buffer");
518 ret_exclusions
= NULL
;
522 local_exclusions
->count
= nb_exclusions
;
523 for (i
= 0; i
< nb_exclusions
; i
++) {
524 /* Truncation is already checked at the setter level. */
527 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
529 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
530 strncpy(local_exclusions
->names
[i
], tmp
, LTTNG_SYMBOL_NAME_LEN
);
531 local_exclusions
->names
[i
][LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
534 /* Pass ownership. */
535 ret_exclusions
= local_exclusions
;
536 local_exclusions
= NULL
;
538 free(local_exclusions
);
539 return ret_exclusions
;
542 static void destroy_lttng_exclusions_element(void *ptr
)
547 static unsigned long lttng_event_rule_tracepoint_hash(
548 const struct lttng_event_rule
*rule
)
551 unsigned int i
, exclusion_count
;
552 enum lttng_event_rule_status status
;
553 struct lttng_event_rule_tracepoint
*tp_rule
=
554 container_of(rule
, typeof(*tp_rule
), parent
);
556 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_TRACEPOINT
,
558 hash
^= hash_key_ulong((void *) tp_rule
->domain
, lttng_ht_seed
);
559 hash
^= hash_key_str(tp_rule
->pattern
, lttng_ht_seed
);
561 if (tp_rule
->filter_expression
) {
562 hash
^= hash_key_str(tp_rule
->filter_expression
, lttng_ht_seed
);
565 hash
^= hash_key_ulong((void *) tp_rule
->loglevel
.type
,
567 if (tp_rule
->loglevel
.type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
568 hash
^= hash_key_ulong(
569 (void *) (unsigned long) tp_rule
->loglevel
.value
,
573 status
= lttng_event_rule_tracepoint_get_exclusions_count(rule
,
575 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
577 for (i
= 0; i
< exclusion_count
; i
++) {
578 const char *exclusion
;
580 status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
581 rule
, i
, &exclusion
);
582 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
583 hash
^= hash_key_str(exclusion
, lttng_ht_seed
);
589 struct lttng_event_rule
*lttng_event_rule_tracepoint_create(
590 enum lttng_domain_type domain_type
)
592 struct lttng_event_rule
*rule
= NULL
;
593 struct lttng_event_rule_tracepoint
*tp_rule
;
595 if (domain_type
== LTTNG_DOMAIN_NONE
) {
599 tp_rule
= zmalloc(sizeof(struct lttng_event_rule_tracepoint
));
604 rule
= &tp_rule
->parent
;
605 lttng_event_rule_init(&tp_rule
->parent
, LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
606 tp_rule
->parent
.validate
= lttng_event_rule_tracepoint_validate
;
607 tp_rule
->parent
.serialize
= lttng_event_rule_tracepoint_serialize
;
608 tp_rule
->parent
.equal
= lttng_event_rule_tracepoint_is_equal
;
609 tp_rule
->parent
.destroy
= lttng_event_rule_tracepoint_destroy
;
610 tp_rule
->parent
.generate_filter_bytecode
=
611 lttng_event_rule_tracepoint_generate_filter_bytecode
;
612 tp_rule
->parent
.get_filter
=
613 lttng_event_rule_tracepoint_get_internal_filter
;
614 tp_rule
->parent
.get_filter_bytecode
=
615 lttng_event_rule_tracepoint_get_internal_filter_bytecode
;
616 tp_rule
->parent
.generate_exclusions
=
617 lttng_event_rule_tracepoint_generate_exclusions
;
618 tp_rule
->parent
.hash
= lttng_event_rule_tracepoint_hash
;
620 tp_rule
->domain
= domain_type
;
621 tp_rule
->loglevel
.type
= LTTNG_EVENT_LOGLEVEL_ALL
;
623 lttng_dynamic_pointer_array_init(&tp_rule
->exclusions
,
624 destroy_lttng_exclusions_element
);
630 ssize_t
lttng_event_rule_tracepoint_create_from_payload(
631 struct lttng_payload_view
*view
,
632 struct lttng_event_rule
**_event_rule
)
634 ssize_t ret
, offset
= 0;
636 enum lttng_event_rule_status status
;
637 enum lttng_domain_type domain_type
;
638 enum lttng_loglevel_type loglevel_type
;
639 const struct lttng_event_rule_tracepoint_comm
*tracepoint_comm
;
641 const char *filter_expression
= NULL
;
642 const char **exclusions
= NULL
;
643 const uint32_t *exclusion_len
;
644 const char *exclusion
;
645 struct lttng_buffer_view current_buffer_view
;
646 struct lttng_event_rule
*rule
= NULL
;
653 current_buffer_view
= lttng_buffer_view_from_view(
654 &view
->buffer
, offset
, sizeof(*tracepoint_comm
));
655 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
656 ERR("Failed to initialize from malformed event rule tracepoint: buffer too short to contain header.");
661 tracepoint_comm
= (typeof(tracepoint_comm
)) current_buffer_view
.data
;
663 if (tracepoint_comm
->domain_type
<= LTTNG_DOMAIN_NONE
||
664 tracepoint_comm
->domain_type
> LTTNG_DOMAIN_PYTHON
) {
665 /* Invalid domain value. */
666 ERR("Invalid domain type value (%i) found in tracepoint_comm buffer.",
667 (int) tracepoint_comm
->domain_type
);
672 domain_type
= (enum lttng_domain_type
) tracepoint_comm
->domain_type
;
673 rule
= lttng_event_rule_tracepoint_create(domain_type
);
675 ERR("Failed to create event rule tracepoint.");
680 loglevel_type
= (enum lttng_loglevel_type
)
681 tracepoint_comm
->loglevel_type
;
682 switch (loglevel_type
) {
683 case LTTNG_EVENT_LOGLEVEL_ALL
:
684 status
= lttng_event_rule_tracepoint_set_log_level_all(rule
);
686 case LTTNG_EVENT_LOGLEVEL_RANGE
:
687 status
= lttng_event_rule_tracepoint_set_log_level_range_lower_bound(rule
,
688 (enum lttng_loglevel_type
) tracepoint_comm
691 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
692 status
= lttng_event_rule_tracepoint_set_log_level(rule
,
693 (enum lttng_loglevel_type
) tracepoint_comm
697 ERR("Failed to set event rule tracepoint loglevel: unknown loglevel type.");
702 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
703 ERR("Failed to set event rule tracepoint loglevel.");
706 /* Skip to payload. */
707 offset
+= current_buffer_view
.size
;
709 /* Map the pattern. */
710 current_buffer_view
= lttng_buffer_view_from_view(
711 &view
->buffer
, offset
, tracepoint_comm
->pattern_len
);
713 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
718 pattern
= current_buffer_view
.data
;
719 if (!lttng_buffer_view_contains_string(¤t_buffer_view
, pattern
,
720 tracepoint_comm
->pattern_len
)) {
725 /* Skip after the pattern. */
726 offset
+= tracepoint_comm
->pattern_len
;
728 if (!tracepoint_comm
->filter_expression_len
) {
729 goto skip_filter_expression
;
732 /* Map the filter_expression. */
733 current_buffer_view
= lttng_buffer_view_from_view(&view
->buffer
, offset
,
734 tracepoint_comm
->filter_expression_len
);
735 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
740 filter_expression
= current_buffer_view
.data
;
741 if (!lttng_buffer_view_contains_string(¤t_buffer_view
,
743 tracepoint_comm
->filter_expression_len
)) {
748 /* Skip after the pattern. */
749 offset
+= tracepoint_comm
->filter_expression_len
;
751 skip_filter_expression
:
752 for (i
= 0; i
< tracepoint_comm
->exclusions_count
; i
++) {
753 current_buffer_view
= lttng_buffer_view_from_view(
754 &view
->buffer
, offset
, sizeof(*exclusion_len
));
755 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
760 exclusion_len
= (typeof(exclusion_len
)) current_buffer_view
.data
;
761 offset
+= sizeof(*exclusion_len
);
763 current_buffer_view
= lttng_buffer_view_from_view(
764 &view
->buffer
, offset
, *exclusion_len
);
765 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
770 exclusion
= current_buffer_view
.data
;
771 if (!lttng_buffer_view_contains_string(¤t_buffer_view
,
772 exclusion
, *exclusion_len
)) {
777 status
= lttng_event_rule_tracepoint_add_exclusion(rule
, exclusion
);
778 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
779 ERR("Failed to add event rule tracepoint exclusion \"%s\".",
785 /* Skip to next exclusion. */
786 offset
+= *exclusion_len
;
789 status
= lttng_event_rule_tracepoint_set_pattern(rule
, pattern
);
790 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
791 ERR("Failed to set event rule tracepoint pattern.");
796 if (filter_expression
) {
797 status
= lttng_event_rule_tracepoint_set_filter(
798 rule
, filter_expression
);
799 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
800 ERR("Failed to set event rule tracepoint pattern.");
811 lttng_event_rule_destroy(rule
);
815 enum lttng_event_rule_status
lttng_event_rule_tracepoint_set_pattern(
816 struct lttng_event_rule
*rule
, const char *pattern
)
818 char *pattern_copy
= NULL
;
819 struct lttng_event_rule_tracepoint
*tracepoint
;
820 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
822 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !pattern
||
823 strlen(pattern
) == 0) {
824 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
828 tracepoint
= container_of(
829 rule
, struct lttng_event_rule_tracepoint
, parent
);
830 pattern_copy
= strdup(pattern
);
832 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
836 free(tracepoint
->pattern
);
838 tracepoint
->pattern
= pattern_copy
;
844 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_pattern(
845 const struct lttng_event_rule
*rule
, const char **pattern
)
847 struct lttng_event_rule_tracepoint
*tracepoint
;
848 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
850 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !pattern
) {
851 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
855 tracepoint
= container_of(
856 rule
, struct lttng_event_rule_tracepoint
, parent
);
857 if (!tracepoint
->pattern
) {
858 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
862 *pattern
= tracepoint
->pattern
;
867 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_domain_type(
868 const struct lttng_event_rule
*rule
,
869 enum lttng_domain_type
*type
)
871 struct lttng_event_rule_tracepoint
*tracepoint
;
872 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
874 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !type
) {
875 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
879 tracepoint
= container_of(
880 rule
, struct lttng_event_rule_tracepoint
, parent
);
881 *type
= tracepoint
->domain
;
886 enum lttng_event_rule_status
lttng_event_rule_tracepoint_set_filter(
887 struct lttng_event_rule
*rule
, const char *expression
)
889 char *expression_copy
= NULL
;
890 struct lttng_event_rule_tracepoint
*tracepoint
;
891 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
893 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !expression
||
894 strlen(expression
) == 0) {
895 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
899 tracepoint
= container_of(
900 rule
, struct lttng_event_rule_tracepoint
, parent
);
901 expression_copy
= strdup(expression
);
902 if (!expression_copy
) {
903 PERROR("Failed to copy filter expression");
904 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
908 if (tracepoint
->filter_expression
) {
909 free(tracepoint
->filter_expression
);
912 tracepoint
->filter_expression
= expression_copy
;
913 expression_copy
= NULL
;
918 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_filter(
919 const struct lttng_event_rule
*rule
, const char **expression
)
921 struct lttng_event_rule_tracepoint
*tracepoint
;
922 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
924 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !expression
) {
925 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
929 tracepoint
= container_of(
930 rule
, struct lttng_event_rule_tracepoint
, parent
);
931 if (!tracepoint
->filter_expression
) {
932 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
936 *expression
= tracepoint
->filter_expression
;
941 static bool log_level_value_valid(
942 int level
, enum lttng_domain_type domain
)
947 case LTTNG_DOMAIN_KERNEL
:
948 case LTTNG_DOMAIN_UST
:
949 if (level
< LTTNG_LOGLEVEL_EMERG
) {
953 if (level
> LTTNG_LOGLEVEL_DEBUG
) {
960 case LTTNG_DOMAIN_JUL
:
961 case LTTNG_DOMAIN_LOG4J
:
962 case LTTNG_DOMAIN_PYTHON
:
964 * For both JUL and LOG4J custom log level are possible and can
965 * spawn the entire int32 range.
966 * For python, custom log level are possible, it is not clear if
967 * negative value are accepted (NOTSET == 0) but the source code
968 * validate against the int type implying that negative values
974 case LTTNG_DOMAIN_NONE
:
983 enum lttng_event_rule_status
lttng_event_rule_tracepoint_set_log_level(
984 struct lttng_event_rule
*rule
, int level
)
986 struct lttng_event_rule_tracepoint
*tracepoint
;
987 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
989 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
)) {
990 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
994 tracepoint
= container_of(
995 rule
, struct lttng_event_rule_tracepoint
, parent
);
997 if (!log_level_value_valid(level
, tracepoint
->domain
)) {
998 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1002 tracepoint
->loglevel
.value
= level
;
1003 tracepoint
->loglevel
.type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1008 enum lttng_event_rule_status
1009 lttng_event_rule_tracepoint_set_log_level_range_lower_bound(
1010 struct lttng_event_rule
*rule
, int level
)
1012 struct lttng_event_rule_tracepoint
*tracepoint
;
1013 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1015 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
)) {
1016 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1020 tracepoint
= container_of(
1021 rule
, struct lttng_event_rule_tracepoint
, parent
);
1023 if (!log_level_value_valid(level
, tracepoint
->domain
)) {
1024 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1028 tracepoint
->loglevel
.value
= level
;
1029 tracepoint
->loglevel
.type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1034 enum lttng_event_rule_status
lttng_event_rule_tracepoint_set_log_level_all(
1035 struct lttng_event_rule
*rule
)
1037 struct lttng_event_rule_tracepoint
*tracepoint
;
1038 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1040 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
)) {
1041 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1045 tracepoint
= container_of(
1046 rule
, struct lttng_event_rule_tracepoint
, parent
);
1047 tracepoint
->loglevel
.type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1052 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_log_level_type(
1053 const struct lttng_event_rule
*rule
,
1054 enum lttng_loglevel_type
*type
)
1056 struct lttng_event_rule_tracepoint
*tracepoint
;
1057 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1059 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !type
) {
1060 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1064 tracepoint
= container_of(
1065 rule
, struct lttng_event_rule_tracepoint
, parent
);
1066 *type
= tracepoint
->loglevel
.type
;
1071 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_log_level(
1072 const struct lttng_event_rule
*rule
, int *level
)
1074 struct lttng_event_rule_tracepoint
*tracepoint
;
1075 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1077 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !level
) {
1078 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1082 tracepoint
= container_of(
1083 rule
, struct lttng_event_rule_tracepoint
, parent
);
1084 if (tracepoint
->loglevel
.type
== LTTNG_EVENT_LOGLEVEL_ALL
) {
1085 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
1089 *level
= tracepoint
->loglevel
.value
;
1094 enum lttng_event_rule_status
lttng_event_rule_tracepoint_add_exclusion(
1095 struct lttng_event_rule
*rule
,
1096 const char *exclusion
)
1099 char *exclusion_copy
= NULL
;
1100 struct lttng_event_rule_tracepoint
*tracepoint
;
1101 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1102 enum lttng_domain_type domain_type
;
1104 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) ||
1106 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1110 tracepoint
= container_of(
1111 rule
, struct lttng_event_rule_tracepoint
, parent
);
1113 status
= lttng_event_rule_tracepoint_get_domain_type(
1114 rule
, &domain_type
);
1115 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1119 switch (domain_type
) {
1120 case LTTNG_DOMAIN_KERNEL
:
1121 case LTTNG_DOMAIN_JUL
:
1122 case LTTNG_DOMAIN_LOG4J
:
1123 case LTTNG_DOMAIN_PYTHON
:
1124 status
= LTTNG_EVENT_RULE_STATUS_UNSUPPORTED
;
1126 case LTTNG_DOMAIN_UST
:
1127 /* Exclusions supported. */
1133 if (strlen(exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
1134 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1138 exclusion_copy
= strdup(exclusion
);
1139 if (!exclusion_copy
) {
1140 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
1144 ret
= lttng_dynamic_pointer_array_add_pointer(&tracepoint
->exclusions
,
1147 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
1151 exclusion_copy
= NULL
;
1153 free(exclusion_copy
);
1157 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_exclusions_count(
1158 const struct lttng_event_rule
*rule
, unsigned int *count
)
1160 struct lttng_event_rule_tracepoint
*tracepoint
;
1161 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1163 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !count
) {
1164 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1168 tracepoint
= container_of(
1169 rule
, struct lttng_event_rule_tracepoint
, parent
);
1170 *count
= lttng_dynamic_pointer_array_get_count(&tracepoint
->exclusions
);
1175 enum lttng_event_rule_status
lttng_event_rule_tracepoint_get_exclusion_at_index(
1176 const struct lttng_event_rule
*rule
,
1178 const char **exclusion
)
1181 struct lttng_event_rule_tracepoint
*tracepoint
;
1182 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1184 if (!rule
|| !IS_TRACEPOINT_EVENT_RULE(rule
) || !exclusion
) {
1185 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1189 tracepoint
= container_of(
1190 rule
, struct lttng_event_rule_tracepoint
, parent
);
1191 if (lttng_event_rule_tracepoint_get_exclusions_count(rule
, &count
) !=
1192 LTTNG_EVENT_RULE_STATUS_OK
) {
1196 if (index
>= count
) {
1200 *exclusion
= lttng_dynamic_pointer_array_get_pointer(
1201 &tracepoint
->exclusions
, index
);