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>
16 #include <lttng/ust-abi.h>
18 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
21 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
23 int lttng_pthread_setname_np(const char *name
)
26 * Some implementations don't error out, replicate this behavior for
29 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
33 return pthread_setname_np(pthread_self(), name
);
37 int lttng_pthread_getname_np(char *name
, size_t len
)
39 return pthread_getname_np(pthread_self(), name
, len
);
41 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
43 int lttng_pthread_setname_np(const char *name
)
45 return pthread_setname_np(name
);
49 int lttng_pthread_getname_np(char *name
, size_t len
)
51 return pthread_getname_np(name
, len
);
53 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
55 #include <pthread_np.h>
57 int lttng_pthread_setname_np(const char *name
)
59 /* Replicate pthread_setname_np's behavior */
60 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
64 pthread_set_name_np(pthread_self(), name
);
69 int lttng_pthread_getname_np(char *name
, size_t len
)
71 pthread_get_name_np(pthread_self(), name
, len
);
74 #elif defined(__linux__)
76 /* Fallback on prtctl on Linux */
77 #include <sys/prctl.h>
80 int lttng_pthread_setname_np(const char *name
)
82 /* Replicate pthread_setname_np's behavior */
83 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
86 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
90 int lttng_pthread_getname_np(char *name
, size_t len
)
92 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
96 #error "Please add pthread name support for your OS."
100 * If a pthread setname/set_name function is available, declare
101 * the setustprocname() function that will add '-ust' to the end
102 * of the current process name, while truncating it if needed.
105 int lttng_ust_setustprocname(void)
108 char name
[LTTNG_UST_ABI_PROCNAME_LEN
];
109 int limit
= LTTNG_UST_ABI_PROCNAME_LEN
- strlen(LTTNG_UST_PROCNAME_SUFFIX
) - 1;
112 * Get the current thread name.
114 ret
= lttng_pthread_getname_np(name
, LTTNG_UST_ABI_PROCNAME_LEN
);
124 ret
= sprintf(name
+ len
, LTTNG_UST_PROCNAME_SUFFIX
);
125 if (ret
!= strlen(LTTNG_UST_PROCNAME_SUFFIX
)) {
129 ret
= lttng_pthread_setname_np(name
);
138 #define ENODATA ENOMSG
141 #endif /* _UST_COMPAT_H */