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/hashtable/hashtable.h>
12 #include <common/hashtable/utils.h>
13 #include <common/macros.h>
14 #include <common/mi-lttng.h>
15 #include <common/optional.h>
16 #include <common/payload-view.h>
17 #include <common/payload.h>
18 #include <common/runas.h>
19 #include <common/string-utils/string-utils.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/event-rule/user-tracepoint-internal.h>
22 #include <lttng/event.h>
23 #include <lttng/log-level-rule.h>
25 #define IS_USER_TRACEPOINT_EVENT_RULE(rule) \
26 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT)
28 static void lttng_event_rule_user_tracepoint_destroy(struct lttng_event_rule
*rule
)
30 struct lttng_event_rule_user_tracepoint
*tracepoint
;
36 tracepoint
= container_of(
37 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
39 lttng_log_level_rule_destroy(tracepoint
->log_level_rule
);
40 lttng_dynamic_pointer_array_reset(&tracepoint
->exclusions
);
41 free(tracepoint
->pattern
);
42 free(tracepoint
->filter_expression
);
43 free(tracepoint
->internal_filter
.filter
);
44 free(tracepoint
->internal_filter
.bytecode
);
48 static bool lttng_event_rule_user_tracepoint_validate(
49 const struct lttng_event_rule
*rule
)
52 struct lttng_event_rule_user_tracepoint
*tracepoint
;
58 tracepoint
= container_of(
59 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
62 if (!tracepoint
->pattern
) {
63 ERR("Invalid user tracepoint event rule: a pattern must be set.");
72 static int lttng_event_rule_user_tracepoint_serialize(
73 const struct lttng_event_rule
*rule
,
74 struct lttng_payload
*payload
)
77 size_t pattern_len
, filter_expression_len
, exclusions_len
, header_offset
;
78 size_t size_before_log_level_rule
;
79 struct lttng_event_rule_user_tracepoint
*tracepoint
;
80 struct lttng_event_rule_user_tracepoint_comm tracepoint_comm
;
81 enum lttng_event_rule_status status
;
82 unsigned int exclusion_count
;
83 size_t exclusions_appended_len
= 0;
84 struct lttng_event_rule_user_tracepoint_comm
*header
;
86 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
)) {
91 header_offset
= payload
->buffer
.size
;
93 DBG("Serializing user tracepoint event rule.");
94 tracepoint
= container_of(
95 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
97 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule
, &exclusion_count
);
98 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
100 pattern_len
= strlen(tracepoint
->pattern
) + 1;
102 if (tracepoint
->filter_expression
!= NULL
) {
103 filter_expression_len
=
104 strlen(tracepoint
->filter_expression
) + 1;
106 filter_expression_len
= 0;
110 for (i
= 0; i
< exclusion_count
; i
++) {
111 const char *exclusion
;
113 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
114 rule
, i
, &exclusion
);
115 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
118 exclusions_len
+= sizeof(uint32_t);
119 /* Payload (null terminated). */
120 exclusions_len
+= strlen(exclusion
) + 1;
123 tracepoint_comm
.pattern_len
= pattern_len
;
124 tracepoint_comm
.filter_expression_len
= filter_expression_len
;
125 tracepoint_comm
.exclusions_count
= exclusion_count
;
126 tracepoint_comm
.exclusions_len
= exclusions_len
;
128 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &tracepoint_comm
,
129 sizeof(tracepoint_comm
));
134 ret
= lttng_dynamic_buffer_append(
135 &payload
->buffer
, tracepoint
->pattern
, pattern_len
);
140 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, tracepoint
->filter_expression
,
141 filter_expression_len
);
146 size_before_log_level_rule
= payload
->buffer
.size
;
148 ret
= lttng_log_level_rule_serialize(tracepoint
->log_level_rule
, payload
);
153 header
= (typeof(header
)) ((char *) payload
->buffer
.data
+ header_offset
);
154 header
->log_level_rule_len
=
155 payload
->buffer
.size
- size_before_log_level_rule
;
157 for (i
= 0; i
< exclusion_count
; i
++) {
159 const char *exclusion
;
161 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
162 rule
, i
, &exclusion
);
163 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
165 len
= strlen(exclusion
) + 1;
166 /* Append exclusion length, includes the null terminator. */
167 ret
= lttng_dynamic_buffer_append(
168 &payload
->buffer
, &len
, sizeof(uint32_t));
173 exclusions_appended_len
+= sizeof(uint32_t);
175 /* Include the '\0' in the payload. */
176 ret
= lttng_dynamic_buffer_append(
177 &payload
->buffer
, exclusion
, len
);
182 exclusions_appended_len
+= len
;
185 assert(exclusions_len
== exclusions_appended_len
);
191 static bool lttng_event_rule_user_tracepoint_is_equal(
192 const struct lttng_event_rule
*_a
,
193 const struct lttng_event_rule
*_b
)
196 bool is_equal
= false;
197 struct lttng_event_rule_user_tracepoint
*a
, *b
;
198 unsigned int count_a
, count_b
;
199 enum lttng_event_rule_status status
;
201 a
= container_of(_a
, struct lttng_event_rule_user_tracepoint
, parent
);
202 b
= container_of(_b
, struct lttng_event_rule_user_tracepoint
, parent
);
204 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_a
, &count_a
);
205 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
206 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_b
, &count_b
);
207 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
210 if (count_a
!= count_b
) {
214 if (!!a
->filter_expression
!= !!b
->filter_expression
) {
221 if (strcmp(a
->pattern
, b
->pattern
)) {
225 if (a
->filter_expression
&& b
->filter_expression
) {
226 if (strcmp(a
->filter_expression
, b
->filter_expression
)) {
229 } else if (!!a
->filter_expression
!= !!b
->filter_expression
) {
230 /* One is set; not the other. */
234 if (!lttng_log_level_rule_is_equal(
235 a
->log_level_rule
, b
->log_level_rule
)) {
239 for (i
= 0; i
< count_a
; i
++) {
240 const char *exclusion_a
, *exclusion_b
;
242 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
243 _a
, i
, &exclusion_a
);
244 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
245 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
246 _b
, i
, &exclusion_b
);
247 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
248 if (strcmp(exclusion_a
, exclusion_b
)) {
258 static enum lttng_error_code
259 lttng_event_rule_user_tracepoint_generate_filter_bytecode(
260 struct lttng_event_rule
*rule
,
261 const struct lttng_credentials
*creds
)
264 enum lttng_error_code ret_code
;
265 struct lttng_event_rule_user_tracepoint
*tracepoint
;
266 enum lttng_event_rule_status status
;
268 struct lttng_bytecode
*bytecode
= NULL
;
272 tracepoint
= container_of(
273 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
275 status
= lttng_event_rule_user_tracepoint_get_filter(rule
, &filter
);
276 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
278 } else if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
279 ret_code
= LTTNG_ERR_FILTER_INVAL
;
283 if (filter
&& filter
[0] == '\0') {
284 ret_code
= LTTNG_ERR_FILTER_INVAL
;
289 tracepoint
->internal_filter
.filter
= strdup(filter
);
290 if (tracepoint
->internal_filter
.filter
== NULL
) {
291 ret_code
= LTTNG_ERR_NOMEM
;
295 tracepoint
->internal_filter
.filter
= NULL
;
298 if (tracepoint
->internal_filter
.filter
== NULL
) {
303 ret
= run_as_generate_filter_bytecode(
304 tracepoint
->internal_filter
.filter
, creds
,
307 ret_code
= LTTNG_ERR_FILTER_INVAL
;
311 tracepoint
->internal_filter
.bytecode
= bytecode
;
321 static const char *lttng_event_rule_user_tracepoint_get_internal_filter(
322 const struct lttng_event_rule
*rule
)
324 struct lttng_event_rule_user_tracepoint
*tracepoint
;
327 tracepoint
= container_of(
328 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
329 return tracepoint
->internal_filter
.filter
;
332 static const struct lttng_bytecode
*
333 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode(
334 const struct lttng_event_rule
*rule
)
336 struct lttng_event_rule_user_tracepoint
*tracepoint
;
339 tracepoint
= container_of(
340 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
341 return tracepoint
->internal_filter
.bytecode
;
344 static enum lttng_event_rule_generate_exclusions_status
345 lttng_event_rule_user_tracepoint_generate_exclusions(
346 const struct lttng_event_rule
*rule
,
347 struct lttng_event_exclusion
**_exclusions
)
349 unsigned int nb_exclusions
= 0, i
;
350 struct lttng_event_exclusion
*exclusions
;
351 enum lttng_event_rule_status event_rule_status
;
352 enum lttng_event_rule_generate_exclusions_status ret_status
;
356 event_rule_status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
357 rule
, &nb_exclusions
);
358 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
359 if (nb_exclusions
== 0) {
362 ret_status
= LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
;
366 exclusions
= zmalloc(sizeof(struct lttng_event_exclusion
) +
367 (LTTNG_SYMBOL_NAME_LEN
* nb_exclusions
));
369 PERROR("Failed to allocate exclusions buffer");
370 ret_status
= LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OUT_OF_MEMORY
;
374 exclusions
->count
= nb_exclusions
;
375 for (i
= 0; i
< nb_exclusions
; i
++) {
377 const char *exclusion_str
;
380 lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
381 rule
, i
, &exclusion_str
);
382 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
384 copy_ret
= lttng_strncpy(exclusions
->names
[i
], exclusion_str
,
385 LTTNG_SYMBOL_NAME_LEN
);
389 ret_status
= LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_ERROR
;
394 ret_status
= LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
;
397 *_exclusions
= exclusions
;
401 static void destroy_lttng_exclusions_element(void *ptr
)
406 static unsigned long lttng_event_rule_user_tracepoint_hash(
407 const struct lttng_event_rule
*rule
)
410 unsigned int i
, exclusion_count
;
411 enum lttng_event_rule_status status
;
412 struct lttng_event_rule_user_tracepoint
*tp_rule
=
413 container_of(rule
, typeof(*tp_rule
), parent
);
415 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
,
417 hash
^= hash_key_str(tp_rule
->pattern
, lttng_ht_seed
);
419 if (tp_rule
->filter_expression
) {
420 hash
^= hash_key_str(tp_rule
->filter_expression
, lttng_ht_seed
);
423 if (tp_rule
->log_level_rule
) {
424 hash
^= lttng_log_level_rule_hash(tp_rule
->log_level_rule
);
427 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule
,
429 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
431 for (i
= 0; i
< exclusion_count
; i
++) {
432 const char *exclusion
;
434 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
435 rule
, i
, &exclusion
);
436 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
437 hash
^= hash_key_str(exclusion
, lttng_ht_seed
);
443 static enum lttng_error_code
lttng_event_rule_user_tracepoint_mi_serialize(
444 const struct lttng_event_rule
*rule
, struct mi_writer
*writer
)
447 enum lttng_error_code ret_code
;
448 enum lttng_event_rule_status status
;
449 const char *filter
= NULL
;
450 const char *name_pattern
= NULL
;
451 const struct lttng_log_level_rule
*log_level_rule
= NULL
;
452 unsigned int exclusion_count
= 0;
456 assert(IS_USER_TRACEPOINT_EVENT_RULE(rule
));
458 status
= lttng_event_rule_user_tracepoint_get_name_pattern(
459 rule
, &name_pattern
);
460 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
461 assert(name_pattern
);
463 status
= lttng_event_rule_user_tracepoint_get_filter(rule
, &filter
);
464 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
||
465 status
== LTTNG_EVENT_RULE_STATUS_UNSET
);
467 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
468 rule
, &log_level_rule
);
469 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
||
470 status
== LTTNG_EVENT_RULE_STATUS_UNSET
);
472 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
473 rule
, &exclusion_count
);
474 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
476 /* Open event rule user tracepoint element. */
477 ret
= mi_lttng_writer_open_element(
478 writer
, mi_lttng_element_event_rule_user_tracepoint
);
484 ret
= mi_lttng_writer_write_element_string(writer
,
485 mi_lttng_element_event_rule_name_pattern
, name_pattern
);
490 /* Filter expression. */
491 if (filter
!= NULL
) {
492 ret
= mi_lttng_writer_write_element_string(writer
,
493 mi_lttng_element_event_rule_filter_expression
,
500 /* Log level rule. */
501 if (log_level_rule
) {
502 ret_code
= lttng_log_level_rule_mi_serialize(
503 log_level_rule
, writer
);
504 if (ret_code
!= LTTNG_OK
) {
509 if (exclusion_count
!= 0) {
512 /* Open the exclusion list. */
513 ret
= mi_lttng_writer_open_element(writer
,
514 mi_lttng_element_event_rule_user_tracepoint_name_pattern_exclusions
);
519 for (i
= 0; i
< exclusion_count
; i
++) {
520 const char *exclusion
;
522 status
= lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
523 rule
, i
, &exclusion
);
524 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
526 ret
= mi_lttng_writer_write_element_string(writer
,
527 mi_lttng_element_event_rule_user_tracepoint_name_pattern_exclusion
,
534 /* Close the list. */
535 ret
= mi_lttng_writer_close_element(writer
);
541 /* Close event rule user tracepoint element. */
542 ret
= mi_lttng_writer_close_element(writer
);
551 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
556 struct lttng_event_rule
*lttng_event_rule_user_tracepoint_create(void)
558 struct lttng_event_rule
*rule
= NULL
;
559 struct lttng_event_rule_user_tracepoint
*tp_rule
;
560 enum lttng_event_rule_status status
;
562 tp_rule
= zmalloc(sizeof(struct lttng_event_rule_user_tracepoint
));
567 rule
= &tp_rule
->parent
;
568 lttng_event_rule_init(&tp_rule
->parent
, LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
569 tp_rule
->parent
.validate
= lttng_event_rule_user_tracepoint_validate
;
570 tp_rule
->parent
.serialize
= lttng_event_rule_user_tracepoint_serialize
;
571 tp_rule
->parent
.equal
= lttng_event_rule_user_tracepoint_is_equal
;
572 tp_rule
->parent
.destroy
= lttng_event_rule_user_tracepoint_destroy
;
573 tp_rule
->parent
.generate_filter_bytecode
=
574 lttng_event_rule_user_tracepoint_generate_filter_bytecode
;
575 tp_rule
->parent
.get_filter
=
576 lttng_event_rule_user_tracepoint_get_internal_filter
;
577 tp_rule
->parent
.get_filter_bytecode
=
578 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode
;
579 tp_rule
->parent
.generate_exclusions
=
580 lttng_event_rule_user_tracepoint_generate_exclusions
;
581 tp_rule
->parent
.hash
= lttng_event_rule_user_tracepoint_hash
;
582 tp_rule
->parent
.mi_serialize
= lttng_event_rule_user_tracepoint_mi_serialize
;
584 /* Not necessary for now. */
585 tp_rule
->parent
.generate_lttng_event
= NULL
;
587 tp_rule
->log_level_rule
= NULL
;
589 lttng_dynamic_pointer_array_init(&tp_rule
->exclusions
,
590 destroy_lttng_exclusions_element
);
592 /* Default pattern is '*'. */
593 status
= lttng_event_rule_user_tracepoint_set_name_pattern(rule
, "*");
594 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
595 lttng_event_rule_destroy(rule
);
604 ssize_t
lttng_event_rule_user_tracepoint_create_from_payload(
605 struct lttng_payload_view
*view
,
606 struct lttng_event_rule
**_event_rule
)
608 ssize_t ret
, offset
= 0;
610 enum lttng_event_rule_status status
;
611 const struct lttng_event_rule_user_tracepoint_comm
*tracepoint_comm
;
613 const char *filter_expression
= NULL
;
614 const char **exclusions
= NULL
;
615 const uint32_t *exclusion_len
;
616 const char *exclusion
;
617 struct lttng_buffer_view current_buffer_view
;
618 struct lttng_event_rule
*rule
= NULL
;
619 struct lttng_log_level_rule
*log_level_rule
= NULL
;
626 current_buffer_view
= lttng_buffer_view_from_view(
627 &view
->buffer
, offset
, sizeof(*tracepoint_comm
));
628 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
629 ERR("Failed to initialize from malformed event rule tracepoint: buffer too short to contain header.");
634 tracepoint_comm
= (typeof(tracepoint_comm
)) current_buffer_view
.data
;
636 rule
= lttng_event_rule_user_tracepoint_create();
638 ERR("Failed to create event rule user tracepoint.");
643 /* Skip to payload. */
644 offset
+= current_buffer_view
.size
;
646 /* Map the pattern. */
647 current_buffer_view
= lttng_buffer_view_from_view(
648 &view
->buffer
, offset
, tracepoint_comm
->pattern_len
);
650 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
655 pattern
= current_buffer_view
.data
;
656 if (!lttng_buffer_view_contains_string(¤t_buffer_view
, pattern
,
657 tracepoint_comm
->pattern_len
)) {
662 /* Skip after the pattern. */
663 offset
+= tracepoint_comm
->pattern_len
;
665 if (!tracepoint_comm
->filter_expression_len
) {
666 goto skip_filter_expression
;
669 /* Map the filter_expression. */
670 current_buffer_view
= lttng_buffer_view_from_view(&view
->buffer
, offset
,
671 tracepoint_comm
->filter_expression_len
);
672 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
677 filter_expression
= current_buffer_view
.data
;
678 if (!lttng_buffer_view_contains_string(¤t_buffer_view
,
680 tracepoint_comm
->filter_expression_len
)) {
685 /* Skip after the pattern. */
686 offset
+= tracepoint_comm
->filter_expression_len
;
688 skip_filter_expression
:
689 if (!tracepoint_comm
->log_level_rule_len
) {
690 goto skip_log_level_rule
;
694 /* Map the log level rule. */
695 struct lttng_payload_view current_payload_view
=
696 lttng_payload_view_from_view(view
, offset
,
697 tracepoint_comm
->log_level_rule_len
);
699 ret
= lttng_log_level_rule_create_from_payload(
700 ¤t_payload_view
, &log_level_rule
);
706 assert(ret
== tracepoint_comm
->log_level_rule_len
);
709 /* Skip after the log level rule. */
710 offset
+= tracepoint_comm
->log_level_rule_len
;
713 for (i
= 0; i
< tracepoint_comm
->exclusions_count
; i
++) {
714 current_buffer_view
= lttng_buffer_view_from_view(
715 &view
->buffer
, offset
, sizeof(*exclusion_len
));
716 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
721 exclusion_len
= (typeof(exclusion_len
)) current_buffer_view
.data
;
722 offset
+= sizeof(*exclusion_len
);
724 current_buffer_view
= lttng_buffer_view_from_view(
725 &view
->buffer
, offset
, *exclusion_len
);
726 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
731 exclusion
= current_buffer_view
.data
;
732 if (!lttng_buffer_view_contains_string(¤t_buffer_view
,
733 exclusion
, *exclusion_len
)) {
738 status
= lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(rule
, exclusion
);
739 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
740 ERR("Failed to add event rule user tracepoint exclusion \"%s\".",
746 /* Skip to next exclusion. */
747 offset
+= *exclusion_len
;
750 status
= lttng_event_rule_user_tracepoint_set_name_pattern(rule
, pattern
);
751 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
752 ERR("Failed to set event rule user tracepoint pattern.");
757 if (filter_expression
) {
758 status
= lttng_event_rule_user_tracepoint_set_filter(
759 rule
, filter_expression
);
760 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
761 ERR("Failed to set event rule user tracepoint pattern.");
767 if (log_level_rule
) {
768 status
= lttng_event_rule_user_tracepoint_set_log_level_rule(
769 rule
, log_level_rule
);
770 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
771 ERR("Failed to set event rule user tracepoint log level rule.");
782 lttng_log_level_rule_destroy(log_level_rule
);
783 lttng_event_rule_destroy(rule
);
787 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_set_name_pattern(
788 struct lttng_event_rule
*rule
, const char *pattern
)
790 char *pattern_copy
= NULL
;
791 struct lttng_event_rule_user_tracepoint
*tracepoint
;
792 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
794 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !pattern
||
795 strlen(pattern
) == 0) {
796 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
800 tracepoint
= container_of(
801 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
802 pattern_copy
= strdup(pattern
);
804 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
808 /* Normalize the pattern. */
809 strutils_normalize_star_glob_pattern(pattern_copy
);
811 free(tracepoint
->pattern
);
813 tracepoint
->pattern
= pattern_copy
;
819 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_get_name_pattern(
820 const struct lttng_event_rule
*rule
, const char **pattern
)
822 struct lttng_event_rule_user_tracepoint
*tracepoint
;
823 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
825 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !pattern
) {
826 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
830 tracepoint
= container_of(
831 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
832 if (!tracepoint
->pattern
) {
833 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
837 *pattern
= tracepoint
->pattern
;
842 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_set_filter(
843 struct lttng_event_rule
*rule
, const char *expression
)
845 char *expression_copy
= NULL
;
846 struct lttng_event_rule_user_tracepoint
*tracepoint
;
847 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
849 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !expression
||
850 strlen(expression
) == 0) {
851 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
855 tracepoint
= container_of(
856 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
857 expression_copy
= strdup(expression
);
858 if (!expression_copy
) {
859 PERROR("Failed to copy filter expression");
860 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
864 if (tracepoint
->filter_expression
) {
865 free(tracepoint
->filter_expression
);
868 tracepoint
->filter_expression
= expression_copy
;
869 expression_copy
= NULL
;
874 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_get_filter(
875 const struct lttng_event_rule
*rule
, const char **expression
)
877 struct lttng_event_rule_user_tracepoint
*tracepoint
;
878 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
880 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !expression
) {
881 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
885 tracepoint
= container_of(
886 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
887 if (!tracepoint
->filter_expression
) {
888 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
892 *expression
= tracepoint
->filter_expression
;
897 static bool log_level_rule_valid(const struct lttng_log_level_rule
*rule
)
900 enum lttng_log_level_rule_status status
;
903 switch (lttng_log_level_rule_get_type(rule
)) {
904 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
905 status
= lttng_log_level_rule_exactly_get_level(rule
, &level
);
907 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
908 status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
915 assert(status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
917 if (level
< LTTNG_LOGLEVEL_EMERG
) {
921 if (level
> LTTNG_LOGLEVEL_DEBUG
) {
932 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_set_log_level_rule(
933 struct lttng_event_rule
*rule
,
934 const struct lttng_log_level_rule
*log_level_rule
)
936 struct lttng_event_rule_user_tracepoint
*tracepoint
;
937 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
938 struct lttng_log_level_rule
*copy
= NULL
;
940 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
)) {
941 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
945 tracepoint
= container_of(
946 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
948 if (!log_level_rule_valid(log_level_rule
)) {
949 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
953 copy
= lttng_log_level_rule_copy(log_level_rule
);
955 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
959 if (tracepoint
->log_level_rule
) {
960 lttng_log_level_rule_destroy(tracepoint
->log_level_rule
);
963 tracepoint
->log_level_rule
= copy
;
969 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_get_log_level_rule(
970 const struct lttng_event_rule
*rule
,
971 const struct lttng_log_level_rule
**log_level_rule
974 struct lttng_event_rule_user_tracepoint
*tracepoint
;
975 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
977 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !log_level_rule
) {
978 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
982 tracepoint
= container_of(
983 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
984 if (tracepoint
->log_level_rule
== NULL
) {
985 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
989 *log_level_rule
= tracepoint
->log_level_rule
;
994 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
995 struct lttng_event_rule
*rule
,
996 const char *exclusion
)
999 char *exclusion_copy
= NULL
;
1000 struct lttng_event_rule_user_tracepoint
*tracepoint
;
1001 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1003 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) ||
1005 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1009 tracepoint
= container_of(
1010 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
1012 if (strlen(exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
1013 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1017 exclusion_copy
= strdup(exclusion
);
1018 if (!exclusion_copy
) {
1019 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
1023 ret
= lttng_dynamic_pointer_array_add_pointer(&tracepoint
->exclusions
,
1026 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
1030 exclusion_copy
= NULL
;
1032 free(exclusion_copy
);
1036 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
1037 const struct lttng_event_rule
*rule
, unsigned int *count
)
1039 struct lttng_event_rule_user_tracepoint
*tracepoint
;
1040 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1042 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !count
) {
1043 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1047 tracepoint
= container_of(
1048 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
1049 *count
= lttng_dynamic_pointer_array_get_count(&tracepoint
->exclusions
);
1054 enum lttng_event_rule_status
lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
1055 const struct lttng_event_rule
*rule
,
1057 const char **exclusion
)
1060 struct lttng_event_rule_user_tracepoint
*tracepoint
;
1061 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
1063 if (!rule
|| !IS_USER_TRACEPOINT_EVENT_RULE(rule
) || !exclusion
) {
1064 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
1068 tracepoint
= container_of(
1069 rule
, struct lttng_event_rule_user_tracepoint
, parent
);
1070 if (lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule
, &count
) !=
1071 LTTNG_EVENT_RULE_STATUS_OK
) {
1075 if (index
>= count
) {
1079 *exclusion
= lttng_dynamic_pointer_array_get_pointer(
1080 &tracepoint
->exclusions
, index
);