trigger: lttng_trigger_get_underlying_domain_type_restriction
[lttng-tools.git] / src / common / trigger.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/trigger/trigger-internal.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/condition/event-rule.h>
11 #include <lttng/condition/buffer-usage.h>
12 #include <lttng/event-rule/event-rule-internal.h>
13 #include <lttng/action/action-internal.h>
14 #include <common/credentials.h>
15 #include <common/payload.h>
16 #include <common/payload-view.h>
17 #include <lttng/domain.h>
18 #include <common/error.h>
19 #include <common/dynamic-array.h>
20 #include <common/optional.h>
21 #include <assert.h>
22 #include <inttypes.h>
23
24 LTTNG_HIDDEN
25 bool lttng_trigger_validate(struct lttng_trigger *trigger)
26 {
27 bool valid;
28
29 if (!trigger) {
30 valid = false;
31 goto end;
32 }
33
34 if (!trigger->creds.uid.is_set) {
35 valid = false;
36 goto end;
37 }
38
39 valid = lttng_condition_validate(trigger->condition) &&
40 lttng_action_validate(trigger->action);
41 end:
42 return valid;
43 }
44
45 struct lttng_trigger *lttng_trigger_create(
46 struct lttng_condition *condition,
47 struct lttng_action *action)
48 {
49 struct lttng_trigger *trigger = NULL;
50
51 if (!condition || !action) {
52 goto end;
53 }
54
55 trigger = zmalloc(sizeof(struct lttng_trigger));
56 if (!trigger) {
57 goto end;
58 }
59
60 urcu_ref_init(&trigger->ref);
61
62 trigger->firing_policy.type = LTTNG_TRIGGER_FIRING_POLICY_EVERY_N;
63 trigger->firing_policy.threshold = 1;
64
65 lttng_condition_get(condition);
66 trigger->condition = condition;
67
68 lttng_action_get(action);
69 trigger->action = action;
70
71 end:
72 return trigger;
73 }
74
75 /*
76 * Note: the lack of reference counting 'get' on the condition object is normal.
77 * This API was exposed as such in 2.11. The client is not expected to call
78 * lttng_condition_destroy on the returned object.
79 */
80 struct lttng_condition *lttng_trigger_get_condition(
81 struct lttng_trigger *trigger)
82 {
83 return trigger ? trigger->condition : NULL;
84 }
85
86 LTTNG_HIDDEN
87 const struct lttng_condition *lttng_trigger_get_const_condition(
88 const struct lttng_trigger *trigger)
89 {
90 return trigger->condition;
91 }
92
93
94 /*
95 * Note: the lack of reference counting 'get' on the action object is normal.
96 * This API was exposed as such in 2.11. The client is not expected to call
97 * lttng_action_destroy on the returned object.
98 */
99 struct lttng_action *lttng_trigger_get_action(
100 struct lttng_trigger *trigger)
101 {
102 return trigger ? trigger->action : NULL;
103 }
104
105 LTTNG_HIDDEN
106 const struct lttng_action *lttng_trigger_get_const_action(
107 const struct lttng_trigger *trigger)
108 {
109 return trigger->action;
110 }
111
112 static void trigger_destroy_ref(struct urcu_ref *ref)
113 {
114 struct lttng_trigger *trigger =
115 container_of(ref, struct lttng_trigger, ref);
116 struct lttng_action *action = lttng_trigger_get_action(trigger);
117 struct lttng_condition *condition =
118 lttng_trigger_get_condition(trigger);
119
120 assert(action);
121 assert(condition);
122
123 /* Release ownership. */
124 lttng_action_put(action);
125 lttng_condition_put(condition);
126
127 free(trigger->name);
128 free(trigger);
129 }
130
131 void lttng_trigger_destroy(struct lttng_trigger *trigger)
132 {
133 lttng_trigger_put(trigger);
134 }
135
136 static bool is_firing_policy_valid(enum lttng_trigger_firing_policy policy)
137 {
138 bool valid = false;
139
140 switch (policy) {
141 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N:
142 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N:
143 valid = true;
144 break;
145 default:
146 valid = false;
147 break;
148 }
149
150 return valid;
151 }
152
153 LTTNG_HIDDEN
154 ssize_t lttng_trigger_create_from_payload(
155 struct lttng_payload_view *src_view,
156 struct lttng_trigger **_trigger)
157 {
158 ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
159 struct lttng_condition *condition = NULL;
160 struct lttng_action *action = NULL;
161 const struct lttng_trigger_comm *trigger_comm;
162 const char *name = NULL;
163 uint64_t firing_policy_threshold;
164 enum lttng_trigger_firing_policy firing_policy;
165 struct lttng_credentials creds = {
166 .uid = LTTNG_OPTIONAL_INIT_UNSET,
167 .gid = LTTNG_OPTIONAL_INIT_UNSET,
168 };
169 struct lttng_trigger *trigger = NULL;
170 const struct lttng_payload_view trigger_comm_view =
171 lttng_payload_view_from_view(
172 src_view, 0, sizeof(*trigger_comm));
173
174 if (!src_view || !_trigger) {
175 ret = -1;
176 goto end;
177 }
178
179 if (!lttng_payload_view_is_valid(&trigger_comm_view)) {
180 /* Payload not large enough to contain the header. */
181 ret = -1;
182 goto end;
183 }
184
185 /* lttng_trigger_comm header */
186 trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data;
187
188 /* Set the trigger's creds. */
189 if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) {
190 /* UID out of range for this platform. */
191 ret = -1;
192 goto end;
193 }
194
195 LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid);
196
197 offset += sizeof(*trigger_comm);
198
199 firing_policy = trigger_comm->firing_policy_type;
200 if (!is_firing_policy_valid(firing_policy)) {
201 ret =-1;
202 goto end;
203 }
204
205 firing_policy_threshold = trigger_comm->firing_policy_threshold;
206 if (trigger_comm->name_length != 0) {
207 /* Name. */
208 const struct lttng_payload_view name_view =
209 lttng_payload_view_from_view(
210 src_view, offset,
211 trigger_comm->name_length);
212
213 if (!lttng_payload_view_is_valid(&name_view)) {
214 ret = -1;
215 goto end;
216 }
217
218 name = name_view.buffer.data;
219 if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
220 trigger_comm->name_length)) {
221 ret = -1;
222 goto end;
223 }
224
225 offset += trigger_comm->name_length;
226 name_size = trigger_comm->name_length;
227 }
228
229 {
230 /* struct lttng_condition */
231 struct lttng_payload_view condition_view =
232 lttng_payload_view_from_view(
233 src_view, offset, -1);
234
235 condition_size = lttng_condition_create_from_payload(&condition_view,
236 &condition);
237 }
238
239 if (condition_size < 0) {
240 ret = condition_size;
241 goto end;
242 }
243
244 offset += condition_size;
245 {
246 /* struct lttng_action */
247 struct lttng_payload_view action_view =
248 lttng_payload_view_from_view(
249 src_view, offset, -1);
250
251 action_size = lttng_action_create_from_payload(&action_view, &action);
252 }
253
254 if (action_size < 0) {
255 ret = action_size;
256 goto end;
257 }
258 offset += action_size;
259
260 /* Unexpected size of inner-elements; the buffer is corrupted. */
261 if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
262 ret = -1;
263 goto error;
264 }
265
266 trigger = lttng_trigger_create(condition, action);
267 if (!trigger) {
268 ret = -1;
269 goto error;
270 }
271
272 lttng_trigger_set_credentials(trigger, &creds);
273
274 /*
275 * The trigger object owns references to the action and condition
276 * objects.
277 */
278 lttng_condition_put(condition);
279 condition = NULL;
280
281 lttng_action_put(action);
282 action = NULL;
283
284 if (name) {
285 const enum lttng_trigger_status status =
286 lttng_trigger_set_name(trigger, name);
287
288 if (status != LTTNG_TRIGGER_STATUS_OK) {
289 ret = -1;
290 goto end;
291 }
292 }
293
294 /* Set the policy. */
295 {
296 const enum lttng_trigger_status status =
297 lttng_trigger_set_firing_policy(trigger,
298 firing_policy,
299 firing_policy_threshold);
300
301 if (status != LTTNG_TRIGGER_STATUS_OK) {
302 ret = -1;
303 goto end;
304 }
305 }
306
307 ret = offset;
308
309 error:
310 lttng_condition_put(condition);
311 lttng_action_put(action);
312 end:
313 if (ret >= 0) {
314 *_trigger = trigger;
315 } else {
316 lttng_trigger_put(trigger);
317 }
318
319 return ret;
320 }
321
322 /*
323 * Both elements are stored contiguously, see their "*_comm" structure
324 * for the detailed format.
325 */
326 LTTNG_HIDDEN
327 int lttng_trigger_serialize(const struct lttng_trigger *trigger,
328 struct lttng_payload *payload)
329 {
330 int ret;
331 size_t header_offset, size_before_payload, size_name;
332 struct lttng_trigger_comm trigger_comm = {};
333 struct lttng_trigger_comm *header;
334 const struct lttng_credentials *creds = NULL;
335
336 creds = lttng_trigger_get_credentials(trigger);
337 assert(creds);
338
339 trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
340
341 if (trigger->name != NULL) {
342 size_name = strlen(trigger->name) + 1;
343 } else {
344 size_name = 0;
345 }
346
347 trigger_comm.name_length = size_name;
348 trigger_comm.firing_policy_type = (uint8_t) trigger->firing_policy.type;
349 trigger_comm.firing_policy_threshold = (uint64_t) trigger->firing_policy.threshold;
350
351 header_offset = payload->buffer.size;
352 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
353 sizeof(trigger_comm));
354 if (ret) {
355 goto end;
356 }
357
358 size_before_payload = payload->buffer.size;
359
360 /* Trigger name. */
361 ret = lttng_dynamic_buffer_append(
362 &payload->buffer, trigger->name, size_name);
363 if (ret) {
364 goto end;
365 }
366
367 ret = lttng_condition_serialize(trigger->condition, payload);
368 if (ret) {
369 goto end;
370 }
371
372 ret = lttng_action_serialize(trigger->action, payload);
373 if (ret) {
374 goto end;
375 }
376
377 /* Update payload size. */
378 header = (typeof(header)) (payload->buffer.data + header_offset);
379 header->length = payload->buffer.size - size_before_payload;
380 end:
381 return ret;
382 }
383
384 LTTNG_HIDDEN
385 bool lttng_trigger_is_equal(
386 const struct lttng_trigger *a, const struct lttng_trigger *b)
387 {
388 if (a->firing_policy.type != b->firing_policy.type) {
389 return false;
390 }
391
392 if (a->firing_policy.threshold != b->firing_policy.threshold) {
393 return false;
394 }
395
396 /*
397 * Name is not taken into account since it is cosmetic only.
398 */
399 if (!lttng_condition_is_equal(a->condition, b->condition)) {
400 return false;
401 }
402
403 if (!lttng_action_is_equal(a->action, b->action)) {
404 return false;
405 }
406
407 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a),
408 lttng_trigger_get_credentials(b))) {
409 return false;
410 }
411
412 return true;
413 }
414
415 enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
416 const char* name)
417 {
418 char *name_copy = NULL;
419 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
420
421 if (!trigger || !name ||
422 strlen(name) == 0) {
423 status = LTTNG_TRIGGER_STATUS_INVALID;
424 goto end;
425 }
426
427 name_copy = strdup(name);
428 if (!name_copy) {
429 status = LTTNG_TRIGGER_STATUS_ERROR;
430 goto end;
431 }
432
433 free(trigger->name);
434
435 trigger->name = name_copy;
436 name_copy = NULL;
437 end:
438 return status;
439 }
440
441 enum lttng_trigger_status lttng_trigger_get_name(
442 const struct lttng_trigger *trigger, const char **name)
443 {
444 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
445
446 if (!trigger || !name) {
447 status = LTTNG_TRIGGER_STATUS_INVALID;
448 goto end;
449 }
450
451 if (!trigger->name) {
452 status = LTTNG_TRIGGER_STATUS_UNSET;
453 }
454
455 *name = trigger->name;
456 end:
457 return status;
458 }
459
460 LTTNG_HIDDEN
461 int lttng_trigger_assign_name(struct lttng_trigger *dst,
462 const struct lttng_trigger *src)
463 {
464 int ret = 0;
465 enum lttng_trigger_status status;
466
467 status = lttng_trigger_set_name(dst, src->name);
468 if (status != LTTNG_TRIGGER_STATUS_OK) {
469 ret = -1;
470 ERR("Failed to set name for trigger");
471 goto end;
472 }
473 end:
474 return ret;
475 }
476
477 LTTNG_HIDDEN
478 void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
479 uint64_t token)
480 {
481 assert(trigger);
482 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
483 }
484
485 LTTNG_HIDDEN
486 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
487 {
488 assert(trigger);
489
490 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
491 }
492
493 LTTNG_HIDDEN
494 int lttng_trigger_generate_name(struct lttng_trigger *trigger,
495 uint64_t unique_id)
496 {
497 int ret = 0;
498 char *generated_name = NULL;
499
500 ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id);
501 if (ret < 0) {
502 ERR("Failed to generate trigger name");
503 ret = -1;
504 goto end;
505 }
506
507 ret = 0;
508 free(trigger->name);
509 trigger->name = generated_name;
510 end:
511 return ret;
512 }
513
514 LTTNG_HIDDEN
515 void lttng_trigger_get(struct lttng_trigger *trigger)
516 {
517 urcu_ref_get(&trigger->ref);
518 }
519
520 LTTNG_HIDDEN
521 void lttng_trigger_put(struct lttng_trigger *trigger)
522 {
523 if (!trigger) {
524 return;
525 }
526
527 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
528 }
529
530 static void delete_trigger_array_element(void *ptr)
531 {
532 struct lttng_trigger *trigger = ptr;
533
534 lttng_trigger_put(trigger);
535 }
536
537 LTTNG_HIDDEN
538 struct lttng_triggers *lttng_triggers_create(void)
539 {
540 struct lttng_triggers *triggers = NULL;
541
542 triggers = zmalloc(sizeof(*triggers));
543 if (!triggers) {
544 goto end;
545 }
546
547 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
548
549 end:
550 return triggers;
551 }
552
553 LTTNG_HIDDEN
554 struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
555 const struct lttng_triggers *triggers, unsigned int index)
556 {
557 struct lttng_trigger *trigger = NULL;
558
559 assert(triggers);
560 if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
561 goto end;
562 }
563
564 trigger = (struct lttng_trigger *)
565 lttng_dynamic_pointer_array_get_pointer(
566 &triggers->array, index);
567 end:
568 return trigger;
569 }
570
571 LTTNG_HIDDEN
572 int lttng_triggers_add(
573 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
574 {
575 int ret;
576
577 assert(triggers);
578 assert(trigger);
579
580 lttng_trigger_get(trigger);
581
582 ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
583 if (ret) {
584 lttng_trigger_put(trigger);
585 }
586
587 return ret;
588 }
589
590 const struct lttng_trigger *lttng_triggers_get_at_index(
591 const struct lttng_triggers *triggers, unsigned int index)
592 {
593 return lttng_triggers_borrow_mutable_at_index(triggers, index);
594 }
595
596 enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
597 {
598 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
599
600 if (!triggers || !count) {
601 status = LTTNG_TRIGGER_STATUS_INVALID;
602 goto end;
603 }
604
605 *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
606 end:
607 return status;
608 }
609
610 void lttng_triggers_destroy(struct lttng_triggers *triggers)
611 {
612 if (!triggers) {
613 return;
614 }
615
616 lttng_dynamic_pointer_array_reset(&triggers->array);
617 free(triggers);
618 }
619
620 int lttng_triggers_serialize(const struct lttng_triggers *triggers,
621 struct lttng_payload *payload)
622 {
623 int ret;
624 unsigned int i, count;
625 size_t size_before_payload;
626 struct lttng_triggers_comm triggers_comm = {};
627 struct lttng_triggers_comm *header;
628 enum lttng_trigger_status status;
629 const size_t header_offset = payload->buffer.size;
630
631 status = lttng_triggers_get_count(triggers, &count);
632 if (status != LTTNG_TRIGGER_STATUS_OK) {
633 ret = LTTNG_ERR_INVALID;
634 goto end;
635 }
636
637 triggers_comm.count = count;
638
639 /* Placeholder header; updated at the end. */
640 ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
641 sizeof(triggers_comm));
642 if (ret) {
643 goto end;
644 }
645
646 size_before_payload = payload->buffer.size;
647
648 for (i = 0; i < count; i++) {
649 const struct lttng_trigger *trigger =
650 lttng_triggers_get_at_index(triggers, i);
651
652 assert(trigger);
653
654 ret = lttng_trigger_serialize(trigger, payload);
655 if (ret) {
656 goto end;
657 }
658 }
659
660 /* Update payload size. */
661 header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
662 header->length = payload->buffer.size - size_before_payload;
663 end:
664 return ret;
665 }
666
667 LTTNG_HIDDEN
668 ssize_t lttng_triggers_create_from_payload(
669 struct lttng_payload_view *src_view,
670 struct lttng_triggers **triggers)
671 {
672 ssize_t ret, offset = 0, triggers_size = 0;
673 unsigned int i;
674 const struct lttng_triggers_comm *triggers_comm;
675 struct lttng_triggers *local_triggers = NULL;
676
677 if (!src_view || !triggers) {
678 ret = -1;
679 goto error;
680 }
681
682 /* lttng_trigger_comms header */
683 triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
684 offset += sizeof(*triggers_comm);
685
686 local_triggers = lttng_triggers_create();
687 if (!local_triggers) {
688 ret = -1;
689 goto error;
690 }
691
692 for (i = 0; i < triggers_comm->count; i++) {
693 struct lttng_trigger *trigger = NULL;
694 struct lttng_payload_view trigger_view =
695 lttng_payload_view_from_view(src_view, offset, -1);
696 ssize_t trigger_size;
697
698 trigger_size = lttng_trigger_create_from_payload(
699 &trigger_view, &trigger);
700 if (trigger_size < 0) {
701 ret = trigger_size;
702 goto error;
703 }
704
705 /* Transfer ownership of the trigger to the collection. */
706 ret = lttng_triggers_add(local_triggers, trigger);
707 lttng_trigger_put(trigger);
708 if (ret < 0) {
709 ret = -1;
710 goto error;
711 }
712
713 offset += trigger_size;
714 triggers_size += trigger_size;
715 }
716
717 /* Unexpected size of inner-elements; the buffer is corrupted. */
718 if ((ssize_t) triggers_comm->length != triggers_size) {
719 ret = -1;
720 goto error;
721 }
722
723 /* Pass ownership to caller. */
724 *triggers = local_triggers;
725 local_triggers = NULL;
726
727 ret = offset;
728 error:
729
730 lttng_triggers_destroy(local_triggers);
731 return ret;
732 }
733
734 LTTNG_HIDDEN
735 const struct lttng_credentials *lttng_trigger_get_credentials(
736 const struct lttng_trigger *trigger)
737 {
738 return &trigger->creds;
739 }
740
741 LTTNG_HIDDEN
742 void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
743 const struct lttng_credentials *creds)
744 {
745 assert(creds);
746 trigger->creds = *creds;
747 }
748
749 enum lttng_trigger_status lttng_trigger_set_owner_uid(
750 struct lttng_trigger *trigger, uid_t uid)
751 {
752 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
753 const struct lttng_credentials creds = {
754 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
755 .gid = LTTNG_OPTIONAL_INIT_UNSET,
756 };
757
758 if (!trigger) {
759 ret = LTTNG_TRIGGER_STATUS_INVALID;
760 goto end;
761 }
762
763 /* Client-side validation only to report a clearer error. */
764 if (geteuid() != 0) {
765 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
766 goto end;
767 }
768
769 lttng_trigger_set_credentials(trigger, &creds);
770
771 end:
772 return ret;
773 }
774
775 enum lttng_trigger_status lttng_trigger_get_owner_uid(
776 const struct lttng_trigger *trigger, uid_t *uid)
777 {
778 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
779 const struct lttng_credentials *creds = NULL;
780
781 if (!trigger || !uid ) {
782 ret = LTTNG_TRIGGER_STATUS_INVALID;
783 goto end;
784 }
785
786 if (!trigger->creds.uid.is_set ) {
787 ret = LTTNG_TRIGGER_STATUS_UNSET;
788 goto end;
789 }
790
791 creds = lttng_trigger_get_credentials(trigger);
792 *uid = lttng_credentials_get_uid(creds);
793
794 end:
795 return ret;
796 }
797
798 enum lttng_trigger_status lttng_trigger_set_firing_policy(
799 struct lttng_trigger *trigger,
800 enum lttng_trigger_firing_policy policy_type,
801 uint64_t threshold)
802 {
803 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
804 assert(trigger);
805
806 if (threshold < 1) {
807 ret = LTTNG_TRIGGER_STATUS_INVALID;
808 goto end;
809 }
810
811 trigger->firing_policy.type = policy_type;
812 trigger->firing_policy.threshold = threshold;
813
814 end:
815 return ret;
816 }
817
818 enum lttng_trigger_status lttng_trigger_get_firing_policy(
819 const struct lttng_trigger *trigger,
820 enum lttng_trigger_firing_policy *policy_type,
821 uint64_t *threshold)
822 {
823 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
824
825 if (!trigger || !policy_type || !threshold) {
826 status = LTTNG_TRIGGER_STATUS_INVALID;
827 goto end;
828 }
829
830 *policy_type = trigger->firing_policy.type;
831 *threshold = trigger->firing_policy.threshold;
832
833 end:
834 return status;
835 }
836
837 LTTNG_HIDDEN
838 bool lttng_trigger_should_fire(const struct lttng_trigger *trigger)
839 {
840 bool ready_to_fire = false;
841
842 assert(trigger);
843
844 switch (trigger->firing_policy.type) {
845 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N:
846 if (trigger->firing_policy.current_count < trigger->firing_policy.threshold) {
847 ready_to_fire = true;
848 }
849 break;
850 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N:
851 if (trigger->firing_policy.current_count < trigger->firing_policy.threshold) {
852 ready_to_fire = true;
853 }
854 break;
855 default:
856 abort();
857 };
858
859 return ready_to_fire;
860 }
861
862 LTTNG_HIDDEN
863 void lttng_trigger_fire(struct lttng_trigger *trigger)
864 {
865 assert(trigger);
866
867 trigger->firing_policy.current_count++;
868
869 switch (trigger->firing_policy.type) {
870 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N:
871 if (trigger->firing_policy.current_count == trigger->firing_policy.threshold) {
872 trigger->firing_policy.current_count = 0;
873 }
874
875 break;
876 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N:
877 /*
878 * TODO:
879 * As an optimisation, deactivate the trigger condition and
880 * remove any checks in the traced application or kernel since
881 * the trigger will never fire again.
882 */
883 break;
884 default:
885 abort();
886 };
887 }
888
889 LTTNG_HIDDEN
890 enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
891 const struct lttng_trigger *trigger)
892 {
893 enum lttng_domain_type type = LTTNG_DOMAIN_NONE;
894 const struct lttng_event_rule *event_rule;
895 enum lttng_condition_status c_status;
896 enum lttng_condition_type c_type;
897
898 assert(trigger);
899 assert(trigger->condition);
900
901 c_type = lttng_condition_get_type(trigger->condition);
902 assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN);
903
904 switch (c_type) {
905 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
906 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
907 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
908 /* Apply to any domain. */
909 type = LTTNG_DOMAIN_NONE;
910 break;
911 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
912 /* Return the domain of the event rule. */
913 c_status = lttng_condition_event_rule_get_rule(
914 trigger->condition, &event_rule);
915 assert(c_status == LTTNG_CONDITION_STATUS_OK);
916 type = lttng_event_rule_get_domain_type(event_rule);
917 break;
918 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
919 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
920 /* Return the domain of the channel being monitored. */
921 c_status = lttng_condition_buffer_usage_get_domain_type(
922 trigger->condition, &type);
923 assert(c_status == LTTNG_CONDITION_STATUS_OK);
924 break;
925 default:
926 abort();
927 }
928
929 return type;
930 }
This page took 0.050239 seconds and 4 git commands to generate.