Fix static provider linking: introduce TRACEPOINT_PROBE_DYNAMIC_LINKAGE
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 4 Feb 2012 19:37:34 +0000 (14:37 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 4 Feb 2012 19:37:34 +0000 (14:37 -0500)
We need to emit an unresolved symbol in the case where the provider is
put into a statically linked library, otherwise the linker removes the
provider object.

See README for details.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
README
include/lttng/tracepoint-types.h
include/lttng/tracepoint.h
include/lttng/ust-tracepoint-event.h
tests/demo/demo.c

diff --git a/README b/README
index a3979763353d079bd8a6c1c1261f13446d3ca382..23b831978a748d7acc1cd63e16c11ad7c9082714 100644 (file)
--- a/README
+++ b/README
@@ -56,14 +56,31 @@ USAGE:
 
   - Create an instrumentation header following the tracepoint examples.
     See lttng/tracepoint.h, and examples.
-  - Either compile the Tracepoint probes with the application:
+
+  There are 2 ways to compile the Tracepoint Provider with the
+  application: either statically or dynamically. Please follow
+  carefully:
+
+  1.1) Compile the Tracepoint provider with the application, either
+       directly or through a static library (.a):
+    - Into exactly one object of your application: define
+      "TRACEPOINT_DEFINE" and include the tracepoint provider.
     - Use "-I." for the compilation unit containing the tracepoint
       provider include (e.g. tp.c).
     - Link application with "-ldl -llttng-ust".
+    - Include the tracepoint provider header into all C files using
+      the provider.
     - Example:
         tests/hello/  hello.c tp.c ust_tests_hello.h Makefile.example
-  - Or compile the Tracepoint probes separately from the application,
-    using dynamic linking:
+
+  2) Compile the Tracepoint Provider separately from the application,
+     using dynamic linking:
+    - Into exactly one object of your application: define
+      "TRACEPOINT_DEFINE" _and_ also define
+      "TRACEPOINT_PROBE_DYNAMIC_LINKAGE", then include the tracepoint
+      provider header.
+    - Include the tracepoint provider header into all instrumented C
+      files that use the provider.
     - Compile the tracepoint provider with "-I.".
     - Link the tracepoint provider with "-llttng-ust".
     - Link application with "-ldl".
@@ -72,6 +89,7 @@ USAGE:
       needed.
     - Example:
       - tests/demo/   demo.c  tp*.c ust_tests_demo*.h demo-trace
+
   - Note about dlopen() usage: due to locking side-effects due to the
     way libc lazily resolves Thread-Local Storage (TLS) symbols when a
     library is dlopen'd, linking the tracepoint probe or liblttng-ust
index b6353c80a3827130e0e74c48db099e7461e88673..37a7662b605f8a9deb5bb3082bd65437c11312e7 100644 (file)
@@ -23,6 +23,7 @@ struct tracepoint {
        const char *name;
        int state;
        struct tracepoint_probe *probes;
+       int *tracepoint_provider_ref;
 };
 
 #endif /* _LTTNG_TRACEPOINT_TYPES_H */
index fa39b272d8ff19527fd065c7e393ac947b46c8b2..887c9deea87b4653d944f8b9658b454d380de8bb 100644 (file)
@@ -152,18 +152,35 @@ extern int __tracepoint_probe_unregister(const char *name, void *func, void *dat
 
 #ifdef TRACEPOINT_DEFINE
 
+/*
+ * When TRACEPOINT_PROBE_DYNAMIC_LINKAGE is defined, we do not emit a
+ * unresolved symbol that requires the provider to be linked in. When
+ * TRACEPOINT_PROBE_DYNAMIC_LINKAGE is not defined, we emit an
+ * unresolved symbol that depends on having the provider linked in,
+ * otherwise the linker complains. This deals with use of static
+ * libraries, ensuring that the linker does not remove the provider
+ * object from the executable.
+ */
+#ifdef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
+#define _TRACEPOINT_UNDEFINED_REF(provider)
+#else  /* TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
+#define _TRACEPOINT_UNDEFINED_REF(provider)    \
+       &__tracepoint_provider_##provider,
+#endif /* TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
+
 /*
  * Note: to allow PIC code, we need to allow the linker to update the pointers
  * in the __tracepoints_ptrs section.
  * Therefore, this section is _not_ const (read-only).
  */
 #define _DEFINE_TRACEPOINT(provider, name)                                     \
+       extern int __tracepoint_provider_##provider;                            \
        static const char __tp_strtab_##provider##___##name[]                   \
                __attribute__((section("__tracepoints_strings"))) =             \
                        #provider ":" #name;                                    \
        struct tracepoint __tracepoint_##provider##___##name                    \
                __attribute__((section("__tracepoints"))) =                     \
-                       { __tp_strtab_##provider##___##name, 0, NULL };         \
+                       { __tp_strtab_##provider##___##name, 0, NULL, _TRACEPOINT_UNDEFINED_REF(provider) };            \
        static struct tracepoint * __tracepoint_ptr_##provider##___##name       \
                __attribute__((used, section("__tracepoints_ptrs"))) =          \
                        &__tracepoint_##provider##___##name;
index dbd0485565cfd18e2d920811d5dbbb3cb4c79f93..18aa4e44f1439b56d194601e5c1d4c89aee4ad9c 100644 (file)
@@ -514,11 +514,13 @@ static struct lttng_probe_desc _TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PR
  * Stage 9 of tracepoint event generation.
  *
  * Register/unregister probes at module load/unload.
+ *
+ * Generate the constructor as an externally visible symbol for use when
+ * linking the probe statically.
  */
 
 /* Reset all macros within TRACEPOINT_EVENT */
 #include <lttng/ust-tracepoint-event-reset.h>
-
 static void __attribute__((constructor))
 _TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
 {
@@ -533,3 +535,5 @@ _TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
 {
        ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
 }
+
+int _TP_COMBINE_TOKENS(__tracepoint_provider_, TRACEPOINT_PROVIDER);
index 66a90607fee7dc3bab04ccb5e269f0eec4c38a6c..e2b61c2e49e82eb5e09be395a380690dec264269 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 
 #define TRACEPOINT_DEFINE
+#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
 #include "ust_tests_demo.h"
 #include "ust_tests_demo2.h"
 #include "ust_tests_demo3.h"
This page took 0.027734 seconds and 4 git commands to generate.