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
18 #include <lttng/lttng.h>
19 #include <common/macros.h>
23 enum unregistration_trigger_instance
{
24 UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
,
25 UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
,
28 typedef void (*test_function
)(enum unregistration_trigger_instance
);
31 const char *get_trigger_name(const struct lttng_trigger
*trigger
)
33 const char *trigger_name
;
34 enum lttng_trigger_status trigger_status
;
36 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
37 switch (trigger_status
) {
38 case LTTNG_TRIGGER_STATUS_OK
:
40 case LTTNG_TRIGGER_STATUS_UNSET
:
41 trigger_name
= "(anonymous)";
44 trigger_name
= "(failed to get name)";
52 const char *unregistration_trigger_instance_name(
53 enum unregistration_trigger_instance unregistration_trigger
)
57 switch (unregistration_trigger
) {
58 case UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
:
59 name
= "from listing";
61 case UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
:
62 name
= "used for registration";
72 * Returns a negative error code on error, else the number of unregistered
76 int unregister_all_triggers(void)
79 enum lttng_error_code ret_code
;
80 enum lttng_trigger_status trigger_status
;
81 struct lttng_triggers
*triggers
= NULL
;
82 unsigned int trigger_count
, i
, unregistered_trigger_count
= 0;
84 ret_code
= lttng_list_triggers(&triggers
);
85 if (ret_code
!= LTTNG_OK
) {
86 fail("Failed to list triggers");
91 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
92 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
93 fail("Failed to get count of triggers returned by listing");
98 for (i
= 0; i
< trigger_count
; i
++) {
99 const struct lttng_trigger
*trigger
;
101 trigger
= lttng_triggers_get_at_index(triggers
, i
);
104 ret
= lttng_unregister_trigger(trigger
);
106 fail("Failed to unregister trigger: trigger name = '%s'");
110 unregistered_trigger_count
++;
113 ret
= (int) unregistered_trigger_count
;
116 lttng_triggers_destroy(triggers
);
121 int get_registered_triggers_count(void)
124 enum lttng_error_code ret_code
;
125 enum lttng_trigger_status trigger_status
;
126 struct lttng_triggers
*triggers
= NULL
;
127 unsigned int trigger_count
;
129 ret_code
= lttng_list_triggers(&triggers
);
130 if (ret_code
!= LTTNG_OK
) {
131 fail("Failed to list triggers");
136 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
137 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
138 fail("Failed to get count of triggers returned by listing");
143 ret
= (int) trigger_count
;
146 lttng_triggers_destroy(triggers
);
151 * Create a generic trigger. The specifics of the condition and action are not
152 * important for the purposes of this test.
155 struct lttng_trigger
*create_trigger(uint64_t threshold
)
157 struct lttng_condition
*condition
= NULL
;
158 struct lttng_action
*action
= NULL
;
159 struct lttng_trigger
*trigger
= NULL
;
160 enum lttng_condition_status condition_status
;
161 const char * const session_name
= "test session";
163 condition
= lttng_condition_session_consumed_size_create();
165 fail("Failed to create 'session consumed size' condition");
169 condition_status
= lttng_condition_session_consumed_size_set_session_name(condition
, session_name
);
170 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
171 fail("Failed to set session name on 'session consumed size' condition");
175 condition_status
= lttng_condition_session_consumed_size_set_threshold(
176 condition
, threshold
);
177 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
178 fail("Failed to set threshold on 'session consumed size' condition");
182 action
= lttng_action_notify_create();
184 fail("Failed to create 'notify' action");
188 trigger
= lttng_trigger_create(condition
, action
);
190 fail("Failed to create trigger");
195 lttng_condition_destroy(condition
);
196 lttng_action_destroy(action
);
201 void register_anonymous_trigger(
202 enum unregistration_trigger_instance unregistration_trigger
)
205 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
206 enum lttng_trigger_status trigger_status
;
207 const char *trigger_name
;
208 struct lttng_triggers
*triggers
= NULL
;
209 unsigned int trigger_count
, i
;
210 enum lttng_error_code ret_code
;
212 diag("Register an anonymous trigger (Unregistration performed with the trigger instance %s)",
213 unregistration_trigger_instance_name(
214 unregistration_trigger
));
217 fail("Failed to create trigger");
221 ret
= lttng_register_trigger(trigger
);
222 ok(ret
== 0, "Registered anonymous trigger");
224 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
225 ok(trigger_status
== LTTNG_TRIGGER_STATUS_UNSET
,
226 "Anonymous trigger name remains unset after registration: trigger name = '%s'",
227 get_trigger_name(trigger
));
229 ret_code
= lttng_list_triggers(&triggers
);
230 if (ret_code
!= LTTNG_OK
) {
231 fail("Failed to list triggers");
236 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
237 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
238 fail("Failed to get count of triggers returned by listing");
243 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
245 for (i
= 0; i
< trigger_count
; i
++) {
246 const struct lttng_trigger
*trigger_from_listing
;
248 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
249 assert(trigger_from_listing
);
251 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &trigger_name
);
252 ok(trigger_status
== LTTNG_TRIGGER_STATUS_UNSET
,
253 "Anonymous trigger returned by listing has an unset name: trigger name = '%s'",
254 get_trigger_name(trigger_from_listing
));
256 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
257 ret
= lttng_unregister_trigger(trigger_from_listing
);
258 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance returned by the listing");
262 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
263 ret
= lttng_unregister_trigger(trigger
);
264 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance used on registration");
268 lttng_triggers_destroy(triggers
);
269 lttng_trigger_destroy(trigger
);
273 void register_named_trigger(
274 enum unregistration_trigger_instance unregistration_trigger
)
277 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
278 enum lttng_trigger_status trigger_status
;
279 const char *returned_trigger_name
;
280 struct lttng_triggers
*triggers
= NULL
;
281 unsigned int trigger_count
, i
;
282 enum lttng_error_code ret_code
;
283 const char * const trigger_name
= "some name that is hopefully unique";
285 diag("Register a named trigger (Unregistration performed with the trigger instance %s)",
286 unregistration_trigger_instance_name(
287 unregistration_trigger
));
290 fail("Failed to create trigger");
294 ret_code
= lttng_register_trigger_with_name(trigger
, trigger_name
);
295 ok(ret_code
== LTTNG_OK
, "Registered trigger with name: trigger name = '%s'",
296 get_trigger_name(trigger
));
298 trigger_status
= lttng_trigger_get_name(trigger
, &returned_trigger_name
);
299 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
300 "Trigger name is set after registration: trigger name = '%s'",
301 get_trigger_name(trigger
));
303 ok(!strcmp(get_trigger_name(trigger
), trigger_name
),
304 "Name set on trigger after registration is correct");
306 ret_code
= lttng_list_triggers(&triggers
);
307 if (ret_code
!= LTTNG_OK
) {
308 fail("Failed to list triggers");
313 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
314 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
315 fail("Failed to get count of triggers returned by listing");
320 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
322 for (i
= 0; i
< trigger_count
; i
++) {
323 const struct lttng_trigger
*trigger_from_listing
;
325 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
326 assert(trigger_from_listing
);
328 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &returned_trigger_name
);
329 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
330 "Trigger returned by listing has a name: trigger name = '%s'",
331 get_trigger_name(trigger_from_listing
));
333 ok(!strcmp(get_trigger_name(trigger_from_listing
),
335 "Name set on trigger returned from listing is correct: name returned from listing = '%s', expected name = '%s'",
336 get_trigger_name(trigger_from_listing
),
339 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
340 ret
= lttng_unregister_trigger(trigger_from_listing
);
341 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance returned by the listing");
345 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
346 ret
= lttng_unregister_trigger(trigger
);
347 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance used on registration");
351 lttng_triggers_destroy(triggers
);
352 lttng_trigger_destroy(trigger
);
356 void register_automatic_name_trigger(
357 enum unregistration_trigger_instance unregistration_trigger
)
360 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
361 enum lttng_trigger_status trigger_status
;
362 const char *returned_trigger_name
;
363 struct lttng_triggers
*triggers
= NULL
;
364 unsigned int trigger_count
, i
;
365 enum lttng_error_code ret_code
;
367 diag("Register an automatic name trigger (Unregistration performed with the trigger instance %s)",
368 unregistration_trigger_instance_name(
369 unregistration_trigger
));
372 fail("Failed to create trigger");
376 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
377 ok(ret_code
== LTTNG_OK
, "Registered trigger with automatic name");
379 trigger_status
= lttng_trigger_get_name(trigger
, &returned_trigger_name
);
380 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
381 "Trigger name is set after registration: trigger name = '%s'",
382 get_trigger_name(trigger
));
384 ok(returned_trigger_name
&& strlen(returned_trigger_name
) > 0,
385 "Automatic name set on trigger after registration longer is not an empty string");
387 ret_code
= lttng_list_triggers(&triggers
);
388 if (ret_code
!= LTTNG_OK
) {
389 fail("Failed to list triggers");
394 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
395 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
396 fail("Failed to get count of triggers returned by listing");
401 ok(trigger_count
== 1, "Trigger listing returns 1 trigger");
403 for (i
= 0; i
< trigger_count
; i
++) {
404 const struct lttng_trigger
*trigger_from_listing
;
406 trigger_from_listing
= lttng_triggers_get_at_index(triggers
, i
);
407 assert(trigger_from_listing
);
409 trigger_status
= lttng_trigger_get_name(trigger_from_listing
, &returned_trigger_name
);
410 ok(trigger_status
== LTTNG_TRIGGER_STATUS_OK
,
411 "Trigger returned by listing has a name: trigger name = '%s'",
412 get_trigger_name(trigger_from_listing
));
414 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
) {
415 ret
= lttng_unregister_trigger(trigger_from_listing
);
416 ok(ret
== 0, "Successfully unregistered automatic name trigger using the trigger instance returned by the listing");
420 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
421 ret
= lttng_unregister_trigger(trigger
);
422 ok(ret
== 0, "Successfully unregistered automatic trigger using the trigger instance used on registration");
426 lttng_triggers_destroy(triggers
);
427 lttng_trigger_destroy(trigger
);
431 void double_register_anonymous_trigger(
432 enum unregistration_trigger_instance unregistration_trigger
)
435 struct lttng_trigger
*trigger
= create_trigger(0xbadc0ffee);
436 struct lttng_triggers
*triggers
= NULL
;
438 diag("Register duplicate anonymous trigger (Unregistration performed with the trigger instance %s)",
439 unregistration_trigger_instance_name(
440 unregistration_trigger
));
443 fail("Failed to create trigger");
447 ret
= lttng_register_trigger(trigger
);
448 ok(ret
== 0, "Registered anonymous trigger");
450 ret
= lttng_register_trigger(trigger
);
451 ok(ret
== -LTTNG_ERR_TRIGGER_EXISTS
,
452 "Registering identical anonymous trigger fails with `LTTNG_ERR_TRIGGER_EXISTS`");
455 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
456 ret
= lttng_unregister_trigger(trigger
);
457 ok(ret
== 0, "Successfully unregistered anonymous trigger using the trigger instance used on registration");
459 ok(get_registered_triggers_count() == 1,
460 "Trigger listing returns 1 trigger");
461 ok(unregister_all_triggers() == 1,
462 "Successfully unregistered anonymous trigger using the trigger instance returned by the listing");
466 lttng_triggers_destroy(triggers
);
467 lttng_trigger_destroy(trigger
);
471 void double_register_named_trigger(
472 enum unregistration_trigger_instance unregistration_trigger
)
475 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
476 struct lttng_trigger
*trigger_b
= create_trigger(0xbadc0ffee);
477 struct lttng_triggers
*triggers
= NULL
;
478 const char * const trigger_name
= "a unique trigger name";
479 enum lttng_error_code ret_code
;
481 diag("Register duplicate named trigger (Unregistration performed with the trigger instance %s)",
482 unregistration_trigger_instance_name(
483 unregistration_trigger
));
485 if (!trigger_a
|| !trigger_b
) {
486 fail("Failed to create triggers");
490 ret_code
= lttng_register_trigger_with_name(trigger_a
, trigger_name
);
491 ok(ret_code
== LTTNG_OK
, "Registered named trigger");
493 ret
= lttng_register_trigger(trigger_a
);
494 ok(ret
== -LTTNG_ERR_INVALID
,
495 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (anonymous registration)");
497 ret_code
= lttng_register_trigger_with_name(trigger_a
, trigger_name
);
498 ok(ret_code
== LTTNG_ERR_INVALID
,
499 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (register with name)");
501 ret_code
= lttng_register_trigger_with_automatic_name(trigger_a
);
502 ok(ret_code
== LTTNG_ERR_INVALID
,
503 "Registering a trigger instance already used for registration fails with `LTTNG_ERR_INVALID` (register with automatic name)");
505 ret_code
= lttng_register_trigger_with_name(trigger_b
, trigger_name
);
506 ok(ret_code
== LTTNG_ERR_TRIGGER_EXISTS
, "Registering trigger with an already used name fails with `LTTNG_ERR_TRIGGER_EXISTS`");
508 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
509 ret
= lttng_unregister_trigger(trigger_a
);
510 ok(ret
== 0, "Successfully unregistered named trigger using the trigger instance used on registration");
512 ok(get_registered_triggers_count() == 1,
513 "Trigger listing returns 1 trigger");
514 ok(unregister_all_triggers() == 1,
515 "Successfully unregistered named trigger using the trigger instance returned by the listing");
519 lttng_triggers_destroy(triggers
);
520 lttng_trigger_destroy(trigger_a
);
521 lttng_trigger_destroy(trigger_b
);
525 void double_register_automatic_name_trigger(
526 enum unregistration_trigger_instance unregistration_trigger
)
529 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
530 struct lttng_trigger
*trigger_b
= create_trigger(0xbadc0ffee);
531 struct lttng_triggers
*triggers
= NULL
;
532 enum lttng_error_code ret_code
;
534 diag("Register duplicate automatic name trigger (Unregistration performed with the trigger instance %s)",
535 unregistration_trigger_instance_name(
536 unregistration_trigger
));
538 if (!trigger_a
|| !trigger_b
) {
539 fail("Failed to create triggers");
543 ret_code
= lttng_register_trigger_with_automatic_name(trigger_a
);
544 ok(ret_code
== LTTNG_OK
, "Registered automatic name trigger: trigger name = '%s'", get_trigger_name(trigger_a
));
546 ret
= lttng_register_trigger_with_automatic_name(trigger_b
);
547 ok(ret_code
== LTTNG_OK
, "Registering an identical trigger instance with an automatic name succeeds: trigger name = '%s'", get_trigger_name(trigger_b
));
549 ok(strcmp(get_trigger_name(trigger_a
), get_trigger_name(trigger_b
)),
550 "Two identical triggers registered with an automatic name have different names");
552 if (unregistration_trigger
== UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
) {
553 ret
= lttng_unregister_trigger(trigger_a
);
554 ok(ret
== 0, "Successfully unregistered automatic trigger A using the trigger instance used on registration");
556 ret
= lttng_unregister_trigger(trigger_b
);
557 ok(ret
== 0, "Successfully unregistered automatic trigger B using the trigger instance used on registration");
559 ok(get_registered_triggers_count() == 2,
560 "Trigger listing returns 2 trigger");
561 ok(unregister_all_triggers() == 2,
562 "Successfully unregistered automatic name triggers using the trigger instance returned by the listing");
566 lttng_triggers_destroy(triggers
);
567 lttng_trigger_destroy(trigger_a
);
568 lttng_trigger_destroy(trigger_b
);
572 void register_multiple_anonymous_triggers(void)
575 struct lttng_trigger
*trigger_a
= create_trigger(0xbadc0ffee);
576 struct lttng_trigger
*trigger_b
= create_trigger(0xbadf00d);
578 diag("Register two different anonymous triggers");
580 if (!trigger_a
|| !trigger_b
) {
581 fail("Failed to create triggers");
585 ret
= lttng_register_trigger(trigger_a
);
586 ok(ret
== 0, "Registered first anonymous trigger");
588 ret
= lttng_register_trigger(trigger_b
);
589 ok(ret
== 0, "Registered second anonymous trigger");
591 ok(get_registered_triggers_count() == 2,
592 "Trigger listing returns 2 trigger");
593 ok(unregister_all_triggers() == 2,
594 "Successfully unregistered two anonymous triggers");
597 lttng_trigger_destroy(trigger_a
);
598 lttng_trigger_destroy(trigger_b
);
601 const test_function test_functions
[] = {
602 register_anonymous_trigger
,
603 register_named_trigger
,
604 register_automatic_name_trigger
,
605 double_register_anonymous_trigger
,
606 double_register_named_trigger
,
607 double_register_automatic_name_trigger
,
610 int main(int argc
, const char **argv
)
614 plan_tests(TEST_COUNT
);
616 if (get_registered_triggers_count() != 0) {
617 fail("Session daemon already has registered triggers, bailing out");
621 for (i
= 0; i
< ARRAY_SIZE(test_functions
); i
++) {
622 const test_function fn
= test_functions
[i
];
624 fn(UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING
);
625 if (get_registered_triggers_count() != 0) {
626 fail("Previous test left registered triggers, bailing out");
631 for (i
= 0; i
< ARRAY_SIZE(test_functions
); i
++) {
632 const test_function fn
= test_functions
[i
];
634 fn(UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION
);
635 if (get_registered_triggers_count() != 0) {
636 fail("Previous test left registered triggers, bailing out");
641 register_multiple_anonymous_triggers();
643 return exit_status();