Build fix: build without lttng-ust
[lttng-tools.git] / tests / regression / tools / trigger / name / trigger_name.c
CommitLineData
2d0e6286
JG
1/*
2 * trigger_name.c
3 *
4 * Tests suite for anonymous, named, and automatic name triggers.
5 *
6 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * SPDX-License-Identifier: MIT
9 *
10 */
11
12#include <stdio.h>
13#include <unistd.h>
14#include <assert.h>
15#include <tap/tap.h>
16#include <stdint.h>
17#include <string.h>
18#include <lttng/lttng.h>
19#include <common/macros.h>
20
21#define TEST_COUNT 70
22
23enum unregistration_trigger_instance {
24 UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION,
25 UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING,
26};
27
28typedef void (*test_function)(enum unregistration_trigger_instance);
29
30static
31const char *get_trigger_name(const struct lttng_trigger *trigger)
32{
33 const char *trigger_name;
34 enum lttng_trigger_status trigger_status;
35
36 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
37 switch (trigger_status) {
38 case LTTNG_TRIGGER_STATUS_OK:
39 break;
40 case LTTNG_TRIGGER_STATUS_UNSET:
41 trigger_name = "(anonymous)";
42 break;
43 default:
44 trigger_name = "(failed to get name)";
45 break;
46 }
47
48 return trigger_name;
49}
50
51static
52const char *unregistration_trigger_instance_name(
53 enum unregistration_trigger_instance unregistration_trigger)
54{
55 const char *name;
56
57 switch (unregistration_trigger) {
58 case UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING:
59 name = "from listing";
60 break;
61 case UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION:
62 name = "used for registration";
63 break;
64 default:
65 abort();
66 }
67
68 return name;
69}
70
71/*
72 * Returns a negative error code on error, else the number of unregistered
73 * triggers.
74 */
75static
76int unregister_all_triggers(void)
77{
78 int ret;
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;
83
84 ret_code = lttng_list_triggers(&triggers);
85 if (ret_code != LTTNG_OK) {
86 fail("Failed to list triggers");
87 ret = -1;
88 goto end;
89 }
90
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");
94 ret = -1;
95 goto end;
96 }
97
98 for (i = 0; i < trigger_count; i++) {
99 const struct lttng_trigger *trigger;
100
101 trigger = lttng_triggers_get_at_index(triggers, i);
102 assert(trigger);
103
104 ret = lttng_unregister_trigger(trigger);
105 if (ret) {
106 fail("Failed to unregister trigger: trigger name = '%s'");
107 goto end;
108 }
109
110 unregistered_trigger_count++;
111 }
112
113 ret = (int) unregistered_trigger_count;
114
115end:
116 lttng_triggers_destroy(triggers);
117 return ret;
118}
119
120static
121int get_registered_triggers_count(void)
122{
123 int ret;
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;
128
129 ret_code = lttng_list_triggers(&triggers);
130 if (ret_code != LTTNG_OK) {
131 fail("Failed to list triggers");
132 ret = -1;
133 goto end;
134 }
135
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");
139 ret = -1;
140 goto end;
141 }
142
143 ret = (int) trigger_count;
144
145end:
146 lttng_triggers_destroy(triggers);
147 return ret;
148}
149
150/*
151 * Create a generic trigger. The specifics of the condition and action are not
152 * important for the purposes of this test.
153 */
154static
3eda43da 155struct lttng_trigger *create_trigger(uint64_t threshold)
2d0e6286
JG
156{
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";
162
163 condition = lttng_condition_session_consumed_size_create();
164 if (!condition) {
165 fail("Failed to create 'session consumed size' condition");
166 goto end;
167 }
168
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");
172 goto end;
173 }
174
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");
179 goto end;
180 }
181
182 action = lttng_action_notify_create();
183 if (!action) {
184 fail("Failed to create 'notify' action");
185 goto end;
186 }
187
188 trigger = lttng_trigger_create(condition, action);
189 if (!trigger) {
190 fail("Failed to create trigger");
191 goto end;
192 }
193
194end:
195 lttng_condition_destroy(condition);
196 lttng_action_destroy(action);
197 return trigger;
198}
199
200static
201void register_anonymous_trigger(
202 enum unregistration_trigger_instance unregistration_trigger)
203{
204 int ret;
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;
211
212 diag("Register an anonymous trigger (Unregistration performed with the trigger instance %s)",
213 unregistration_trigger_instance_name(
214 unregistration_trigger));
215
216 if (!trigger) {
217 fail("Failed to create trigger");
218 goto end;
219 }
220
221 ret = lttng_register_trigger(trigger);
222 ok(ret == 0, "Registered anonymous trigger");
223
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));
228
229 ret_code = lttng_list_triggers(&triggers);
230 if (ret_code != LTTNG_OK) {
231 fail("Failed to list triggers");
232 ret = -1;
233 goto end;
234 }
235
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");
239 ret = -1;
240 goto end;
241 }
242
243 ok(trigger_count == 1, "Trigger listing returns 1 trigger");
244
245 for (i = 0; i < trigger_count; i++) {
246 const struct lttng_trigger *trigger_from_listing;
247
248 trigger_from_listing = lttng_triggers_get_at_index(triggers, i);
249 assert(trigger_from_listing);
250
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));
255
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");
259 }
260 }
261
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");
265 }
266
267end:
268 lttng_triggers_destroy(triggers);
269 lttng_trigger_destroy(trigger);
270}
271
272static
273void register_named_trigger(
274 enum unregistration_trigger_instance unregistration_trigger)
275{
276 int ret;
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";
284
285 diag("Register a named trigger (Unregistration performed with the trigger instance %s)",
286 unregistration_trigger_instance_name(
287 unregistration_trigger));
288
289 if (!trigger) {
290 fail("Failed to create trigger");
291 goto end;
292 }
293
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));
297
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));
302
303 ok(!strcmp(get_trigger_name(trigger), trigger_name),
304 "Name set on trigger after registration is correct");
305
306 ret_code = lttng_list_triggers(&triggers);
307 if (ret_code != LTTNG_OK) {
308 fail("Failed to list triggers");
309 ret = -1;
310 goto end;
311 }
312
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");
316 ret = -1;
317 goto end;
318 }
319
320 ok(trigger_count == 1, "Trigger listing returns 1 trigger");
321
322 for (i = 0; i < trigger_count; i++) {
323 const struct lttng_trigger *trigger_from_listing;
324
325 trigger_from_listing = lttng_triggers_get_at_index(triggers, i);
326 assert(trigger_from_listing);
327
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));
332
333 ok(!strcmp(get_trigger_name(trigger_from_listing),
334 trigger_name),
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),
337 trigger_name);
338
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");
342 }
343 }
344
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");
348 }
349
350end:
351 lttng_triggers_destroy(triggers);
352 lttng_trigger_destroy(trigger);
353}
354
355static
356void register_automatic_name_trigger(
357 enum unregistration_trigger_instance unregistration_trigger)
358{
359 int ret;
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;
366
367 diag("Register an automatic name trigger (Unregistration performed with the trigger instance %s)",
368 unregistration_trigger_instance_name(
369 unregistration_trigger));
370
371 if (!trigger) {
372 fail("Failed to create trigger");
373 goto end;
374 }
375
376 ret_code = lttng_register_trigger_with_automatic_name(trigger);
377 ok(ret_code == LTTNG_OK, "Registered trigger with automatic name");
378
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));
383
384 ok(returned_trigger_name && strlen(returned_trigger_name) > 0,
385 "Automatic name set on trigger after registration longer is not an empty string");
386
387 ret_code = lttng_list_triggers(&triggers);
388 if (ret_code != LTTNG_OK) {
389 fail("Failed to list triggers");
390 ret = -1;
391 goto end;
392 }
393
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");
397 ret = -1;
398 goto end;
399 }
400
401 ok(trigger_count == 1, "Trigger listing returns 1 trigger");
402
403 for (i = 0; i < trigger_count; i++) {
404 const struct lttng_trigger *trigger_from_listing;
405
406 trigger_from_listing = lttng_triggers_get_at_index(triggers, i);
407 assert(trigger_from_listing);
408
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));
413
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");
417 }
418 }
419
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");
423 }
424
425end:
426 lttng_triggers_destroy(triggers);
427 lttng_trigger_destroy(trigger);
428}
429
430static
431void double_register_anonymous_trigger(
432 enum unregistration_trigger_instance unregistration_trigger)
433{
434 int ret;
435 struct lttng_trigger *trigger = create_trigger(0xbadc0ffee);
436 struct lttng_triggers *triggers = NULL;
437
438 diag("Register duplicate anonymous trigger (Unregistration performed with the trigger instance %s)",
439 unregistration_trigger_instance_name(
440 unregistration_trigger));
441
442 if (!trigger) {
443 fail("Failed to create trigger");
444 goto end;
445 }
446
447 ret = lttng_register_trigger(trigger);
448 ok(ret == 0, "Registered anonymous trigger");
449
450 ret = lttng_register_trigger(trigger);
451 ok(ret == -LTTNG_ERR_TRIGGER_EXISTS,
452 "Registering identical anonymous trigger fails with `LTTNG_ERR_TRIGGER_EXISTS`");
453
454
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");
458 } else {
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");
463 }
464
465end:
466 lttng_triggers_destroy(triggers);
467 lttng_trigger_destroy(trigger);
468}
469
470static
471void double_register_named_trigger(
472 enum unregistration_trigger_instance unregistration_trigger)
473{
474 int ret;
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;
480
481 diag("Register duplicate named trigger (Unregistration performed with the trigger instance %s)",
482 unregistration_trigger_instance_name(
483 unregistration_trigger));
484
485 if (!trigger_a || !trigger_b) {
486 fail("Failed to create triggers");
487 goto end;
488 }
489
490 ret_code = lttng_register_trigger_with_name(trigger_a, trigger_name);
491 ok(ret_code == LTTNG_OK, "Registered named trigger");
492
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)");
496
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)");
500
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)");
504
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`");
507
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");
511 } else {
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");
516 }
517
518end:
519 lttng_triggers_destroy(triggers);
520 lttng_trigger_destroy(trigger_a);
521 lttng_trigger_destroy(trigger_b);
522}
523
524static
525void double_register_automatic_name_trigger(
526 enum unregistration_trigger_instance unregistration_trigger)
527{
528 int ret;
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;
533
534 diag("Register duplicate automatic name trigger (Unregistration performed with the trigger instance %s)",
535 unregistration_trigger_instance_name(
536 unregistration_trigger));
537
538 if (!trigger_a || !trigger_b) {
539 fail("Failed to create triggers");
540 goto end;
541 }
542
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));
545
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));
548
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");
551
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");
555
556 ret = lttng_unregister_trigger(trigger_b);
557 ok(ret == 0, "Successfully unregistered automatic trigger B using the trigger instance used on registration");
558 } else {
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");
563 }
564
565end:
566 lttng_triggers_destroy(triggers);
567 lttng_trigger_destroy(trigger_a);
568 lttng_trigger_destroy(trigger_b);
569}
570
571static
572void register_multiple_anonymous_triggers(void)
573{
574 int ret;
575 struct lttng_trigger *trigger_a = create_trigger(0xbadc0ffee);
576 struct lttng_trigger *trigger_b = create_trigger(0xbadf00d);
577
578 diag("Register two different anonymous triggers");
579
580 if (!trigger_a || !trigger_b) {
581 fail("Failed to create triggers");
582 goto end;
583 }
584
585 ret = lttng_register_trigger(trigger_a);
586 ok(ret == 0, "Registered first anonymous trigger");
587
588 ret = lttng_register_trigger(trigger_b);
589 ok(ret == 0, "Registered second anonymous trigger");
590
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");
595
596end:
597 lttng_trigger_destroy(trigger_a);
598 lttng_trigger_destroy(trigger_b);
599}
600
601const 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,
608};
609
610int main(int argc, const char **argv)
611{
612 size_t i;
613
614 plan_tests(TEST_COUNT);
615
616 if (get_registered_triggers_count() != 0) {
617 fail("Session daemon already has registered triggers, bailing out");
618 goto end;
619 }
620
621 for (i = 0; i < ARRAY_SIZE(test_functions); i++) {
622 const test_function fn = test_functions[i];
623
624 fn(UNREGISTRATION_TRIGGER_INSTANCE_FROM_LISTING);
625 if (get_registered_triggers_count() != 0) {
626 fail("Previous test left registered triggers, bailing out");
627 goto end;
628 }
629 }
630
631 for (i = 0; i < ARRAY_SIZE(test_functions); i++) {
632 const test_function fn = test_functions[i];
633
634 fn(UNREGISTRATION_TRIGGER_INSTANCE_USED_FOR_REGISTRATION);
635 if (get_registered_triggers_count() != 0) {
636 fail("Previous test left registered triggers, bailing out");
637 goto end;
638 }
639 }
640
641 register_multiple_anonymous_triggers();
642end:
643 return exit_status();
644}
This page took 0.068286 seconds and 4 git commands to generate.