Allow weak binding from instrumented apps to liblttng-ust
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 27 Nov 2011 10:08:02 +0000 (10:08 +0000)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 27 Nov 2011 10:08:02 +0000 (10:08 +0000)
This introduce the TRACEPOINT_DEFINE macro. See demo program for usage
example. This is an API change for instrumentation.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/lttng/tracepoint-event.h
include/lttng/tracepoint.h
liblttng-ust/probes/lttng-probe-ust.c
tests/demo/Makefile.am
tests/demo/README
tests/demo/demo.c
tests/demo/ust_tests_demo.h
tests/fork/fork.c
tests/fork/fork2.c
tests/hello/hello.c

index 94b0bcb17623117fb0a0c541e3eab7a9c561d8f6..9d757940d381a63b1ad8efd52562a893aa2de5b2 100644 (file)
@@ -21,8 +21,7 @@ extern "C" {
 #define __tp_stringify(x)      __tp_stringify1(x)
 
 #undef TRACEPOINT_EVENT_INSTANCE
-#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args)  \
-       _DEFINE_TRACEPOINT(_provider, _name)
+#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args)
 
 #undef TRACEPOINT_EVENT
 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields)             \
index dc7832cb25531b1912d2e0449976da8a58ce5cd3..831447dd64643dc754ec0aa6ad9cd39a12ce7d36 100644 (file)
@@ -17,6 +17,7 @@
 #include <urcu-bp.h>
 #include <tracepoint-types.h>
 #include <urcu/compiler.h>
+#include <dlfcn.h>     /* for dlopen */
 
 #ifdef __cplusplus
 extern "C" {
@@ -108,8 +109,10 @@ static inline void __tracepoint_cb_##provider##___##name(_TP_ARGS_PROTO(__VA_ARG
 {                                                                                      \
        struct tracepoint_probe *__tp_probe;                                            \
                                                                                        \
+       if (!rcu_read_lock_bp)                                                          \
+               return;                                                                 \
        rcu_read_lock_bp();                                                             \
-       __tp_probe = rcu_dereference(__tracepoint_##provider##___##name.probes);        \
+       __tp_probe = rcu_dereference_bp(__tracepoint_##provider##___##name.probes);     \
        if (caa_unlikely(!__tp_probe))                                                  \
                goto end;                                                               \
        do {                                                                            \
@@ -133,6 +136,11 @@ static inline void __tracepoint_unregister_##provider##___##name(char *name,               \
        __tracepoint_probe_unregister(name, func, data);                                \
 }
 
+extern int __tracepoint_probe_register(const char *name, void *func, void *data);
+extern int __tracepoint_probe_unregister(const char *name, void *func, void *data);
+
+#ifdef TRACEPOINT_DEFINE
+
 /*
  * Note: to allow PIC code, we need to allow the linker to update the pointers
  * in the __tracepoints_ptrs section.
@@ -149,11 +157,10 @@ static inline void __tracepoint_unregister_##provider##___##name(char *name,              \
                __attribute__((used, section("__tracepoints_ptrs"))) =          \
                        &__tracepoint_##provider##___##name;
 
-extern int __tracepoint_probe_register(const char *name, void *func, void *data);
-extern int __tracepoint_probe_unregister(const char *name, void *func, void *data);
-extern int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
+static int (*tracepoint_register_lib)(struct tracepoint * const *tracepoints_start,
                int tracepoints_count);
-extern int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
+static int (*tracepoint_unregister_lib)(struct tracepoint * const *tracepoints_start);
+static void *liblttngust_handle;
 
 /*
  * These weak symbols, the constructor, and destructor take care of
@@ -171,6 +178,14 @@ static void __attribute__((constructor)) __tracepoints__init(void)
 {
        if (__tracepoint_registered++)
                return;
+
+       liblttngust_handle = dlopen("liblttng-ust.so", RTLD_NOW | RTLD_GLOBAL);
+       if (!liblttngust_handle)
+               return;
+       tracepoint_register_lib = dlsym(liblttngust_handle,
+                                       "tracepoint_register_lib");
+       tracepoint_unregister_lib = dlsym(liblttngust_handle,
+                                       "tracepoint_unregister_lib");
        tracepoint_register_lib(__start___tracepoints_ptrs,
                                __stop___tracepoints_ptrs -
                                __start___tracepoints_ptrs);
@@ -178,11 +193,25 @@ static void __attribute__((constructor)) __tracepoints__init(void)
 
 static void __attribute__((destructor)) __tracepoints__destroy(void)
 {
+       int ret;
        if (--__tracepoint_registered)
                return;
-       tracepoint_unregister_lib(__start___tracepoints_ptrs);
+       if (tracepoint_unregister_lib)
+               tracepoint_unregister_lib(__start___tracepoints_ptrs);
+       if (liblttngust_handle) {
+               tracepoint_unregister_lib = NULL;
+               tracepoint_register_lib = NULL;
+               ret = dlclose(liblttngust_handle);
+               assert(!ret);
+       }
 }
 
+#else /* TRACEPOINT_DEFINE */
+
+#define _DEFINE_TRACEPOINT(provider, name)
+
+#endif /* #else TRACEPOINT_DEFINE */
+
 #ifdef __cplusplus
 }
 #endif
@@ -273,12 +302,14 @@ static void __attribute__((destructor)) __tracepoints__destroy(void)
  */
 
 #define TRACEPOINT_EVENT(provider, name, args, fields)                 \
-       _DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args))
+       _DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args))           \
+       _DEFINE_TRACEPOINT(provider, name)
 
 #define TRACEPOINT_EVENT_CLASS(provider, name, args, fields)
 
 #define TRACEPOINT_EVENT_INSTANCE(provider, _template, name, args)     \
