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
27 #include <lttng/ust-abi.h>
29 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
32 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
34 int lttng_pthread_setname_np(const char *name
)
36 return pthread_setname_np(pthread_self(), name
);
40 int lttng_pthread_getname_np(char *name
, size_t len
)
42 return pthread_getname_np(pthread_self(), name
, len
);
44 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
46 int lttng_pthread_setname_np(const char *name
)
48 return pthread_setname_np(name
);
52 int lttng_pthread_getname_np(char *name
, size_t len
)
54 return pthread_getname_np(name
, len
);
56 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
58 #include <pthread_np.h>
60 int lttng_pthread_setname_np(const char *name
)
62 pthread_set_name_np(pthread_self(), name
);
67 int lttng_pthread_getname_np(char *name
, size_t len
)
69 pthread_get_name_np(pthread_self(), name
, len
);
72 #elif defined(__linux__)
74 /* Fallback on prtctl on Linux */
75 #include <sys/prctl.h>
78 int lttng_pthread_setname_np(const char *name
)
80 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
84 int lttng_pthread_getname_np(char *name
, size_t len
)
86 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
90 #error "Please add pthread name support for your OS."
94 * If a pthread setname/set_name function is available, declare
95 * the setustprocname() function that will add '-ust' to the end
96 * of the current process name, while truncating it if needed.
99 int lttng_ust_setustprocname(void)
102 char name
[LTTNG_UST_ABI_PROCNAME_LEN
];
103 int limit
= LTTNG_UST_ABI_PROCNAME_LEN
- strlen(LTTNG_UST_PROCNAME_SUFFIX
) - 1;
106 * Get the current thread name.
108 ret
= lttng_pthread_getname_np(name
, LTTNG_UST_ABI_PROCNAME_LEN
);
118 ret
= sprintf(name
+ len
, LTTNG_UST_PROCNAME_SUFFIX
);
119 if (ret
!= strlen(LTTNG_UST_PROCNAME_SUFFIX
)) {
123 ret
= lttng_pthread_setname_np(name
);
132 #define ENODATA ENOMSG
135 #endif /* _UST_COMPAT_H */