| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include "compat.h" |
| 10 | |
| 11 | #include "tap.h" |
| 12 | |
| 13 | #define TEST_NAME_PROPER_LEN 16 |
| 14 | |
| 15 | int main() |
| 16 | { |
| 17 | int ret; |
| 18 | char name1[TEST_NAME_PROPER_LEN]; |
| 19 | char name2[TEST_NAME_PROPER_LEN]; |
| 20 | char too_long_name[] = "thisnameistoolong"; |
| 21 | char short_name[] = "labatt50"; |
| 22 | char short_name_ust[] = "labatt50-ust"; |
| 23 | char long_name[] = "procrastinating"; |
| 24 | char long_name_ust[] = "procrastina-ust"; |
| 25 | |
| 26 | plan_tests(12); |
| 27 | |
| 28 | /* Get the initial thread name */ |
| 29 | ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN); |
| 30 | ok(ret == 0, "Get the thread name: '%s'", name1); |
| 31 | |
| 32 | /* Set a thread name of more than 16 bytes, should fail */ |
| 33 | ret = lttng_pthread_setname_np(too_long_name); |
| 34 | ok(ret == ERANGE, "Set a too long thread name: '%s'", too_long_name); |
| 35 | |
| 36 | /* Get the thread name again, shouldn't have changed */ |
| 37 | ret = lttng_pthread_getname_np(name2, TEST_NAME_PROPER_LEN); |
| 38 | ok(ret == 0, "Get the thread name: '%s'", name2); |
| 39 | ok(strcmp(name1, name2) == 0, "Compare the initial thread name: '%s' == '%s'", name1, name2); |
| 40 | |
| 41 | /* Set a thread name of less than 16 bytes */ |
| 42 | ret = lttng_pthread_setname_np(short_name); |
| 43 | ok(ret == 0, "Set a short thread name: '%s'", short_name); |
| 44 | |
| 45 | /* Get the thread name again, should be the one we set */ |
| 46 | ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN); |
| 47 | ok(ret == 0, "Get a short thread name: '%s'", name1); |
| 48 | ok(strcmp(short_name, name1) == 0, "Compare the short thread name: '%s' == '%s'", short_name, name1); |
| 49 | |
| 50 | /* Append "-ust" to the thread name */ |
| 51 | lttng_ust_setustprocname(); |
| 52 | ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN); |
| 53 | ok(strcmp(short_name_ust, name1) == 0, "Compare the short UST thread name: '%s' == '%s'", short_name_ust, name1); |
| 54 | |
| 55 | |
| 56 | /* Set a thread name of 16 bytes */ |
| 57 | ret = lttng_pthread_setname_np(long_name); |
| 58 | ok(ret == 0, "Set a long thread name: '%s'", long_name); |
| 59 | |
| 60 | /* Get the thread name again, should be the one we set */ |
| 61 | ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN); |
| 62 | ok(ret == 0, "Get a long thread name: '%s'", name1); |
| 63 | ok(strncmp(long_name, name1, TEST_NAME_PROPER_LEN - 1) == 0, "Compare the long thread name: '%s' == '%s'", long_name, name1); |
| 64 | |
| 65 | /* Append "-ust" to the thread name which will truncate its end */ |
| 66 | lttng_ust_setustprocname(); |
| 67 | ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN); |
| 68 | ok(strcmp(long_name_ust, name1) == 0, "Compare the long UST thread name: '%s' == '%s'", long_name_ust, name1); |
| 69 | |
| 70 | return exit_status(); |
| 71 | } |