2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.h>
9 #include <common/macros.h>
10 #include <common/mi-lttng.h>
13 #include <lttng/condition/condition-internal.h>
14 #include <lttng/condition/event-rule-matches-internal.h>
15 #include <lttng/condition/event-rule-matches.h>
16 #include <lttng/event-expr-internal.h>
17 #include <lttng/event-expr.h>
18 #include <lttng/event-field-value-internal.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/lttng-error.h>
23 #include <vendor/msgpack/msgpack.h>
25 #define IS_EVENT_RULE_MATCHES_CONDITION(condition) \
26 (lttng_condition_get_type(condition) == \
27 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES)
29 static bool is_event_rule_matches_evaluation(
30 const struct lttng_evaluation
*evaluation
)
32 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
34 return type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
37 static bool lttng_condition_event_rule_matches_validate(
38 const struct lttng_condition
*condition
);
39 static int lttng_condition_event_rule_matches_serialize(
40 const struct lttng_condition
*condition
,
41 struct lttng_payload
*payload
);
42 static bool lttng_condition_event_rule_matches_is_equal(
43 const struct lttng_condition
*_a
,
44 const struct lttng_condition
*_b
);
45 static void lttng_condition_event_rule_matches_destroy(
46 struct lttng_condition
*condition
);
48 static bool lttng_condition_event_rule_matches_validate(
49 const struct lttng_condition
*condition
)
52 struct lttng_condition_event_rule_matches
*event_rule
;
58 event_rule
= container_of(condition
,
59 struct lttng_condition_event_rule_matches
, 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
);
245 static struct lttng_capture_descriptor
*
246 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
247 const struct lttng_condition
*condition
, unsigned int index
)
249 const struct lttng_condition_event_rule_matches
250 *event_rule_matches_cond
= container_of(condition
,
251 const struct lttng_condition_event_rule_matches
,
253 struct lttng_capture_descriptor
*desc
= NULL
;
255 enum lttng_condition_status status
;
257 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
261 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
263 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
267 if (index
>= count
) {
271 desc
= lttng_dynamic_pointer_array_get_pointer(
272 &event_rule_matches_cond
->capture_descriptors
, index
);
277 static int lttng_condition_event_rule_matches_serialize(
278 const struct lttng_condition
*condition
,
279 struct lttng_payload
*payload
)
282 struct lttng_condition_event_rule_matches
*event_rule_matches_condition
;
283 enum lttng_condition_status status
;
284 /* Used for iteration and communication (size matters). */
285 uint32_t i
, capture_descr_count
;
287 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
292 DBG("Serializing on event condition");
293 event_rule_matches_condition
= container_of(condition
,
294 struct lttng_condition_event_rule_matches
, parent
);
296 DBG("Serializing on event condition's event rule");
297 ret
= lttng_event_rule_serialize(
298 event_rule_matches_condition
->rule
, payload
);
303 status
= lttng_condition_event_rule_matches_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_event_rule_matches_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_event_rule_matches_get_capture_descriptor_count(
347 condition_a
, &capture_descr_count_a
);
348 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
352 status
= lttng_condition_event_rule_matches_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_event_rule_matches_get_capture_descriptor_at_index(
366 const struct lttng_event_expr
*expr_b
=
367 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
370 if (!lttng_event_expr_is_equal(expr_a
, expr_b
)) {
384 static bool lttng_condition_event_rule_matches_is_equal(
385 const struct lttng_condition
*_a
,
386 const struct lttng_condition
*_b
)
388 bool is_equal
= false;
389 struct lttng_condition_event_rule_matches
*a
, *b
;
391 a
= container_of(_a
, struct lttng_condition_event_rule_matches
, parent
);
392 b
= container_of(_b
, struct lttng_condition_event_rule_matches
, parent
);
394 /* Both event rules must be set or both must be unset. */
395 if ((a
->rule
&& !b
->rule
) || (!a
->rule
&& b
->rule
)) {
396 WARN("Comparing event_rule conditions with uninitialized rule");
400 is_equal
= lttng_event_rule_is_equal(a
->rule
, b
->rule
);
405 is_equal
= capture_descriptors_are_equal(_a
, _b
);
411 static void lttng_condition_event_rule_matches_destroy(
412 struct lttng_condition
*condition
)
414 struct lttng_condition_event_rule_matches
*event_rule_matches_condition
;
416 event_rule_matches_condition
= container_of(condition
,
417 struct lttng_condition_event_rule_matches
, parent
);
419 lttng_event_rule_put(event_rule_matches_condition
->rule
);
420 lttng_dynamic_pointer_array_reset(
421 &event_rule_matches_condition
->capture_descriptors
);
422 free(event_rule_matches_condition
);
426 void destroy_capture_descriptor(void *ptr
)
428 struct lttng_capture_descriptor
*desc
=
429 (struct lttng_capture_descriptor
*) ptr
;
431 lttng_event_expr_destroy(desc
->event_expression
);
432 free(desc
->bytecode
);
436 static enum lttng_error_code
lttng_condition_event_rule_matches_mi_serialize(
437 const struct lttng_condition
*condition
,
438 struct mi_writer
*writer
)
441 enum lttng_error_code ret_code
;
442 enum lttng_condition_status status
;
443 const struct lttng_event_rule
*rule
= NULL
;
444 unsigned int capture_descriptor_count
, i
;
446 LTTNG_ASSERT(condition
);
447 LTTNG_ASSERT(writer
);
448 LTTNG_ASSERT(IS_EVENT_RULE_MATCHES_CONDITION(condition
));
450 status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
451 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
452 LTTNG_ASSERT(rule
!= NULL
);
454 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
455 condition
, &capture_descriptor_count
);
456 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
458 /* Open condition event rule matches element. */
459 ret
= mi_lttng_writer_open_element(
460 writer
, mi_lttng_element_condition_event_rule_matches
);
465 /* Serialize the event rule. */
466 ret_code
= lttng_event_rule_mi_serialize(rule
, writer
);
467 if (ret_code
!= LTTNG_OK
) {
471 /* Open the capture descriptors element. */
472 ret
= mi_lttng_writer_open_element(
473 writer
, mi_lttng_element_capture_descriptors
);
478 for (i
= 0; i
< capture_descriptor_count
; i
++) {
479 const struct lttng_event_expr
*descriptor
= NULL
;
481 descriptor
= lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
483 LTTNG_ASSERT(descriptor
);
485 ret_code
= lttng_event_expr_mi_serialize(descriptor
, writer
);
486 if (ret_code
!= LTTNG_OK
) {
491 /* Close capture descriptors element. */
492 ret
= mi_lttng_writer_close_element(writer
);
497 /* Close condition_event_rule_matches. */
498 ret
= mi_lttng_writer_close_element(writer
);
506 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
511 struct lttng_condition
*lttng_condition_event_rule_matches_create(
512 struct lttng_event_rule
*rule
)
514 struct lttng_condition
*parent
= NULL
;
515 struct lttng_condition_event_rule_matches
*condition
= NULL
;
521 condition
= zmalloc(sizeof(struct lttng_condition_event_rule_matches
));
526 lttng_condition_init(&condition
->parent
,
527 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
528 condition
->parent
.validate
=
529 lttng_condition_event_rule_matches_validate
,
530 condition
->parent
.serialize
=
531 lttng_condition_event_rule_matches_serialize
,
532 condition
->parent
.equal
= lttng_condition_event_rule_matches_is_equal
,
533 condition
->parent
.destroy
= lttng_condition_event_rule_matches_destroy
,
534 condition
->parent
.mi_serialize
= lttng_condition_event_rule_matches_mi_serialize
,
536 lttng_event_rule_get(rule
);
537 condition
->rule
= rule
;
540 lttng_dynamic_pointer_array_init(&condition
->capture_descriptors
,
541 destroy_capture_descriptor
);
543 parent
= &condition
->parent
;
549 uint64_t uint_from_buffer(const struct lttng_buffer_view
*view
, size_t size
,
553 const struct lttng_buffer_view uint_view
=
554 lttng_buffer_view_from_view(view
, *offset
, size
);
556 if (!lttng_buffer_view_is_valid(&uint_view
)) {
563 ret
= (uint64_t) *uint_view
.data
;
565 case sizeof(uint32_t):
569 memcpy(&u32
, uint_view
.data
, sizeof(u32
));
570 ret
= (uint64_t) u32
;
574 memcpy(&ret
, uint_view
.data
, sizeof(ret
));
587 const char *str_from_buffer(const struct lttng_buffer_view
*view
,
593 len
= uint_from_buffer(view
, sizeof(uint32_t), offset
);
594 if (len
== UINT64_C(-1)) {
598 ret
= &view
->data
[*offset
];
600 if (!lttng_buffer_view_contains_string(view
, ret
, len
)) {
615 struct lttng_event_expr
*event_expr_from_payload(
616 struct lttng_payload_view
*view
, size_t *offset
)
618 struct lttng_event_expr
*expr
= NULL
;
622 type
= uint_from_buffer(&view
->buffer
, sizeof(uint8_t), offset
);
623 if (type
== UINT64_C(-1)) {
628 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
629 str
= str_from_buffer(&view
->buffer
, offset
);
634 expr
= lttng_event_expr_event_payload_field_create(str
);
636 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
637 str
= str_from_buffer(&view
->buffer
, offset
);
642 expr
= lttng_event_expr_channel_context_field_create(str
);
644 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
646 const char *provider_name
;
647 const char *type_name
;
649 provider_name
= str_from_buffer(&view
->buffer
, offset
);
650 if (!provider_name
) {
654 type_name
= str_from_buffer(&view
->buffer
, offset
);
659 expr
= lttng_event_expr_app_specific_context_field_create(
660 provider_name
, type_name
);
663 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
665 struct lttng_event_expr
*array_field_expr
;
666 const uint64_t index
= uint_from_buffer(
667 &view
->buffer
, sizeof(uint32_t), offset
);
669 if (index
== UINT64_C(-1)) {
673 /* Array field expression is the encoded after this. */
674 array_field_expr
= event_expr_from_payload(view
, offset
);
675 if (!array_field_expr
) {
679 /* Move ownership of `array_field_expr` to new expression. */
680 expr
= lttng_event_expr_array_field_element_create(
681 array_field_expr
, (unsigned int) index
);
683 /* `array_field_expr` not moved: destroy it. */
684 lttng_event_expr_destroy(array_field_expr
);
690 ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64
,
698 lttng_event_expr_destroy(expr
);
706 ssize_t
lttng_condition_event_rule_matches_create_from_payload(
707 struct lttng_payload_view
*view
,
708 struct lttng_condition
**_condition
)
710 ssize_t consumed_length
;
712 ssize_t event_rule_length
;
713 uint32_t i
, capture_descr_count
;
714 struct lttng_condition
*condition
= NULL
;
715 struct lttng_event_rule
*event_rule
= NULL
;
717 if (!view
|| !_condition
) {
721 /* Struct lttng_event_rule. */
723 struct lttng_payload_view event_rule_view
=
724 lttng_payload_view_from_view(view
, offset
, -1);
726 event_rule_length
= lttng_event_rule_create_from_payload(
727 &event_rule_view
, &event_rule
);
730 if (event_rule_length
< 0 || !event_rule
) {
734 offset
+= event_rule_length
;
736 /* Create condition (no capture descriptors yet) at this point */
737 condition
= lttng_condition_event_rule_matches_create(event_rule
);
742 /* Capture descriptor count. */
743 LTTNG_ASSERT(event_rule_length
>= 0);
744 capture_descr_count
= uint_from_buffer(&view
->buffer
, sizeof(uint32_t), &offset
);
745 if (capture_descr_count
== UINT32_C(-1)) {
749 /* Capture descriptors. */
750 for (i
= 0; i
< capture_descr_count
; i
++) {
751 enum lttng_condition_status status
;
752 struct lttng_event_expr
*expr
= event_expr_from_payload(
759 /* Move ownership of `expr` to `condition`. */
760 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
762 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
763 /* `expr` not moved: destroy it. */
764 lttng_event_expr_destroy(expr
);
769 consumed_length
= (ssize_t
) offset
;
770 *_condition
= condition
;
775 consumed_length
= -1;
778 lttng_event_rule_put(event_rule
);
779 lttng_condition_put(condition
);
780 return consumed_length
;
784 enum lttng_condition_status
785 lttng_condition_event_rule_matches_borrow_rule_mutable(
786 const struct lttng_condition
*condition
,
787 struct lttng_event_rule
**rule
)
789 struct lttng_condition_event_rule_matches
*event_rule
;
790 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
792 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
794 status
= LTTNG_CONDITION_STATUS_INVALID
;
798 event_rule
= container_of(condition
,
799 struct lttng_condition_event_rule_matches
, parent
);
800 if (!event_rule
->rule
) {
801 status
= LTTNG_CONDITION_STATUS_UNSET
;
805 *rule
= event_rule
->rule
;
810 enum lttng_condition_status
lttng_condition_event_rule_matches_get_rule(
811 const struct lttng_condition
*condition
,
812 const struct lttng_event_rule
**rule
)
814 struct lttng_event_rule
*mutable_rule
= NULL
;
815 const enum lttng_condition_status status
=
816 lttng_condition_event_rule_matches_borrow_rule_mutable(
817 condition
, &mutable_rule
);
819 *rule
= mutable_rule
;
824 void lttng_condition_event_rule_matches_set_error_counter_index(
825 struct lttng_condition
*condition
, uint64_t error_counter_index
)
827 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
828 container_of(condition
,
829 struct lttng_condition_event_rule_matches
,
832 LTTNG_OPTIONAL_SET(&event_rule_matches_cond
->error_counter_index
,
833 error_counter_index
);
837 uint64_t lttng_condition_event_rule_matches_get_error_counter_index(
838 const struct lttng_condition
*condition
)
840 const struct lttng_condition_event_rule_matches
841 *event_rule_matches_cond
= container_of(condition
,
842 const struct lttng_condition_event_rule_matches
,
845 return LTTNG_OPTIONAL_GET(event_rule_matches_cond
->error_counter_index
);
848 enum lttng_condition_status
849 lttng_condition_event_rule_matches_append_capture_descriptor(
850 struct lttng_condition
*condition
,
851 struct lttng_event_expr
*expr
)
854 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
855 struct lttng_condition_event_rule_matches
*event_rule_matches_cond
=
856 container_of(condition
,
857 struct lttng_condition_event_rule_matches
,
859 struct lttng_capture_descriptor
*descriptor
= NULL
;
860 const struct lttng_event_rule
*rule
= NULL
;
862 /* Only accept l-values. */
863 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
864 !expr
|| !lttng_event_expr_is_lvalue(expr
)) {
865 status
= LTTNG_CONDITION_STATUS_INVALID
;
869 status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
870 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
874 switch(lttng_event_rule_get_type(rule
)) {
875 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
876 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
877 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
878 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
879 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
880 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
882 status
= LTTNG_CONDITION_STATUS_OK
;
884 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
885 status
= LTTNG_CONDITION_STATUS_INVALID
;
888 status
= LTTNG_CONDITION_STATUS_UNSUPPORTED
;
892 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
896 descriptor
= malloc(sizeof(*descriptor
));
897 if (descriptor
== NULL
) {
898 status
= LTTNG_CONDITION_STATUS_ERROR
;
902 descriptor
->event_expression
= expr
;
903 descriptor
->bytecode
= NULL
;
905 ret
= lttng_dynamic_pointer_array_add_pointer(
906 &event_rule_matches_cond
->capture_descriptors
,
909 status
= LTTNG_CONDITION_STATUS_ERROR
;
913 /* Ownership is transfered to the internal capture_descriptors array */
920 enum lttng_condition_status
921 lttng_condition_event_rule_matches_get_capture_descriptor_count(
922 const struct lttng_condition
*condition
, unsigned int *count
)
924 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
925 const struct lttng_condition_event_rule_matches
926 *event_rule_matches_condition
= container_of(condition
,
927 const struct lttng_condition_event_rule_matches
,
930 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
) ||
932 status
= LTTNG_CONDITION_STATUS_INVALID
;
936 *count
= lttng_dynamic_pointer_array_get_count(
937 &event_rule_matches_condition
->capture_descriptors
);
943 const struct lttng_event_expr
*
944 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
945 const struct lttng_condition
*condition
, unsigned int index
)
947 const struct lttng_event_expr
*expr
= NULL
;
948 const struct lttng_capture_descriptor
*desc
= NULL
;
950 desc
= lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
955 expr
= desc
->event_expression
;
962 ssize_t
lttng_evaluation_event_rule_matches_create_from_payload(
963 const struct lttng_condition_event_rule_matches
*condition
,
964 struct lttng_payload_view
*view
,
965 struct lttng_evaluation
**_evaluation
)
967 ssize_t ret
, offset
= 0;
968 struct lttng_evaluation
*evaluation
= NULL
;
969 uint32_t capture_payload_size
;
970 const char *capture_payload
= NULL
;
978 const struct lttng_payload_view current_view
=
979 lttng_payload_view_from_view(view
, offset
, -1);
981 if (current_view
.buffer
.size
< sizeof(capture_payload_size
)) {
986 memcpy(&capture_payload_size
, current_view
.buffer
.data
,
987 sizeof(capture_payload_size
));
989 offset
+= sizeof(capture_payload_size
);
991 if (capture_payload_size
> 0) {
992 const struct lttng_payload_view current_view
=
993 lttng_payload_view_from_view(view
, offset
, -1);
995 if (current_view
.buffer
.size
< capture_payload_size
) {
1000 capture_payload
= current_view
.buffer
.data
;
1003 evaluation
= lttng_evaluation_event_rule_matches_create(
1004 condition
, capture_payload
, capture_payload_size
, true);
1010 offset
+= capture_payload_size
;
1011 *_evaluation
= evaluation
;
1016 lttng_evaluation_destroy(evaluation
);
1020 static int lttng_evaluation_event_rule_matches_serialize(
1021 const struct lttng_evaluation
*evaluation
,
1022 struct lttng_payload
*payload
)
1025 struct lttng_evaluation_event_rule_matches
*hit
;
1026 uint32_t capture_payload_size
;
1028 hit
= container_of(evaluation
,
1029 struct lttng_evaluation_event_rule_matches
, parent
);
1031 capture_payload_size
= (uint32_t) hit
->capture_payload
.size
;
1032 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &capture_payload_size
,
1033 sizeof(capture_payload_size
));
1038 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, hit
->capture_payload
.data
,
1039 hit
->capture_payload
.size
);
1049 bool msgpack_str_is_equal(const struct msgpack_object
*obj
, const char *str
)
1051 bool is_equal
= true;
1053 LTTNG_ASSERT(obj
->type
== MSGPACK_OBJECT_STR
);
1055 if (obj
->via
.str
.size
!= strlen(str
)) {
1060 if (strncmp(obj
->via
.str
.ptr
, str
, obj
->via
.str
.size
) != 0) {
1070 const msgpack_object
*get_msgpack_map_obj(const struct msgpack_object
*map_obj
,
1073 const msgpack_object
*ret
= NULL
;
1076 LTTNG_ASSERT(map_obj
->type
== MSGPACK_OBJECT_MAP
);
1078 for (i
= 0; i
< map_obj
->via
.map
.size
; i
++) {
1079 const struct msgpack_object_kv
*kv
= &map_obj
->via
.map
.ptr
[i
];
1081 LTTNG_ASSERT(kv
->key
.type
== MSGPACK_OBJECT_STR
);
1083 if (msgpack_str_is_equal(&kv
->key
, name
)) {
1093 static void lttng_evaluation_event_rule_matches_destroy(
1094 struct lttng_evaluation
*evaluation
)
1096 struct lttng_evaluation_event_rule_matches
*hit
;
1098 hit
= container_of(evaluation
,
1099 struct lttng_evaluation_event_rule_matches
, parent
);
1100 lttng_dynamic_buffer_reset(&hit
->capture_payload
);
1101 lttng_event_field_value_destroy(hit
->captured_values
);
1106 int event_field_value_from_obj(const msgpack_object
*obj
,
1107 struct lttng_event_field_value
**field_val
)
1112 LTTNG_ASSERT(field_val
);
1114 switch (obj
->type
) {
1115 case MSGPACK_OBJECT_NIL
:
1119 case MSGPACK_OBJECT_POSITIVE_INTEGER
:
1120 *field_val
= lttng_event_field_value_uint_create(
1123 case MSGPACK_OBJECT_NEGATIVE_INTEGER
:
1124 *field_val
= lttng_event_field_value_int_create(
1127 case MSGPACK_OBJECT_FLOAT32
:
1128 case MSGPACK_OBJECT_FLOAT64
:
1129 *field_val
= lttng_event_field_value_real_create(
1132 case MSGPACK_OBJECT_STR
:
1133 *field_val
= lttng_event_field_value_string_create_with_size(
1134 obj
->via
.str
.ptr
, obj
->via
.str
.size
);
1136 case MSGPACK_OBJECT_ARRAY
:
1140 *field_val
= lttng_event_field_value_array_create();
1145 for (i
= 0; i
< obj
->via
.array
.size
; i
++) {
1146 const msgpack_object
*elem_obj
= &obj
->via
.array
.ptr
[i
];
1147 struct lttng_event_field_value
*elem_field_val
;
1149 ret
= event_field_value_from_obj(elem_obj
,
1155 if (elem_field_val
) {
1156 ret
= lttng_event_field_value_array_append(
1157 *field_val
, elem_field_val
);
1159 ret
= lttng_event_field_value_array_append_unavailable(
1164 lttng_event_field_value_destroy(elem_field_val
);
1171 case MSGPACK_OBJECT_MAP
:
1174 * As of this version, the only valid map object is
1175 * for an enumeration value, for example:
1182 * - Carling Black Label
1184 const msgpack_object
*inner_obj
;
1187 inner_obj
= get_msgpack_map_obj(obj
, "type");
1189 ERR("Missing `type` entry in map object");
1193 if (inner_obj
->type
!= MSGPACK_OBJECT_STR
) {
1194 ERR("Map object's `type` entry is not a string: type = %s",
1195 msgpack_object_type_str(inner_obj
->type
));
1199 if (!msgpack_str_is_equal(inner_obj
, "enum")) {
1200 ERR("Map object's `type` entry: expecting `enum`");
1204 inner_obj
= get_msgpack_map_obj(obj
, "value");
1206 ERR("Missing `value` entry in map object");
1210 if (inner_obj
->type
== MSGPACK_OBJECT_POSITIVE_INTEGER
) {
1211 *field_val
= lttng_event_field_value_enum_uint_create(
1212 inner_obj
->via
.u64
);
1213 } else if (inner_obj
->type
== MSGPACK_OBJECT_NEGATIVE_INTEGER
) {
1214 *field_val
= lttng_event_field_value_enum_int_create(
1215 inner_obj
->via
.i64
);
1217 ERR("Map object's `value` entry is not an integer: type = %s",
1218 msgpack_object_type_str(inner_obj
->type
));
1226 inner_obj
= get_msgpack_map_obj(obj
, "labels");
1232 if (inner_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1233 ERR("Map object's `labels` entry is not an array: type = %s",
1234 msgpack_object_type_str(inner_obj
->type
));
1238 for (label_i
= 0; label_i
< inner_obj
->via
.array
.size
;
1241 const msgpack_object
*elem_obj
=
1242 &inner_obj
->via
.array
.ptr
[label_i
];
1244 if (elem_obj
->type
!= MSGPACK_OBJECT_STR
) {
1245 ERR("Map object's `labels` entry's type is not a string: type = %s",
1246 msgpack_object_type_str(elem_obj
->type
));
1250 iret
= lttng_event_field_value_enum_append_label_with_size(
1251 *field_val
, elem_obj
->via
.str
.ptr
,
1252 elem_obj
->via
.str
.size
);
1261 ERR("Unexpected object type: type = %s",
1262 msgpack_object_type_str(obj
->type
));
1273 lttng_event_field_value_destroy(*field_val
);
1281 static struct lttng_event_field_value
*event_field_value_from_capture_payload(
1282 const struct lttng_condition_event_rule_matches
*condition
,
1283 const char *capture_payload
,
1284 size_t capture_payload_size
)
1286 struct lttng_event_field_value
*ret
= NULL
;
1287 msgpack_unpacked unpacked
;
1288 msgpack_unpack_return unpack_return
;
1289 const msgpack_object
*root_obj
;
1290 const msgpack_object_array
*root_array_obj
;
1294 LTTNG_ASSERT(condition
);
1295 LTTNG_ASSERT(capture_payload
);
1297 /* Initialize value. */
1298 msgpack_unpacked_init(&unpacked
);
1301 unpack_return
= msgpack_unpack_next(&unpacked
, capture_payload
,
1302 capture_payload_size
, NULL
);
1303 if (unpack_return
!= MSGPACK_UNPACK_SUCCESS
) {
1304 ERR("msgpack_unpack_next() failed to decode the "
1305 "MessagePack-encoded capture payload: "
1306 "size = %zu, ret = %d",
1307 capture_payload_size
, unpack_return
);
1311 /* Get root array. */
1312 root_obj
= &unpacked
.data
;
1314 if (root_obj
->type
!= MSGPACK_OBJECT_ARRAY
) {
1315 ERR("Expecting an array as the root object: type = %s",
1316 msgpack_object_type_str(root_obj
->type
));
1320 root_array_obj
= &root_obj
->via
.array
;
1322 /* Create an empty root array event field value. */
1323 ret
= lttng_event_field_value_array_create();
1329 * For each capture descriptor in the condition object:
1331 * 1. Get its corresponding captured field value MessagePack
1334 * 2. Create a corresponding event field value.
1336 * 3. Append it to `ret` (the root array event field value).
1338 count
= lttng_dynamic_pointer_array_get_count(
1339 &condition
->capture_descriptors
);
1340 LTTNG_ASSERT(count
> 0);
1342 for (i
= 0; i
< count
; i
++) {
1343 const struct lttng_capture_descriptor
*capture_descriptor
=
1344 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1345 &condition
->parent
, i
);
1346 const msgpack_object
*elem_obj
;
1347 struct lttng_event_field_value
*elem_field_val
;
1350 LTTNG_ASSERT(capture_descriptor
);
1352 elem_obj
= &root_array_obj
->ptr
[i
];
1353 iret
= event_field_value_from_obj(elem_obj
,
1359 if (elem_field_val
) {
1360 iret
= lttng_event_field_value_array_append(ret
,
1363 iret
= lttng_event_field_value_array_append_unavailable(
1368 lttng_event_field_value_destroy(elem_field_val
);
1376 lttng_event_field_value_destroy(ret
);
1380 msgpack_unpacked_destroy(&unpacked
);
1385 struct lttng_evaluation
*lttng_evaluation_event_rule_matches_create(
1386 const struct lttng_condition_event_rule_matches
*condition
,
1387 const char *capture_payload
,
1388 size_t capture_payload_size
,
1389 bool decode_capture_payload
)
1391 struct lttng_evaluation_event_rule_matches
*hit
;
1392 struct lttng_evaluation
*evaluation
= NULL
;
1394 hit
= zmalloc(sizeof(struct lttng_evaluation_event_rule_matches
));
1399 lttng_dynamic_buffer_init(&hit
->capture_payload
);
1401 if (capture_payload
) {
1402 const int ret
= lttng_dynamic_buffer_append(
1403 &hit
->capture_payload
, capture_payload
,
1404 capture_payload_size
);
1406 ERR("Failed to initialize capture payload of event rule evaluation");
1410 if (decode_capture_payload
) {
1411 hit
->captured_values
=
1412 event_field_value_from_capture_payload(
1415 capture_payload_size
);
1416 if (!hit
->captured_values
) {
1417 ERR("Failed to decode the capture payload: size = %zu",
1418 capture_payload_size
);
1424 hit
->parent
.type
= LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
;
1425 hit
->parent
.serialize
= lttng_evaluation_event_rule_matches_serialize
;
1426 hit
->parent
.destroy
= lttng_evaluation_event_rule_matches_destroy
;
1428 evaluation
= &hit
->parent
;
1433 lttng_evaluation_event_rule_matches_destroy(&hit
->parent
);
1439 enum lttng_evaluation_event_rule_matches_status
1440 lttng_evaluation_event_rule_matches_get_captured_values(
1441 const struct lttng_evaluation
*evaluation
,
1442 const struct lttng_event_field_value
**field_val
)
1444 struct lttng_evaluation_event_rule_matches
*hit
;
1445 enum lttng_evaluation_event_rule_matches_status status
=
1446 LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_OK
;
1448 if (!evaluation
|| !is_event_rule_matches_evaluation(evaluation
) ||
1450 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_INVALID
;
1454 hit
= container_of(evaluation
,
1455 struct lttng_evaluation_event_rule_matches
, parent
);
1456 if (!hit
->captured_values
) {
1457 status
= LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_NONE
;
1461 *field_val
= hit
->captured_values
;
1468 enum lttng_error_code
1469 lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
1470 struct lttng_condition
*condition
)
1472 enum lttng_error_code ret
;
1473 enum lttng_condition_status status
;
1474 unsigned int capture_count
, i
;
1476 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1477 ret
= LTTNG_ERR_FATAL
;
1481 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1482 condition
, &capture_count
);
1483 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1484 ret
= LTTNG_ERR_FATAL
;
1488 for (i
= 0; i
< capture_count
; i
++) {
1489 struct lttng_capture_descriptor
*local_capture_desc
=
1490 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1493 if (local_capture_desc
== NULL
) {
1494 ret
= LTTNG_ERR_FATAL
;
1498 /* Generate the bytecode. */
1499 status
= lttng_event_expr_to_bytecode(
1500 local_capture_desc
->event_expression
,
1501 &local_capture_desc
->bytecode
);
1502 if (status
< 0 || local_capture_desc
->bytecode
== NULL
) {
1503 ret
= LTTNG_ERR_INVALID_CAPTURE_EXPRESSION
;
1508 /* Everything went better than expected */
1516 const struct lttng_bytecode
*
1517 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
1518 const struct lttng_condition
*condition
, unsigned int index
)
1520 const struct lttng_condition_event_rule_matches
1521 *event_rule_matches_cond
= container_of(condition
,
1522 const struct lttng_condition_event_rule_matches
,
1524 struct lttng_capture_descriptor
*desc
= NULL
;
1525 struct lttng_bytecode
*bytecode
= NULL
;
1527 enum lttng_condition_status status
;
1529 if (!condition
|| !IS_EVENT_RULE_MATCHES_CONDITION(condition
)) {
1533 status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
1535 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1539 if (index
>= count
) {
1543 desc
= lttng_dynamic_pointer_array_get_pointer(
1544 &event_rule_matches_cond
->capture_descriptors
, index
);
1549 bytecode
= desc
->bytecode
;