4 * Tests suite for anonymous, named, and automatic name triggers.
6 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * SPDX-License-Identifier: MIT
17 #include <lttng/lttng.h>
18 #include <common/macros.h>
22 enum unregistration_trigger_instance
{
23 UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
,
24 UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
,
27 typedef void (*test_function
)(enum unregistration_trigger_instance
);
30 const char *get_trigger_name(const struct lttng_trigger
*trigger
)
32 const char *trigger_name
;
33 enum lttng_trigger_status trigger_status
;
35 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
36 switch (trigger_status
) {
37 case LTTNG_TRIGGER_STATUS_OK
:
39 case LTTNG_TRIGGER_STATUS_UNSET
:
40 trigger_name
= "(anonymous)";
43 trigger_name
= "(failed to get name)";
51 const char *unregistration_trigger_instance_name(
52 enum unregistration_trigger_instance unregistration_trigger
)
56 switch (unregistration_trigger
) {
57 case UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
:
58 name
= "from listing";
60 case UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
:
61 name
= "used for registration";
71 * Returns a negative error code on error, else the number of unregistered
75 int unregister_all_triggers(void)
78 enum lttng_error_code ret_code
;
79 enum lttng_trigger_status trigger_status
;
80 struct lttng_triggers
*triggers
= NULL
;
81 unsigned int trigger_count
, i
, unregistered_trigger_count
= 0;
83 ret_code
= lttng_list_triggers(&triggers
);
84 if (ret_code
!= LTTNG_OK
) {
85 fail("Failed to list triggers");
90 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
91 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
92 fail("Failed to get count of triggers returned by listing");
97 for (i
= 0; i
< trigger_count
; i
++) {
98 const struct lttng_trigger
*trigger
;
100 trigger
= lttng_triggers_get_at_index(triggers
, i
);
101 LTTNG_ASSERT(trigger
);
103 ret
= lttng_unregister_trigger(trigger
);
106 enum lttng_trigger_status get_name_status
=
107 lttng_trigger_get_name(trigger
, &name
);
108 if (get_name_status
== LTTNG_TRIGGER_STATUS_OK
) {
109 fail("Failed to unregister trigger: trigger name = '%s'", name
);
111 fail("Failed to unregister trigger");
116 unregistered_trigger_count
++;
119 ret
= (int) unregistered_trigger_count
;
122 lttng_triggers_destroy(triggers
);
127 int get_registered_triggers_count(void)
130 enum lttng_error_code ret_code
;
131 enum lttng_trigger_status trigger_status
;
132 struct lttng_triggers
*triggers
= NULL
;
133 unsigned int trigger_count
;
135 ret_code
= lttng_list_triggers(&triggers
);
136 if (ret_code
!= LTTNG_OK
) {
137 fail("Failed to list triggers");
142 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
143 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
144 fail("Failed to get count of triggers returned by listing");
149 ret
= (int) trigger_count
;
152 lttng_triggers_destroy(triggers
);
157 * Create a generic trigger. The specifics of the condition and action are not
158 * important for the purposes of this test.
161 struct lttng_trigger
*create_trigger(uint64_t threshold
)
163 struct lttng_condition
*condition
= NULL
;
164 struct lttng_action
*action
= NULL
;
165 struct lttng_trigger
*trigger
= NULL
;
166 enum lttng_condition_status condition_status
;
167 const char * const session_name
= "test session";
169 condition
= lttng_condition_session_consumed_size_create();
171 fail("Failed to create 'session consumed size' condition");
175 condition_status
= lttng_condition_session_consumed_size_set_session_name(condition
, session_name
);
176 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
177 fail("Failed to set session name on 'session consumed size' condition");
181 condition_status
= lttng_condition_session_consumed_size_set_threshold(
182 condition
, threshold
);
183 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
184 fail("Failed to set threshold on 'session consumed size' condition");
188 action
= lttng_action_notify_create();
190 fail("Failed to create 'notify' action");
194 trigger
= lttng_trigger_create(condition
, action
);
196 fail("Failed to create trigger");
201 lttng_condition_destroy(condition
);
202 lttng_action_destroy(action
);
207 void register_anonymous_trigger(
208 enum unregistration_trigger_instance unregistration_trigger
)
211 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
212 enum lttng_trigger_status trigger_status
;
213 const char *trigger_name
;
214 struct lttng_triggers
*triggers
= NULL
;
215 unsigned int trigger_count
, i
;
216 enum lttng_error_code ret_code
;
218 diag("Register an anonymous trigger (Unregistration performed with the trigger instance %s)",
219 unregistration_trigger_instance_name(
220 unregistration_trigger
));
223 fail("Failed to create trigger");
227 ret
= lttng_register_trigger(trigger
);
228 ok(ret
== 0, "Registered anonymous trigger");
230 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
231 ok(trigger_status
== LTTNG_TRIGGER_STATUS_UNSET
,
232 "Anonymous trigger name remains unset after registration: trigger name = '%s'",
233 get_trigger_name(trigger
));
235 ret_code
= lttng_list_triggers(&triggers
);
236 if (ret_code
!= LTTNG_OK
) {
237 fail("Failed to list triggers");
242 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
243 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
244 fail("Failed to get count of triggers returned by listing");
249 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
251 for (i
= 0; i
< trigger_count
; i
++) {
252 const struct lttng_trigger
*trigger_from_listing
;
254 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
255 LTTNG_ASSERT(trigger_from_listing
);
257 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &trigger_name
);
258 ok(trigger_status
== LTTNG_TRIGGER_STATUS_UNSET
,
259 "Anonymous trigger returned by listing has an unset name: trigger name = '%s'",
260 get_trigger_name(trigger_from_listing
));
262 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
263 ret
= lttng_unregister_trigger(trigger_from_listing
);
264 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance returned by the listing");
268 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
269 ret
= lttng_unregister_trigger(trigger
);
270 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance used on registration");
274 lttng_triggers_destroy(triggers
);
275 lttng_trigger_destroy(trigger
);
279 void register_named_trigger(
280 enum unregistration_trigger_instance unregistration_trigger
)
283 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
284 enum lttng_trigger_status trigger_status
;
285 const char *returned_trigger_name
;
286 struct lttng_triggers
*triggers
= NULL
;
287 unsigned int trigger_count
, i
;
288 enum lttng_error_code ret_code
;
289 const char * const trigger_name
= "some name that is hopefully unique";
291 diag("Register a named trigger (Unregistration performed with the trigger instance %s)",
292 unregistration_trigger_instance_name(
293 unregistration_trigger
));
296 fail("Failed to create trigger");
300 ret_code
= lttng_register_trigger_with_name(trigger
, trigger_name
);
301 ok(ret_code
== LTTNG_OK
, "Registered trigger with name: trigger name = '%s'",
302 get_trigger_name(trigger
));
304 trigger_status
= lttng_trigger_get_name(trigger
, &returned_trigger_name
);
305 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
306 "Trigger name is set after registration: trigger name = '%s'",
307 get_trigger_name(trigger
));
309 ok(!strcmp(get_trigger_name(trigger
), trigger_name
),
310 "Name set on trigger after registration is correct");
312 ret_code
= lttng_list_triggers(&triggers
);
313 if (ret_code
!= LTTNG_OK
) {
314 fail("Failed to list triggers");
319 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
320 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
321 fail("Failed to get count of triggers returned by listing");
326 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
328 for (i
= 0; i
< trigger_count
; i
++) {
329 const struct lttng_trigger
*trigger_from_listing
;
331 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
332 LTTNG_ASSERT(trigger_from_listing
);
334 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &returned_trigger_name
);
335 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
336 "Trigger returned by listing has a name: trigger name = '%s'",
337 get_trigger_name(trigger_from_listing
));
339 ok(!strcmp(get_trigger_name(trigger_from_listing
),
341 "Name set on trigger returned from listing is correct: name returned from listing = '%s', expected name = '%s'",
342 get_trigger_name(trigger_from_listing
),
345 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
346 ret
= lttng_unregister_trigger(trigger_from_listing
);
347 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance returned by the listing");
351 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
352 ret
= lttng_unregister_trigger(trigger
);
353 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance used on registration");
357 lttng_triggers_destroy(triggers
);
358 lttng_trigger_destroy(trigger
);
362 void register_automatic_name_trigger(
363 enum unregistration_trigger_instance unregistration_trigger
)
366 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
367 enum lttng_trigger_status trigger_status
;
368 const char *returned_trigger_name
;
369 struct lttng_triggers
*triggers
= NULL
;
370 unsigned int trigger_count
, i
;
371 enum lttng_error_code ret_code
;
373 diag("Register an automatic name trigger (Unregistration performed with the trigger instance %s)",
374 unregistration_trigger_instance_name(
375 unregistration_trigger
));
378 fail("Failed to create trigger");
382 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
383 ok(ret_code
== LTTNG_OK
, "Registered trigger with automatic name");
385 trigger_status
= lttng_trigger_get_name(trigger
, &returned_trigger_name
);
386 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
387 "Trigger name is set after registration: trigger name = '%s'",
388 get_trigger_name(trigger
));
390 ok(returned_trigger_name
&& strlen(returned_trigger_name
) > 0,
391 "Automatic name set on trigger after registration longer is not an empty string");
393 ret_code
= lttng_list_triggers(&triggers
);
394 if (ret_code
!= LTTNG_OK
) {
395 fail("Failed to list triggers");
400 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
401 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
402 fail("Failed to get count of triggers returned by listing");
407 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
409 for (i
= 0; i
< trigger_count
; i
++) {
410 const struct lttng_trigger
*trigger_from_listing
;
412 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
413 LTTNG_ASSERT(trigger_from_listing
);
415 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &returned_trigger_name
);
416 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
417 "Trigger returned by listing has a name: trigger name = '%s'",
418 get_trigger_name(trigger_from_listing
));
420 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
421 ret
= lttng_unregister_trigger(trigger_from_listing
);
422 ok(ret
== 0, "Successfully unregistered automatic name trigger using the trigger instance returned by the listing");
426 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
427 ret
= lttng_unregister_trigger(trigger
);
428 ok(ret
== 0, "Successfully unregistered automatic trigger using the trigger instance used on registration");
432 lttng_triggers_destroy(triggers
);
433 lttng_trigger_destroy(trigger
);
437 void double_register_anonymous_trigger(
438 enum unregistration_trigger_instance unregistration_trigger
)
441 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
442 struct lttng_triggers
*triggers
= NULL
;
444 diag("Register duplicate anonymous trigger (Unregistration performed with the trigger instance %s)",
445 unregistration_trigger_instance_name(
446 unregistration_trigger
));
449 fail("Failed to create trigger");
453 ret
= lttng_register_trigger(trigger
);
454 ok(ret
== 0, "Registered anonymous trigger");
456 ret
= lttng_register_trigger(trigger
);
457 ok(ret
== -LTTNG_ERR_TRIGGER_EXISTS
,
458 "Registering identical anonymous trigger fails with `LTTNG_ERR_TRIGGER_EXISTS`");
461 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
462 ret
= lttng_unregister_trigger(trigger
);
463 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance used on registration");
465 ok(get_registered_triggers_count() == 1,
466 "Trigger listing returns 1 trigger");
467 ok(unregister_all_triggers() == 1,
468 "Successfully unregistered anonymous trigger using the trigger instance returned by the listing");
472 lttng_triggers_destroy(triggers
);
473 lttng_trigger_destroy(trigger
);
477 void double_register_named_trigger(
478 enum unregistration_trigger_instance unregistration_trigger
)
481 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
482 struct lttng_trigger
*trigger_b
= create_trigger(0xbadc0ffee);
483 struct lttng_triggers
*triggers
= NULL
;
484 const char * const trigger_name
= "a unique trigger name";
485 enum lttng_error_code ret_code
;
487 diag("Register duplicate named trigger (Unregistration performed with the trigger instance %s)",
488 unregistration_trigger_instance_name(
489 unregistration_trigger
));
491 if (!trigger_a
|| !trigger_b
) {
492 fail("Failed to create triggers");
496 ret_code
= lttng_register_trigger_with_name(trigger_a
, trigger_name
);
497 ok(ret_code
== LTTNG_OK
, "Registered named trigger");
499 ret
= lttng_register_trigger(trigger_a
);
500 ok(ret
== -LTTNG_ERR_INVALID
,
501 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (anonymous registration)");
503 ret_code
= lttng_register_trigger_with_name(trigger_a
, trigger_name
);
504 ok(ret_code
== LTTNG_ERR_INVALID
,
505 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (register with name)");
507 ret_code
= lttng_register_trigger_with_automatic_name(trigger_a
);
508 ok(ret_code
== LTTNG_ERR_INVALID
,
509 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (register with automatic name)");
511 ret_code
= lttng_register_trigger_with_name(trigger_b
, trigger_name
);
512 ok(ret_code
== LTTNG_ERR_TRIGGER_EXISTS
, "Registering trigger with an already used name fails with `LTTNG_ERR_TRIGGER_EXISTS`");
514 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
515 ret
= lttng_unregister_trigger(trigger_a
);
516 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance used on registration");
518 ok(get_registered_triggers_count() == 1,
519 "Trigger listing returns 1 trigger");
520 ok(unregister_all_triggers() == 1,
521 "Successfully unregistered named trigger using the trigger instance returned by the listing");
525 lttng_triggers_destroy(triggers
);
526 lttng_trigger_destroy(trigger_a
);
527 lttng_trigger_destroy(trigger_b
);
531 void double_register_automatic_name_trigger(
532 enum unregistration_trigger_instance unregistration_trigger
)
535 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
536 struct lttng_trigger
*trigger_b
= create_trigger(0xbadc0ffee);
537 struct lttng_triggers
*triggers
= NULL
;
538 enum lttng_error_code ret_code
;
540 diag("Register duplicate automatic name trigger (Unregistration performed with the trigger instance %s)",
541 unregistration_trigger_instance_name(
542 unregistration_trigger
));
544 if (!trigger_a
|| !trigger_b
) {
545 fail("Failed to create triggers");
549 ret_code
= lttng_register_trigger_with_automatic_name(trigger_a
);
550 ok(ret_code
== LTTNG_OK
, "Registered automatic name trigger: trigger name = '%s'", get_trigger_name(trigger_a
));
552 ret
= lttng_register_trigger_with_automatic_name(trigger_b
);
553 ok(ret_code
== LTTNG_OK
, "Registering an identical trigger instance with an automatic name succeeds: trigger name = '%s'", get_trigger_name(trigger_b
));
555 ok(strcmp(get_trigger_name(trigger_a
), get_trigger_name(trigger_b
)),
556 "Two identical triggers registered with an automatic name have different names");
558 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
559 ret
= lttng_unregister_trigger(trigger_a
);
560 ok(ret
== 0, "Successfully unregistered automatic trigger A using the trigger instance used on registration");
562 ret
= lttng_unregister_trigger(trigger_b
);
563 ok(ret
== 0, "Successfully unregistered automatic trigger B using the trigger instance used on registration");
565 ok(get_registered_triggers_count() == 2,
566 "Trigger listing returns 2 trigger");
567 ok(unregister_all_triggers() == 2,
568 "Successfully unregistered automatic name triggers using the trigger instance returned by the listing");
572 lttng_triggers_destroy(triggers
);
573 lttng_trigger_destroy(trigger_a
);
574 lttng_trigger_destroy(trigger_b
);
578 void register_multiple_anonymous_triggers(void)
581 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
582 struct lttng_trigger
*trigger_b
= create_trigger(0xbadf00d);
584 diag("Register two different anonymous triggers");
586 if (!trigger_a
|| !trigger_b
) {
587 fail("Failed to create triggers");
591 ret
= lttng_register_trigger(trigger_a
);
592 ok(ret
== 0, "Registered first anonymous trigger");
594 ret
= lttng_register_trigger(trigger_b
);
595 ok(ret
== 0, "Registered second anonymous trigger");
597 ok(get_registered_triggers_count() == 2,
598 "Trigger listing returns 2 trigger");
599 ok(unregister_all_triggers() == 2,
600 "Successfully unregistered two anonymous triggers");
603 lttng_trigger_destroy(trigger_a
);
604 lttng_trigger_destroy(trigger_b
);
607 const test_function test_functions
[] = {
608 register_anonymous_trigger
,
609 register_named_trigger
,
610 register_automatic_name_trigger
,
611 double_register_anonymous_trigger
,
612 double_register_named_trigger
,
613 double_register_automatic_name_trigger
,
616 int main(int argc
, const char **argv
)
620 plan_tests(TEST_COUNT
);
622 if (get_registered_triggers_count() != 0) {
623 fail("Session daemon already has registered triggers, bailing out");
627 for (i
= 0; i
< ARRAY_SIZE(test_functions
); i
++) {
628 const test_function fn
= test_functions
[i
];
630 fn(UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
);
631 if (get_registered_triggers_count() != 0) {
632 fail("Previous test left registered triggers, bailing out");
637 for (i
= 0; i
< ARRAY_SIZE(test_functions
); i
++) {
638 const test_function fn
= test_functions
[i
];
640 fn(UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
);
641 if (get_registered_triggers_count() != 0) {
642 fail("Previous test left registered triggers, bailing out");
647 register_multiple_anonymous_triggers();
649 return exit_status();