lttng: Capture is only supported by tracepoint and syscall event-rules
[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_EVENT_RULE_CONDITION(condition) \
28 (lttng_condition_get_type(condition) == \
29 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT)
30
31 static bool is_event_rule_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_EVENT_RULE_HIT;
36 }
37
38 static bool lttng_condition_event_rule_validate(
39 const struct lttng_condition *condition);
40 static int lttng_condition_event_rule_serialize(
41 const struct lttng_condition *condition,
42 struct lttng_payload *payload);
43 static bool lttng_condition_event_rule_is_equal(
44 const struct lttng_condition *_a,
45 const struct lttng_condition *_b);
46 static void lttng_condition_event_rule_destroy(
47 struct lttng_condition *condition);
48
49 static bool lttng_condition_event_rule_validate(
50 const struct lttng_condition *condition)
51 {
52 bool valid = false;
53 struct lttng_condition_event_rule *event_rule;
54
55 if (!condition) {
56 goto end;
57 }
58
59 event_rule = container_of(
60 condition, struct lttng_condition_event_rule, parent);
61 if (!event_rule->rule) {
62 ERR("Invalid event rule 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_event_rule_get_internal_capture_descriptor_at_index(
249 const struct lttng_condition *condition, unsigned int index)
250 {
251 const struct lttng_condition_event_rule *event_rule_cond =
252 container_of(condition,
253 const struct lttng_condition_event_rule,
254 parent);
255 struct lttng_capture_descriptor *desc = NULL;
256 unsigned int count;
257 enum lttng_condition_status status;
258
259 if (!condition || !IS_EVENT_RULE_CONDITION(condition)) {
260 goto end;
261 }
262
263 status = lttng_condition_event_rule_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 &event_rule_cond->capture_descriptors, index);
275 end:
276 return desc;
277 }
278
279 static int lttng_condition_event_rule_serialize(
280 const struct lttng_condition *condition,
281 struct lttng_payload *payload)
282 {
283 int ret;
284 struct lttng_condition_event_rule *event_rule;
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_EVENT_RULE_CONDITION(condition)) {
290 ret = -1;
291 goto end;
292 }
293
294 DBG("Serializing event rule condition");
295 event_rule = container_of(
296 condition, struct lttng_condition_event_rule, parent);
297
298 DBG("Serializing event rule condition's event rule");
299 ret = lttng_event_rule_serialize(event_rule->rule, payload);
300 if (ret) {
301 goto end;
302 }
303
304 status = lttng_condition_event_rule_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 event rule 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_event_rule_get_internal_capture_descriptor_at_index(
322 condition, i);
323
324 DBG("Serializing event rule 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_event_rule_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_event_rule_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_event_rule_get_capture_descriptor_at_index(
366 condition_a,
367 i);
368 const struct lttng_event_expr *expr_b =
369 lttng_condition_event_rule_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_event_rule_is_equal(
388 const struct lttng_condition *_a,
389 const struct lttng_condition *_b)
390 {
391 bool is_equal = false;
392 struct lttng_condition_event_rule *a, *b;
393
394 a = container_of(_a, struct lttng_condition_event_rule, parent);
395 b = container_of(_b, struct lttng_condition_event_rule, 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_event_rule_destroy(
415 struct lttng_condition *condition)
416 {
417 struct lttng_condition_event_rule *event_rule;
418
419 event_rule = container_of(
420 condition, struct lttng_condition_event_rule, parent);
421
422 lttng_event_rule_put(event_rule->rule);
423 lttng_dynamic_pointer_array_reset(&event_rule->capture_descriptors);
424 free(event_rule);
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_event_rule_create(
439 struct lttng_event_rule *rule)
440 {
441 struct lttng_condition *parent = NULL;
442 struct lttng_condition_event_rule *condition = NULL;
443
444 if (!rule) {
445 goto end;
446 }
447
448 condition = zmalloc(sizeof(struct lttng_condition_event_rule));
449 if (!condition) {
450 return NULL;
451 }
452
453 lttng_condition_init(&condition->parent,
454 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
455 condition->parent.validate = lttng_condition_event_rule_validate,
456 condition->parent.serialize = lttng_condition_event_rule_serialize,
457 condition->parent.equal = lttng_condition_event_rule_is_equal,
458 condition->parent.destroy = lttng_condition_event_rule_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_event_rule_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_event_rule_create(event_rule);
658 if (!condition) {
659 goto error;
660 }
661
662
663 /* Capture descriptor count. */
664 assert(event_rule_length >= 0);
665 offset += (size_t) event_rule_length;
666 capture_descr_count = uint_from_buffer(&view->buffer, sizeof(uint32_t), &offset);
667 if (capture_descr_count == UINT32_C(-1)) {
668 goto error;
669 }
670
671 /* Capture descriptors. */
672 for (i = 0; i < capture_descr_count; i++) {
673 enum lttng_condition_status status;
674 struct lttng_event_expr *expr = event_expr_from_payload(
675 view, &offset);
676
677 if (!expr) {
678 goto error;
679 }
680
681 /* Move ownership of `expr` to `condition`. */
682 status = lttng_condition_event_rule_append_capture_descriptor(
683 condition, expr);
684 if (status != LTTNG_CONDITION_STATUS_OK) {
685 /* `expr` not moved: destroy it. */
686 lttng_event_expr_destroy(expr);
687 goto error;
688 }
689 }
690
691 consumed_length = (ssize_t) offset;
692 *_condition = condition;
693 condition = NULL;
694 goto end;
695
696 error:
697 consumed_length = -1;
698
699 end:
700 lttng_event_rule_put(event_rule);
701 lttng_condition_put(condition);
702 return consumed_length;
703 }
704
705 LTTNG_HIDDEN
706 enum lttng_condition_status lttng_condition_event_rule_borrow_rule_mutable(
707 const struct lttng_condition *condition,
708 struct lttng_event_rule **rule)
709 {
710 struct lttng_condition_event_rule *event_rule;
711 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
712
713 if (!condition || !IS_EVENT_RULE_CONDITION(condition) || !rule) {
714 status = LTTNG_CONDITION_STATUS_INVALID;
715 goto end;
716 }
717
718 event_rule = container_of(
719 condition, struct lttng_condition_event_rule, parent);
720 if (!event_rule->rule) {
721 status = LTTNG_CONDITION_STATUS_UNSET;
722 goto end;
723 }
724
725 *rule = event_rule->rule;
726 end:
727 return status;
728 }
729
730 enum lttng_condition_status lttng_condition_event_rule_get_rule(
731 const struct lttng_condition *condition,
732 const struct lttng_event_rule **rule)
733 {
734 struct lttng_event_rule *mutable_rule = NULL;
735 const enum lttng_condition_status status =
736 lttng_condition_event_rule_borrow_rule_mutable(
737 condition, &mutable_rule);
738
739 *rule = mutable_rule;
740 return status;
741 }
742
743 enum lttng_condition_status
744 lttng_condition_event_rule_append_capture_descriptor(
745 struct lttng_condition *condition,
746 struct lttng_event_expr *expr)
747 {
748 int ret;
749 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
750 struct lttng_condition_event_rule *event_rule_cond =
751 container_of(condition,
752 struct lttng_condition_event_rule, parent);
753 struct lttng_capture_descriptor *descriptor = NULL;
754 const struct lttng_event_rule *rule = NULL;
755
756 /* Only accept l-values. */
757 if (!condition || !IS_EVENT_RULE_CONDITION(condition) || !expr ||
758 !lttng_event_expr_is_lvalue(expr)) {
759 status = LTTNG_CONDITION_STATUS_INVALID;
760 goto end;
761 }
762
763 status = lttng_condition_event_rule_get_rule(condition, &rule);
764 if (status != LTTNG_CONDITION_STATUS_OK) {
765 goto end;
766 }
767
768 switch(lttng_event_rule_get_type(rule)) {
769 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
770 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
771 /* Supported. */
772 status = LTTNG_CONDITION_STATUS_OK;
773 break;
774 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
775 status = LTTNG_CONDITION_STATUS_INVALID;
776 break;
777 default:
778 status = LTTNG_CONDITION_STATUS_UNSUPPORTED;
779 break;
780 }
781
782 if (status != LTTNG_CONDITION_STATUS_OK) {
783 goto end;
784 }
785
786 descriptor = malloc(sizeof(*descriptor));
787 if (descriptor == NULL) {
788 status = LTTNG_CONDITION_STATUS_ERROR;
789 goto end;
790 }
791
792 descriptor->event_expression = expr;
793 descriptor->bytecode = NULL;
794
795 ret = lttng_dynamic_pointer_array_add_pointer(
796 &event_rule_cond->capture_descriptors, descriptor);
797 if (ret) {
798 status = LTTNG_CONDITION_STATUS_ERROR;
799 goto end;
800 }
801
802 /* Ownership is transfered to the internal capture_descriptors array */
803 descriptor = NULL;
804 end:
805 free(descriptor);
806 return status;
807 }
808
809 enum lttng_condition_status
810 lttng_condition_event_rule_get_capture_descriptor_count(
811 const struct lttng_condition *condition, unsigned int *count)
812 {
813 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
814 const struct lttng_condition_event_rule *event_rule_cond =
815 container_of(condition,
816 const struct lttng_condition_event_rule,
817 parent);
818
819 if (!condition || !IS_EVENT_RULE_CONDITION(condition) || !count) {
820 status = LTTNG_CONDITION_STATUS_INVALID;
821 goto end;
822 }
823
824 *count = lttng_dynamic_pointer_array_get_count(
825 &event_rule_cond->capture_descriptors);
826
827 end:
828 return status;
829 }
830
831 const struct lttng_event_expr *
832 lttng_condition_event_rule_get_capture_descriptor_at_index(
833 const struct lttng_condition *condition, unsigned int index)
834 {
835 const struct lttng_event_expr *expr = NULL;
836 const struct lttng_capture_descriptor *desc = NULL;
837
838 desc = lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
839 condition, index);
840 if (desc == NULL) {
841 goto end;
842 }
843 expr = desc->event_expression;
844
845 end:
846 return expr;
847 }
848
849 LTTNG_HIDDEN
850 ssize_t lttng_evaluation_event_rule_create_from_payload(
851 const struct lttng_condition_event_rule *condition,
852 struct lttng_payload_view *view,
853 struct lttng_evaluation **_evaluation)
854 {
855 ssize_t ret, offset = 0;
856 const char *trigger_name;
857 struct lttng_evaluation *evaluation = NULL;
858 const struct lttng_evaluation_event_rule_comm *header;
859 const struct lttng_payload_view header_view =
860 lttng_payload_view_from_view(
861 view, 0, sizeof(*header));
862 uint32_t capture_payload_size;
863 const char *capture_payload = NULL;
864
865 if (!_evaluation) {
866 ret = -1;
867 goto error;
868 }
869
870 if (!lttng_payload_view_is_valid(&header_view)) {
871 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header");
872 ret = -1;
873 goto error;
874 }
875
876 header = (typeof(header)) header_view.buffer.data;
877
878 /* Map the originating trigger's name. */
879 offset += sizeof(*header);
880 {
881 const struct lttng_payload_view current_view =
882 lttng_payload_view_from_view(view, offset,
883 header->trigger_name_length);
884
885 if (!lttng_payload_view_is_valid(&current_view)) {
886 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
887 ret = -1;
888 goto error;
889 }
890
891 trigger_name = current_view.buffer.data;
892 if (!lttng_buffer_view_contains_string(&current_view.buffer,
893 trigger_name, header->trigger_name_length)) {
894 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
895 ret = -1;
896 goto error;
897 }
898 }
899
900 offset += header->trigger_name_length;
901 {
902 const struct lttng_payload_view current_view =
903 lttng_payload_view_from_view(view, offset, -1);
904
905 if (current_view.buffer.size < sizeof(capture_payload_size)) {
906 ret = -1;
907 goto error;
908 }
909
910 memcpy(&capture_payload_size, current_view.buffer.data,
911 sizeof(capture_payload_size));
912 }
913 offset += sizeof(capture_payload_size);
914
915 if (capture_payload_size > 0) {
916 const struct lttng_payload_view current_view =
917 lttng_payload_view_from_view(view, offset, -1);
918
919 if (current_view.buffer.size < capture_payload_size) {
920 ret = -1;
921 goto error;
922 }
923
924 capture_payload = current_view.buffer.data;
925 }
926
927 evaluation = lttng_evaluation_event_rule_create(condition, trigger_name,
928 capture_payload, capture_payload_size, true);
929 if (!evaluation) {
930 ret = -1;
931 goto error;
932 }
933
934 offset += capture_payload_size;
935 *_evaluation = evaluation;
936 evaluation = NULL;
937 ret = offset;
938
939 error:
940 lttng_evaluation_destroy(evaluation);
941 return ret;
942 }
943
944 static int lttng_evaluation_event_rule_serialize(
945 const struct lttng_evaluation *evaluation,
946 struct lttng_payload *payload)
947 {
948 int ret = 0;
949 struct lttng_evaluation_event_rule *hit;
950 struct lttng_evaluation_event_rule_comm comm;
951 uint32_t capture_payload_size;
952
953 hit = container_of(
954 evaluation, struct lttng_evaluation_event_rule, parent);
955
956 assert(hit->name);
957 comm.trigger_name_length = strlen(hit->name) + 1;
958
959 ret = lttng_dynamic_buffer_append(
960 &payload->buffer, &comm, sizeof(comm));
961 if (ret) {
962 goto end;
963 }
964
965 ret = lttng_dynamic_buffer_append(
966 &payload->buffer, hit->name, comm.trigger_name_length);
967 if (ret) {
968 goto end;
969 }
970
971 capture_payload_size = (uint32_t) hit->capture_payload.size;
972 ret = lttng_dynamic_buffer_append(&payload->buffer, &capture_payload_size,
973 sizeof(capture_payload_size));
974 if (ret) {
975 goto end;
976 }
977
978 ret = lttng_dynamic_buffer_append(&payload->buffer, hit->capture_payload.data,
979 hit->capture_payload.size);
980 if (ret) {
981 goto end;
982 }
983
984 end:
985 return ret;
986 }
987
988 static
989 bool msgpack_str_is_equal(const struct msgpack_object *obj, const char *str)
990 {
991 bool is_equal = true;
992
993 assert(obj->type == MSGPACK_OBJECT_STR);
994
995 if (obj->via.str.size != strlen(str)) {
996 is_equal = false;
997 goto end;
998 }
999
1000 if (strncmp(obj->via.str.ptr, str, obj->via.str.size) != 0) {
1001 is_equal = false;
1002 goto end;
1003 }
1004
1005 end:
1006 return is_equal;
1007 }
1008
1009 static
1010 const msgpack_object *get_msgpack_map_obj(const struct msgpack_object *map_obj,
1011 const char *name)
1012 {
1013 const msgpack_object *ret = NULL;
1014 size_t i;
1015
1016 assert(map_obj->type == MSGPACK_OBJECT_MAP);
1017
1018 for (i = 0; i < map_obj->via.map.size; i++) {
1019 const struct msgpack_object_kv *kv = &map_obj->via.map.ptr[i];
1020
1021 assert(kv->key.type == MSGPACK_OBJECT_STR);
1022
1023 if (msgpack_str_is_equal(&kv->key, name)) {
1024 ret = &kv->val;
1025 goto end;
1026 }
1027 }
1028
1029 end:
1030 return ret;
1031 }
1032
1033 static void lttng_evaluation_event_rule_destroy(
1034 struct lttng_evaluation *evaluation)
1035 {
1036 struct lttng_evaluation_event_rule *hit;
1037
1038 hit = container_of(
1039 evaluation, struct lttng_evaluation_event_rule, parent);
1040 free(hit->name);
1041 lttng_dynamic_buffer_reset(&hit->capture_payload);
1042 lttng_event_field_value_destroy(hit->captured_values);
1043 free(hit);
1044 }
1045
1046 static
1047 int event_field_value_from_obj(const msgpack_object *obj,
1048 struct lttng_event_field_value **field_val)
1049 {
1050 int ret = 0;
1051
1052 assert(obj);
1053 assert(field_val);
1054
1055 switch (obj->type) {
1056 case MSGPACK_OBJECT_NIL:
1057 /* Unavailable. */
1058 *field_val = NULL;
1059 goto end;
1060 case MSGPACK_OBJECT_POSITIVE_INTEGER:
1061 *field_val = lttng_event_field_value_uint_create(
1062 obj->via.u64);
1063 break;
1064 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
1065 *field_val = lttng_event_field_value_int_create(
1066 obj->via.i64);
1067 break;
1068 case MSGPACK_OBJECT_FLOAT32:
1069 case MSGPACK_OBJECT_FLOAT64:
1070 *field_val = lttng_event_field_value_real_create(
1071 obj->via.f64);
1072 break;
1073 case MSGPACK_OBJECT_STR:
1074 *field_val = lttng_event_field_value_string_create_with_size(
1075 obj->via.str.ptr, obj->via.str.size);
1076 break;
1077 case MSGPACK_OBJECT_ARRAY:
1078 {
1079 size_t i;
1080
1081 *field_val = lttng_event_field_value_array_create();
1082 if (!*field_val) {
1083 goto error;
1084 }
1085
1086 for (i = 0; i < obj->via.array.size; i++) {
1087 const msgpack_object *elem_obj = &obj->via.array.ptr[i];
1088 struct lttng_event_field_value *elem_field_val;
1089
1090 ret = event_field_value_from_obj(elem_obj,
1091 &elem_field_val);
1092 if (ret) {
1093 goto error;
1094 }
1095
1096 if (elem_field_val) {
1097 ret = lttng_event_field_value_array_append(
1098 *field_val, elem_field_val);
1099 } else {
1100 ret = lttng_event_field_value_array_append_unavailable(
1101 *field_val);
1102 }
1103
1104 if (ret) {
1105 lttng_event_field_value_destroy(elem_field_val);
1106 goto error;
1107 }
1108 }
1109
1110 break;
1111 }
1112 case MSGPACK_OBJECT_MAP:
1113 {
1114 /*
1115 * As of this version, the only valid map object is
1116 * for an enumeration value, for example:
1117 *
1118 * type: enum
1119 * value: 177
1120 * labels:
1121 * - Labatt 50
1122 * - Molson Dry
1123 * - Carling Black Label
1124 */
1125 const msgpack_object *inner_obj;
1126 size_t label_i;
1127
1128 inner_obj = get_msgpack_map_obj(obj, "type");
1129 if (!inner_obj) {
1130 ERR("Missing `type` entry in map object");
1131 goto error;
1132 }
1133
1134 if (inner_obj->type != MSGPACK_OBJECT_STR) {
1135 ERR("Map object's `type` entry is not a string: type = %s",
1136 msgpack_object_type_str(inner_obj->type));
1137 goto error;
1138 }
1139
1140 if (!msgpack_str_is_equal(inner_obj, "enum")) {
1141 ERR("Map object's `type` entry: expecting `enum`");
1142 goto error;
1143 }
1144
1145 inner_obj = get_msgpack_map_obj(obj, "value");
1146 if (!inner_obj) {
1147 ERR("Missing `value` entry in map object");
1148 goto error;
1149 }
1150
1151 if (inner_obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
1152 *field_val = lttng_event_field_value_enum_uint_create(
1153 inner_obj->via.u64);
1154 } else if (inner_obj->type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
1155 *field_val = lttng_event_field_value_enum_int_create(
1156 inner_obj->via.i64);
1157 } else {
1158 ERR("Map object's `value` entry is not an integer: type = %s",
1159 msgpack_object_type_str(inner_obj->type));
1160 goto error;
1161 }
1162
1163 if (!*field_val) {
1164 goto error;
1165 }
1166
1167 inner_obj = get_msgpack_map_obj(obj, "labels");
1168 if (!inner_obj) {
1169 /* No labels */
1170 goto end;
1171 }
1172
1173 if (inner_obj->type != MSGPACK_OBJECT_ARRAY) {
1174 ERR("Map object's `labels` entry is not an array: type = %s",
1175 msgpack_object_type_str(inner_obj->type));
1176 goto error;
1177 }
1178
1179 for (label_i = 0; label_i < inner_obj->via.array.size;
1180 label_i++) {
1181 int iret;
1182 const msgpack_object *elem_obj =
1183 &inner_obj->via.array.ptr[label_i];
1184
1185 if (elem_obj->type != MSGPACK_OBJECT_STR) {
1186 ERR("Map object's `labels` entry's type is not a string: type = %s",
1187 msgpack_object_type_str(elem_obj->type));
1188 goto error;
1189 }
1190
1191 iret = lttng_event_field_value_enum_append_label_with_size(
1192 *field_val, elem_obj->via.str.ptr,
1193 elem_obj->via.str.size);
1194 if (iret) {
1195 goto error;
1196 }
1197 }
1198
1199 break;
1200 }
1201 default:
1202 ERR("Unexpected object type: type = %s",
1203 msgpack_object_type_str(obj->type));
1204 goto error;
1205 }
1206
1207 if (!*field_val) {
1208 goto error;
1209 }
1210
1211 goto end;
1212
1213 error:
1214 lttng_event_field_value_destroy(*field_val);
1215 *field_val = NULL;
1216 ret = -1;
1217
1218 end:
1219 return ret;
1220 }
1221
1222 static
1223 struct lttng_event_field_value *event_field_value_from_capture_payload(
1224 const struct lttng_condition_event_rule *condition,
1225 const char *capture_payload, size_t capture_payload_size)
1226 {
1227 struct lttng_event_field_value *ret = NULL;
1228 msgpack_unpacked unpacked;
1229 msgpack_unpack_return unpack_return;
1230 const msgpack_object *root_obj;
1231 const msgpack_object_array *root_array_obj;
1232 size_t i;
1233 size_t count;
1234
1235 assert(condition);
1236 assert(capture_payload);
1237
1238 /* Initialize value. */
1239 msgpack_unpacked_init(&unpacked);
1240
1241 /* Decode. */
1242 unpack_return = msgpack_unpack_next(&unpacked, capture_payload,
1243 capture_payload_size, NULL);
1244 if (unpack_return != MSGPACK_UNPACK_SUCCESS) {
1245 ERR("msgpack_unpack_next() failed to decode the "
1246 "MessagePack-encoded capture payload: "
1247 "size = %zu, ret = %d",
1248 capture_payload_size, unpack_return);
1249 goto error;
1250 }
1251
1252 /* Get root array. */
1253 root_obj = &unpacked.data;
1254
1255 if (root_obj->type != MSGPACK_OBJECT_ARRAY) {
1256 ERR("Expecting an array as the root object: type = %s",
1257 msgpack_object_type_str(root_obj->type));
1258 goto error;
1259 }
1260
1261 root_array_obj = &root_obj->via.array;
1262
1263 /* Create an empty root array event field value. */
1264 ret = lttng_event_field_value_array_create();
1265 if (!ret) {
1266 goto error;
1267 }
1268
1269 /*
1270 * For each capture descriptor in the condition object:
1271 *
1272 * 1. Get its corresponding captured field value MessagePack
1273 * object.
1274 *
1275 * 2. Create a corresponding event field value.
1276 *
1277 * 3. Append it to `ret` (the root array event field value).
1278 */
1279 count = lttng_dynamic_pointer_array_get_count(
1280 &condition->capture_descriptors);
1281 assert(count > 0);
1282
1283 for (i = 0; i < count; i++) {
1284 const struct lttng_capture_descriptor *capture_descriptor =
1285 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1286 &condition->parent, i);
1287 const msgpack_object *elem_obj;
1288 struct lttng_event_field_value *elem_field_val;
1289 int iret;
1290
1291 assert(capture_descriptor);
1292
1293 elem_obj = &root_array_obj->ptr[i];
1294 iret = event_field_value_from_obj(elem_obj,
1295 &elem_field_val);
1296 if (iret) {
1297 goto error;
1298 }
1299
1300 if (elem_field_val) {
1301 iret = lttng_event_field_value_array_append(ret,
1302 elem_field_val);
1303 } else {
1304 iret = lttng_event_field_value_array_append_unavailable(
1305 ret);
1306 }
1307
1308 if (iret) {
1309 lttng_event_field_value_destroy(elem_field_val);
1310 goto error;
1311 }
1312 }
1313
1314 goto end;
1315
1316 error:
1317 lttng_event_field_value_destroy(ret);
1318 ret = NULL;
1319
1320 end:
1321 msgpack_unpacked_destroy(&unpacked);
1322 return ret;
1323 }
1324
1325 LTTNG_HIDDEN
1326 struct lttng_evaluation *lttng_evaluation_event_rule_create(
1327 const struct lttng_condition_event_rule *condition,
1328 const char *trigger_name,
1329 const char *capture_payload, size_t capture_payload_size,
1330 bool decode_capture_payload)
1331 {
1332 struct lttng_evaluation_event_rule *hit;
1333 struct lttng_evaluation *evaluation = NULL;
1334
1335 hit = zmalloc(sizeof(struct lttng_evaluation_event_rule));
1336 if (!hit) {
1337 goto error;
1338 }
1339
1340 hit->name = strdup(trigger_name);
1341 if (!hit->name) {
1342 goto error;
1343 }
1344
1345 lttng_dynamic_buffer_init(&hit->capture_payload);
1346
1347 if (capture_payload) {
1348 const int ret = lttng_dynamic_buffer_append(
1349 &hit->capture_payload, capture_payload,
1350 capture_payload_size);
1351 if (ret) {
1352 ERR("Failed to initialize capture payload of event rule evaluation");
1353 goto error;
1354 }
1355
1356 if (decode_capture_payload) {
1357 hit->captured_values =
1358 event_field_value_from_capture_payload(
1359 condition,
1360 capture_payload,
1361 capture_payload_size);
1362 if (!hit->captured_values) {
1363 ERR("Failed to decode the capture payload: size = %zu",
1364 capture_payload_size);
1365 goto error;
1366 }
1367 }
1368 }
1369
1370 hit->parent.type = LTTNG_CONDITION_TYPE_EVENT_RULE_HIT;
1371 hit->parent.serialize = lttng_evaluation_event_rule_serialize;
1372 hit->parent.destroy = lttng_evaluation_event_rule_destroy;
1373
1374 evaluation = &hit->parent;
1375 hit = NULL;
1376
1377 error:
1378 if (hit) {
1379 lttng_evaluation_event_rule_destroy(&hit->parent);
1380 }
1381
1382 return evaluation;
1383 }
1384
1385 enum lttng_evaluation_status lttng_evaluation_event_rule_get_captured_values(
1386 const struct lttng_evaluation *evaluation,
1387 const struct lttng_event_field_value **field_val)
1388 {
1389 struct lttng_evaluation_event_rule *hit;
1390 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
1391
1392 if (!evaluation || !is_event_rule_evaluation(evaluation) ||
1393 !field_val) {
1394 status = LTTNG_EVALUATION_STATUS_INVALID;
1395 goto end;
1396 }
1397
1398 hit = container_of(evaluation, struct lttng_evaluation_event_rule,
1399 parent);
1400 if (!hit->captured_values) {
1401 status = LTTNG_EVALUATION_STATUS_INVALID;
1402 goto end;
1403 }
1404
1405 *field_val = hit->captured_values;
1406
1407 end:
1408 return status;
1409 }
1410
1411 enum lttng_evaluation_status lttng_evaluation_event_rule_get_trigger_name(
1412 const struct lttng_evaluation *evaluation, const char **name)
1413 {
1414 struct lttng_evaluation_event_rule *hit;
1415 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
1416
1417 if (!evaluation || !is_event_rule_evaluation(evaluation) || !name) {
1418 status = LTTNG_EVALUATION_STATUS_INVALID;
1419 goto end;
1420 }
1421
1422 hit = container_of(
1423 evaluation, struct lttng_evaluation_event_rule, parent);
1424 *name = hit->name;
1425 end:
1426 return status;
1427 }
1428
1429 LTTNG_HIDDEN
1430 enum lttng_error_code
1431 lttng_condition_event_rule_generate_capture_descriptor_bytecode(
1432 struct lttng_condition *condition)
1433 {
1434 enum lttng_error_code ret;
1435 enum lttng_condition_status status;
1436 unsigned int capture_count, i;
1437
1438 if (!condition || !IS_EVENT_RULE_CONDITION(condition)) {
1439 ret = LTTNG_ERR_FATAL;
1440 goto end;
1441 }
1442
1443 status = lttng_condition_event_rule_get_capture_descriptor_count(
1444 condition, &capture_count);
1445 if (status != LTTNG_CONDITION_STATUS_OK) {
1446 ret = LTTNG_ERR_FATAL;
1447 goto end;
1448 }
1449
1450 for (i = 0; i < capture_count; i++) {
1451 struct lttng_capture_descriptor *local_capture_desc =
1452 lttng_condition_event_rule_get_internal_capture_descriptor_at_index(
1453 condition, i);
1454
1455 if (local_capture_desc == NULL) {
1456 ret = LTTNG_ERR_FATAL;
1457 goto end;
1458 }
1459
1460 /* Generate the bytecode. */
1461 status = lttng_event_expr_to_bytecode(
1462 local_capture_desc->event_expression,
1463 &local_capture_desc->bytecode);
1464 if (status < 0 || local_capture_desc->bytecode == NULL) {
1465 ret = LTTNG_ERR_INVALID_CAPTURE_EXPRESSION;
1466 goto end;
1467 }
1468 }
1469
1470 /* Everything went better than expected */
1471 ret = LTTNG_OK;
1472
1473 end:
1474 return ret;
1475 }
1476
1477 LTTNG_HIDDEN
1478 const struct lttng_bytecode *
1479 lttng_condition_event_rule_get_capture_bytecode_at_index(
1480 const struct lttng_condition *condition, unsigned int index)
1481 {
1482 const struct lttng_condition_event_rule *event_rule_cond =
1483 container_of(condition,
1484 const struct lttng_condition_event_rule,
1485 parent);
1486 struct lttng_capture_descriptor *desc = NULL;
1487 struct lttng_bytecode *bytecode = NULL;
1488 unsigned int count;
1489 enum lttng_condition_status status;
1490
1491 if (!condition || !IS_EVENT_RULE_CONDITION(condition)) {
1492 goto end;
1493 }
1494
1495 status = lttng_condition_event_rule_get_capture_descriptor_count(
1496 condition, &count);
1497 if (status != LTTNG_CONDITION_STATUS_OK) {
1498 goto end;
1499 }
1500
1501 if (index >= count) {
1502 goto end;
1503 }
1504
1505 desc = lttng_dynamic_pointer_array_get_pointer(
1506 &event_rule_cond->capture_descriptors, index);
1507 if (desc == NULL) {
1508 goto end;
1509 }
1510
1511 bytecode = desc->bytecode;
1512 end:
1513 return bytecode;
1514 }
This page took 0.078826 seconds and 5 git commands to generate.