-       _DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args))
+       _DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args))           \
+       _DEFINE_TRACEPOINT(provider, name)
 
 #endif /* #ifndef TRACEPOINT_EVENT */
 
index a5fba8683fca936a3b8780698ddc3fb59cac6b07..a5594c95ffcf3d9199a578301f747226d82f9dea 100644 (file)
@@ -8,9 +8,6 @@
  * Dual LGPL v2.1/GPL v2 license.
  */
 
-/*
- * Create LTTng tracepoint probes.
- */
+#define TRACEPOINT_DEFINE
 #define TRACEPOINT_CREATE_PROBES
-
 #include "lttng-probe-ust.h"
index 7ed5ce1e02ed8bd290bd3599fabd46ea1e31b6a8..1988dac46951d0dafe80835a0d6a2d1967b1b5a5 100644 (file)
@@ -1,5 +1,20 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include
 
+lib_LTLIBRARIES = liblttng-ust-provider-ust-tests-demo.la \
+       liblttng-ust-provider-ust-tests-demo3.la
+
+#contains ust_tests_demo.h and ust_tests_demo2.h provider probes
+liblttng_ust_provider_ust_tests_demo_la_SOURCES = \
+       tp.c ust_tests_demo.h \
+       tp2.c ust_tests_demo2.h
+liblttng_ust_provider_ust_tests_demo_la_LIBADD = \
+       $(top_builddir)/liblttng-ust/liblttng-ust.la
+
+#contains ust_tests_demo3.h provider probes
+liblttng_ust_provider_ust_tests_demo3_la_SOURCES = \
+       tp3.c ust_tests_demo3.h
+liblttng_ust_provider_ust_tests_demo3_la_LIBADD = \
+       $(top_builddir)/liblttng-ust/liblttng-ust.la
+
 noinst_PROGRAMS = demo
