Commit | Line | Data |
---|---|---|
b728d87e MD |
1 | #ifndef _UST_COMPAT_H |
2 | #define _UST_COMPAT_H | |
3 | ||
4 | /* | |
e92f3e28 | 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
01f0e40c | 6 | * Copyright (C) 2016 Raphaël Beamonte <raphael.beamonte@gmail.com> |
0db3d6ee | 7 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
b728d87e | 8 | * |
e92f3e28 MD |
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. | |
b728d87e | 13 | * |
e92f3e28 MD |
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 | |
b728d87e MD |
22 | */ |
23 | ||
d9c56a93 | 24 | #include <pthread.h> |
0db3d6ee | 25 | #include <errno.h> |
f2db7517 | 26 | #include <string.h> |
d9c56a93 | 27 | |
0db3d6ee | 28 | #include <lttng/ust-abi.h> |
08114193 | 29 | |
d9c56a93 | 30 | #define LTTNG_UST_PROCNAME_SUFFIX "-ust" |
08114193 | 31 | |
08114193 | 32 | |
d9c56a93 | 33 | #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) |
08114193 | 34 | static inline |
d9c56a93 | 35 | int lttng_pthread_setname_np(const char *name) |
08114193 | 36 | { |
0db3d6ee | 37 | return pthread_setname_np(pthread_self(), name); |
08114193 MD |
38 | } |
39 | ||
01f0e40c | 40 | static inline |
d9c56a93 | 41 | int lttng_pthread_getname_np(char *name, size_t len) |
01f0e40c | 42 | { |
0db3d6ee | 43 | return pthread_getname_np(pthread_self(), name, len); |
01f0e40c | 44 | } |
d9c56a93 | 45 | #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) |
e4486ebc | 46 | static inline |
d9c56a93 | 47 | int lttng_pthread_setname_np(const char *name) |
e4486ebc | 48 | { |
0db3d6ee | 49 | return pthread_setname_np(name); |
e4486ebc | 50 | } |
08114193 | 51 | |
d9c56a93 MJ |
52 | static inline |
53 | int lttng_pthread_getname_np(char *name, size_t len) | |
54 | { | |
0db3d6ee | 55 | return pthread_getname_np(name, len); |
d9c56a93 | 56 | } |
0db3d6ee MJ |
57 | #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) |
58 | ||
59 | #include <pthread_np.h> | |
01f0e40c | 60 | static inline |
d9c56a93 | 61 | int lttng_pthread_setname_np(const char *name) |
01f0e40c | 62 | { |
f2db7517 MJ |
63 | /* Replicate pthread_setname_np's behavior */ |
64 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { | |
65 | return ERANGE; | |
66 | } | |
67 | ||
0db3d6ee MJ |
68 | pthread_set_name_np(pthread_self(), name); |
69 | return 0; | |
01f0e40c | 70 | } |
01f0e40c | 71 | |
d9c56a93 MJ |
72 | static inline |
73 | int lttng_pthread_getname_np(char *name, size_t len) | |
74 | { | |
0db3d6ee MJ |
75 | pthread_get_name_np(pthread_self(), name, len); |
76 | return 0; | |
77 | } | |
78 | #elif defined(__linux__) | |
79 | ||
80 | /* Fallback on prtctl on Linux */ | |
81 | #include <sys/prctl.h> | |
82 | ||
83 | static inline | |
84 | int lttng_pthread_setname_np(const char *name) | |
85 | { | |
f2db7517 MJ |
86 | /* Replicate pthread_setname_np's behavior */ |
87 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { | |
88 | return ERANGE; | |
89 | } | |
0db3d6ee | 90 | return prctl(PR_SET_NAME, name, 0, 0, 0); |
d9c56a93 | 91 | } |
01f0e40c | 92 | |
d9c56a93 | 93 | static inline |
0db3d6ee | 94 | int lttng_pthread_getname_np(char *name, size_t len) |
d9c56a93 | 95 | { |
0db3d6ee | 96 | return prctl(PR_GET_NAME, name, 0, 0, 0); |
d9c56a93 | 97 | } |
01f0e40c | 98 | |
0db3d6ee MJ |
99 | #else |
100 | #error "Please add pthread name support for your OS." | |
101 | #endif | |
102 | ||
103 | /* | |
104 | * If a pthread setname/set_name function is available, declare | |
105 | * the setustprocname() function that will add '-ust' to the end | |
106 | * of the current process name, while truncating it if needed. | |
107 | */ | |
01f0e40c RB |
108 | static inline |
109 | int lttng_ust_setustprocname(void) | |
110 | { | |
111 | int ret = 0, len; | |
0db3d6ee MJ |
112 | char name[LTTNG_UST_ABI_PROCNAME_LEN]; |
113 | int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1; | |
114 | ||
115 | /* | |
116 | * Get the current thread name. | |
117 | */ | |
118 | ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN); | |
119 | if (ret) { | |
120 | goto error; | |
121 | } | |
01f0e40c RB |
122 | |
123 | len = strlen(name); | |
124 | if (len > limit) { | |
125 | len = limit; | |
126 | } | |
127 | ||
128 | ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); | |
8db078dc | 129 | if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { |
01f0e40c RB |
130 | goto error; |
131 | } | |
132 | ||
d9c56a93 | 133 | ret = lttng_pthread_setname_np(name); |
01f0e40c RB |
134 | |
135 | error: | |
136 | return ret; | |
137 | } | |
d9c56a93 | 138 | |
bdcf8d82 MD |
139 | #include <errno.h> |
140 | ||
141 | #ifndef ENODATA | |
142 | #define ENODATA ENOMSG | |
143 | #endif | |
144 | ||
b728d87e | 145 | #endif /* _UST_COMPAT_H */ |