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/event-rule-matches-internal.h>
16 #include <lttng/condition/event-rule-matches.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_EVENT_RULE_MATCHES_CONDITION(condition) \
27 (lttng_condition_get_type(condition) == \
28 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES)
30 static bool is_event_rule_matches_evaluation(
31 const struct lttng_evaluation
*evaluation
)
33 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
35 return type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
38 static bool lttng_condition_event_rule_matches_validate(
39 const struct lttng_condition
*condition
);
40 static int lttng_condition_event_rule_matches_serialize(
41 const struct lttng_condition
*condition
,
42 struct lttng_payload
*payload
);
43 static bool lttng_condition_event_rule_matches_is_equal(
44 const struct lttng_condition
*_a
,
45 const struct lttng_condition
*_b
);
46 static void lttng_condition_event_rule_matches_destroy(
47 struct lttng_condition
*condition
);
49 static bool lttng_condition_event_rule_matches_validate(
50 const struct lttng_condition
*condition
)
53 struct lttng_condition_event_rule_matches
*event_rule
;
59 event_rule
= container_of(condition
,
60 struct lttng_condition_event_rule_matches
, 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
);
246 static struct lttng_capture_descriptor
*
247 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
248 const struct lttng_condition
*condition
, unsigned int index
)
250 const struct lttng_condition_event_rule_matches
251 *event_rule_matches_cond
= container_of(condition
,
252 const struct lttng_condition_event_rule_matches
,
254 struct lttng_capture_descriptor
*desc
= NULL
;
256 enum lttng_condition_status status
;
258 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
262 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
264 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
268 if (index
>= count
) {
272 desc
= lttng_dynamic_pointer_array_get_pointer(
273 &event_rule_matches_cond
->capture_descriptors
, index
);
278 static int lttng_condition_event_rule_matches_serialize(
279 const struct lttng_condition
*condition
,
280 struct lttng_payload
*payload
)
283 struct lttng_condition_event_rule_matches
*event_rule_matches_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_EVENT_RULE_MATCHES_CONDITION(condition
)) {
293 DBG("Serializing on event condition");
294 event_rule_matches_condition
= container_of(condition
,
295 struct lttng_condition_event_rule_matches
, parent
);
297 DBG("Serializing on event condition's event rule");
298 ret
= lttng_event_rule_serialize(
299 event_rule_matches_condition
->rule
, payload
);
304 status
= lttng_condition_event_rule_matches_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_event_rule_matches_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_event_rule_matches_get_capture_descriptor_count(
348 condition_a
, &capture_descr_count_a
);
349 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
353 status
= lttng_condition_event_rule_matches_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_matches_get_capture_descriptor_at_index(
367 const struct lttng_event_expr
*expr_b
=
368 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
371 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
385 static bool lttng_condition_event_rule_matches_is_equal(
386 const struct lttng_condition
*_a
,
387 const struct lttng_condition
*_b
)
389 bool is_equal
= false;
390 struct lttng_condition_event_rule_matches
*a
, *b
;
392 a
= container_of(_a
, struct lttng_condition_event_rule_matches
, parent
);
393 b
= container_of(_b
, struct lttng_condition_event_rule_matches
, parent
);
395 /* Both event rules must be set or both must be unset. */
396 if ((a
->rule
&& !b
->rule
) || (!a
->rule
&& b
->rule
)) {
397 WARN("Comparing event_rule conditions with uninitialized rule");
401 is_equal
= lttng_event_rule_is_equal(a
->rule
, b
->rule
);
406 is_equal
= capture_descriptors_are_equal(_a
, _b
);
412 static void lttng_condition_event_rule_matches_destroy(
413 struct lttng_condition
*condition
)
415 struct lttng_condition_event_rule_matches
*event_rule_matches_condition
;
417 event_rule_matches_condition
= container_of(condition
,
418 struct lttng_condition_event_rule_matches
, parent
);
420 lttng_event_rule_put(event_rule_matches_condition
->rule
);
421 lttng_dynamic_pointer_array_reset(
422 &event_rule_matches_condition
->capture_descriptors
);
423 free(event_rule_matches_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_event_rule_matches_create(
438 struct lttng_event_rule
*rule
)
440 struct lttng_condition
*parent
= NULL
;
441 struct lttng_condition_event_rule_matches
*condition
= NULL
;
447 condition
= zmalloc(sizeof(struct lttng_condition_event_rule_matches
));
452 lttng_condition_init(&condition
->parent
,
453 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
454 condition
->parent
.validate
=
455 lttng_condition_event_rule_matches_validate
,
456 condition
->parent
.serialize
=
457 lttng_condition_event_rule_matches_serialize
,
458 condition
->parent
.equal
= lttng_condition_event_rule_matches_is_equal
,
459 condition
->parent
.destroy
= lttng_condition_event_rule_matches_destroy
,
461 lttng_event_rule_get(rule
);
462 condition
->rule
= rule
;
465 lttng_dynamic_pointer_array_init(&condition
->capture_descriptors
,
466 destroy_capture_descriptor
);
468 parent
= &condition
->parent
;
474 uint64_t uint_from_buffer(const struct lttng_buffer_view
*view
, size_t size
,
478 const struct lttng_buffer_view uint_view
=
479 lttng_buffer_view_from_view(view
, *offset
, size
);
481 if (!lttng_buffer_view_is_valid(&uint_view
)) {
488 ret
= (uint64_t) *uint_view
.data
;
490 case sizeof(uint32_t):
494 memcpy(&u32
, uint_view
.data
, sizeof(u32
));
495 ret
= (uint64_t) u32
;
499 memcpy(&ret
, uint_view
.data
, sizeof(ret
));
512 const char *str_from_buffer(const struct lttng_buffer_view
*view
,
518 len
= uint_from_buffer(view
, sizeof(uint32_t), offset
);
519 if (len
== UINT64_C(-1)) {
523 ret
= &view
->data
[*offset
];
525 if (!lttng_buffer_view_contains_string(view
, ret
, len
)) {
540 struct lttng_event_expr
*event_expr_from_payload(
541 struct lttng_payload_view
*view
, size_t *offset
)
543 struct lttng_event_expr
*expr
= NULL
;
547 type
= uint_from_buffer(&view
->buffer
, sizeof(uint8_t), offset
);
548 if (type
== UINT64_C(-1)) {
553 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
554 str
= str_from_buffer(&view
->buffer
, offset
);
559 expr
= lttng_event_expr_event_payload_field_create(str
);
561 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
562 str
= str_from_buffer(&view
->buffer
, offset
);
567 expr
= lttng_event_expr_channel_context_field_create(str
);
569 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
571 const char *provider_name
;
572 const char *type_name
;
574 provider_name
= str_from_buffer(&view
->buffer
, offset
);
575 if (!provider_name
) {
579 type_name
= str_from_buffer(&view
->buffer
, offset
);
584 expr
= lttng_event_expr_app_specific_context_field_create(
585 provider_name
, type_name
);
588 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
590 struct lttng_event_expr
*array_field_expr
;
591 const uint64_t index
= uint_from_buffer(
592 &view
->buffer
, sizeof(uint32_t), offset
);
594 if (index
== UINT64_C(-1)) {
598 /* Array field expression is the encoded after this. */
599 array_field_expr
= event_expr_from_payload(view
, offset
);
600 if (!array_field_expr
) {
604 /* Move ownership of `array_field_expr` to new expression. */
605 expr
= lttng_event_expr_array_field_element_create(
606 array_field_expr
, (unsigned int) index
);
608 /* `array_field_expr` not moved: destroy it. */
609 lttng_event_expr_destroy(array_field_expr
);
615 ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64
,
623 lttng_event_expr_destroy(expr
);
631 ssize_t
lttng_condition_event_rule_matches_create_from_payload(
632 struct lttng_payload_view
*view
,
633 struct lttng_condition
**_condition
)
635 ssize_t consumed_length
;
637 ssize_t event_rule_length
;
638 uint32_t i
, capture_descr_count
;
639 struct lttng_condition
*condition
= NULL
;
640 struct lttng_event_rule
*event_rule
= NULL
;
642 if (!view
|| !_condition
) {
646 /* Struct lttng_event_rule. */
648 struct lttng_payload_view event_rule_view
=
649 lttng_payload_view_from_view(view
, offset
, -1);
651 event_rule_length
= lttng_event_rule_create_from_payload(
652 &event_rule_view
, &event_rule
);
655 if (event_rule_length
< 0 || !event_rule
) {
659 offset
+= event_rule_length
;
661 /* Create condition (no capture descriptors yet) at this point */
662 condition
= lttng_condition_event_rule_matches_create(event_rule
);
667 /* Capture descriptor count. */
668 assert(event_rule_length
>= 0);
669 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
670 if (capture_descr_count
== UINT32_C(-1)) {
674 /* Capture descriptors. */
675 for (i
= 0; i
< capture_descr_count
; i
++) {
676 enum lttng_condition_status status
;
677 struct lttng_event_expr
*expr
= event_expr_from_payload(
684 /* Move ownership of `expr` to `condition`. */
685 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
687 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
688 /* `expr` not moved: destroy it. */
689 lttng_event_expr_destroy(expr
);
694 consumed_length
= (ssize_t
) offset
;
695 *_condition
= condition
;
700 consumed_length
= -1;
703 lttng_event_rule_put(event_rule
);
704 lttng_condition_put(condition
);
705 return consumed_length
;
709 enum lttng_condition_status
710 lttng_condition_event_rule_matches_borrow_rule_mutable(
711 const struct lttng_condition
*condition
,
712 struct lttng_event_rule
**rule
)
714 struct lttng_condition_event_rule_matches
*event_rule
;
715 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
717 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
719 status
= LTTNG_CONDITION_STATUS_INVALID
;
723 event_rule
= container_of(condition
,
724 struct lttng_condition_event_rule_matches
, parent
);
725 if (!event_rule
->rule
) {
726 status
= LTTNG_CONDITION_STATUS_UNSET
;
730 *rule
= event_rule
->rule
;
735 enum lttng_condition_status
lttng_condition_event_rule_matches_get_rule(
736 const struct lttng_condition
*condition
,
737 const struct lttng_event_rule
**rule
)
739 struct lttng_event_rule
*mutable_rule
= NULL
;
740 const enum lttng_condition_status status
=
741 lttng_condition_event_rule_matches_borrow_rule_mutable(
742 condition
, &mutable_rule
);
744 *rule
= mutable_rule
;
749 void lttng_condition_event_rule_matches_set_error_counter_index(
750 struct lttng_condition
*condition
, uint64_t error_counter_index
)
752 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
753 container_of(condition
,
754 struct lttng_condition_event_rule_matches
,
757 LTTNG_OPTIONAL_SET(&event_rule_matches_cond
->error_counter_index
,
758 error_counter_index
);
762 uint64_t lttng_condition_event_rule_matches_get_error_counter_index(
763 const struct lttng_condition
*condition
)
765 const struct lttng_condition_event_rule_matches
766 *event_rule_matches_cond
= container_of(condition
,
767 const struct lttng_condition_event_rule_matches
,
770 return LTTNG_OPTIONAL_GET(event_rule_matches_cond
->error_counter_index
);
773 enum lttng_condition_status
774 lttng_condition_event_rule_matches_append_capture_descriptor(
775 struct lttng_condition
*condition
,
776 struct lttng_event_expr
*expr
)
779 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
780 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
781 container_of(condition
,
782 struct lttng_condition_event_rule_matches
,
784 struct lttng_capture_descriptor
*descriptor
= NULL
;
785 const struct lttng_event_rule
*rule
= NULL
;
787 /* Only accept l-values. */
788 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
789 !expr
|| !lttng_event_expr_is_lvalue(expr
)) {
790 status
= LTTNG_CONDITION_STATUS_INVALID
;
794 status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
795 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
799 switch(lttng_event_rule_get_type(rule
)) {
800 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
801 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
803 status
= LTTNG_CONDITION_STATUS_OK
;
805 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
806 status
= LTTNG_CONDITION_STATUS_INVALID
;
809 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
813 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
817 descriptor
= malloc(sizeof(*descriptor
));
818 if (descriptor
== NULL
) {
819 status
= LTTNG_CONDITION_STATUS_ERROR
;
823 descriptor
->event_expression
= expr
;
824 descriptor
->bytecode
= NULL
;
826 ret
= lttng_dynamic_pointer_array_add_pointer(
827 &event_rule_matches_cond
->capture_descriptors
,
830 status
= LTTNG_CONDITION_STATUS_ERROR
;
834 /* Ownership is transfered to the internal capture_descriptors array */
841 enum lttng_condition_status
842 lttng_condition_event_rule_matches_get_capture_descriptor_count(
843 const struct lttng_condition
*condition
, unsigned int *count
)
845 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
846 const struct lttng_condition_event_rule_matches
847 *event_rule_matches_condition
= container_of(condition
,
848 const struct lttng_condition_event_rule_matches
,
851 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
853 status
= LTTNG_CONDITION_STATUS_INVALID
;
857 *count
= lttng_dynamic_pointer_array_get_count(
858 &event_rule_matches_condition
->capture_descriptors
);
864 const struct lttng_event_expr
*
865 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
866 const struct lttng_condition
*condition
, unsigned int index
)
868 const struct lttng_event_expr
*expr
= NULL
;
869 const struct lttng_capture_descriptor
*desc
= NULL
;
871 desc
= lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
876 expr
= desc
->event_expression
;
883 ssize_t
lttng_evaluation_event_rule_matches_create_from_payload(
884 const struct lttng_condition_event_rule_matches
*condition
,
885 struct lttng_payload_view
*view
,
886 struct lttng_evaluation
**_evaluation
)
888 ssize_t ret
, offset
= 0;
889 struct lttng_evaluation
*evaluation
= NULL
;
890 uint32_t capture_payload_size
;
891 const char *capture_payload
= NULL
;
899 const struct lttng_payload_view current_view
=
900 lttng_payload_view_from_view(view
, offset
, -1);
902 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
907 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
908 sizeof(capture_payload_size
));
910 offset
+= sizeof(capture_payload_size
);
912 if (capture_payload_size
> 0) {
913 const struct lttng_payload_view current_view
=
914 lttng_payload_view_from_view(view
, offset
, -1);
916 if (current_view
.buffer
.size
< capture_payload_size
) {
921 capture_payload
= current_view
.buffer
.data
;
924 evaluation
= lttng_evaluation_event_rule_matches_create(
925 condition
, capture_payload
, capture_payload_size
, true);
931 offset
+= capture_payload_size
;
932 *_evaluation
= evaluation
;
937 lttng_evaluation_destroy(evaluation
);
941 static int lttng_evaluation_event_rule_matches_serialize(
942 const struct lttng_evaluation
*evaluation
,
943 struct lttng_payload
*payload
)
946 struct lttng_evaluation_event_rule_matches
*hit
;
947 uint32_t capture_payload_size
;
949 hit
= container_of(evaluation
,
950 struct lttng_evaluation_event_rule_matches
, parent
);
952 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
953 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
954 sizeof(capture_payload_size
));
959 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
960 hit
->capture_payload
.size
);
970 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
972 bool is_equal
= true;
974 assert(obj
->type
== MSGPACK_OBJECT_STR
);
976 if (obj
->via
.str
.size
!= strlen(str
)) {
981 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
991 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
994 const msgpack_object
*ret
= NULL
;
997 assert(map_obj
->type
== MSGPACK_OBJECT_MAP
);
999 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
1000 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
1002 assert(kv
->key
.type
== MSGPACK_OBJECT_STR
);
1004 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1014 static void lttng_evaluation_event_rule_matches_destroy(
1015 struct lttng_evaluation
*evaluation
)
1017 struct lttng_evaluation_event_rule_matches
*hit
;
1019 hit
= container_of(evaluation
,
1020 struct lttng_evaluation_event_rule_matches
, parent
);
1021 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1022 lttng_event_field_value_destroy(hit
->captured_values
);
1027 int event_field_value_from_obj(const msgpack_object
*obj
,
1028 struct lttng_event_field_value
**field_val
)
1035 switch (obj
->type
) {
1036 case MSGPACK_OBJECT_NIL
:
1040 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1041 *field_val
= lttng_event_field_value_uint_create(
1044 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1045 *field_val
= lttng_event_field_value_int_create(
1048 case MSGPACK_OBJECT_FLOAT32
:
1049 case MSGPACK_OBJECT_FLOAT64
:
1050 *field_val
= lttng_event_field_value_real_create(
1053 case MSGPACK_OBJECT_STR
:
1054 *field_val
= lttng_event_field_value_string_create_with_size(
1055 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1057 case MSGPACK_OBJECT_ARRAY
:
1061 *field_val
= lttng_event_field_value_array_create();
1066 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1067 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1068 struct lttng_event_field_value
*elem_field_val
;
1070 ret
= event_field_value_from_obj(elem_obj
,
1076 if (elem_field_val
) {
1077 ret
= lttng_event_field_value_array_append(
1078 *field_val
, elem_field_val
);
1080 ret
= lttng_event_field_value_array_append_unavailable(
1085 lttng_event_field_value_destroy(elem_field_val
);
1092 case MSGPACK_OBJECT_MAP
:
1095 * As of this version, the only valid map object is
1096 * for an enumeration value, for example:
1103 * - Carling Black Label
1105 const msgpack_object
*inner_obj
;
1108 inner_obj
= get_msgpack_map_obj(obj
, "type");
1110 ERR("Missing `type` entry in map object");
1114 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1115 ERR("Map object's `type` entry is not a string: type = %s",
1116 msgpack_object_type_str(inner_obj
->type
));
1120 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1121 ERR("Map object's `type` entry: expecting `enum`");
1125 inner_obj
= get_msgpack_map_obj(obj
, "value");
1127 ERR("Missing `value` entry in map object");
1131 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1132 *field_val
= lttng_event_field_value_enum_uint_create(
1133 inner_obj
->via
.u64
);
1134 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1135 *field_val
= lttng_event_field_value_enum_int_create(
1136 inner_obj
->via
.i64
);
1138 ERR("Map object's `value` entry is not an integer: type = %s",
1139 msgpack_object_type_str(inner_obj
->type
));
1147 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1153 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1154 ERR("Map object's `labels` entry is not an array: type = %s",
1155 msgpack_object_type_str(inner_obj
->type
));
1159 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1162 const msgpack_object
*elem_obj
=
1163 &inner_obj
->via
.array
.ptr
[label_i
];
1165 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1166 ERR("Map object's `labels` entry's type is not a string: type = %s",
1167 msgpack_object_type_str(elem_obj
->type
));
1171 iret
= lttng_event_field_value_enum_append_label_with_size(
1172 *field_val
, elem_obj
->via
.str
.ptr
,
1173 elem_obj
->via
.str
.size
);
1182 ERR("Unexpected object type: type = %s",
1183 msgpack_object_type_str(obj
->type
));
1194 lttng_event_field_value_destroy(*field_val
);
1202 static struct lttng_event_field_value
*event_field_value_from_capture_payload(
1203 const struct lttng_condition_event_rule_matches
*condition
,
1204 const char *capture_payload
,
1205 size_t capture_payload_size
)
1207 struct lttng_event_field_value
*ret
= NULL
;
1208 msgpack_unpacked unpacked
;
1209 msgpack_unpack_return unpack_return
;
1210 const msgpack_object
*root_obj
;
1211 const msgpack_object_array
*root_array_obj
;
1216 assert(capture_payload
);
1218 /* Initialize value. */
1219 msgpack_unpacked_init(&unpacked
);
1222 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1223 capture_payload_size
, NULL
);
1224 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1225 ERR("msgpack_unpack_next() failed to decode the "
1226 "MessagePack-encoded capture payload: "
1227 "size = %zu, ret = %d",
1228 capture_payload_size
, unpack_return
);
1232 /* Get root array. */
1233 root_obj
= &unpacked
.data
;
1235 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1236 ERR("Expecting an array as the root object: type = %s",
1237 msgpack_object_type_str(root_obj
->type
));
1241 root_array_obj
= &root_obj
->via
.array
;
1243 /* Create an empty root array event field value. */
1244 ret
= lttng_event_field_value_array_create();
1250 * For each capture descriptor in the condition object:
1252 * 1. Get its corresponding captured field value MessagePack
1255 * 2. Create a corresponding event field value.
1257 * 3. Append it to `ret` (the root array event field value).
1259 count
= lttng_dynamic_pointer_array_get_count(
1260 &condition
->capture_descriptors
);
1263 for (i
= 0; i
< count
; i
++) {
1264 const struct lttng_capture_descriptor
*capture_descriptor
=
1265 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1266 &condition
->parent
, i
);
1267 const msgpack_object
*elem_obj
;
1268 struct lttng_event_field_value
*elem_field_val
;
1271 assert(capture_descriptor
);
1273 elem_obj
= &root_array_obj
->ptr
[i
];
1274 iret
= event_field_value_from_obj(elem_obj
,
1280 if (elem_field_val
) {
1281 iret
= lttng_event_field_value_array_append(ret
,
1284 iret
= lttng_event_field_value_array_append_unavailable(
1289 lttng_event_field_value_destroy(elem_field_val
);
1297 lttng_event_field_value_destroy(ret
);
1301 msgpack_unpacked_destroy(&unpacked
);
1306 struct lttng_evaluation
*lttng_evaluation_event_rule_matches_create(
1307 const struct lttng_condition_event_rule_matches
*condition
,
1308 const char *capture_payload
,
1309 size_t capture_payload_size
,
1310 bool decode_capture_payload
)
1312 struct lttng_evaluation_event_rule_matches
*hit
;
1313 struct lttng_evaluation
*evaluation
= NULL
;
1315 hit
= zmalloc(sizeof(struct lttng_evaluation_event_rule_matches
));
1320 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1322 if (capture_payload
) {
1323 const int ret
= lttng_dynamic_buffer_append(
1324 &hit
->capture_payload
, capture_payload
,
1325 capture_payload_size
);
1327 ERR("Failed to initialize capture payload of event rule evaluation");
1331 if (decode_capture_payload
) {
1332 hit
->captured_values
=
1333 event_field_value_from_capture_payload(
1336 capture_payload_size
);
1337 if (!hit
->captured_values
) {
1338 ERR("Failed to decode the capture payload: size = %zu",
1339 capture_payload_size
);
1345 hit
->parent
.type
= LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
1346 hit
->parent
.serialize
= lttng_evaluation_event_rule_matches_serialize
;
1347 hit
->parent
.destroy
= lttng_evaluation_event_rule_matches_destroy
;
1349 evaluation
= &hit
->parent
;
1354 lttng_evaluation_event_rule_matches_destroy(&hit
->parent
);
1360 enum lttng_evaluation_event_rule_matches_status
1361 lttng_evaluation_event_rule_matches_get_captured_values(
1362 const struct lttng_evaluation
*evaluation
,
1363 const struct lttng_event_field_value
**field_val
)
1365 struct lttng_evaluation_event_rule_matches
*hit
;
1366 enum lttng_evaluation_event_rule_matches_status status
=
1367 LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_OK
;
1369 if (!evaluation
|| !is_event_rule_matches_evaluation(evaluation
) ||
1371 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_INVALID
;
1375 hit
= container_of(evaluation
,
1376 struct lttng_evaluation_event_rule_matches
, parent
);
1377 if (!hit
->captured_values
) {
1378 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_NONE
;
1382 *field_val
= hit
->captured_values
;
1389 enum lttng_error_code
1390 lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
1391 struct lttng_condition
*condition
)
1393 enum lttng_error_code ret
;
1394 enum lttng_condition_status status
;
1395 unsigned int capture_count
, i
;
1397 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1398 ret
= LTTNG_ERR_FATAL
;
1402 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1403 condition
, &capture_count
);
1404 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1405 ret
= LTTNG_ERR_FATAL
;
1409 for (i
= 0; i
< capture_count
; i
++) {
1410 struct lttng_capture_descriptor
*local_capture_desc
=
1411 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1414 if (local_capture_desc
== NULL
) {
1415 ret
= LTTNG_ERR_FATAL
;
1419 /* Generate the bytecode. */
1420 status
= lttng_event_expr_to_bytecode(
1421 local_capture_desc
->event_expression
,
1422 &local_capture_desc
->bytecode
);
1423 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1424 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1429 /* Everything went better than expected */
1437 const struct lttng_bytecode
*
1438 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
1439 const struct lttng_condition
*condition
, unsigned int index
)
1441 const struct lttng_condition_event_rule_matches
1442 *event_rule_matches_cond
= container_of(condition
,
1443 const struct lttng_condition_event_rule_matches
,
1445 struct lttng_capture_descriptor
*desc
= NULL
;
1446 struct lttng_bytecode
*bytecode
= NULL
;
1448 enum lttng_condition_status status
;
1450 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1454 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1456 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1460 if (index
>= count
) {
1464 desc
= lttng_dynamic_pointer_array_get_pointer(
1465 &event_rule_matches_cond
->capture_descriptors
, index
);
1470 bytecode
= desc
->bytecode
;