Initialize liblttng-ust-common in dependent libraries
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 13 Apr 2021 20:39:18 +0000 (16:39 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 19 Apr 2021 17:31:27 +0000 (13:31 -0400)
liblttng-ust-common is a new public library introduced in this
developement cycle to contain the shared state between liblttng-ust and
our other libs that can't link directly on liblttng-ust. The constructor
of each of these libraries should now call the liblttng-ust-common
constructor early in their own constructors to ensure proper execution
order.

  * Rename library constructors with the _ctor suffix to help
    distinguish them from other _init functions.
  * Hide the fd-tracker init fonction, the liblttng-ust-common ctor
    should be called instead.
  * All libraries linked on 'liblttng-ust-common' call its constructor
    early in theirs.

Change-Id: I8e083f392b4ef2427addd00120cffc61a6314697
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 files changed:
include/Makefile.am
include/lttng/ust-common.h [new file with mode: 0644]
include/lttng/ust-libc-wrapper.h
src/common/ust-fd.h
src/lib/lttng-ust-common/Makefile.am
src/lib/lttng-ust-common/fd-tracker.c
src/lib/lttng-ust-common/fd-tracker.h [new file with mode: 0644]
src/lib/lttng-ust-common/ust-common.c
src/lib/lttng-ust-ctl/ustctl.c
src/lib/lttng-ust-fd/Makefile.am
src/lib/lttng-ust-fd/lttng-ust-fd.c
src/lib/lttng-ust-libc-wrapper/lttng-ust-malloc.c
src/lib/lttng-ust-tracepoint/tracepoint.c
src/lib/lttng-ust/lttng-ust-comm.c

index 287fe13dafa842a1f26b7e059dba0abd30f7aed4..72239e5a1a3c9cb82cfd682c65f3fa1a681c958e 100644 (file)
@@ -15,6 +15,7 @@ nobase_include_HEADERS = \
        lttng/ust-tracepoint-event-nowrite.h \
        lttng/ust-arch.h \
        lttng/ust-events.h \
+       lttng/ust-common.h \
        lttng/ust-ctl.h \
        lttng/ust-abi.h \
        lttng/ust-tracer.h \
diff --git a/include/lttng/ust-common.h b/include/lttng/ust-common.h
new file mode 100644 (file)
index 0000000..8396e6b
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2021 Michael Jeanson <mjeanson@efficios.com>
+ *
+ * Public symbols of liblttng-ust-common.so
+ */
+
+#ifndef _LTTNG_UST_COMMON_H
+#define _LTTNG_UST_COMMON_H
+
+/*
+ * The liblttng-ust-common constructor is public so it can be called in the
+ * constructors of client libraries since there is no reliable way to guarantee
+ * the execution order of constructors across shared library.
+ */
+void lttng_ust_common_ctor(void)
+       __attribute__((constructor));
+
+#endif
index 2eaf5dc6d4c7016827f6ff0928fea0c1e11ace65..0853997bd838f79b277cb7681dd4a34b6c5f897b 100644 (file)
@@ -20,7 +20,7 @@
  * function in liblttng-ust that is overridden by the one in
  * liblttng-ust-wrapper-libc when it's preloaded.
  */
-void lttng_ust_libc_wrapper_malloc_init(void)
+void lttng_ust_libc_wrapper_malloc_ctor(void)
        __attribute__((constructor));
 
 #endif
index 9d14583c2135ba0db104410457f091dfc4148850..eb9c525cbe80af985b5b3d020b1c5ac15c16ea44 100644 (file)
@@ -17,7 +17,6 @@
 
 #include <stdio.h>
 
-void lttng_ust_init_fd_tracker(void);
 int lttng_ust_add_fd_to_tracker(int fd);
 void lttng_ust_delete_fd_from_tracker(int fd);
 void lttng_ust_lock_fd_tracker(void);
index bb32360f1bb1401f88f8b5c5d4cc7f89abeaedac..5d0b2242f26a8d8e250474f2bb4f5f6c0df6de1a 100644 (file)
@@ -4,6 +4,7 @@ lib_LTLIBRARIES = liblttng-ust-common.la
 
 liblttng_ust_common_la_SOURCES = \
        fd-tracker.c \
+       fd-tracker.h \
        ust-common.c \
        lttng-ust-urcu.c \
        lttng-ust-urcu-pointer.c
index 026a9327f297ac016ca009763716eb308e752e37..54f7deb4c027bd89fe14ea51e2a2d5d269252596 100644 (file)
@@ -30,6 +30,8 @@
 #include <lttng/ust-error.h>
 #include "common/logging.h"
 
+#include "lib/lttng-ust-common/fd-tracker.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]))
@@ -87,7 +89,7 @@ void lttng_ust_fixup_fd_tracker_tls(void)
  * process. This will be called during the constructor execution
  * and will also be called in the child after fork via lttng_ust_init.
  */
-void lttng_ust_init_fd_tracker(void)
+void lttng_ust_fd_tracker_init(void)
 {
        struct rlimit rlim;
        int i;
@@ -269,7 +271,7 @@ int lttng_ust_add_fd_to_tracker(int fd)
         * Ensure the tracker is initialized when called from
         * constructors.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
        assert(URCU_TLS(ust_fd_mutex_nest));
 
        if (IS_FD_STD(fd)) {
@@ -301,7 +303,7 @@ void lttng_ust_delete_fd_from_tracker(int fd)
         * Ensure the tracker is initialized when called from
         * constructors.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
 
        assert(URCU_TLS(ust_fd_mutex_nest));
        /* Not a valid fd. */
@@ -327,7 +329,7 @@ int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd))
         * Ensure the tracker is initialized when called from
         * constructors.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
 
        /*
         * If called from lttng-ust, we directly call close without
@@ -363,7 +365,7 @@ int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream))
         * Ensure the tracker is initialized when called from
         * constructors.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
 
        /*
         * If called from lttng-ust, we directly call fclose without
@@ -418,7 +420,7 @@ int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
         * Ensure the tracker is initialized when called from
         * constructors.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
 
        if (lowfd < 0) {
                /*
diff --git a/src/lib/lttng-ust-common/fd-tracker.h b/src/lib/lttng-ust-common/fd-tracker.h
new file mode 100644 (file)
index 0000000..07c1372
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2021 Michael Jeanson <mjeanson@efficios.com>
+ */
+
+#ifndef _LTTNG_UST_COMMON_FD_TRACKER_H
+#define _LTTNG_UST_COMMON_FD_TRACKER_H
+
+void lttng_ust_fd_tracker_init(void)
+       __attribute__((visibility("hidden")));
+
+#endif
index cefff67a82d721d0c63c6940e86563ed9cf53c38..e163383d2b458bca3b9386f91b680cb0da825ea9 100644 (file)
@@ -4,18 +4,24 @@
  * Copyright (C) 2021 Michael Jeanson <mjeanson@efficios.com>
  */
 
+#include <lttng/ust-common.h>
+
 #include "common/logging.h"
 #include "common/ust-fd.h"
+#include "common/getenv.h"
+
+#include "lib/lttng-ust-common/fd-tracker.h"
 
-static
-void lttng_ust_common_init(void)
-       __attribute__((constructor));
-static
-void lttng_ust_common_init(void)
+/*
+ * The liblttng-ust-common constructor, initialize the internal shared state.
+ * Libraries linking on liblttng-ust-common should also call this early in
+ * their constructor since there is no reliable way to guarantee the execution
+ * order of constructors across shared library.
+ */
+void lttng_ust_common_ctor(void)
 {
        /*
-        * Initialize the fd-tracker, other libraries using it should also call
-        * this in their constructor in case it gets executed before this one.
+        * Initialize the shared state of the fd tracker.
         */
-       lttng_ust_init_fd_tracker();
+       lttng_ust_fd_tracker_init();
 }
index 394f197079b0d323a0cb312bde86f5ec4a88493d..75b3a25e52b260777f4370e591e0c006168a267f 100644 (file)
@@ -16,6 +16,7 @@
 #include <lttng/ust-ctl.h>
 #include <lttng/ust-abi.h>
 #include <lttng/ust-endian.h>
+#include <lttng/ust-common.h>
 
 #include "common/logging.h"
 #include "common/ustcomm.h"
@@ -2901,11 +2902,16 @@ int ustctl_counter_clear(struct ustctl_daemon_counter *counter,
 }
 
 static
-void ustctl_init(void)
+void lttng_ust_ctl_ctor(void)
        __attribute__((constructor));
 static
-void ustctl_init(void)
+void lttng_ust_ctl_ctor(void)
 {
+       /*
+        * Call the liblttng-ust-common constructor to ensure it runs first.
+        */
+       lttng_ust_common_ctor();
+
        lttng_ust_clock_init();
        lttng_ust_ring_buffer_clients_init();
        lttng_ust_counter_clients_init();
index 145240d6c94ae8ba3a5872cd5a08ad32d5fad034..23c4d13682a3c17fe63323b4b45327b487931d60 100644 (file)
@@ -8,6 +8,8 @@ liblttng_ust_fd_la_SOURCES = \
 
 liblttng_ust_fd_la_LIBADD = \
        $(top_builddir)/src/lib/lttng-ust/liblttng-ust.la \
+       $(top_builddir)/src/lib/lttng-ust-common/liblttng-ust-common.la \
+       $(top_builddir)/src/common/libcommon.la \
        $(DL_LIBS)
 
 liblttng_ust_fd_la_LDFLAGS = -version-info $(LTTNG_UST_LIBRARY_VERSION)
index 5efc0acde29583479bee79d1f3a251ab854507f7..56efb457269c2f7e9f85478a65b2daab61b6d7ff 100644 (file)
@@ -9,14 +9,25 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include "common/ust-fd.h"
 #include <dlfcn.h>
 
+#include <lttng/ust-common.h>
+
 #include "common/macros.h"
+#include "common/ust-fd.h"
 
 static int (*__lttng_ust_fd_plibc_close)(int fd);
 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream);
 
+static
+void _lttng_ust_fd_ctor(void)
+       __attribute__((constructor));
+static
+void _lttng_ust_fd_ctor(void)
+{
+       lttng_ust_common_ctor();
+}
+
 static
 int _lttng_ust_fd_libc_close(int fd)
 {
index a93c7c1a016742ef54a7b87eedb1bbb2bd9a2e50..0306939960ef2a0e1104b4a9a6f8ad417fafc9e7 100644 (file)
@@ -428,7 +428,7 @@ void lttng_ust_fixup_malloc_nesting_tls(void)
        asm volatile ("" : : "m" (URCU_TLS(malloc_nesting)));
 }
 
-void lttng_ust_libc_wrapper_malloc_init(void)
+void lttng_ust_libc_wrapper_malloc_ctor(void)
 {
        /* Initialization already done */
        if (cur_alloc.calloc) {
index e43c1aeb312967aefaec9e52757dad447fb7034e..b84604d19ee6028c5b491448fd2ac7b2633742be 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <lttng/tracepoint.h>
 #include <lttng/ust-abi.h>     /* for LTTNG_UST_ABI_SYM_NAME_LEN */
+#include <lttng/ust-common.h>
 
 #include "common/logging.h"
 #include "common/macros.h"
@@ -963,6 +964,7 @@ void lttng_ust_tp_init(void)
        if (uatomic_xchg(&initialized, 1) == 1)
                return;
        lttng_ust_logging_init();
+       lttng_ust_common_ctor();
        check_weak_hidden();
 }
 
index cd5b378c49be203d8d9fa72e37dc43f327fa86e3..2f96167e419601bf48e75c3ccc87f4b65a606ffa 100644 (file)
@@ -37,6 +37,7 @@
 #include <lttng/ust-libc-wrapper.h>
 #include <lttng/ust-thread.h>
 #include <lttng/ust-tracer.h>
+#include <lttng/ust-common.h>
 #include <urcu/tls-compat.h>
 #include "common/compat/futex.h"
 #include "common/ustcomm.h"
@@ -2061,7 +2062,7 @@ quit:
  * Weak symbol to call when the ust malloc wrapper is not loaded.
  */
 __attribute__((weak))
-void lttng_ust_libc_wrapper_malloc_init(void)
+void lttng_ust_libc_wrapper_malloc_ctor(void)
 {
 }
 
@@ -2070,10 +2071,10 @@ void lttng_ust_libc_wrapper_malloc_init(void)
  * sessiond by polling the application common named pipe.
  */
 static
-void lttng_ust_init(void)
+void lttng_ust_ctor(void)
        __attribute__((constructor));
 static
-void lttng_ust_init(void)
+void lttng_ust_ctor(void)
 {
        struct timespec constructor_timeout;
        sigset_t sig_all_blocked, orig_parent_mask;
@@ -2131,8 +2132,10 @@ void lttng_ust_init(void)
        lttng_ust_logging_init();
        lttng_ust_getenv_init();
 
+       /* Call the liblttng-ust-common constructor. */
+       lttng_ust_common_ctor();
+
        lttng_ust_tp_init();
-       lttng_ust_init_fd_tracker();
        lttng_ust_clock_init();
        lttng_ust_getcpu_plugin_init();
        lttng_ust_statedump_init();
@@ -2142,7 +2145,7 @@ void lttng_ust_init(void)
        /*
         * Invoke ust malloc wrapper init before starting other threads.
         */
-       lttng_ust_libc_wrapper_malloc_init();
+       lttng_ust_libc_wrapper_malloc_ctor();
 
        timeout_mode = get_constructor_timeout(&constructor_timeout);
 
@@ -2445,7 +2448,7 @@ void lttng_ust_after_fork_parent(sigset_t *restore_sigset)
  * After fork, in the child, we need to cleanup all the leftover state,
  * except the worker thread which already magically disappeared thanks
  * to the weird Linux fork semantics. After tyding up, we call
- * lttng_ust_init() again to start over as a new PID.
+ * lttng_ust_ctor() again to start over as a new PID.
  *
  * This is meant for forks() that have tracing in the child between the
  * fork and following exec call (if there is any).
@@ -2466,7 +2469,7 @@ void lttng_ust_after_fork_child(sigset_t *restore_sigset)
        lttng_ust_cleanup(0);
        /* Release mutexes and reenable signals */
        ust_after_fork_common(restore_sigset);
-       lttng_ust_init();
+       lttng_ust_ctor();
 }
 
 void lttng_ust_after_setns(void)
This page took 0.03209 seconds and 4 git commands to generate.