/*
* Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
* Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
+ * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
*/
#include <pthread.h>
+#include <errno.h>
-/*
- * Limit imposed by Linux UST-sessiond ABI.
- */
-#define LTTNG_UST_PROCNAME_LEN 17
+#include <lttng/ust-abi.h>
#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
static inline
int lttng_pthread_setname_np(const char *name)
{
- return pthread_setname_np(pthread_self(), name);
+ return pthread_setname_np(pthread_self(), name);
}
static inline
int lttng_pthread_getname_np(char *name, size_t len)
{
- return pthread_getname_np(pthread_self(), name, len);
+ return pthread_getname_np(pthread_self(), name, len);
}
#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
static inline
int lttng_pthread_setname_np(const char *name)
{
- return pthread_setname_np(name);
+ return pthread_setname_np(name);
}
static inline
int lttng_pthread_getname_np(char *name, size_t len)
{
- return pthread_getname_np(name, len);
+ return pthread_getname_np(name, len);
}
-#else
-/*
- * For platforms without thread name support, do nothing.
- */
+#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
+
+#include <pthread_np.h>
static inline
int lttng_pthread_setname_np(const char *name)
{
- return -ENOSYS;
+ pthread_set_name_np(pthread_self(), name);
+ return 0;
}
static inline
int lttng_pthread_getname_np(char *name, size_t len)
{
- return -ENOSYS;
+ pthread_get_name_np(pthread_self(), name, len);
+ return 0;
+}
+#elif defined(__linux__)
+
+/* Fallback on prtctl on Linux */
+#include <sys/prctl.h>
+
+static inline
+int lttng_pthread_setname_np(const char *name)
+{
+ return prctl(PR_SET_NAME, name, 0, 0, 0);
}
-#endif
static inline
-void lttng_ust_getprocname(char *name)
+int lttng_pthread_getname_np(char *name, size_t len)
{
- lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
+ return prctl(PR_GET_NAME, name, 0, 0, 0);
}
+#else
+#error "Please add pthread name support for your OS."
+#endif
+
+/*
+ * If a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.
+ */
static inline
int lttng_ust_setustprocname(void)
{
int ret = 0, len;
- char name[LTTNG_UST_PROCNAME_LEN];
- int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
-
- lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
+ char name[LTTNG_UST_ABI_PROCNAME_LEN];
+ int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
+
+ /*
+ * Get the current thread name.
+ */
+ ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN);
+ if (ret) {
+ goto error;
+ }
len = strlen(name);
if (len > limit) {
return ret;
}
-
#include <errno.h>
#ifndef ENODATA
CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
/* Increment nesting before updating cache. */
cmm_barrier();
- lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]);
- URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0';
+ lttng_pthread_getname_np(URCU_TLS(cached_procname)[nesting], LTTNG_UST_ABI_PROCNAME_LEN);
+ URCU_TLS(cached_procname)[nesting][LTTNG_UST_ABI_PROCNAME_LEN - 1] = '\0';
/* Decrement nesting after updating cache. */
cmm_barrier();
CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
static
size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
{
- return LTTNG_UST_PROCNAME_LEN;
+ return LTTNG_UST_ABI_PROCNAME_LEN;
}
static
char *procname;
procname = wrapper_getprocname();
- chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN);
+ chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN);
}
static
field->event_field.type.atype = atype_array_nestable;
field->event_field.type.u.array_nestable.elem_type =
&procname_array_elem_type;
- field->event_field.type.u.array_nestable.length = LTTNG_UST_PROCNAME_LEN;
+ field->event_field.type.u.array_nestable.length = LTTNG_UST_ABI_PROCNAME_LEN;
field->get_size = procname_get_size;
field->record = procname_record;
field->get_value = procname_get_value;
[Have function pthread_setname_np(const char*)])],
[AC_MSG_RESULT(no)])
+# FreeBSD
+AC_MSG_CHECKING(for pthread_set_name_np(pthread_t, const char*))
+AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM(
+ [#include <pthread.h>
+ #include <pthread_np.h>],
+ [pthread_set_name_np(pthread_self(), "example")])],
+ [AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_PTHREAD_SET_NAME_NP_WITH_TID,1,
+ [Have function pthread_set_name_np(pthread_t, const char*)])],
+ [AC_MSG_RESULT(no)])
+
LDFLAGS=$lttng_pthread_setname_np_save_LDFLAGS
LIBS=$lttng_pthread_setname_np_save_LIBS