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
)
37 return pthread_setname_np(pthread_self(), name
);
41 int lttng_pthread_getname_np(char *name
, size_t len
)
43 return pthread_getname_np(pthread_self(), name
, len
);
45 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
47 int lttng_pthread_setname_np(const char *name
)
49 return pthread_setname_np(name
);
53 int lttng_pthread_getname_np(char *name
, size_t len
)
55 return pthread_getname_np(name
, len
);
57 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
59 #include <pthread_np.h>
61 int lttng_pthread_setname_np(const char *name
)
63 /* Replicate pthread_setname_np's behavior */
64 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
68 pthread_set_name_np(pthread_self(), name
);
73 int lttng_pthread_getname_np(char *name
, size_t len
)
75 pthread_get_name_np(pthread_self(), name
, len
);
78 #elif defined(__linux__)
80 /* Fallback on prtctl on Linux */
81 #include <sys/prctl.h>
84 int lttng_pthread_setname_np(const char *name
)
86 /* Replicate pthread_setname_np's behavior */
87 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
90 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
94 int lttng_pthread_getname_np(char *name
, size_t len
)
96 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
100 #error "Please add pthread name support for your OS."
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.
109 int lttng_ust_setustprocname(void)
112 char name
[LTTNG_UST_ABI_PROCNAME_LEN
];
113 int limit
= LTTNG_UST_ABI_PROCNAME_LEN
- strlen(LTTNG_UST_PROCNAME_SUFFIX
) - 1;
116 * Get the current thread name.
118 ret
= lttng_pthread_getname_np(name
, LTTNG_UST_ABI_PROCNAME_LEN
);
128 ret
= sprintf(name
+ len
, LTTNG_UST_PROCNAME_SUFFIX
);
129 if (ret
!= strlen(LTTNG_UST_PROCNAME_SUFFIX
)) {
133 ret
= lttng_pthread_setname_np(name
);
142 #define ENODATA ENOMSG
145 #endif /* _UST_COMPAT_H */