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>
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.
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.
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
28 #include <lttng/ust-abi.h>
30 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
33 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
35 int lttng_pthread_setname_np(const char *name
)
38 * Some implementations don't error out, replicate this behavior for
41 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
45 return pthread_setname_np(pthread_self(), name
);
49 int lttng_pthread_getname_np(char *name
, size_t len
)
51 return pthread_getname_np(pthread_self(), name
, len
);
53 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
55 int lttng_pthread_setname_np(const char *name
)
57 return pthread_setname_np(name
);
61 int lttng_pthread_getname_np(char *name
, size_t len
)
63 return pthread_getname_np(name
, len
);
65 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
67 #include <pthread_np.h>
69 int lttng_pthread_setname_np(const char *name
)
71 /* Replicate pthread_setname_np's behavior */
72 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
76 pthread_set_name_np(pthread_self(), name
);
81 int lttng_pthread_getname_np(char *name
, size_t len
)
83 pthread_get_name_np(pthread_self(), name
, len
);
86 #elif defined(__linux__)
88 /* Fallback on prtctl on Linux */
89 #include <sys/prctl.h>
92 int lttng_pthread_setname_np(const char *name
)
94 /* Replicate pthread_setname_np's behavior */
95 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
98 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
102 int lttng_pthread_getname_np(char *name
, size_t len
)
104 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
108 #error "Please add pthread name support for your OS."
112 * If a pthread setname/set_name function is available, declare
113 * the setustprocname() function that will add '-ust' to the end
114 * of the current process name, while truncating it if needed.
117 int lttng_ust_setustprocname(void)
120 char name
[LTTNG_UST_ABI_PROCNAME_LEN
];
121 int limit
= LTTNG_UST_ABI_PROCNAME_LEN
- strlen(LTTNG_UST_PROCNAME_SUFFIX
) - 1;
124 * Get the current thread name.
126 ret
= lttng_pthread_getname_np(name
, LTTNG_UST_ABI_PROCNAME_LEN
);
136 ret
= sprintf(name
+ len
, LTTNG_UST_PROCNAME_SUFFIX
);
137 if (ret
!= strlen(LTTNG_UST_PROCNAME_SUFFIX
)) {
141 ret
= lttng_pthread_setname_np(name
);
150 #define ENODATA ENOMSG
153 #endif /* _UST_COMPAT_H */