2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <common/macros.h>
13 #include <lttng/condition/condition-internal.h>
14 #include <lttng/condition/event-rule-matches-internal.h>
15 #include <lttng/condition/event-rule-matches.h>
16 #include <lttng/event-expr-internal.h>
17 #include <lttng/event-expr.h>
18 #include <lttng/event-field-value-internal.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/lttng-error.h>
23 #include <vendor/msgpack/msgpack.h>
25 #define IS_EVENT_RULE_MATCHES_CONDITION(condition) \
26 (lttng_condition_get_type(condition) == \
27 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES)
29 static bool is_event_rule_matches_evaluation(
30 const struct lttng_evaluation
*evaluation
)
32 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
34 return type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
37 static bool lttng_condition_event_rule_matches_validate(
38 const struct lttng_condition
*condition
);
39 static int lttng_condition_event_rule_matches_serialize(
40 const struct lttng_condition
*condition
,
41 struct lttng_payload
*payload
);
42 static bool lttng_condition_event_rule_matches_is_equal(
43 const struct lttng_condition
*_a
,
44 const struct lttng_condition
*_b
);
45 static void lttng_condition_event_rule_matches_destroy(
46 struct lttng_condition
*condition
);
48 static bool lttng_condition_event_rule_matches_validate(
49 const struct lttng_condition
*condition
)
52 struct lttng_condition_event_rule_matches
*event_rule
;
58 event_rule
= container_of(condition
,
59 struct lttng_condition_event_rule_matches
, parent
);
60 if (!event_rule
->rule
) {
61 ERR("Invalid on event condition: a rule must be set");
65 valid
= lttng_event_rule_validate(event_rule
->rule
);
70 static const char *msgpack_object_type_str(msgpack_object_type type
)
75 case MSGPACK_OBJECT_NIL
:
76 name
= "MSGPACK_OBJECT_NIL";
78 case MSGPACK_OBJECT_BOOLEAN
:
79 name
= "MSGPACK_OBJECT_BOOLEAN";
81 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
82 name
= "MSGPACK_OBJECT_POSITIVE_INTEGER";
84 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
85 name
= "MSGPACK_OBJECT_NEGATIVE_INTEGER";
87 case MSGPACK_OBJECT_FLOAT32
:
88 name
= "MSGPACK_OBJECT_FLOAT32";
90 case MSGPACK_OBJECT_FLOAT
:
91 /* Same value as MSGPACK_OBJECT_FLOAT64 */
92 name
= "MSGPACK_OBJECT_FLOAT(64)";
94 case MSGPACK_OBJECT_STR
:
95 name
= "MSGPACK_OBJECT_STR";
97 case MSGPACK_OBJECT_ARRAY
:
98 name
= "MSGPACK_OBJECT_ARRAY";
100 case MSGPACK_OBJECT_MAP
:
101 name
= "MSGPACK_OBJECT_MAP";
103 case MSGPACK_OBJECT_BIN
:
104 name
= "MSGPACK_OBJECT_BIN";
106 case MSGPACK_OBJECT_EXT
:
107 name
= "MSGPACK_OBJECT_EXT";
117 * Serializes the C string `str` into `buf`.
119 * Encoding is the length of `str` plus one (for the null character),
120 * and then the string, including its null terminator.
123 int serialize_cstr(const char *str
, struct lttng_dynamic_buffer
*buf
)
126 const uint32_t len
= strlen(str
) + 1;
128 /* Serialize the length, including the null terminator. */
129 DBG("Serializing C string's length (including null terminator): "
131 ret
= lttng_dynamic_buffer_append(buf
, &len
, sizeof(len
));
136 /* Serialize the string. */
137 DBG("Serializing C string: '%s'", str
);
138 ret
= lttng_dynamic_buffer_append(buf
, str
, len
);
148 * Serializes the event expression `expr` into `buf`.
151 int serialize_event_expr(const struct lttng_event_expr
*expr
,
152 struct lttng_payload
*payload
)
154 const uint8_t type
= expr
->type
;
157 /* Serialize the expression's type. */
158 DBG("Serializing event expression's type: %d", expr
->type
);
159 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &type
, sizeof(type
));
164 /* Serialize the expression */
165 switch (expr
->type
) {
166 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
167 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
169 const struct lttng_event_expr_field
*field_expr
=
171 const struct lttng_event_expr_field
,
174 /* Serialize the field name. */
175 DBG("Serializing field event expression's field name: '%s'",
177 ret
= serialize_cstr(field_expr
->name
, &payload
->buffer
);
184 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
186 const struct lttng_event_expr_app_specific_context_field
*field_expr
=
188 const struct lttng_event_expr_app_specific_context_field
,
191 /* Serialize the provider name. */
192 DBG("Serializing app-specific context field event expression's "
193 "provider name: '%s'",
194 field_expr
->provider_name
);
195 ret
= serialize_cstr(field_expr
->provider_name
, &payload
->buffer
);
200 /* Serialize the type name. */
201 DBG("Serializing app-specific context field event expression's "
203 field_expr
->provider_name
);
204 ret
= serialize_cstr(field_expr
->type_name
, &payload
->buffer
);
211 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
213 const struct lttng_event_expr_array_field_element
*elem_expr
=
215 const struct lttng_event_expr_array_field_element
,
217 const uint32_t index
= elem_expr
->index
;
219 /* Serialize the index. */
220 DBG("Serializing array field element event expression's "
221 "index: %u", elem_expr
->index
);
222 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &index
, sizeof(index
));
227 /* Serialize the parent array field expression. */
228 DBG("Serializing array field element event expression's "
229 "parent array field event expression");
230 ret
= serialize_event_expr(elem_expr
->array_field_expr
, payload
);
245 static struct lttng_capture_descriptor
*
246 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
247 const struct lttng_condition
*condition
, unsigned int index
)
249 const struct lttng_condition_event_rule_matches
250 *event_rule_matches_cond
= container_of(condition
,
251 const struct lttng_condition_event_rule_matches
,
253 struct lttng_capture_descriptor
*desc
= NULL
;
255 enum lttng_condition_status status
;
257 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
261 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
263 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
267 if (index
>= count
) {
271 desc
= lttng_dynamic_pointer_array_get_pointer(
272 &event_rule_matches_cond
->capture_descriptors
, index
);
277 static int lttng_condition_event_rule_matches_serialize(
278 const struct lttng_condition
*condition
,
279 struct lttng_payload
*payload
)
282 struct lttng_condition_event_rule_matches
*event_rule_matches_condition
;
283 enum lttng_condition_status status
;
284 /* Used for iteration and communication (size matters). */
285 uint32_t i
, capture_descr_count
;
287 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
292 DBG("Serializing on event condition");
293 event_rule_matches_condition
= container_of(condition
,
294 struct lttng_condition_event_rule_matches
, parent
);
296 DBG("Serializing on event condition's event rule");
297 ret
= lttng_event_rule_serialize(
298 event_rule_matches_condition
->rule
, payload
);
303 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
304 condition
, &capture_descr_count
);
305 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
310 DBG("Serializing on event condition's capture descriptor count: %" PRIu32
,
311 capture_descr_count
);
312 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_descr_count
,
313 sizeof(capture_descr_count
));
318 for (i
= 0; i
< capture_descr_count
; i
++) {
319 const struct lttng_capture_descriptor
*desc
=
320 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
323 DBG("Serializing on event condition's capture descriptor %" PRIu32
,
325 ret
= serialize_event_expr(desc
->event_expression
, payload
);
336 bool capture_descriptors_are_equal(
337 const struct lttng_condition
*condition_a
,
338 const struct lttng_condition
*condition_b
)
340 bool is_equal
= true;
341 unsigned int capture_descr_count_a
;
342 unsigned int capture_descr_count_b
;
344 enum lttng_condition_status status
;
346 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
347 condition_a
, &capture_descr_count_a
);
348 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
352 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
353 condition_b
, &capture_descr_count_b
);
354 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
358 if (capture_descr_count_a
!= capture_descr_count_b
) {
362 for (i
= 0; i
< capture_descr_count_a
; i
++) {
363 const struct lttng_event_expr
*expr_a
=
364 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
366 const struct lttng_event_expr
*expr_b
=
367 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
370 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
384 static bool lttng_condition_event_rule_matches_is_equal(
385 const struct lttng_condition
*_a
,
386 const struct lttng_condition
*_b
)
388 bool is_equal
= false;
389 struct lttng_condition_event_rule_matches
*a
, *b
;
391 a
= container_of(_a
, struct lttng_condition_event_rule_matches
, parent
);
392 b
= container_of(_b
, struct lttng_condition_event_rule_matches
, parent
);
394 /* Both event rules must be set or both must be unset. */
395 if ((a
->rule
&& !b
->rule
) || (!a
->rule
&& b
->rule
)) {
396 WARN("Comparing event_rule conditions with uninitialized rule");
400 is_equal
= lttng_event_rule_is_equal(a
->rule
, b
->rule
);
405 is_equal
= capture_descriptors_are_equal(_a
, _b
);
411 static void lttng_condition_event_rule_matches_destroy(
412 struct lttng_condition
*condition
)
414 struct lttng_condition_event_rule_matches
*event_rule_matches_condition
;
416 event_rule_matches_condition
= container_of(condition
,
417 struct lttng_condition_event_rule_matches
, parent
);
419 lttng_event_rule_put(event_rule_matches_condition
->rule
);
420 lttng_dynamic_pointer_array_reset(
421 &event_rule_matches_condition
->capture_descriptors
);
422 free(event_rule_matches_condition
);
426 void destroy_capture_descriptor(void *ptr
)
428 struct lttng_capture_descriptor
*desc
=
429 (struct lttng_capture_descriptor
*) ptr
;
431 lttng_event_expr_destroy(desc
->event_expression
);
432 free(desc
->bytecode
);
436 struct lttng_condition
*lttng_condition_event_rule_matches_create(
437 struct lttng_event_rule
*rule
)
439 struct lttng_condition
*parent
= NULL
;
440 struct lttng_condition_event_rule_matches
*condition
= NULL
;
446 condition
= zmalloc(sizeof(struct lttng_condition_event_rule_matches
));
451 lttng_condition_init(&condition
->parent
,
452 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
453 condition
->parent
.validate
=
454 lttng_condition_event_rule_matches_validate
,
455 condition
->parent
.serialize
=
456 lttng_condition_event_rule_matches_serialize
,
457 condition
->parent
.equal
= lttng_condition_event_rule_matches_is_equal
,
458 condition
->parent
.destroy
= lttng_condition_event_rule_matches_destroy
,
460 lttng_event_rule_get(rule
);
461 condition
->rule
= rule
;
464 lttng_dynamic_pointer_array_init(&condition
->capture_descriptors
,
465 destroy_capture_descriptor
);
467 parent
= &condition
->parent
;
473 uint64_t uint_from_buffer(const struct lttng_buffer_view
*view
, size_t size
,
477 const struct lttng_buffer_view uint_view
=
478 lttng_buffer_view_from_view(view
, *offset
, size
);
480 if (!lttng_buffer_view_is_valid(&uint_view
)) {
487 ret
= (uint64_t) *uint_view
.data
;
489 case sizeof(uint32_t):
493 memcpy(&u32
, uint_view
.data
, sizeof(u32
));
494 ret
= (uint64_t) u32
;
498 memcpy(&ret
, uint_view
.data
, sizeof(ret
));
511 const char *str_from_buffer(const struct lttng_buffer_view
*view
,
517 len
= uint_from_buffer(view
, sizeof(uint32_t), offset
);
518 if (len
== UINT64_C(-1)) {
522 ret
= &view
->data
[*offset
];
524 if (!lttng_buffer_view_contains_string(view
, ret
, len
)) {
539 struct lttng_event_expr
*event_expr_from_payload(
540 struct lttng_payload_view
*view
, size_t *offset
)
542 struct lttng_event_expr
*expr
= NULL
;
546 type
= uint_from_buffer(&view
->buffer
, sizeof(uint8_t), offset
);
547 if (type
== UINT64_C(-1)) {
552 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
553 str
= str_from_buffer(&view
->buffer
, offset
);
558 expr
= lttng_event_expr_event_payload_field_create(str
);
560 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
561 str
= str_from_buffer(&view
->buffer
, offset
);
566 expr
= lttng_event_expr_channel_context_field_create(str
);
568 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
570 const char *provider_name
;
571 const char *type_name
;
573 provider_name
= str_from_buffer(&view
->buffer
, offset
);
574 if (!provider_name
) {
578 type_name
= str_from_buffer(&view
->buffer
, offset
);
583 expr
= lttng_event_expr_app_specific_context_field_create(
584 provider_name
, type_name
);
587 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
589 struct lttng_event_expr
*array_field_expr
;
590 const uint64_t index
= uint_from_buffer(
591 &view
->buffer
, sizeof(uint32_t), offset
);
593 if (index
== UINT64_C(-1)) {
597 /* Array field expression is the encoded after this. */
598 array_field_expr
= event_expr_from_payload(view
, offset
);
599 if (!array_field_expr
) {
603 /* Move ownership of `array_field_expr` to new expression. */
604 expr
= lttng_event_expr_array_field_element_create(
605 array_field_expr
, (unsigned int) index
);
607 /* `array_field_expr` not moved: destroy it. */
608 lttng_event_expr_destroy(array_field_expr
);
614 ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64
,
622 lttng_event_expr_destroy(expr
);
630 ssize_t
lttng_condition_event_rule_matches_create_from_payload(
631 struct lttng_payload_view
*view
,
632 struct lttng_condition
**_condition
)
634 ssize_t consumed_length
;
636 ssize_t event_rule_length
;
637 uint32_t i
, capture_descr_count
;
638 struct lttng_condition
*condition
= NULL
;
639 struct lttng_event_rule
*event_rule
= NULL
;
641 if (!view
|| !_condition
) {
645 /* Struct lttng_event_rule. */
647 struct lttng_payload_view event_rule_view
=
648 lttng_payload_view_from_view(view
, offset
, -1);
650 event_rule_length
= lttng_event_rule_create_from_payload(
651 &event_rule_view
, &event_rule
);
654 if (event_rule_length
< 0 || !event_rule
) {
658 offset
+= event_rule_length
;
660 /* Create condition (no capture descriptors yet) at this point */
661 condition
= lttng_condition_event_rule_matches_create(event_rule
);
666 /* Capture descriptor count. */
667 assert(event_rule_length
>= 0);
668 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
669 if (capture_descr_count
== UINT32_C(-1)) {
673 /* Capture descriptors. */
674 for (i
= 0; i
< capture_descr_count
; i
++) {
675 enum lttng_condition_status status
;
676 struct lttng_event_expr
*expr
= event_expr_from_payload(
683 /* Move ownership of `expr` to `condition`. */
684 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
686 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
687 /* `expr` not moved: destroy it. */
688 lttng_event_expr_destroy(expr
);
693 consumed_length
= (ssize_t
) offset
;
694 *_condition
= condition
;
699 consumed_length
= -1;
702 lttng_event_rule_put(event_rule
);
703 lttng_condition_put(condition
);
704 return consumed_length
;
708 enum lttng_condition_status
709 lttng_condition_event_rule_matches_borrow_rule_mutable(
710 const struct lttng_condition
*condition
,
711 struct lttng_event_rule
**rule
)
713 struct lttng_condition_event_rule_matches
*event_rule
;
714 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
716 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
718 status
= LTTNG_CONDITION_STATUS_INVALID
;
722 event_rule
= container_of(condition
,
723 struct lttng_condition_event_rule_matches
, parent
);
724 if (!event_rule
->rule
) {
725 status
= LTTNG_CONDITION_STATUS_UNSET
;
729 *rule
= event_rule
->rule
;
734 enum lttng_condition_status
lttng_condition_event_rule_matches_get_rule(
735 const struct lttng_condition
*condition
,
736 const struct lttng_event_rule
**rule
)
738 struct lttng_event_rule
*mutable_rule
= NULL
;
739 const enum lttng_condition_status status
=
740 lttng_condition_event_rule_matches_borrow_rule_mutable(
741 condition
, &mutable_rule
);
743 *rule
= mutable_rule
;
748 void lttng_condition_event_rule_matches_set_error_counter_index(
749 struct lttng_condition
*condition
, uint64_t error_counter_index
)
751 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
752 container_of(condition
,
753 struct lttng_condition_event_rule_matches
,
756 LTTNG_OPTIONAL_SET(&event_rule_matches_cond
->error_counter_index
,
757 error_counter_index
);
761 uint64_t lttng_condition_event_rule_matches_get_error_counter_index(
762 const struct lttng_condition
*condition
)
764 const struct lttng_condition_event_rule_matches
765 *event_rule_matches_cond
= container_of(condition
,
766 const struct lttng_condition_event_rule_matches
,
769 return LTTNG_OPTIONAL_GET(event_rule_matches_cond
->error_counter_index
);
772 enum lttng_condition_status
773 lttng_condition_event_rule_matches_append_capture_descriptor(
774 struct lttng_condition
*condition
,
775 struct lttng_event_expr
*expr
)
778 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
779 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
780 container_of(condition
,
781 struct lttng_condition_event_rule_matches
,
783 struct lttng_capture_descriptor
*descriptor
= NULL
;
784 const struct lttng_event_rule
*rule
= NULL
;
786 /* Only accept l-values. */
787 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
788 !expr
|| !lttng_event_expr_is_lvalue(expr
)) {
789 status
= LTTNG_CONDITION_STATUS_INVALID
;
793 status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
794 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
798 switch(lttng_event_rule_get_type(rule
)) {
799 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
800 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
801 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
802 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
803 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
804 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
806 status
= LTTNG_CONDITION_STATUS_OK
;
808 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
809 status
= LTTNG_CONDITION_STATUS_INVALID
;
812 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
816 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
820 descriptor
= malloc(sizeof(*descriptor
));
821 if (descriptor
== NULL
) {
822 status
= LTTNG_CONDITION_STATUS_ERROR
;
826 descriptor
->event_expression
= expr
;
827 descriptor
->bytecode
= NULL
;
829 ret
= lttng_dynamic_pointer_array_add_pointer(
830 &event_rule_matches_cond
->capture_descriptors
,
833 status
= LTTNG_CONDITION_STATUS_ERROR
;
837 /* Ownership is transfered to the internal capture_descriptors array */
844 enum lttng_condition_status
845 lttng_condition_event_rule_matches_get_capture_descriptor_count(
846 const struct lttng_condition
*condition
, unsigned int *count
)
848 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
849 const struct lttng_condition_event_rule_matches
850 *event_rule_matches_condition
= container_of(condition
,
851 const struct lttng_condition_event_rule_matches
,
854 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
856 status
= LTTNG_CONDITION_STATUS_INVALID
;
860 *count
= lttng_dynamic_pointer_array_get_count(
861 &event_rule_matches_condition
->capture_descriptors
);
867 const struct lttng_event_expr
*
868 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
869 const struct lttng_condition
*condition
, unsigned int index
)
871 const struct lttng_event_expr
*expr
= NULL
;
872 const struct lttng_capture_descriptor
*desc
= NULL
;
874 desc
= lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
879 expr
= desc
->event_expression
;
886 ssize_t
lttng_evaluation_event_rule_matches_create_from_payload(
887 const struct lttng_condition_event_rule_matches
*condition
,
888 struct lttng_payload_view
*view
,
889 struct lttng_evaluation
**_evaluation
)
891 ssize_t ret
, offset
= 0;
892 struct lttng_evaluation
*evaluation
= NULL
;
893 uint32_t capture_payload_size
;
894 const char *capture_payload
= NULL
;
902 const struct lttng_payload_view current_view
=
903 lttng_payload_view_from_view(view
, offset
, -1);
905 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
910 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
911 sizeof(capture_payload_size
));
913 offset
+= sizeof(capture_payload_size
);
915 if (capture_payload_size
> 0) {
916 const struct lttng_payload_view current_view
=
917 lttng_payload_view_from_view(view
, offset
, -1);
919 if (current_view
.buffer
.size
< capture_payload_size
) {
924 capture_payload
= current_view
.buffer
.data
;
927 evaluation
= lttng_evaluation_event_rule_matches_create(
928 condition
, capture_payload
, capture_payload_size
, true);
934 offset
+= capture_payload_size
;
935 *_evaluation
= evaluation
;
940 lttng_evaluation_destroy(evaluation
);
944 static int lttng_evaluation_event_rule_matches_serialize(
945 const struct lttng_evaluation
*evaluation
,
946 struct lttng_payload
*payload
)
949 struct lttng_evaluation_event_rule_matches
*hit
;
950 uint32_t capture_payload_size
;
952 hit
= container_of(evaluation
,
953 struct lttng_evaluation_event_rule_matches
, parent
);
955 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
956 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
957 sizeof(capture_payload_size
));
962 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
963 hit
->capture_payload
.size
);
973 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
975 bool is_equal
= true;
977 assert(obj
->type
== MSGPACK_OBJECT_STR
);
979 if (obj
->via
.str
.size
!= strlen(str
)) {
984 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
994 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
997 const msgpack_object
*ret
= NULL
;
1000 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
1002 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
1003 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
1005 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
1007 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1017 static void lttng_evaluation_event_rule_matches_destroy(
1018 struct lttng_evaluation
*evaluation
)
1020 struct lttng_evaluation_event_rule_matches
*hit
;
1022 hit
= container_of(evaluation
,
1023 struct lttng_evaluation_event_rule_matches
, parent
);
1024 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1025 lttng_event_field_value_destroy(hit
->captured_values
);
1030 int event_field_value_from_obj(const msgpack_object
*obj
,
1031 struct lttng_event_field_value
**field_val
)
1038 switch (obj
->type
) {
1039 case MSGPACK_OBJECT_NIL
:
1043 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1044 *field_val
= lttng_event_field_value_uint_create(
1047 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1048 *field_val
= lttng_event_field_value_int_create(
1051 case MSGPACK_OBJECT_FLOAT32
:
1052 case MSGPACK_OBJECT_FLOAT64
:
1053 *field_val
= lttng_event_field_value_real_create(
1056 case MSGPACK_OBJECT_STR
:
1057 *field_val
= lttng_event_field_value_string_create_with_size(
1058 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1060 case MSGPACK_OBJECT_ARRAY
:
1064 *field_val
= lttng_event_field_value_array_create();
1069 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1070 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1071 struct lttng_event_field_value
*elem_field_val
;
1073 ret
= event_field_value_from_obj(elem_obj
,
1079 if (elem_field_val
) {
1080 ret
= lttng_event_field_value_array_append(
1081 *field_val
, elem_field_val
);
1083 ret
= lttng_event_field_value_array_append_unavailable(
1088 lttng_event_field_value_destroy(elem_field_val
);
1095 case MSGPACK_OBJECT_MAP
:
1098 * As of this version, the only valid map object is
1099 * for an enumeration value, for example:
1106 * - Carling Black Label
1108 const msgpack_object
*inner_obj
;
1111 inner_obj
= get_msgpack_map_obj(obj
, "type");
1113 ERR("Missing `type` entry in map object");
1117 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1118 ERR("Map object's `type` entry is not a string: type = %s",
1119 msgpack_object_type_str(inner_obj
->type
));
1123 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1124 ERR("Map object's `type` entry: expecting `enum`");
1128 inner_obj
= get_msgpack_map_obj(obj
, "value");
1130 ERR("Missing `value` entry in map object");
1134 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1135 *field_val
= lttng_event_field_value_enum_uint_create(
1136 inner_obj
->via
.u64
);
1137 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1138 *field_val
= lttng_event_field_value_enum_int_create(
1139 inner_obj
->via
.i64
);
1141 ERR("Map object's `value` entry is not an integer: type = %s",
1142 msgpack_object_type_str(inner_obj
->type
));
1150 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1156 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1157 ERR("Map object's `labels` entry is not an array: type = %s",
1158 msgpack_object_type_str(inner_obj
->type
));
1162 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1165 const msgpack_object
*elem_obj
=
1166 &inner_obj
->via
.array
.ptr
[label_i
];
1168 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1169 ERR("Map object's `labels` entry's type is not a string: type = %s",
1170 msgpack_object_type_str(elem_obj
->type
));
1174 iret
= lttng_event_field_value_enum_append_label_with_size(
1175 *field_val
, elem_obj
->via
.str
.ptr
,
1176 elem_obj
->via
.str
.size
);
1185 ERR("Unexpected object type: type = %s",
1186 msgpack_object_type_str(obj
->type
));
1197 lttng_event_field_value_destroy(*field_val
);
1205 static struct lttng_event_field_value
*event_field_value_from_capture_payload(
1206 const struct lttng_condition_event_rule_matches
*condition
,
1207 const char *capture_payload
,
1208 size_t capture_payload_size
)
1210 struct lttng_event_field_value
*ret
= NULL
;
1211 msgpack_unpacked unpacked
;
1212 msgpack_unpack_return unpack_return
;
1213 const msgpack_object
*root_obj
;
1214 const msgpack_object_array
*root_array_obj
;
1219 assert(capture_payload
);
1221 /* Initialize value. */
1222 msgpack_unpacked_init(&unpacked
);
1225 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1226 capture_payload_size
, NULL
);
1227 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1228 ERR("msgpack_unpack_next() failed to decode the "
1229 "MessagePack-encoded capture payload: "
1230 "size = %zu, ret = %d",
1231 capture_payload_size
, unpack_return
);
1235 /* Get root array. */
1236 root_obj
= &unpacked
.data
;
1238 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1239 ERR("Expecting an array as the root object: type = %s",
1240 msgpack_object_type_str(root_obj
->type
));
1244 root_array_obj
= &root_obj
->via
.array
;
1246 /* Create an empty root array event field value. */
1247 ret
= lttng_event_field_value_array_create();
1253 * For each capture descriptor in the condition object:
1255 * 1. Get its corresponding captured field value MessagePack
1258 * 2. Create a corresponding event field value.
1260 * 3. Append it to `ret` (the root array event field value).
1262 count
= lttng_dynamic_pointer_array_get_count(
1263 &condition
->capture_descriptors
);
1266 for (i
= 0; i
< count
; i
++) {
1267 const struct lttng_capture_descriptor
*capture_descriptor
=
1268 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1269 &condition
->parent
, i
);
1270 const msgpack_object
*elem_obj
;
1271 struct lttng_event_field_value
*elem_field_val
;
1274 assert(capture_descriptor
);
1276 elem_obj
= &root_array_obj
->ptr
[i
];
1277 iret
= event_field_value_from_obj(elem_obj
,
1283 if (elem_field_val
) {
1284 iret
= lttng_event_field_value_array_append(ret
,
1287 iret
= lttng_event_field_value_array_append_unavailable(
1292 lttng_event_field_value_destroy(elem_field_val
);
1300 lttng_event_field_value_destroy(ret
);
1304 msgpack_unpacked_destroy(&unpacked
);
1309 struct lttng_evaluation
*lttng_evaluation_event_rule_matches_create(
1310 const struct lttng_condition_event_rule_matches
*condition
,
1311 const char *capture_payload
,
1312 size_t capture_payload_size
,
1313 bool decode_capture_payload
)
1315 struct lttng_evaluation_event_rule_matches
*hit
;
1316 struct lttng_evaluation
*evaluation
= NULL
;
1318 hit
= zmalloc(sizeof(struct lttng_evaluation_event_rule_matches
));
1323 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1325 if (capture_payload
) {
1326 const int ret
= lttng_dynamic_buffer_append(
1327 &hit
->capture_payload
, capture_payload
,
1328 capture_payload_size
);
1330 ERR("Failed to initialize capture payload of event rule evaluation");
1334 if (decode_capture_payload
) {
1335 hit
->captured_values
=
1336 event_field_value_from_capture_payload(
1339 capture_payload_size
);
1340 if (!hit
->captured_values
) {
1341 ERR("Failed to decode the capture payload: size = %zu",
1342 capture_payload_size
);
1348 hit
->parent
.type
= LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
1349 hit
->parent
.serialize
= lttng_evaluation_event_rule_matches_serialize
;
1350 hit
->parent
.destroy
= lttng_evaluation_event_rule_matches_destroy
;
1352 evaluation
= &hit
->parent
;
1357 lttng_evaluation_event_rule_matches_destroy(&hit
->parent
);
1363 enum lttng_evaluation_event_rule_matches_status
1364 lttng_evaluation_event_rule_matches_get_captured_values(
1365 const struct lttng_evaluation
*evaluation
,
1366 const struct lttng_event_field_value
**field_val
)
1368 struct lttng_evaluation_event_rule_matches
*hit
;
1369 enum lttng_evaluation_event_rule_matches_status status
=
1370 LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_OK
;
1372 if (!evaluation
|| !is_event_rule_matches_evaluation(evaluation
) ||
1374 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_INVALID
;
1378 hit
= container_of(evaluation
,
1379 struct lttng_evaluation_event_rule_matches
, parent
);
1380 if (!hit
->captured_values
) {
1381 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_NONE
;
1385 *field_val
= hit
->captured_values
;
1392 enum lttng_error_code
1393 lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
1394 struct lttng_condition
*condition
)
1396 enum lttng_error_code ret
;
1397 enum lttng_condition_status status
;
1398 unsigned int capture_count
, i
;
1400 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1401 ret
= LTTNG_ERR_FATAL
;
1405 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1406 condition
, &capture_count
);
1407 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1408 ret
= LTTNG_ERR_FATAL
;
1412 for (i
= 0; i
< capture_count
; i
++) {
1413 struct lttng_capture_descriptor
*local_capture_desc
=
1414 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1417 if (local_capture_desc
== NULL
) {
1418 ret
= LTTNG_ERR_FATAL
;
1422 /* Generate the bytecode. */
1423 status
= lttng_event_expr_to_bytecode(
1424 local_capture_desc
->event_expression
,
1425 &local_capture_desc
->bytecode
);
1426 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1427 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1432 /* Everything went better than expected */
1440 const struct lttng_bytecode
*
1441 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
1442 const struct lttng_condition
*condition
, unsigned int index
)
1444 const struct lttng_condition_event_rule_matches
1445 *event_rule_matches_cond
= container_of(condition
,
1446 const struct lttng_condition_event_rule_matches
,
1448 struct lttng_capture_descriptor
*desc
= NULL
;
1449 struct lttng_bytecode
*bytecode
= NULL
;
1451 enum lttng_condition_status status
;
1453 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1457 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1459 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1463 if (index
>= count
) {
1467 desc
= lttng_dynamic_pointer_array_get_pointer(
1468 &event_rule_matches_cond
->capture_descriptors
, index
);
1473 bytecode
= desc
->bytecode
;