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_ON_EVENT_CONDITION(condition) \
28 (lttng_condition_get_type(condition) == \
29 LTTNG_CONDITION_TYPE_ON_EVENT)
31 static bool is_on_event_evaluation(const struct lttng_evaluation
*evaluation
)
33 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
35 return type
== LTTNG_CONDITION_TYPE_ON_EVENT
;
38 static bool lttng_condition_on_event_validate(
39 const struct lttng_condition
*condition
);
40 static int lttng_condition_on_event_serialize(
41 const struct lttng_condition
*condition
,
42 struct lttng_payload
*payload
);
43 static bool lttng_condition_on_event_is_equal(
44 const struct lttng_condition
*_a
,
45 const struct lttng_condition
*_b
);
46 static void lttng_condition_on_event_destroy(
47 struct lttng_condition
*condition
);
49 static bool lttng_condition_on_event_validate(
50 const struct lttng_condition
*condition
)
53 struct lttng_condition_on_event
*event_rule
;
59 event_rule
= container_of(
60 condition
, struct lttng_condition_on_event
, parent
);
61 if (!event_rule
->rule
) {
62 ERR("Invalid on event 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_on_event_get_internal_capture_descriptor_at_index(
249 const struct lttng_condition
*condition
, unsigned int index
)
251 const struct lttng_condition_on_event
*on_event_cond
=
252 container_of(condition
,
253 const struct lttng_condition_on_event
,
255 struct lttng_capture_descriptor
*desc
= NULL
;
257 enum lttng_condition_status status
;
259 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
263 status
= lttng_condition_on_event_get_capture_descriptor_count(
265 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
269 if (index
>= count
) {
273 desc
= lttng_dynamic_pointer_array_get_pointer(
274 &on_event_cond
->capture_descriptors
, index
);
279 static int lttng_condition_on_event_serialize(
280 const struct lttng_condition
*condition
,
281 struct lttng_payload
*payload
)
284 struct lttng_condition_on_event
*on_event_condition
;
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_ON_EVENT_CONDITION(condition
)) {
294 DBG("Serializing on event condition");
295 on_event_condition
= container_of(
296 condition
, struct lttng_condition_on_event
, parent
);
298 DBG("Serializing on event condition's event rule");
299 ret
= lttng_event_rule_serialize(on_event_condition
->rule
, payload
);
304 status
= lttng_condition_on_event_get_capture_descriptor_count(
305 condition
, &capture_descr_count
);
306 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
311 DBG("Serializing on event 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_on_event_get_internal_capture_descriptor_at_index(
324 DBG("Serializing on event 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_on_event_get_capture_descriptor_count(
348 condition_a
, &capture_descr_count_a
);
349 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
353 status
= lttng_condition_on_event_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_on_event_get_capture_descriptor_at_index(
368 const struct lttng_event_expr
*expr_b
=
369 lttng_condition_on_event_get_capture_descriptor_at_index(
373 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
387 static bool lttng_condition_on_event_is_equal(
388 const struct lttng_condition
*_a
,
389 const struct lttng_condition
*_b
)
391 bool is_equal
= false;
392 struct lttng_condition_on_event
*a
, *b
;
394 a
= container_of(_a
, struct lttng_condition_on_event
, parent
);
395 b
= container_of(_b
, struct lttng_condition_on_event
, 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_on_event_destroy(
415 struct lttng_condition
*condition
)
417 struct lttng_condition_on_event
*on_event_condition
;
419 on_event_condition
= container_of(
420 condition
, struct lttng_condition_on_event
, parent
);
422 lttng_event_rule_put(on_event_condition
->rule
);
423 lttng_dynamic_pointer_array_reset(&on_event_condition
->capture_descriptors
);
424 free(on_event_condition
);
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_on_event_create(
439 struct lttng_event_rule
*rule
)
441 struct lttng_condition
*parent
= NULL
;
442 struct lttng_condition_on_event
*condition
= NULL
;
448 condition
= zmalloc(sizeof(struct lttng_condition_on_event
));
453 lttng_condition_init(&condition
->parent
,
454 LTTNG_CONDITION_TYPE_ON_EVENT
);
455 condition
->parent
.validate
= lttng_condition_on_event_validate
,
456 condition
->parent
.serialize
= lttng_condition_on_event_serialize
,
457 condition
->parent
.equal
= lttng_condition_on_event_is_equal
,
458 condition
->parent
.destroy
= lttng_condition_on_event_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_on_event_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_on_event_create(event_rule
);
662 /* Capture descriptor count. */
663 assert(event_rule_length
>= 0);
664 offset
+= (size_t) event_rule_length
;
665 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
666 if (capture_descr_count
== UINT32_C(-1)) {
670 /* Capture descriptors. */
671 for (i
= 0; i
< capture_descr_count
; i
++) {
672 enum lttng_condition_status status
;
673 struct lttng_event_expr
*expr
= event_expr_from_payload(
680 /* Move ownership of `expr` to `condition`. */
681 status
= lttng_condition_on_event_append_capture_descriptor(
683 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
684 /* `expr` not moved: destroy it. */
685 lttng_event_expr_destroy(expr
);
690 consumed_length
= (ssize_t
) offset
;
691 *_condition
= condition
;
696 consumed_length
= -1;
699 lttng_event_rule_put(event_rule
);
700 lttng_condition_put(condition
);
701 return consumed_length
;
705 enum lttng_condition_status
lttng_condition_on_event_borrow_rule_mutable(
706 const struct lttng_condition
*condition
,
707 struct lttng_event_rule
**rule
)
709 struct lttng_condition_on_event
*event_rule
;
710 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
712 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !rule
) {
713 status
= LTTNG_CONDITION_STATUS_INVALID
;
717 event_rule
= container_of(
718 condition
, struct lttng_condition_on_event
, parent
);
719 if (!event_rule
->rule
) {
720 status
= LTTNG_CONDITION_STATUS_UNSET
;
724 *rule
= event_rule
->rule
;
729 enum lttng_condition_status
lttng_condition_on_event_get_rule(
730 const struct lttng_condition
*condition
,
731 const struct lttng_event_rule
**rule
)
733 struct lttng_event_rule
*mutable_rule
= NULL
;
734 const enum lttng_condition_status status
=
735 lttng_condition_on_event_borrow_rule_mutable(
736 condition
, &mutable_rule
);
738 *rule
= mutable_rule
;
742 enum lttng_condition_status
743 lttng_condition_on_event_append_capture_descriptor(
744 struct lttng_condition
*condition
,
745 struct lttng_event_expr
*expr
)
748 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
749 struct lttng_condition_on_event
*event_rule_cond
=
750 container_of(condition
,
751 struct lttng_condition_on_event
, parent
);
752 struct lttng_capture_descriptor
*descriptor
= NULL
;
753 const struct lttng_event_rule
*rule
= NULL
;
755 /* Only accept l-values. */
756 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !expr
||
757 !lttng_event_expr_is_lvalue(expr
)) {
758 status
= LTTNG_CONDITION_STATUS_INVALID
;
762 status
= lttng_condition_on_event_get_rule(condition
, &rule
);
763 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
767 switch(lttng_event_rule_get_type(rule
)) {
768 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
769 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
771 status
= LTTNG_CONDITION_STATUS_OK
;
773 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
774 status
= LTTNG_CONDITION_STATUS_INVALID
;
777 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
781 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
785 descriptor
= malloc(sizeof(*descriptor
));
786 if (descriptor
== NULL
) {
787 status
= LTTNG_CONDITION_STATUS_ERROR
;
791 descriptor
->event_expression
= expr
;
792 descriptor
->bytecode
= NULL
;
794 ret
= lttng_dynamic_pointer_array_add_pointer(
795 &event_rule_cond
->capture_descriptors
, descriptor
);
797 status
= LTTNG_CONDITION_STATUS_ERROR
;
801 /* Ownership is transfered to the internal capture_descriptors array */
808 enum lttng_condition_status
809 lttng_condition_on_event_get_capture_descriptor_count(
810 const struct lttng_condition
*condition
, unsigned int *count
)
812 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
813 const struct lttng_condition_on_event
*on_event_condition
=
814 container_of(condition
,
815 const struct lttng_condition_on_event
,
818 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
) || !count
) {
819 status
= LTTNG_CONDITION_STATUS_INVALID
;
823 *count
= lttng_dynamic_pointer_array_get_count(
824 &on_event_condition
->capture_descriptors
);
830 const struct lttng_event_expr
*
831 lttng_condition_on_event_get_capture_descriptor_at_index(
832 const struct lttng_condition
*condition
, unsigned int index
)
834 const struct lttng_event_expr
*expr
= NULL
;
835 const struct lttng_capture_descriptor
*desc
= NULL
;
837 desc
= lttng_condition_on_event_get_internal_capture_descriptor_at_index(
842 expr
= desc
->event_expression
;
849 ssize_t
lttng_evaluation_on_event_create_from_payload(
850 const struct lttng_condition_on_event
*condition
,
851 struct lttng_payload_view
*view
,
852 struct lttng_evaluation
**_evaluation
)
854 ssize_t ret
, offset
= 0;
855 const char *trigger_name
;
856 struct lttng_evaluation
*evaluation
= NULL
;
857 const struct lttng_evaluation_on_event_comm
*header
;
858 const struct lttng_payload_view header_view
=
859 lttng_payload_view_from_view(
860 view
, 0, sizeof(*header
));
861 uint32_t capture_payload_size
;
862 const char *capture_payload
= NULL
;
869 if (!lttng_payload_view_is_valid(&header_view
)) {
870 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header");
875 header
= (typeof(header
)) header_view
.buffer
.data
;
877 /* Map the originating trigger's name. */
878 offset
+= sizeof(*header
);
880 const struct lttng_payload_view current_view
=
881 lttng_payload_view_from_view(view
, offset
,
882 header
->trigger_name_length
);
884 if (!lttng_payload_view_is_valid(¤t_view
)) {
885 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
890 trigger_name
= current_view
.buffer
.data
;
891 if (!lttng_buffer_view_contains_string(¤t_view
.buffer
,
892 trigger_name
, header
->trigger_name_length
)) {
893 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
899 offset
+= header
->trigger_name_length
;
901 const struct lttng_payload_view current_view
=
902 lttng_payload_view_from_view(view
, offset
, -1);
904 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
909 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
910 sizeof(capture_payload_size
));
912 offset
+= sizeof(capture_payload_size
);
914 if (capture_payload_size
> 0) {
915 const struct lttng_payload_view current_view
=
916 lttng_payload_view_from_view(view
, offset
, -1);
918 if (current_view
.buffer
.size
< capture_payload_size
) {
923 capture_payload
= current_view
.buffer
.data
;
926 evaluation
= lttng_evaluation_on_event_create(condition
, trigger_name
,
927 capture_payload
, capture_payload_size
, true);
933 offset
+= capture_payload_size
;
934 *_evaluation
= evaluation
;
939 lttng_evaluation_destroy(evaluation
);
943 static int lttng_evaluation_on_event_serialize(
944 const struct lttng_evaluation
*evaluation
,
945 struct lttng_payload
*payload
)
948 struct lttng_evaluation_on_event
*hit
;
949 struct lttng_evaluation_on_event_comm comm
;
950 uint32_t capture_payload_size
;
953 evaluation
, struct lttng_evaluation_on_event
, parent
);
956 comm
.trigger_name_length
= strlen(hit
->name
) + 1;
958 ret
= lttng_dynamic_buffer_append(
959 &payload
->buffer
, &comm
, sizeof(comm
));
964 ret
= lttng_dynamic_buffer_append(
965 &payload
->buffer
, hit
->name
, comm
.trigger_name_length
);
970 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
971 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
972 sizeof(capture_payload_size
));
977 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
978 hit
->capture_payload
.size
);
988 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
990 bool is_equal
= true;
992 assert(obj
->type
== MSGPACK_OBJECT_STR
);
994 if (obj
->via
.str
.size
!= strlen(str
)) {
999 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
1009 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
1012 const msgpack_object
*ret
= NULL
;
1015 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
1017 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
1018 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
1020 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
1022 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1032 static void lttng_evaluation_on_event_destroy(
1033 struct lttng_evaluation
*evaluation
)
1035 struct lttng_evaluation_on_event
*hit
;
1038 evaluation
, struct lttng_evaluation_on_event
, parent
);
1040 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1041 lttng_event_field_value_destroy(hit
->captured_values
);
1046 int event_field_value_from_obj(const msgpack_object
*obj
,
1047 struct lttng_event_field_value
**field_val
)
1054 switch (obj
->type
) {
1055 case MSGPACK_OBJECT_NIL
:
1059 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1060 *field_val
= lttng_event_field_value_uint_create(
1063 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1064 *field_val
= lttng_event_field_value_int_create(
1067 case MSGPACK_OBJECT_FLOAT32
:
1068 case MSGPACK_OBJECT_FLOAT64
:
1069 *field_val
= lttng_event_field_value_real_create(
1072 case MSGPACK_OBJECT_STR
:
1073 *field_val
= lttng_event_field_value_string_create_with_size(
1074 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1076 case MSGPACK_OBJECT_ARRAY
:
1080 *field_val
= lttng_event_field_value_array_create();
1085 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1086 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1087 struct lttng_event_field_value
*elem_field_val
;
1089 ret
= event_field_value_from_obj(elem_obj
,
1095 if (elem_field_val
) {
1096 ret
= lttng_event_field_value_array_append(
1097 *field_val
, elem_field_val
);
1099 ret
= lttng_event_field_value_array_append_unavailable(
1104 lttng_event_field_value_destroy(elem_field_val
);
1111 case MSGPACK_OBJECT_MAP
:
1114 * As of this version, the only valid map object is
1115 * for an enumeration value, for example:
1122 * - Carling Black Label
1124 const msgpack_object
*inner_obj
;
1127 inner_obj
= get_msgpack_map_obj(obj
, "type");
1129 ERR("Missing `type` entry in map object");
1133 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1134 ERR("Map object's `type` entry is not a string: type = %s",
1135 msgpack_object_type_str(inner_obj
->type
));
1139 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1140 ERR("Map object's `type` entry: expecting `enum`");
1144 inner_obj
= get_msgpack_map_obj(obj
, "value");
1146 ERR("Missing `value` entry in map object");
1150 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1151 *field_val
= lttng_event_field_value_enum_uint_create(
1152 inner_obj
->via
.u64
);
1153 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1154 *field_val
= lttng_event_field_value_enum_int_create(
1155 inner_obj
->via
.i64
);
1157 ERR("Map object's `value` entry is not an integer: type = %s",
1158 msgpack_object_type_str(inner_obj
->type
));
1166 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1172 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1173 ERR("Map object's `labels` entry is not an array: type = %s",
1174 msgpack_object_type_str(inner_obj
->type
));
1178 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1181 const msgpack_object
*elem_obj
=
1182 &inner_obj
->via
.array
.ptr
[label_i
];
1184 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1185 ERR("Map object's `labels` entry's type is not a string: type = %s",
1186 msgpack_object_type_str(elem_obj
->type
));
1190 iret
= lttng_event_field_value_enum_append_label_with_size(
1191 *field_val
, elem_obj
->via
.str
.ptr
,
1192 elem_obj
->via
.str
.size
);
1201 ERR("Unexpected object type: type = %s",
1202 msgpack_object_type_str(obj
->type
));
1213 lttng_event_field_value_destroy(*field_val
);
1222 struct lttng_event_field_value
*event_field_value_from_capture_payload(
1223 const struct lttng_condition_on_event
*condition
,
1224 const char *capture_payload
, size_t capture_payload_size
)
1226 struct lttng_event_field_value
*ret
= NULL
;
1227 msgpack_unpacked unpacked
;
1228 msgpack_unpack_return unpack_return
;
1229 const msgpack_object
*root_obj
;
1230 const msgpack_object_array
*root_array_obj
;
1235 assert(capture_payload
);
1237 /* Initialize value. */
1238 msgpack_unpacked_init(&unpacked
);
1241 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1242 capture_payload_size
, NULL
);
1243 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1244 ERR("msgpack_unpack_next() failed to decode the "
1245 "MessagePack-encoded capture payload: "
1246 "size = %zu, ret = %d",
1247 capture_payload_size
, unpack_return
);
1251 /* Get root array. */
1252 root_obj
= &unpacked
.data
;
1254 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1255 ERR("Expecting an array as the root object: type = %s",
1256 msgpack_object_type_str(root_obj
->type
));
1260 root_array_obj
= &root_obj
->via
.array
;
1262 /* Create an empty root array event field value. */
1263 ret
= lttng_event_field_value_array_create();
1269 * For each capture descriptor in the condition object:
1271 * 1. Get its corresponding captured field value MessagePack
1274 * 2. Create a corresponding event field value.
1276 * 3. Append it to `ret` (the root array event field value).
1278 count
= lttng_dynamic_pointer_array_get_count(
1279 &condition
->capture_descriptors
);
1282 for (i
= 0; i
< count
; i
++) {
1283 const struct lttng_capture_descriptor
*capture_descriptor
=
1284 lttng_condition_on_event_get_internal_capture_descriptor_at_index(
1285 &condition
->parent
, i
);
1286 const msgpack_object
*elem_obj
;
1287 struct lttng_event_field_value
*elem_field_val
;
1290 assert(capture_descriptor
);
1292 elem_obj
= &root_array_obj
->ptr
[i
];
1293 iret
= event_field_value_from_obj(elem_obj
,
1299 if (elem_field_val
) {
1300 iret
= lttng_event_field_value_array_append(ret
,
1303 iret
= lttng_event_field_value_array_append_unavailable(
1308 lttng_event_field_value_destroy(elem_field_val
);
1316 lttng_event_field_value_destroy(ret
);
1320 msgpack_unpacked_destroy(&unpacked
);
1325 struct lttng_evaluation
*lttng_evaluation_on_event_create(
1326 const struct lttng_condition_on_event
*condition
,
1327 const char *trigger_name
,
1328 const char *capture_payload
, size_t capture_payload_size
,
1329 bool decode_capture_payload
)
1331 struct lttng_evaluation_on_event
*hit
;
1332 struct lttng_evaluation
*evaluation
= NULL
;
1334 hit
= zmalloc(sizeof(struct lttng_evaluation_on_event
));
1339 hit
->name
= strdup(trigger_name
);
1344 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1346 if (capture_payload
) {
1347 const int ret
= lttng_dynamic_buffer_append(
1348 &hit
->capture_payload
, capture_payload
,
1349 capture_payload_size
);
1351 ERR("Failed to initialize capture payload of event rule evaluation");
1355 if (decode_capture_payload
) {
1356 hit
->captured_values
=
1357 event_field_value_from_capture_payload(
1360 capture_payload_size
);
1361 if (!hit
->captured_values
) {
1362 ERR("Failed to decode the capture payload: size = %zu",
1363 capture_payload_size
);
1369 hit
->parent
.type
= LTTNG_CONDITION_TYPE_ON_EVENT
;
1370 hit
->parent
.serialize
= lttng_evaluation_on_event_serialize
;
1371 hit
->parent
.destroy
= lttng_evaluation_on_event_destroy
;
1373 evaluation
= &hit
->parent
;
1378 lttng_evaluation_on_event_destroy(&hit
->parent
);
1384 enum lttng_evaluation_status
lttng_evaluation_on_event_get_captured_values(
1385 const struct lttng_evaluation
*evaluation
,
1386 const struct lttng_event_field_value
**field_val
)
1388 struct lttng_evaluation_on_event
*hit
;
1389 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1391 if (!evaluation
|| !is_on_event_evaluation(evaluation
) ||
1393 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1397 hit
= container_of(evaluation
, struct lttng_evaluation_on_event
,
1399 if (!hit
->captured_values
) {
1400 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1404 *field_val
= hit
->captured_values
;
1410 enum lttng_evaluation_status
lttng_evaluation_on_event_get_trigger_name(
1411 const struct lttng_evaluation
*evaluation
, const char **name
)
1413 struct lttng_evaluation_on_event
*hit
;
1414 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
1416 if (!evaluation
|| !is_on_event_evaluation(evaluation
) || !name
) {
1417 status
= LTTNG_EVALUATION_STATUS_INVALID
;
1422 evaluation
, struct lttng_evaluation_on_event
, parent
);
1429 enum lttng_error_code
1430 lttng_condition_on_event_generate_capture_descriptor_bytecode(
1431 struct lttng_condition
*condition
)
1433 enum lttng_error_code ret
;
1434 enum lttng_condition_status status
;
1435 unsigned int capture_count
, i
;
1437 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
1438 ret
= LTTNG_ERR_FATAL
;
1442 status
= lttng_condition_on_event_get_capture_descriptor_count(
1443 condition
, &capture_count
);
1444 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1445 ret
= LTTNG_ERR_FATAL
;
1449 for (i
= 0; i
< capture_count
; i
++) {
1450 struct lttng_capture_descriptor
*local_capture_desc
=
1451 lttng_condition_on_event_get_internal_capture_descriptor_at_index(
1454 if (local_capture_desc
== NULL
) {
1455 ret
= LTTNG_ERR_FATAL
;
1459 /* Generate the bytecode. */
1460 status
= lttng_event_expr_to_bytecode(
1461 local_capture_desc
->event_expression
,
1462 &local_capture_desc
->bytecode
);
1463 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1464 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1469 /* Everything went better than expected */
1477 const struct lttng_bytecode
*
1478 lttng_condition_on_event_get_capture_bytecode_at_index(
1479 const struct lttng_condition
*condition
, unsigned int index
)
1481 const struct lttng_condition_on_event
*event_rule_cond
=
1482 container_of(condition
,
1483 const struct lttng_condition_on_event
,
1485 struct lttng_capture_descriptor
*desc
= NULL
;
1486 struct lttng_bytecode
*bytecode
= NULL
;
1488 enum lttng_condition_status status
;
1490 if (!condition
|| !IS_ON_EVENT_CONDITION(condition
)) {
1494 status
= lttng_condition_on_event_get_capture_descriptor_count(
1496 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1500 if (index
>= count
) {
1504 desc
= lttng_dynamic_pointer_array_get_pointer(
1505 &event_rule_cond
->capture_descriptors
, index
);
1510 bytecode
= desc
->bytecode
;