Add -ust to the name of UST threads of the application
[lttng-ust.git] / liblttng-ust / compat.h
CommitLineData
b728d87e
MD
1#ifndef _UST_COMPAT_H
2#define _UST_COMPAT_H
3
4/*
e92f3e28 5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
01f0e40c 6 * Copyright (C) 2016 Raphaël Beamonte <raphael.beamonte@gmail.com>
b728d87e 7 *
e92f3e28
MD
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
b728d87e 12 *
e92f3e28
MD
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
b728d87e
MD
21 */
22
08114193
MD
23/*
24 * lttng_ust_getprocname.
25 */
26#ifdef __linux__
27
28#include <sys/prctl.h>
29
30#define LTTNG_UST_PROCNAME_LEN 17
31
32static inline
33void lttng_ust_getprocname(char *name)
34{
35 (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
36}
37
01f0e40c
RB
38/*
39 * If pthread_setname_np is available.
40 */
41#ifdef HAVE_PTHREAD_SETNAME_NP
42static inline
43int lttng_pthread_setname_np(pthread_t thread, const char *name)
44{
45 return pthread_setname_np(thread, name);
46}
47#endif
48
08114193 49#elif defined(__FreeBSD__)
01f0e40c 50
08114193
MD
51#include <stdlib.h>
52#include <string.h>
e4486ebc 53
08114193
MD
54/*
55 * Limit imposed by Linux UST-sessiond ABI.
56 */
57#define LTTNG_UST_PROCNAME_LEN 17
58
59/*
60 * Acts like linux prctl, the string is not necessarily 0-terminated if
61 * 16-byte long.
62 */
e4486ebc 63static inline
08114193 64void lttng_ust_getprocname(char *name)
e4486ebc 65{
08114193 66 const char *bsd_name;
e4486ebc 67
08114193
MD
68 bsd_name = getprogname();
69 if (!bsd_name)
70 name[0] = '\0';
7c41892f 71 else
1c7b4a9b 72 strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
e4486ebc 73}
08114193 74
01f0e40c
RB
75/*
76 * If pthread_set_name_np is available.
77 */
78#ifdef HAVE_PTHREAD_SET_NAME_NP
79static inline
80int lttng_pthread_setname_np(pthread_t thread, const char *name)
81{
82 return pthread_set_name_np(thread, name);
83}
84#endif
85
86#endif
87
88/*
89 * If a pthread setname/set_name function is available, declare
90 * the setustprocname() function that will add '-ust' to the end
91 * of the current process name, while truncating it if needed.
92 */
93#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP)
94#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
95
96#include <pthread.h>
97
98static inline
99int lttng_ust_setustprocname(void)
100{
101 int ret = 0, len;
102 char name[LTTNG_UST_PROCNAME_LEN];
103 int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX);
104
105 lttng_ust_getprocname(name);
106
107 len = strlen(name);
108 if (len > limit) {
109 len = limit;
110 }
111
112 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
113 if (ret) {
114 goto error;
115 }
116
117 ret = lttng_pthread_setname_np(pthread_self(), name);
118
119error:
120 return ret;
121}
122#else
123static inline
124int lttng_ust_setustprocname(void)
125{
126 return 0;
127}
08114193
MD
128#endif
129
bdcf8d82
MD
130#include <errno.h>
131
132#ifndef ENODATA
133#define ENODATA ENOMSG
134#endif
135
b728d87e 136#endif /* _UST_COMPAT_H */
This page took 0.032541 seconds and 4 git commands to generate.