port: FreeBSD 12.2 added pthread_setname_np
[deliverable/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>
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 34static inline
d9c56a93 35int lttng_pthread_setname_np(const char *name)
08114193 36{
bebc0c82
MJ
37 /*
38 * Some implementations don't error out, replicate this behavior for
39 * consistency.
40 */
41 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
42 return ERANGE;
43 }
44
0db3d6ee 45 return pthread_setname_np(pthread_self(), name);
08114193
MD
46}
47
01f0e40c 48static inline
d9c56a93 49int lttng_pthread_getname_np(char *name, size_t len)
01f0e40c 50{
0db3d6ee 51 return pthread_getname_np(pthread_self(), name, len);
01f0e40c 52}
d9c56a93 53#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
e4486ebc 54static inline
d9c56a93 55int lttng_pthread_setname_np(const char *name)
e4486ebc 56{
0db3d6ee 57 return pthread_setname_np(name);
e4486ebc 58}
08114193 59
d9c56a93
MJ
60static inline
61int lttng_pthread_getname_np(char *name, size_t len)
62{
0db3d6ee 63 return pthread_getname_np(name, len);
d9c56a93 64}
0db3d6ee
MJ
65#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
66
67#include <pthread_np.h>
01f0e40c 68static inline
d9c56a93 69int lttng_pthread_setname_np(const char *name)
01f0e40c 70{
f2db7517
MJ
71 /* Replicate pthread_setname_np's behavior */
72 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
73 return ERANGE;
74 }
75
0db3d6ee
MJ
76 pthread_set_name_np(pthread_self(), name);
77 return 0;
01f0e40c 78}
01f0e40c 79
d9c56a93
MJ
80static inline
81int lttng_pthread_getname_np(char *name, size_t len)
82{
0db3d6ee
MJ
83 pthread_get_name_np(pthread_self(), name, len);
84 return 0;
85}
86#elif defined(__linux__)
87
88/* Fallback on prtctl on Linux */
89#include <sys/prctl.h>
90
91static inline
92int lttng_pthread_setname_np(const char *name)
93{
f2db7517
MJ
94 /* Replicate pthread_setname_np's behavior */
95 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
96 return ERANGE;
97 }
0db3d6ee 98 return prctl(PR_SET_NAME, name, 0, 0, 0);
d9c56a93 99}
01f0e40c 100
d9c56a93 101static inline
0db3d6ee 102int lttng_pthread_getname_np(char *name, size_t len)
d9c56a93 103{
0db3d6ee 104 return prctl(PR_GET_NAME, name, 0, 0, 0);
d9c56a93 105}
01f0e40c 106
0db3d6ee
MJ
107#else
108#error "Please add pthread name support for your OS."
109#endif
110
111/*
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.
115 */
01f0e40c
RB
116static inline
117int lttng_ust_setustprocname(void)
118{
119 int ret = 0, len;
0db3d6ee
MJ
120 char name[LTTNG_UST_ABI_PROCNAME_LEN];
121 int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
122
123 /*
124 * Get the current thread name.
125 */
126 ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN);
127 if (ret) {
128 goto error;
129 }
01f0e40c
RB
130
131 len = strlen(name);
132 if (len > limit) {
133 len = limit;
134 }
135
136 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
8db078dc 137 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
01f0e40c
RB
138 goto error;
139 }
140
d9c56a93 141 ret = lttng_pthread_setname_np(name);
01f0e40c
RB
142
143error:
144 return ret;
145}
d9c56a93 146
bdcf8d82
MD
147#include <errno.h>
148
149#ifndef ENODATA
150#define ENODATA ENOMSG
151#endif
152
b728d87e 153#endif /* _UST_COMPAT_H */
This page took 0.043743 seconds and 5 git commands to generate.