-demo_SOURCES = demo.c tp.c ust_tests_demo.h
-demo_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la
+demo_SOURCES = demo.c ust_tests_demo.h
index 68b11e69fa00e82fffaac5a7b6b44f09e388a1b8..73becb0f47eb1d3b3efb4b7cb5cf12b76bbdb85e 100644 (file)
@@ -1 +1,7 @@
-This is a hello world application used to test the LTTng userspace tracer.
+This is a demo application used to test the LTTng userspace tracer.
+
+demo-trace shell script preloads the provider shared objects before
+executing the demo. Executing "demo" without the shell wrapper will not
+provide any tracing support. This ensures the demo binary can be
+distributed on distros without depending on having liblttng-ust.so in
+place. Note: the "demo" program must be compiled with "-ldl".
index eff50529637510f24af8a79ed9e7f0a15d7f77ec..3ab8bfd8fbf3e55dbed4c3f4cd36cbf7f6ad2a50 100644 (file)
 #include <string.h>
 #include <arpa/inet.h>
 
+#define TRACEPOINT_DEFINE
 #include "ust_tests_demo.h"
+#include "ust_tests_demo2.h"
+#include "ust_tests_demo3.h"
 
 int main(int argc, char **argv)
 {
@@ -50,10 +53,11 @@ int main(int argc, char **argv)
        tracepoint(ust_tests_demo, starting, 123);
        for (i = 0; i < 5; i++) {
                netint = htonl(i);
-               tracepoint(ust_tests_demo, loop, i, netint, values,
+               tracepoint(ust_tests_demo2, loop, i, netint, values,
                           text, strlen(text), dbl, flt);
        }
        tracepoint(ust_tests_demo, done, 456);
+       tracepoint(ust_tests_demo3, done, 42);
        fprintf(stderr, " done.\n");
        return 0;
 }
index d74932825d2cf1dae8a9c31bdabeee0e00f24487..355867d2eb6d1c68fb5ffca6af1a2dd810190cef 100644 (file)
@@ -29,29 +29,6 @@ TRACEPOINT_LOGLEVEL_ENUM(
        tp_loglevel(debug, 2)
 )
 
-TRACEPOINT_EVENT(ust_tests_demo, loop,
-       TP_ARGS(int, anint, int, netint, long *, values,
-                char *, text, size_t, textlen,
-                double, doublearg, float, floatarg),
-       TP_FIELDS(
-               ctf_integer(int, intfield, anint)
-               ctf_integer_hex(int, intfield2, anint)
-               ctf_integer(long, longfield, anint)
-               ctf_integer_network(int, netintfield, netint)
-               ctf_integer_network_hex(int, netintfieldhex, netint)
-               ctf_array(long, arrfield1, values, 3)
-               ctf_array_text(char, arrfield2, text, 10)
-               ctf_sequence(char, seqfield1, text,
-                            size_t, textlen)
-               ctf_sequence_text(char, seqfield2, text,
-                            size_t, textlen)
-               ctf_string(stringfield, text)
-               ctf_float(float, floatfield, floatarg)
-               ctf_float(double, doublefield, doublearg)
-       )
-)
-TRACEPOINT_LOGLEVEL(ust_tests_demo, loop, debug)
-
 TRACEPOINT_EVENT(ust_tests_demo, starting,
        TP_ARGS(int, value),
        TP_FIELDS(
index f15a692535d165a1d0ecb7c914e3f18b5d3e7120..0d2906901e4ccb1b8ab0320bd3e610253b9d9157 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/types.h>
 #include <stdlib.h>
 
+#define TRACEPOINT_DEFINE
 #define TRACEPOINT_CREATE_PROBES
 #include "ust_tests_fork.h"
 
index 5727f3f4751cd83112eb1e2a482e0fa8dbcc1820..49baba8a727e21bd19613b09eb58e5ff03e23da2 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#define TRACEPOINT_DEFINE
 #define TRACEPOINT_CREATE_PROBES
 #include "ust_tests_fork.h"
 
index 68fca7a721ef06dfb0fe9960edfe177d25cb2803..da33407b0db671aeb3e37bf5c332af4b074c543a 100644 (file)
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <arpa/inet.h>
 
+#define TRACEPOINT_DEFINE
 #include "ust_tests_hello.h"
 
 void inthandler(int sig)
This page took 0.03108 seconds and 4 git commands to generate.