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/event-expr-to-bytecode.h>
11 #include <common/macros.h>
14 #include <lttng/condition/condition-internal.h>
15 #include <lttng/event-rule/event-rule-internal.h>
16 #include <lttng/condition/event-rule-internal.h>
17 #include <lttng/condition/event-rule.h>
18 #include <lttng/event-expr-internal.h>
19 #include <lttng/event-expr.h>
20 #include <lttng/event-field-value-internal.h>
21 #include <lttng/event-rule/event-rule-internal.h>
22 #include <lttng/lttng-error.h>
25 #include <vendor/msgpack/msgpack.h>
27 #define IS_EVENT_RULE_CONDITION(condition) \
28 (lttng_condition_get_type(condition) == \
29 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT)
31 static bool is_event_rule_evaluation(const struct lttng_evaluation
*evaluation
)
33 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
35 return type
== LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
;
38 static bool lttng_condition_event_rule_validate(
39 const struct lttng_condition
*condition
);
40 static int lttng_condition_event_rule_serialize(
41 const struct lttng_condition
*condition
,
42 struct lttng_payload
*payload
);
43 static bool lttng_condition_event_rule_is_equal(
44 const struct lttng_condition
*_a
,
45 const struct lttng_condition
*_b
);
46 static void lttng_condition_event_rule_destroy(
47 struct lttng_condition
*condition
);
49 static bool lttng_condition_event_rule_validate(
50 const struct lttng_condition
*condition
)
53 struct lttng_condition_event_rule
*event_rule
;
59 event_rule
= container_of(
60 condition
, struct lttng_condition_event_rule
, parent
);
61 if (!event_rule
->rule
) {
62 ERR("Invalid event rule condition: a rule must be set");
66 valid
= lttng_event_rule_validate(event_rule
->rule
);
71 static const char *msgpack_object_type_str(msgpack_object_type type
)
76 case MSGPACK_OBJECT_NIL
:
77 name
= "MSGPACK_OBJECT_NIL";
79 case MSGPACK_OBJECT_BOOLEAN
:
80 name
= "MSGPACK_OBJECT_BOOLEAN";
82 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
83 name
= "MSGPACK_OBJECT_POSITIVE_INTEGER";
85 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
86 name
= "MSGPACK_OBJECT_NEGATIVE_INTEGER";
88 case MSGPACK_OBJECT_FLOAT32
:
89 name
= "MSGPACK_OBJECT_FLOAT32";
91 case MSGPACK_OBJECT_FLOAT
:
92 /* Same value as MSGPACK_OBJECT_FLOAT64 */
93 name
= "MSGPACK_OBJECT_FLOAT(64)";
95 case MSGPACK_OBJECT_STR
:
96 name
= "MSGPACK_OBJECT_STR";
98 case MSGPACK_OBJECT_ARRAY
:
99 name
= "MSGPACK_OBJECT_ARRAY";
101 case MSGPACK_OBJECT_MAP
:
102 name
= "MSGPACK_OBJECT_MAP";
104 case MSGPACK_OBJECT_BIN
:
105 name
= "MSGPACK_OBJECT_BIN";
107 case MSGPACK_OBJECT_EXT
:
108 name
= "MSGPACK_OBJECT_EXT";
118 * Serializes the C string `str` into `buf`.
120 * Encoding is the length of `str` plus one (for the null character),
121 * and then the string, including its null terminator.
124 int serialize_cstr(const char *str
, struct lttng_dynamic_buffer
*buf
)
127 const uint32_t len
= strlen(str
) + 1;
129 /* Serialize the length, including the null terminator. */
130 DBG("Serializing C string's length (including null terminator): "
132 ret
= lttng_dynamic_buffer_append(buf
, &len
, sizeof(len
));
137 /* Serialize the string. */
138 DBG("Serializing C string: '%s'", str
);
139 ret
= lttng_dynamic_buffer_append(buf
, str
, len
);
149 * Serializes the event expression `expr` into `buf`.
152 int serialize_event_expr(const struct lttng_event_expr
*expr
,
153 struct lttng_payload
*payload
)
155 const uint8_t type
= expr
->type
;
158 /* Serialize the expression's type. */
159 DBG("Serializing event expression's type: %d", expr
->type
);
160 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &type
, sizeof(type
));
165 /* Serialize the expression */
166 switch (expr
->type
) {
167 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
168 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
170 const struct lttng_event_expr_field
*field_expr
=
172 const struct lttng_event_expr_field
,
175 /* Serialize the field name. */
176 DBG("Serializing field event expression's field name: '%s'",
178 ret
= serialize_cstr(field_expr
->name
, &payload
->buffer
);
185 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
187 const struct lttng_event_expr_app_specific_context_field
*field_expr
=
189 const struct lttng_event_expr_app_specific_context_field
,
192 /* Serialize the provider name. */
193 DBG("Serializing app-specific context field event expression's "
194 "provider name: '%s'",
195 field_expr
->provider_name
);
196 ret
= serialize_cstr(field_expr
->provider_name
, &payload
->buffer
);
201 /* Serialize the type name. */
202 DBG("Serializing app-specific context field event expression's "
204 field_expr
->provider_name
);
205 ret
= serialize_cstr(field_expr
->type_name
, &payload
->buffer
);
212 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
214 const struct lttng_event_expr_array_field_element
*elem_expr
=
216 const struct lttng_event_expr_array_field_element
,
218 const uint32_t index
= elem_expr
->index
;
220 /* Serialize the index. */
221 DBG("Serializing array field element event expression's "
222 "index: %u", elem_expr
->index
);
223 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &index
, sizeof(index
));
228 /* Serialize the parent array field expression. */
229 DBG("Serializing array field element event expression's "
230 "parent array field event expression");
231 ret
= serialize_event_expr(elem_expr
->array_field_expr
, payload
);
247 struct lttng_capture_descriptor
*
248 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
249 const struct lttng_condition
*condition
, unsigned int index
)
251 const struct lttng_condition_event_rule
*event_rule_cond
=
252 container_of(condition
,
253 const struct lttng_condition_event_rule
,
255 struct lttng_capture_descriptor
*desc
= NULL
;
257 enum lttng_condition_status status
;
259 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
263 status
= lttng_condition_event_rule_get_capture_descriptor_count(
265 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
269 if (index
>= count
) {
273 desc
= lttng_dynamic_pointer_array_get_pointer(
274 &event_rule_cond
->capture_descriptors
, index
);
279 static int lttng_condition_event_rule_serialize(
280 const struct lttng_condition
*condition
,
281 struct lttng_payload
*payload
)
284 struct lttng_condition_event_rule
*event_rule
;
285 enum lttng_condition_status status
;
286 /* Used for iteration and communication (size matters). */
287 uint32_t i
, capture_descr_count
;
289 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
294 DBG("Serializing event rule condition");
295 event_rule
= container_of(
296 condition
, struct lttng_condition_event_rule
, parent
);
298 DBG("Serializing event rule condition's event rule");
299 ret
= lttng_event_rule_serialize(event_rule
->rule
, payload
);
304 status
= lttng_condition_event_rule_get_capture_descriptor_count(
305 condition
, &capture_descr_count
);
306 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
311 DBG("Serializing event rule condition's capture descriptor count: %" PRIu32
,
312 capture_descr_count
);
313 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_descr_count
,
314 sizeof(capture_descr_count
));
319 for (i
= 0; i
< capture_descr_count
; i
++) {
320 const struct lttng_capture_descriptor
*desc
=
321 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
324 DBG("Serializing event rule condition's capture descriptor %" PRIu32
,
326 ret
= serialize_event_expr(desc
->event_expression
, payload
);
337 bool capture_descriptors_are_equal(
338 const struct lttng_condition
*condition_a
,
339 const struct lttng_condition
*condition_b
)
341 bool is_equal
= true;
342 unsigned int capture_descr_count_a
;
343 unsigned int capture_descr_count_b
;
345 enum lttng_condition_status status
;
347 status
= lttng_condition_event_rule_get_capture_descriptor_count(
348 condition_a
, &capture_descr_count_a
);
349 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
353 status
= lttng_condition_event_rule_get_capture_descriptor_count(
354 condition_b
, &capture_descr_count_b
);
355 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
359 if (capture_descr_count_a
!= capture_descr_count_b
) {
363 for (i
= 0; i
< capture_descr_count_a
; i
++) {
364 const struct lttng_event_expr
*expr_a
=
365 lttng_condition_event_rule_get_capture_descriptor_at_index(
368 const struct lttng_event_expr
*expr_b
=
369 lttng_condition_event_rule_get_capture_descriptor_at_index(
373 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
387 static bool lttng_condition_event_rule_is_equal(
388 const struct lttng_condition
*_a
,
389 const struct lttng_condition
*_b
)
391 bool is_equal
= false;
392 struct lttng_condition_event_rule
*a
, *b
;
394 a
= container_of(_a
, struct lttng_condition_event_rule
, parent
);
395 b
= container_of(_b
, struct lttng_condition_event_rule
, parent
);
397 /* Both event rules must be set or both must be unset. */
398 if ((a
->rule
&& !b
->rule
) || (!a
->rule
&& b
->rule
)) {
399 WARN("Comparing event_rule conditions with uninitialized rule");
403 is_equal
= lttng_event_rule_is_equal(a
->rule
, b
->rule
);
408 is_equal
= capture_descriptors_are_equal(_a
, _b
);
414 static void lttng_condition_event_rule_destroy(
415 struct lttng_condition
*condition
)
417 struct lttng_condition_event_rule
*event_rule
;
419 event_rule
= container_of(
420 condition
, struct lttng_condition_event_rule
, parent
);
422 lttng_event_rule_put(event_rule
->rule
);
423 lttng_dynamic_pointer_array_reset(&event_rule
->capture_descriptors
);
428 void destroy_capture_descriptor(void *ptr
)
430 struct lttng_capture_descriptor
*desc
=
431 (struct lttng_capture_descriptor
*) ptr
;
433 lttng_event_expr_destroy(desc
->event_expression
);
434 free(desc
->bytecode
);
438 struct lttng_condition
*lttng_condition_event_rule_create(
439 struct lttng_event_rule
*rule
)
441 struct lttng_condition
*parent
= NULL
;
442 struct lttng_condition_event_rule
*condition
= NULL
;
448 condition
= zmalloc(sizeof(struct lttng_condition_event_rule
));
453 lttng_condition_init(&condition
->parent
,
454 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
455 condition
->parent
.validate
= lttng_condition_event_rule_validate
,
456 condition
->parent
.serialize
= lttng_condition_event_rule_serialize
,
457 condition
->parent
.equal
= lttng_condition_event_rule_is_equal
,
458 condition
->parent
.destroy
= lttng_condition_event_rule_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
);
620 lttng_event_expr_destroy(expr
);
628 ssize_t
lttng_condition_event_rule_create_from_payload(
629 struct lttng_payload_view
*view
,
630 struct lttng_condition
**_condition
)
632 ssize_t consumed_length
;
634 ssize_t event_rule_length
;
635 uint32_t i
, capture_descr_count
;
636 struct lttng_condition
*condition
= NULL
;
637 struct lttng_event_rule
*event_rule
= NULL
;
639 if (!view
|| !_condition
) {
643 /* Struct lttng_event_rule. */
645 struct lttng_payload_view event_rule_view
=
646 lttng_payload_view_from_view(view
, offset
, -1);
648 event_rule_length
= lttng_event_rule_create_from_payload(
649 &event_rule_view
, &event_rule
);
652 if (event_rule_length
< 0 || !event_rule
) {
656 /* Create condition (no capture descriptors yet) at this point. */
657 condition
= lttng_condition_event_rule_create(event_rule
);
663 /* Capture descriptor count. */
664 assert(event_rule_length
>= 0);
665 offset
+= (size_t) event_rule_length
;
666 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
667 if (capture_descr_count
== UINT32_C(-1)) {
671 /* Capture descriptors. */
672 for (i
= 0; i
< capture_descr_count
; i
++) {
673 enum lttng_condition_status status
;
674 struct lttng_event_expr
*expr
= event_expr_from_payload(
681 /* Move ownership of `expr` to `condition`. */
682 status
= lttng_condition_event_rule_append_capture_descriptor(
684 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
685 /* `expr` not moved: destroy it. */
686 lttng_event_expr_destroy(expr
);
691 consumed_length
= (ssize_t
) offset
;
692 *_condition
= condition
;
697 consumed_length
= -1;
700 lttng_event_rule_put(event_rule
);
701 lttng_condition_put(condition
);
702 return consumed_length
;
706 enum lttng_condition_status
lttng_condition_event_rule_borrow_rule_mutable(
707 const struct lttng_condition
*condition
,
708 struct lttng_event_rule
**rule
)
710 struct lttng_condition_event_rule
*event_rule
;
711 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
713 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
) || !rule
) {
714 status
= LTTNG_CONDITION_STATUS_INVALID
;
718 event_rule
= container_of(
719 condition
, struct lttng_condition_event_rule
, parent
);
720 if (!event_rule
->rule
) {
721 status
= LTTNG_CONDITION_STATUS_UNSET
;
725 *rule
= event_rule
->rule
;
730 enum lttng_condition_status
lttng_condition_event_rule_get_rule(
731 const struct lttng_condition
*condition
,
732 const struct lttng_event_rule
**rule
)
734 struct lttng_event_rule
*mutable_rule
= NULL
;
735 const enum lttng_condition_status status
=
736 lttng_condition_event_rule_borrow_rule_mutable(
737 condition
, &mutable_rule
);
739 *rule
= mutable_rule
;
743 enum lttng_condition_status
744 lttng_condition_event_rule_append_capture_descriptor(
745 struct lttng_condition
*condition
,
746 struct lttng_event_expr
*expr
)
749 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
750 struct lttng_condition_event_rule
*event_rule_cond
=
751 container_of(condition
,
752 struct lttng_condition_event_rule
, parent
);
753 struct lttng_capture_descriptor
*descriptor
= NULL
;
755 /* Only accept l-values. */
756 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
) || !expr
||
757 !lttng_event_expr_is_lvalue(expr
)) {
758 status
= LTTNG_CONDITION_STATUS_INVALID
;
762 descriptor
= malloc(sizeof(*descriptor
));
763 if (descriptor
== NULL
) {
764 status
= LTTNG_CONDITION_STATUS_ERROR
;
768 descriptor
->event_expression
= expr
;
769 descriptor
->bytecode
= NULL
;
771 ret
= lttng_dynamic_pointer_array_add_pointer(
772 &event_rule_cond
->capture_descriptors
, descriptor
);
774 status
= LTTNG_CONDITION_STATUS_ERROR
;
778 /* Ownership is transfered to the internal capture_descriptors array */
785 enum lttng_condition_status
786 lttng_condition_event_rule_get_capture_descriptor_count(
787 const struct lttng_condition
*condition
, unsigned int *count
)
789 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
790 const struct lttng_condition_event_rule
*event_rule_cond
=
791 container_of(condition
,
792 const struct lttng_condition_event_rule
,
795 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
) || !count
) {
796 status
= LTTNG_CONDITION_STATUS_INVALID
;
800 *count
= lttng_dynamic_pointer_array_get_count(
801 &event_rule_cond
->capture_descriptors
);
807 const struct lttng_event_expr
*
808 lttng_condition_event_rule_get_capture_descriptor_at_index(
809 const struct lttng_condition
*condition
, unsigned int index
)
811 const struct lttng_event_expr
*expr
= NULL
;
812 const struct lttng_capture_descriptor
*desc
= NULL
;
814 desc
= lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
819 expr
= desc
->event_expression
;
826 ssize_t
lttng_evaluation_event_rule_create_from_payload(
827 const struct lttng_condition_event_rule
*condition
,
828 struct lttng_payload_view
*view
,
829 struct lttng_evaluation
**_evaluation
)
831 ssize_t ret
, offset
= 0;
832 const char *trigger_name
;
833 struct lttng_evaluation
*evaluation
= NULL
;
834 const struct lttng_evaluation_event_rule_comm
*header
;
835 const struct lttng_payload_view header_view
=
836 lttng_payload_view_from_view(
837 view
, 0, sizeof(*header
));
838 uint32_t capture_payload_size
;
839 const char *capture_payload
= NULL
;
846 if (!lttng_payload_view_is_valid(&header_view
)) {
847 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header");
852 header
= (typeof(header
)) header_view
.buffer
.data
;
854 /* Map the originating trigger's name. */
855 offset
+= sizeof(*header
);
857 const struct lttng_payload_view current_view
=
858 lttng_payload_view_from_view(view
, offset
,
859 header
->trigger_name_length
);
861 if (!lttng_payload_view_is_valid(¤t_view
)) {
862 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
867 trigger_name
= current_view
.buffer
.data
;
868 if (!lttng_buffer_view_contains_string(¤t_view
.buffer
,
869 trigger_name
, header
->trigger_name_length
)) {
870 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
876 offset
+= header
->trigger_name_length
;
878 const struct lttng_payload_view current_view
=
879 lttng_payload_view_from_view(view
, offset
, -1);
881 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
886 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
887 sizeof(capture_payload_size
));
889 offset
+= sizeof(capture_payload_size
);
891 if (capture_payload_size
> 0) {
892 const struct lttng_payload_view current_view
=
893 lttng_payload_view_from_view(view
, offset
, -1);
895 if (current_view
.buffer
.size
< capture_payload_size
) {
900 capture_payload
= current_view
.buffer
.data
;
903 evaluation
= lttng_evaluation_event_rule_create(condition
, trigger_name
,
904 capture_payload
, capture_payload_size
, true);
910 offset
+= capture_payload_size
;
911 *_evaluation
= evaluation
;
916 lttng_evaluation_destroy(evaluation
);
920 static int lttng_evaluation_event_rule_serialize(
921 const struct lttng_evaluation
*evaluation
,
922 struct lttng_payload
*payload
)
925 struct lttng_evaluation_event_rule
*hit
;
926 struct lttng_evaluation_event_rule_comm comm
;
927 uint32_t capture_payload_size
;
930 evaluation
, struct lttng_evaluation_event_rule
, parent
);
933 comm
.trigger_name_length
= strlen(hit
->name
) + 1;
935 ret
= lttng_dynamic_buffer_append(
936 &payload
->buffer
, &comm
, sizeof(comm
));
941 ret
= lttng_dynamic_buffer_append(
942 &payload
->buffer
, hit
->name
, comm
.trigger_name_length
);
947 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
948 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
949 sizeof(capture_payload_size
));
954 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
955 hit
->capture_payload
.size
);
965 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
967 bool is_equal
= true;
969 assert(obj
->type
== MSGPACK_OBJECT_STR
);
971 if (obj
->via
.str
.size
!= strlen(str
)) {
976 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
986 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
989 const msgpack_object
*ret
= NULL
;
992 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
994 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
995 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
997 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
999 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1009 static void lttng_evaluation_event_rule_destroy(
1010 struct lttng_evaluation
*evaluation
)
1012 struct lttng_evaluation_event_rule
*hit
;
1015 evaluation
, struct lttng_evaluation_event_rule
, parent
);
1017 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1018 lttng_event_field_value_destroy(hit
->captured_values
);
1023 int event_field_value_from_obj(const msgpack_object
*obj
,
1024 struct lttng_event_field_value
**field_val
)
1031 switch (obj
->type
) {
1032 case MSGPACK_OBJECT_NIL
:
1036 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1037 *field_val
= lttng_event_field_value_uint_create(
1040 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1041 *field_val
= lttng_event_field_value_int_create(
1044 case MSGPACK_OBJECT_FLOAT32
:
1045 case MSGPACK_OBJECT_FLOAT64
:
1046 *field_val
= lttng_event_field_value_real_create(
1049 case MSGPACK_OBJECT_STR
:
1050 *field_val
= lttng_event_field_value_string_create_with_size(
1051 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1053 case MSGPACK_OBJECT_ARRAY
:
1057 *field_val
= lttng_event_field_value_array_create();
1062 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1063 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1064 struct lttng_event_field_value
*elem_field_val
;
1066 ret
= event_field_value_from_obj(elem_obj
,
1072 if (elem_field_val
) {
1073 ret
= lttng_event_field_value_array_append(
1074 *field_val
, elem_field_val
);
1076 ret
= lttng_event_field_value_array_append_unavailable(
1081 lttng_event_field_value_destroy(elem_field_val
);
1088 case MSGPACK_OBJECT_MAP
:
1091 * As of this version, the only valid map object is
1092 * for an enumeration value, for example:
1099 * - Carling Black Label
1101 const msgpack_object
*inner_obj
;
1104 inner_obj
= get_msgpack_map_obj(obj
, "type");
1106 ERR("Missing `type` entry in map object");
1110 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1111 ERR("Map object's `type` entry is not a string: type = %s",
1112 msgpack_object_type_str(inner_obj
->type
));
1116 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1117 ERR("Map object's `type` entry: expecting `enum`");
1121 inner_obj
= get_msgpack_map_obj(obj
, "value");
1123 ERR("Missing `value` entry in map object");
1127 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1128 *field_val
= lttng_event_field_value_enum_uint_create(
1129 inner_obj
->via
.u64
);
1130 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1131 *field_val
= lttng_event_field_value_enum_int_create(
1132 inner_obj
->via
.i64
);
1134 ERR("Map object's `value` entry is not an integer: type = %s",
1135 msgpack_object_type_str(inner_obj
->type
));
1143 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1149 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1150 ERR("Map object's `labels` entry is not an array: type = %s",
1151 msgpack_object_type_str(inner_obj
->type
));
1155 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1158 const msgpack_object
*elem_obj
=
1159 &inner_obj
->via
.array
.ptr
[label_i
];
1161 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1162 ERR("Map object's `labels` entry's type is not a string: type = %s",
1163 msgpack_object_type_str(elem_obj
->type
));
1167 iret
= lttng_event_field_value_enum_append_label_with_size(
1168 *field_val
, elem_obj
->via
.str
.ptr
,
1169 elem_obj
->via
.str
.size
);
1178 ERR("Unexpected object type: type = %s",
1179 msgpack_object_type_str(obj
->type
));
1190 lttng_event_field_value_destroy(*field_val
);
1199 struct lttng_event_field_value
*event_field_value_from_capture_payload(
1200 const struct lttng_condition_event_rule
*condition
,
1201 const char *capture_payload
, size_t capture_payload_size
)
1203 struct lttng_event_field_value
*ret
= NULL
;
1204 msgpack_unpacked unpacked
;
1205 msgpack_unpack_return unpack_return
;
1206 const msgpack_object
*root_obj
;
1207 const msgpack_object_array
*root_array_obj
;
1212 assert(capture_payload
);
1214 /* Initialize value. */
1215 msgpack_unpacked_init(&unpacked
);
1218 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1219 capture_payload_size
, NULL
);
1220 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1221 ERR("msgpack_unpack_next() failed to decode the "
1222 "MessagePack-encoded capture payload: "
1223 "size = %zu, ret = %d",
1224 capture_payload_size
, unpack_return
);
1228 /* Get root array. */
1229 root_obj
= &unpacked
.data
;
1231 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1232 ERR("Expecting an array as the root object: type = %s",
1233 msgpack_object_type_str(root_obj
->type
));
1237 root_array_obj
= &root_obj
->via
.array
;
1239 /* Create an empty root array event field value. */
1240 ret
= lttng_event_field_value_array_create();
1246 * For each capture descriptor in the condition object:
1248 * 1. Get its corresponding captured field value MessagePack
1251 * 2. Create a corresponding event field value.
1253 * 3. Append it to `ret` (the root array event field value).
1255 count
= lttng_dynamic_pointer_array_get_count(
1256 &condition
->capture_descriptors
);
1259 for (i
= 0; i
< count
; i
++) {
1260 const struct lttng_capture_descriptor
*capture_descriptor
=
1261 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1262 &condition
->parent
, i
);
1263 const msgpack_object
*elem_obj
;
1264 struct lttng_event_field_value
*elem_field_val
;
1267 assert(capture_descriptor
);
1269 elem_obj
= &root_array_obj
->ptr
[i
];
1270 iret
= event_field_value_from_obj(elem_obj
,
1276 if (elem_field_val
) {
1277 iret
= lttng_event_field_value_array_append(ret
,
1280 iret
= lttng_event_field_value_array_append_unavailable(
1285 lttng_event_field_value_destroy(elem_field_val
);
1293 lttng_event_field_value_destroy(ret
);
1297 msgpack_unpacked_destroy(&unpacked
);
1302 struct lttng_evaluation
*lttng_evaluation_event_rule_create(
1303 const struct lttng_condition_event_rule
*condition
,
1304 const char *trigger_name
,
1305 const char *capture_payload
, size_t capture_payload_size
,
1306 bool decode_capture_payload
)
1308 struct lttng_evaluation_event_rule
*hit
;
1309 struct lttng_evaluation
*evaluation
= NULL
;
1311 hit
= zmalloc(sizeof(struct lttng_evaluation_event_rule
));
1316 hit
->name
= strdup(trigger_name
);
1321 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1323 if (capture_payload
) {
1324 const int ret
= lttng_dynamic_buffer_append(
1325 &hit
->capture_payload
, capture_payload
,
1326 capture_payload_size
);
1328 ERR("Failed to initialize capture payload of event rule evaluation");
1332 if (decode_capture_payload
) {
1333 hit
->captured_values
=
1334 event_field_value_from_capture_payload(
1337 capture_payload_size
);
1338 if (!hit
->captured_values
) {
1339 ERR("Failed to decode the capture payload: size = %zu",
1340 capture_payload_size
);
1346 hit
->parent
.type
= LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
;
1347 hit
->parent
.serialize
= lttng_evaluation_event_rule_serialize
;
1348 hit
->parent
.destroy
= lttng_evaluation_event_rule_destroy
;
1350 evaluation
= &hit
->parent
;
1355 lttng_evaluation_event_rule_destroy(&hit
->parent
);
1361 enum lttng_evaluation_status
lttng_evaluation_event_rule_get_captured_values(
1362 const struct lttng_evaluation
*evaluation
,
1363 const struct lttng_event_field_value
**field_val
)
1365 struct lttng_evaluation_event_rule
*hit
;
1366 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1368 if (!evaluation
|| !is_event_rule_evaluation(evaluation
) ||
1370 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1374 hit
= container_of(evaluation
, struct lttng_evaluation_event_rule
,
1376 if (!hit
->captured_values
) {
1377 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1381 *field_val
= hit
->captured_values
;
1387 enum lttng_evaluation_status
lttng_evaluation_event_rule_get_trigger_name(
1388 const struct lttng_evaluation
*evaluation
, const char **name
)
1390 struct lttng_evaluation_event_rule
*hit
;
1391 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1393 if (!evaluation
|| !is_event_rule_evaluation(evaluation
) || !name
) {
1394 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1399 evaluation
, struct lttng_evaluation_event_rule
, parent
);
1406 enum lttng_error_code
1407 lttng_condition_event_rule_generate_capture_descriptor_bytecode(
1408 struct lttng_condition
*condition
)
1410 enum lttng_error_code ret
;
1411 enum lttng_condition_status status
;
1412 unsigned int capture_count
, i
;
1414 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
1415 ret
= LTTNG_ERR_FATAL
;
1419 status
= lttng_condition_event_rule_get_capture_descriptor_count(
1420 condition
, &capture_count
);
1421 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1422 ret
= LTTNG_ERR_FATAL
;
1426 for (i
= 0; i
< capture_count
; i
++) {
1427 struct lttng_capture_descriptor
*local_capture_desc
=
1428 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1431 if (local_capture_desc
== NULL
) {
1432 ret
= LTTNG_ERR_FATAL
;
1436 /* Generate the bytecode. */
1437 status
= lttng_event_expr_to_bytecode(
1438 local_capture_desc
->event_expression
,
1439 &local_capture_desc
->bytecode
);
1440 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1441 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1446 /* Everything went better than expected */
1454 const struct lttng_bytecode
*
1455 lttng_condition_event_rule_get_capture_bytecode_at_index(
1456 const struct lttng_condition
*condition
, unsigned int index
)
1458 const struct lttng_condition_event_rule
*event_rule_cond
=
1459 container_of(condition
,
1460 const struct lttng_condition_event_rule
,
1462 struct lttng_capture_descriptor
*desc
= NULL
;
1463 struct lttng_bytecode
*bytecode
= NULL
;
1465 enum lttng_condition_status status
;
1467 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
1471 status
= lttng_condition_event_rule_get_capture_descriptor_count(
1473 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1477 if (index
>= count
) {
1481 desc
= lttng_dynamic_pointer_array_get_pointer(
1482 &event_rule_cond
->capture_descriptors
, index
);
1487 bytecode
= desc
->bytecode
;