2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
6 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
17 #include <pthread_np.h>
20 #include <lttng/ust-abi.h>
22 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
25 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
27 int lttng_pthread_setname_np(const char *name
)
30 * Some implementations don't error out, replicate this behavior for
33 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
37 return pthread_setname_np(pthread_self(), name
);
39 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
41 int lttng_pthread_setname_np(const char *name
)
43 return pthread_setname_np(name
);
45 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
48 int lttng_pthread_setname_np(const char *name
)
50 /* Replicate pthread_setname_np's behavior */
51 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
55 pthread_set_name_np(pthread_self(), name
);
58 #elif defined(__linux__)
60 /* Fallback on prtctl on Linux */
61 #include <sys/prctl.h>
64 int lttng_pthread_setname_np(const char *name
)
66 /* Replicate pthread_setname_np's behavior */
67 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
70 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
73 #error "Please add pthread set name support for your OS."
77 #if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID)
79 int lttng_pthread_getname_np(char *name
, size_t len
)
81 return pthread_getname_np(pthread_self(), name
, len
);
83 #elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID)
85 int lttng_pthread_getname_np(char *name
, size_t len
)
87 return pthread_getname_np(name
, len
);
89 #elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID)
92 int lttng_pthread_getname_np(char *name
, size_t len
)
94 pthread_get_name_np(pthread_self(), name
, len
);
97 #elif defined(__linux__)
99 /* Fallback on prtctl on Linux */
100 #include <sys/prctl.h>
103 int lttng_pthread_getname_np(char *name
, size_t len
)
105 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
109 #error "Please add pthread get name support for your OS."
113 * If a pthread setname/set_name function is available, declare
114 * the setustprocname() function that will add '-ust' to the end
115 * of the current process name, while truncating it if needed.
118 int lttng_ust_setustprocname(void)
121 char name
[LTTNG_UST_ABI_PROCNAME_LEN
];
122 int limit
= LTTNG_UST_ABI_PROCNAME_LEN
- strlen(LTTNG_UST_PROCNAME_SUFFIX
) - 1;
125 * Get the current thread name.
127 ret
= lttng_pthread_getname_np(name
, LTTNG_UST_ABI_PROCNAME_LEN
);
137 ret
= sprintf(name
+ len
, LTTNG_UST_PROCNAME_SUFFIX
);
138 if (ret
!= strlen(LTTNG_UST_PROCNAME_SUFFIX
)) {
142 ret
= lttng_pthread_setname_np(name
);
151 #define ENODATA ENOMSG
154 #endif /* _UST_COMPAT_H */