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
;
754 const struct lttng_event_rule
*rule
= NULL
;
756 /* Only accept l-values. */
757 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
) || !expr
||
758 !lttng_event_expr_is_lvalue(expr
)) {
759 status
= LTTNG_CONDITION_STATUS_INVALID
;
763 status
= lttng_condition_event_rule_get_rule(condition
, &rule
);
764 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
768 switch(lttng_event_rule_get_type(rule
)) {
769 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
770 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
772 status
= LTTNG_CONDITION_STATUS_OK
;
774 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
775 status
= LTTNG_CONDITION_STATUS_INVALID
;
778 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
782 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
786 descriptor
= malloc(sizeof(*descriptor
));
787 if (descriptor
== NULL
) {
788 status
= LTTNG_CONDITION_STATUS_ERROR
;
792 descriptor
->event_expression
= expr
;
793 descriptor
->bytecode
= NULL
;
795 ret
= lttng_dynamic_pointer_array_add_pointer(
796 &event_rule_cond
->capture_descriptors
, descriptor
);
798 status
= LTTNG_CONDITION_STATUS_ERROR
;
802 /* Ownership is transfered to the internal capture_descriptors array */
809 enum lttng_condition_status
810 lttng_condition_event_rule_get_capture_descriptor_count(
811 const struct lttng_condition
*condition
, unsigned int *count
)
813 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
814 const struct lttng_condition_event_rule
*event_rule_cond
=
815 container_of(condition
,
816 const struct lttng_condition_event_rule
,
819 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
) || !count
) {
820 status
= LTTNG_CONDITION_STATUS_INVALID
;
824 *count
= lttng_dynamic_pointer_array_get_count(
825 &event_rule_cond
->capture_descriptors
);
831 const struct lttng_event_expr
*
832 lttng_condition_event_rule_get_capture_descriptor_at_index(
833 const struct lttng_condition
*condition
, unsigned int index
)
835 const struct lttng_event_expr
*expr
= NULL
;
836 const struct lttng_capture_descriptor
*desc
= NULL
;
838 desc
= lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
843 expr
= desc
->event_expression
;
850 ssize_t
lttng_evaluation_event_rule_create_from_payload(
851 const struct lttng_condition_event_rule
*condition
,
852 struct lttng_payload_view
*view
,
853 struct lttng_evaluation
**_evaluation
)
855 ssize_t ret
, offset
= 0;
856 const char *trigger_name
;
857 struct lttng_evaluation
*evaluation
= NULL
;
858 const struct lttng_evaluation_event_rule_comm
*header
;
859 const struct lttng_payload_view header_view
=
860 lttng_payload_view_from_view(
861 view
, 0, sizeof(*header
));
862 uint32_t capture_payload_size
;
863 const char *capture_payload
= NULL
;
870 if (!lttng_payload_view_is_valid(&header_view
)) {
871 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header");
876 header
= (typeof(header
)) header_view
.buffer
.data
;
878 /* Map the originating trigger's name. */
879 offset
+= sizeof(*header
);
881 const struct lttng_payload_view current_view
=
882 lttng_payload_view_from_view(view
, offset
,
883 header
->trigger_name_length
);
885 if (!lttng_payload_view_is_valid(¤t_view
)) {
886 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
891 trigger_name
= current_view
.buffer
.data
;
892 if (!lttng_buffer_view_contains_string(¤t_view
.buffer
,
893 trigger_name
, header
->trigger_name_length
)) {
894 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
900 offset
+= header
->trigger_name_length
;
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_create(condition
, trigger_name
,
928 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_serialize(
945 const struct lttng_evaluation
*evaluation
,
946 struct lttng_payload
*payload
)
949 struct lttng_evaluation_event_rule
*hit
;
950 struct lttng_evaluation_event_rule_comm comm
;
951 uint32_t capture_payload_size
;
954 evaluation
, struct lttng_evaluation_event_rule
, parent
);
957 comm
.trigger_name_length
= strlen(hit
->name
) + 1;
959 ret
= lttng_dynamic_buffer_append(
960 &payload
->buffer
, &comm
, sizeof(comm
));
965 ret
= lttng_dynamic_buffer_append(
966 &payload
->buffer
, hit
->name
, comm
.trigger_name_length
);
971 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
972 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
973 sizeof(capture_payload_size
));
978 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
979 hit
->capture_payload
.size
);
989 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
991 bool is_equal
= true;
993 assert(obj
->type
== MSGPACK_OBJECT_STR
);
995 if (obj
->via
.str
.size
!= strlen(str
)) {
1000 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
1010 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
1013 const msgpack_object
*ret
= NULL
;
1016 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
1018 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
1019 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
1021 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
1023 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1033 static void lttng_evaluation_event_rule_destroy(
1034 struct lttng_evaluation
*evaluation
)
1036 struct lttng_evaluation_event_rule
*hit
;
1039 evaluation
, struct lttng_evaluation_event_rule
, parent
);
1041 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1042 lttng_event_field_value_destroy(hit
->captured_values
);
1047 int event_field_value_from_obj(const msgpack_object
*obj
,
1048 struct lttng_event_field_value
**field_val
)
1055 switch (obj
->type
) {
1056 case MSGPACK_OBJECT_NIL
:
1060 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1061 *field_val
= lttng_event_field_value_uint_create(
1064 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1065 *field_val
= lttng_event_field_value_int_create(
1068 case MSGPACK_OBJECT_FLOAT32
:
1069 case MSGPACK_OBJECT_FLOAT64
:
1070 *field_val
= lttng_event_field_value_real_create(
1073 case MSGPACK_OBJECT_STR
:
1074 *field_val
= lttng_event_field_value_string_create_with_size(
1075 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1077 case MSGPACK_OBJECT_ARRAY
:
1081 *field_val
= lttng_event_field_value_array_create();
1086 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1087 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1088 struct lttng_event_field_value
*elem_field_val
;
1090 ret
= event_field_value_from_obj(elem_obj
,
1096 if (elem_field_val
) {
1097 ret
= lttng_event_field_value_array_append(
1098 *field_val
, elem_field_val
);
1100 ret
= lttng_event_field_value_array_append_unavailable(
1105 lttng_event_field_value_destroy(elem_field_val
);
1112 case MSGPACK_OBJECT_MAP
:
1115 * As of this version, the only valid map object is
1116 * for an enumeration value, for example:
1123 * - Carling Black Label
1125 const msgpack_object
*inner_obj
;
1128 inner_obj
= get_msgpack_map_obj(obj
, "type");
1130 ERR("Missing `type` entry in map object");
1134 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1135 ERR("Map object's `type` entry is not a string: type = %s",
1136 msgpack_object_type_str(inner_obj
->type
));
1140 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1141 ERR("Map object's `type` entry: expecting `enum`");
1145 inner_obj
= get_msgpack_map_obj(obj
, "value");
1147 ERR("Missing `value` entry in map object");
1151 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1152 *field_val
= lttng_event_field_value_enum_uint_create(
1153 inner_obj
->via
.u64
);
1154 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1155 *field_val
= lttng_event_field_value_enum_int_create(
1156 inner_obj
->via
.i64
);
1158 ERR("Map object's `value` entry is not an integer: type = %s",
1159 msgpack_object_type_str(inner_obj
->type
));
1167 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1173 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1174 ERR("Map object's `labels` entry is not an array: type = %s",
1175 msgpack_object_type_str(inner_obj
->type
));
1179 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1182 const msgpack_object
*elem_obj
=
1183 &inner_obj
->via
.array
.ptr
[label_i
];
1185 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1186 ERR("Map object's `labels` entry's type is not a string: type = %s",
1187 msgpack_object_type_str(elem_obj
->type
));
1191 iret
= lttng_event_field_value_enum_append_label_with_size(
1192 *field_val
, elem_obj
->via
.str
.ptr
,
1193 elem_obj
->via
.str
.size
);
1202 ERR("Unexpected object type: type = %s",
1203 msgpack_object_type_str(obj
->type
));
1214 lttng_event_field_value_destroy(*field_val
);
1223 struct lttng_event_field_value
*event_field_value_from_capture_payload(
1224 const struct lttng_condition_event_rule
*condition
,
1225 const char *capture_payload
, size_t capture_payload_size
)
1227 struct lttng_event_field_value
*ret
= NULL
;
1228 msgpack_unpacked unpacked
;
1229 msgpack_unpack_return unpack_return
;
1230 const msgpack_object
*root_obj
;
1231 const msgpack_object_array
*root_array_obj
;
1236 assert(capture_payload
);
1238 /* Initialize value. */
1239 msgpack_unpacked_init(&unpacked
);
1242 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1243 capture_payload_size
, NULL
);
1244 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1245 ERR("msgpack_unpack_next() failed to decode the "
1246 "MessagePack-encoded capture payload: "
1247 "size = %zu, ret = %d",
1248 capture_payload_size
, unpack_return
);
1252 /* Get root array. */
1253 root_obj
= &unpacked
.data
;
1255 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1256 ERR("Expecting an array as the root object: type = %s",
1257 msgpack_object_type_str(root_obj
->type
));
1261 root_array_obj
= &root_obj
->via
.array
;
1263 /* Create an empty root array event field value. */
1264 ret
= lttng_event_field_value_array_create();
1270 * For each capture descriptor in the condition object:
1272 * 1. Get its corresponding captured field value MessagePack
1275 * 2. Create a corresponding event field value.
1277 * 3. Append it to `ret` (the root array event field value).
1279 count
= lttng_dynamic_pointer_array_get_count(
1280 &condition
->capture_descriptors
);
1283 for (i
= 0; i
< count
; i
++) {
1284 const struct lttng_capture_descriptor
*capture_descriptor
=
1285 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1286 &condition
->parent
, i
);
1287 const msgpack_object
*elem_obj
;
1288 struct lttng_event_field_value
*elem_field_val
;
1291 assert(capture_descriptor
);
1293 elem_obj
= &root_array_obj
->ptr
[i
];
1294 iret
= event_field_value_from_obj(elem_obj
,
1300 if (elem_field_val
) {
1301 iret
= lttng_event_field_value_array_append(ret
,
1304 iret
= lttng_event_field_value_array_append_unavailable(
1309 lttng_event_field_value_destroy(elem_field_val
);
1317 lttng_event_field_value_destroy(ret
);
1321 msgpack_unpacked_destroy(&unpacked
);
1326 struct lttng_evaluation
*lttng_evaluation_event_rule_create(
1327 const struct lttng_condition_event_rule
*condition
,
1328 const char *trigger_name
,
1329 const char *capture_payload
, size_t capture_payload_size
,
1330 bool decode_capture_payload
)
1332 struct lttng_evaluation_event_rule
*hit
;
1333 struct lttng_evaluation
*evaluation
= NULL
;
1335 hit
= zmalloc(sizeof(struct lttng_evaluation_event_rule
));
1340 hit
->name
= strdup(trigger_name
);
1345 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1347 if (capture_payload
) {
1348 const int ret
= lttng_dynamic_buffer_append(
1349 &hit
->capture_payload
, capture_payload
,
1350 capture_payload_size
);
1352 ERR("Failed to initialize capture payload of event rule evaluation");
1356 if (decode_capture_payload
) {
1357 hit
->captured_values
=
1358 event_field_value_from_capture_payload(
1361 capture_payload_size
);
1362 if (!hit
->captured_values
) {
1363 ERR("Failed to decode the capture payload: size = %zu",
1364 capture_payload_size
);
1370 hit
->parent
.type
= LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
;
1371 hit
->parent
.serialize
= lttng_evaluation_event_rule_serialize
;
1372 hit
->parent
.destroy
= lttng_evaluation_event_rule_destroy
;
1374 evaluation
= &hit
->parent
;
1379 lttng_evaluation_event_rule_destroy(&hit
->parent
);
1385 enum lttng_evaluation_status
lttng_evaluation_event_rule_get_captured_values(
1386 const struct lttng_evaluation
*evaluation
,
1387 const struct lttng_event_field_value
**field_val
)
1389 struct lttng_evaluation_event_rule
*hit
;
1390 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1392 if (!evaluation
|| !is_event_rule_evaluation(evaluation
) ||
1394 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1398 hit
= container_of(evaluation
, struct lttng_evaluation_event_rule
,
1400 if (!hit
->captured_values
) {
1401 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1405 *field_val
= hit
->captured_values
;
1411 enum lttng_evaluation_status
lttng_evaluation_event_rule_get_trigger_name(
1412 const struct lttng_evaluation
*evaluation
, const char **name
)
1414 struct lttng_evaluation_event_rule
*hit
;
1415 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1417 if (!evaluation
|| !is_event_rule_evaluation(evaluation
) || !name
) {
1418 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1423 evaluation
, struct lttng_evaluation_event_rule
, parent
);
1430 enum lttng_error_code
1431 lttng_condition_event_rule_generate_capture_descriptor_bytecode(
1432 struct lttng_condition
*condition
)
1434 enum lttng_error_code ret
;
1435 enum lttng_condition_status status
;
1436 unsigned int capture_count
, i
;
1438 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
1439 ret
= LTTNG_ERR_FATAL
;
1443 status
= lttng_condition_event_rule_get_capture_descriptor_count(
1444 condition
, &capture_count
);
1445 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1446 ret
= LTTNG_ERR_FATAL
;
1450 for (i
= 0; i
< capture_count
; i
++) {
1451 struct lttng_capture_descriptor
*local_capture_desc
=
1452 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1455 if (local_capture_desc
== NULL
) {
1456 ret
= LTTNG_ERR_FATAL
;
1460 /* Generate the bytecode. */
1461 status
= lttng_event_expr_to_bytecode(
1462 local_capture_desc
->event_expression
,
1463 &local_capture_desc
->bytecode
);
1464 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1465 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1470 /* Everything went better than expected */
1478 const struct lttng_bytecode
*
1479 lttng_condition_event_rule_get_capture_bytecode_at_index(
1480 const struct lttng_condition
*condition
, unsigned int index
)
1482 const struct lttng_condition_event_rule
*event_rule_cond
=
1483 container_of(condition
,
1484 const struct lttng_condition_event_rule
,
1486 struct lttng_capture_descriptor
*desc
= NULL
;
1487 struct lttng_bytecode
*bytecode
= NULL
;
1489 enum lttng_condition_status status
;
1491 if (!condition
|| !IS_EVENT_RULE_CONDITION(condition
)) {
1495 status
= lttng_condition_event_rule_get_capture_descriptor_count(
1497 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1501 if (index
>= count
) {
1505 desc
= lttng_dynamic_pointer_array_get_pointer(
1506 &event_rule_cond
->capture_descriptors
, index
);
1511 bytecode
= desc
->bytecode
;