From: Michael Jeanson Date: Sat, 3 Apr 2021 00:52:58 +0000 (-0400) Subject: Split and move compat.h to 'common/compat/' X-Git-Tag: v2.13.0-rc1~128 X-Git-Url: https://git.lttng.org/?p=lttng-ust.git;a=commitdiff_plain;h=27b98e6c9ed3fbfc9ee84b56582892db3009b1b7 Split and move compat.h to 'common/compat/' Move compat.h to 'src/common/compat/' and split it in specific compat headers. This is part of an effort to standardize our autotools setup across projects to simplify maintenance. Change-Id: Ic34ddc11acf81d0463d69e23986d83c29be9023b Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 1c6fcbf0..fa2a9da8 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -15,11 +15,14 @@ noinst_HEADERS = \ logging.h \ macros.h \ patient.h \ + procname.h \ safe-snprintf.h noinst_HEADERS += \ compat/dlfcn.h \ + compat/errno.h \ compat/mmap.h \ + compat/pthread.h \ compat/tid.h # These headers should be moved to the public headers when tested and diff --git a/src/common/compat/errno.h b/src/common/compat/errno.h new file mode 100644 index 00000000..c761d6e9 --- /dev/null +++ b/src/common/compat/errno.h @@ -0,0 +1,16 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2020 Michael Jeanson + */ + +#ifndef _UST_COMMON_COMPAT_ERRNO_H +#define _UST_COMMON_COMPAT_ERRNO_H + +#include + +#ifndef ENODATA +#define ENODATA ENOMSG +#endif + +#endif /* _UST_COMMON_COMPAT_ERRNO_H */ diff --git a/src/common/compat/pthread.h b/src/common/compat/pthread.h new file mode 100644 index 00000000..8779fa7e --- /dev/null +++ b/src/common/compat/pthread.h @@ -0,0 +1,108 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2011 Mathieu Desnoyers + * Copyright (C) 2016 Raphaël Beamonte + * Copyright (C) 2020 Michael Jeanson + */ + +#ifndef _UST_COMMON_COMPAT_PTHREAD_H +#define _UST_COMMON_COMPAT_PTHREAD_H + +#include +#include + +#include + +#ifdef __FreeBSD__ +#include +#endif + +#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) +static inline +int lttng_pthread_setname_np(const char *name) +{ + /* + * Some implementations don't error out, replicate this behavior for + * consistency. + */ + if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { + return ERANGE; + } + + return pthread_setname_np(pthread_self(), name); +} +#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) +static inline +int lttng_pthread_setname_np(const char *name) +{ + return pthread_setname_np(name); +} +#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) + +static inline +int lttng_pthread_setname_np(const char *name) +{ + /* Replicate pthread_setname_np's behavior */ + if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { + return ERANGE; + } + + pthread_set_name_np(pthread_self(), name); + return 0; +} +#elif defined(__linux__) + +/* Fallback on prtctl on Linux */ +#include + +static inline +int lttng_pthread_setname_np(const char *name) +{ + /* Replicate pthread_setname_np's behavior */ + if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { + return ERANGE; + } + return prctl(PR_SET_NAME, name, 0, 0, 0); +} +#else +#error "Please add pthread set name support for your OS." +#endif + + +#if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID) +static inline +int lttng_pthread_getname_np(char *name, size_t len) +{ + return pthread_getname_np(pthread_self(), name, len); +} +#elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID) +static inline +int lttng_pthread_getname_np(char *name, size_t len) +{ + return pthread_getname_np(name, len); +} +#elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID) + +static inline +int lttng_pthread_getname_np(char *name, size_t len) +{ + pthread_get_name_np(pthread_self(), name, len); + return 0; +} +#elif defined(__linux__) + +/* Fallback on prtctl on Linux */ +#include + +static inline +int lttng_pthread_getname_np(char *name, size_t len) +{ + return prctl(PR_GET_NAME, name, 0, 0, 0); +} + +#else +#error "Please add pthread get name support for your OS." +#endif + +#endif /* _UST_COMMON_COMPAT_PTHREAD_H */ diff --git a/src/common/procname.h b/src/common/procname.h new file mode 100644 index 00000000..061202ce --- /dev/null +++ b/src/common/procname.h @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2011 Mathieu Desnoyers + * Copyright (C) 2016 Raphaël Beamonte + * Copyright (C) 2020 Michael Jeanson + */ + +#ifndef _UST_COMMON_PROCNAME_H +#define _UST_COMMON_PROCNAME_H + +#include +#include + +#include "common/compat/pthread.h" + +#define LTTNG_UST_PROCNAME_SUFFIX "-ust" + +/* + * 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_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) { + len = limit; + } + + ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); + if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { + goto error; + } + + ret = lttng_pthread_setname_np(name); + +error: + return ret; +} + +#endif /* _UST_COMMON_PROCNAME_H */ diff --git a/src/common/ringbuffer/ring_buffer_frontend.c b/src/common/ringbuffer/ring_buffer_frontend.c index 9074dbcb..d08fab75 100644 --- a/src/common/ringbuffer/ring_buffer_frontend.c +++ b/src/common/ringbuffer/ring_buffer_frontend.c @@ -62,7 +62,7 @@ #include "frontend.h" #include "shm.h" #include "rb-init.h" -#include "liblttng-ust/compat.h" /* For ENODATA */ +#include "common/compat/errno.h" /* For ENODATA */ /* Print DBG() messages about events lost only every 1048576 hits */ #define DBG_PRINT_NR_LOST (1UL << 20) diff --git a/src/common/ustcomm.c b/src/common/ustcomm.c index 2d445cba..ddd23ba8 100644 --- a/src/common/ustcomm.c +++ b/src/common/ustcomm.c @@ -28,7 +28,7 @@ #include "common/logging.h" #include "../liblttng-ust/ust-events-internal.h" -#include "../liblttng-ust/compat.h" +#include "common/compat/pthread.h" #define USTCOMM_CODE_OFFSET(code) \ (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1)) diff --git a/src/liblttng-ust/Makefile.am b/src/liblttng-ust/Makefile.am index d064fad6..bca09ad8 100644 --- a/src/liblttng-ust/Makefile.am +++ b/src/liblttng-ust/Makefile.am @@ -80,7 +80,6 @@ liblttng_ust_runtime_la_SOURCES = \ tracepoint-internal.h \ ust-events-internal.h \ clock.h \ - compat.h \ wait.h \ jhash.h \ lttng-ust-uuid.h \ diff --git a/src/liblttng-ust/compat.h b/src/liblttng-ust/compat.h deleted file mode 100644 index 7f0f5027..00000000 --- a/src/liblttng-ust/compat.h +++ /dev/null @@ -1,154 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-only - * - * Copyright (C) 2011 Mathieu Desnoyers - * Copyright (C) 2016 Raphaël Beamonte - * Copyright (C) 2020 Michael Jeanson - */ - -#ifndef _UST_COMPAT_H -#define _UST_COMPAT_H - -#include -#include -#include - -#ifdef __FreeBSD__ -#include -#endif - -#include - -#define LTTNG_UST_PROCNAME_SUFFIX "-ust" - - -#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) -static inline -int lttng_pthread_setname_np(const char *name) -{ - /* - * Some implementations don't error out, replicate this behavior for - * consistency. - */ - if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { - return ERANGE; - } - - return pthread_setname_np(pthread_self(), name); -} -#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) -static inline -int lttng_pthread_setname_np(const char *name) -{ - return pthread_setname_np(name); -} -#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) - -static inline -int lttng_pthread_setname_np(const char *name) -{ - /* Replicate pthread_setname_np's behavior */ - if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { - return ERANGE; - } - - pthread_set_name_np(pthread_self(), name); - return 0; -} -#elif defined(__linux__) - -/* Fallback on prtctl on Linux */ -#include - -static inline -int lttng_pthread_setname_np(const char *name) -{ - /* Replicate pthread_setname_np's behavior */ - if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { - return ERANGE; - } - return prctl(PR_SET_NAME, name, 0, 0, 0); -} -#else -#error "Please add pthread set name support for your OS." -#endif - - -#if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID) -static inline -int lttng_pthread_getname_np(char *name, size_t len) -{ - return pthread_getname_np(pthread_self(), name, len); -} -#elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID) -static inline -int lttng_pthread_getname_np(char *name, size_t len) -{ - return pthread_getname_np(name, len); -} -#elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID) - -static inline -int lttng_pthread_getname_np(char *name, size_t len) -{ - pthread_get_name_np(pthread_self(), name, len); - return 0; -} -#elif defined(__linux__) - -/* Fallback on prtctl on Linux */ -#include - -static inline -int lttng_pthread_getname_np(char *name, size_t len) -{ - return prctl(PR_GET_NAME, name, 0, 0, 0); -} - -#else -#error "Please add pthread get 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_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) { - len = limit; - } - - ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); - if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { - goto error; - } - - ret = lttng_pthread_setname_np(name); - -error: - return ret; -} - -#include - -#ifndef ENODATA -#define ENODATA ENOMSG -#endif - -#endif /* _UST_COMPAT_H */ diff --git a/src/liblttng-ust/fd-tracker.c b/src/liblttng-ust/fd-tracker.c index df14f0e0..026a9327 100644 --- a/src/liblttng-ust/fd-tracker.c +++ b/src/liblttng-ust/fd-tracker.c @@ -30,8 +30,6 @@ #include #include "common/logging.h" -#include "liblttng-ust/compat.h" - /* Operations on the fd set. */ #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd) #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE])) diff --git a/src/liblttng-ust/lttng-context-procname.c b/src/liblttng-ust/lttng-context-procname.c index 96a1b2ca..bbc4687a 100644 --- a/src/liblttng-ust/lttng-context-procname.c +++ b/src/liblttng-ust/lttng-context-procname.c @@ -13,7 +13,7 @@ #include #include #include -#include "compat.h" +#include "common/compat/pthread.h" #include "lttng-tracer-core.h" #include "context-internal.h" diff --git a/src/liblttng-ust/lttng-events.c b/src/liblttng-ust/lttng-events.c index ccd3dfd9..d5e08849 100644 --- a/src/liblttng-ust/lttng-events.c +++ b/src/liblttng-ust/lttng-events.c @@ -40,7 +40,6 @@ #include "common/dynamic-type.h" #include "common/ust-context-provider.h" #include "error.h" -#include "compat.h" #include "lttng-ust-uuid.h" #include "tracepoint-internal.h" diff --git a/src/liblttng-ust/lttng-tracer.h b/src/liblttng-ust/lttng-tracer.h index b02e4a80..b55388ef 100644 --- a/src/liblttng-ust/lttng-tracer.h +++ b/src/liblttng-ust/lttng-tracer.h @@ -15,7 +15,6 @@ #include #include #include "lttng-tracer-core.h" -#include "compat.h" /* Tracer properties */ #define CTF_MAGIC_NUMBER 0xC1FC1FC1 diff --git a/src/liblttng-ust/lttng-ust-comm.c b/src/liblttng-ust/lttng-ust-comm.c index a753206f..15cb5550 100644 --- a/src/liblttng-ust/lttng-ust-comm.c +++ b/src/liblttng-ust/lttng-ust-comm.c @@ -45,7 +45,8 @@ #include "common/macros.h" #include "tracepoint-internal.h" #include "lttng-tracer-core.h" -#include "compat.h" +#include "common/compat/pthread.h" +#include "common/procname.h" #include "common/ringbuffer/rb-init.h" #include "lttng-ust-statedump.h" #include "clock.h" diff --git a/src/liblttng-ust/lttng-ust-statedump-provider.h b/src/liblttng-ust/lttng-ust-statedump-provider.h index cab628aa..c6600f34 100644 --- a/src/liblttng-ust/lttng-ust-statedump-provider.h +++ b/src/liblttng-ust/lttng-ust-statedump-provider.h @@ -19,7 +19,6 @@ extern "C" { #include #include #include -#include "compat.h" #define LTTNG_UST_STATEDUMP_PROVIDER #include diff --git a/src/liblttng-ust/lttng-ust-statedump.c b/src/liblttng-ust/lttng-ust-statedump.c index cd8bc7c8..cd4df182 100644 --- a/src/liblttng-ust/lttng-ust-statedump.c +++ b/src/liblttng-ust/lttng-ust-statedump.c @@ -22,7 +22,6 @@ #include "lttng-ust-statedump.h" #include "jhash.h" #include "getenv.h" -#include "compat.h" #include "ust-events-internal.h" #define TRACEPOINT_DEFINE diff --git a/tests/unit/pthread_name/pthread_name.c b/tests/unit/pthread_name/pthread_name.c index 6faacf68..a662d826 100644 --- a/tests/unit/pthread_name/pthread_name.c +++ b/tests/unit/pthread_name/pthread_name.c @@ -6,7 +6,8 @@ #include #include -#include "../../../src/liblttng-ust/compat.h" +#include "common/compat/pthread.h" +#include "common/procname.h" #include "tap.h"