b2abcc7648e072f2225a5cf50b472278d2f84989
[lttng-tools.git] / src / common / conditions / event-rule-matches.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/condition/event-rule-matches-internal.h>
16 #include <lttng/condition/event-rule-matches.h>
17 #include <lttng/event-expr-internal.h>
18 #include <lttng/event-expr.h>
19 #include <lttng/event-field-value-internal.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/lttng-error.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <vendor/msgpack/msgpack.h>
25
26 #define IS_EVENT_RULE_MATCHES_CONDITION(condition) \
27 (lttng_condition_get_type(condition) == \
28 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES)
29
30 static bool is_event_rule_matches_evaluation(
31 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_MATCHES;
36 }
37
38 static bool lttng_condition_event_rule_matches_validate(
39 const struct lttng_condition *condition);
40 static int lttng_condition_event_rule_matches_serialize(
41 const struct lttng_condition *condition,
42 struct lttng_payload *payload);
43 static bool lttng_condition_event_rule_matches_is_equal(
44 const struct lttng_condition *_a,
45 const struct lttng_condition *_b);
46 static void lttng_condition_event_rule_matches_destroy(
47 struct lttng_condition *condition);
48
49 static bool lttng_condition_event_rule_matches_validate(
50 const struct lttng_condition *condition)
51 {
52 bool valid = false;
53 struct lttng_condition_event_rule_matches *event_rule;
54
55 if (!condition) {
56 goto end;
57 }
58
59 event_rule = container_of(condition,
60 struct lttng_condition_event_rule_matches, parent);
61 if (!event_rule->rule) {
62 ERR("Invalid on event condition: a rule must be set");
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 struct lttng_capture_descriptor *
247 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
248 const struct lttng_condition *condition, unsigned int index)
249 {
250 const struct lttng_condition_event_rule_matches
251 *event_rule_matches_cond = container_of(condition,
252 const struct lttng_condition_event_rule_matches,
253 parent);
254 struct lttng_capture_descriptor *desc = NULL;
255 unsigned int count;
256 enum lttng_condition_status status;
257
258 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition)) {
259 goto end;
260 }
261
262 status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
263 condition, &count);
264 if (status != LTTNG_CONDITION_STATUS_OK) {
265 goto end;
266 }
267
268 if (index >= count) {
269 goto end;
270 }
271
272 desc = lttng_dynamic_pointer_array_get_pointer(
273 &event_rule_matches_cond->capture_descriptors, index);
274 end:
275 return desc;
276 }
277
278 static int lttng_condition_event_rule_matches_serialize(
279 const struct lttng_condition *condition,
280 struct lttng_payload *payload)
281 {
282 int ret;
283 struct lttng_condition_event_rule_matches *event_rule_matches_condition;
284 enum lttng_condition_status status;
285 /* Used for iteration and communication (size matters). */
286 uint32_t i, capture_descr_count;
287
288 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition)) {
289 ret = -1;
290 goto end;
291 }
292
293 DBG("Serializing on event condition");
294 event_rule_matches_condition = container_of(condition,
295 struct lttng_condition_event_rule_matches, parent);
296
297 DBG("Serializing on event condition's event rule");
298 ret = lttng_event_rule_serialize(
299 event_rule_matches_condition->rule, payload);
300 if (ret) {
301 goto end;
302 }
303
304 status = lttng_condition_event_rule_matches_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_event_rule_matches_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_event_rule_matches_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_matches_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_matches_get_capture_descriptor_at_index(
366 condition_a, i);
367 const struct lttng_event_expr *expr_b =
368 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
369 condition_b, i);
370
371 if (!lttng_event_expr_is_equal(expr_a, expr_b)) {
372 goto not_equal;
373 }
374 }
375
376 goto end;
377
378 not_equal:
379 is_equal = false;
380
381 end:
382 return is_equal;
383 }
384
385 static bool lttng_condition_event_rule_matches_is_equal(
386 const struct lttng_condition *_a,
387 const struct lttng_condition *_b)
388 {
389 bool is_equal = false;
390 struct lttng_condition_event_rule_matches *a, *b;
391
392 a = container_of(_a, struct lttng_condition_event_rule_matches, parent);
393 b = container_of(_b, struct lttng_condition_event_rule_matches, parent);
394
395 /* Both event rules must be set or both must be unset. */
396 if ((a->rule && !b->rule) || (!a->rule && b->rule)) {
397 WARN("Comparing event_rule conditions with uninitialized rule");
398 goto end;
399 }
400
401 is_equal = lttng_event_rule_is_equal(a->rule, b->rule);
402 if (!is_equal) {
403 goto end;
404 }
405
406 is_equal = capture_descriptors_are_equal(_a, _b);
407
408 end:
409 return is_equal;
410 }
411
412 static void lttng_condition_event_rule_matches_destroy(
413 struct lttng_condition *condition)
414 {
415 struct lttng_condition_event_rule_matches *event_rule_matches_condition;
416
417 event_rule_matches_condition = container_of(condition,
418 struct lttng_condition_event_rule_matches, parent);
419
420 lttng_event_rule_put(event_rule_matches_condition->rule);
421 lttng_dynamic_pointer_array_reset(
422 &event_rule_matches_condition->capture_descriptors);
423 free(event_rule_matches_condition);
424 }
425
426 static
427 void destroy_capture_descriptor(void *ptr)
428 {
429 struct lttng_capture_descriptor *desc =
430 (struct lttng_capture_descriptor *) ptr;
431
432 lttng_event_expr_destroy(desc->event_expression);
433 free(desc->bytecode);
434 free(desc);
435 }
436
437 struct lttng_condition *lttng_condition_event_rule_matches_create(
438 struct lttng_event_rule *rule)
439 {
440 struct lttng_condition *parent = NULL;
441 struct lttng_condition_event_rule_matches *condition = NULL;
442
443 if (!rule) {
444 goto end;
445 }
446
447 condition = zmalloc(sizeof(struct lttng_condition_event_rule_matches));
448 if (!condition) {
449 return NULL;
450 }
451
452 lttng_condition_init(&condition->parent,
453 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
454 condition->parent.validate =
455 lttng_condition_event_rule_matches_validate,
456 condition->parent.serialize =
457 lttng_condition_event_rule_matches_serialize,
458 condition->parent.equal = lttng_condition_event_rule_matches_is_equal,
459 condition->parent.destroy = lttng_condition_event_rule_matches_destroy,
460
461 lttng_event_rule_get(rule);
462 condition->rule = rule;
463 rule = NULL;
464
465 lttng_dynamic_pointer_array_init(&condition->capture_descriptors,
466 destroy_capture_descriptor);
467
468 parent = &condition->parent;
469 end:
470 return parent;
471 }
472
473 static
474 uint64_t uint_from_buffer(const struct lttng_buffer_view *view, size_t size,
475 size_t *offset)
476 {
477 uint64_t ret;
478 const struct lttng_buffer_view uint_view =
479 lttng_buffer_view_from_view(view, *offset, size);
480
481 if (!lttng_buffer_view_is_valid(&uint_view)) {
482 ret = UINT64_C(-1);
483 goto end;
484 }
485
486 switch (size) {
487 case 1:
488 ret = (uint64_t) *uint_view.data;
489 break;
490 case sizeof(uint32_t):
491 {
492 uint32_t u32;
493
494 memcpy(&u32, uint_view.data, sizeof(u32));
495 ret = (uint64_t) u32;
496 break;
497 }
498 case sizeof(ret):
499 memcpy(&ret, uint_view.data, sizeof(ret));
500 break;
501 default:
502 abort();
503 }
504
505 *offset += size;
506
507 end:
508 return ret;
509 }
510
511 static
512 const char *str_from_buffer(const struct lttng_buffer_view *view,
513 size_t *offset)
514 {
515 uint64_t len;
516 const char *ret;
517
518 len = uint_from_buffer(view, sizeof(uint32_t), offset);
519 if (len == UINT64_C(-1)) {
520 goto error;
521 }
522
523 ret = &view->data[*offset];
524
525 if (!lttng_buffer_view_contains_string(view, ret, len)) {
526 goto error;
527 }
528
529 *offset += len;
530 goto end;
531
532 error:
533 ret = NULL;
534
535 end:
536 return ret;
537 }
538
539 static
540 struct lttng_event_expr *event_expr_from_payload(
541 struct lttng_payload_view *view, size_t *offset)
542 {
543 struct lttng_event_expr *expr = NULL;
544 const char *str;
545 uint64_t type;
546
547 type = uint_from_buffer(&view->buffer, sizeof(uint8_t), offset);
548 if (type == UINT64_C(-1)) {
549 goto error;
550 }
551
552 switch (type) {
553 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD:
554 str = str_from_buffer(&view->buffer, offset);
555 if (!str) {
556 goto error;
557 }
558
559 expr = lttng_event_expr_event_payload_field_create(str);
560 break;
561 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD:
562 str = str_from_buffer(&view->buffer, offset);
563 if (!str) {
564 goto error;
565 }
566
567 expr = lttng_event_expr_channel_context_field_create(str);
568 break;
569 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD:
570 {
571 const char *provider_name;
572 const char *type_name;
573
574 provider_name = str_from_buffer(&view->buffer, offset);
575 if (!provider_name) {
576 goto error;
577 }
578
579 type_name = str_from_buffer(&view->buffer, offset);
580 if (!type_name) {
581 goto error;
582 }
583
584 expr = lttng_event_expr_app_specific_context_field_create(
585 provider_name, type_name);
586 break;
587 }
588 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT:
589 {
590 struct lttng_event_expr *array_field_expr;
591 const uint64_t index = uint_from_buffer(
592 &view->buffer, sizeof(uint32_t), offset);
593
594 if (index == UINT64_C(-1)) {
595 goto error;
596 }
597
598 /* Array field expression is the encoded after this. */
599 array_field_expr = event_expr_from_payload(view, offset);
600 if (!array_field_expr) {
601 goto error;
602 }
603
604 /* Move ownership of `array_field_expr` to new expression. */
605 expr = lttng_event_expr_array_field_element_create(
606 array_field_expr, (unsigned int) index);
607 if (!expr) {
608 /* `array_field_expr` not moved: destroy it. */
609 lttng_event_expr_destroy(array_field_expr);
610 }
611
612 break;
613 }
614 default:
615 ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64,
616 type);
617 goto error;
618 }
619
620 goto end;
621
622 error:
623 lttng_event_expr_destroy(expr);
624 expr = NULL;
625
626 end:
627 return expr;
628 }
629
630 LTTNG_HIDDEN
631 ssize_t lttng_condition_event_rule_matches_create_from_payload(
632 struct lttng_payload_view *view,
633 struct lttng_condition **_condition)
634 {
635 ssize_t consumed_length;
636 size_t offset = 0;
637 ssize_t event_rule_length;
638 uint32_t i, capture_descr_count;
639 struct lttng_condition *condition = NULL;
640 struct lttng_event_rule *event_rule = NULL;
641
642 if (!view || !_condition) {
643 goto error;
644 }
645
646 /* Struct lttng_event_rule. */
647 {
648 struct lttng_payload_view event_rule_view =
649 lttng_payload_view_from_view(view, offset, -1);
650
651 event_rule_length = lttng_event_rule_create_from_payload(
652 &event_rule_view, &event_rule);
653 }
654
655 if (event_rule_length < 0 || !event_rule) {
656 goto error;
657 }
658
659 offset += event_rule_length;
660
661 /* Create condition (no capture descriptors yet) at this point */
662 condition = lttng_condition_event_rule_matches_create(event_rule);
663 if (!condition) {
664 goto error;
665 }
666
667 /* Capture descriptor count. */
668 assert(event_rule_length >= 0);
669 capture_descr_count = uint_from_buffer(&view->buffer, sizeof(uint32_t), &offset);
670 if (capture_descr_count == UINT32_C(-1)) {
671 goto error;
672 }
673
674 /* Capture descriptors. */
675 for (i = 0; i < capture_descr_count; i++) {
676 enum lttng_condition_status status;
677 struct lttng_event_expr *expr = event_expr_from_payload(
678 view, &offset);
679
680 if (!expr) {
681 goto error;
682 }
683
684 /* Move ownership of `expr` to `condition`. */
685 status = lttng_condition_event_rule_matches_append_capture_descriptor(
686 condition, expr);
687 if (status != LTTNG_CONDITION_STATUS_OK) {
688 /* `expr` not moved: destroy it. */
689 lttng_event_expr_destroy(expr);
690 goto error;
691 }
692 }
693
694 consumed_length = (ssize_t) offset;
695 *_condition = condition;
696 condition = NULL;
697 goto end;
698
699 error:
700 consumed_length = -1;
701
702 end:
703 lttng_event_rule_put(event_rule);
704 lttng_condition_put(condition);
705 return consumed_length;
706 }
707
708 LTTNG_HIDDEN
709 enum lttng_condition_status
710 lttng_condition_event_rule_matches_borrow_rule_mutable(
711 const struct lttng_condition *condition,
712 struct lttng_event_rule **rule)
713 {
714 struct lttng_condition_event_rule_matches *event_rule;
715 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
716
717 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition) ||
718 !rule) {
719 status = LTTNG_CONDITION_STATUS_INVALID;
720 goto end;
721 }
722
723 event_rule = container_of(condition,
724 struct lttng_condition_event_rule_matches, parent);
725 if (!event_rule->rule) {
726 status = LTTNG_CONDITION_STATUS_UNSET;
727 goto end;
728 }
729
730 *rule = event_rule->rule;
731 end:
732 return status;
733 }
734
735 enum lttng_condition_status lttng_condition_event_rule_matches_get_rule(
736 const struct lttng_condition *condition,
737 const struct lttng_event_rule **rule)
738 {
739 struct lttng_event_rule *mutable_rule = NULL;
740 const enum lttng_condition_status status =
741 lttng_condition_event_rule_matches_borrow_rule_mutable(
742 condition, &mutable_rule);
743
744 *rule = mutable_rule;
745 return status;
746 }
747
748 LTTNG_HIDDEN
749 void lttng_condition_event_rule_matches_set_error_counter_index(
750 struct lttng_condition *condition, uint64_t error_counter_index)
751 {
752 struct lttng_condition_event_rule_matches *event_rule_matches_cond =
753 container_of(condition,
754 struct lttng_condition_event_rule_matches,
755 parent);
756
757 LTTNG_OPTIONAL_SET(&event_rule_matches_cond->error_counter_index,
758 error_counter_index);
759 }
760
761 LTTNG_HIDDEN
762 uint64_t lttng_condition_event_rule_matches_get_error_counter_index(
763 const struct lttng_condition *condition)
764 {
765 const struct lttng_condition_event_rule_matches
766 *event_rule_matches_cond = container_of(condition,
767 const struct lttng_condition_event_rule_matches,
768 parent);
769
770 return LTTNG_OPTIONAL_GET(event_rule_matches_cond->error_counter_index);
771 }
772
773 enum lttng_condition_status
774 lttng_condition_event_rule_matches_append_capture_descriptor(
775 struct lttng_condition *condition,
776 struct lttng_event_expr *expr)
777 {
778 int ret;
779 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
780 struct lttng_condition_event_rule_matches *event_rule_matches_cond =
781 container_of(condition,
782 struct lttng_condition_event_rule_matches,
783 parent);
784 struct lttng_capture_descriptor *descriptor = NULL;
785 const struct lttng_event_rule *rule = NULL;
786
787 /* Only accept l-values. */
788 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition) ||
789 !expr || !lttng_event_expr_is_lvalue(expr)) {
790 status = LTTNG_CONDITION_STATUS_INVALID;
791 goto end;
792 }
793
794 status = lttng_condition_event_rule_matches_get_rule(condition, &rule);
795 if (status != LTTNG_CONDITION_STATUS_OK) {
796 goto end;
797 }
798
799 switch(lttng_event_rule_get_type(rule)) {
800 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
801 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
802 /* Supported. */
803 status = LTTNG_CONDITION_STATUS_OK;
804 break;
805 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
806 status = LTTNG_CONDITION_STATUS_INVALID;
807 break;
808 default:
809 status = LTTNG_CONDITION_STATUS_UNSUPPORTED;
810 break;
811 }
812
813 if (status != LTTNG_CONDITION_STATUS_OK) {
814 goto end;
815 }
816
817 descriptor = malloc(sizeof(*descriptor));
818 if (descriptor == NULL) {
819 status = LTTNG_CONDITION_STATUS_ERROR;
820 goto end;
821 }
822
823 descriptor->event_expression = expr;
824 descriptor->bytecode = NULL;
825
826 ret = lttng_dynamic_pointer_array_add_pointer(
827 &event_rule_matches_cond->capture_descriptors,
828 descriptor);
829 if (ret) {
830 status = LTTNG_CONDITION_STATUS_ERROR;
831 goto end;
832 }
833
834 /* Ownership is transfered to the internal capture_descriptors array */
835 descriptor = NULL;
836 end:
837 free(descriptor);
838 return status;
839 }
840
841 enum lttng_condition_status
842 lttng_condition_event_rule_matches_get_capture_descriptor_count(
843 const struct lttng_condition *condition, unsigned int *count)
844 {
845 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
846 const struct lttng_condition_event_rule_matches
847 *event_rule_matches_condition = container_of(condition,
848 const struct lttng_condition_event_rule_matches,
849 parent);
850
851 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition) ||
852 !count) {
853 status = LTTNG_CONDITION_STATUS_INVALID;
854 goto end;
855 }
856
857 *count = lttng_dynamic_pointer_array_get_count(
858 &event_rule_matches_condition->capture_descriptors);
859
860 end:
861 return status;
862 }
863
864 const struct lttng_event_expr *
865 lttng_condition_event_rule_matches_get_capture_descriptor_at_index(
866 const struct lttng_condition *condition, unsigned int index)
867 {
868 const struct lttng_event_expr *expr = NULL;
869 const struct lttng_capture_descriptor *desc = NULL;
870
871 desc = lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
872 condition, index);
873 if (desc == NULL) {
874 goto end;
875 }
876 expr = desc->event_expression;
877
878 end:
879 return expr;
880 }
881
882 LTTNG_HIDDEN
883 ssize_t lttng_evaluation_event_rule_matches_create_from_payload(
884 const struct lttng_condition_event_rule_matches *condition,
885 struct lttng_payload_view *view,
886 struct lttng_evaluation **_evaluation)
887 {
888 ssize_t ret, offset = 0;
889 struct lttng_evaluation *evaluation = NULL;
890 uint32_t capture_payload_size;
891 const char *capture_payload = NULL;
892
893 if (!_evaluation) {
894 ret = -1;
895 goto error;
896 }
897
898 {
899 const struct lttng_payload_view current_view =
900 lttng_payload_view_from_view(view, offset, -1);
901
902 if (current_view.buffer.size < sizeof(capture_payload_size)) {
903 ret = -1;
904 goto error;
905 }
906
907 memcpy(&capture_payload_size, current_view.buffer.data,
908 sizeof(capture_payload_size));
909 }
910 offset += sizeof(capture_payload_size);
911
912 if (capture_payload_size > 0) {
913 const struct lttng_payload_view current_view =
914 lttng_payload_view_from_view(view, offset, -1);
915
916 if (current_view.buffer.size < capture_payload_size) {
917 ret = -1;
918 goto error;
919 }
920
921 capture_payload = current_view.buffer.data;
922 }
923
924 evaluation = lttng_evaluation_event_rule_matches_create(
925 condition, capture_payload, capture_payload_size, true);
926 if (!evaluation) {
927 ret = -1;
928 goto error;
929 }
930
931 offset += capture_payload_size;
932 *_evaluation = evaluation;
933 evaluation = NULL;
934 ret = offset;
935
936 error:
937 lttng_evaluation_destroy(evaluation);
938 return ret;
939 }
940
941 static int lttng_evaluation_event_rule_matches_serialize(
942 const struct lttng_evaluation *evaluation,
943 struct lttng_payload *payload)
944 {
945 int ret = 0;
946 struct lttng_evaluation_event_rule_matches *hit;
947 uint32_t capture_payload_size;
948
949 hit = container_of(evaluation,
950 struct lttng_evaluation_event_rule_matches, parent);
951
952 capture_payload_size = (uint32_t) hit->capture_payload.size;
953 ret = lttng_dynamic_buffer_append(&payload->buffer, &capture_payload_size,
954 sizeof(capture_payload_size));
955 if (ret) {
956 goto end;
957 }
958
959 ret = lttng_dynamic_buffer_append(&payload->buffer, hit->capture_payload.data,
960 hit->capture_payload.size);
961 if (ret) {
962 goto end;
963 }
964
965 end:
966 return ret;
967 }
968
969 static
970 bool msgpack_str_is_equal(const struct msgpack_object *obj, const char *str)
971 {
972 bool is_equal = true;
973
974 assert(obj->type == MSGPACK_OBJECT_STR);
975
976 if (obj->via.str.size != strlen(str)) {
977 is_equal = false;
978 goto end;
979 }
980
981 if (strncmp(obj->via.str.ptr, str, obj->via.str.size) != 0) {
982 is_equal = false;
983 goto end;
984 }
985
986 end:
987 return is_equal;
988 }
989
990 static
991 const msgpack_object *get_msgpack_map_obj(const struct msgpack_object *map_obj,
992 const char *name)
993 {
994 const msgpack_object *ret = NULL;
995 size_t i;
996
997 assert(map_obj->type == MSGPACK_OBJECT_MAP);
998
999 for (i = 0; i < map_obj->via.map.size; i++) {
1000 const struct msgpack_object_kv *kv = &map_obj->via.map.ptr[i];
1001
1002 assert(kv->key.type == MSGPACK_OBJECT_STR);
1003
1004 if (msgpack_str_is_equal(&kv->key, name)) {
1005 ret = &kv->val;
1006 goto end;
1007 }
1008 }
1009
1010 end:
1011 return ret;
1012 }
1013
1014 static void lttng_evaluation_event_rule_matches_destroy(
1015 struct lttng_evaluation *evaluation)
1016 {
1017 struct lttng_evaluation_event_rule_matches *hit;
1018
1019 hit = container_of(evaluation,
1020 struct lttng_evaluation_event_rule_matches, parent);
1021 lttng_dynamic_buffer_reset(&hit->capture_payload);
1022 lttng_event_field_value_destroy(hit->captured_values);
1023 free(hit);
1024 }
1025
1026 static
1027 int event_field_value_from_obj(const msgpack_object *obj,
1028 struct lttng_event_field_value **field_val)
1029 {
1030 int ret = 0;
1031
1032 assert(obj);
1033 assert(field_val);
1034
1035 switch (obj->type) {
1036 case MSGPACK_OBJECT_NIL:
1037 /* Unavailable. */
1038 *field_val = NULL;
1039 goto end;
1040 case MSGPACK_OBJECT_POSITIVE_INTEGER:
1041 *field_val = lttng_event_field_value_uint_create(
1042 obj->via.u64);
1043 break;
1044 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
1045 *field_val = lttng_event_field_value_int_create(
1046 obj->via.i64);
1047 break;
1048 case MSGPACK_OBJECT_FLOAT32:
1049 case MSGPACK_OBJECT_FLOAT64:
1050 *field_val = lttng_event_field_value_real_create(
1051 obj->via.f64);
1052 break;
1053 case MSGPACK_OBJECT_STR:
1054 *field_val = lttng_event_field_value_string_create_with_size(
1055 obj->via.str.ptr, obj->via.str.size);
1056 break;
1057 case MSGPACK_OBJECT_ARRAY:
1058 {
1059 size_t i;
1060
1061 *field_val = lttng_event_field_value_array_create();
1062 if (!*field_val) {
1063 goto error;
1064 }
1065
1066 for (i = 0; i < obj->via.array.size; i++) {
1067 const msgpack_object *elem_obj = &obj->via.array.ptr[i];
1068 struct lttng_event_field_value *elem_field_val;
1069
1070 ret = event_field_value_from_obj(elem_obj,
1071 &elem_field_val);
1072 if (ret) {
1073 goto error;
1074 }
1075
1076 if (elem_field_val) {
1077 ret = lttng_event_field_value_array_append(
1078 *field_val, elem_field_val);
1079 } else {
1080 ret = lttng_event_field_value_array_append_unavailable(
1081 *field_val);
1082 }
1083
1084 if (ret) {
1085 lttng_event_field_value_destroy(elem_field_val);
1086 goto error;
1087 }
1088 }
1089
1090 break;
1091 }
1092 case MSGPACK_OBJECT_MAP:
1093 {
1094 /*
1095 * As of this version, the only valid map object is
1096 * for an enumeration value, for example:
1097 *
1098 * type: enum
1099 * value: 177
1100 * labels:
1101 * - Labatt 50
1102 * - Molson Dry
1103 * - Carling Black Label
1104 */
1105 const msgpack_object *inner_obj;
1106 size_t label_i;
1107
1108 inner_obj = get_msgpack_map_obj(obj, "type");
1109 if (!inner_obj) {
1110 ERR("Missing `type` entry in map object");
1111 goto error;
1112 }
1113
1114 if (inner_obj->type != MSGPACK_OBJECT_STR) {
1115 ERR("Map object's `type` entry is not a string: type = %s",
1116 msgpack_object_type_str(inner_obj->type));
1117 goto error;
1118 }
1119
1120 if (!msgpack_str_is_equal(inner_obj, "enum")) {
1121 ERR("Map object's `type` entry: expecting `enum`");
1122 goto error;
1123 }
1124
1125 inner_obj = get_msgpack_map_obj(obj, "value");
1126 if (!inner_obj) {
1127 ERR("Missing `value` entry in map object");
1128 goto error;
1129 }
1130
1131 if (inner_obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
1132 *field_val = lttng_event_field_value_enum_uint_create(
1133 inner_obj->via.u64);
1134 } else if (inner_obj->type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
1135 *field_val = lttng_event_field_value_enum_int_create(
1136 inner_obj->via.i64);
1137 } else {
1138 ERR("Map object's `value` entry is not an integer: type = %s",
1139 msgpack_object_type_str(inner_obj->type));
1140 goto error;
1141 }
1142
1143 if (!*field_val) {
1144 goto error;
1145 }
1146
1147 inner_obj = get_msgpack_map_obj(obj, "labels");
1148 if (!inner_obj) {
1149 /* No labels */
1150 goto end;
1151 }
1152
1153 if (inner_obj->type != MSGPACK_OBJECT_ARRAY) {
1154 ERR("Map object's `labels` entry is not an array: type = %s",
1155 msgpack_object_type_str(inner_obj->type));
1156 goto error;
1157 }
1158
1159 for (label_i = 0; label_i < inner_obj->via.array.size;
1160 label_i++) {
1161 int iret;
1162 const msgpack_object *elem_obj =
1163 &inner_obj->via.array.ptr[label_i];
1164
1165 if (elem_obj->type != MSGPACK_OBJECT_STR) {
1166 ERR("Map object's `labels` entry's type is not a string: type = %s",
1167 msgpack_object_type_str(elem_obj->type));
1168 goto error;
1169 }
1170
1171 iret = lttng_event_field_value_enum_append_label_with_size(
1172 *field_val, elem_obj->via.str.ptr,
1173 elem_obj->via.str.size);
1174 if (iret) {
1175 goto error;
1176 }
1177 }
1178
1179 break;
1180 }
1181 default:
1182 ERR("Unexpected object type: type = %s",
1183 msgpack_object_type_str(obj->type));
1184 goto error;
1185 }
1186
1187 if (!*field_val) {
1188 goto error;
1189 }
1190
1191 goto end;
1192
1193 error:
1194 lttng_event_field_value_destroy(*field_val);
1195 *field_val = NULL;
1196 ret = -1;
1197
1198 end:
1199 return ret;
1200 }
1201
1202 static struct lttng_event_field_value *event_field_value_from_capture_payload(
1203 const struct lttng_condition_event_rule_matches *condition,
1204 const char *capture_payload,
1205 size_t capture_payload_size)
1206 {
1207 struct lttng_event_field_value *ret = NULL;
1208 msgpack_unpacked unpacked;
1209 msgpack_unpack_return unpack_return;
1210 const msgpack_object *root_obj;
1211 const msgpack_object_array *root_array_obj;
1212 size_t i;
1213 size_t count;
1214
1215 assert(condition);
1216 assert(capture_payload);
1217
1218 /* Initialize value. */
1219 msgpack_unpacked_init(&unpacked);
1220
1221 /* Decode. */
1222 unpack_return = msgpack_unpack_next(&unpacked, capture_payload,
1223 capture_payload_size, NULL);
1224 if (unpack_return != MSGPACK_UNPACK_SUCCESS) {
1225 ERR("msgpack_unpack_next() failed to decode the "
1226 "MessagePack-encoded capture payload: "
1227 "size = %zu, ret = %d",
1228 capture_payload_size, unpack_return);
1229 goto error;
1230 }
1231
1232 /* Get root array. */
1233 root_obj = &unpacked.data;
1234
1235 if (root_obj->type != MSGPACK_OBJECT_ARRAY) {
1236 ERR("Expecting an array as the root object: type = %s",
1237 msgpack_object_type_str(root_obj->type));
1238 goto error;
1239 }
1240
1241 root_array_obj = &root_obj->via.array;
1242
1243 /* Create an empty root array event field value. */
1244 ret = lttng_event_field_value_array_create();
1245 if (!ret) {
1246 goto error;
1247 }
1248
1249 /*
1250 * For each capture descriptor in the condition object:
1251 *
1252 * 1. Get its corresponding captured field value MessagePack
1253 * object.
1254 *
1255 * 2. Create a corresponding event field value.
1256 *
1257 * 3. Append it to `ret` (the root array event field value).
1258 */
1259 count = lttng_dynamic_pointer_array_get_count(
1260 &condition->capture_descriptors);
1261 assert(count > 0);
1262
1263 for (i = 0; i < count; i++) {
1264 const struct lttng_capture_descriptor *capture_descriptor =
1265 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1266 &condition->parent, i);
1267 const msgpack_object *elem_obj;
1268 struct lttng_event_field_value *elem_field_val;
1269 int iret;
1270
1271 assert(capture_descriptor);
1272
1273 elem_obj = &root_array_obj->ptr[i];
1274 iret = event_field_value_from_obj(elem_obj,
1275 &elem_field_val);
1276 if (iret) {
1277 goto error;
1278 }
1279
1280 if (elem_field_val) {
1281 iret = lttng_event_field_value_array_append(ret,
1282 elem_field_val);
1283 } else {
1284 iret = lttng_event_field_value_array_append_unavailable(
1285 ret);
1286 }
1287
1288 if (iret) {
1289 lttng_event_field_value_destroy(elem_field_val);
1290 goto error;
1291 }
1292 }
1293
1294 goto end;
1295
1296 error:
1297 lttng_event_field_value_destroy(ret);
1298 ret = NULL;
1299
1300 end:
1301 msgpack_unpacked_destroy(&unpacked);
1302 return ret;
1303 }
1304
1305 LTTNG_HIDDEN
1306 struct lttng_evaluation *lttng_evaluation_event_rule_matches_create(
1307 const struct lttng_condition_event_rule_matches *condition,
1308 const char *capture_payload,
1309 size_t capture_payload_size,
1310 bool decode_capture_payload)
1311 {
1312 struct lttng_evaluation_event_rule_matches *hit;
1313 struct lttng_evaluation *evaluation = NULL;
1314
1315 hit = zmalloc(sizeof(struct lttng_evaluation_event_rule_matches));
1316 if (!hit) {
1317 goto error;
1318 }
1319
1320 lttng_dynamic_buffer_init(&hit->capture_payload);
1321
1322 if (capture_payload) {
1323 const int ret = lttng_dynamic_buffer_append(
1324 &hit->capture_payload, capture_payload,
1325 capture_payload_size);
1326 if (ret) {
1327 ERR("Failed to initialize capture payload of event rule evaluation");
1328 goto error;
1329 }
1330
1331 if (decode_capture_payload) {
1332 hit->captured_values =
1333 event_field_value_from_capture_payload(
1334 condition,
1335 capture_payload,
1336 capture_payload_size);
1337 if (!hit->captured_values) {
1338 ERR("Failed to decode the capture payload: size = %zu",
1339 capture_payload_size);
1340 goto error;
1341 }
1342 }
1343 }
1344
1345 hit->parent.type = LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES;
1346 hit->parent.serialize = lttng_evaluation_event_rule_matches_serialize;
1347 hit->parent.destroy = lttng_evaluation_event_rule_matches_destroy;
1348
1349 evaluation = &hit->parent;
1350 hit = NULL;
1351
1352 error:
1353 if (hit) {
1354 lttng_evaluation_event_rule_matches_destroy(&hit->parent);
1355 }
1356
1357 return evaluation;
1358 }
1359
1360 enum lttng_evaluation_event_rule_matches_status
1361 lttng_evaluation_event_rule_matches_get_captured_values(
1362 const struct lttng_evaluation *evaluation,
1363 const struct lttng_event_field_value **field_val)
1364 {
1365 struct lttng_evaluation_event_rule_matches *hit;
1366 enum lttng_evaluation_event_rule_matches_status status =
1367 LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_OK;
1368
1369 if (!evaluation || !is_event_rule_matches_evaluation(evaluation) ||
1370 !field_val) {
1371 status = LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_INVALID;
1372 goto end;
1373 }
1374
1375 hit = container_of(evaluation,
1376 struct lttng_evaluation_event_rule_matches, parent);
1377 if (!hit->captured_values) {
1378 status = LTTNG_EVALUATION_EVENT_RULE_MATCHES_STATUS_NONE;
1379 goto end;
1380 }
1381
1382 *field_val = hit->captured_values;
1383
1384 end:
1385 return status;
1386 }
1387
1388 LTTNG_HIDDEN
1389 enum lttng_error_code
1390 lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
1391 struct lttng_condition *condition)
1392 {
1393 enum lttng_error_code ret;
1394 enum lttng_condition_status status;
1395 unsigned int capture_count, i;
1396
1397 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition)) {
1398 ret = LTTNG_ERR_FATAL;
1399 goto end;
1400 }
1401
1402 status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
1403 condition, &capture_count);
1404 if (status != LTTNG_CONDITION_STATUS_OK) {
1405 ret = LTTNG_ERR_FATAL;
1406 goto end;
1407 }
1408
1409 for (i = 0; i < capture_count; i++) {
1410 struct lttng_capture_descriptor *local_capture_desc =
1411 lttng_condition_event_rule_matches_get_internal_capture_descriptor_at_index(
1412 condition, i);
1413
1414 if (local_capture_desc == NULL) {
1415 ret = LTTNG_ERR_FATAL;
1416 goto end;
1417 }
1418
1419 /* Generate the bytecode. */
1420 status = lttng_event_expr_to_bytecode(
1421 local_capture_desc->event_expression,
1422 &local_capture_desc->bytecode);
1423 if (status < 0 || local_capture_desc->bytecode == NULL) {
1424 ret = LTTNG_ERR_INVALID_CAPTURE_EXPRESSION;
1425 goto end;
1426 }
1427 }
1428
1429 /* Everything went better than expected */
1430 ret = LTTNG_OK;
1431
1432 end:
1433 return ret;
1434 }
1435
1436 LTTNG_HIDDEN
1437 const struct lttng_bytecode *
1438 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
1439 const struct lttng_condition *condition, unsigned int index)
1440 {
1441 const struct lttng_condition_event_rule_matches
1442 *event_rule_matches_cond = container_of(condition,
1443 const struct lttng_condition_event_rule_matches,
1444 parent);
1445 struct lttng_capture_descriptor *desc = NULL;
1446 struct lttng_bytecode *bytecode = NULL;
1447 unsigned int count;
1448 enum lttng_condition_status status;
1449
1450 if (!condition || !IS_EVENT_RULE_MATCHES_CONDITION(condition)) {
1451 goto end;
1452 }
1453
1454 status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
1455 condition, &count);
1456 if (status != LTTNG_CONDITION_STATUS_OK) {
1457 goto end;
1458 }
1459
1460 if (index >= count) {
1461 goto end;
1462 }
1463
1464 desc = lttng_dynamic_pointer_array_get_pointer(
1465 &event_rule_matches_cond->capture_descriptors, index);
1466 if (desc == NULL) {
1467 goto end;
1468 }
1469
1470 bytecode = desc->bytecode;
1471 end:
1472 return bytecode;
1473 }
This page took 0.057628 seconds and 3 git commands to generate.