port: fix pthread_setname_np integration
[lttng-ust.git] / liblttng-ust / compat.h
1 #ifndef _UST_COMPAT_H
2 #define _UST_COMPAT_H
3
4 /*
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
7 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <pthread.h>
25 #include <errno.h>
26
27 #include <lttng/ust-abi.h>
28
29 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
30
31
32 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
33 static inline
34 int lttng_pthread_setname_np(const char *name)
35 {
36 return pthread_setname_np(pthread_self(), name);
37 }
38
39 static inline
40 int lttng_pthread_getname_np(char *name, size_t len)
41 {
42 return pthread_getname_np(pthread_self(), name, len);
43 }
44 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
45 static inline
46 int lttng_pthread_setname_np(const char *name)
47 {
48 return pthread_setname_np(name);
49 }
50
51 static inline
52 int lttng_pthread_getname_np(char *name, size_t len)
53 {
54 return pthread_getname_np(name, len);
55 }
56 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
57
58 #include <pthread_np.h>
59 static inline
60 int lttng_pthread_setname_np(const char *name)
61 {
62 pthread_set_name_np(pthread_self(), name);
63 return 0;
64 }
65
66 static inline
67 int lttng_pthread_getname_np(char *name, size_t len)
68 {
69 pthread_get_name_np(pthread_self(), name, len);
70 return 0;
71 }
72 #elif defined(__linux__)
73
74 /* Fallback on prtctl on Linux */
75 #include <sys/prctl.h>
76
77 static inline
78 int lttng_pthread_setname_np(const char *name)
79 {
80 return prctl(PR_SET_NAME, name, 0, 0, 0);
81 }
82
83 static inline
84 int lttng_pthread_getname_np(char *name, size_t len)
85 {
86 return prctl(PR_GET_NAME, name, 0, 0, 0);
87 }
88
89 #else
90 #error "Please add pthread name support for your OS."
91 #endif
92
93 /*
94 * If a pthread setname/set_name function is available, declare
95 * the setustprocname() function that will add '-ust' to the end
96 * of the current process name, while truncating it if needed.
97 */
98 static inline
99 int lttng_ust_setustprocname(void)
100 {
101 int ret = 0, len;
102 char name[LTTNG_UST_ABI_PROCNAME_LEN];
103 int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
104
105 /*
106 * Get the current thread name.
107 */
108 ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN);
109 if (ret) {
110 goto error;
111 }
112
113 len = strlen(name);
114 if (len > limit) {
115 len = limit;
116 }
117
118 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
119 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
120 goto error;
121 }
122
123 ret = lttng_pthread_setname_np(name);
124
125 error:
126 return ret;
127 }
128
129 #include <errno.h>
130
131 #ifndef ENODATA
132 #define ENODATA ENOMSG
133 #endif
134
135 #endif /* _UST_COMPAT_H */
This page took 0.031027 seconds and 4 git commands to generate.