port: fix pthread_setname_np integration
[lttng-ust.git] / liblttng-ust / compat.h
CommitLineData
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>
d9c56a93 26
0db3d6ee 27#include <lttng/ust-abi.h>
08114193 28
d9c56a93 29#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
08114193 30
08114193 31
d9c56a93 32#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
08114193 33static inline
d9c56a93 34int lttng_pthread_setname_np(const char *name)
08114193 35{
0db3d6ee 36 return pthread_setname_np(pthread_self(), name);
08114193
MD
37}
38
01f0e40c 39static inline
d9c56a93 40int lttng_pthread_getname_np(char *name, size_t len)
01f0e40c 41{
0db3d6ee 42 return pthread_getname_np(pthread_self(), name, len);
01f0e40c 43}
d9c56a93 44#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
e4486ebc 45static inline
d9c56a93 46int lttng_pthread_setname_np(const char *name)
e4486ebc 47{
0db3d6ee 48 return pthread_setname_np(name);
e4486ebc 49}
08114193 50
d9c56a93
MJ
51static inline
52int lttng_pthread_getname_np(char *name, size_t len)
53{
0db3d6ee 54 return pthread_getname_np(name, len);
d9c56a93 55}
0db3d6ee
MJ
56#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
57
58#include <pthread_np.h>
01f0e40c 59static inline
d9c56a93 60int lttng_pthread_setname_np(const char *name)
01f0e40c 61{
0db3d6ee
MJ
62 pthread_set_name_np(pthread_self(), name);
63 return 0;
01f0e40c 64}
01f0e40c 65
d9c56a93
MJ
66static inline
67int lttng_pthread_getname_np(char *name, size_t len)
68{
0db3d6ee
MJ
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
77static inline
78int lttng_pthread_setname_np(const char *name)
79{
80 return prctl(PR_SET_NAME, name, 0, 0, 0);
d9c56a93 81}
01f0e40c 82
d9c56a93 83static inline
0db3d6ee 84int lttng_pthread_getname_np(char *name, size_t len)
d9c56a93 85{
0db3d6ee 86 return prctl(PR_GET_NAME, name, 0, 0, 0);
d9c56a93 87}
01f0e40c 88
0db3d6ee
MJ
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 */
01f0e40c
RB
98static inline
99int lttng_ust_setustprocname(void)
100{
101 int ret = 0, len;
0db3d6ee
MJ
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 }
01f0e40c
RB
112
113 len = strlen(name);
114 if (len > limit) {
115 len = limit;
116 }
117
118 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
8db078dc 119 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
01f0e40c
RB
120 goto error;
121 }
122
d9c56a93 123 ret = lttng_pthread_setname_np(name);
01f0e40c
RB
124
125error:
126 return ret;
127}
d9c56a93 128
bdcf8d82
MD
129#include <errno.h>
130
131#ifndef ENODATA
132#define ENODATA ENOMSG
133#endif
134
b728d87e 135#endif /* _UST_COMPAT_H */
This page took 0.035426 seconds and 4 git commands to generate.