From 0db3d6ee9be2906d02955fedee802338bcfaef3a Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Thu, 15 Oct 2020 17:11:21 -0400 Subject: [PATCH] port: fix pthread_setname_np integration 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 Signed-off-by: Mathieu Desnoyers --- liblttng-ust-comm/lttng-ust-comm.c | 2 +- liblttng-ust/compat.h | 67 ++++++++++++++------- liblttng-ust/lttng-context-procname.c | 10 +-- liblttng-ust/lttng-ust-comm.c | 6 +- liblttng-ust/lttng-ust-statedump-provider.h | 2 +- m4/lttng_pthread_setname_np.m4 | 12 ++++ 6 files changed, 67 insertions(+), 32 deletions(-) diff --git a/liblttng-ust-comm/lttng-ust-comm.c b/liblttng-ust-comm/lttng-ust-comm.c index 619424cd..2447c9a9 100644 --- a/liblttng-ust-comm/lttng-ust-comm.c +++ b/liblttng-ust-comm/lttng-ust-comm.c @@ -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, ®_msg, sizeof(reg_msg)); diff --git a/liblttng-ust/compat.h b/liblttng-ust/compat.h index 7d1ee590..69761880 100644 --- a/liblttng-ust/compat.h +++ b/liblttng-ust/compat.h @@ -4,6 +4,7 @@ /* * Copyright (C) 2011 Mathieu Desnoyers * Copyright (C) 2016 Raphaël Beamonte + * Copyright (C) 2020 Michael Jeanson * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,11 +22,9 @@ */ #include +#include -/* - * Limit imposed by Linux UST-sessiond ABI. - */ -#define LTTNG_UST_PROCNAME_LEN 17 +#include #define LTTNG_UST_PROCNAME_SUFFIX "-ust" @@ -34,57 +33,82 @@ 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 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 + +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 #ifndef ENODATA diff --git a/liblttng-ust/lttng-context-procname.c b/liblttng-ust/lttng-context-procname.c index c6660e3c..238ec10a 100644 --- a/liblttng-ust/lttng-context-procname.c +++ b/liblttng-ust/lttng-context-procname.c @@ -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; diff --git a/liblttng-ust/lttng-ust-comm.c b/liblttng-ust/lttng-ust-comm.c index 6258bfd2..b265bcd3 100644 --- a/liblttng-ust/lttng-ust-comm.c +++ b/liblttng-ust/lttng-ust-comm.c @@ -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; } diff --git a/liblttng-ust/lttng-ust-statedump-provider.h b/liblttng-ust/lttng-ust-statedump-provider.h index 474e2d22..ea8d5ed3 100644 --- a/liblttng-ust/lttng-ust-statedump-provider.h +++ b/liblttng-ust/lttng-ust-statedump-provider.h @@ -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) ) ) diff --git a/m4/lttng_pthread_setname_np.m4 b/m4/lttng_pthread_setname_np.m4 index a8526e8d..6eff705b 100644 --- a/m4/lttng_pthread_setname_np.m4 +++ b/m4/lttng_pthread_setname_np.m4 @@ -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 + #include ], + [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 -- 2.34.1