port: fix pthread_setname_np integration
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 15 Oct 2020 21:11:21 +0000 (17:11 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 15 Oct 2020 21:17:32 +0000 (17:17 -0400)
The detection was broken and initialy went undetected because of the
failover code. Replace it with a hard fail and fix the detection for
each platform.

Also, replace the private LTTNG_UST_PROCNAME_LEN with
LTTNG_UST_ABI_PROCNAME_LEN from the public headers. The limit of 16
bytes does in fact include the terminating null byte contrary to what
was defined in LTTNG_UST_PROCNAME_LEN. This was not catched before since
the string is silently truncated if it exceeds 16 bytes.

Change-Id: I1ea95dfd882dfeb80fdc3d71ebec6618b1324a79
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-comm/lttng-ust-comm.c
liblttng-ust/compat.h
liblttng-ust/lttng-context-procname.c
liblttng-ust/lttng-ust-comm.c
liblttng-ust/lttng-ust-statedump-provider.h
m4/lttng_pthread_setname_np.m4

index 619424cd813a1cebc7fdd1de99209aac9f3f1d45..2447c9a938492f78f491a5868b7102f07ef157a2 100644 (file)
@@ -754,7 +754,7 @@ int ustcomm_send_reg_msg(int sock,
        reg_msg.uint64_t_alignment = uint64_t_alignment;
        reg_msg.long_alignment = long_alignment;
        reg_msg.socket_type = type;
-       lttng_ust_getprocname(reg_msg.name);
+       lttng_pthread_getname_np(reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
        memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
 
        len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
index 7d1ee590326fd1b9616569420b32846d4e458f7f..69761880e125fbff0504320697c22203180b7d97 100644 (file)
@@ -4,6 +4,7 @@
 /*
  * 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) {
@@ -102,7 +126,6 @@ error:
        return ret;
 }
 
-
 #include <errno.h>
 
 #ifndef ENODATA
index c6660e3c02319334989c44d277fdef6179ed297f..238ec10a5ccbf66077e65411f8e81cd32e02b8e3 100644 (file)
@@ -58,8 +58,8 @@ char *wrapper_getprocname(void)
                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);
@@ -79,7 +79,7 @@ void lttng_context_procname_reset(void)
 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
@@ -90,7 +90,7 @@ void procname_record(struct lttng_ctx_field *field,
        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
@@ -118,7 +118,7 @@ int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
        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;
index 6258bfd2485691eae04411278f1bc2ed111d58c0..b265bcd3c61e254c9e8dc889d56fef389f2c18e1 100644 (file)
@@ -265,7 +265,7 @@ struct sock_info {
        int statedump_pending;
        int initial_statedump_done;
        /* Keep procname for statedump */
-       char procname[LTTNG_UST_PROCNAME_LEN];
+       char procname[LTTNG_UST_ABI_PROCNAME_LEN];
 };
 
 /* Socket from app (connect) to session daemon (listen) for communication */
@@ -482,7 +482,7 @@ int setup_global_apps(void)
        }
 
        global_apps.allowed = 1;
-       lttng_ust_getprocname(global_apps.procname);
+       lttng_pthread_getname_np(global_apps.procname, LTTNG_UST_ABI_PROCNAME_LEN);
 error:
        return ret;
 }
@@ -528,7 +528,7 @@ int setup_local_apps(void)
                goto end;
        }
 
-       lttng_ust_getprocname(local_apps.procname);
+       lttng_pthread_getname_np(local_apps.procname, LTTNG_UST_ABI_PROCNAME_LEN);
 end:
        return ret;
 }
index 474e2d22e34005a23c60cf6b4817d502847ccbba..ea8d5ed3702b1c21045d4feea88e3a8ed6316116 100644 (file)
@@ -99,7 +99,7 @@ TRACEPOINT_EVENT(lttng_ust_statedump, procname,
                char *, name
        ),
        TP_FIELDS(
-               ctf_array_text(char, procname, name, LTTNG_UST_PROCNAME_LEN)
+               ctf_array_text(char, procname, name, LTTNG_UST_ABI_PROCNAME_LEN)
        )
 )
 
index a8526e8d23eac3985a3d0bcb068fafe5343ab261..6eff705baa3b7a6d00f4c480d19f3c02f7d2b5b1 100644 (file)
@@ -63,6 +63,18 @@ AC_LINK_IFELSE(
         [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
 
This page took 0.029895 seconds and 4 git commands to generate.