Rename lttng_condition_event_rule to lttng_condition_on_event
[lttng-tools.git] / src / common / conditions / event-rule.c
1 /*
2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/error.h>
10 #include <common/event-expr-to-bytecode.h>
11 #include <common/macros.h>
12 #include <inttypes.h>
13 #include <limits.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>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <vendor/msgpack/msgpack.h>
26
27 #define IS_ON_EVENT_CONDITION(condition) \
28 (lttng_condition_get_type(condition) == \
29 LTTNG_CONDITION_TYPE_ON_EVENT)
30
31 static bool is_on_event_evaluation(const struct lttng_evaluation *evaluation)
32 {
33 enum lttng_condition_type type = lttng_evaluation_get_type(evaluation);
34
35 return type == LTTNG_CONDITION_TYPE_ON_EVENT;
36 }
37
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);
48
49 static bool lttng_condition_on_event_validate(
50 const struct lttng_condition *condition)
51 {
52 bool valid = false;
53 struct lttng_condition_on_event *event_rule;
54
55 if (!condition) {
56 goto end;
57 }
58
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");
63 goto end;
64 }
65
66 valid = lttng_event_rule_validate(event_rule->rule);
67 end:
68 return valid;
69 }
70
71 static const char *msgpack_object_type_str(msgpack_object_type type)
72 {
73 const char *name;
74
75 switch (type) {
76 case MSGPACK_OBJECT_NIL:
77 name = "MSGPACK_OBJECT_NIL";
78 break;
79 case MSGPACK_OBJECT_BOOLEAN:
80 name = "MSGPACK_OBJECT_BOOLEAN";
81 break;
82 case MSGPACK_OBJECT_POSITIVE_INTEGER:
83 name = "MSGPACK_OBJECT_POSITIVE_INTEGER";
84 break;
85 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
86 name = "MSGPACK_OBJECT_NEGATIVE_INTEGER";
87 break;
88 case MSGPACK_OBJECT_FLOAT32:
89 name = "MSGPACK_OBJECT_FLOAT32";
90 break;
91 case MSGPACK_OBJECT_FLOAT:
92 /* Same value as MSGPACK_OBJECT_FLOAT64 */
93 name = "MSGPACK_OBJECT_FLOAT(64)";
94 break;
95 case MSGPACK_OBJECT_STR:
96 name = "MSGPACK_OBJECT_STR";
97 break;
98 case MSGPACK_OBJECT_ARRAY:
99 name = "MSGPACK_OBJECT_ARRAY";
100 break;
101 case MSGPACK_OBJECT_MAP:
102 name = "MSGPACK_OBJECT_MAP";
103 break;
104 case MSGPACK_OBJECT_BIN:
105 name = "MSGPACK_OBJECT_BIN";
106 break;
107 case MSGPACK_OBJECT_EXT:
108 name = "MSGPACK_OBJECT_EXT";
109 break;
110 default:
111 abort();
112 }
113
114 return name;
115 }
116
117 /*
118 * Serializes the C string `str` into `buf`.
119 *
120 * Encoding is the length of `str` plus one (for the null character),
121 * and then the string, including its null terminator.
122 */
123 static
124 int serialize_cstr(const char *str, struct lttng_dynamic_buffer *buf)
125 {
126 int ret;
127 const uint32_t len = strlen(str) + 1;
128
129 /* Serialize the length, including the null terminator. */
130 DBG("Serializing C string's length (including null terminator): "
131 "%" PRIu32, len);
132 ret = lttng_dynamic_buffer_append(buf, &len, sizeof(len));
133 if (ret) {
134 goto end;
135 }
136
137 /* Serialize the string. */
138 DBG("Serializing C string: '%s'", str);
139 ret = lttng_dynamic_buffer_append(buf, str, len);
140 if (ret) {
141 goto end;
142 }
143
144 end:
145 return ret;
146 }
147
148 /*
149 * Serializes the event expression `expr` into `buf`.
150 */
151 static
152 int serialize_event_expr(const struct lttng_event_expr *expr,
153 struct lttng_payload *payload)
154 {
155 const uint8_t type = expr->type;
156 int ret;
157
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));
161 if (ret) {
162 goto end;
163 }
164
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:
169 {
170 const struct lttng_event_expr_field *field_expr =
171 container_of(expr,
172 const struct lttng_event_expr_field,
173 parent);
174
175 /* Serialize the field name. */
176 DBG("Serializing field event expression's field name: '%s'",
177 field_expr->name);
178 ret = serialize_cstr(field_expr->name, &payload->buffer);
179 if (ret) {
180 goto end;
181 }
182
183 break;
184 }
185 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD:
186 {
187 const struct lttng_event_expr_app_specific_context_field *field_expr =
188 container_of(expr,
189 const struct lttng_event_expr_app_specific_context_field,
190 parent);
191
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);
197 if (ret) {
198 goto end;
199 }
200
201 /* Serialize the type name. */
202 DBG("Serializing app-specific context field event expression's "
203 "type name: '%s'",
204 field_expr->provider_name);
205 ret = serialize_cstr(field_expr->type_name, &payload->buffer);
206 if (ret) {
207 goto end;
208 }
209
210 break;
211 }
212 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT:
213 {
214 const struct lttng_event_expr_array_field_element *elem_expr =
215 container_of(expr,
216 const struct lttng_event_expr_array_field_element,
217 parent);
218 const uint32_t index = elem_expr->index;
219
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));
224 if (ret) {
225 goto end;
226 }
227
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);
232 if (ret) {
233 goto end;
234 }
235
236 break;
237 }
238 default:
239 break;
240 }
241
242 end:
243 return ret;
244 }
245
246 static
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)
250 {
251 const struct lttng_condition_on_event *on_event_cond =
252 container_of(condition,
253 const struct lttng_condition_on_event,
254 parent);
255 struct lttng_capture_descriptor *desc = NULL;
256 unsigned int count;
257 enum lttng_condition_status status;
258
259 if (!condition || !IS_ON_EVENT_CONDITION(condition)) {
260 goto end;
261 }
262
263 status = lttng_condition_on_event_get_capture_descriptor_count(
264 condition, &count);
265 if (status != LTTNG_CONDITION_STATUS_OK) {
266 goto end;
267 }
268
269 if (index >= count) {
270 goto end;
271 }
272
273 desc = lttng_dynamic_pointer_array_get_pointer(
274 &on_event_cond->capture_descriptors, index);
275 end:
276 return desc;
277 }
278
279 static int lttng_condition_on_event_serialize(
280 const struct lttng_condition *condition,
281 struct lttng_payload *payload)
282 {
283 int ret;
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;
288
289 if (!condition || !IS_ON_EVENT_CONDITION(condition)) {
290 ret = -1;
291 goto end;
292 }
293
294 DBG("Serializing on event condition");
295 on_event_condition = container_of(
296 condition, struct lttng_condition_on_event, parent);
297
298 DBG("Serializing on event condition's event rule");
299 ret = lttng_event_rule_serialize(on_event_condition->rule, payload);
300 if (ret) {
301 goto end;
302 }
303
304 status = lttng_condition_on_event_get_capture_descriptor_count(
305 condition, &capture_descr_count);
306 if (status != LTTNG_CONDITION_STATUS_OK) {
307 ret = -1;
308 goto end;
309 };
310
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));
315 if (ret) {
316 goto end;
317 }
318
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(
322 condition, i);
323
324 DBG("Serializing on event condition's capture descriptor %" PRIu32,
325 i);
326 ret = serialize_event_expr(desc->event_expression, payload);
327 if (ret) {
328 goto end;
329 }
330 }
331
332 end:
333 return ret;
334 }
335
336 static
337 bool capture_descriptors_are_equal(
338 const struct lttng_condition *condition_a,
339 const struct lttng_condition *condition_b)
340 {
341 bool is_equal = true;
342 unsigned int capture_descr_count_a;
343 unsigned int capture_descr_count_b;
344 size_t i;
345 enum lttng_condition_status status;
346
347 status = lttng_condition_on_event_get_capture_descriptor_count(
348 condition_a, &capture_descr_count_a);
349 if (status != LTTNG_CONDITION_STATUS_OK) {
350 goto not_equal;
351 }
352
353 status = lttng_condition_on_event_get_capture_descriptor_count(
354 condition_b, &capture_descr_count_b);
355 if (status != LTTNG_CONDITION_STATUS_OK) {
356 goto not_equal;
357 }
358
359 if (capture_descr_count_a != capture_descr_count_b) {
360 goto not_equal;
361 }
362
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(
366 condition_a,
367 i);
368 const struct lttng_event_expr *expr_b =
369 lttng_condition_on_event_get_capture_descriptor_at_index(
370 condition_b,
371 i);
372
373 if (!lttng_event_expr_is_equal(expr_a, expr_b)) {
374 goto not_equal;
375 }
376 }
377
378 goto end;
379
380 not_equal:
381 is_equal = false;
382
383 end:
384 return is_equal;
385 }
386
387 static bool lttng_condition_on_event_is_equal(
388 const struct lttng_condition *_a,
389 const struct lttng_condition *_b)
390 {
391 bool is_equal = false;
392 struct lttng_condition_on_event *a, *b;
393
394 a = container_of(_a, struct lttng_condition_on_event, parent);
395 b = container_of(_b, struct lttng_condition_on_event, parent);
396
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");
400 goto end;
401 }
402
403 is_equal = lttng_event_rule_is_equal(a->rule, b->rule);
404 if (!is_equal) {
405 goto end;
406 }
407
408 is_equal = capture_descriptors_are_equal(_a, _b);
409
410 end:
411 return is_equal;
412 }
413
414 static void lttng_condition_on_event_destroy(
415 struct lttng_condition *condition)
416 {
417 struct lttng_condition_on_event *on_event_condition;
418
419 on_event_condition = container_of(
420 condition, struct lttng_condition_on_event, parent);
421
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);
425 }
426
427 static
428 void destroy_capture_descriptor(void *ptr)
429 {
430 struct lttng_capture_descriptor *desc =
431 (struct lttng_capture_descriptor *) ptr;
432
433 lttng_event_expr_destroy(desc->event_expression);
434 free(desc->bytecode);
435 free(desc);
436 }
437
438 struct lttng_condition *lttng_condition_on_event_create(
439 struct lttng_event_rule *rule)
440 {
441 struct lttng_condition *parent = NULL;
442 struct lttng_condition_on_event *condition = NULL;
443
444 if (!rule) {
445 goto end;
446 }
447
448 condition = zmalloc(sizeof(struct lttng_condition_on_event));
449 if (!condition) {
450 return NULL;
451 }
452
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,
459
460 lttng_event_rule_get(rule);
461 condition->rule = rule;
462 rule = NULL;
463
464 lttng_dynamic_pointer_array_init(&condition->capture_descriptors,
465 destroy_capture_descriptor);
466
467 parent = &condition->parent;
468 end:
469 return parent;
470 }
471
472 static
473 uint64_t uint_from_buffer(const struct lttng_buffer_view *view, size_t size,
474 size_t *offset)
475 {
476 uint64_t ret;
477 const struct lttng_buffer_view uint_view =
478 lttng_buffer_view_from_view(view, *offset, size);
479
480 if (!lttng_buffer_view_is_valid(&uint_view)) {
481 ret = UINT64_C(-1);
482 goto end;
483 }
484
485 switch (size) {
486 case 1:
487 ret = (uint64_t) *uint_view.data;
488 break;
489 case sizeof(uint32_t):
490 {
491 uint32_t u32;
492
493 memcpy(&u32, uint_view.data, sizeof(u32));
494 ret = (uint64_t) u32;
495 break;
496 }
497 case sizeof(ret):
498 memcpy(&ret, uint_view.data, sizeof(ret));
499 break;
500 default:
501 abort();
502 }
503
504 *offset += size;
505
506 end:
507 return ret;
508 }
509
510 static
511 const char *str_from_buffer(const struct lttng_buffer_view *view,
512 size_t *offset)
513 {
514 uint64_t len;
515 const char *ret;
516
517 len = uint_from_buffer(view, sizeof(uint32_t), offset);
518 if (len == UINT64_C(-1)) {
519 goto error;
520 }
521
522 ret = &view->data[*offset];
523
524 if (!lttng_buffer_view_contains_string(view, ret, len)) {
525 goto error;
526 }
527
528 *offset += len;
529 goto end;
530
531 error:
532 ret = NULL;
533
534 end:
535 return ret;
536 }
537
538 static
539 struct lttng_event_expr *event_expr_from_payload(
540 struct lttng_payload_view *view, size_t *offset)
541 {
542 struct lttng_event_expr *expr = NULL;
543 const char *str;
544 uint64_t type;
545
546 type = uint_from_buffer(&view->buffer, sizeof(uint8_t), offset);
547 if (type == UINT64_C(-1)) {
548 goto error;
549 }
550
551 switch (type) {
552 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD:
553 str = str_from_buffer(&view->buffer, offset);
554 if (!str) {
555 goto error;
556 }
557
558 expr = lttng_event_expr_event_payload_field_create(str);
559 break;
560 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD:
561 str = str_from_buffer(&view->buffer, offset);
562 if (!str) {
563 goto error;
564 }
565
566 expr = lttng_event_expr_channel_context_field_create(str);
567 break;
568 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD:
569 {
570 const char *provider_name;
571 const char *type_name;
572
573 provider_name = str_from_buffer(&view->buffer, offset);
574 if (!provider_name) {
575 goto error;
576 }
577
578 type_name = str_from_buffer(&view->buffer, offset);
579 if (!type_name) {
580 goto error;
581 }
582
583 expr = lttng_event_expr_app_specific_context_field_create(
584 provider_name, type_name);
585 break;
586 }
587 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT:
588 {
589 struct lttng_event_expr *array_field_expr;
590 const uint64_t index = uint_from_buffer(
591 &view->buffer, sizeof(uint32_t), offset);
592
593 if (index == UINT64_C(-1)) {
594 goto error;
595 }
596
597 /* Array field expression is the encoded after this. */
598 array_field_expr = event_expr_from_payload(view, offset);
599 if (!array_field_expr) {
600 goto error;
601 }
602
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);
606 if (!expr) {
607 /* `array_field_expr` not moved: destroy it. */
608 lttng_event_expr_destroy(array_field_expr);
609 }
610
611 break;
612 }
613 default:
614 abort();
615 }
616
617 goto end;
618
619 error:
620 lttng_event_expr_destroy(expr);
621 expr = NULL;
622
623 end:
624 return expr;
625 }
626
627 LTTNG_HIDDEN
628 ssize_t lttng_condition_on_event_create_from_payload(
629 struct lttng_payload_view *view,
630 struct lttng_condition **_condition)
631 {
632 ssize_t consumed_length;
633 size_t offset = 0;
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;
638
639 if (!view || !_condition) {
640 goto error;
641 }
642
643 /* Struct lttng_event_rule. */
644 {
645 struct lttng_payload_view event_rule_view =
646 lttng_payload_view_from_view(view, offset, -1);
647
648 event_rule_length = lttng_event_rule_create_from_payload(
649 &event_rule_view, &event_rule);
650 }
651
652 if (event_rule_length < 0 || !event_rule) {
653 goto error;
654 }
655
656 /* Create condition (no capture descriptors yet) at this point */
657 condition = lttng_condition_on_event_create(event_rule);
658 if (!condition) {
659 goto error;
660 }
661
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)) {
667 goto error;
668 }
669
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(
674 view, &offset);
675
676 if (!expr) {
677 goto error;
678 }
679
680 /* Move ownership of `expr` to `condition`. */
681 status = lttng_condition_on_event_append_capture_descriptor(
682 condition, expr);
683 if (status != LTTNG_CONDITION_STATUS_OK) {
684 /* `expr` not moved: destroy it. */
685 lttng_event_expr_destroy(expr);
686 goto error;
687 }
688 }
689
690 consumed_length = (ssize_t) offset;
691 *_condition = condition;
692 condition = NULL;
693 goto end;
694
695 error:
696 consumed_length = -1;
697
698 end:
699 lttng_event_rule_put(event_rule);
700 lttng_condition_put(condition);
701 return consumed_length;
702 }
703
704 LTTNG_HIDDEN
705 enum lttng_condition_status lttng_condition_on_event_borrow_rule_mutable(
706 const struct lttng_condition *condition,
707 struct lttng_event_rule **rule)
708 {
709 struct lttng_condition_on_event *event_rule;
710 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
711
712 if (!condition || !IS_ON_EVENT_CONDITION(condition) || !rule) {
713 status = LTTNG_CONDITION_STATUS_INVALID;
714 goto end;
715 }
716
717 event_rule = container_of(
718 condition, struct lttng_condition_on_event, parent);
719 if (!event_rule->rule) {
720 status = LTTNG_CONDITION_STATUS_UNSET;
721 goto end;
722 }
723
724 *rule = event_rule->rule;
725 end:
726 return status;
727 }
728
729 enum lttng_condition_status lttng_condition_on_event_get_rule(
730 const struct lttng_condition *condition,
731 const struct lttng_event_rule **rule)
732 {
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);
737
738 *rule = mutable_rule;
739 return status;
740 }
741
742 enum lttng_condition_status
743 lttng_condition_on_event_append_capture_descriptor(
744 struct lttng_condition *condition,
745 struct lttng_event_expr *expr)
746 {
747 int ret;
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;
754
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;
759 goto end;
760 }
761
762 status = lttng_condition_on_event_get_rule(condition, &rule);
763 if (status != LTTNG_CONDITION_STATUS_OK) {
764 goto end;
765 }
766
767 switch(lttng_event_rule_get_type(rule)) {
768 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
769 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
770 /* Supported. */
771 status = LTTNG_CONDITION_STATUS_OK;
772 break;
773 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
774 status = LTTNG_CONDITION_STATUS_INVALID;
775 break;
776 default:
777 status = LTTNG_CONDITION_STATUS_UNSUPPORTED;
778 break;
779 }
780
781 if (status != LTTNG_CONDITION_STATUS_OK) {
782 goto end;
783 }
784
785 descriptor = malloc(sizeof(*descriptor));
786 if (descriptor == NULL) {
787 status = LTTNG_CONDITION_STATUS_ERROR;
788 goto end;
789 }
790
791 descriptor->event_expression = expr;
792 descriptor->bytecode = NULL;
793
794 ret = lttng_dynamic_pointer_array_add_pointer(
795 &event_rule_cond->capture_descriptors, descriptor);
796 if (ret) {
797 status = LTTNG_CONDITION_STATUS_ERROR;
798 goto end;
799 }
800
801 /* Ownership is transfered to the internal capture_descriptors array */
802 descriptor = NULL;
803 end:
804 free(descriptor);
805 return status;
806 }
807
808 enum lttng_condition_status
809 lttng_condition_on_event_get_capture_descriptor_count(
810 const struct lttng_condition *condition, unsigned int *count)
811 {
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,
816 parent);
817
818 if (!condition || !IS_ON_EVENT_CONDITION(condition) || !count) {
819 status = LTTNG_CONDITION_STATUS_INVALID;
820 goto end;
821 }
822
823 *count = lttng_dynamic_pointer_array_get_count(
824 &on_event_condition->capture_descriptors);
825
826 end:
827 return status;
828 }
829
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)
833 {
834 const struct lttng_event_expr *expr = NULL;
835 const struct lttng_capture_descriptor *desc = NULL;
836
837 desc = lttng_condition_on_event_get_internal_capture_descriptor_at_index(
838 condition, index);
839 if (desc == NULL) {
840 goto end;
841 }
842 expr = desc->event_expression;
843
844 end:
845 return expr;
846 }
847
848 LTTNG_HIDDEN
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)
853 {
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;
863
864 if (!_evaluation) {
865 ret = -1;
866 goto error;
867 }
868
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");
871 ret = -1;
872 goto error;
873 }
874
875 header = (typeof(header)) header_view.buffer.data;
876
877 /* Map the originating trigger's name. */
878 offset += sizeof(*header);
879 {
880 const struct lttng_payload_view current_view =
881 lttng_payload_view_from_view(view, offset,
882 header->trigger_name_length);
883
884 if (!lttng_payload_view_is_valid(&current_view)) {
885 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
886 ret = -1;
887 goto error;
888 }
889
890 trigger_name = current_view.buffer.data;
891 if (!lttng_buffer_view_contains_string(&current_view.buffer,
892 trigger_name, header->trigger_name_length)) {
893 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
894 ret = -1;
895 goto error;
896 }
897 }
898
899 offset += header->trigger_name_length;
900 {
901 const struct lttng_payload_view current_view =
902 lttng_payload_view_from_view(view, offset, -1);
903
904 if (current_view.buffer.size < sizeof(capture_payload_size)) {
905 ret = -1;
906 goto error;
907 }
908
909 memcpy(&capture_payload_size, current_view.buffer.data,
910 sizeof(capture_payload_size));
911 }
912 offset += sizeof(capture_payload_size);
913
914 if (capture_payload_size > 0) {
915 const struct lttng_payload_view current_view =
916 lttng_payload_view_from_view(view, offset, -1);
917
918 if (current_view.buffer.size < capture_payload_size) {
919 ret = -1;
920 goto error;
921 }
922
923 capture_payload = current_view.buffer.data;
924 }
925
926 evaluation = lttng_evaluation_on_event_create(condition, trigger_name,
927 capture_payload, capture_payload_size, true);
928 if (!evaluation) {
929 ret = -1;
930 goto error;
931 }
932
933 offset += capture_payload_size;
934 *_evaluation = evaluation;
935 evaluation = NULL;
936 ret = offset;
937
938 error:
939 lttng_evaluation_destroy(evaluation);
940 return ret;
941 }
942
943 static int lttng_evaluation_on_event_serialize(
944 const struct lttng_evaluation *evaluation,
945 struct lttng_payload *payload)
946 {
947 int ret = 0;
948 struct lttng_evaluation_on_event *hit;
949 struct lttng_evaluation_on_event_comm comm;
950 uint32_t capture_payload_size;
951
952 hit = container_of(
953 evaluation, struct lttng_evaluation_on_event, parent);
954
955 assert(hit->name);
956 comm.trigger_name_length = strlen(hit->name) + 1;
957
958 ret = lttng_dynamic_buffer_append(
959 &payload->buffer, &comm, sizeof(comm));
960 if (ret) {
961 goto end;
962 }
963
964 ret = lttng_dynamic_buffer_append(
965 &payload->buffer, hit->name, comm.trigger_name_length);
966 if (ret) {
967 goto end;
968 }
969
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));
973 if (ret) {
974 goto end;
975 }
976
977 ret = lttng_dynamic_buffer_append(&payload->buffer, hit->capture_payload.data,
978 hit->capture_payload.size);
979 if (ret) {
980 goto end;
981 }
982
983 end:
984 return ret;
985 }
986
987 static
988 bool msgpack_str_is_equal(const struct msgpack_object *obj, const char *str)
989 {
990 bool is_equal = true;
991
992 assert(obj->type == MSGPACK_OBJECT_STR);
993
994 if (obj->via.str.size != strlen(str)) {
995 is_equal = false;
996 goto end;
997 }
998
999 if (strncmp(obj->via.str.ptr, str, obj->via.str.size) != 0) {
1000 is_equal = false;
1001 goto end;
1002 }
1003
1004 end:
1005 return is_equal;
1006 }
1007
1008 static
1009 const msgpack_object *get_msgpack_map_obj(const struct msgpack_object *map_obj,
1010 const char *name)
1011 {
1012 const msgpack_object *ret = NULL;
1013 size_t i;
1014
1015 assert(map_obj->type == MSGPACK_OBJECT_MAP);
1016
1017 for (i = 0; i < map_obj->via.map.size; i++) {
1018 const struct msgpack_object_kv *kv = &map_obj->via.map.ptr[i];
1019
1020 assert(kv->key.type == MSGPACK_OBJECT_STR);
1021
1022 if (msgpack_str_is_equal(&kv->key, name)) {
1023 ret = &kv->val;
1024 goto end;
1025 }
1026 }
1027
1028 end:
1029 return ret;
1030 }
1031
1032 static void lttng_evaluation_on_event_destroy(
1033 struct lttng_evaluation *evaluation)
1034 {
1035 struct lttng_evaluation_on_event *hit;
1036
1037 hit = container_of(
1038 evaluation, struct lttng_evaluation_on_event, parent);
1039 free(hit->name);
1040 lttng_dynamic_buffer_reset(&hit->capture_payload);
1041 lttng_event_field_value_destroy(hit->captured_values);
1042 free(hit);
1043 }
1044
1045 static
1046 int event_field_value_from_obj(const msgpack_object *obj,
1047 struct lttng_event_field_value **field_val)
1048 {
1049 int ret = 0;
1050
1051 assert(obj);
1052 assert(field_val);
1053
1054 switch (obj->type) {
1055 case MSGPACK_OBJECT_NIL:
1056 /* Unavailable. */
1057 *field_val = NULL;
1058 goto end;
1059 case MSGPACK_OBJECT_POSITIVE_INTEGER:
1060 *field_val = lttng_event_field_value_uint_create(
1061 obj->via.u64);
1062 break;
1063 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
1064 *field_val = lttng_event_field_value_int_create(
1065 obj->via.i64);
1066 break;
1067 case MSGPACK_OBJECT_FLOAT32:
1068 case MSGPACK_OBJECT_FLOAT64:
1069 *field_val = lttng_event_field_value_real_create(
1070 obj->via.f64);
1071 break;
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);
1075 break;
1076 case MSGPACK_OBJECT_ARRAY:
1077 {
1078 size_t i;
1079
1080 *field_val = lttng_event_field_value_array_create();
1081 if (!*field_val) {
1082 goto error;
1083 }
1084
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;
1088
1089 ret = event_field_value_from_obj(elem_obj,
1090 &elem_field_val);
1091 if (ret) {
1092 goto error;
1093 }
1094
1095 if (elem_field_val) {
1096 ret = lttng_event_field_value_array_append(
1097 *field_val, elem_field_val);
1098 } else {
1099 ret = lttng_event_field_value_array_append_unavailable(
1100 *field_val);
1101 }
1102
1103 if (ret) {
1104 lttng_event_field_value_destroy(elem_field_val);
1105 goto error;
1106 }
1107 }
1108
1109 break;
1110 }
1111 case MSGPACK_OBJECT_MAP:
1112 {
1113 /*
1114 * As of this version, the only valid map object is
1115 * for an enumeration value, for example:
1116 *
1117 * type: enum
1118 * value: 177
1119 * labels:
1120 * - Labatt 50
1121 * - Molson Dry
1122 * - Carling Black Label
1123 */
1124 const msgpack_object *inner_obj;
1125 size_t label_i;
1126
1127 inner_obj = get_msgpack_map_obj(obj, "type");
1128 if (!inner_obj) {
1129 ERR("Missing `type` entry in map object");
1130 goto error;
1131 }
1132
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));
1136 goto error;
1137 }
1138
1139 if (!msgpack_str_is_equal(inner_obj, "enum")) {
1140 ERR("Map object's `type` entry: expecting `enum`");
1141 goto error;
1142 }
1143
1144 inner_obj = get_msgpack_map_obj(obj, "value");
1145 if (!inner_obj) {
1146 ERR("Missing `value` entry in map object");
1147 goto error;
1148 }
1149
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);
1156 } else {
1157 ERR("Map object's `value` entry is not an integer: type = %s",
1158 msgpack_object_type_str(inner_obj->type));
1159 goto error;
1160 }
1161
1162 if (!*field_val) {
1163 goto error;
1164 }
1165
1166 inner_obj = get_msgpack_map_obj(obj, "labels");
1167 if (!inner_obj) {
1168 /* No labels */
1169 goto end;
1170 }
1171
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));
1175 goto error;
1176 }
1177
1178 for (label_i = 0; label_i < inner_obj->via.array.size;
1179 label_i++) {
1180 int iret;
1181 const msgpack_object *elem_obj =
1182 &inner_obj->via.array.ptr[label_i];
1183
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));
1187 goto error;
1188 }
1189
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);
1193 if (iret) {
1194 goto error;
1195 }
1196 }
1197
1198 break;
1199 }
1200 default:
1201 ERR("Unexpected object type: type = %s",
1202 msgpack_object_type_str(obj->type));
1203 goto error;
1204 }
1205
1206 if (!*field_val) {
1207 goto error;
1208 }
1209
1210 goto end;
1211
1212 error:
1213 lttng_event_field_value_destroy(*field_val);
1214 *field_val = NULL;
1215 ret = -1;
1216
1217 end:
1218 return ret;
1219 }
1220
1221 static
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)
1225 {
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;
1231 size_t i;
1232 size_t count;
1233
1234 assert(condition);
1235 assert(capture_payload);
1236
1237 /* Initialize value. */
1238 msgpack_unpacked_init(&unpacked);
1239
1240 /* Decode. */
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);
1248 goto error;
1249 }
1250
1251 /* Get root array. */
1252 root_obj = &unpacked.data;
1253
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));
1257 goto error;
1258 }
1259
1260 root_array_obj = &root_obj->via.array;
1261
1262 /* Create an empty root array event field value. */
1263 ret = lttng_event_field_value_array_create();
1264 if (!ret) {
1265 goto error;
1266 }
1267
1268 /*
1269 * For each capture descriptor in the condition object:
1270 *
1271 * 1. Get its corresponding captured field value MessagePack
1272 * object.
1273 *
1274 * 2. Create a corresponding event field value.
1275 *
1276 * 3. Append it to `ret` (the root array event field value).
1277 */
1278 count = lttng_dynamic_pointer_array_get_count(
1279 &condition->capture_descriptors);
1280 assert(count > 0);
1281
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;
1288 int iret;
1289
1290 assert(capture_descriptor);
1291
1292 elem_obj = &root_array_obj->ptr[i];
1293 iret = event_field_value_from_obj(elem_obj,
1294 &elem_field_val);
1295 if (iret) {
1296 goto error;
1297 }
1298
1299 if (elem_field_val) {
1300 iret = lttng_event_field_value_array_append(ret,
1301 elem_field_val);
1302 } else {
1303 iret = lttng_event_field_value_array_append_unavailable(
1304 ret);
1305 }
1306
1307 if (iret) {
1308 lttng_event_field_value_destroy(elem_field_val);
1309 goto error;
1310 }
1311 }
1312
1313 goto end;
1314
1315 error:
1316 lttng_event_field_value_destroy(ret);
1317 ret = NULL;
1318
1319 end:
1320 msgpack_unpacked_destroy(&unpacked);
1321 return ret;
1322 }
1323
1324 LTTNG_HIDDEN
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)
1330 {
1331 struct lttng_evaluation_on_event *hit;
1332 struct lttng_evaluation *evaluation = NULL;
1333
1334 hit = zmalloc(sizeof(struct lttng_evaluation_on_event));
1335 if (!hit) {
1336 goto error;
1337 }
1338
1339 hit->name = strdup(trigger_name);
1340 if (!hit->name) {
1341 goto error;
1342 }
1343
1344 lttng_dynamic_buffer_init(&hit->capture_payload);
1345
1346 if (capture_payload) {
1347 const int ret = lttng_dynamic_buffer_append(
1348 &hit->capture_payload, capture_payload,
1349 capture_payload_size);
1350 if (ret) {
1351 ERR("Failed to initialize capture payload of event rule evaluation");
1352 goto error;
1353 }
1354
1355 if (decode_capture_payload) {
1356 hit->captured_values =
1357 event_field_value_from_capture_payload(
1358 condition,
1359 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);
1364 goto error;
1365 }
1366 }
1367 }
1368
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;
1372
1373 evaluation = &hit->parent;
1374 hit = NULL;
1375
1376 error:
1377 if (hit) {
1378 lttng_evaluation_on_event_destroy(&hit->parent);
1379 }
1380
1381 return evaluation;
1382 }
1383
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)
1387 {
1388 struct lttng_evaluation_on_event *hit;
1389 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
1390
1391 if (!evaluation || !is_on_event_evaluation(evaluation) ||
1392 !field_val) {
1393 status = LTTNG_EVALUATION_STATUS_INVALID;
1394 goto end;
1395 }
1396
1397 hit = container_of(evaluation, struct lttng_evaluation_on_event,
1398 parent);
1399 if (!hit->captured_values) {
1400 status = LTTNG_EVALUATION_STATUS_INVALID;
1401 goto end;
1402 }
1403
1404 *field_val = hit->captured_values;
1405
1406 end:
1407 return status;
1408 }
1409
1410 enum lttng_evaluation_status lttng_evaluation_on_event_get_trigger_name(
1411 const struct lttng_evaluation *evaluation, const char **name)
1412 {
1413 struct lttng_evaluation_on_event *hit;
1414 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
1415
1416 if (!evaluation || !is_on_event_evaluation(evaluation) || !name) {
1417 status = LTTNG_EVALUATION_STATUS_INVALID;
1418 goto end;
1419 }
1420
1421 hit = container_of(
1422 evaluation, struct lttng_evaluation_on_event, parent);
1423 *name = hit->name;
1424 end:
1425 return status;
1426 }
1427
1428 LTTNG_HIDDEN
1429 enum lttng_error_code
1430 lttng_condition_on_event_generate_capture_descriptor_bytecode(
1431 struct lttng_condition *condition)
1432 {
1433 enum lttng_error_code ret;
1434 enum lttng_condition_status status;
1435 unsigned int capture_count, i;
1436
1437 if (!condition || !IS_ON_EVENT_CONDITION(condition)) {
1438 ret = LTTNG_ERR_FATAL;
1439 goto end;
1440 }
1441
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;
1446 goto end;
1447 }
1448
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(
1452 condition, i);
1453
1454 if (local_capture_desc == NULL) {
1455 ret = LTTNG_ERR_FATAL;
1456 goto end;
1457 }
1458
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;
1465 goto end;
1466 }
1467 }
1468
1469 /* Everything went better than expected */
1470 ret = LTTNG_OK;
1471
1472 end:
1473 return ret;
1474 }
1475
1476 LTTNG_HIDDEN
1477 const struct lttng_bytecode *
1478 lttng_condition_on_event_get_capture_bytecode_at_index(
1479 const struct lttng_condition *condition, unsigned int index)
1480 {
1481 const struct lttng_condition_on_event *event_rule_cond =
1482 container_of(condition,
1483 const struct lttng_condition_on_event,
1484 parent);
1485 struct lttng_capture_descriptor *desc = NULL;
1486 struct lttng_bytecode *bytecode = NULL;
1487 unsigned int count;
1488 enum lttng_condition_status status;
1489
1490 if (!condition || !IS_ON_EVENT_CONDITION(condition)) {
1491 goto end;
1492 }
1493
1494 status = lttng_condition_on_event_get_capture_descriptor_count(
1495 condition, &count);
1496 if (status != LTTNG_CONDITION_STATUS_OK) {
1497 goto end;
1498 }
1499
1500 if (index >= count) {
1501 goto end;
1502 }
1503
1504 desc = lttng_dynamic_pointer_array_get_pointer(
1505 &event_rule_cond->capture_descriptors, index);
1506 if (desc == NULL) {
1507 goto end;
1508 }
1509
1510 bytecode = desc->bytecode;
1511 end:
1512 return bytecode;
1513 }
This page took 0.100131 seconds and 4 git commands to generate.