From 40b2b5a43e4c2067849770b3379ca5360facacd5 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 8 Dec 2011 08:28:42 -0500 Subject: [PATCH] Fix dynamic linking with GNU gold linker Programs that were not linked explicitely with -lurcu-bp generated no trace output when linked with GNU gold, because the urcu-bp library symbols were not used to populate the weak urcu-bp symbols used by the program. (even though they worked fine with the standard GNU ld). Solve this by creating those wrapper symbols into tracepoint.c, and explicitely get them with dlsym() in the constructor. Signed-off-by: Mathieu Desnoyers --- include/Makefile.am | 1 + include/lttng/tracepoint-rcu.h | 49 ++++++++++++++++++++++++++++++++++ include/lttng/tracepoint.h | 31 +++++++++++++-------- liblttng-ust/tracepoint.c | 22 +++++++++++++++ tests/demo/demo.c | 1 + tests/hello.cxx/hello.cpp | 1 + tests/hello/hello.c | 1 + 7 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 include/lttng/tracepoint-rcu.h diff --git a/include/Makefile.am b/include/Makefile.am index 13fa44b3..efaffc85 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,5 +1,6 @@ nobase_include_HEADERS = \ lttng/tracepoint.h \ + lttng/tracepoint-rcu.h \ lttng/tracepoint-types.h \ lttng/tracepoint-event.h \ lttng/ust-tracepoint-event.h \ diff --git a/include/lttng/tracepoint-rcu.h b/include/lttng/tracepoint-rcu.h new file mode 100644 index 00000000..852cfe48 --- /dev/null +++ b/include/lttng/tracepoint-rcu.h @@ -0,0 +1,49 @@ +#ifndef _LTTNG_TRACEPOINT_RCU_H +#define _LTTNG_TRACEPOINT_RCU_H + +/* + * Copyright (c) 2011 - Mathieu Desnoyers + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED + * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program + * for any purpose, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + * + * This file allows weak linking on tracepoint RCU symbols for non-LGPL + * code. + */ + +#include + +#ifdef _LGPL_SOURCE + +#include + +#define TP_RCU_LINK_TEST() 1 +#define tp_rcu_read_lock_bp rcu_read_lock_bp +#define tp_rcu_read_unlock_bp rcu_read_unlock_bp +#define tp_rcu_dereference_bp rcu_dereference_bp + +#else /* _LGPL_SOURCE */ + +#define TP_RCU_LINK_TEST() tp_rcu_read_lock_bp + +/* Symbols looked up with dlsym */ +static void (*tp_rcu_read_lock_bp)(void) __attribute__((unused)); +static void (*tp_rcu_read_unlock_bp)(void) __attribute__((unused)); +static void *(*tp_rcu_dereference_sym_bp)(void *p) __attribute__((unused)); + +#define tp_rcu_dereference_bp(p) \ + ({ \ + typeof(p) _________p1 = URCU_FORCE_CAST(typeof(p), \ + tp_rcu_dereference_sym_bp(URCU_FORCE_CAST(void *, p))); \ + (_________p1); \ + }) + +#endif /* _LGPL_SOURCE */ + +#endif /* _LTTNG_TRACEPOINT_RCU_H */ diff --git a/include/lttng/tracepoint.h b/include/lttng/tracepoint.h index a224caeb..2942c175 100644 --- a/include/lttng/tracepoint.h +++ b/include/lttng/tracepoint.h @@ -14,10 +14,11 @@ * modified is included with the above copyright notice. */ -#include #include +#include #include #include /* for dlopen */ +#include #ifdef __cplusplus extern "C" { @@ -113,22 +114,16 @@ extern "C" { #define _TP_ARGS_DATA_VAR(...) _TP_DATA_VAR_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__) #define _TP_PARAMS(...) __VA_ARGS__ -#ifdef _LGPL_SOURCE -#define _TP_RCU_LINK_TEST() 1 -#else -#define _TP_RCU_LINK_TEST() rcu_read_lock_bp -#endif - #define _DECLARE_TRACEPOINT(provider, name, ...) \ extern struct tracepoint __tracepoint_##provider##___##name; \ static inline void __tracepoint_cb_##provider##___##name(_TP_ARGS_PROTO(__VA_ARGS__)) \ { \ struct tracepoint_probe *__tp_probe; \ \ - if (!_TP_RCU_LINK_TEST()) \ + if (!TP_RCU_LINK_TEST()) \ return; \ - rcu_read_lock_bp(); \ - __tp_probe = rcu_dereference_bp(__tracepoint_##provider##___##name.probes); \ + tp_rcu_read_lock_bp(); \ + __tp_probe = tp_rcu_dereference_bp(__tracepoint_##provider##___##name.probes); \ if (caa_unlikely(!__tp_probe)) \ goto end; \ do { \ @@ -139,7 +134,7 @@ static inline void __tracepoint_cb_##provider##___##name(_TP_ARGS_PROTO(__VA_ARG (_TP_ARGS_DATA_VAR(__VA_ARGS__)); \ } while ((++__tp_probe)->func); \ end: \ - rcu_read_unlock_bp(); \ + tp_rcu_read_unlock_bp(); \ } \ static inline void __tracepoint_register_##provider##___##name(char *name, \ void *func, void *data) \ @@ -206,6 +201,20 @@ static void __attribute__((constructor)) __tracepoints__init(void) URCU_FORCE_CAST(int (*)(struct tracepoint * const *), dlsym(liblttngust_handle, "tracepoint_unregister_lib")); +#ifndef _LGPL_SOURCE + tp_rcu_read_lock_bp = + URCU_FORCE_CAST(void (*)(void), + dlsym(liblttngust_handle, + "tp_rcu_read_lock_bp")); + tp_rcu_read_unlock_bp = + URCU_FORCE_CAST(void (*)(void), + dlsym(liblttngust_handle, + "tp_rcu_read_unlock_bp")); + tp_rcu_dereference_sym_bp = + URCU_FORCE_CAST(void *(*)(void *p), + dlsym(liblttngust_handle, + "tp_rcu_dereference_sym_bp")); +#endif tracepoint_register_lib(__start___tracepoints_ptrs, __stop___tracepoints_ptrs - __start___tracepoints_ptrs); diff --git a/liblttng-ust/tracepoint.c b/liblttng-ust/tracepoint.c index 3a50df66..0393aa46 100644 --- a/liblttng-ust/tracepoint.c +++ b/liblttng-ust/tracepoint.c @@ -579,3 +579,25 @@ void exit_tracepoint(void) { initialized = 0; } + +/* + * Create the wrapper symbols. + */ +#undef tp_rcu_read_lock_bp +#undef tp_rcu_read_unlock_bp +#undef tp_rcu_dereference_bp + +void tp_rcu_read_lock_bp(void) +{ + rcu_read_lock_bp(); +} + +void tp_rcu_read_unlock_bp(void) +{ + rcu_read_unlock_bp(); +} + +void *tp_rcu_dereference_sym_bp(void *p) +{ + return rcu_dereference_bp(p); +} diff --git a/tests/demo/demo.c b/tests/demo/demo.c index 3ab8bfd8..66a90607 100644 --- a/tests/demo/demo.c +++ b/tests/demo/demo.c @@ -27,6 +27,7 @@ #include #include #include +#include #define TRACEPOINT_DEFINE #include "ust_tests_demo.h" diff --git a/tests/hello.cxx/hello.cpp b/tests/hello.cxx/hello.cpp index da33407b..584d3f7b 100644 --- a/tests/hello.cxx/hello.cpp +++ b/tests/hello.cxx/hello.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #define TRACEPOINT_DEFINE #include "ust_tests_hello.h" diff --git a/tests/hello/hello.c b/tests/hello/hello.c index da33407b..584d3f7b 100644 --- a/tests/hello/hello.c +++ b/tests/hello/hello.c @@ -27,6 +27,7 @@ #include #include #include +#include #define TRACEPOINT_DEFINE #include "ust_tests_hello.h" -- 2.34.1