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/condition/on-event-internal.h>
16 #include <lttng/condition/on-event.h>
17 #include <lttng/event-expr-internal.h>
18 #include <lttng/event-expr.h>
19 #include <lttng/event-field-value-internal.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/lttng-error.h>
24 #include <vendor/msgpack/msgpack.h>
26 #define IS_ON_EVENT_CONDITION(condition) \
27 (lttng_condition_get_type(condition) == \
28 LTTNG_CONDITION_TYPE_ON_EVENT)
30 static bool is_on_event_evaluation(const struct lttng_evaluation
*evaluation
)
32 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
34 return type
== LTTNG_CONDITION_TYPE_ON_EVENT
;
37 static bool lttng_condition_on_event_validate(
38 const struct lttng_condition
*condition
);
39 static int lttng_condition_on_event_serialize(
40 const struct lttng_condition
*condition
,
41 struct lttng_payload
*payload
);
42 static bool lttng_condition_on_event_is_equal(
43 const struct lttng_condition
*_a
,
44 const struct lttng_condition
*_b
);
45 static void lttng_condition_on_event_destroy(
46 struct lttng_condition
*condition
);
48 static bool lttng_condition_on_event_validate(
49 const struct lttng_condition
*condition
)
52 struct lttng_condition_on_event
*event_rule
;
58 event_rule
= container_of(
59 condition
, struct lttng_condition_on_event
, 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
);
246 struct lttng_capture_descriptor
*
247 lttng_condition_on_event_get_internal_capture_descriptor_at_index(
248 const struct lttng_condition
*condition
, unsigned int index
)
250 const struct lttng_condition_on_event
*on_event_cond
=
251 container_of(condition
,
252 const struct lttng_condition_on_event
,
254 struct lttng_capture_descriptor
*desc
= NULL
;
256 enum lttng_condition_status status
;
258 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
262 status
= lttng_condition_on_event_get_capture_descriptor_count(
264 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
268 if (index
>= count
) {
272 desc
= lttng_dynamic_pointer_array_get_pointer(
273 &on_event_cond
->capture_descriptors
, index
);
278 static int lttng_condition_on_event_serialize(
279 const struct lttng_condition
*condition
,
280 struct lttng_payload
*payload
)
283 struct lttng_condition_on_event
*on_event_condition
;
284 enum lttng_condition_status status
;
285 /* Used for iteration and communication (size matters). */
286 uint32_t i
, capture_descr_count
;
288 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
293 DBG("Serializing on event condition");
294 on_event_condition
= container_of(
295 condition
, struct lttng_condition_on_event
, parent
);
297 DBG("Serializing on event condition's event rule");
298 ret
= lttng_event_rule_serialize(on_event_condition
->rule
, payload
);
303 status
= lttng_condition_on_event_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_on_event_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_on_event_get_capture_descriptor_count(
347 condition_a
, &capture_descr_count_a
);
348 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
352 status
= lttng_condition_on_event_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_on_event_get_capture_descriptor_at_index(
367 const struct lttng_event_expr
*expr_b
=
368 lttng_condition_on_event_get_capture_descriptor_at_index(
372 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
386 static bool lttng_condition_on_event_is_equal(
387 const struct lttng_condition
*_a
,
388 const struct lttng_condition
*_b
)
390 bool is_equal
= false;
391 struct lttng_condition_on_event
*a
, *b
;
393 a
= container_of(_a
, struct lttng_condition_on_event
, parent
);
394 b
= container_of(_b
, struct lttng_condition_on_event
, parent
);
396 /* Both event rules must be set or both must be unset. */
397 if ((a
->rule
&& !b
->rule
) || (!a
->rule
&& b
->rule
)) {
398 WARN("Comparing event_rule conditions with uninitialized rule");
402 is_equal
= lttng_event_rule_is_equal(a
->rule
, b
->rule
);
407 is_equal
= capture_descriptors_are_equal(_a
, _b
);
413 static void lttng_condition_on_event_destroy(
414 struct lttng_condition
*condition
)
416 struct lttng_condition_on_event
*on_event_condition
;
418 on_event_condition
= container_of(
419 condition
, struct lttng_condition_on_event
, parent
);
421 lttng_event_rule_put(on_event_condition
->rule
);
422 lttng_dynamic_pointer_array_reset(&on_event_condition
->capture_descriptors
);
423 free(on_event_condition
);
427 void destroy_capture_descriptor(void *ptr
)
429 struct lttng_capture_descriptor
*desc
=
430 (struct lttng_capture_descriptor
*) ptr
;
432 lttng_event_expr_destroy(desc
->event_expression
);
433 free(desc
->bytecode
);
437 struct lttng_condition
*lttng_condition_on_event_create(
438 struct lttng_event_rule
*rule
)
440 struct lttng_condition
*parent
= NULL
;
441 struct lttng_condition_on_event
*condition
= NULL
;
447 condition
= zmalloc(sizeof(struct lttng_condition_on_event
));
452 lttng_condition_init(&condition
->parent
,
453 LTTNG_CONDITION_TYPE_ON_EVENT
);
454 condition
->parent
.validate
= lttng_condition_on_event_validate
,
455 condition
->parent
.serialize
= lttng_condition_on_event_serialize
,
456 condition
->parent
.equal
= lttng_condition_on_event_is_equal
,
457 condition
->parent
.destroy
= lttng_condition_on_event_destroy
,
459 lttng_event_rule_get(rule
);
460 condition
->rule
= rule
;
463 lttng_dynamic_pointer_array_init(&condition
->capture_descriptors
,
464 destroy_capture_descriptor
);
466 parent
= &condition
->parent
;
472 uint64_t uint_from_buffer(const struct lttng_buffer_view
*view
, size_t size
,
476 const struct lttng_buffer_view uint_view
=
477 lttng_buffer_view_from_view(view
, *offset
, size
);
479 if (!lttng_buffer_view_is_valid(&uint_view
)) {
486 ret
= (uint64_t) *uint_view
.data
;
488 case sizeof(uint32_t):
492 memcpy(&u32
, uint_view
.data
, sizeof(u32
));
493 ret
= (uint64_t) u32
;
497 memcpy(&ret
, uint_view
.data
, sizeof(ret
));
510 const char *str_from_buffer(const struct lttng_buffer_view
*view
,
516 len
= uint_from_buffer(view
, sizeof(uint32_t), offset
);
517 if (len
== UINT64_C(-1)) {
521 ret
= &view
->data
[*offset
];
523 if (!lttng_buffer_view_contains_string(view
, ret
, len
)) {
538 struct lttng_event_expr
*event_expr_from_payload(
539 struct lttng_payload_view
*view
, size_t *offset
)
541 struct lttng_event_expr
*expr
= NULL
;
545 type
= uint_from_buffer(&view
->buffer
, sizeof(uint8_t), offset
);
546 if (type
== UINT64_C(-1)) {
551 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
552 str
= str_from_buffer(&view
->buffer
, offset
);
557 expr
= lttng_event_expr_event_payload_field_create(str
);
559 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
560 str
= str_from_buffer(&view
->buffer
, offset
);
565 expr
= lttng_event_expr_channel_context_field_create(str
);
567 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
569 const char *provider_name
;
570 const char *type_name
;
572 provider_name
= str_from_buffer(&view
->buffer
, offset
);
573 if (!provider_name
) {
577 type_name
= str_from_buffer(&view
->buffer
, offset
);
582 expr
= lttng_event_expr_app_specific_context_field_create(
583 provider_name
, type_name
);
586 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
588 struct lttng_event_expr
*array_field_expr
;
589 const uint64_t index
= uint_from_buffer(
590 &view
->buffer
, sizeof(uint32_t), offset
);
592 if (index
== UINT64_C(-1)) {
596 /* Array field expression is the encoded after this. */
597 array_field_expr
= event_expr_from_payload(view
, offset
);
598 if (!array_field_expr
) {
602 /* Move ownership of `array_field_expr` to new expression. */
603 expr
= lttng_event_expr_array_field_element_create(
604 array_field_expr
, (unsigned int) index
);
606 /* `array_field_expr` not moved: destroy it. */
607 lttng_event_expr_destroy(array_field_expr
);
613 ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64
,
621 lttng_event_expr_destroy(expr
);
629 ssize_t
lttng_condition_on_event_create_from_payload(
630 struct lttng_payload_view
*view
,
631 struct lttng_condition
**_condition
)
633 ssize_t consumed_length
;
635 ssize_t event_rule_length
;
636 uint32_t i
, capture_descr_count
;
637 struct lttng_condition
*condition
= NULL
;
638 struct lttng_event_rule
*event_rule
= NULL
;
640 if (!view
|| !_condition
) {
644 /* Struct lttng_event_rule. */
646 struct lttng_payload_view event_rule_view
=
647 lttng_payload_view_from_view(view
, offset
, -1);
649 event_rule_length
= lttng_event_rule_create_from_payload(
650 &event_rule_view
, &event_rule
);
653 if (event_rule_length
< 0 || !event_rule
) {
657 offset
+= event_rule_length
;
659 /* Create condition (no capture descriptors yet) at this point */
660 condition
= lttng_condition_on_event_create(event_rule
);
665 /* Capture descriptor count. */
666 assert(event_rule_length
>= 0);
667 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
668 if (capture_descr_count
== UINT32_C(-1)) {
672 /* Capture descriptors. */
673 for (i
= 0; i
< capture_descr_count
; i
++) {
674 enum lttng_condition_status status
;
675 struct lttng_event_expr
*expr
= event_expr_from_payload(
682 /* Move ownership of `expr` to `condition`. */
683 status
= lttng_condition_on_event_append_capture_descriptor(
685 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
686 /* `expr` not moved: destroy it. */
687 lttng_event_expr_destroy(expr
);
692 consumed_length
= (ssize_t
) offset
;
693 *_condition
= condition
;
698 consumed_length
= -1;
701 lttng_event_rule_put(event_rule
);
702 lttng_condition_put(condition
);
703 return consumed_length
;
707 enum lttng_condition_status
lttng_condition_on_event_borrow_rule_mutable(
708 const struct lttng_condition
*condition
,
709 struct lttng_event_rule
**rule
)
711 struct lttng_condition_on_event
*event_rule
;
712 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
714 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !rule
) {
715 status
= LTTNG_CONDITION_STATUS_INVALID
;
719 event_rule
= container_of(
720 condition
, struct lttng_condition_on_event
, parent
);
721 if (!event_rule
->rule
) {
722 status
= LTTNG_CONDITION_STATUS_UNSET
;
726 *rule
= event_rule
->rule
;
731 enum lttng_condition_status
lttng_condition_on_event_get_rule(
732 const struct lttng_condition
*condition
,
733 const struct lttng_event_rule
**rule
)
735 struct lttng_event_rule
*mutable_rule
= NULL
;
736 const enum lttng_condition_status status
=
737 lttng_condition_on_event_borrow_rule_mutable(
738 condition
, &mutable_rule
);
740 *rule
= mutable_rule
;
745 void lttng_condition_on_event_set_error_counter_index(
746 struct lttng_condition
*condition
, uint64_t error_counter_index
)
748 struct lttng_condition_on_event
*on_event_cond
=
749 container_of(condition
,
750 struct lttng_condition_on_event
, parent
);
752 LTTNG_OPTIONAL_SET(&on_event_cond
->error_counter_index
, error_counter_index
);
756 uint64_t lttng_condition_on_event_get_error_counter_index(
757 const struct lttng_condition
*condition
)
759 const struct lttng_condition_on_event
*on_event_cond
=
760 container_of(condition
,
761 const struct lttng_condition_on_event
, parent
);
763 return LTTNG_OPTIONAL_GET(on_event_cond
->error_counter_index
);
766 enum lttng_condition_status
767 lttng_condition_on_event_append_capture_descriptor(
768 struct lttng_condition
*condition
,
769 struct lttng_event_expr
*expr
)
772 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
773 struct lttng_condition_on_event
*on_event_cond
=
774 container_of(condition
,
775 struct lttng_condition_on_event
, parent
);
776 struct lttng_capture_descriptor
*descriptor
= NULL
;
777 const struct lttng_event_rule
*rule
= NULL
;
779 /* Only accept l-values. */
780 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !expr
||
781 !lttng_event_expr_is_lvalue(expr
)) {
782 status
= LTTNG_CONDITION_STATUS_INVALID
;
786 status
= lttng_condition_on_event_get_rule(condition
, &rule
);
787 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
791 switch(lttng_event_rule_get_type(rule
)) {
792 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
793 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
795 status
= LTTNG_CONDITION_STATUS_OK
;
797 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
798 status
= LTTNG_CONDITION_STATUS_INVALID
;
801 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
805 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
809 descriptor
= malloc(sizeof(*descriptor
));
810 if (descriptor
== NULL
) {
811 status
= LTTNG_CONDITION_STATUS_ERROR
;
815 descriptor
->event_expression
= expr
;
816 descriptor
->bytecode
= NULL
;
818 ret
= lttng_dynamic_pointer_array_add_pointer(
819 &on_event_cond
->capture_descriptors
, descriptor
);
821 status
= LTTNG_CONDITION_STATUS_ERROR
;
825 /* Ownership is transfered to the internal capture_descriptors array */
832 enum lttng_condition_status
833 lttng_condition_on_event_get_capture_descriptor_count(
834 const struct lttng_condition
*condition
, unsigned int *count
)
836 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
837 const struct lttng_condition_on_event
*on_event_condition
=
838 container_of(condition
,
839 const struct lttng_condition_on_event
,
842 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !count
) {
843 status
= LTTNG_CONDITION_STATUS_INVALID
;
847 *count
= lttng_dynamic_pointer_array_get_count(
848 &on_event_condition
->capture_descriptors
);
854 const struct lttng_event_expr
*
855 lttng_condition_on_event_get_capture_descriptor_at_index(
856 const struct lttng_condition
*condition
, unsigned int index
)
858 const struct lttng_event_expr
*expr
= NULL
;
859 const struct lttng_capture_descriptor
*desc
= NULL
;
861 desc
= lttng_condition_on_event_get_internal_capture_descriptor_at_index(
866 expr
= desc
->event_expression
;
873 ssize_t
lttng_evaluation_on_event_create_from_payload(
874 const struct lttng_condition_on_event
*condition
,
875 struct lttng_payload_view
*view
,
876 struct lttng_evaluation
**_evaluation
)
878 ssize_t ret
, offset
= 0;
879 struct lttng_evaluation
*evaluation
= NULL
;
880 uint32_t capture_payload_size
;
881 const char *capture_payload
= NULL
;
889 const struct lttng_payload_view current_view
=
890 lttng_payload_view_from_view(view
, offset
, -1);
892 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
897 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
898 sizeof(capture_payload_size
));
900 offset
+= sizeof(capture_payload_size
);
902 if (capture_payload_size
> 0) {
903 const struct lttng_payload_view current_view
=
904 lttng_payload_view_from_view(view
, offset
, -1);
906 if (current_view
.buffer
.size
< capture_payload_size
) {
911 capture_payload
= current_view
.buffer
.data
;
914 evaluation
= lttng_evaluation_on_event_create(condition
,
915 capture_payload
, capture_payload_size
, true);
921 offset
+= capture_payload_size
;
922 *_evaluation
= evaluation
;
927 lttng_evaluation_destroy(evaluation
);
931 static int lttng_evaluation_on_event_serialize(
932 const struct lttng_evaluation
*evaluation
,
933 struct lttng_payload
*payload
)
936 struct lttng_evaluation_on_event
*hit
;
937 uint32_t capture_payload_size
;
940 evaluation
, struct lttng_evaluation_on_event
, parent
);
942 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
943 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
944 sizeof(capture_payload_size
));
949 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
950 hit
->capture_payload
.size
);
960 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
962 bool is_equal
= true;
964 assert(obj
->type
== MSGPACK_OBJECT_STR
);
966 if (obj
->via
.str
.size
!= strlen(str
)) {
971 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
981 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
984 const msgpack_object
*ret
= NULL
;
987 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
989 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
990 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
992 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
994 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1004 static void lttng_evaluation_on_event_destroy(
1005 struct lttng_evaluation
*evaluation
)
1007 struct lttng_evaluation_on_event
*hit
;
1010 evaluation
, struct lttng_evaluation_on_event
, parent
);
1011 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1012 lttng_event_field_value_destroy(hit
->captured_values
);
1017 int event_field_value_from_obj(const msgpack_object
*obj
,
1018 struct lttng_event_field_value
**field_val
)
1025 switch (obj
->type
) {
1026 case MSGPACK_OBJECT_NIL
:
1030 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1031 *field_val
= lttng_event_field_value_uint_create(
1034 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1035 *field_val
= lttng_event_field_value_int_create(
1038 case MSGPACK_OBJECT_FLOAT32
:
1039 case MSGPACK_OBJECT_FLOAT64
:
1040 *field_val
= lttng_event_field_value_real_create(
1043 case MSGPACK_OBJECT_STR
:
1044 *field_val
= lttng_event_field_value_string_create_with_size(
1045 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1047 case MSGPACK_OBJECT_ARRAY
:
1051 *field_val
= lttng_event_field_value_array_create();
1056 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1057 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1058 struct lttng_event_field_value
*elem_field_val
;
1060 ret
= event_field_value_from_obj(elem_obj
,
1066 if (elem_field_val
) {
1067 ret
= lttng_event_field_value_array_append(
1068 *field_val
, elem_field_val
);
1070 ret
= lttng_event_field_value_array_append_unavailable(
1075 lttng_event_field_value_destroy(elem_field_val
);
1082 case MSGPACK_OBJECT_MAP
:
1085 * As of this version, the only valid map object is
1086 * for an enumeration value, for example:
1093 * - Carling Black Label
1095 const msgpack_object
*inner_obj
;
1098 inner_obj
= get_msgpack_map_obj(obj
, "type");
1100 ERR("Missing `type` entry in map object");
1104 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1105 ERR("Map object's `type` entry is not a string: type = %s",
1106 msgpack_object_type_str(inner_obj
->type
));
1110 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1111 ERR("Map object's `type` entry: expecting `enum`");
1115 inner_obj
= get_msgpack_map_obj(obj
, "value");
1117 ERR("Missing `value` entry in map object");
1121 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1122 *field_val
= lttng_event_field_value_enum_uint_create(
1123 inner_obj
->via
.u64
);
1124 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1125 *field_val
= lttng_event_field_value_enum_int_create(
1126 inner_obj
->via
.i64
);
1128 ERR("Map object's `value` entry is not an integer: type = %s",
1129 msgpack_object_type_str(inner_obj
->type
));
1137 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1143 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1144 ERR("Map object's `labels` entry is not an array: type = %s",
1145 msgpack_object_type_str(inner_obj
->type
));
1149 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1152 const msgpack_object
*elem_obj
=
1153 &inner_obj
->via
.array
.ptr
[label_i
];
1155 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1156 ERR("Map object's `labels` entry's type is not a string: type = %s",
1157 msgpack_object_type_str(elem_obj
->type
));
1161 iret
= lttng_event_field_value_enum_append_label_with_size(
1162 *field_val
, elem_obj
->via
.str
.ptr
,
1163 elem_obj
->via
.str
.size
);
1172 ERR("Unexpected object type: type = %s",
1173 msgpack_object_type_str(obj
->type
));
1184 lttng_event_field_value_destroy(*field_val
);
1193 struct lttng_event_field_value
*event_field_value_from_capture_payload(
1194 const struct lttng_condition_on_event
*condition
,
1195 const char *capture_payload
, size_t capture_payload_size
)
1197 struct lttng_event_field_value
*ret
= NULL
;
1198 msgpack_unpacked unpacked
;
1199 msgpack_unpack_return unpack_return
;
1200 const msgpack_object
*root_obj
;
1201 const msgpack_object_array
*root_array_obj
;
1206 assert(capture_payload
);
1208 /* Initialize value. */
1209 msgpack_unpacked_init(&unpacked
);
1212 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1213 capture_payload_size
, NULL
);
1214 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1215 ERR("msgpack_unpack_next() failed to decode the "
1216 "MessagePack-encoded capture payload: "
1217 "size = %zu, ret = %d",
1218 capture_payload_size
, unpack_return
);
1222 /* Get root array. */
1223 root_obj
= &unpacked
.data
;
1225 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1226 ERR("Expecting an array as the root object: type = %s",
1227 msgpack_object_type_str(root_obj
->type
));
1231 root_array_obj
= &root_obj
->via
.array
;
1233 /* Create an empty root array event field value. */
1234 ret
= lttng_event_field_value_array_create();
1240 * For each capture descriptor in the condition object:
1242 * 1. Get its corresponding captured field value MessagePack
1245 * 2. Create a corresponding event field value.
1247 * 3. Append it to `ret` (the root array event field value).
1249 count
= lttng_dynamic_pointer_array_get_count(
1250 &condition
->capture_descriptors
);
1253 for (i
= 0; i
< count
; i
++) {
1254 const struct lttng_capture_descriptor
*capture_descriptor
=
1255 lttng_condition_on_event_get_internal_capture_descriptor_at_index(
1256 &condition
->parent
, i
);
1257 const msgpack_object
*elem_obj
;
1258 struct lttng_event_field_value
*elem_field_val
;
1261 assert(capture_descriptor
);
1263 elem_obj
= &root_array_obj
->ptr
[i
];
1264 iret
= event_field_value_from_obj(elem_obj
,
1270 if (elem_field_val
) {
1271 iret
= lttng_event_field_value_array_append(ret
,
1274 iret
= lttng_event_field_value_array_append_unavailable(
1279 lttng_event_field_value_destroy(elem_field_val
);
1287 lttng_event_field_value_destroy(ret
);
1291 msgpack_unpacked_destroy(&unpacked
);
1296 struct lttng_evaluation
*lttng_evaluation_on_event_create(
1297 const struct lttng_condition_on_event
*condition
,
1298 const char *capture_payload
, size_t capture_payload_size
,
1299 bool decode_capture_payload
)
1301 struct lttng_evaluation_on_event
*hit
;
1302 struct lttng_evaluation
*evaluation
= NULL
;
1304 hit
= zmalloc(sizeof(struct lttng_evaluation_on_event
));
1309 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1311 if (capture_payload
) {
1312 const int ret
= lttng_dynamic_buffer_append(
1313 &hit
->capture_payload
, capture_payload
,
1314 capture_payload_size
);
1316 ERR("Failed to initialize capture payload of event rule evaluation");
1320 if (decode_capture_payload
) {
1321 hit
->captured_values
=
1322 event_field_value_from_capture_payload(
1325 capture_payload_size
);
1326 if (!hit
->captured_values
) {
1327 ERR("Failed to decode the capture payload: size = %zu",
1328 capture_payload_size
);
1334 hit
->parent
.type
= LTTNG_CONDITION_TYPE_ON_EVENT
;
1335 hit
->parent
.serialize
= lttng_evaluation_on_event_serialize
;
1336 hit
->parent
.destroy
= lttng_evaluation_on_event_destroy
;
1338 evaluation
= &hit
->parent
;
1343 lttng_evaluation_on_event_destroy(&hit
->parent
);
1349 enum lttng_evaluation_on_event_status
1350 lttng_evaluation_on_event_get_captured_values(
1351 const struct lttng_evaluation
*evaluation
,
1352 const struct lttng_event_field_value
**field_val
)
1354 struct lttng_evaluation_on_event
*hit
;
1355 enum lttng_evaluation_on_event_status status
=
1356 LTTNG_EVALUATION_ON_EVENT_STATUS_OK
;
1358 if (!evaluation
|| !is_on_event_evaluation(evaluation
) ||
1360 status
= LTTNG_EVALUATION_ON_EVENT_STATUS_INVALID
;
1364 hit
= container_of(evaluation
, struct lttng_evaluation_on_event
,
1366 if (!hit
->captured_values
) {
1367 status
= LTTNG_EVALUATION_ON_EVENT_STATUS_NONE
;
1371 *field_val
= hit
->captured_values
;
1378 enum lttng_error_code
1379 lttng_condition_on_event_generate_capture_descriptor_bytecode(
1380 struct lttng_condition
*condition
)
1382 enum lttng_error_code ret
;
1383 enum lttng_condition_status status
;
1384 unsigned int capture_count
, i
;
1386 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
1387 ret
= LTTNG_ERR_FATAL
;
1391 status
= lttng_condition_on_event_get_capture_descriptor_count(
1392 condition
, &capture_count
);
1393 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1394 ret
= LTTNG_ERR_FATAL
;
1398 for (i
= 0; i
< capture_count
; i
++) {
1399 struct lttng_capture_descriptor
*local_capture_desc
=
1400 lttng_condition_on_event_get_internal_capture_descriptor_at_index(
1403 if (local_capture_desc
== NULL
) {
1404 ret
= LTTNG_ERR_FATAL
;
1408 /* Generate the bytecode. */
1409 status
= lttng_event_expr_to_bytecode(
1410 local_capture_desc
->event_expression
,
1411 &local_capture_desc
->bytecode
);
1412 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1413 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1418 /* Everything went better than expected */
1426 const struct lttng_bytecode
*
1427 lttng_condition_on_event_get_capture_bytecode_at_index(
1428 const struct lttng_condition
*condition
, unsigned int index
)
1430 const struct lttng_condition_on_event
*on_event_cond
=
1431 container_of(condition
,
1432 const struct lttng_condition_on_event
,
1434 struct lttng_capture_descriptor
*desc
= NULL
;
1435 struct lttng_bytecode
*bytecode
= NULL
;
1437 enum lttng_condition_status status
;
1439 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
1443 status
= lttng_condition_on_event_get_capture_descriptor_count(
1445 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1449 if (index
>= count
) {
1453 desc
= lttng_dynamic_pointer_array_get_pointer(
1454 &on_event_cond
->capture_descriptors
, index
);
1459 bytecode
= desc
->bytecode
;