b6c7831b5720d0c8b4d8cd389736a1d5b055956f
[lttng-ust.git] / tests / unit / pthread_name / pthread_name.c
1 /* Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; only
6 * version 2.1 of the License.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "compat.h"
21
22 #include "tap.h"
23
24 #define TEST_NAME_PROPER_LEN 16
25
26 int main()
27 {
28 int ret;
29 char name1[TEST_NAME_PROPER_LEN];
30 char name2[TEST_NAME_PROPER_LEN];
31 char too_long_name[] = "thisnameistoolong";
32 char short_name[] = "labatt50";
33 char short_name_ust[] = "labatt50-ust";
34 char long_name[] = "procrastinating";
35 char long_name_ust[] = "procrastina-ust";
36
37 plan_tests(12);
38
39 /* Get the initial thread name */
40 ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN);
41 ok(ret == 0, "Get the thread name: '%s'", name1);
42
43 /* Set a thread name of more than 16 bytes, should fail */
44 ret = lttng_pthread_setname_np(too_long_name);
45 ok(ret == ERANGE, "Set a too long thread name: '%s'", too_long_name);
46
47 /* Get the thread name again, shouldn't have changed */
48 ret = lttng_pthread_getname_np(name2, TEST_NAME_PROPER_LEN);
49 ok(ret == 0, "Get the thread name: '%s'", name2);
50 ok(strcmp(name1, name2) == 0, "Compare the initial thread name: '%s' == '%s'", name1, name2);
51
52 /* Set a thread name of less than 16 bytes */
53 ret = lttng_pthread_setname_np(short_name);
54 ok(ret == 0, "Set a short thread name: '%s'", short_name);
55
56 /* Get the thread name again, should be the one we set */
57 ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN);
58 ok(ret == 0, "Get a short thread name: '%s'", name1);
59 ok(strcmp(short_name, name1) == 0, "Compare the short thread name: '%s' == '%s'", short_name, name1);
60
61 /* Append "-ust" to the thread name */
62 lttng_ust_setustprocname();
63 ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN);
64 ok(strcmp(short_name_ust, name1) == 0, "Compare the short UST thread name: '%s' == '%s'", short_name_ust, name1);
65
66
67 /* Set a thread name of 16 bytes */
68 ret = lttng_pthread_setname_np(long_name);
69 ok(ret == 0, "Set a long thread name: '%s'", long_name);
70
71 /* Get the thread name again, should be the one we set */
72 ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN);
73 ok(ret == 0, "Get a long thread name: '%s'", name1);
74 ok(strncmp(long_name, name1, TEST_NAME_PROPER_LEN - 1) == 0, "Compare the long thread name: '%s' == '%s'", long_name, name1);
75
76 /* Append "-ust" to the thread name which will truncate its end */
77 lttng_ust_setustprocname();
78 ret = lttng_pthread_getname_np(name1, TEST_NAME_PROPER_LEN);
79 ok(strcmp(long_name_ust, name1) == 0, "Compare the long UST thread name: '%s' == '%s'", long_name_ust, name1);
80
81 return exit_status();
82 }
This page took 0.030807 seconds and 3 git commands to generate.