port: FreeBSD 12.2 added pthread_setname_np
[deliverable/lttng-ust.git] / liblttng-ust / compat.h
... / ...
CommitLineData
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#include <string.h>
27
28#include <lttng/ust-abi.h>
29
30#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
31
32
33#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
34static inline
35int lttng_pthread_setname_np(const char *name)
36{
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
45 return pthread_setname_np(pthread_self(), name);
46}
47
48static inline
49int lttng_pthread_getname_np(char *name, size_t len)
50{
51 return pthread_getname_np(pthread_self(), name, len);
52}
53#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
54static inline
55int lttng_pthread_setname_np(const char *name)
56{
57 return pthread_setname_np(name);
58}
59
60static inline
61int lttng_pthread_getname_np(char *name, size_t len)
62{
63 return pthread_getname_np(name, len);
64}
65#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
66
67#include <pthread_np.h>
68static inline
69int lttng_pthread_setname_np(const char *name)
70{
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
76 pthread_set_name_np(pthread_self(), name);
77 return 0;
78}
79
80static inline
81int lttng_pthread_getname_np(char *name, size_t len)
82{
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{
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 }
98 return prctl(PR_SET_NAME, name, 0, 0, 0);
99}
100
101static inline
102int lttng_pthread_getname_np(char *name, size_t len)
103{
104 return prctl(PR_GET_NAME, name, 0, 0, 0);
105}
106
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 */
116static inline
117int lttng_ust_setustprocname(void)
118{
119 int ret = 0, len;
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 }
130
131 len = strlen(name);
132 if (len > limit) {
133 len = limit;
134 }
135
136 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
137 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
138 goto error;
139 }
140
141 ret = lttng_pthread_setname_np(name);
142
143error:
144 return ret;
145}
146
147#include <errno.h>
148
149#ifndef ENODATA
150#define ENODATA ENOMSG
151#endif
152
153#endif /* _UST_COMPAT_H */
This page took 0.025792 seconds and 5 git commands to generate.